git.delta.rocks / unique-network / refs/commits / 0f67e15c3f4a

difftreelog

test relay does root ops

Daniel Shiposha2023-09-28parent: #f61ec5a.patch.diff
in: master

3 files changed

modifiedtests/src/xcm/lowLevelXcmQuartz.test.tsdiffbeforeafterboth
15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
1616
17import {IKeyringPair} from '@polkadot/types/types';17import {IKeyringPair} from '@polkadot/types/types';
18import {itSub, expect, describeXCM, usingPlaygrounds, usingKaruraPlaygrounds, usingMoonriverPlaygrounds, usingShidenPlaygrounds} from '../util';18import {itSub, expect, describeXCM, usingPlaygrounds, usingKaruraPlaygrounds, usingMoonriverPlaygrounds, usingShidenPlaygrounds, usingRelayPlaygrounds} from '../util';
19import {DevUniqueHelper, Event} from '../util/playgrounds/unique.dev';19import {DevUniqueHelper, Event} from '../util/playgrounds/unique.dev';
20import {STATEMINE_CHAIN, QUARTZ_CHAIN, KARURA_CHAIN, MOONRIVER_CHAIN, SHIDEN_CHAIN, STATEMINE_DECIMALS, KARURA_DECIMALS, QTZ_DECIMALS, RELAY_DECIMALS, SHIDEN_DECIMALS, karuraUrl, moonriverUrl, relayUrl, shidenUrl, statemineUrl, SAFE_XCM_VERSION, XcmTestHelper, TRANSFER_AMOUNT} from './xcm.types';20import {STATEMINE_CHAIN, QUARTZ_CHAIN, KARURA_CHAIN, MOONRIVER_CHAIN, SHIDEN_CHAIN, STATEMINE_DECIMALS, KARURA_DECIMALS, QTZ_DECIMALS, RELAY_DECIMALS, SHIDEN_DECIMALS, karuraUrl, moonriverUrl, relayUrl, shidenUrl, statemineUrl, SAFE_XCM_VERSION, XcmTestHelper, TRANSFER_AMOUNT} from './xcm.types';
2121
380 });380 });
381});381});
382
383describeXCM('[XCMLL] Integration test: The relay can do some root ops', () => {
384 let sudoer: IKeyringPair;
385
386 before(async function () {
387 await usingRelayPlaygrounds(relayUrl, async (_, privateKey) => {
388 sudoer = await privateKey('//Alice');
389 });
390 });
391
392 // At the moment there is no reliable way
393 // to establish the correspondence between the `ExecutedDownward` event
394 // and the relay's sent message due to `SetTopic` instruction
395 // containing an unpredictable topic silently added by the relay on the router level.
396 // This changes the message hash on arrival to our chain.
397 //
398 // See:
399 // * The relay's router: https://github.com/paritytech/polkadot-sdk/blob/f60318f68687e601c47de5ad5ca88e2c3f8139a7/polkadot/runtime/westend/src/xcm_config.rs#L83
400 // * The `WithUniqueTopic` helper: https://github.com/paritytech/polkadot-sdk/blob/945ebbbcf66646be13d5b1d1bc26c8b0d3296d9e/polkadot/xcm/xcm-builder/src/routing.rs#L36
401 //
402 // Because of this, we insert time gaps between tests so
403 // different `ExecutedDownward` events won't interfere with each other.
404 afterEach(async () => {
405 await usingPlaygrounds(async (helper) => {
406 await helper.wait.newBlocks(3);
407 });
408 });
409
410 itSub('The relay can set storage', async () => {
411 await testHelper.relayIsPermittedToSetStorage(sudoer, 'plain');
412 });
413
414 itSub('The relay can batch set storage', async () => {
415 await testHelper.relayIsPermittedToSetStorage(sudoer, 'batch');
416 });
417
418 itSub('The relay can batchAll set storage', async () => {
419 await testHelper.relayIsPermittedToSetStorage(sudoer, 'batchAll');
420 });
421
422 itSub('The relay can forceBatch set storage', async () => {
423 await testHelper.relayIsPermittedToSetStorage(sudoer, 'forceBatch');
424 });
425
426 itSub('[negative] The relay cannot set balance', async () => {
427 await testHelper.relayIsNotPermittedToSetBalance(sudoer, 'plain');
428 });
429
430 itSub('[negative] The relay cannot set balance via batch', async () => {
431 await testHelper.relayIsNotPermittedToSetBalance(sudoer, 'batch');
432 });
433
434 itSub('[negative] The relay cannot set balance via batchAll', async () => {
435 await testHelper.relayIsNotPermittedToSetBalance(sudoer, 'batchAll');
436 });
437
438 itSub('[negative] The relay cannot set balance via forceBatch', async () => {
439 await testHelper.relayIsNotPermittedToSetBalance(sudoer, 'forceBatch');
440 });
441
442 itSub('[negative] The relay cannot set balance via dispatchAs', async () => {
443 await testHelper.relayIsNotPermittedToSetBalance(sudoer, 'dispatchAs');
444 });
445});
382446
modifiedtests/src/xcm/lowLevelXcmUnique.test.tsdiffbeforeafterboth
1616
17import {IKeyringPair} from '@polkadot/types/types';17import {IKeyringPair} from '@polkadot/types/types';
18import config from '../config';18import config from '../config';
19import {itSub, expect, describeXCM, usingPlaygrounds, usingAcalaPlaygrounds, usingMoonbeamPlaygrounds, usingAstarPlaygrounds, usingPolkadexPlaygrounds} from '../util';19import {itSub, expect, describeXCM, usingPlaygrounds, usingAcalaPlaygrounds, usingMoonbeamPlaygrounds, usingAstarPlaygrounds, usingPolkadexPlaygrounds, usingRelayPlaygrounds, requirePalletsOrSkip, Pallets} from '../util';
20import {Event} from '../util/playgrounds/unique.dev';20import {Event} from '../util/playgrounds/unique.dev';
21import {nToBigInt} from '@polkadot/util';21import {nToBigInt} from '@polkadot/util';
22import {hexToString} from '@polkadot/util';22import {hexToString} from '@polkadot/util';
23import {ASTAR_DECIMALS, NETWORKS, SAFE_XCM_VERSION, UNIQUE_CHAIN, UNQ_DECIMALS, XcmTestHelper, acalaUrl, astarUrl, expectFailedToTransact, expectUntrustedReserveLocationFail, getDevPlayground, mapToChainId, mapToChainUrl, maxWaitBlocks, moonbeamUrl, polkadexUrl, uniqueAssetId, uniqueVersionedMultilocation} from './xcm.types';23import {ASTAR_DECIMALS, NETWORKS, SAFE_XCM_VERSION, UNIQUE_CHAIN, UNQ_DECIMALS, XcmTestHelper, acalaUrl, astarUrl, expectFailedToTransact, expectUntrustedReserveLocationFail, getDevPlayground, mapToChainId, mapToChainUrl, maxWaitBlocks, moonbeamUrl, polkadexUrl, relayUrl, uniqueAssetId, uniqueVersionedMultilocation} from './xcm.types';
2424
2525
26const TRANSFER_AMOUNT = 2000000_000_000_000_000_000_000n;26const TRANSFER_AMOUNT = 2000000_000_000_000_000_000_000n;
673 });673 });
674});674});
675
676describeXCM('[XCMLL] Integration test: The relay can do some root ops', () => {
677 let sudoer: IKeyringPair;
678
679 before(async function () {
680 await usingRelayPlaygrounds(relayUrl, async (_, privateKey) => {
681 sudoer = await privateKey('//Alice');
682 });
683 });
684
685 // At the moment there is no reliable way
686 // to establish the correspondence between the `ExecutedDownward` event
687 // and the relay's sent message due to `SetTopic` instruction
688 // containing an unpredictable topic silently added by the relay on the router level.
689 // This changes the message hash on arrival to our chain.
690 //
691 // See:
692 // * The relay's router: https://github.com/paritytech/polkadot-sdk/blob/f60318f68687e601c47de5ad5ca88e2c3f8139a7/polkadot/runtime/westend/src/xcm_config.rs#L83
693 // * The `WithUniqueTopic` helper: https://github.com/paritytech/polkadot-sdk/blob/945ebbbcf66646be13d5b1d1bc26c8b0d3296d9e/polkadot/xcm/xcm-builder/src/routing.rs#L36
694 //
695 // Because of this, we insert time gaps between tests so
696 // different `ExecutedDownward` events won't interfere with each other.
697 afterEach(async () => {
698 await usingPlaygrounds(async (helper) => {
699 await helper.wait.newBlocks(3);
700 });
701 });
702
703 itSub('The relay can set storage', async () => {
704 await testHelper.relayIsPermittedToSetStorage(sudoer, 'plain');
705 });
706
707 itSub('The relay can batch set storage', async () => {
708 await testHelper.relayIsPermittedToSetStorage(sudoer, 'batch');
709 });
710
711 itSub('The relay can batchAll set storage', async () => {
712 await testHelper.relayIsPermittedToSetStorage(sudoer, 'batchAll');
713 });
714
715 itSub('The relay can forceBatch set storage', async () => {
716 await testHelper.relayIsPermittedToSetStorage(sudoer, 'forceBatch');
717 });
718
719 itSub('[negative] The relay cannot set balance', async () => {
720 await testHelper.relayIsNotPermittedToSetBalance(sudoer, 'plain');
721 });
722
723 itSub('[negative] The relay cannot set balance via batch', async () => {
724 await testHelper.relayIsNotPermittedToSetBalance(sudoer, 'batch');
725 });
726
727 itSub('[negative] The relay cannot set balance via batchAll', async () => {
728 await testHelper.relayIsNotPermittedToSetBalance(sudoer, 'batchAll');
729 });
730
731 itSub('[negative] The relay cannot set balance via forceBatch', async () => {
732 await testHelper.relayIsNotPermittedToSetBalance(sudoer, 'forceBatch');
733 });
734
735 itSub('[negative] The relay cannot set balance via dispatchAs', async () => {
736 await testHelper.relayIsNotPermittedToSetBalance(sudoer, 'dispatchAs');
737 });
738});
675739
modifiedtests/src/xcm/xcm.types.tsdiffbeforeafterboth
1import {IKeyringPair} from '@polkadot/types/types';1import {IKeyringPair} from '@polkadot/types/types';
2import {hexToString} from '@polkadot/util';
2import {expect, usingAcalaPlaygrounds, usingAstarPlaygrounds, usingKaruraPlaygrounds, usingMoonbeamPlaygrounds, usingMoonriverPlaygrounds, usingPlaygrounds, usingPolkadexPlaygrounds, usingShidenPlaygrounds} from '../util';3import {expect, usingAcalaPlaygrounds, usingAstarPlaygrounds, usingKaruraPlaygrounds, usingMoonbeamPlaygrounds, usingMoonriverPlaygrounds, usingPlaygrounds, usingPolkadexPlaygrounds, usingRelayPlaygrounds, usingShidenPlaygrounds} from '../util';
3import {DevUniqueHelper, Event} from '../util/playgrounds/unique.dev';4import {DevUniqueHelper, Event} from '../util/playgrounds/unique.dev';
4import config from '../config';5import config from '../config';
6import {blake2AsHex} from '@polkadot/util-crypto';
57
6export const UNIQUE_CHAIN = +(process.env.RELAY_UNIQUE_ID || 2037);8export const UNIQUE_CHAIN = +(process.env.RELAY_UNIQUE_ID || 2037);
7export const STATEMINT_CHAIN = +(process.env.RELAY_STATEMINT_ID || 1000);9export const STATEMINT_CHAIN = +(process.env.RELAY_STATEMINT_ID || 1000);
68 && event.outcome.isUntrustedReserveLocation);70 && event.outcome.isUntrustedReserveLocation);
69};71};
72
73export const expectDownwardXcmNoPermission = async (helper: DevUniqueHelper) => {
74 // The correct messageHash for downward messages can't be reliably obtained
75 await helper.wait.expectEvent(maxWaitBlocks, Event.DmpQueue.ExecutedDownward, event => event.outcome.asIncomplete[1].isNoPermission);
76};
77
78export const expectDownwardXcmComplete = async (helper: DevUniqueHelper) => {
79 // The correct messageHash for downward messages can't be reliably obtained
80 await helper.wait.expectEvent(maxWaitBlocks, Event.DmpQueue.ExecutedDownward, event => event.outcome.isComplete);
81};
7082
71export const NETWORKS = {83export const NETWORKS = {
72 acala: usingAcalaPlaygrounds,84 acala: usingAcalaPlaygrounds,
161 }173 }
162 }174 }
175
176 uniqueChainMultilocationForRelay() {
177 return {
178 V3: {
179 parents: 0,
180 interior: {
181 X1: {Parachain: this._getNativeId()},
182 },
183 },
184 };
185 }
163186
164 async sendUnqTo(187 async sendUnqTo(
165 networkName: keyof typeof NETWORKS,188 networkName: keyof typeof NETWORKS,
470 });493 });
471 }494 }
472495
496 private async _relayXcmTransactSetStorage(variant: 'plain' | 'batch' | 'batchAll' | 'forceBatch') {
497 // eslint-disable-next-line require-await
498 return await usingPlaygrounds(async (helper) => {
499 const relayForceKV = () => {
500 const random = Math.random();
501 const key = `relay-forced-key (instance: ${random})`;
502 const val = `relay-forced-value (instance: ${random})`;
503 const call = helper.constructApiCall('api.tx.system.setStorage', [[[key, val]]]).method.toHex();
504
505 return {
506 call,
507 key,
508 val,
509 };
510 };
511
512 if(variant == 'plain') {
513 const kv = relayForceKV();
514 return {
515 program: helper.arrange.makeTransactProgram({
516 weightMultiplier: 1,
517 call: kv.call,
518 }),
519 kvs: [kv],
520 };
521 } else {
522 const kv0 = relayForceKV();
523 const kv1 = relayForceKV();
524
525 const batchCall = helper.constructApiCall(`api.tx.utility.${variant}`, [[kv0.call, kv1.call]]).method.toHex();
526 return {
527 program: helper.arrange.makeTransactProgram({
528 weightMultiplier: 2,
529 call: batchCall,
530 }),
531 kvs: [kv0, kv1],
532 };
533 }
534 });
535 }
536
537 async relayIsPermittedToSetStorage(relaySudoer: IKeyringPair, variant: 'plain' | 'batch' | 'batchAll' | 'forceBatch') {
538 const {program, kvs} = await this._relayXcmTransactSetStorage(variant);
539
540 await usingRelayPlaygrounds(relayUrl, async (helper) => {
541 await helper.getSudo().executeExtrinsic(relaySudoer, 'api.tx.xcmPallet.send', [
542 this.uniqueChainMultilocationForRelay(),
543 program,
544 ]);
545 });
546
547 await usingPlaygrounds(async (helper) => {
548 await expectDownwardXcmComplete(helper);
549
550 for(const kv of kvs) {
551 const forcedValue = await helper.callRpc('api.rpc.state.getStorage', [kv.key]);
552 expect(hexToString(forcedValue.toHex())).to.be.equal(kv.val);
553 }
554 });
555 }
556
557 private async _relayXcmTransactSetBalance(variant: 'plain' | 'batch' | 'batchAll' | 'forceBatch' | 'dispatchAs') {
558 // eslint-disable-next-line require-await
559 return await usingPlaygrounds(async (helper) => {
560 const emptyAccount = helper.arrange.createEmptyAccount().address;
561
562 const forceSetBalanceCall = helper.constructApiCall('api.tx.balances.forceSetBalance', [emptyAccount, 10_000n]).method.toHex();
563
564 let call;
565
566 if(variant == 'plain') {
567 call = forceSetBalanceCall;
568
569 } else if(variant == 'dispatchAs') {
570 call = helper.constructApiCall('api.tx.utility.dispatchAs', [
571 {
572 system: 'Root',
573 },
574 forceSetBalanceCall,
575 ]).method.toHex();
576 } else {
577 call = helper.constructApiCall(`api.tx.utility.${variant}`, [[forceSetBalanceCall]]).method.toHex();
578 }
579
580 return {
581 program: helper.arrange.makeTransactProgram({
582 weightMultiplier: 1,
583 call,
584 }),
585 emptyAccount,
586 };
587 });
588 }
589
590 async relayIsNotPermittedToSetBalance(
591 relaySudoer: IKeyringPair,
592 variant: 'plain' | 'batch' | 'batchAll' | 'forceBatch' | 'dispatchAs',
593 ) {
594 const {program, emptyAccount} = await this._relayXcmTransactSetBalance(variant);
595
596 await usingRelayPlaygrounds(relayUrl, async (helper) => {
597 await helper.getSudo().executeExtrinsic(relaySudoer, 'api.tx.xcmPallet.send', [
598 this.uniqueChainMultilocationForRelay(),
599 program,
600 ]);
601 });
602
603 await usingPlaygrounds(async (helper) => {
604 await expectDownwardXcmNoPermission(helper);
605 expect(await helper.balance.getSubstrate(emptyAccount)).to.be.equal(0n);
606 });
607 }
473}608}
609