difftreelog
test(scheduler) root origin tests
in: master
1 file changed
tests/src/.outdated/scheduler.test.tsdiffbeforeafterboth45describe('Scheduling token and balance transfers', () => {45describe('Scheduling token and balance transfers', () => {46 let alice: IKeyringPair;46 let alice: IKeyringPair;47 let bob: IKeyringPair;47 let bob: IKeyringPair;48 let charlie: IKeyringPair;484949 before(async () => {50 before(async () => {50 await usingPlaygrounds(async (_, privateKeyWrapper) => {51 await usingPlaygrounds(async (_, privateKeyWrapper) => {51 alice = privateKeyWrapper('//Alice');52 alice = privateKeyWrapper('//Alice');52 bob = privateKeyWrapper('//Bob');53 bob = privateKeyWrapper('//Bob');54 charlie = privateKeyWrapper('//Charlie');53 });55 });54 });56 });555756 itSub.ifWithPallets('Can delay a transfer of an owned token', [Pallets.Scheduler], async ({helper}) => {58 itSub.ifWithPallets('Can delay a transfer of an owned token', [Pallets.Scheduler], async ({helper}) => {57 const collection = await helper.nft.mintCollection(alice, {tokenPrefix: 'schd'});59 const collection = await helper.nft.mintCollection(alice, {tokenPrefix: 'schd'});58 const token = await collection.mintToken(alice);60 const token = await collection.mintToken(alice);59 const schedulerId = await helper.scheduler.makeScheduledId();61 const schedulerId = await helper.arrange.makeScheduledId();60 const blocksBeforeExecution = 4;62 const blocksBeforeExecution = 4;616362 await token.scheduleAfter(schedulerId, blocksBeforeExecution)64 await token.scheduleAfter(schedulerId, blocksBeforeExecution)70 });72 });717372 itSub.ifWithPallets('Can transfer funds periodically', [Pallets.Scheduler], async ({helper}) => {74 itSub.ifWithPallets('Can transfer funds periodically', [Pallets.Scheduler], async ({helper}) => {73 const scheduledId = await helper.scheduler.makeScheduledId();75 const scheduledId = await helper.arrange.makeScheduledId();74 const waitForBlocks = 1;76 const waitForBlocks = 1;757776 const amount = 1n * UNIQUE;78 const amount = 1n * UNIQUE;107 const collection = await helper.nft.mintCollection(alice, {tokenPrefix: 'schd'});109 const collection = await helper.nft.mintCollection(alice, {tokenPrefix: 'schd'});108 const token = await collection.mintToken(alice);110 const token = await collection.mintToken(alice);109111110 const scheduledId = await helper.scheduler.makeScheduledId();112 const scheduledId = await helper.arrange.makeScheduledId();111 const waitForBlocks = 4;113 const waitForBlocks = 4;112114113 expect(await token.getOwner()).to.be.deep.equal({Substrate: alice.address});115 expect(await token.getOwner()).to.be.deep.equal({Substrate: alice.address});129 repetitions: 2,131 repetitions: 2,130 };132 };131133132 const scheduledId = await helper.scheduler.makeScheduledId();134 const scheduledId = await helper.arrange.makeScheduledId();133135134 const amount = 1n * UNIQUE;136 const amount = 1n * UNIQUE;135137160 });162 });161163162 itSub.ifWithPallets('Scheduled tasks are transactional', [Pallets.Scheduler, Pallets.TestUtils], async ({helper}) => {164 itSub.ifWithPallets('Scheduled tasks are transactional', [Pallets.Scheduler, Pallets.TestUtils], async ({helper}) => {163 const scheduledId = await helper.scheduler.makeScheduledId();165 const scheduledId = await helper.arrange.makeScheduledId();164 const waitForBlocks = 4;166 const waitForBlocks = 4;165167166 const initTestVal = 42;168 const initTestVal = 42;189 });191 });190192191 itSub.ifWithPallets('Scheduled tasks should take correct fees', [Pallets.Scheduler, Pallets.TestUtils], async function({helper}) {193 itSub.ifWithPallets('Scheduled tasks should take correct fees', [Pallets.Scheduler, Pallets.TestUtils], async function({helper}) {192 const scheduledId = await helper.scheduler.makeScheduledId();194 const scheduledId = await helper.arrange.makeScheduledId();193 const waitForBlocks = 8;195 const waitForBlocks = 8;194 const periodic = {196 const periodic = {195 period: 2,197 period: 2,249 const [251 const [250 scheduledId,252 scheduledId,251 scheduledCancelId,253 scheduledCancelId,252 ] = await helper.scheduler.makeScheduledIds(2);254 ] = await helper.arrange.makeScheduledIds(2);253255254 const periodic = {256 const periodic = {255 period: 5,257 period: 5,277279278 // Cancel the inc tx after 2 executions280 // Cancel the inc tx after 2 executions279 // *in the same block* in which the second execution is scheduled281 // *in the same block* in which the second execution is scheduled280 await helper.scheduler.scheduleAt(scheduledCancelId, firstExecutionBlockNumber + periodic.period)282 await helper.scheduler.scheduleAt(283 scheduledCancelId,284 firstExecutionBlockNumber + periodic.period,281 .scheduler.cancelScheduled(alice, scheduledId);285 ).scheduler.cancelScheduled(alice, scheduledId);282286283 await helper.wait.newBlocks(blocksBeforeExecution);287 await helper.wait.newBlocks(blocksBeforeExecution);284288300 });304 });301305302 itSub.ifWithPallets('A scheduled operation can cancel itself', [Pallets.Scheduler, Pallets.TestUtils], async ({helper}) => {306 itSub.ifWithPallets('A scheduled operation can cancel itself', [Pallets.Scheduler, Pallets.TestUtils], async ({helper}) => {303 const scheduledId = await helper.scheduler.makeScheduledId();307 const scheduledId = await helper.arrange.makeScheduledId();304 const waitForBlocks = 8;308 const waitForBlocks = 8;305 const periodic = {309 const periodic = {306 period: 2,310 period: 2,344 .to.be.equal(initTestVal + 2);348 .to.be.equal(initTestVal + 2);345 });349 });350351 itSub.ifWithPallets('Root can cancel any scheduled operation', [Pallets.Scheduler], async ({helper}) => {352 const collection = await helper.nft.mintCollection(bob, {tokenPrefix: 'schd'});353 const token = await collection.mintToken(bob);354355 const scheduledId = await helper.arrange.makeScheduledId();356 const waitForBlocks = 4;357358 await token.scheduleAfter(scheduledId, waitForBlocks)359 .transfer(bob, {Substrate: alice.address});360361 await helper.getSudo().scheduler.cancelScheduled(alice, scheduledId);362363 await helper.wait.newBlocks(waitForBlocks + 1);364365 expect(await token.getOwner()).to.be.deep.equal({Substrate: bob.address});366 });367368 itSub.ifWithPallets('Root can set prioritized scheduled operation', [Pallets.Scheduler], async ({helper}) => {369 const scheduledId = await helper.arrange.makeScheduledId();370 const waitForBlocks = 4;371372 const amount = 42n * UNIQUE;373374 const balanceBefore = await helper.balance.getSubstrate(charlie.address);375376 await helper.getSudo()377 .scheduler.scheduleAfter(scheduledId, waitForBlocks, {priority: 42})378 .balance.forceTransferToSubstrate(alice, bob.address, charlie.address, amount);379380 await helper.wait.newBlocks(waitForBlocks + 1);381382 const balanceAfter = await helper.balance.getSubstrate(charlie.address);383384 expect(balanceAfter > balanceBefore).to.be.true;385386 const diff = balanceAfter - balanceBefore;387 expect(diff).to.be.equal(amount);388 });389390 itSub.ifWithPallets("Root can change scheduled operation's priority", [Pallets.Scheduler], async ({helper}) => {391 const collection = await helper.nft.mintCollection(bob, {tokenPrefix: 'schd'});392 const token = await collection.mintToken(bob);393394 const scheduledId = await helper.arrange.makeScheduledId();395 const waitForBlocks = 4;396397 await token.scheduleAfter(scheduledId, waitForBlocks)398 .transfer(bob, {Substrate: alice.address});399400 const priority = 112;401 await helper.getSudo().scheduler.changePriority(alice, scheduledId, priority);402403 const priorityChanged = await helper.wait.event(404 waitForBlocks,405 'scheduler',406 'PriorityChanged',407 );408409 expect(priorityChanged !== null).to.be.true;410 expect(priorityChanged!.event.data[2].toString()).to.be.equal(priority.toString());411 });412413 itSub.ifWithPallets.only('Prioritized operations executes in valid order', [Pallets.Scheduler], async ({helper}) => {414 const [415 scheduledFirstId,416 scheduledSecondId,417 ] = await helper.arrange.makeScheduledIds(2);418419 const currentBlockNumber = await helper.chain.getLatestBlockNumber();420 const blocksBeforeExecution = 4;421 const firstExecutionBlockNumber = currentBlockNumber + blocksBeforeExecution;422423 const prioHigh = 0;424 const prioLow = 255;425426 const periodic = {427 period: 8,428 repetitions: 2,429 };430431 const amount = 1n * UNIQUE;432433 // Scheduler a task with a lower priority first, then with a higher priority434 await helper.getSudo().scheduler.scheduleAt(scheduledFirstId, firstExecutionBlockNumber, {priority: prioLow, periodic})435 .balance.forceTransferToSubstrate(alice, alice.address, bob.address, amount);436437 await helper.getSudo().scheduler.scheduleAt(scheduledSecondId, firstExecutionBlockNumber, {priority: prioHigh, periodic})438 .balance.forceTransferToSubstrate(alice, alice.address, bob.address, amount);439440 const capture = await helper.arrange.captureEvents('scheduler', 'Dispatched');441442 await helper.wait.newBlocks(blocksBeforeExecution);443444 // Flip priorities445 await helper.getSudo().scheduler.changePriority(alice, scheduledFirstId, prioHigh);446 await helper.getSudo().scheduler.changePriority(alice, scheduledSecondId, prioLow);447448 await helper.wait.newBlocks(periodic.period);449450 const dispatchEvents = capture.extractCapturedEvents();451 expect(dispatchEvents.length).to.be.equal(4);452453 const dispatchedIds = dispatchEvents.map(r => r.event.data[1].toString());454455 const firstExecuctionIds = [dispatchedIds[0], dispatchedIds[1]];456 const secondExecuctionIds = [dispatchedIds[2], dispatchedIds[3]];457458 expect(firstExecuctionIds[0]).to.be.equal(scheduledSecondId);459 expect(firstExecuctionIds[1]).to.be.equal(scheduledFirstId);460461 expect(secondExecuctionIds[0]).to.be.equal(scheduledFirstId);462 expect(secondExecuctionIds[1]).to.be.equal(scheduledSecondId);463 });346});464});347465348describe('Negative Test: Scheduling', () => {466describe('Negative Test: Scheduling', () => {356 });474 });357 });475 });358476359 itSub.ifWithPallets("Can't overwrite a scheduled ID", [Pallets.Scheduler, Pallets.TestUtils], async ({helper}) => {477 itSub.ifWithPallets("Can't overwrite a scheduled ID", [Pallets.Scheduler], async ({helper}) => {360 const collection = await helper.nft.mintCollection(alice, {tokenPrefix: 'schd'});478 const collection = await helper.nft.mintCollection(alice, {tokenPrefix: 'schd'});361 const token = await collection.mintToken(alice);479 const token = await collection.mintToken(alice);362480363 const scheduledId = await helper.scheduler.makeScheduledId();481 const scheduledId = await helper.arrange.makeScheduledId();364 const waitForBlocks = 4;482 const waitForBlocks = 4;365483366 await token.scheduleAfter(scheduledId, waitForBlocks)484 await token.scheduleAfter(scheduledId, waitForBlocks)367 .transfer(alice, {Substrate: bob.address});485 .transfer(alice, {Substrate: bob.address});368486369 await expect(helper.scheduler.scheduleAfter(scheduledId, waitForBlocks)487 const scheduled = helper.scheduler.scheduleAfter(scheduledId, waitForBlocks);370 .balance.transferToSubstrate(alice, bob.address, 1n * UNIQUE))488 await expect(scheduled.balance.transferToSubstrate(alice, bob.address, 1n * UNIQUE))371 .to.be.rejectedWith(/scheduler\.FailedToSchedule/);489 .to.be.rejectedWith(/scheduler\.FailedToSchedule/);372490373 const bobsBalanceBefore = await helper.balance.getSubstrate(bob.address);491 const bobsBalanceBefore = await helper.balance.getSubstrate(bob.address);380 expect(bobsBalanceBefore).to.be.equal(bobsBalanceAfter);498 expect(bobsBalanceBefore).to.be.equal(bobsBalanceAfter);381 });499 });382500383 itSub.ifWithPallets("Can't cancel an operation which is not scheduled", [Pallets.Scheduler, Pallets.TestUtils], async ({helper}) => {501 itSub.ifWithPallets("Can't cancel an operation which is not scheduled", [Pallets.Scheduler], async ({helper}) => {384 const scheduledId = await helper.scheduler.makeScheduledId();502 const scheduledId = await helper.arrange.makeScheduledId();385 await expect(helper.scheduler.cancelScheduled(alice, scheduledId))503 await expect(helper.scheduler.cancelScheduled(alice, scheduledId))386 .to.be.rejectedWith(/scheduler\.NotFound/);504 .to.be.rejectedWith(/scheduler\.NotFound/);387 });505 });388506389 itSub.ifWithPallets("Can't cancel a non-owned scheduled operation", [Pallets.Scheduler, Pallets.TestUtils], async ({helper}) => {507 itSub.ifWithPallets("Can't cancel a non-owned scheduled operation", [Pallets.Scheduler], async ({helper}) => {390 const collection = await helper.nft.mintCollection(alice, {tokenPrefix: 'schd'});508 const collection = await helper.nft.mintCollection(alice, {tokenPrefix: 'schd'});391 const token = await collection.mintToken(alice);509 const token = await collection.mintToken(alice);392510393 const scheduledId = await helper.scheduler.makeScheduledId();511 const scheduledId = await helper.arrange.makeScheduledId();394 const waitForBlocks = 8;512 const waitForBlocks = 8;395513396 await token.scheduleAfter(scheduledId, waitForBlocks)514 await token.scheduleAfter(scheduledId, waitForBlocks)402 await helper.wait.newBlocks(waitForBlocks + 1);520 await helper.wait.newBlocks(waitForBlocks + 1);403521404 expect(await token.getOwner()).to.be.deep.equal({Substrate: bob.address});522 expect(await token.getOwner()).to.be.deep.equal({Substrate: bob.address});523 });524525 itSub.ifWithPallets("Regular user can't set prioritized scheduled operation", [Pallets.Scheduler], async ({helper}) => {526 const scheduledId = await helper.arrange.makeScheduledId();527 const waitForBlocks = 4;528529 const amount = 42n * UNIQUE;530531 const balanceBefore = await helper.balance.getSubstrate(bob.address);532533 const scheduled = helper.scheduler.scheduleAfter(scheduledId, waitForBlocks, {priority: 42});534 535 await expect(scheduled.balance.transferToSubstrate(alice, bob.address, amount))536 .to.be.rejectedWith(/badOrigin/);537538 await helper.wait.newBlocks(waitForBlocks + 1);539540 const balanceAfter = await helper.balance.getSubstrate(bob.address);541542 expect(balanceAfter).to.be.equal(balanceBefore);543 });544545 itSub.ifWithPallets("Regular user can't change scheduled operation's priority", [Pallets.Scheduler], async ({helper}) => {546 const collection = await helper.nft.mintCollection(bob, {tokenPrefix: 'schd'});547 const token = await collection.mintToken(bob);548549 const scheduledId = await helper.arrange.makeScheduledId();550 const waitForBlocks = 4;551552 await token.scheduleAfter(scheduledId, waitForBlocks)553 .transfer(bob, {Substrate: alice.address});554555 const priority = 112;556 await expect(helper.scheduler.changePriority(alice, scheduledId, priority))557 .to.be.rejectedWith(/badOrigin/);558559 const priorityChanged = await helper.wait.event(560 waitForBlocks,561 'scheduler',562 'PriorityChanged',563 );564565 expect(priorityChanged === null).to.be.true;405 });566 });406});567});407568