Skip to content

Commit d459d81

Browse files
committed
polish
1 parent 56bbc57 commit d459d81

File tree

1 file changed

+46
-16
lines changed

1 file changed

+46
-16
lines changed

packages/eth-providers/src/base-provider.ts

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,10 @@ export abstract class BaseProvider extends AbstractProvider {
578578
if (cached) return cached;
579579

580580
const apiAt = await this.api.at(blockHash);
581-
this.apiCache.set(blockHash, apiAt); // cache key is blockhash, so no need to check for finalization
581+
582+
// do we need to check for finalization here?
583+
// ApiAt is only a decoration and the actuall call is through api, so should be fine?
584+
this.apiCache.set(blockHash, apiAt);
582585

583586
return apiAt;
584587
};
@@ -616,6 +619,10 @@ export abstract class BaseProvider extends AbstractProvider {
616619
const blockHash = header.hash.toHex();
617620
const blockNumber = header.number.toNumber();
618621

622+
const cacheKey = `block-${blockHash}`;
623+
const cached = this.queryCache.get<BlockData>(cacheKey);
624+
if (cached) return cached;
625+
619626
const [block, headerExtended, timestamp, receiptsFromSubql] = await Promise.all([
620627
this.api.rpc.chain.getBlock(blockHash),
621628
this.api.derive.chain.getHeader(blockHash),
@@ -624,7 +631,7 @@ export abstract class BaseProvider extends AbstractProvider {
624631
]);
625632

626633
// blockscout need `toLowerCase`
627-
const author = (await this.getEvmAddress(headerExtended.author.toString())).toLowerCase();
634+
const author = (await this.getEvmAddress(headerExtended.author.toString(), blockHash)).toLowerCase();
628635

629636
let receipts: TransactionReceipt[];
630637
if (receiptsFromSubql?.length) {
@@ -643,7 +650,7 @@ export abstract class BaseProvider extends AbstractProvider {
643650

644651
const gasUsed = receipts.reduce((totalGas, tx) => totalGas.add(tx.gasUsed), BIGNUMBER_ZERO);
645652

646-
return {
653+
const blockData: BlockData = {
647654
hash: blockHash,
648655
parentHash: headerExtended.parentHash.toHex(),
649656
number: blockNumber,
@@ -667,6 +674,13 @@ export abstract class BaseProvider extends AbstractProvider {
667674

668675
transactions,
669676
};
677+
678+
const isFinalized = blockNumber <= await this.finalizedBlockNumber;
679+
if (isFinalized) {
680+
this.queryCache.set(cacheKey, blockData);
681+
}
682+
683+
return blockData;
670684
};
671685

672686
getBlock = async (_blockHashOrBlockTag: BlockTag | string | Promise<BlockTag | string>): Promise<Block> =>
@@ -1476,14 +1490,11 @@ export abstract class BaseProvider extends AbstractProvider {
14761490
return logger.throwArgumentError('block number should be less than u32', 'blockNumber', blockNumber);
14771491
}
14781492

1479-
const isFinalized = blockNumber.lte(await this.finalizedBlockNumber);
14801493
const cacheKey = `blockHash-${blockNumber.toHexString()}`;
14811494

1482-
if (isFinalized) {
1483-
const cached = this.queryCache.get(cacheKey);
1484-
if (cached) {
1485-
return cached;
1486-
}
1495+
const cached = this.queryCache.get(cacheKey);
1496+
if (cached) {
1497+
return cached;
14871498
}
14881499

14891500
const _blockHash = await this.api.rpc.chain.getBlockHash(blockNumber.toBigInt());
@@ -1493,6 +1504,8 @@ export abstract class BaseProvider extends AbstractProvider {
14931504
}
14941505
const blockHash = _blockHash.toHex();
14951506

1507+
// no need to check for canonicality here since this hash is just queries from rpc.chain.getBlockHash
1508+
const isFinalized = blockNumber.lte(await this.finalizedBlockNumber);
14961509
if (isFinalized) {
14971510
this.queryCache.set(cacheKey, blockHash);
14981511
}
@@ -1555,18 +1568,35 @@ export abstract class BaseProvider extends AbstractProvider {
15551568
};
15561569

15571570
_getBlockHeader = async (blockTag?: BlockTag | Promise<BlockTag>): Promise<Header> => {
1558-
const blockHash = await this._getBlockHash(await blockTag);
1571+
const [blockHash, blockNumber] = await Promise.all([
1572+
this._getBlockHash(await blockTag),
1573+
this._getBlockNumber(await blockTag),
1574+
]);
1575+
1576+
const cacheKey = `header-${blockNumber}`;
1577+
const cached = this.queryCache.get<Header>(cacheKey);
1578+
if (cached) {
1579+
return cached;
1580+
}
15591581

15601582
try {
1561-
const header = await this.api.rpc.chain.getHeader(blockHash);
1583+
const [header, isCanonical] = await Promise.all([
1584+
this.api.rpc.chain.getHeader(blockHash),
1585+
this._isBlockCanonical(blockHash, blockNumber),
1586+
]);
1587+
1588+
const isFinalized = (
1589+
await this.finalizedBlockNumber >= blockNumber &&
1590+
isCanonical
1591+
);
1592+
1593+
if (isFinalized) {
1594+
this.queryCache.set(cacheKey, header);
1595+
}
15621596

15631597
return header;
15641598
} catch (error) {
1565-
if (
1566-
typeof error === 'object' &&
1567-
typeof (error as any).message === 'string' &&
1568-
(error as any).message.match(/Unable to retrieve header and parent from supplied hash/gi)
1569-
) {
1599+
if ((error as any)?.message?.match?.(/Unable to retrieve header and parent from supplied hash/gi)) {
15701600
//@ts-ignore
15711601
return logger.throwError('header not found', PROVIDER_ERRORS.HEADER_NOT_FOUND, { blockHash });
15721602
}

0 commit comments

Comments
 (0)