Skip to content

Commit

Permalink
fix: finalization for /blocks/head (#631)
Browse files Browse the repository at this point in the history
  • Loading branch information
TarikGul committed Aug 9, 2021
1 parent f20b033 commit 8d0d538
Showing 1 changed file with 18 additions and 43 deletions.
61 changes: 18 additions & 43 deletions src/controllers/blocks/BlocksController.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ApiPromise } from '@polkadot/api';
import { BlockHash } from '@polkadot/types/interfaces';
import { isHex } from '@polkadot/util';
import { RequestHandler } from 'express';

Expand All @@ -13,12 +12,6 @@ interface ControllerOptions {
blockWeightStore: {};
}

interface IFinalizationOpts {
hash: BlockHash;
omitFinalizedTag: boolean;
queryFinalizedHead: boolean;
}

/**
* GET a block.
*
Expand Down Expand Up @@ -114,9 +107,24 @@ export default class BlocksController extends AbstractController<BlocksService>
const eventDocsArg = eventDocs === 'true';
const extrinsicDocsArg = extrinsicDocs === 'true';

const paramFinalized = finalized === 'true' ? true : false;
const { hash, queryFinalizedHead, omitFinalizedTag } =
await this.parseFinalizationOpts(this.options.finalizes, paramFinalized);
let hash, queryFinalizedHead, omitFinalizedTag;
if (!this.options.finalizes) {
// If the network chain doesn't finalize blocks, we dont want a finalized tag.
omitFinalizedTag = true;
queryFinalizedHead = false;
hash = (await this.api.rpc.chain.getHeader()).hash;
} else if (finalized === 'false') {
// We query the finalized head to know where the latest finalized block
// is. It is a way to confirm whether the queried block is less than or
// equal to the finalized head.
omitFinalizedTag = false;
queryFinalizedHead = true;
hash = (await this.api.rpc.chain.getHeader()).hash;
} else {
omitFinalizedTag = false;
queryFinalizedHead = false;
hash = await this.api.rpc.chain.getFinalizedHead();
}

const options = {
eventDocs: eventDocsArg,
Expand Down Expand Up @@ -206,37 +214,4 @@ export default class BlocksController extends AbstractController<BlocksService>
await this.service.fetchBlockHeader(hash)
);
};

/**
* This also returns the hash for the block to query.
*
* @param optFinalizes
* @param paramFinalized
*/
private parseFinalizationOpts = async (
optFinalizes: boolean,
paramFinalized: boolean
): Promise<IFinalizationOpts> => {
let hash, queryFinalizedHead, omitFinalizedTag;
if (!optFinalizes) {
// If the network chain doesn't finalize blocks, we dont want a finalized tag.
omitFinalizedTag = true;
queryFinalizedHead = false;
hash = (await this.api.rpc.chain.getHeader()).hash;
} else if (!paramFinalized) {
omitFinalizedTag = false;
queryFinalizedHead = true;
hash = (await this.api.rpc.chain.getHeader()).hash;
} else {
omitFinalizedTag = false;
queryFinalizedHead = false;
hash = await this.api.rpc.chain.getFinalizedHead();
}

return {
hash,
omitFinalizedTag,
queryFinalizedHead,
};
};
}

0 comments on commit 8d0d538

Please sign in to comment.