git.delta.rocks / unique-network / refs/commits / 6d1c84661df9

difftreelog

source

js-packages/tests/maintenance.seqtest.ts17.1 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import type {IKeyringPair} from '@polkadot/types/types';18import {ApiPromise} from '@polkadot/api';19import {expect, itSched, itSub, Pallets, requirePalletsOrSkip, usingPlaygrounds} from './util/index.js';20import {itEth} from './eth/util/index.js';21import {main as correctState} from './migrations/correctStateAfterMaintenance.js';22import type {PalletBalancesIdAmount} from '@polkadot/types/lookup';23import type {Vec} from '@polkadot/types-codec';2425async function maintenanceEnabled(api: ApiPromise): Promise<boolean> {26  return (await api.query.maintenance.enabled()).toJSON() as boolean;27}2829describe('Integration Test: Maintenance Functionality', () => {30  let superuser: IKeyringPair;31  let donor: IKeyringPair;32  let bob: IKeyringPair;3334  before(async function() {35    await usingPlaygrounds(async (helper, privateKey) => {36      requirePalletsOrSkip(this, helper, [Pallets.Maintenance]);37      superuser = await privateKey('//Alice');38      donor = await privateKey({url: import.meta.url});39      [bob] = await helper.arrange.createAccounts([10000n], donor);4041    });42  });4344  describe('Maintenance Mode', () => {45    before(async function() {46      await usingPlaygrounds(async (helper) => {47        if(await maintenanceEnabled(helper.getApi())) {48          console.warn('\tMaintenance mode was left enabled BEFORE the test suite! Disabling it now.');49          await expect(helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', [])).to.be.fulfilled;50        }51      });52    });5354    itSub('Allows superuser to enable and disable maintenance mode - and disallows anyone else', async ({helper}) => {55      // Make sure non-sudo can't enable maintenance mode56      await expect(helper.executeExtrinsic(superuser, 'api.tx.maintenance.enable', []), 'on commoner enabling MM')57        .to.be.rejectedWith(/BadOrigin/);5859      // Set maintenance mode60      await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.enable', []);61      expect(await maintenanceEnabled(helper.getApi()), 'MM is OFF when it should be ON').to.be.true;6263      // Make sure non-sudo can't disable maintenance mode64      await expect(helper.executeExtrinsic(bob, 'api.tx.maintenance.disable', []), 'on commoner disabling MM')65        .to.be.rejectedWith(/BadOrigin/);6667      // Disable maintenance mode68      await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', []);69      expect(await maintenanceEnabled(helper.getApi()), 'MM is ON when it should be OFF').to.be.false;70    });7172    itSub('MM blocks unique pallet calls', async ({helper}) => {73      // Can create an NFT collection before enabling the MM74      const nftCollection = await helper.nft.mintCollection(bob, {75        tokenPropertyPermissions: [{76          key: 'test', permission: {77            collectionAdmin: true,78            tokenOwner: true,79            mutable: true,80          },81        }],82      });8384      // Can mint an NFT before enabling the MM85      const nft = await nftCollection.mintToken(bob);8687      // Can create an FT collection before enabling the MM88      const ftCollection = await helper.ft.mintCollection(superuser);8990      // Can mint an FT before enabling the MM91      await expect(ftCollection.mint(superuser)).to.be.fulfilled;9293      await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.enable', []);94      expect(await maintenanceEnabled(helper.getApi()), 'MM is OFF when it should be ON').to.be.true;9596      // Unable to create a collection when the MM is enabled97      await expect(helper.nft.mintCollection(superuser), 'cudo forbidden stuff')98        .to.be.rejectedWith(/Invalid Transaction: Transaction call is not expected/);99100      // Unable to set token properties when the MM is enabled101      await expect(nft.setProperties(102        bob,103        [{key: 'test', value: 'test-val'}],104      )).to.be.rejectedWith(/Invalid Transaction: Transaction call is not expected/);105106      // Unable to mint an NFT when the MM is enabled107      await expect(nftCollection.mintToken(superuser))108        .to.be.rejectedWith(/Invalid Transaction: Transaction call is not expected/);109110      // Unable to mint an FT when the MM is enabled111      await expect(ftCollection.mint(superuser))112        .to.be.rejectedWith(/Invalid Transaction: Transaction call is not expected/);113114      await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', []);115      expect(await maintenanceEnabled(helper.getApi()), 'MM is ON when it should be OFF').to.be.false;116117      // Can create a collection after disabling the MM118      await expect(helper.nft.mintCollection(bob), 'MM is disabled, the collection should be created').to.be.fulfilled;119120      // Can set token properties after disabling the MM121      await nft.setProperties(bob, [{key: 'test', value: 'test-val'}]);122123      // Can mint an NFT after disabling the MM124      await nftCollection.mintToken(bob);125126      // Can mint an FT after disabling the MM127      await ftCollection.mint(superuser);128    });129130    itSub.ifWithPallets('MM blocks unique pallet calls (Re-Fungible)', [Pallets.ReFungible], async ({helper}) => {131      // Can create an RFT collection before enabling the MM132      const rftCollection = await helper.rft.mintCollection(superuser);133134      // Can mint an RFT before enabling the MM135      await expect(rftCollection.mintToken(superuser)).to.be.fulfilled;136137      await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.enable', []);138      expect(await maintenanceEnabled(helper.getApi()), 'MM is OFF when it should be ON').to.be.true;139140      // Unable to mint an RFT when the MM is enabled141      await expect(rftCollection.mintToken(superuser))142        .to.be.rejectedWith(/Invalid Transaction: Transaction call is not expected/);143144      await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', []);145      expect(await maintenanceEnabled(helper.getApi()), 'MM is ON when it should be OFF').to.be.false;146147      // Can mint an RFT after disabling the MM148      await rftCollection.mintToken(superuser);149    });150151    itSub('MM allows native token transfers and RPC calls', async ({helper}) => {152      // We can use RPC before the MM is enabled153      const totalCount = await helper.collection.getTotalCount();154155      // We can transfer funds before the MM is enabled156      await expect(helper.balance.transferToSubstrate(superuser, bob.address, 2n)).to.be.fulfilled;157158      await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.enable', []);159      expect(await maintenanceEnabled(helper.getApi()), 'MM is OFF when it should be ON').to.be.true;160161      // RPCs work while in maintenance162      expect(await helper.collection.getTotalCount()).to.be.deep.equal(totalCount);163164      // We still able to transfer funds165      await expect(helper.balance.transferToSubstrate(bob, superuser.address, 1n)).to.be.fulfilled;166167      await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', []);168      expect(await maintenanceEnabled(helper.getApi()), 'MM is ON when it should be OFF').to.be.false;169170      // RPCs work after maintenance171      expect(await helper.collection.getTotalCount()).to.be.deep.equal(totalCount);172173      // Transfers work after maintenance174      await expect(helper.balance.transferToSubstrate(bob, superuser.address, 1n)).to.be.fulfilled;175    });176177    itSched.ifWithPallets('MM blocks scheduled calls and the scheduler itself', [Pallets.UniqueScheduler], async (scheduleKind, {helper}) => {178      const collection = await helper.nft.mintCollection(bob);179180      const nftBeforeMM = await collection.mintToken(bob);181      const nftDuringMM = await collection.mintToken(bob);182      const nftAfterMM = await collection.mintToken(bob);183184      const [185        scheduledIdBeforeMM,186        scheduledIdDuringMM,187        scheduledIdBunkerThroughMM,188        scheduledIdAttemptDuringMM,189        scheduledIdAfterMM,190      ] = scheduleKind == 'named'191        ? helper.arrange.makeScheduledIds(5)192        : new Array(5);193194      const blocksToWait = 6;195196      // Scheduling works before the maintenance197      await helper.scheduler.scheduleAfter(blocksToWait, {scheduledId: scheduledIdBeforeMM})198        .nft.transferToken(bob, collection.collectionId, nftBeforeMM.tokenId, {Substrate: superuser.address});199200201      await helper.wait.newBlocks(blocksToWait + 1);202      expect(await nftBeforeMM.getOwner()).to.be.deep.equal({Substrate: superuser.address});203204      // Schedule a transaction that should occur *during* the maintenance205      await helper.scheduler.scheduleAfter(blocksToWait, {scheduledId: scheduledIdDuringMM})206        .nft.transferToken(bob, collection.collectionId, nftDuringMM.tokenId, {Substrate: superuser.address});207208209      // Schedule a transaction that should occur *after* the maintenance210      await helper.scheduler.scheduleAfter(blocksToWait * 2, {scheduledId: scheduledIdBunkerThroughMM})211        .nft.transferToken(bob, collection.collectionId, nftDuringMM.tokenId, {Substrate: superuser.address});212213214      await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.enable', []);215      expect(await maintenanceEnabled(helper.getApi()), 'MM is OFF when it should be ON').to.be.true;216217      await helper.wait.newBlocks(blocksToWait + 1);218      // The owner should NOT change since the scheduled transaction should be rejected219      expect(await nftDuringMM.getOwner()).to.be.deep.equal({Substrate: bob.address});220221      // Any attempts to schedule a tx during the MM should be rejected222      await expect(helper.scheduler.scheduleAfter(blocksToWait, {scheduledId: scheduledIdAttemptDuringMM})223        .nft.transferToken(bob, collection.collectionId, nftDuringMM.tokenId, {Substrate: superuser.address}))224        .to.be.rejectedWith(/Invalid Transaction: Transaction call is not expected/);225226      await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', []);227      expect(await maintenanceEnabled(helper.getApi()), 'MM is ON when it should be OFF').to.be.false;228229      // Scheduling works after the maintenance230      await helper.scheduler.scheduleAfter(blocksToWait, {scheduledId: scheduledIdAfterMM})231        .nft.transferToken(bob, collection.collectionId, nftAfterMM.tokenId, {Substrate: superuser.address});232233      await helper.wait.newBlocks(blocksToWait + 1);234235      expect(await nftAfterMM.getOwner()).to.be.deep.equal({Substrate: superuser.address});236      // The owner of the token scheduled for transaction *before* maintenance should now change *after* maintenance237      expect(await nftDuringMM.getOwner()).to.be.deep.equal({Substrate: superuser.address});238    });239240    itEth('Disallows Ethereum transactions to execute while in maintenance', async ({helper}) => {241      const owner = await helper.eth.createAccountWithBalance(donor);242      const receiver = helper.eth.createAccount();243244      const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, 'A', 'B', 'C', '');245246      // Set maintenance mode247      await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.enable', []);248      expect(await maintenanceEnabled(helper.getApi()), 'MM is OFF when it should be ON').to.be.true;249250      const contract = await helper.ethNativeContract.collection(collectionAddress, 'nft', owner);251      const tokenId = await contract.methods.nextTokenId().call();252      expect(tokenId).to.be.equal('1');253254      await expect(contract.methods.mintWithTokenURI(receiver, 'Test URI').send())255        .to.be.rejectedWith(/Returned error: unknown error/);256257      await expect(contract.methods.ownerOf(tokenId).call()).rejectedWith(/token not found/);258259      // Disable maintenance mode260      await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', []);261      expect(await maintenanceEnabled(helper.getApi()), 'MM is ON when it should be OFF').to.be.false;262    });263264    itSub('Allows to enable and disable MM repeatedly', async ({helper}) => {265      // Set maintenance mode266      await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.enable', []);267      await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.enable', []);268      expect(await maintenanceEnabled(helper.getApi()), 'MM is OFF when it should be ON').to.be.true;269270      // Disable maintenance mode271      await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', []);272      await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', []);273      expect(await maintenanceEnabled(helper.getApi()), 'MM is ON when it should be OFF').to.be.false;274    });275276    afterEach(async () => {277      await usingPlaygrounds(async helper => {278        if(helper.fetchMissingPalletNames([Pallets.Maintenance]).length != 0) return;279        if(await maintenanceEnabled(helper.getApi())) {280          console.warn('\tMaintenance mode was left enabled AFTER a test has finished! Be careful. Disabling it now.');281          await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', []);282        }283        expect(await maintenanceEnabled(helper.getApi()), 'Disastrous! Exited the test suite with maintenance mode on.').to.be.false;284      });285    });286  });287288  describe('Integration Test: Maintenance mode & App Promo', () => {289    let superuser: IKeyringPair;290291    before(async function() {292      await usingPlaygrounds(async (helper, privateKey) => {293        requirePalletsOrSkip(this, helper, [Pallets.Maintenance]);294        superuser = await privateKey('//Alice');295      });296    });297298    describe('Test AppPromo script for check state after Maintenance mode', () => {299      before(async function () {300        await usingPlaygrounds(async (helper) => {301          if(await maintenanceEnabled(helper.getApi())) {302            console.warn('\tMaintenance mode was left enabled BEFORE the test suite! Disabling it now.');303            await expect(helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', [])).to.be.fulfilled;304          }305        });306      });307      itSub('Can find and fix inconsistent state', async ({helper}) => {308        const api = helper.getApi();309310        await helper.executeExtrinsic(superuser, 'api.tx.sudo.sudo', [api.tx.system.setStorage([311          // pendingUnstake(1 -> [superuser.address, 100UNQ])312          ['0x42b67acb8bd223c60d0c8f621ffefc0ae280fa2db99bd3827aac976de75af95f5153cb1f00942ff401000000',313            '0x04d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d000010632d5ec76b0500000000000000'],314          // pendingUnstake(2 -> [superuser.address, 100UNQ])315          ['0x42b67acb8bd223c60d0c8f621ffefc0ae280fa2db99bd3827aac976de75af95f9eb2dcce60f37a2702000000',316            '0x04d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d000010632d5ec76b0500000000000000'],317          // Balances.freezes(superuser.address -> freeze with app promo id and 200 UNQ )318          ['0xc2261276cc9d1f8598ea4b6a74b15c2fb1c0eb12e038e5c7f91e120ed4b7ebf1de1e86a9a8c739864cf3cc5ec2bea59fd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d',319            '0x046170707374616b656170707374616b65000020c65abc8ed70a00000000000000'],320        ])]);321322        expect((await api.query.appPromotion.pendingUnstake(1)).toJSON()).to.be.deep.equal([[superuser.address, '0x00000000000000056bc75e2d63100000']]);323        expect((await api.query.appPromotion.pendingUnstake(2)).toJSON()).to.be.deep.equal([[superuser.address, '0x00000000000000056bc75e2d63100000']]);324        expect((await api.query.balances.freezes(superuser.address) as Vec<PalletBalancesIdAmount>)325          .map(lock => ({id: lock.id.toUtf8(), amount: lock.amount.toBigInt()})))326          .to.be.deep.equal([{id: 'appstakeappstake', amount: 200000000000000000000n}]);327        await correctState();328329        expect((await api.query.appPromotion.pendingUnstake(1)).toJSON()).to.be.deep.equal([]);330        expect((await api.query.appPromotion.pendingUnstake(2)).toJSON()).to.be.deep.equal([]);331        expect((await api.query.balances.freezes(superuser.address)).toJSON()).to.be.deep.equal([]);332333      });334335      itSub('(!negative test!) Only works when Maintenance mode is disabled', async({helper}) => {336        await expect(helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.enable', [])).to.be.fulfilled;337        await expect(correctState()).to.be.rejectedWith('The network is still in maintenance mode');338        await expect(helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', [])).to.be.fulfilled;339      });340    });341  });342343  after(async () => {344    await usingPlaygrounds(async(helper) => {345      await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', []);346    });347  });348});