1234567891011121314151617import {IKeyringPair} from '@polkadot/types/types';18import {ApiPromise} from '@polkadot/api';19import {expect, itSched, itSub, Pallets, requirePalletsOrSkip, 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 Functionality', () => {27 let superuser: IKeyringPair;28 let donor: IKeyringPair;29 let bob: IKeyringPair;3031 before(async function() {32 await usingPlaygrounds(async (helper, privateKey) => {33 requirePalletsOrSkip(this, helper, [Pallets.Maintenance]);34 superuser = await privateKey('//Alice');35 donor = await privateKey({filename: __filename});36 [bob] = await helper.arrange.createAccounts([10000n], donor);3738 });39 });4041 describe('Maintenance Mode', () => {42 before(async function() {43 await usingPlaygrounds(async (helper) => {44 if (await maintenanceEnabled(helper.getApi())) {45 console.warn('\tMaintenance mode was left enabled BEFORE the test suite! Disabling it now.');46 await expect(helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', [])).to.be.fulfilled;47 }48 });49 });5051 itSub('Allows superuser to enable and disable maintenance mode - and disallows anyone else', async ({helper}) => {52 53 await expect(helper.executeExtrinsic(superuser, 'api.tx.maintenance.enable', []), 'on commoner enabling MM')54 .to.be.rejectedWith(/BadOrigin/);5556 57 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.enable', []);58 expect(await maintenanceEnabled(helper.getApi()), 'MM is OFF when it should be ON').to.be.true;5960 61 await expect(helper.executeExtrinsic(bob, 'api.tx.maintenance.disable', []), 'on commoner disabling MM')62 .to.be.rejectedWith(/BadOrigin/);6364 65 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', []);66 expect(await maintenanceEnabled(helper.getApi()), 'MM is ON when it should be OFF').to.be.false;67 });6869 itSub('MM blocks unique pallet calls', async ({helper}) => {70 71 const nftCollection = await helper.nft.mintCollection(bob, {72 tokenPropertyPermissions: [{key: 'test', permission: {73 collectionAdmin: true,74 tokenOwner: true,75 mutable: true,76 }}],77 });7879 80 const nft = await nftCollection.mintToken(bob);8182 83 const ftCollection = await helper.ft.mintCollection(superuser);8485 86 await expect(ftCollection.mint(superuser)).to.be.fulfilled;8788 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.enable', []);89 expect(await maintenanceEnabled(helper.getApi()), 'MM is OFF when it should be ON').to.be.true;9091 92 await expect(helper.nft.mintCollection(superuser), 'cudo forbidden stuff')93 .to.be.rejectedWith(/Invalid Transaction: Transaction call is not expected/);9495 96 await expect(nft.setProperties(97 bob,98 [{key: 'test', value: 'test-val'}],99 )).to.be.rejectedWith(/Invalid Transaction: Transaction call is not expected/);100101 102 await expect(nftCollection.mintToken(superuser))103 .to.be.rejectedWith(/Invalid Transaction: Transaction call is not expected/);104105 106 await expect(ftCollection.mint(superuser))107 .to.be.rejectedWith(/Invalid Transaction: Transaction call is not expected/);108109 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', []);110 expect(await maintenanceEnabled(helper.getApi()), 'MM is ON when it should be OFF').to.be.false;111112 113 await expect(helper.nft.mintCollection(bob), 'MM is disabled, the collection should be created').to.be.fulfilled;114115 116 await nft.setProperties(bob, [{key: 'test', value: 'test-val'}]);117118 119 await nftCollection.mintToken(bob);120121 122 await ftCollection.mint(superuser);123 });124125 itSub.ifWithPallets('MM blocks unique pallet calls (Re-Fungible)', [Pallets.ReFungible], async ({helper}) => {126 127 const rftCollection = await helper.rft.mintCollection(superuser);128129 130 await expect(rftCollection.mintToken(superuser)).to.be.fulfilled;131132 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.enable', []);133 expect(await maintenanceEnabled(helper.getApi()), 'MM is OFF when it should be ON').to.be.true;134135 136 await expect(rftCollection.mintToken(superuser))137 .to.be.rejectedWith(/Invalid Transaction: Transaction call is not expected/);138139 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', []);140 expect(await maintenanceEnabled(helper.getApi()), 'MM is ON when it should be OFF').to.be.false;141142 143 await rftCollection.mintToken(superuser);144 });145146 itSub('MM allows native token transfers and RPC calls', async ({helper}) => {147 148 const totalCount = await helper.collection.getTotalCount();149150 151 await expect(helper.balance.transferToSubstrate(superuser, bob.address, 2n)).to.be.fulfilled;152153 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.enable', []);154 expect(await maintenanceEnabled(helper.getApi()), 'MM is OFF when it should be ON').to.be.true;155156 157 expect(await helper.collection.getTotalCount()).to.be.deep.equal(totalCount);158159 160 await expect(helper.balance.transferToSubstrate(bob, superuser.address, 1n)).to.be.fulfilled;161162 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', []);163 expect(await maintenanceEnabled(helper.getApi()), 'MM is ON when it should be OFF').to.be.false;164165 166 expect(await helper.collection.getTotalCount()).to.be.deep.equal(totalCount);167168 169 await expect(helper.balance.transferToSubstrate(bob, superuser.address, 1n)).to.be.fulfilled;170 });171172 itSched.ifWithPallets('MM blocks scheduled calls and the scheduler itself', [Pallets.Scheduler], async (scheduleKind, {helper}) => {173 const collection = await helper.nft.mintCollection(bob);174175 const nftBeforeMM = await collection.mintToken(bob);176 const nftDuringMM = await collection.mintToken(bob);177 const nftAfterMM = await collection.mintToken(bob);178179 const [180 scheduledIdBeforeMM,181 scheduledIdDuringMM,182 scheduledIdBunkerThroughMM,183 scheduledIdAttemptDuringMM,184 scheduledIdAfterMM,185 ] = scheduleKind == 'named'186 ? helper.arrange.makeScheduledIds(5)187 : new Array(5);188189 const blocksToWait = 6;190191 192 await nftBeforeMM.scheduleAfter(blocksToWait, {scheduledId: scheduledIdBeforeMM})193 .transfer(bob, {Substrate: superuser.address});194195 await helper.wait.newBlocks(blocksToWait + 1);196 expect(await nftBeforeMM.getOwner()).to.be.deep.equal({Substrate: superuser.address});197198 199 await nftDuringMM.scheduleAfter(blocksToWait, {scheduledId: scheduledIdDuringMM})200 .transfer(bob, {Substrate: superuser.address});201202 203 await nftDuringMM.scheduleAfter(blocksToWait * 2, {scheduledId: scheduledIdBunkerThroughMM})204 .transfer(bob, {Substrate: superuser.address});205206 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.enable', []);207 expect(await maintenanceEnabled(helper.getApi()), 'MM is OFF when it should be ON').to.be.true;208209 await helper.wait.newBlocks(blocksToWait + 1);210 211 expect(await nftDuringMM.getOwner()).to.be.deep.equal({Substrate: bob.address});212213 214 await expect(nftDuringMM.scheduleAfter(blocksToWait, {scheduledId: scheduledIdAttemptDuringMM})215 .transfer(bob, {Substrate: superuser.address}))216 .to.be.rejectedWith(/Invalid Transaction: Transaction call is not expected/);217218 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', []);219 expect(await maintenanceEnabled(helper.getApi()), 'MM is ON when it should be OFF').to.be.false;220221 222 await nftAfterMM.scheduleAfter(blocksToWait, {scheduledId: scheduledIdAfterMM})223 .transfer(bob, {Substrate: superuser.address});224225 await helper.wait.newBlocks(blocksToWait + 1);226227 expect(await nftAfterMM.getOwner()).to.be.deep.equal({Substrate: superuser.address});228 229 expect(await nftDuringMM.getOwner()).to.be.deep.equal({Substrate: superuser.address});230 });231232 itEth('Disallows Ethereum transactions to execute while in maintenance', async ({helper}) => {233 const owner = await helper.eth.createAccountWithBalance(donor);234 const receiver = helper.eth.createAccount();235236 const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, 'A', 'B', 'C', '');237238 239 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.enable', []);240 expect(await maintenanceEnabled(helper.getApi()), 'MM is OFF when it should be ON').to.be.true;241242 const contract = await helper.ethNativeContract.collection(collectionAddress, 'nft', owner);243 const tokenId = await contract.methods.nextTokenId().call();244 expect(tokenId).to.be.equal('1');245246 await expect(contract.methods.mintWithTokenURI(receiver, 'Test URI').send())247 .to.be.rejectedWith(/Returned error: unknown error/);248249 await expect(contract.methods.ownerOf(tokenId).call()).rejectedWith(/token not found/);250251 252 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', []);253 expect(await maintenanceEnabled(helper.getApi()), 'MM is ON when it should be OFF').to.be.false;254 });255256 itSub('Allows to enable and disable MM repeatedly', async ({helper}) => {257 258 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.enable', []);259 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.enable', []);260 expect(await maintenanceEnabled(helper.getApi()), 'MM is OFF when it should be ON').to.be.true;261262 263 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', []);264 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', []);265 expect(await maintenanceEnabled(helper.getApi()), 'MM is ON when it should be OFF').to.be.false;266 });267268 afterEach(async () => {269 await usingPlaygrounds(async helper => {270 if (helper.fetchMissingPalletNames([Pallets.Maintenance]).length != 0) return;271 if (await maintenanceEnabled(helper.getApi())) {272 console.warn('\tMaintenance mode was left enabled AFTER a test has finished! Be careful. Disabling it now.');273 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', []);274 }275 expect(await maintenanceEnabled(helper.getApi()), 'Disastrous! Exited the test suite with maintenance mode on.').to.be.false;276 });277 });278 });279280 describe('Preimage Execution', () => {281 let preimageHash: string;282283 before(async function() {284 await usingPlaygrounds(async (helper) => {285 requirePalletsOrSkip(this, helper, [Pallets.Preimage, Pallets.Maintenance]);286287 288 const randomAccounts = await helper.arrange.createCrowd(10, 0n, superuser);289 const randomIdentities = randomAccounts.map((acc, i) => [290 acc.address, {291 deposit: 0n,292 judgements: [],293 info: {294 display: {295 raw: `Random Account #${i}`,296 },297 },298 },299 ]);300 const preimage = helper.constructApiCall('api.tx.identity.forceInsertIdentities', [randomIdentities]).method.toHex();301 const result = await helper.preimage.notePreimage(bob, preimage);302 const events = result.result.events.filter(x => x.event.method === 'Noted' && x.event.section === 'preimage');303 preimageHash = events[0].event.data[0].toHuman();304 });305 });306307 itSub('Successfully executes call in a preimage', async ({helper}) => {308 const result = await expect(helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.executePreimage', [preimageHash]))309 .to.be.fulfilled;310311 312 const events = result.result.events.filter((x: any) => x.event.method === 'IdentitiesInserted' && x.event.section === 'identity');313 expect(events.length).to.be.equal(1);314315 316 expect(await helper.preimage.getPreimageInfo(preimageHash)).to.have.property('unrequested');317 });318319 itSub('Does not allow preimage execution with non-root', async ({helper}) => {320 await expect(helper.executeExtrinsic(bob, 'api.tx.maintenance.executePreimage', [preimageHash]))321 .to.be.rejectedWith(/BadOrigin/);322 });323324 itSub('Does not allow execution of non-existent preimages', async ({helper}) => {325 await expect(helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.executePreimage', [326 '0x1010101010101010101010101010101010101010101010101010101010101010',327 ])).to.be.rejectedWith(/Unavailable/);328 });329330 after(async function() {331 await usingPlaygrounds(async (helper) => {332 if (helper.fetchMissingPalletNames([Pallets.Preimage, Pallets.Maintenance]).length != 0) return;333334 await helper.preimage.unnotePreimage(bob, preimageHash);335 });336 });337 });338});