-
Notifications
You must be signed in to change notification settings - Fork 160
Implement runtime/code & runtime/spec
#228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9ba1f58
Implement `runtime/code` & `runtime/spec`
emostov cf0ac4a
Apply suggestions Joe from code review
emostov fdd6462
Add eof
emostov 2904c0f
Merge branch 'master' of https://github.com/paritytech/substrate-api-…
emostov 6349ff8
Update readme links
emostov 31b8beb
Update src/services/runtime/RuntimeCodeService.ts
emostov 801f378
Merge origin master
emostov 8709b2c
Merge branch 'zeke-runtime-code' of https://github.com/paritytech/sub…
emostov 51d3cfe
Merge branch 'master' into zeke-runtime-code
joepetrowski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { ApiPromise } from '@polkadot/api'; | ||
| import { RequestHandler } from 'express'; | ||
|
|
||
| import { RuntimeCodeService } from '../../services'; | ||
| import AbstractController from '../AbstractController'; | ||
|
|
||
| /** | ||
| * Get version information of the Substrate runtime. | ||
| * | ||
| * Query: | ||
| * - (Optional)`at`: Block at which to retrieve runtime version information at. Block | ||
| * identifier, as the block height or block hash. Defaults to most recent block. | ||
| * | ||
| * Returns: | ||
| * - `at`: Block number and hash at which the call was made. | ||
| * - `code`: Runtime code wasm blob. | ||
| */ | ||
| export default class RuntimeCodeController extends AbstractController< | ||
| RuntimeCodeService | ||
| > { | ||
| constructor(api: ApiPromise) { | ||
| super(api, '/runtime/code', new RuntimeCodeService(api)); | ||
| this.initRoutes(); | ||
| } | ||
|
|
||
| protected initRoutes(): void { | ||
| this.safeMountAsyncGetHandlers([['', this.getCodeAtBlock]]); | ||
| } | ||
|
|
||
| /** | ||
| * Get the chain's latest metadata in a decoded, JSON format. | ||
| * | ||
| * @param _req Express Request | ||
| * @param res Express Response | ||
| */ | ||
|
|
||
| private getCodeAtBlock: RequestHandler = async ( | ||
| { query: { at } }, | ||
| res | ||
| ): Promise<void> => { | ||
| const hash = | ||
| typeof at === 'string' | ||
| ? await this.getHashForBlock(at) | ||
| : await this.api.rpc.chain.getFinalizedHead(); | ||
|
|
||
| RuntimeCodeController.sanitizedSend( | ||
| res, | ||
| await this.service.fetchCode(hash) | ||
| ); | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import { ApiPromise } from '@polkadot/api'; | ||
| import { RequestHandler } from 'express'; | ||
|
|
||
| import { RuntimeSpecService } from '../../services'; | ||
| import AbstractController from '../AbstractController'; | ||
|
|
||
| /** | ||
| * Get version information of the Substrate runtime. | ||
| * | ||
| * Query: | ||
| * - (Optional)`at`: Block at which to retrieve runtime version information at. Block | ||
|
emostov marked this conversation as resolved.
Outdated
|
||
| * identifier, as the block height or block hash. Defaults to most recent block. | ||
| * | ||
| * Returns: | ||
| * - `at`: Block number and hash at which the call was made. | ||
| * - `authoringVersion`: The version of the authorship interface. An authoring node | ||
| * will not attempt to author blocks unless this is equal to its native runtime. | ||
| * - `chainType`: Type of the chain. | ||
| * - `implVersion`: Version of the implementation specification. Non-consensus-breaking | ||
| * optimizations are about the only changes that could be made which would | ||
| * result in only the `impl_version` changing. The `impl_version` is set to 0 | ||
| * when `spec_version` is incremented. | ||
| * - `specName`: Identifies different Substrate runtimes. | ||
|
emostov marked this conversation as resolved.
Outdated
|
||
| * - `specVersion`: Version of the runtime specification. | ||
| * - `transactionVersion`: All existing dispatches are fully compatible when this | ||
| * number doesn't change. This number must change when an existing dispatchable | ||
| * (module ID, dispatch ID) is changed, either through an alteration in its | ||
| * user-level semantics, a parameter added/removed/changed, a dispatchable | ||
| * its index. | ||
| * - `properties`: Arbitrary properties defined in the chain spec. | ||
| */ | ||
| export default class RuntimeSpecController extends AbstractController< | ||
| RuntimeSpecService | ||
| > { | ||
| constructor(api: ApiPromise) { | ||
| super(api, '/runtime/spec', new RuntimeSpecService(api)); | ||
| this.initRoutes(); | ||
| } | ||
|
|
||
| protected initRoutes(): void { | ||
| this.safeMountAsyncGetHandlers([['', this.getSpec]]); | ||
| } | ||
|
|
||
| private getSpec: RequestHandler = async ({ query: { at } }, res) => { | ||
| const hash = | ||
| typeof at === 'string' | ||
| ? await this.getHashForBlock(at) | ||
| : await this.api.rpc.chain.getFinalizedHead(); | ||
|
|
||
| RuntimeSpecController.sanitizedSend( | ||
| res, | ||
| await this.service.fetchSpec(hash) | ||
| ); | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,3 @@ | ||
| export { default as Metadata } from './RuntimeMetadataController'; | ||
| export { default as RuntimeCode } from './RuntimeCodeController'; | ||
| export { default as RuntimeSpec } from './RuntimeSpecController'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { sanitizeNumbers } from '../../sanitize/sanitizeNumbers'; | ||
| import { blockHash789629, mockApi } from '../test-helpers/mock'; | ||
| import * as codeResponse from '../test-helpers/responses/runtime/code789629.json'; | ||
| import { RuntimeCodeService } from './RuntimeCodeService'; | ||
|
|
||
| const runtimeCodeService = new RuntimeCodeService(mockApi); | ||
|
|
||
| describe('RuntimeCodeService', () => { | ||
| describe('fetchCode', () => { | ||
| it('works when ApiPromise works', async () => { | ||
| expect( | ||
| sanitizeNumbers( | ||
| await runtimeCodeService.fetchCode(blockHash789629) | ||
| ) | ||
| ).toStrictEqual(codeResponse); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { Option, Raw } from '@polkadot/types'; | ||
| import { BlockHash } from '@polkadot/types/interfaces'; | ||
| import { IMetadataCode } from 'src/types/responses'; | ||
|
|
||
| import { AbstractService } from '../AbstractService'; | ||
|
|
||
| // https://github.com/shawntabrizi/substrate-graph-benchmarks/blob/master/js/extensions/known-keys.js#L21 | ||
|
emostov marked this conversation as resolved.
Outdated
|
||
| export const CODE_KEY = '0x3a636f6465'; | ||
|
joepetrowski marked this conversation as resolved.
Outdated
|
||
|
|
||
| export class RuntimeCodeService extends AbstractService { | ||
| /** | ||
| * Fetch `Metadata` in decoded JSON form. | ||
| * | ||
| * @param hash `BlockHash` to make call at | ||
| */ | ||
| async fetchCode(hash: BlockHash): Promise<IMetadataCode> { | ||
| const api = await this.ensureMeta(hash); | ||
|
|
||
| const [code, { number }] = await Promise.all([ | ||
| api.rpc.state.getStorage(CODE_KEY, hash), | ||
| api.rpc.chain.getHeader(hash), | ||
| ]); | ||
|
|
||
| return { | ||
| at: { | ||
| hash, | ||
| height: number.unwrap().toString(10), | ||
| }, | ||
| code: code as Option<Raw>, | ||
| }; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { sanitizeNumbers } from '../../sanitize/sanitizeNumbers'; | ||
| import { blockHash789629, mockApi } from '../test-helpers/mock'; | ||
| import * as response from '../test-helpers/responses/runtime/spec.json'; | ||
| import { RuntimeSpecService } from './RuntimeSpecService'; | ||
|
|
||
| const runtimeSpecService = new RuntimeSpecService(mockApi); | ||
|
|
||
| describe('RuntimeSpecService', () => { | ||
| describe('fetchSpec', () => { | ||
| it('works when ApiPromise works', async () => { | ||
| expect( | ||
| sanitizeNumbers( | ||
| await runtimeSpecService.fetchSpec(blockHash789629) | ||
| ) | ||
| ).toStrictEqual(response); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import { BlockHash } from '@polkadot/types/interfaces'; | ||
| import { IRuntimeSpec } from 'src/types/responses'; | ||
|
|
||
| import { AbstractService } from '../AbstractService'; | ||
| export class RuntimeSpecService extends AbstractService { | ||
| async fetchSpec(hash: BlockHash): Promise<IRuntimeSpec> { | ||
| const [ | ||
| { | ||
| authoringVersion, | ||
| specName, | ||
| specVersion, | ||
| transactionVersion, | ||
| implVersion, | ||
| }, | ||
| chainType, | ||
| properties, | ||
| ] = await Promise.all([ | ||
| this.api.rpc.state.getRuntimeVersion(hash), | ||
| this.api.rpc.system.chainType(), | ||
| this.api.rpc.system.properties(), | ||
| ]); | ||
|
|
||
| return { | ||
| authoringVersion, | ||
| transactionVersion, | ||
| implVersion, | ||
| specName, | ||
| specVersion, | ||
| chainType, | ||
| properties, | ||
| }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,3 @@ | ||
| export * from './RuntimeMetadataService'; | ||
| export * from './RuntimeCodeService'; | ||
| export * from './RuntimeSpecService'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "at": { | ||
| "hash": "0x7b713de604a99857f6c25eacc115a4f28d2611a23d9ddff99ab0e4f1c17a8578", | ||
| "height": "789629" | ||
| }, | ||
| "code": "0x" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| "authoringVersion": "0", | ||
| "transactionVersion": "2", | ||
| "implVersion": "0", | ||
| "specName": "polkadot", | ||
| "specVersion": "16", | ||
| "chainType": { | ||
| "Live": null | ||
| }, | ||
| "properties": { | ||
| "ss58Format": "0", | ||
| "tokenDecimals": "12", | ||
| "tokenSymbol": "DOT" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { Option, Raw } from '@polkadot/types'; | ||
|
|
||
| import { IAt } from '.'; | ||
|
|
||
| export interface IMetadataCode { | ||
| at: IAt; | ||
| code: Option<Raw>; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import { ChainProperties, ChainType } from '@polkadot/types/interfaces'; | ||
| import { Text, u32 } from '@polkadot/types/primitive'; | ||
|
|
||
| export interface IRuntimeSpec { | ||
| authoringVersion: u32; | ||
| transactionVersion: u32; | ||
| implVersion: u32; | ||
| specName: Text; | ||
| specVersion: u32; | ||
| chainType: ChainType; | ||
| properties: ChainProperties; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.