Skip to content

Commit

Permalink
fix: Bump polkadot-js/api to get parachain types: Document user contr…
Browse files Browse the repository at this point in the history
…act regressions (#535)
  • Loading branch information
emostov committed May 6, 2021
1 parent 77f2e1c commit 7b96c21
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 116 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
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
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
4 changes: 2 additions & 2 deletions src/services/test-helpers/responses/blocks/blocks789629.json
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

0 comments on commit 7b96c21

Please sign in to comment.