@@ -578,7 +578,10 @@ export abstract class BaseProvider extends AbstractProvider {
578
578
if ( cached ) return cached ;
579
579
580
580
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 ) ;
582
585
583
586
return apiAt ;
584
587
} ;
@@ -616,6 +619,10 @@ export abstract class BaseProvider extends AbstractProvider {
616
619
const blockHash = header . hash . toHex ( ) ;
617
620
const blockNumber = header . number . toNumber ( ) ;
618
621
622
+ const cacheKey = `block-${ blockHash } ` ;
623
+ const cached = this . queryCache . get < BlockData > ( cacheKey ) ;
624
+ if ( cached ) return cached ;
625
+
619
626
const [ block , headerExtended , timestamp , receiptsFromSubql ] = await Promise . all ( [
620
627
this . api . rpc . chain . getBlock ( blockHash ) ,
621
628
this . api . derive . chain . getHeader ( blockHash ) ,
@@ -624,7 +631,7 @@ export abstract class BaseProvider extends AbstractProvider {
624
631
] ) ;
625
632
626
633
// blockscout need `toLowerCase`
627
- const author = ( await this . getEvmAddress ( headerExtended . author . toString ( ) ) ) . toLowerCase ( ) ;
634
+ const author = ( await this . getEvmAddress ( headerExtended . author . toString ( ) , blockHash ) ) . toLowerCase ( ) ;
628
635
629
636
let receipts : TransactionReceipt [ ] ;
630
637
if ( receiptsFromSubql ?. length ) {
@@ -643,7 +650,7 @@ export abstract class BaseProvider extends AbstractProvider {
643
650
644
651
const gasUsed = receipts . reduce ( ( totalGas , tx ) => totalGas . add ( tx . gasUsed ) , BIGNUMBER_ZERO ) ;
645
652
646
- return {
653
+ const blockData : BlockData = {
647
654
hash : blockHash ,
648
655
parentHash : headerExtended . parentHash . toHex ( ) ,
649
656
number : blockNumber ,
@@ -667,6 +674,13 @@ export abstract class BaseProvider extends AbstractProvider {
667
674
668
675
transactions,
669
676
} ;
677
+
678
+ const isFinalized = blockNumber <= await this . finalizedBlockNumber ;
679
+ if ( isFinalized ) {
680
+ this . queryCache . set ( cacheKey , blockData ) ;
681
+ }
682
+
683
+ return blockData ;
670
684
} ;
671
685
672
686
getBlock = async ( _blockHashOrBlockTag : BlockTag | string | Promise < BlockTag | string > ) : Promise < Block > =>
@@ -1476,14 +1490,11 @@ export abstract class BaseProvider extends AbstractProvider {
1476
1490
return logger . throwArgumentError ( 'block number should be less than u32' , 'blockNumber' , blockNumber ) ;
1477
1491
}
1478
1492
1479
- const isFinalized = blockNumber . lte ( await this . finalizedBlockNumber ) ;
1480
1493
const cacheKey = `blockHash-${ blockNumber . toHexString ( ) } ` ;
1481
1494
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 ;
1487
1498
}
1488
1499
1489
1500
const _blockHash = await this . api . rpc . chain . getBlockHash ( blockNumber . toBigInt ( ) ) ;
@@ -1493,6 +1504,8 @@ export abstract class BaseProvider extends AbstractProvider {
1493
1504
}
1494
1505
const blockHash = _blockHash . toHex ( ) ;
1495
1506
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 ) ;
1496
1509
if ( isFinalized ) {
1497
1510
this . queryCache . set ( cacheKey , blockHash ) ;
1498
1511
}
@@ -1555,18 +1568,35 @@ export abstract class BaseProvider extends AbstractProvider {
1555
1568
} ;
1556
1569
1557
1570
_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
+ }
1559
1581
1560
1582
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
+ }
1562
1596
1563
1597
return header ;
1564
1598
} catch ( error ) {
1565
- if (
1566
- typeof error === 'object' &&
1567
- typeof ( error as any ) . message === 'string' &&
1568
- ( error as any ) . message . match ( / U n a b l e t o r e t r i e v e h e a d e r a n d p a r e n t f r o m s u p p l i e d h a s h / gi)
1569
- ) {
1599
+ if ( ( error as any ) ?. message ?. match ?.( / U n a b l e t o r e t r i e v e h e a d e r a n d p a r e n t f r o m s u p p l i e d h a s h / gi) ) {
1570
1600
//@ts -ignore
1571
1601
return logger . throwError ( 'header not found' , PROVIDER_ERRORS . HEADER_NOT_FOUND , { blockHash } ) ;
1572
1602
}
0 commit comments