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
This commit is contained in:
Jaco Greeff
2020-08-30 12:12:18 +02:00
committed by GitHub
parent a83980b213
commit 7876bb9ced
10 changed files with 81 additions and 24 deletions
+4 -1
View File
@@ -2,9 +2,12 @@
## 1.31.0-beta.x ## 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) - **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(<hash>)` & `query.system.events.at(<hash>)` now works out-of-the-box with tehe correct metadata & types
- Add `toBigInt()` (JS built-in `BigInt`) on `Int/Uint`, & `Compact<*>` 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 - `derive.democracy.locks` now returns delegated locks for an account as well
## 1.30.1 Aug 24, 2020 ## 1.30.1 Aug 24, 2020
+7 -4
View File
@@ -62,6 +62,7 @@ export default abstract class Init<ApiType extends ApiTypes> extends Decorate<Ap
this._rx.queryMulti = this._decorateMulti(this._rxDecorateMethod); this._rx.queryMulti = this._decorateMulti(this._rxDecorateMethod);
this._rx.signer = options.signer; this._rx.signer = options.signer;
this._rpcCore.setRegistrySwap((hash?: string | Uint8Array | null) => this.swapRegistry(hash));
this._rpcCore.provider.on('disconnected', this.#onProviderDisconnect); this._rpcCore.provider.on('disconnected', this.#onProviderDisconnect);
this._rpcCore.provider.on('error', this.#onProviderError); this._rpcCore.provider.on('error', this.#onProviderError);
this._rpcCore.provider.on('connected', this.#onProviderConnect); this._rpcCore.provider.on('connected', this.#onProviderConnect);
@@ -87,13 +88,15 @@ export default abstract class Init<ApiType extends ApiTypes> extends Decorate<Ap
return this.setRegistry(this.#registryDefault); return this.setRegistry(this.#registryDefault);
} }
const { parentHash } = this._genesisHash?.eq(blockHash) // We have to assume that on the RPC layer the calls used here does not call back into
// the registry swap, so getHeader & getRuntimeVersion should not be historic
const header = this._genesisHash?.eq(blockHash)
? { parentHash: this._genesisHash } ? { parentHash: this._genesisHash }
: await this._rpcCore.chain.getHeader(blockHash).toPromise(); : await this._rpcCore.chain.getHeader(blockHash).toPromise();
assert(!parentHash.isEmpty, 'Unable to determine parent hash from supplied block'); assert(header?.parentHash && !header.parentHash.isEmpty, 'Unable to retrieve header and parent from supplied hash');
const version = await this._rpcCore.state.getRuntimeVersion(parentHash).toPromise(); const version = await this._rpcCore.state.getRuntimeVersion(header.parentHash).toPromise();
const existing = this.#registries.find(({ specVersion }) => specVersion.eq(version.specVersion)); const existing = this.#registries.find(({ specVersion }) => specVersion.eq(version.specVersion));
if (existing) { if (existing) {
@@ -111,7 +114,7 @@ export default abstract class Init<ApiType extends ApiTypes> extends Decorate<Ap
} }
// retrieve the metadata now that we have all types set // retrieve the metadata now that we have all types set
const metadata = await this._rpcCore.state.getMetadata(parentHash).toPromise(); const metadata = await this._rpcCore.state.getMetadata(header.parentHash).toPromise();
// TODO: Not convinced (yet) that we really want to re-decorate, keep on ice since it does muddle-up // TODO: Not convinced (yet) that we really want to re-decorate, keep on ice since it does muddle-up
// this.injectMetadata(metadata, false); // this.injectMetadata(metadata, false);
+46 -19
View File
@@ -8,8 +8,8 @@ import { AnyJson, Codec, DefinitionRpc, DefinitionRpcExt, DefinitionRpcSub, Regi
import { RpcInterface, RpcInterfaceMethod } from './types'; import { RpcInterface, RpcInterfaceMethod } from './types';
import memoizee from 'memoizee'; import memoizee from 'memoizee';
import { combineLatest, from, Observable, Observer, of, throwError } from 'rxjs'; import { from, Observable, Observer, of } from 'rxjs';
import { catchError, map, publishReplay, refCount, switchMap } from 'rxjs/operators'; import { publishReplay, refCount, switchMap } from 'rxjs/operators';
import jsonrpc from '@polkadot/types/interfaces/jsonrpc'; import jsonrpc from '@polkadot/types/interfaces/jsonrpc';
import { Option, StorageKey, Vec, createClass, createTypeUnsafe } from '@polkadot/types'; import { Option, StorageKey, Vec, createClass, createTypeUnsafe } from '@polkadot/types';
import { assert, hexToU8a, isFunction, isNull, isUndefined, logger, u8aToU8a } from '@polkadot/util'; import { assert, hexToU8a, isFunction, isNull, isUndefined, logger, u8aToU8a } from '@polkadot/util';
@@ -68,6 +68,8 @@ function logErrorMessage (method: string, { params, type }: DefinitionRpc, error
export default class Rpc implements RpcInterface { export default class Rpc implements RpcInterface {
#registry: Registry; #registry: Registry;
#registrySwap?: (blockHash?: string | Uint8Array | null) => Promise<Registry>;
readonly #storageCache = new Map<string, string | null>(); readonly #storageCache = new Map<string, string | null>();
public readonly mapping = new Map<string, DefinitionRpcExt>(); public readonly mapping = new Map<string, DefinitionRpcExt>();
@@ -138,6 +140,13 @@ export default class Rpc implements RpcInterface {
this.#registry = registry; this.#registry = registry;
} }
/**
* @description Sets a registry swap (typically from Api)
*/
public setRegistrySwap (registrySwap: (blockHash?: string | Uint8Array | null) => Promise<Registry>): void {
this.#registrySwap = registrySwap;
}
public addUserInterfaces<Section extends keyof RpcInterface> (userRpc: Record<string, Record<string, DefinitionRpc | DefinitionRpcSub>>): void { public addUserInterfaces<Section extends keyof RpcInterface> (userRpc: Record<string, Record<string, DefinitionRpc | DefinitionRpcSub>>): void {
// add any extra user-defined sections // add any extra user-defined sections
this.sections.push(...Object.keys(userRpc).filter((key) => !this.sections.includes(key))); 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 { private _createMethodSend (section: string, method: string, def: DefinitionRpc): RpcInterfaceMethod {
const rpcName = `${section}_${method}`; 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<Codec | Codec[]> => {
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<any> => { const creator = (isRaw: boolean) => (...values: any[]): Observable<any> => {
// Here, logically, it should be `of(this.formatInputs(method, values))`. // 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 // - first do `of(1)` - won't throw
// - then do `map(()=>this.formatInputs)` - might throw, but inside Observable. // - then do `map(()=>this.formatInputs)` - might throw, but inside Observable.
return of(1).pipe( return of(1).pipe(
map(() => this._formatInputs(def, values)), switchMap(() => from(callWithSwap(isRaw, 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);
}),
publishReplay(1), // create a Replay(1) publishReplay(1), // create a Replay(1)
refCount() // Unsubscribe WS when there are no more subscribers refCount() // Unsubscribe WS when there are no more subscribers
); );
@@ -27,6 +27,7 @@ export default {
{ {
name: 'hash', name: 'hash',
type: 'BlockHash', type: 'BlockHash',
isHistoric: true,
isOptional: true isOptional: true
} }
], ],
@@ -23,6 +23,7 @@ export default {
{ {
name: 'at', name: 'at',
type: 'Hash', type: 'Hash',
isHistoric: true,
isOptional: true isOptional: true
} }
], ],
@@ -42,6 +43,7 @@ export default {
{ {
name: 'at', name: 'at',
type: 'Hash', type: 'Hash',
isHistoric: true,
isOptional: true isOptional: true
} }
], ],
@@ -61,6 +63,7 @@ export default {
{ {
name: 'at', name: 'at',
type: 'Hash', type: 'Hash',
isHistoric: true,
isOptional: true isOptional: true
} }
], ],
@@ -80,6 +83,7 @@ export default {
{ {
name: 'at', name: 'at',
type: 'Hash', type: 'Hash',
isHistoric: true,
isOptional: true isOptional: true
} }
], ],
@@ -19,6 +19,7 @@ export default {
{ {
name: 'at', name: 'at',
type: 'BlockHash', type: 'BlockHash',
isHistoric: true,
isOptional: true isOptional: true
} }
], ],
@@ -38,6 +39,7 @@ export default {
{ {
name: 'at', name: 'at',
type: 'BlockHash', type: 'BlockHash',
isHistoric: true,
isOptional: true isOptional: true
} }
], ],
@@ -53,6 +55,7 @@ export default {
{ {
name: 'at', name: 'at',
type: 'BlockHash', type: 'BlockHash',
isHistoric: true,
isOptional: true isOptional: true
} }
], ],
@@ -19,6 +19,7 @@ export default {
{ {
name: 'at', name: 'at',
type: 'BlockHash', type: 'BlockHash',
isHistoric: true,
isOptional: true isOptional: true
} }
], ],
@@ -24,6 +24,7 @@ export default {
{ {
name: 'at', name: 'at',
type: 'BlockHash', type: 'BlockHash',
isHistoric: true,
isOptional: true isOptional: true
} }
], ],
@@ -39,6 +40,7 @@ export default {
{ {
name: 'at', name: 'at',
type: 'BlockHash', type: 'BlockHash',
isHistoric: true,
isOptional: true isOptional: true
} }
], ],
@@ -54,6 +56,7 @@ export default {
{ {
name: 'at', name: 'at',
type: 'BlockHash', type: 'BlockHash',
isHistoric: true,
isOptional: true isOptional: true
} }
], ],
@@ -79,6 +82,7 @@ export default {
{ {
name: 'at', name: 'at',
type: 'BlockHash', type: 'BlockHash',
isHistoric: true,
isOptional: true isOptional: true
} }
], ],
@@ -95,6 +99,7 @@ export default {
{ {
name: 'at', name: 'at',
type: 'BlockHash', type: 'BlockHash',
isHistoric: true,
isOptional: true isOptional: true
} }
], ],
@@ -111,6 +116,7 @@ export default {
{ {
name: 'at', name: 'at',
type: 'BlockHash', type: 'BlockHash',
isHistoric: true,
isOptional: true isOptional: true
} }
], ],
@@ -127,6 +133,7 @@ export default {
{ {
name: 'at', name: 'at',
type: 'BlockHash', type: 'BlockHash',
isHistoric: true,
isOptional: true isOptional: true
} }
], ],
@@ -154,6 +161,7 @@ export default {
{ {
name: 'at', name: 'at',
type: 'BlockHash', type: 'BlockHash',
isHistoric: true,
isOptional: true isOptional: true
} }
], ],
@@ -181,6 +189,7 @@ export default {
{ {
name: 'at', name: 'at',
type: 'BlockHash', type: 'BlockHash',
isHistoric: true,
isOptional: true isOptional: true
} }
], ],
@@ -208,6 +217,7 @@ export default {
{ {
name: 'at', name: 'at',
type: 'BlockHash', type: 'BlockHash',
isHistoric: true,
isOptional: true isOptional: true
} }
], ],
@@ -235,6 +245,7 @@ export default {
{ {
name: 'at', name: 'at',
type: 'BlockHash', type: 'BlockHash',
isHistoric: true,
isOptional: true isOptional: true
} }
], ],
@@ -292,6 +303,7 @@ export default {
{ {
name: 'at', name: 'at',
type: 'BlockHash', type: 'BlockHash',
isHistoric: true,
isOptional: true isOptional: true
} }
], ],
@@ -307,6 +319,7 @@ export default {
{ {
name: 'at', name: 'at',
type: 'BlockHash', type: 'BlockHash',
isHistoric: true,
isOptional: true isOptional: true
} }
], ],
@@ -31,6 +31,7 @@ export default {
{ {
name: 'at', name: 'at',
type: 'BlockHash', type: 'BlockHash',
isHistoric: true,
isOptional: true isOptional: true
} }
], ],
+1
View File
@@ -13,6 +13,7 @@ export type DefinitionTypeStruct = Record<string, DefinitionTypeType> | { _alias
export type DefinitionType = string | DefinitionTypeEnum | DefinitionTypeSet | DefinitionTypeStruct; export type DefinitionType = string | DefinitionTypeEnum | DefinitionTypeSet | DefinitionTypeStruct;
export interface DefinitionRpcParam { export interface DefinitionRpcParam {
isHistoric?: boolean;
isOptional?: boolean; isOptional?: boolean;
name: string; name: string;
type: DefinitionTypeType; type: DefinitionTypeType;