From 7876bb9cedec0ded270492998c3e3a0d732e613e Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Sun, 30 Aug 2020 12:12:18 +0200 Subject: [PATCH] Add isHistoric flags to RPC with hash params (#2535) * Add isHistoric flags to RPC with hash params * Update packages/types/src/interfaces/chain/definitions.ts * Apply suggestions from code review * Transparently swap registry on RPC layer * Simplify (& linting fixes) * Cleanups * CHANGELOG --- CHANGELOG.md | 5 +- packages/api/src/base/Init.ts | 11 ++-- packages/rpc-core/src/index.ts | 65 +++++++++++++------ .../types/src/interfaces/chain/definitions.ts | 1 + .../src/interfaces/childstate/definitions.ts | 4 ++ .../src/interfaces/contracts/definitions.ts | 3 + .../src/interfaces/payment/definitions.ts | 1 + .../types/src/interfaces/state/definitions.ts | 13 ++++ .../src/interfaces/system/definitions.ts | 1 + packages/types/src/types/definitions.ts | 1 + 10 files changed, 81 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7972da3743..061d7ac43d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,9 +2,12 @@ ## 1.31.0-beta.x +Upgrade priority: Low, unless using the new `RewardDestination` or there is a need for transparent at-hash queries + - **Important** Substrate changed the `RewardDestination` enum with an extra field for payout-to-any account. If on an older chain consider adding `RewardDestination: 'RewardDestinationTo257'` (Newer chains with this requirement met in their Substrate version should update the API for support) +- Add transparent support for types/metadata, i.e. hash queries such as `rpc.chain.getBlock()` & `query.system.events.at()` now works out-of-the-box with tehe correct metadata & types - Add `toBigInt()` (JS built-in `BigInt`) on `Int/Uint`, & `Compact<*>` types -- Add `api.swapRegistry(blockHash?)` to cleanly swap to types for a specific block (no param swaps to default) +- Add `api.swapRegistry(blockHash?)` to manually swap to types for a specific block (no param swaps to default) - `derive.democracy.locks` now returns delegated locks for an account as well ## 1.30.1 Aug 24, 2020 diff --git a/packages/api/src/base/Init.ts b/packages/api/src/base/Init.ts index baa586e6a0..fe9dc6fe5e 100644 --- a/packages/api/src/base/Init.ts +++ b/packages/api/src/base/Init.ts @@ -62,6 +62,7 @@ export default abstract class Init extends Decorate this.swapRegistry(hash)); this._rpcCore.provider.on('disconnected', this.#onProviderDisconnect); this._rpcCore.provider.on('error', this.#onProviderError); this._rpcCore.provider.on('connected', this.#onProviderConnect); @@ -87,13 +88,15 @@ export default abstract class Init extends Decorate specVersion.eq(version.specVersion)); if (existing) { @@ -111,7 +114,7 @@ export default abstract class Init extends Decorate Promise; + readonly #storageCache = new Map(); public readonly mapping = new Map(); @@ -138,6 +140,13 @@ export default class Rpc implements RpcInterface { this.#registry = registry; } + /** + * @description Sets a registry swap (typically from Api) + */ + public setRegistrySwap (registrySwap: (blockHash?: string | Uint8Array | null) => Promise): void { + this.#registrySwap = registrySwap; + } + public addUserInterfaces
(userRpc: Record>): void { // add any extra user-defined sections this.sections.push(...Object.keys(userRpc).filter((key) => !this.sections.includes(key))); @@ -199,6 +208,40 @@ export default class Rpc implements RpcInterface { private _createMethodSend (section: string, method: string, def: DefinitionRpc): RpcInterfaceMethod { const rpcName = `${section}_${method}`; + const hashIndex = def.params.findIndex(({ isHistoric }) => isHistoric); + + // execute the RPC call, doing a registry swap for historic as applicable + const callWithSwap = async (isRaw: boolean, values: any[]): Promise => { + const hash = hashIndex === -1 + ? undefined + : values[hashIndex] as Uint8Array; + + try { + if (hash && this.#registrySwap) { + await this.#registrySwap(hash); + } + + const params = this._formatInputs(def, values); + const data = await this.provider.send(rpcName, params.map((param): AnyJson => param.toJSON())) as AnyJson; + const result = isRaw + ? this.#registry.createType('Raw', data) + : this._formatOutput(method, def, params, data); + + if (hash && this.#registrySwap) { + await this.#registrySwap(); + } + + return result; + } catch (error) { + logErrorMessage(method, def, error); + + if (hash && this.#registrySwap) { + await this.#registrySwap(); + } + + throw error; + } + }; const creator = (isRaw: boolean) => (...values: any[]): Observable => { // Here, logically, it should be `of(this.formatInputs(method, values))`. @@ -208,23 +251,7 @@ export default class Rpc implements RpcInterface { // - first do `of(1)` - won't throw // - then do `map(()=>this.formatInputs)` - might throw, but inside Observable. return of(1).pipe( - map(() => this._formatInputs(def, values)), - switchMap((params): Observable<[Codec[], any]> => - combineLatest([ - of(params), - from(this.provider.send(rpcName, params.map((param): AnyJson => param.toJSON()))) - ]) - ), - map(([params, result]) => - isRaw - ? this.#registry.createType('Raw', result) - : this._formatOutput(method, def, params, result) - ), - catchError((error): any => { - logErrorMessage(method, def, error); - - return throwError(error); - }), + switchMap(() => from(callWithSwap(isRaw, values))), publishReplay(1), // create a Replay(1) refCount() // Unsubscribe WS when there are no more subscribers ); diff --git a/packages/types/src/interfaces/chain/definitions.ts b/packages/types/src/interfaces/chain/definitions.ts index 32a76a13e0..f327c3b721 100644 --- a/packages/types/src/interfaces/chain/definitions.ts +++ b/packages/types/src/interfaces/chain/definitions.ts @@ -27,6 +27,7 @@ export default { { name: 'hash', type: 'BlockHash', + isHistoric: true, isOptional: true } ], diff --git a/packages/types/src/interfaces/childstate/definitions.ts b/packages/types/src/interfaces/childstate/definitions.ts index 98df9eed94..5e0373eb2a 100644 --- a/packages/types/src/interfaces/childstate/definitions.ts +++ b/packages/types/src/interfaces/childstate/definitions.ts @@ -23,6 +23,7 @@ export default { { name: 'at', type: 'Hash', + isHistoric: true, isOptional: true } ], @@ -42,6 +43,7 @@ export default { { name: 'at', type: 'Hash', + isHistoric: true, isOptional: true } ], @@ -61,6 +63,7 @@ export default { { name: 'at', type: 'Hash', + isHistoric: true, isOptional: true } ], @@ -80,6 +83,7 @@ export default { { name: 'at', type: 'Hash', + isHistoric: true, isOptional: true } ], diff --git a/packages/types/src/interfaces/contracts/definitions.ts b/packages/types/src/interfaces/contracts/definitions.ts index 6480f56d7c..cfab63e329 100644 --- a/packages/types/src/interfaces/contracts/definitions.ts +++ b/packages/types/src/interfaces/contracts/definitions.ts @@ -19,6 +19,7 @@ export default { { name: 'at', type: 'BlockHash', + isHistoric: true, isOptional: true } ], @@ -38,6 +39,7 @@ export default { { name: 'at', type: 'BlockHash', + isHistoric: true, isOptional: true } ], @@ -53,6 +55,7 @@ export default { { name: 'at', type: 'BlockHash', + isHistoric: true, isOptional: true } ], diff --git a/packages/types/src/interfaces/payment/definitions.ts b/packages/types/src/interfaces/payment/definitions.ts index debd71e8d6..b5b4c1d19d 100644 --- a/packages/types/src/interfaces/payment/definitions.ts +++ b/packages/types/src/interfaces/payment/definitions.ts @@ -19,6 +19,7 @@ export default { { name: 'at', type: 'BlockHash', + isHistoric: true, isOptional: true } ], diff --git a/packages/types/src/interfaces/state/definitions.ts b/packages/types/src/interfaces/state/definitions.ts index 3a00b751b7..766363b6b6 100644 --- a/packages/types/src/interfaces/state/definitions.ts +++ b/packages/types/src/interfaces/state/definitions.ts @@ -24,6 +24,7 @@ export default { { name: 'at', type: 'BlockHash', + isHistoric: true, isOptional: true } ], @@ -39,6 +40,7 @@ export default { { name: 'at', type: 'BlockHash', + isHistoric: true, isOptional: true } ], @@ -54,6 +56,7 @@ export default { { name: 'at', type: 'BlockHash', + isHistoric: true, isOptional: true } ], @@ -79,6 +82,7 @@ export default { { name: 'at', type: 'BlockHash', + isHistoric: true, isOptional: true } ], @@ -95,6 +99,7 @@ export default { { name: 'at', type: 'BlockHash', + isHistoric: true, isOptional: true } ], @@ -111,6 +116,7 @@ export default { { name: 'at', type: 'BlockHash', + isHistoric: true, isOptional: true } ], @@ -127,6 +133,7 @@ export default { { name: 'at', type: 'BlockHash', + isHistoric: true, isOptional: true } ], @@ -154,6 +161,7 @@ export default { { name: 'at', type: 'BlockHash', + isHistoric: true, isOptional: true } ], @@ -181,6 +189,7 @@ export default { { name: 'at', type: 'BlockHash', + isHistoric: true, isOptional: true } ], @@ -208,6 +217,7 @@ export default { { name: 'at', type: 'BlockHash', + isHistoric: true, isOptional: true } ], @@ -235,6 +245,7 @@ export default { { name: 'at', type: 'BlockHash', + isHistoric: true, isOptional: true } ], @@ -292,6 +303,7 @@ export default { { name: 'at', type: 'BlockHash', + isHistoric: true, isOptional: true } ], @@ -307,6 +319,7 @@ export default { { name: 'at', type: 'BlockHash', + isHistoric: true, isOptional: true } ], diff --git a/packages/types/src/interfaces/system/definitions.ts b/packages/types/src/interfaces/system/definitions.ts index df61e395b0..ff4ba4c9c1 100644 --- a/packages/types/src/interfaces/system/definitions.ts +++ b/packages/types/src/interfaces/system/definitions.ts @@ -31,6 +31,7 @@ export default { { name: 'at', type: 'BlockHash', + isHistoric: true, isOptional: true } ], diff --git a/packages/types/src/types/definitions.ts b/packages/types/src/types/definitions.ts index 7586e1a014..9cb828ce83 100644 --- a/packages/types/src/types/definitions.ts +++ b/packages/types/src/types/definitions.ts @@ -13,6 +13,7 @@ export type DefinitionTypeStruct = Record | { _alias export type DefinitionType = string | DefinitionTypeEnum | DefinitionTypeSet | DefinitionTypeStruct; export interface DefinitionRpcParam { + isHistoric?: boolean; isOptional?: boolean; name: string; type: DefinitionTypeType;