User-provided types for chain & spec (#1361)

* User-provided types for chain & spec

* Remove unused node name

* Remove re-registration warning
This commit is contained in:
Jaco Greeff
2019-09-03 11:39:22 +02:00
committed by GitHub
parent 7809ea5dad
commit 7b844274fd
3 changed files with 28 additions and 12 deletions
+19 -10
View File
@@ -35,8 +35,8 @@ const TYPES_SUBSTRATE_1 = {
ValidatorPrefs: 'ValidatorPrefs0to145' ValidatorPrefs: 'ValidatorPrefs0to145'
}; };
// Type overrides for specific node types // Type overrides for specific spec types as given in runtimeVersion
const SPEC_TYPES: Record<string, Record<string, string>> = { const TYPES_SPEC: Record<string, Record<string, string>> = {
kusama: TYPES_FOR_POLKADOT, kusama: TYPES_FOR_POLKADOT,
polkadot: TYPES_FOR_POLKADOT polkadot: TYPES_FOR_POLKADOT
}; };
@@ -89,21 +89,31 @@ export default abstract class Init<ApiType> extends Decorate<ApiType> {
} }
private async metaFromChain (optMetadata: Record<string, string>): Promise<Metadata> { private async metaFromChain (optMetadata: Record<string, string>): Promise<Metadata> {
[this._genesisHash, this._runtimeVersion] = await Promise.all([ const { typesChain = {}, typesSpec = {} } = this._options;
const [genesisHash, runtimeVersion, chain] = await Promise.all([
this._rpcCore.chain.getBlockHash(0).toPromise(), this._rpcCore.chain.getBlockHash(0).toPromise(),
this._rpcCore.state.getRuntimeVersion().toPromise() this._rpcCore.state.getRuntimeVersion().toPromise(),
this._rpcCore.system.chain().toPromise()
]); ]);
const specName = runtimeVersion.specName.toString();
// based on the node, inject specific types // based on the node spec & chain, inject specific type overrides
this.registerTypes( this.registerTypes({
SPEC_TYPES[this._runtimeVersion.specName.toString()] ...(TYPES_SPEC[specName] || {}),
); ...(typesSpec[specName] || {}),
...(typesChain[chain.toString()] || {})
});
const metadataKey = `${this._genesisHash}-${this._runtimeVersion.specVersion}`; // retrieve metadata, either from chain or as pass-in via options
const metadataKey = `${genesisHash}-${runtimeVersion.specVersion}`;
const metadata = metadataKey in optMetadata const metadata = metadataKey in optMetadata
? new Metadata(optMetadata[metadataKey]) ? new Metadata(optMetadata[metadataKey])
: await this._rpcCore.state.getMetadata().toPromise(); : await this._rpcCore.state.getMetadata().toPromise();
// set our chain version & genesisHash as returned
this._genesisHash = genesisHash;
this._runtimeVersion = runtimeVersion;
// get unique types & validate // get unique types & validate
metadata.getUniqTypes(false); metadata.getUniqTypes(false);
@@ -171,7 +181,6 @@ export default abstract class Init<ApiType> extends Decorate<ApiType> {
}); });
}, KEEPALIVE_INTERVAL); }, KEEPALIVE_INTERVAL);
} catch (_error) { } catch (_error) {
console.error(_error);
const error = new Error(`FATAL: Unable to initialize the API: ${_error.message}`); const error = new Error(`FATAL: Unable to initialize the API: ${_error.message}`);
l.error(error); l.error(error);
+8
View File
@@ -188,6 +188,14 @@ export interface ApiOptions {
* uses types not available in the base Substrate runtime. * uses types not available in the base Substrate runtime.
*/ */
types?: RegistryTypes; types?: RegistryTypes;
/**
* @description Additional types that are injected based on the chain we are connecting to. There are keyed by the chain, i.e. `{ 'Kusama CC1': { ... } }`
*/
typesChain?: Record<string, RegistryTypes>;
/**
* @description Additional types that are injected based on the type of node we are connecting to, as set via specName in the runtime version. There are keyed by the node, i.e. `{ 'edgeware': { ... } }`
*/
typesSpec?: Record<string, RegistryTypes>;
} }
// A smaller interface of ApiRx, used in derive and in SubmittableExtrinsic // A smaller interface of ApiRx, used in derive and in SubmittableExtrinsic
+1 -2
View File
@@ -47,9 +47,8 @@ export class TypeRegistry {
? type ? type
: JSON.stringify(type); : JSON.stringify(type);
// we alreday have this type, remove the classes registered for it
if (this._classes.has(name)) { if (this._classes.has(name)) {
console.warn(`The type '${name}' is already existing as a class, re-registering definition`);
this._classes.delete(name); this._classes.delete(name);
} }