Skip to content
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

feat: KILT controller integration #542

Merged
merged 4 commits into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

## [6.0.0](https://github.com/paritytech/substrate-api-sidecar/compare/v5.0.1...v6.0.0) (2021-05-03)

### ⚠ BREAKING CHANGES

* Changes to serialization of `ConsensusEngineId` in block digest logs. Check spec diffs for details. (#535)

### Types

* Bump @polkadot/api for the latest parachain support. ([#535](https://github.com/paritytech/substrate-api-sidecar/pull/535)) ([`7b96c21`](https://github.com/paritytech/substrate-api-sidecar/commit/7b96c211c3f47c1284628b6fa20cd591d4a1c95a))


## [5.0.1](https://github.com/paritytech/substrate-api-sidecar/compare/v5.0.0...v5.0.1) (2021-05-03)

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "5.0.1",
"version": "6.0.0",
"name": "@substrate/api-sidecar",
"description": "REST service that makes it easy to interact with blockchain nodes built using Substrate's FRAME framework.",
"homepage": "https://github.com/paritytech/substrate-api-sidecar#readme",
Expand Down Expand Up @@ -36,7 +36,7 @@
"test": "substrate-exec-jest --silent"
},
"dependencies": {
"@polkadot/api": "^4.8.1",
"@polkadot/api": "^4.9.2",
"@polkadot/apps-config": "^0.90.1",
"@polkadot/util-crypto": "^6.3.1",
"@substrate/calc": "^0.2.0",
Expand All @@ -54,7 +54,7 @@
"@types/morgan": "^1.9.2",
"@types/triple-beam": "^1.3.2",
"rimraf": "^3.0.2",
"standard-version": "^9.2.0",
"standard-version": "^9.3.0",
"tsc-watch": "^4.2.9",
"typescript": "4.2.4"
},
Expand Down
2 changes: 2 additions & 0 deletions src/chains-config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ControllerConfig } from '../types/chains-config';
import { defaultControllers } from './defaultControllers';
import { dockMainnetControllers } from './dockMainnetControllers';
import { dockTestnetControllers } from './dockTestnetControllers';
import { kiltControllers } from './kiltControllers';
import { kulupuControllers } from './kulupuControllers';
import { kusamaControllers } from './kusamaControllers';
import { mandalaControllers } from './mandalaControllers';
Expand All @@ -18,6 +19,7 @@ const specToControllerMap = {
polkadot: polkadotControllers,
kusama: kusamaControllers,
kulupu: kulupuControllers,
kilt: kiltControllers,
mandala: mandalaControllers,
'dock-testnet': dockTestnetControllers,
'dock-main-runtime': dockMainnetControllers,
Expand Down
33 changes: 33 additions & 0 deletions src/chains-config/kiltControllers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { ControllerConfig } from '../types/chains-config';

/**
* Controllers for KILT's mashnet.
*/
export const kiltControllers: ControllerConfig = {
controllers: {
Blocks: true,
BlocksExtrinsics: true,
AccountsStakingPayouts: false,
AccountsBalanceInfo: true,
AccountsStakingInfo: true,
AccountsVestingInfo: false,
NodeNetwork: true,
NodeVersion: true,
NodeTransactionPool: true,
RuntimeCode: true,
RuntimeSpec: true,
RuntimeMetadata: true,
TransactionDryRun: true,
TransactionMaterial: true,
TransactionFeeEstimate: true,
TransactionSubmit: true,
PalletsStakingProgress: false,
PalletsStorage: true,
Paras: false,
},
options: {
finalizes: true,
minCalcFeeRuntime: null,
blockWeightStore: {},
},
};
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ function startUpPrompt(wsUrl: string, chainName: string, implName: string) {
'wss://rpc.plasmnet.io/',
'wss://mainnet-rpc.stafi.io',
'wss://rpc.subsocial.network',
'wss://full-nodes-lb.kilt.io:443',
];

logger.info(
Expand Down
7 changes: 3 additions & 4 deletions src/services/blocks/BlocksService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import { ExtBaseWeightValue, PerClassValue } from '../../types/chains-config';
import { IExtrinsic } from '../../types/responses/';
import {
blockHash789629,
getBlock,
mockApi,
mockBlock789629,
mockForkedBlock789629,
Expand Down Expand Up @@ -96,8 +95,8 @@ describe('BlocksService', () => {
queryFinalizedHead: false,
omitFinalizedTag: false,
};

mockApi.rpc.chain.getBlock = (() =>
const tempGetBlock = mockApi.derive.chain.getBlock;
mockApi.derive.chain.getBlock = (() =>
Promise.resolve().then(() => {
return {
block: mockBlock789629BadExt,
Expand All @@ -112,7 +111,7 @@ describe('BlocksService', () => {
)
);

mockApi.rpc.chain.getBlock = (getBlock as unknown) as GetBlock;
mockApi.derive.chain.getBlock = (tempGetBlock as unknown) as GetBlock;
});

it('Returns the finalized tag as undefined when omitFinalizedTag equals true', async () => {
Expand Down
81 changes: 24 additions & 57 deletions src/services/blocks/BlocksService.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { ApiPromise } from '@polkadot/api';
import { expandMetadata } from '@polkadot/metadata/decorate';
import { Compact, GenericCall, Struct } from '@polkadot/types';
import { Compact, GenericCall, Struct, Vec } from '@polkadot/types';
import { AbstractInt } from '@polkadot/types/codec/AbstractInt';
import {
AccountId,
Block,
BlockHash,
BlockNumber,
BlockWeights,
Digest,
DispatchInfo,
EventRecord,
Hash,
Expand Down Expand Up @@ -90,30 +88,18 @@ export class BlocksService extends AbstractService {
): Promise<IBlock> {
const { api } = this;

let block, events, finalizedHead, sessionValidators;
if (typeof api.query.session?.validators?.at === 'function') {
[
{ block },
events,
sessionValidators,
finalizedHead,
] = await Promise.all([
api.rpc.chain.getBlock(hash),
this.fetchEvents(api, hash),
api.query.session.validators.at(hash),
queryFinalizedHead
? api.rpc.chain.getFinalizedHead()
: Promise.resolve(hash),
]);
} else {
[{ block }, events, finalizedHead] = await Promise.all([
api.rpc.chain.getBlock(hash),
this.fetchEvents(api, hash),
queryFinalizedHead
? api.rpc.chain.getFinalizedHead()
: Promise.resolve(hash),
]);
const [deriveBlock, events, finalizedHead] = await Promise.all([
api.derive.chain.getBlock(hash),
this.fetchEvents(api, hash),
queryFinalizedHead
? api.rpc.chain.getFinalizedHead()
: Promise.resolve(hash),
]);

if (deriveBlock === undefined) {
throw new InternalServerError('Error querying for block');
}
const { block, author: authorId } = deriveBlock;

const {
parentHash,
Expand All @@ -123,10 +109,6 @@ export class BlocksService extends AbstractService {
digest,
} = block.header;

const authorId = sessionValidators
? this.extractAuthor(sessionValidators, digest)
: undefined;

const logs = digest.logs.map(({ type, index, value }) => {
return { type, index, value };
});
Expand Down Expand Up @@ -339,15 +321,23 @@ export class BlocksService extends AbstractService {
*/
private extractExtrinsics(
block: Block,
events: EventRecord[] | string,
events: Vec<EventRecord> | string,
extrinsicDocs: boolean
) {
const defaultSuccess = typeof events === 'string' ? events : false;
// Note, if events is a string then there was an issue getting them from the node.
// In this case we try and create the calls with the registry on `block`.
// The block from `api.derive.chain.getBlock` has the most recent registry,
// which could cause issues with historical querries.
// On the other hand, we know `events` will have the correctly dated query
// since it is a storage query.
const registry =
typeof events === 'string' ? block.registry : events.registry;

return block.extrinsics.map((extrinsic) => {
const { method, nonce, signature, signer, isSigned, tip } = extrinsic;
const hash = u8aToHex(blake2AsU8a(extrinsic.toU8a(), 256));
const call = block.registry.createType('Call', method);
const call = registry.createType('Call', method);

return {
method: {
Expand All @@ -356,7 +346,7 @@ export class BlocksService extends AbstractService {
},
signature: isSigned ? { signature, signer } : null,
nonce: isSigned ? nonce : null,
args: this.parseGenericCall(call, block.registry).args,
args: this.parseGenericCall(call, registry).args,
tip: isSigned ? tip : null,
hash,
info: {},
Expand Down Expand Up @@ -614,7 +604,7 @@ export class BlocksService extends AbstractService {
private async fetchEvents(
api: ApiPromise,
hash: BlockHash
): Promise<EventRecord[] | string> {
): Promise<Vec<EventRecord> | string> {
try {
return await api.query.system.events.at(hash);
} catch {
Expand Down Expand Up @@ -692,29 +682,6 @@ export class BlocksService extends AbstractService {
};
}

// Almost exact mimic of https://github.com/polkadot-js/api/blob/e51e89df5605b692033df864aa5ab6108724af24/packages/api-derive/src/type/util.ts#L6
// but we save a call to `getHeader` by hardcoding the logic here and using the digest from the blocks header.
private extractAuthor(
sessionValidators: AccountId[],
digest: Digest
): AccountId | undefined {
const [pitem] = digest.logs.filter(({ type }) => type === 'PreRuntime');
// extract from the substrate 2.0 PreRuntime digest
if (pitem) {
const [engine, data] = pitem.asPreRuntime;
return engine.extractAuthor(data, sessionValidators);
} else {
const [citem] = digest.logs.filter(({ type }) => type === 'Consensus');
// extract author from the consensus (substrate 1.0, digest)
if (citem) {
const [engine, data] = citem.asConsensus;
return engine.extractAuthor(data, sessionValidators);
}
}

return undefined;
}

/**
* When querying a block this will immediately inform the request whether
* or not the queried block is considered finalized at the time of querying.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
"type": "PreRuntime",
"index": "6",
"value": [
"BABE",
"0x45424142",
"0x036d000000f4f4d80f00000000ec9bd8e2d0368c97f3d888837f7283bbe08266869eb613159db547905026c2502a70f168b9ffcc233344005d11ebecd166769200d270a2eaa642118a00acb708a0487a440b0caf3dd5c91ab173e80ddfe5735ef8b938ea87a6105a1161612707"
]
},
{
"type": "Seal",
"index": "5",
"value": [
"BABE",
"0x45424142",
"0xae78514e1de84a7d32e55b9b652f9d408ab1f7b4bfdbf6b2fad9cad94a91b86b0161cabf08f5ae1d3a1aa4993e2d96d56c94b03cee0898ccb8385a546084f88b"
]
}
Expand Down
Loading