git.delta.rocks / unique-network / refs/commits / 8639714f9550

difftreelog

test(scheduler) root origin tests

Daniel Shiposha2022-09-16parent: #fcb2b32.patch.diff
in: master

1 file changed

modifiedtests/src/.outdated/scheduler.test.tsdiffbeforeafterboth
45describe('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;
4849
49 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 });
5557
56 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;
6163
62 await token.scheduleAfter(schedulerId, blocksBeforeExecution)64 await token.scheduleAfter(schedulerId, blocksBeforeExecution)
70 });72 });
7173
72 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;
7577
76 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);
109111
110 const scheduledId = await helper.scheduler.makeScheduledId();112 const scheduledId = await helper.arrange.makeScheduledId();
111 const waitForBlocks = 4;113 const waitForBlocks = 4;
112114
113 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 };
131133
132 const scheduledId = await helper.scheduler.makeScheduledId();134 const scheduledId = await helper.arrange.makeScheduledId();
133135
134 const amount = 1n * UNIQUE;136 const amount = 1n * UNIQUE;
135137
160 });162 });
161163
162 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;
165167
166 const initTestVal = 42;168 const initTestVal = 42;
189 });191 });
190192
191 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);
253255
254 const periodic = {256 const periodic = {
255 period: 5,257 period: 5,
277279
278 // Cancel the inc tx after 2 executions280 // Cancel the inc tx after 2 executions
279 // *in the same block* in which the second execution is scheduled281 // *in the same block* in which the second execution is scheduled
280 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);
282286
283 await helper.wait.newBlocks(blocksBeforeExecution);287 await helper.wait.newBlocks(blocksBeforeExecution);
284288
300 });304 });
301305
302 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 });
350
351 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);
354
355 const scheduledId = await helper.arrange.makeScheduledId();
356 const waitForBlocks = 4;
357
358 await token.scheduleAfter(scheduledId, waitForBlocks)
359 .transfer(bob, {Substrate: alice.address});
360
361 await helper.getSudo().scheduler.cancelScheduled(alice, scheduledId);
362
363 await helper.wait.newBlocks(waitForBlocks + 1);
364
365 expect(await token.getOwner()).to.be.deep.equal({Substrate: bob.address});
366 });
367
368 itSub.ifWithPallets('Root can set prioritized scheduled operation', [Pallets.Scheduler], async ({helper}) => {
369 const scheduledId = await helper.arrange.makeScheduledId();
370 const waitForBlocks = 4;
371
372 const amount = 42n * UNIQUE;
373
374 const balanceBefore = await helper.balance.getSubstrate(charlie.address);
375
376 await helper.getSudo()
377 .scheduler.scheduleAfter(scheduledId, waitForBlocks, {priority: 42})
378 .balance.forceTransferToSubstrate(alice, bob.address, charlie.address, amount);
379
380 await helper.wait.newBlocks(waitForBlocks + 1);
381
382 const balanceAfter = await helper.balance.getSubstrate(charlie.address);
383
384 expect(balanceAfter > balanceBefore).to.be.true;
385
386 const diff = balanceAfter - balanceBefore;
387 expect(diff).to.be.equal(amount);
388 });
389
390 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);
393
394 const scheduledId = await helper.arrange.makeScheduledId();
395 const waitForBlocks = 4;
396
397 await token.scheduleAfter(scheduledId, waitForBlocks)
398 .transfer(bob, {Substrate: alice.address});
399
400 const priority = 112;
401 await helper.getSudo().scheduler.changePriority(alice, scheduledId, priority);
402
403 const priorityChanged = await helper.wait.event(
404 waitForBlocks,
405 'scheduler',
406 'PriorityChanged',
407 );
408
409 expect(priorityChanged !== null).to.be.true;
410 expect(priorityChanged!.event.data[2].toString()).to.be.equal(priority.toString());
411 });
412
413 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);
418
419 const currentBlockNumber = await helper.chain.getLatestBlockNumber();
420 const blocksBeforeExecution = 4;
421 const firstExecutionBlockNumber = currentBlockNumber + blocksBeforeExecution;
422
423 const prioHigh = 0;
424 const prioLow = 255;
425
426 const periodic = {
427 period: 8,
428 repetitions: 2,
429 };
430
431 const amount = 1n * UNIQUE;
432
433 // Scheduler a task with a lower priority first, then with a higher priority
434 await helper.getSudo().scheduler.scheduleAt(scheduledFirstId, firstExecutionBlockNumber, {priority: prioLow, periodic})
435 .balance.forceTransferToSubstrate(alice, alice.address, bob.address, amount);
436
437 await helper.getSudo().scheduler.scheduleAt(scheduledSecondId, firstExecutionBlockNumber, {priority: prioHigh, periodic})
438 .balance.forceTransferToSubstrate(alice, alice.address, bob.address, amount);
439
440 const capture = await helper.arrange.captureEvents('scheduler', 'Dispatched');
441
442 await helper.wait.newBlocks(blocksBeforeExecution);
443
444 // Flip priorities
445 await helper.getSudo().scheduler.changePriority(alice, scheduledFirstId, prioHigh);
446 await helper.getSudo().scheduler.changePriority(alice, scheduledSecondId, prioLow);
447
448 await helper.wait.newBlocks(periodic.period);
449
450 const dispatchEvents = capture.extractCapturedEvents();
451 expect(dispatchEvents.length).to.be.equal(4);
452
453 const dispatchedIds = dispatchEvents.map(r => r.event.data[1].toString());
454
455 const firstExecuctionIds = [dispatchedIds[0], dispatchedIds[1]];
456 const secondExecuctionIds = [dispatchedIds[2], dispatchedIds[3]];
457
458 expect(firstExecuctionIds[0]).to.be.equal(scheduledSecondId);
459 expect(firstExecuctionIds[1]).to.be.equal(scheduledFirstId);
460
461 expect(secondExecuctionIds[0]).to.be.equal(scheduledFirstId);
462 expect(secondExecuctionIds[1]).to.be.equal(scheduledSecondId);
463 });
346});464});
347465
348describe('Negative Test: Scheduling', () => {466describe('Negative Test: Scheduling', () => {
356 });474 });
357 });475 });
358476
359 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);
362480
363 const scheduledId = await helper.scheduler.makeScheduledId();481 const scheduledId = await helper.arrange.makeScheduledId();
364 const waitForBlocks = 4;482 const waitForBlocks = 4;
365483
366 await token.scheduleAfter(scheduledId, waitForBlocks)484 await token.scheduleAfter(scheduledId, waitForBlocks)
367 .transfer(alice, {Substrate: bob.address});485 .transfer(alice, {Substrate: bob.address});
368486
369 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/);
372490
373 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 });
382500
383 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 });
388506
389 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);
392510
393 const scheduledId = await helper.scheduler.makeScheduledId();511 const scheduledId = await helper.arrange.makeScheduledId();
394 const waitForBlocks = 8;512 const waitForBlocks = 8;
395513
396 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);
403521
404 expect(await token.getOwner()).to.be.deep.equal({Substrate: bob.address});522 expect(await token.getOwner()).to.be.deep.equal({Substrate: bob.address});
523 });
524
525 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;
528
529 const amount = 42n * UNIQUE;
530
531 const balanceBefore = await helper.balance.getSubstrate(bob.address);
532
533 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/);
537
538 await helper.wait.newBlocks(waitForBlocks + 1);
539
540 const balanceAfter = await helper.balance.getSubstrate(bob.address);
541
542 expect(balanceAfter).to.be.equal(balanceBefore);
543 });
544
545 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);
548
549 const scheduledId = await helper.arrange.makeScheduledId();
550 const waitForBlocks = 4;
551
552 await token.scheduleAfter(scheduledId, waitForBlocks)
553 .transfer(bob, {Substrate: alice.address});
554
555 const priority = 112;
556 await expect(helper.scheduler.changePriority(alice, scheduledId, priority))
557 .to.be.rejectedWith(/badOrigin/);
558
559 const priorityChanged = await helper.wait.event(
560 waitForBlocks,
561 'scheduler',
562 'PriorityChanged',
563 );
564
565 expect(priorityChanged === null).to.be.true;
405 });566 });
406});567});
407568