git.delta.rocks / unique-network / refs/commits / 8483c298f6b0

difftreelog

test runtime upgrade helpers

Yaroslav Bolyukin2023-09-08parent: #911b03c.patch.diff
in: master

3 files changed

addedtests/src/util/authorizeEnactUpgrade.tsdiffbeforeafterboth
--- /dev/null
+++ b/tests/src/util/authorizeEnactUpgrade.ts
@@ -0,0 +1,19 @@
+import {readFile} from 'fs/promises';
+import {u8aToHex} from '@polkadot/util';
+import {usingPlaygrounds} from '.';
+import {blake2AsHex} from '@polkadot/util-crypto';
+
+
+const codePath = process.argv[2];
+if(!codePath) throw new Error('missing code path argument');
+
+const code = await readFile(codePath);
+
+await usingPlaygrounds(async (helper, privateKey) => {
+  const alice = await privateKey('//Alice');
+  const hex = blake2AsHex(code);
+  await helper.getSudo().executeExtrinsic(alice, 'api.tx.parachainSystem.authorizeUpgrade', [hex, true]);
+  await helper.getSudo().executeExtrinsicUncheckedWeight(alice, 'api.tx.parachainSystem.enactAuthorizedUpgrade', [u8aToHex(code)]);
+});
+// We miss disconnect/unref somewhere.
+process.exit(0);
modifiedtests/src/util/playgrounds/unique.tsdiffbeforeafterboth
732 }732 }
733 return result as any;733 return result as any;
734 }734 }
735 executeExtrinsicUncheckedWeight<
736 E extends string,
737 V extends (
738 ...args: any) => any = ForceFunction<
739 Get2<
740 AugmentedSubmittables<'promise'>,
741 E, (...args: any) => Invalid<'not found'>
742 >
743 >
744 >(
745 sender: TSigner,
746 extrinsic: `api.tx.${E}`,
747 params: Parameters<V>,
748 expectSuccess = true,
749 options: Partial<SignerOptions> | null = null,/*, failureMessage='expected success'*/
750 ): Promise<ITransactionResult> {
751 throw new Error('executeExtrinsicUncheckedWeight only supported in sudo');
752 }
735753
736 async callRpc754 async callRpc
737 // TODO: make it strongly typed, or use api.query/api.rpc directly755 // TODO: make it strongly typed, or use api.query/api.rpc directly
4056 }4074 }
4057 return result;4075 return result;
4058 }4076 }
4077 async executeExtrinsicUncheckedWeight(
4078 sender: IKeyringPair,
4079 extrinsic: string,
4080 params: any[],
4081 expectSuccess?: boolean,
4082 options: Partial<SignerOptions> | null = null,
4083 ): Promise<ITransactionResult> {
4084 const call = this.constructApiCall(extrinsic, params);
4085 const result = await super.executeExtrinsic(
4086 sender,
4087 'api.tx.sudo.sudoUncheckedWeight',
4088 [call, {refTime: 0, proofSize: 0}],
4089 expectSuccess,
4090 options,
4091 );
4092
4093 if(result.status === 'Fail') return result;
4094
4095 const data = (result.result.events.find(x => x.event.section == 'sudo' && x.event.method == 'Sudid')?.event.data as any).sudoResult;
4096 if(data.isErr) {
4097 if(data.asErr.isModule) {
4098 const error = (result.result.events[1].event.data as any).sudoResult.asErr.asModule;
4099 const metaError = super.getApi()?.registry.findMetaError(error);
4100 throw new Error(`${metaError.section}.${metaError.name}`);
4101 } else if(data.asErr.isToken) {
4102 throw new Error(`Token: ${data.asErr.asToken}`);
4103 }
4104 // May be [object Object] in case of unhandled non-unit enum
4105 throw new Error(`Misc: ${data.asErr.toHuman()}`);
4106 }
4107 return result;
4108 }
4059 };4109 };
4060}4110}
40614111
addedtests/src/util/setCode.tsdiffbeforeafterboth
--- /dev/null
+++ b/tests/src/util/setCode.ts
@@ -0,0 +1,15 @@
+import {readFile} from 'fs/promises';
+import {u8aToHex} from '@polkadot/util';
+import {usingPlaygrounds} from '.';
+
+const codePath = process.argv[2];
+if(!codePath) throw new Error('missing code path argument');
+
+const code = await readFile(codePath);
+
+await usingPlaygrounds(async (helper, privateKey) => {
+  const alice = await privateKey('//Alice');
+  await helper.getSudo().executeExtrinsicUncheckedWeight(alice, 'api.tx.system.setCode', [u8aToHex(code)]);
+});
+// We miss disconnect/unref somewhere.
+process.exit(0);