Make metadata available on registry swap (#2556)
* Make metadata available on registry swap * Return full VersionedRegistry
This commit is contained in:
@@ -20,6 +20,7 @@ import Decorate from './Decorate';
|
||||
interface VersionedRegistry {
|
||||
isDefault: boolean;
|
||||
lastBlockHash: Uint8Array | null;
|
||||
metadata: Metadata;
|
||||
registry: Registry;
|
||||
specVersion: BN;
|
||||
}
|
||||
@@ -61,7 +62,7 @@ export default abstract class Init<ApiType extends ApiTypes> extends Decorate<Ap
|
||||
this._rx.queryMulti = this._decorateMulti(this._rxDecorateMethod);
|
||||
this._rx.signer = options.signer;
|
||||
|
||||
this._rpcCore.setRegistrySwap((hash?: string | Uint8Array | null) => this.getBlockRegistry(hash));
|
||||
this._rpcCore.setRegistrySwap((hash: string | Uint8Array) => this.getBlockRegistry(hash));
|
||||
this._rpcCore.provider.on('disconnected', this.#onProviderDisconnect);
|
||||
this._rpcCore.provider.on('error', this.#onProviderError);
|
||||
this._rpcCore.provider.on('connected', this.#onProviderConnect);
|
||||
@@ -78,17 +79,13 @@ export default abstract class Init<ApiType extends ApiTypes> extends Decorate<Ap
|
||||
/**
|
||||
* @description Sets up a registry based on the block hash defined
|
||||
*/
|
||||
public async getBlockRegistry (blockHash?: string | Uint8Array | null): Promise<Registry> {
|
||||
if (!blockHash) {
|
||||
return this.registry;
|
||||
}
|
||||
|
||||
public async getBlockRegistry (blockHash: string | Uint8Array): Promise<VersionedRegistry> {
|
||||
// shortcut in the case where we have an immediate-same request
|
||||
const lastBlockHash = u8aToU8a(blockHash);
|
||||
const existingViaHash = this.#registries.find((r) => r.lastBlockHash && u8aEq(lastBlockHash, r.lastBlockHash));
|
||||
|
||||
if (existingViaHash) {
|
||||
return existingViaHash.registry;
|
||||
return existingViaHash;
|
||||
}
|
||||
|
||||
// ensure we have everything required
|
||||
@@ -114,7 +111,7 @@ export default abstract class Init<ApiType extends ApiTypes> extends Decorate<Ap
|
||||
if (existingViaVersion) {
|
||||
existingViaVersion.lastBlockHash = lastBlockHash;
|
||||
|
||||
return existingViaVersion.registry;
|
||||
return existingViaVersion;
|
||||
}
|
||||
|
||||
// nothing has been found, construct new
|
||||
@@ -130,14 +127,14 @@ export default abstract class Init<ApiType extends ApiTypes> extends Decorate<Ap
|
||||
|
||||
// retrieve the metadata now that we have all types set
|
||||
const metadata = await this._rpcCore.state.getMetadata(header.parentHash).toPromise();
|
||||
const result = { isDefault: false, lastBlockHash, metadata, registry, specVersion: version.specVersion };
|
||||
|
||||
// TODO: Not convinced (yet) that we really want to re-decorate, keep on ice since it does muddle-up
|
||||
// this.injectMetadata(metadata, false);
|
||||
registry.setMetadata(metadata);
|
||||
this.#registries.push(result);
|
||||
|
||||
this.#registries.push({ isDefault: false, lastBlockHash, registry, specVersion: version.specVersion });
|
||||
|
||||
return registry;
|
||||
return result;
|
||||
}
|
||||
|
||||
protected async _loadMeta (): Promise<boolean> {
|
||||
@@ -215,6 +212,7 @@ export default abstract class Init<ApiType extends ApiTypes> extends Decorate<Ap
|
||||
|
||||
assert(thisRegistry, 'Initialization error, cannot find the default registry');
|
||||
|
||||
thisRegistry.metadata = metadata;
|
||||
thisRegistry.specVersion = version.specVersion;
|
||||
thisRegistry.registry.register(getSpecTypes(thisRegistry.registry, this._runtimeChain as Text, version.specName, version.specVersion));
|
||||
this.injectMetadata(metadata, false, thisRegistry.registry);
|
||||
@@ -265,11 +263,6 @@ export default abstract class Init<ApiType extends ApiTypes> extends Decorate<Ap
|
||||
this._runtimeVersion = runtimeVersion;
|
||||
this._rx.runtimeVersion = runtimeVersion;
|
||||
|
||||
// setup the initial registry, when we have none
|
||||
if (!this.#registries.length) {
|
||||
this.#registries.push({ isDefault: true, lastBlockHash: null, registry: this.registry, specVersion: runtimeVersion.specVersion });
|
||||
}
|
||||
|
||||
// adjust types based on bundled info
|
||||
if (this.registry.knownTypes.typesBundle) {
|
||||
this._adjustBundleTypes(this.registry, chain, runtimeVersion.specName);
|
||||
@@ -289,6 +282,11 @@ export default abstract class Init<ApiType extends ApiTypes> extends Decorate<Ap
|
||||
? new Metadata(this.registry, optMetadata[metadataKey])
|
||||
: await this._rpcCore.state.getMetadata().toPromise();
|
||||
|
||||
// setup the initial registry, when we have none
|
||||
if (!this.#registries.length) {
|
||||
this.#registries.push({ isDefault: true, lastBlockHash: null, metadata, registry: this.registry, specVersion: runtimeVersion.specVersion });
|
||||
}
|
||||
|
||||
// get unique types & validate
|
||||
metadata.getUniqTypes(false);
|
||||
|
||||
|
||||
@@ -68,7 +68,7 @@ function logErrorMessage (method: string, { params, type }: DefinitionRpc, error
|
||||
export default class Rpc implements RpcInterface {
|
||||
#registryDefault: Registry;
|
||||
|
||||
#getBlockRegistry?: (blockHash?: string | Uint8Array | null) => Promise<Registry>;
|
||||
#getBlockRegistry?: (blockHash: string | Uint8Array) => Promise<{ registry: Registry }>;
|
||||
|
||||
readonly #storageCache = new Map<string, string | null>();
|
||||
|
||||
@@ -136,7 +136,7 @@ export default class Rpc implements RpcInterface {
|
||||
/**
|
||||
* @description Sets a registry swap (typically from Api)
|
||||
*/
|
||||
public setRegistrySwap (registrySwap: (blockHash?: string | Uint8Array | null) => Promise<Registry>): void {
|
||||
public setRegistrySwap (registrySwap: (blockHash: string | Uint8Array) => Promise<{ registry: Registry }>): void {
|
||||
this.#getBlockRegistry = registrySwap;
|
||||
}
|
||||
|
||||
@@ -210,9 +210,9 @@ export default class Rpc implements RpcInterface {
|
||||
const hash = hashIndex === -1
|
||||
? undefined
|
||||
: values[hashIndex] as Uint8Array;
|
||||
const registry = hash && this.#getBlockRegistry
|
||||
const { registry } = hash && this.#getBlockRegistry
|
||||
? await this.#getBlockRegistry(hash)
|
||||
: this.#registryDefault;
|
||||
: { registry: this.#registryDefault };
|
||||
const params = this._formatInputs(registry, def, values);
|
||||
const data = await this.provider.send(rpcName, params.map((param): AnyJson => param.toJSON())) as AnyJson;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user