Method -> Extrinsic circular dep (#495)

* Method -> Extrinsic circular dep

* Cleanup test formatting
This commit is contained in:
Jaco Greeff
2018-12-19 14:43:30 +01:00
committed by GitHub
parent ebb04d5e55
commit caff93941b
18 changed files with 71 additions and 72 deletions
+1 -1
View File
@@ -9,7 +9,7 @@ import Method from './Method';
describe('Extrinsic', () => {
beforeAll(() => {
Method.injectExtrinsics(extrinsics);
Method.injectMethods(extrinsics);
});
it('decodes a non-signed properly via JSON', () => {
+1 -1
View File
@@ -8,7 +8,7 @@ import Method from './Method';
describe('Method', () => {
beforeEach(() => {
Method.injectExtrinsics(extrinsics);
Method.injectMethods(extrinsics);
});
it('throws when not decodable', () => {
+20 -21
View File
@@ -9,14 +9,8 @@ import { assert, isHex, isObject, isU8a, hexToU8a } from '@polkadot/util';
import { getTypeDef, getTypeClass } from './codec/createType';
import Struct from './codec/Struct';
import U8aFixed from './codec/U8aFixed';
import Extrinsic from './Extrinsic';
import { FunctionMetadata, FunctionArgumentMetadata } from './Metadata/Modules';
const FN_UNKNOWN = {
method: 'unknown',
section: 'unknown'
} as ExtrinsicFunction;
interface ArgsDef {
[index: string]: Constructor;
}
@@ -31,8 +25,8 @@ interface DecodedMethod extends DecodeMethodInput {
meta: FunctionMetadata;
}
export interface ExtrinsicFunction {
(...args: any[]): Extrinsic;
export interface MethodFunction {
(...args: any[]): Method;
callIndex: Uint8Array;
meta: FunctionMetadata;
method: string;
@@ -40,15 +34,20 @@ export interface ExtrinsicFunction {
toJSON: () => any;
}
export interface ModuleExtrinsics {
[key: string]: ExtrinsicFunction;
export interface Methods {
[key: string]: MethodFunction;
}
export interface Extrinsics {
[key: string]: ModuleExtrinsics; // Will hold modules returned by state_getMetadata
export interface ModulesWithMethods {
[key: string]: Methods; // Will hold modules returned by state_getMetadata
}
const extrinsicFns: { [index: string]: ExtrinsicFunction } = {};
const FN_UNKNOWN = {
method: 'unknown',
section: 'unknown'
} as MethodFunction;
const injected: { [index: string]: MethodFunction } = {};
/**
* @name MethodIndex
@@ -88,7 +87,7 @@ export default class Method extends Struct {
* - hex
* - Uint8Array
* - {@see DecodeMethodInput}
* @param _meta - Metadata to use, so that `injectExtrinsics` lookup is not
* @param _meta - Metadata to use, so that `injectMethods` lookup is not
* necessary.
*/
private static decodeMethod (value: DecodedMethod | Uint8Array | string, _meta?: FunctionMetadata): DecodedMethod {
@@ -140,17 +139,17 @@ export default class Method extends Struct {
: [];
}
// We could only inject the meta (see injectExtrinsics below) and then do a
// We could only inject the meta (see injectMethods below) and then do a
// meta-only lookup via
//
// metadata.modules[callIndex[0]].module.call.functions[callIndex[1]]
//
// As a convenience helper though, we return the full constructor function,
// 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.');
static findFunction (callIndex: Uint8Array): MethodFunction {
assert(Object.keys(injected).length > 0, 'Calling Method.findFunction before extrinsics have been injected.');
return extrinsicFns[callIndex.toString()] || FN_UNKNOWN;
return injected[callIndex.toString()] || FN_UNKNOWN;
}
/**
@@ -172,10 +171,10 @@ export default class Method extends Struct {
// This is called/injected by the API on init, allowing a snapshot of
// the available system extrinsics to be used in lookups
static injectExtrinsics (extrinsics: Extrinsics): void {
Object.values(extrinsics).forEach((methods) =>
static injectMethods (moduleMethods: ModulesWithMethods): void {
Object.values(moduleMethods).forEach((methods) =>
Object.values(methods).forEach((method) =>
extrinsicFns[method.callIndex.toString()] = method
injected[method.callIndex.toString()] = method
)
);
}
+1 -1
View File
@@ -14,7 +14,7 @@ describe('SignedBlock', () => {
let block: SignedBlock;
beforeEach(() => {
Method.injectExtrinsics(extrinsics);
Method.injectMethods(extrinsics);
block = new SignedBlock(rpc.result);
});
+1 -1
View File
@@ -53,7 +53,7 @@ describe('Tuple', () => {
});
it('creates properly via actual hex string', () => {
Method.injectExtrinsics(extrinsics);
Method.injectMethods(extrinsics);
const test = new (Tuple.with([
BlockNumber, Proposal, VoteThreshold
+1 -1
View File
@@ -20,7 +20,7 @@ describe('Vector', () => {
beforeEach(() => {
vector = new Vector(Text, ['1', '23', '345', '4567', new Text('56789')]);
Method.injectExtrinsics(extrinsics);
Method.injectMethods(extrinsics);
});
it('wraps a sequence of values', () => {