Subscribe to runtime updates (#1896)
* Subscribe to runtime updates * console.log -> l.log * Add l.warn to api augment * Expand logs with added & removed * Re-do everything when genesis has changed * augmentObject complexity * Dedup [string[], strings[]] * Update CHANGELOG
This commit is contained in:
@@ -18,7 +18,7 @@ import DecoratedMeta from '@polkadot/metadata/Decorated';
|
||||
import getHasher from '@polkadot/metadata/Decorated/storage/fromMetadata/getHasher';
|
||||
import RpcCore from '@polkadot/rpc-core';
|
||||
import { WsProvider } from '@polkadot/rpc-provider';
|
||||
import { Metadata, Null, Option, TypeRegistry, u64, Vec } from '@polkadot/types';
|
||||
import { Metadata, Null, Option, Text, TypeRegistry, u64, Vec } from '@polkadot/types';
|
||||
import Linkage, { LinkageResult } from '@polkadot/types/codec/Linkage';
|
||||
import { DEFAULT_VERSION as EXTRINSIC_DEFAULT_VERSION } from '@polkadot/types/primitive/Extrinsic/constants';
|
||||
import StorageKey, { StorageEntry } from '@polkadot/types/primitive/StorageKey';
|
||||
@@ -69,6 +69,8 @@ export default abstract class Decorate<ApiType extends ApiTypes> extends Events
|
||||
|
||||
protected _rpcCore: RpcCore;
|
||||
|
||||
protected _runtimeChain?: Text;
|
||||
|
||||
protected _runtimeMetadata?: Metadata;
|
||||
|
||||
protected _runtimeVersion?: RuntimeVersion;
|
||||
@@ -146,18 +148,18 @@ export default abstract class Decorate<ApiType extends ApiTypes> extends Events
|
||||
return this._rpcCore.provider.hasSubscriptions;
|
||||
}
|
||||
|
||||
public injectMetadata (metadata: Metadata): void {
|
||||
public injectMetadata (metadata: Metadata, fromEmpty?: boolean): void {
|
||||
const decoratedMeta = new DecoratedMeta(this.registry, metadata);
|
||||
|
||||
// this API
|
||||
augmentObject('tx', this.decorateExtrinsics(decoratedMeta.tx, this.decorateMethod), this._extrinsics);
|
||||
augmentObject('query', this.decorateStorage(decoratedMeta.query, this.decorateMethod), this._query);
|
||||
augmentObject('consts', decoratedMeta.consts, this._consts);
|
||||
augmentObject('tx', this.decorateExtrinsics(decoratedMeta.tx, this.decorateMethod), this._extrinsics, fromEmpty);
|
||||
augmentObject('query', this.decorateStorage(decoratedMeta.query, this.decorateMethod), this._query, fromEmpty);
|
||||
augmentObject('consts', decoratedMeta.consts, this._consts, fromEmpty);
|
||||
|
||||
// rx
|
||||
augmentObject('', this.decorateExtrinsics(decoratedMeta.tx, this.rxDecorateMethod), this._rx.tx);
|
||||
augmentObject('', this.decorateStorage(decoratedMeta.query, this.rxDecorateMethod), this._rx.query);
|
||||
augmentObject('', decoratedMeta.consts, this._rx.consts);
|
||||
augmentObject('', this.decorateExtrinsics(decoratedMeta.tx, this.rxDecorateMethod), this._rx.tx, fromEmpty);
|
||||
augmentObject('', this.decorateStorage(decoratedMeta.query, this.rxDecorateMethod), this._rx.query, fromEmpty);
|
||||
augmentObject('', decoratedMeta.consts, this._rx.consts, fromEmpty);
|
||||
}
|
||||
|
||||
private decorateFunctionMeta (input: MetaDecoration, output: MetaDecoration): MetaDecoration {
|
||||
|
||||
@@ -2,11 +2,13 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { SignedBlock } from '@polkadot/types/interfaces';
|
||||
import { SignedBlock, RuntimeVersion } from '@polkadot/types/interfaces';
|
||||
import { ApiBase, ApiOptions, ApiTypes, DecorateMethod } from '../types';
|
||||
|
||||
import { Metadata } from '@polkadot/types';
|
||||
import { getChainTypes, getMetadataTypes } from '@polkadot/types/known';
|
||||
import { Subscription, combineLatest, of } from 'rxjs';
|
||||
import { map, switchMap } from 'rxjs/operators';
|
||||
import { Metadata, Text } from '@polkadot/types';
|
||||
import { getMetadataTypes, getSpecTypes, getUserTypes } from '@polkadot/types/known';
|
||||
import { LATEST_EXTRINSIC_VERSION } from '@polkadot/types/primitive/Extrinsic/Extrinsic';
|
||||
import { logger } from '@polkadot/util';
|
||||
import { cryptoWaitReady } from '@polkadot/util-crypto';
|
||||
@@ -15,16 +17,18 @@ import Decorate from './Decorate';
|
||||
|
||||
const KEEPALIVE_INTERVAL = 15000;
|
||||
|
||||
const l = logger('api/decorator');
|
||||
const l = logger('api/init');
|
||||
|
||||
export default abstract class Init<ApiType extends ApiTypes> extends Decorate<ApiType> {
|
||||
private _healthTimer: NodeJS.Timeout | null = null;
|
||||
|
||||
private _updateSub?: Subscription;
|
||||
|
||||
constructor (options: ApiOptions, type: ApiTypes, decorateMethod: DecorateMethod<ApiType>) {
|
||||
super(options, type, decorateMethod);
|
||||
|
||||
if (!this.hasSubscriptions) {
|
||||
console.warn('Api will be available in a limited mode since the provider does not support subscriptions');
|
||||
l.warn('Api will be available in a limited mode since the provider does not support subscriptions');
|
||||
}
|
||||
|
||||
// We only register the types (global) if this is not a cloned instance.
|
||||
@@ -53,15 +57,30 @@ export default abstract class Init<ApiType extends ApiTypes> extends Decorate<Ap
|
||||
}
|
||||
|
||||
protected async loadMeta (): Promise<boolean> {
|
||||
const genesisHash = await this._rpcCore.chain.getBlockHash(0).toPromise();
|
||||
|
||||
// on re-connection to the same chain, we don't want to re-do everything from chain again
|
||||
if (this._isReady && !this._options.source && genesisHash.eq(this._genesisHash)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (this._genesisHash) {
|
||||
l.warn('Connection to new genesis detected, re-initializing');
|
||||
}
|
||||
|
||||
this._genesisHash = genesisHash;
|
||||
|
||||
if (this._updateSub) {
|
||||
this._updateSub.unsubscribe();
|
||||
}
|
||||
|
||||
const { metadata = {} } = this._options;
|
||||
|
||||
// only load from on-chain if we are not a clone (default path), alternatively
|
||||
// just use the values from the source instance provided
|
||||
if (!this._options.source || !this._options.source._isReady) {
|
||||
this._runtimeMetadata = await this.metaFromChain(metadata);
|
||||
} else {
|
||||
this._runtimeMetadata = await this.metaFromSource(this._options.source);
|
||||
}
|
||||
this._runtimeMetadata = this._options.source?._isReady
|
||||
? await this.metaFromSource(this._options.source)
|
||||
: await this.metaFromChain(metadata);
|
||||
|
||||
return this.initFromMeta(this._runtimeMetadata);
|
||||
}
|
||||
@@ -88,32 +107,64 @@ export default abstract class Init<ApiType extends ApiTypes> extends Decorate<Ap
|
||||
return source.runtimeMetadata;
|
||||
}
|
||||
|
||||
// subscribe to metadata updates, inject the types on changes
|
||||
private subscribeUpdates (): void {
|
||||
if (this._updateSub) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._updateSub = this._rpcCore.state.subscribeRuntimeVersion().pipe(
|
||||
switchMap((version: RuntimeVersion) =>
|
||||
combineLatest(
|
||||
of(version),
|
||||
this._rpcCore.state.getMetadata()
|
||||
)
|
||||
),
|
||||
map(([version, metadata]: [RuntimeVersion, Metadata]): boolean => {
|
||||
if (this._runtimeVersion?.specVersion.eq(version.specVersion)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
l.log(`Runtime version updated to ${version.specVersion}`);
|
||||
|
||||
this._runtimeMetadata = metadata;
|
||||
this._runtimeVersion = version;
|
||||
|
||||
this.registerTypes(getSpecTypes(this._runtimeChain as Text, version));
|
||||
this.injectMetadata(metadata, false);
|
||||
|
||||
return true;
|
||||
})
|
||||
).subscribe();
|
||||
}
|
||||
|
||||
private async metaFromChain (optMetadata: Record<string, string>): Promise<Metadata> {
|
||||
const { typesChain, typesSpec } = this._options;
|
||||
const [genesisHash, runtimeVersion, chain, chainProps] = await Promise.all([
|
||||
this._rpcCore.chain.getBlockHash(0).toPromise(),
|
||||
const [runtimeVersion, chain, chainProps] = await Promise.all([
|
||||
this._rpcCore.state.getRuntimeVersion().toPromise(),
|
||||
this._rpcCore.system.chain().toPromise(),
|
||||
this._rpcCore.system.properties().toPromise()
|
||||
]);
|
||||
|
||||
// based on the node spec & chain, inject specific type overrides
|
||||
this.registerTypes(getChainTypes(chain, runtimeVersion, typesChain, typesSpec));
|
||||
// set our chain version & genesisHash as returned
|
||||
this._runtimeChain = chain;
|
||||
this._runtimeVersion = runtimeVersion;
|
||||
|
||||
// do the setup for the specific chain
|
||||
this.registry.setChainProperties(chainProps);
|
||||
this.registerTypes(getSpecTypes(chain, runtimeVersion));
|
||||
this.registerTypes(getUserTypes(chain, runtimeVersion, typesChain, typesSpec));
|
||||
this.subscribeUpdates();
|
||||
|
||||
// filter the RPC methods (this does an rpc-methods call)
|
||||
await this.filterRpc();
|
||||
|
||||
// retrieve metadata, either from chain or as pass-in via options
|
||||
const metadataKey = `${genesisHash}-${runtimeVersion.specVersion}`;
|
||||
const metadataKey = `${this._genesisHash}-${runtimeVersion.specVersion}`;
|
||||
const metadata = metadataKey in optMetadata
|
||||
? new Metadata(this.registry, optMetadata[metadataKey])
|
||||
: await this._rpcCore.state.getMetadata().toPromise();
|
||||
|
||||
// set our chain version & genesisHash as returned
|
||||
this._genesisHash = genesisHash;
|
||||
this._runtimeVersion = runtimeVersion;
|
||||
|
||||
// get unique types & validate
|
||||
metadata.getUniqTypes(false);
|
||||
|
||||
@@ -141,7 +192,7 @@ export default abstract class Init<ApiType extends ApiTypes> extends Decorate<Ap
|
||||
this._rx.genesisHash = this._genesisHash;
|
||||
this._rx.runtimeVersion = this._runtimeVersion;
|
||||
|
||||
this.injectMetadata(metadata);
|
||||
this.injectMetadata(metadata, true);
|
||||
|
||||
// derive is last, since it uses the decorated rx
|
||||
this._rx.derive = this.decorateDeriveRx(this.rxDecorateMethod);
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import augmentObject, { findRemoved } from './augmentObject';
|
||||
import augmentObject from './augmentObject';
|
||||
|
||||
describe('findRemoved', (): void => {
|
||||
describe('augmentObject', (): void => {
|
||||
let spy: any;
|
||||
|
||||
beforeEach((): void => {
|
||||
@@ -15,20 +15,25 @@ describe('findRemoved', (): void => {
|
||||
spy.mockClear();
|
||||
});
|
||||
|
||||
it('logs removed sections', (): void => {
|
||||
findRemoved('test', { foo: {}, bar: {} }, { cde: {}, foo: {}, bar: {}, baz: {} });
|
||||
it('logs added/removed sections and methods', (): void => {
|
||||
augmentObject(
|
||||
'test',
|
||||
{ foo: { f: 1, d: 1 }, bar: { b: 1 }, new: { z: 1 } },
|
||||
{ foo: { f: 1, c: 1 }, bar: { a: 1, c: 1 }, baz: { a: 1 } }
|
||||
);
|
||||
|
||||
expect(spy).toHaveBeenCalledWith('api.test: Found 2 removed modules: baz, cde');
|
||||
expect(spy).toHaveBeenCalledWith(
|
||||
expect.anything(),
|
||||
expect.stringContaining('API/AUGMENT'),
|
||||
'api.test: Found 1 added and 1 removed modules:\n\t added: new\n\tremoved: baz'
|
||||
);
|
||||
expect(spy).toHaveBeenCalledWith(
|
||||
expect.anything(),
|
||||
expect.stringContaining('API/AUGMENT'),
|
||||
'api.test: Found 2 added and 3 removed calls:\n\t added: bar.b, foo.d\n\tremoved: bar.a, bar.c, foo.c'
|
||||
);
|
||||
});
|
||||
|
||||
it('logs removed calls', (): void => {
|
||||
findRemoved('test', { foo: { a: 1 }, bar: {} }, { foo: { a: 1 }, bar: { a: 1 } });
|
||||
|
||||
expect(spy).toHaveBeenCalledWith('api.test: Found 1 removed calls: bar.a');
|
||||
});
|
||||
});
|
||||
|
||||
describe('augmentObject', (): void => {
|
||||
it('copies sections to the dest', (): void => {
|
||||
const src = { foo: { a: 1 }, bar: { b: 1 } };
|
||||
|
||||
@@ -39,6 +44,10 @@ describe('augmentObject', (): void => {
|
||||
const src = { foo: { a: 1 }, bar: { b: 1 } };
|
||||
const dst = { foo: { b: 2 }, baz: { c: 1 } };
|
||||
|
||||
expect(augmentObject('test', src, dst)).toEqual({ foo: { b: 2, a: 1 }, bar: { b: 1 }, baz: { c: 1 } });
|
||||
expect(augmentObject('test', src, dst)).toEqual({
|
||||
foo: { b: 2, a: 1 },
|
||||
bar: { b: 1 },
|
||||
baz: { c: 1 }
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,24 +2,53 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { logger } from '@polkadot/util';
|
||||
|
||||
type StringsStrings = [string[], string[]];
|
||||
|
||||
const l = logger('api/augment');
|
||||
|
||||
function logLength (type: 'added' | 'removed', values: string[], and: string[] = []): string {
|
||||
return values.length
|
||||
? ` ${values.length} ${type}${and.length ? ' and' : ''}`
|
||||
: '';
|
||||
}
|
||||
|
||||
function logValues (type: 'added' | 'removed', values: string[]): string {
|
||||
return values.length
|
||||
? `\n\t${type.padStart(7)}: ${values.sort().join(', ')}`
|
||||
: '';
|
||||
}
|
||||
|
||||
// log details to console
|
||||
function warn (prefix: string, type: 'calls' | 'modules', rmed: string[]): void {
|
||||
if (rmed.length) {
|
||||
console.warn(`api.${prefix}: Found ${rmed.length} removed ${type}: ${rmed.join(', ')}`);
|
||||
function warn (prefix: string, type: 'calls' | 'modules', [added, removed]: StringsStrings): void {
|
||||
if (added.length || removed.length) {
|
||||
l.warn(`api.${prefix}: Found${logLength('added', added, removed)}${logLength('removed', removed)} ${type}:${logValues('added', added)}${logValues('removed', removed)}`);
|
||||
}
|
||||
}
|
||||
|
||||
// log all the stuff that has been removed/
|
||||
export function findRemoved (prefix: string, src: Record<string, Record<string, any>>, dst: Record<string, Record<string, any>>): void {
|
||||
function extractKeys (src: Record<string, Record<string, any>>, dst: Record<string, Record<string, any>>): StringsStrings {
|
||||
return [Object.keys(src), Object.keys(dst)];
|
||||
}
|
||||
|
||||
function findSectionExcludes (a: string[], b: string[]): string[] {
|
||||
return a.filter((section): boolean => !b.includes(section));
|
||||
}
|
||||
|
||||
function extractSections (src: Record<string, Record<string, any>>, dst: Record<string, Record<string, any>>): StringsStrings {
|
||||
const [srcSections, dstSections] = extractKeys(src, dst);
|
||||
|
||||
return [
|
||||
findSectionExcludes(srcSections, dstSections),
|
||||
findSectionExcludes(dstSections, srcSections)
|
||||
];
|
||||
}
|
||||
|
||||
function findMethodExcludes (src: Record<string, Record<string, any>>, dst: Record<string, Record<string, any>>): string[] {
|
||||
const srcSections = Object.keys(src);
|
||||
const dstSections = Object.keys(dst);
|
||||
|
||||
warn(prefix, 'modules', dstSections
|
||||
.filter((section): boolean => !srcSections.includes(section))
|
||||
.sort()
|
||||
);
|
||||
|
||||
warn(prefix, 'calls', dstSections
|
||||
return dstSections
|
||||
.filter((section): boolean => srcSections.includes(section))
|
||||
.reduce((rmMethods: string[], section): string[] => {
|
||||
const srcMethods = Object.keys(src[section]);
|
||||
@@ -30,9 +59,14 @@ export function findRemoved (prefix: string, src: Record<string, Record<string,
|
||||
.filter((method): boolean => !srcMethods.includes(method))
|
||||
.map((method): string => `${section}.${method}`)
|
||||
);
|
||||
}, [])
|
||||
.sort()
|
||||
);
|
||||
}, []);
|
||||
}
|
||||
|
||||
function extractMethods (src: Record<string, Record<string, any>>, dst: Record<string, Record<string, any>>): StringsStrings {
|
||||
return [
|
||||
findMethodExcludes(dst, src),
|
||||
findMethodExcludes(src, dst)
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -40,9 +74,16 @@ export function findRemoved (prefix: string, src: Record<string, Record<string,
|
||||
* already available, but rather just adds new missing ites into the result object.
|
||||
* @internal
|
||||
*/
|
||||
export default function augmentObject (prefix: string, src: Record<string, Record<string, any>>, dst: Record<string, Record<string, any>>): Record<string, Record<string, any>> {
|
||||
export default function augmentObject (prefix: string, src: Record<string, Record<string, any>>, dst: Record<string, Record<string, any>>, fromEmpty = false): Record<string, Record<string, any>> {
|
||||
if (fromEmpty) {
|
||||
Object.keys(dst).forEach((key): void => {
|
||||
delete dst[key];
|
||||
});
|
||||
}
|
||||
|
||||
if (prefix && Object.keys(dst).length) {
|
||||
findRemoved(prefix, src, dst);
|
||||
warn(prefix, 'modules', extractSections(src, dst));
|
||||
warn(prefix, 'calls', extractMethods(src, dst));
|
||||
}
|
||||
|
||||
return Object
|
||||
|
||||
Reference in New Issue
Block a user