Api pre-bundled metadata (#850)

* Changes to bundle meta data

* wip

* As per review

* description for prebundles in apiOption

* WIP

* WIP

* Added blank lines as earlier - code style

* Resolving tslint error

* Skipping end to end test

* changed as per review comments

* WIP

* Fix lint error

* WIP as per comments

* As per review comment

* WIP

* Importing with @polkadot in test file instead of relative
This commit is contained in:
KarishmaBothara
2019-05-02 09:33:32 +02:00
committed by Amaury Martiny
parent f7d48de210
commit 19eb484a29
3 changed files with 71 additions and 3 deletions
+12 -3
View File
@@ -374,12 +374,21 @@ export default abstract class ApiBase<CodecResult, SubscriptionResult> implement
}
private async loadMeta (): Promise<boolean> {
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._rpcBase.state.getMetadata();
this._runtimeVersion = await this._rpcBase.chain.getRuntimeVersion();
this._genesisHash = await this._rpcBase.chain.getBlockHash(0);
[this._genesisHash, this._runtimeVersion] = await Promise.all([
this._rpcBase.chain.getBlockHash(0),
this._rpcBase.chain.getRuntimeVersion()
]);
const metadataKey = `${this._genesisHash}-${(this._runtimeVersion as RuntimeVersion).specVersion}`;
if (metadataKey in metadata) {
this._runtimeMetadata = new Metadata(metadata[metadataKey]);
} else {
this._runtimeMetadata = await this._rpcBase.state.getMetadata();
}
// get unique types & validate
this.runtimeMetadata.getUniqTypes(false);
+52
View File
@@ -0,0 +1,52 @@
// Copyright 2017-2019 @polkadot/api authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { ApiPromise } from '@polkadot/api';
import { ApiOptions } from '@polkadot/api/types';
import { Hash, Metadata } from '@polkadot/types';
import Mock from '@polkadot/rpc-provider/mock/index';
describe.skip('Metadata queries', () => {
let mock: Mock;
beforeEach(() => {
jest.setTimeout(3000000);
mock = new Mock();
});
it('Create API instance with metadata map and makes the runtime, rpc, state & extrinsics available', async () => {
const rpcData = await mock.send('state_getMetadata',[]);
const genesisHash = new Hash(await mock.send('chain_getBlockHash',[])).toHex();
const specVersion = 0;
const metadata: any = {};
const key = `${genesisHash}-${specVersion}`;
metadata[key] = rpcData;
const api = await ApiPromise.create({ provider: mock, metadata } as ApiOptions);
expect(api.genesisHash).toBeDefined();
expect(api.runtimeMetadata.toJSON()).toEqual(new Metadata(rpcData).toJSON());
expect(api.runtimeVersion).toBeDefined();
expect(api.rpc).toBeDefined();
expect(api.query).toBeDefined();
expect(api.tx).toBeDefined();
expect(api.derive).toBeDefined();
});
it('Create API instance without metadata and makes the runtime, rpc, state & extrinsics available', async () => {
const metadata = {};
const api = await ApiPromise.create({
provider: mock, metadata
});
expect(api.genesisHash).toBeDefined();
expect(api.runtimeMetadata).toBeDefined();
expect(api.runtimeVersion).toBeDefined();
expect(api.rpc).toBeDefined();
expect(api.query).toBeDefined();
expect(api.tx).toBeDefined();
expect(api.derive).toBeDefined();
});
});
+7
View File
@@ -114,6 +114,13 @@ export interface ApiOptions {
* @description Add custom derives to be injected
*/
derives?: DeriveCustom;
/**
* @description prebundles is a map of 'genesis hash and runtime spec version' as key to metadata's hex string
* if genesis hash and runtime spec version matches, then use metadata, else fetch it from chain
*/
metadata?: {
[key: string]: string
};
/**
* @description Transport Provider from rpc-provider. If not specified, it will default to
* connecting to a WsProvider connecting localhost with the default port, i.e. `ws://127.0.0.1:9944`