Pass registry to all codec types (#1592)
* Pass registry to all codec types
* Pass through registry inside API (where the rubber meets the road)
* Small additions
* Derive w/ registry
* Types are more-or-less covered
* Some test adjustments (guided by linting)
* Rid the world of some ../injector
* Additional cleanups, merge injector into registry
* Cleanup extrinsics (always passing registry)
* Remove injector imports (not all replaced)
* Typ spec cleanups
* Phase 1 - types seem lint-free
* rpc-{core,provider} lint free
* Start working through metadata
* Small metadata additions
* Some e2e tests (out-of-order)
* Down to a handful
* Fixup tests for packages/types/src/codec/*.spec.ts
* packages/types/src/codec passes all tests
* packages/types ok (Convert storage with registry fn)
* progress (sort out injection, dreaded find-before msg)
* Down to 1 lint issue...
* Linting done... along with on-demand import
* Exposed ordering
* Remove global injects
* Cleanup comments
* Add CHANGELOG & UPGRADING
* Apply suggestions from code review
Co-Authored-By: Amaury Martiny <amaury.martiny@protonmail.com>
* Update Metadata description
This commit is contained in:
co-authored by
Amaury Martiny
parent
099992627b
commit
011e24bd49
@@ -4,16 +4,17 @@
|
||||
|
||||
import testingPairs from '@polkadot/keyring/testingPairs';
|
||||
import MockProvider from '@polkadot/rpc-provider/mock';
|
||||
import { createType } from '@polkadot/types';
|
||||
import { createType, TypeRegistry } from '@polkadot/types';
|
||||
|
||||
import Rpc from '.';
|
||||
|
||||
describe('Cached Observables', (): void => {
|
||||
const registry = new TypeRegistry();
|
||||
let rpc: Rpc;
|
||||
const keyring = testingPairs();
|
||||
|
||||
beforeEach((): void => {
|
||||
rpc = new Rpc(new MockProvider());
|
||||
rpc = new Rpc(registry, new MockProvider(registry));
|
||||
});
|
||||
|
||||
it('creates a single observable for subscriptions (multiple calls)', (): void => {
|
||||
@@ -32,7 +33,7 @@ describe('Cached Observables', (): void => {
|
||||
|
||||
it('creates a single observable (multiple calls, different arguments that should be cached together)', (): void => {
|
||||
const observable1 = rpc.state.subscribeStorage([keyring.alice.address]);
|
||||
const observable2 = rpc.state.subscribeStorage([createType('AccountId', keyring.alice.address)]);
|
||||
const observable2 = rpc.state.subscribeStorage([createType(registry, 'AccountId', keyring.alice.address)]);
|
||||
|
||||
expect(observable2).toBe(observable1);
|
||||
});
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import '@polkadot/types/injector';
|
||||
|
||||
import { Balance } from '@polkadot/types/interfaces';
|
||||
import { Codec } from '@polkadot/types/types';
|
||||
|
||||
@@ -14,9 +12,11 @@ import rpcMetadataV5 from '@polkadot/metadata/Metadata/v5/static';
|
||||
import rpcMetadataV6 from '@polkadot/metadata/Metadata/v6/static';
|
||||
import rpcMetadataV7 from '@polkadot/metadata/Metadata/v7/static';
|
||||
import rpcMetadataV8 from '@polkadot/metadata/Metadata/v8/static';
|
||||
import { TypeRegistry } from '@polkadot/types';
|
||||
|
||||
import Api from '.';
|
||||
import Rpc from '.';
|
||||
|
||||
const registry = new TypeRegistry();
|
||||
const ADDR_ONE = '5DkQbYAExs3M2sZgT1Ec3mKfZnAQCL4Dt9beTCknkCUn5jzo';
|
||||
const ADDR_TWO = '5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF';
|
||||
const BALANCE_KEYS = [
|
||||
@@ -29,7 +29,7 @@ function formattingTests (version: string, decorated: Metadata, encodedValues: [
|
||||
const [ENC_ONE, ENC_TWO, CONTRACT_KEY] = encodedValues;
|
||||
|
||||
describe(`formatting with Metadata ${version}`, (): void => {
|
||||
let api: Api;
|
||||
let api: Rpc;
|
||||
let provider: any;
|
||||
|
||||
beforeEach((): void => {
|
||||
@@ -73,7 +73,7 @@ function formattingTests (version: string, decorated: Metadata, encodedValues: [
|
||||
})
|
||||
};
|
||||
|
||||
api = new Api(provider);
|
||||
api = new Rpc(registry, provider);
|
||||
});
|
||||
|
||||
it('encodes key (with params), decoding response', (done): void => {
|
||||
@@ -182,37 +182,37 @@ function formattingTests (version: string, decorated: Metadata, encodedValues: [
|
||||
});
|
||||
}
|
||||
|
||||
formattingTests('v3', new Metadata(rpcMetadataV3), [
|
||||
formattingTests('v3', new Metadata(registry, rpcMetadataV3), [
|
||||
'0x4af2c53fce3ec33c6ccccf22e926f1a7',
|
||||
'0x3e62f7ed6e788e1337bce2a97b68a12a',
|
||||
'0x777519cd81f845abdb40d253923d6098'
|
||||
]);
|
||||
|
||||
formattingTests('v4', new Metadata(rpcMetadataV4), [
|
||||
formattingTests('v4', new Metadata(registry, rpcMetadataV4), [
|
||||
'0xec8f96437274a883afcac82d01a9defeb68209cd4f2c084632813692aa5e65ad',
|
||||
'0x1dbb0224910f42a14e7f1406b24c6fe8157296691b02a78756e01946038fffab',
|
||||
'0xc7879f4faa637a90d782070a3cb6be99a9fb0316e19a0454ce93c4f0a34712f1'
|
||||
]);
|
||||
|
||||
formattingTests('v5', new Metadata(rpcMetadataV5), [
|
||||
formattingTests('v5', new Metadata(registry, rpcMetadataV5), [
|
||||
'0xec8f96437274a883afcac82d01a9defeb68209cd4f2c084632813692aa5e65ad',
|
||||
'0x1dbb0224910f42a14e7f1406b24c6fe8157296691b02a78756e01946038fffab',
|
||||
'0xc7879f4faa637a90d782070a3cb6be99a9fb0316e19a0454ce93c4f0a34712f1'
|
||||
]);
|
||||
|
||||
formattingTests('v6', new Metadata(rpcMetadataV6), [
|
||||
formattingTests('v6', new Metadata(registry, rpcMetadataV6), [
|
||||
'0xec8f96437274a883afcac82d01a9defeb68209cd4f2c084632813692aa5e65ad',
|
||||
'0x1dbb0224910f42a14e7f1406b24c6fe8157296691b02a78756e01946038fffab',
|
||||
'0xc7879f4faa637a90d782070a3cb6be99a9fb0316e19a0454ce93c4f0a34712f1'
|
||||
]);
|
||||
|
||||
formattingTests('v7', new Metadata(rpcMetadataV7), [
|
||||
formattingTests('v7', new Metadata(registry, rpcMetadataV7), [
|
||||
'0xec8f96437274a883afcac82d01a9defeb68209cd4f2c084632813692aa5e65ad',
|
||||
'0x1dbb0224910f42a14e7f1406b24c6fe8157296691b02a78756e01946038fffab',
|
||||
'0xc7879f4faa637a90d782070a3cb6be99a9fb0316e19a0454ce93c4f0a34712f1'
|
||||
]);
|
||||
|
||||
formattingTests('v8', new Metadata(rpcMetadataV8), [
|
||||
formattingTests('v8', new Metadata(registry, rpcMetadataV8), [
|
||||
'0xec8f96437274a883afcac82d01a9defeb68209cd4f2c084632813692aa5e65ad',
|
||||
'0x1dbb0224910f42a14e7f1406b24c6fe8157296691b02a78756e01946038fffab',
|
||||
'0xc7879f4faa637a90d782070a3cb6be99a9fb0316e19a0454ce93c4f0a34712f1'
|
||||
|
||||
@@ -3,30 +3,33 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import MockProvider from '@polkadot/rpc-provider/mock';
|
||||
import { TypeRegistry } from '@polkadot/types';
|
||||
import { isFunction } from '@polkadot/util';
|
||||
|
||||
import Rpc from '.';
|
||||
|
||||
describe('Api', (): void => {
|
||||
const registry = new TypeRegistry();
|
||||
|
||||
it('requires a provider with a send method', (): void => {
|
||||
expect(
|
||||
(): Rpc => new Rpc({} as any)
|
||||
(): Rpc => new Rpc(registry, {} as any)
|
||||
).toThrow(/Expected Provider/);
|
||||
});
|
||||
|
||||
it('creates an instance with all sections', (): void => {
|
||||
const rpc = new Rpc(new MockProvider());
|
||||
const rpc = new Rpc(registry, new MockProvider(registry));
|
||||
|
||||
expect(
|
||||
Object.keys(rpc).filter((key): boolean => !key.startsWith('_'))
|
||||
).toEqual([
|
||||
'provider', 'mapping', 'sections',
|
||||
'mapping', 'provider', 'registry', 'sections',
|
||||
'account', 'author', 'chain', 'contracts', 'payment', 'rpc', 'state', 'system'
|
||||
]);
|
||||
});
|
||||
|
||||
it('allows for the definition of user RPCs', (): void => {
|
||||
const rpc = new Rpc(new MockProvider(), {
|
||||
const rpc = new Rpc(registry, new MockProvider(registry), {
|
||||
testing: [
|
||||
{
|
||||
name: 'foo',
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
import { ProviderInterface } from '@polkadot/rpc-provider/types';
|
||||
import { RpcMethod, RpcSection, RpcParam } from '@polkadot/jsonrpc/types';
|
||||
import { AnyJson, Codec } from '@polkadot/types/types';
|
||||
import { AnyJson, Codec, Registry } from '@polkadot/types/types';
|
||||
import { RpcInterface } from './jsonrpc.types';
|
||||
import { RpcInterfaceMethod, UserRpc } from './types';
|
||||
|
||||
@@ -68,9 +68,11 @@ function createErrorMessage ({ method, params, type }: RpcMethod, error: Error):
|
||||
export default class Rpc implements RpcInterface {
|
||||
private _storageCache = new Map<string, string | null>();
|
||||
|
||||
public readonly mapping: Map<string, RpcMethod> = new Map();
|
||||
|
||||
public readonly provider: ProviderInterface;
|
||||
|
||||
public readonly mapping: Map<string, RpcMethod> = new Map();
|
||||
public readonly registry: Registry;
|
||||
|
||||
public readonly sections: string[] = [];
|
||||
|
||||
@@ -99,10 +101,11 @@ export default class Rpc implements RpcInterface {
|
||||
* Default constructor for the Api Object
|
||||
* @param {ProviderInterface} provider An API provider using HTTP or WebSocket
|
||||
*/
|
||||
constructor (provider: ProviderInterface, userRpc: UserRpc = {}) {
|
||||
constructor (registry: Registry, provider: ProviderInterface, userRpc: UserRpc = {}) {
|
||||
// eslint-disable-next-line @typescript-eslint/unbound-method
|
||||
assert(provider && isFunction(provider.send), 'Expected Provider to API create');
|
||||
|
||||
this.registry = registry;
|
||||
this.provider = provider;
|
||||
|
||||
this.createInterfaces(jsonrpc, userRpc);
|
||||
@@ -307,7 +310,7 @@ export default class Rpc implements RpcInterface {
|
||||
assert(inputs.length >= reqArgCount && inputs.length <= method.params.length, `Expected ${method.params.length} parameters${optText}, ${inputs.length} found instead`);
|
||||
|
||||
return inputs.map((input, index): Codec =>
|
||||
createTypeUnsafe(method.params[index].type, [input])
|
||||
createTypeUnsafe(this.registry, method.params[index].type, [input])
|
||||
);
|
||||
}
|
||||
|
||||
@@ -351,7 +354,7 @@ export default class Rpc implements RpcInterface {
|
||||
}, [] as Codec[]);
|
||||
}
|
||||
|
||||
return createTypeUnsafe(method.type, [result]);
|
||||
return createTypeUnsafe(this.registry, method.type, [result]);
|
||||
}
|
||||
|
||||
private formatStorageData (key: StorageKey, value: string | null): Codec {
|
||||
@@ -371,14 +374,15 @@ export default class Rpc implements RpcInterface {
|
||||
|
||||
if (meta.modifier.isOptional) {
|
||||
return new Option(
|
||||
createClass(type),
|
||||
this.registry,
|
||||
createClass(this.registry, type),
|
||||
isEmpty
|
||||
? null
|
||||
: createTypeUnsafe(type, [input], true)
|
||||
: createTypeUnsafe(this.registry, type, [input], true)
|
||||
);
|
||||
}
|
||||
|
||||
return createTypeUnsafe(type, [isEmpty ? meta.fallback : input], true);
|
||||
return createTypeUnsafe(this.registry, type, [isEmpty ? meta.fallback : input], true);
|
||||
}
|
||||
|
||||
private formatStorageSet (key: StorageKey, changes: [string, string | null][], witCache: boolean): Codec {
|
||||
@@ -412,13 +416,14 @@ export default class Rpc implements RpcInterface {
|
||||
|
||||
if (meta.modifier.isOptional) {
|
||||
return new Option(
|
||||
createClass(type),
|
||||
this.registry,
|
||||
createClass(this.registry, type),
|
||||
isEmpty
|
||||
? null
|
||||
: createTypeUnsafe(type, [input], true)
|
||||
: createTypeUnsafe(this.registry, type, [input], true)
|
||||
);
|
||||
}
|
||||
|
||||
return createTypeUnsafe(type, [isEmpty ? meta.fallback : input], true);
|
||||
return createTypeUnsafe(this.registry, type, [isEmpty ? meta.fallback : input], true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,9 +2,12 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { TypeRegistry } from '@polkadot/types';
|
||||
|
||||
import Rpc from '.';
|
||||
|
||||
describe('methodSend', (): void => {
|
||||
const registry = new TypeRegistry();
|
||||
let rpc: Rpc;
|
||||
let methods: any;
|
||||
let provider: any;
|
||||
@@ -33,7 +36,7 @@ describe('methodSend', (): void => {
|
||||
})
|
||||
};
|
||||
|
||||
rpc = new Rpc(provider);
|
||||
rpc = new Rpc(registry, provider);
|
||||
});
|
||||
|
||||
it('wraps errors with the call signature', (done): void => {
|
||||
|
||||
@@ -4,14 +4,16 @@
|
||||
|
||||
import { Observable, of } from 'rxjs';
|
||||
import MockProvider from '@polkadot/rpc-provider/mock';
|
||||
import { TypeRegistry } from '@polkadot/types';
|
||||
|
||||
import Rpc from '.';
|
||||
|
||||
describe('replay', (): void => {
|
||||
const registry = new TypeRegistry();
|
||||
let rpc: Rpc;
|
||||
|
||||
beforeEach((): void => {
|
||||
rpc = new Rpc(new MockProvider());
|
||||
rpc = new Rpc(registry, new MockProvider(registry));
|
||||
});
|
||||
|
||||
it('subscribes via the rpc section', (done): void => {
|
||||
|
||||
Reference in New Issue
Block a user