1234567891011121314151617import {IKeyringPair} from '@polkadot/types/types';18import {ApiPromise} from '@polkadot/api';19import {expect, itSub, Pallets, usingPlaygrounds} from './util';20import {itEth} from './eth/util';2122async function maintenanceEnabled(api: ApiPromise): Promise<boolean> {23 return (await api.query.maintenance.enabled()).toJSON() as boolean;24}2526describe('Integration Test: Maintenance Mode', () => {27 let superuser: IKeyringPair;28 let donor: IKeyringPair;29 let bob: IKeyringPair;3031 before(async () => {32 await usingPlaygrounds(async (helper, privateKey) => {33 superuser = await privateKey('//Alice');34 donor = await privateKey({filename: __filename});35 [bob] = await helper.arrange.createAccounts([100n], donor);3637 if (await maintenanceEnabled(helper.getApi())) {38 console.warn('\tMaintenance mode was left enabled BEFORE the test suite! Disabling it now.');39 await expect(helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', [])).to.be.fulfilled;40 }41 });42 });4344 itSub('Allows superuser to enable and disable maintenance mode - and disallows anyone else', async ({helper}) => {45 46 await expect(helper.executeExtrinsic(superuser, 'api.tx.maintenance.enable', []), 'on commoner enabling MM')47 .to.be.rejectedWith(/BadOrigin/);4849 50 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.enable', []);51 expect(await maintenanceEnabled(helper.getApi()), 'MM is OFF when it should be ON').to.be.true;5253 54 await expect(helper.executeExtrinsic(bob, 'api.tx.maintenance.disable', []), 'on commoner disabling MM')55 .to.be.rejectedWith(/BadOrigin/);5657 58 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', []);59 expect(await maintenanceEnabled(helper.getApi()), 'MM is ON when it should be OFF').to.be.false;60 });6162 itSub('MM blocks unique pallet calls', async ({helper}) => {63 64 const nftCollection = await helper.nft.mintCollection(bob, {65 tokenPropertyPermissions: [{key: 'test', permission: {66 collectionAdmin: true,67 tokenOwner: true,68 mutable: true,69 }}],70 });7172 73 const nft = await nftCollection.mintToken(bob);7475 76 const ftCollection = await helper.ft.mintCollection(superuser);7778 79 await expect(ftCollection.mint(superuser)).to.be.fulfilled;8081 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.enable', []);82 expect(await maintenanceEnabled(helper.getApi()), 'MM is OFF when it should be ON').to.be.true;8384 85 await expect(helper.nft.mintCollection(superuser), 'cudo forbidden stuff')86 .to.be.rejectedWith(/Invalid Transaction: Transaction call is not expected/);8788 89 await expect(nft.setProperties(90 bob,91 [{key: 'test', value: 'test-val'}],92 )).to.be.rejectedWith(/Invalid Transaction: Transaction call is not expected/);9394 95 await expect(nftCollection.mintToken(superuser))96 .to.be.rejectedWith(/Invalid Transaction: Transaction call is not expected/);9798 99 await expect(ftCollection.mint(superuser))100 .to.be.rejectedWith(/Invalid Transaction: Transaction call is not expected/);101102 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', []);103 expect(await maintenanceEnabled(helper.getApi()), 'MM is ON when it should be OFF').to.be.false;104105 106 await expect(helper.nft.mintCollection(bob), 'MM is disabled, the collection should be created').to.be.fulfilled;107108 109 await nft.setProperties(bob, [{key: 'test', value: 'test-val'}]);110111 112 await nftCollection.mintToken(bob);113114 115 await ftCollection.mint(superuser);116 });117118 itSub.ifWithPallets('MM blocks unique pallet calls (Re-Fungible)', [Pallets.ReFungible], async ({helper}) => {119 120 const rftCollection = await helper.rft.mintCollection(superuser);121122 123 await expect(rftCollection.mintToken(superuser)).to.be.fulfilled;124125 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.enable', []);126 expect(await maintenanceEnabled(helper.getApi()), 'MM is OFF when it should be ON').to.be.true;127128 129 await expect(rftCollection.mintToken(superuser))130 .to.be.rejectedWith(/Invalid Transaction: Transaction call is not expected/);131 132 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', []);133 expect(await maintenanceEnabled(helper.getApi()), 'MM is ON when it should be OFF').to.be.false;134135 136 await rftCollection.mintToken(superuser);137 });138139 itSub('MM allows native token transfers and RPC calls', async ({helper}) => {140 141 const totalCount = await helper.collection.getTotalCount();142143 144 await expect(helper.balance.transferToSubstrate(superuser, bob.address, 2n)).to.be.fulfilled;145146 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.enable', []);147 expect(await maintenanceEnabled(helper.getApi()), 'MM is OFF when it should be ON').to.be.true;148149 150 expect(await helper.collection.getTotalCount()).to.be.deep.equal(totalCount);151152 153 await expect(helper.balance.transferToSubstrate(bob, superuser.address, 1n)).to.be.fulfilled;154155 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', []);156 expect(await maintenanceEnabled(helper.getApi()), 'MM is ON when it should be OFF').to.be.false;157158 159 expect(await helper.collection.getTotalCount()).to.be.deep.equal(totalCount);160161 162 await expect(helper.balance.transferToSubstrate(bob, superuser.address, 1n)).to.be.fulfilled;163 });164165 itSub.ifWithPallets('MM blocks scheduled calls and the scheduler itself', [Pallets.Scheduler], async ({helper}) => {166 const collection = await helper.nft.mintCollection(bob);167168 const nftBeforeMM = await collection.mintToken(bob);169 const nftDuringMM = await collection.mintToken(bob);170 const nftAfterMM = await collection.mintToken(bob);171172 const scheduledIdBeforeMM = '0x' + '0'.repeat(31) + '0';173 const scheduledIdDuringMM = '0x' + '0'.repeat(31) + '1';174 const scheduledIdBunkerThroughMM = '0x' + '0'.repeat(31) + '2';175 const scheduledIdAttemptDuringMM = '0x' + '0'.repeat(31) + '3';176 const scheduledIdAfterMM = '0x' + '0'.repeat(31) + '4';177178 const blocksToWait = 6;179180 181 await nftBeforeMM.scheduleAfter(scheduledIdBeforeMM, blocksToWait)182 .transfer(bob, {Substrate: superuser.address});183184 await helper.wait.newBlocks(blocksToWait + 1);185 expect(await nftBeforeMM.getOwner()).to.be.deep.equal({Substrate: superuser.address});186187 188 await nftDuringMM.scheduleAfter(scheduledIdDuringMM, blocksToWait)189 .transfer(bob, {Substrate: superuser.address});190 191 192 await nftDuringMM.scheduleAfter(scheduledIdBunkerThroughMM, blocksToWait * 2)193 .transfer(bob, {Substrate: superuser.address});194195 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.enable', []);196 expect(await maintenanceEnabled(helper.getApi()), 'MM is OFF when it should be ON').to.be.true;197198 await helper.wait.newBlocks(blocksToWait + 1);199 200 expect(await nftDuringMM.getOwner()).to.be.deep.equal({Substrate: bob.address});201202 203 await expect(nftDuringMM.scheduleAfter(scheduledIdAttemptDuringMM, blocksToWait)204 .transfer(bob, {Substrate: superuser.address}))205 .to.be.rejectedWith(/Invalid Transaction: Transaction call is not expected/);206207 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', []);208 expect(await maintenanceEnabled(helper.getApi()), 'MM is ON when it should be OFF').to.be.false;209210 211 await nftAfterMM.scheduleAfter(scheduledIdAfterMM, blocksToWait)212 .transfer(bob, {Substrate: superuser.address});213 214 await helper.wait.newBlocks(blocksToWait + 1);215216 expect(await nftAfterMM.getOwner()).to.be.deep.equal({Substrate: superuser.address});217 218 expect(await nftDuringMM.getOwner()).to.be.deep.equal({Substrate: superuser.address});219 });220221 itEth('Disallows Ethereum transactions to execute while in maintenance', async ({helper}) => {222 const owner = await helper.eth.createAccountWithBalance(donor);223 const receiver = helper.eth.createAccount();224 225 const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, 'A', 'B', 'C', '');226227 228 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.enable', []);229 expect(await maintenanceEnabled(helper.getApi()), 'MM is OFF when it should be ON').to.be.true;230231 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);232 const tokenId = await contract.methods.nextTokenId().call();233 expect(tokenId).to.be.equal('1');234235 await expect(contract.methods.mintWithTokenURI(receiver, 'Test URI').send())236 .to.be.rejectedWith(/submit transaction to pool failed: Pool\(InvalidTransaction\(InvalidTransaction::Call\)\)/);237238 await expect(contract.methods.ownerOf(tokenId).call()).rejectedWith(/token not found/);239240 241 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', []);242 expect(await maintenanceEnabled(helper.getApi()), 'MM is ON when it should be OFF').to.be.false;243 });244245 itSub('Allows to enable and disable MM repeatedly', async ({helper}) => {246 247 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.enable', []);248 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.enable', []);249 expect(await maintenanceEnabled(helper.getApi()), 'MM is OFF when it should be ON').to.be.true;250251 252 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', []);253 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', []);254 expect(await maintenanceEnabled(helper.getApi()), 'MM is ON when it should be OFF').to.be.false;255 });256257 afterEach(async () => {258 await usingPlaygrounds(async helper => {259 if (await maintenanceEnabled(helper.getApi())) {260 console.warn('\tMaintenance mode was left enabled AFTER a test has finished! Be careful. Disabling it now.');261 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', []);262 }263 expect(await maintenanceEnabled(helper.getApi()), 'Disastrous! Exited the test suite with maintenance mode on.').to.be.false;264 });265 });266});