From 9f570cbbe9a9818debcdf090f10f0678bd23d69b Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Mon, 22 Oct 2018 12:10:39 +0200 Subject: [PATCH] Make Extrinsic compatible with Method (#335) * Make Extrinsic compatible with Method * Remove Method -> methodIndex, only use callIndex --- ...se-transfer.spec.js => promise-tx.spec.js} | 14 ++++++- .../src/utils/createUnchecked.ts | 6 ++- packages/types/src/Extrinsic.ts | 6 +++ packages/types/src/Method.spec.js | 11 ------ packages/types/src/Method.spec.ts | 28 ++++++++++++++ packages/types/src/Method.ts | 38 +++++++++++++------ ...ethodIndex.spec.js => MethodIndex.spec.ts} | 0 packages/types/src/MethodIndex.ts | 8 +--- 8 files changed, 79 insertions(+), 32 deletions(-) rename packages/api/test/e2e/{promise-transfer.spec.js => promise-tx.spec.js} (66%) delete mode 100644 packages/types/src/Method.spec.js create mode 100644 packages/types/src/Method.spec.ts rename packages/types/src/{MethodIndex.spec.js => MethodIndex.spec.ts} (100%) diff --git a/packages/api/test/e2e/promise-transfer.spec.js b/packages/api/test/e2e/promise-tx.spec.js similarity index 66% rename from packages/api/test/e2e/promise-transfer.spec.js rename to packages/api/test/e2e/promise-tx.spec.js index ac742e2f7c..9f3a579599 100644 --- a/packages/api/test/e2e/promise-transfer.spec.js +++ b/packages/api/test/e2e/promise-tx.spec.js @@ -10,13 +10,14 @@ const keyring = testingPairs(); describe.skip('e2e transfer', () => { let api; + let nonce; beforeEach(async () => { api = await Api.create(); + nonce = await api.query.system.accountNonce(keyring.alice.address()); }); it('makes a transfer', async () => { - const nonce = await api.query.system.accountNonce(keyring.alice.address()); const hash = await api.tx.balances .transfer(keyring.bob.address(), 12345) .sign(keyring.alice, nonce) @@ -26,4 +27,15 @@ describe.skip('e2e transfer', () => { hash.toString() ).not.toEqual('0x'); }); + + it('makes a proposal', async () => { + const hash = await api.tx.democracy + .propose(api.tx.consensus.setCode('0xdeadbeef'), 10000) + .sign(keyring.alice, nonce) + .send(); + + expect( + hash.toString() + ).not.toEqual('0x'); + }); }); diff --git a/packages/type-extrinsics/src/utils/createUnchecked.ts b/packages/type-extrinsics/src/utils/createUnchecked.ts index 366811e0b0..63d1ceb2e3 100644 --- a/packages/type-extrinsics/src/utils/createUnchecked.ts +++ b/packages/type-extrinsics/src/utils/createUnchecked.ts @@ -29,10 +29,12 @@ export default function createDescriptor ( throw new Error(`Extrinsic ${section}.${method} expects ${expectedArgs.length.valueOf()} arguments, got ${args.length}.`); } + console.error(`${section}.${method}`, args); + return new Extrinsic({ method: new Method({ - methodIndex: callIndex, - args + args, + callIndex }, meta) }); }; diff --git a/packages/types/src/Extrinsic.ts b/packages/types/src/Extrinsic.ts index 5a08f885f3..77d007b33d 100644 --- a/packages/types/src/Extrinsic.ts +++ b/packages/types/src/Extrinsic.ts @@ -8,6 +8,7 @@ import { AnyNumber, AnyU8a } from './types'; import { hexToU8a, isHex, isU8a, u8aConcat, u8aToHex } from '@polkadot/util'; import { blake2AsU8a } from '@polkadot/util-crypto'; +import Base from './codec/Base'; import Compact, { DEFAULT_LENGTH_BITS } from './codec/Compact'; import Struct from './codec/Struct'; import ExtrinsicSignature from './ExtrinsicSignature'; @@ -65,6 +66,11 @@ export default class Extrinsic extends Struct { return value as any; } + // expose args so it is compatible with Method (as constructor value) + get args (): Array { + return this.method.args; + } + // the actual [sectionIndex, methodIndex] as used get callIndex (): Uint8Array { return this.method.callIndex; diff --git a/packages/types/src/Method.spec.js b/packages/types/src/Method.spec.js deleted file mode 100644 index a75d8e1f95..0000000000 --- a/packages/types/src/Method.spec.js +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright 2017-2018 @polkadot/extrinsics authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import Method from './Method'; - -describe('Metadata', () => { - it('throws when not decodable', () => { - expect(() => new Method('foo')).toThrowError(/Method: cannot decode value/); - }); -}); diff --git a/packages/types/src/Method.spec.ts b/packages/types/src/Method.spec.ts new file mode 100644 index 0000000000..70553f9ce6 --- /dev/null +++ b/packages/types/src/Method.spec.ts @@ -0,0 +1,28 @@ +// Copyright 2017-2018 @polkadot/extrinsics authors & contributors +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. + +import extrinsics from '@polkadot/extrinsics/static'; + +import Method from './Method'; + +describe('Method', () => { + beforeEach(() => { + Method.injectExtrinsics(extrinsics); + }); + + it('throws when not decodable', () => { + expect( + () => new Method('foo') + ).toThrowError(/Method: cannot decode value/); + }); + + it('handles decoding correctly (bare)', () => { + expect( + new Method({ + args: [], + callIndex: [1, 2] + }).toU8a() + ).toEqual(new Uint8Array([1, 2])); + }); +}); diff --git a/packages/types/src/Method.ts b/packages/types/src/Method.ts index daab2a259c..224942a608 100644 --- a/packages/types/src/Method.ts +++ b/packages/types/src/Method.ts @@ -23,7 +23,7 @@ interface ArgsDef { interface DecodeMethodInput { args: any; - methodIndex: MethodIndex | Uint8Array; + callIndex: MethodIndex | Uint8Array; } interface DecodedMethod extends DecodeMethodInput { @@ -44,7 +44,7 @@ export default class Method extends Struct { const decoded = Method.decodeMethod(value, meta); super({ - methodIndex: MethodIndex, + callIndex: MethodIndex, args: Struct.with(decoded.argsDef) }, decoded); @@ -74,27 +74,40 @@ export default class Method extends Struct { // Get Struct definition of the arguments const argsDef = Method.getArgsDef(meta); - return { args: value.subarray(2), argsDef, methodIndex: callIndex, meta }; + return { + args: value.subarray(2), + argsDef, + callIndex, + meta + }; } else if ( isObject(value) && - value.methodIndex && + value.callIndex && value.args ) { - // Get the correct callIndex - const callIndex = value.methodIndex instanceof MethodIndex - ? value.methodIndex.callIndex - : value.methodIndex; + // destructure value, we only pass args/methodsIndex out + const { args, callIndex } = value; + + // Get the correct lookupIndex + const lookupIndex = callIndex instanceof MethodIndex + ? callIndex.toU8a() + : callIndex; // Find metadata with callIndex - const meta = _meta || Method.findFunction(callIndex).meta; + const meta = _meta || Method.findFunction(lookupIndex).meta; // Get Struct definition of the arguments const argsDef = Method.getArgsDef(meta); - return { ...value, argsDef, meta }; + return { + args, + argsDef, + meta, + callIndex + }; } - throw new Error(`Method: cannot decode value "${value}".`); + throw new Error(`Method: cannot decode value '${value}' or type ${typeof value}`); } // If the extrinsic function has an argument of type `Origin`, we ignore it @@ -116,6 +129,7 @@ export default class Method extends Struct { // which includes the meta, name, section & actual interface for calling static findFunction (callIndex: Uint8Array): ExtrinsicFunction { assert(Object.keys(extrinsicFns).length > 0, 'Calling Method.findFunction before extrinsics have been injected.'); + return extrinsicFns[callIndex.toString()] || FN_UNKNOWN; } @@ -156,7 +170,7 @@ export default class Method extends Struct { } get callIndex (): Uint8Array { - return (this.get('methodIndex') as MethodIndex).callIndex; + return (this.get('callIndex') as MethodIndex).toU8a(); } get data (): Uint8Array { diff --git a/packages/types/src/MethodIndex.spec.js b/packages/types/src/MethodIndex.spec.ts similarity index 100% rename from packages/types/src/MethodIndex.spec.js rename to packages/types/src/MethodIndex.spec.ts diff --git a/packages/types/src/MethodIndex.ts b/packages/types/src/MethodIndex.ts index 2242e6a668..35f66a5604 100644 --- a/packages/types/src/MethodIndex.ts +++ b/packages/types/src/MethodIndex.ts @@ -11,15 +11,11 @@ export default class MethodIndex extends U8aFixed { super(value, 16); } - get callIndex (): Uint8Array { - return this.raw; - } - get methodIndex (): number { - return this.callIndex[1]; + return this.raw[1]; } get sectionIndex (): number { - return this.callIndex[0]; + return this.raw[0]; } }