File tree Expand file tree Collapse file tree 2 files changed +31
-4
lines changed
Expand file tree Collapse file tree 2 files changed +31
-4
lines changed Original file line number Diff line number Diff line change @@ -770,15 +770,27 @@ export class EthImpl implements Eth {
770770
771771 /**
772772 * Gets the block by its block number.
773- * @param blockNumOrTag
773+ * @param blockNumOrTag Possible values are earliest/pending/latest or hex, and can't be null (validator check).
774774 * @param showDetails
775775 */
776776 async getBlockByNumber ( blockNumOrTag : string , showDetails : boolean , requestId ?: string ) : Promise < Block | null > {
777777 const requestIdPrefix = formatRequestIdMessage ( requestId ) ;
778778 this . logger . trace ( `${ requestIdPrefix } getBlockByNumber(blockNum=${ blockNumOrTag } , showDetails=${ showDetails } )` ) ;
779- return this . getBlock ( blockNumOrTag , showDetails , requestId ) . catch ( ( e : any ) => {
780- throw this . genericErrorHandler ( e , `${ requestIdPrefix } Failed to retrieve block for blockNum ${ blockNumOrTag } ` ) ;
781- } ) ;
779+
780+ const cacheKey = `eth_getBlockByNumber_${ blockNumOrTag } _${ showDetails } ` ;
781+ let block = this . cache . get ( cacheKey ) ;
782+ if ( ! block ) {
783+ block = await this . getBlock ( blockNumOrTag , showDetails , requestId ) . catch ( ( e : any ) => {
784+ throw this . genericErrorHandler ( e , `${ requestIdPrefix } Failed to retrieve block for blockNum ${ blockNumOrTag } ` ) ;
785+ } ) ;
786+
787+ if ( blockNumOrTag != EthImpl . blockLatest && blockNumOrTag != EthImpl . blockPending ) {
788+ this . logger . trace ( `${ requestIdPrefix } caching ${ cacheKey } for ${ constants . CACHE_TTL . ONE_HOUR } ms` ) ;
789+ this . cache . set ( cacheKey , block ) ;
790+ }
791+ }
792+
793+ return block ;
782794 }
783795
784796 /**
Original file line number Diff line number Diff line change @@ -677,6 +677,21 @@ describe('Eth calls using MirrorNode', async function () {
677677 verifyBlockConstants ( result ) ;
678678 } ) ;
679679
680+ it ( 'eth_getBlockByNumber should return cached result' , async function ( ) {
681+ // mirror node request mocks
682+ restMock . onGet ( `blocks/${ blockNumber } ` ) . reply ( 200 , defaultBlock ) ;
683+ restMock . onGet ( `contracts/results?timestamp=gte:${ defaultBlock . timestamp . from } ×tamp=lte:${ defaultBlock . timestamp . to } &limit=100&order=asc` ) . reply ( 200 , defaultContractResults ) ;
684+ restMock . onGet ( `contracts/${ contractAddress1 } /results/${ contractTimestamp1 } ` ) . reply ( 200 , defaultDetailedContractResults ) ;
685+ restMock . onGet ( `contracts/${ contractAddress2 } /results/${ contractTimestamp2 } ` ) . reply ( 200 , defaultDetailedContractResults ) ;
686+ restMock . onGet ( 'network/fees' ) . reply ( 200 , defaultNetworkFees ) ;
687+ const resBeforeCache = await ethImpl . getBlockByNumber ( EthImpl . numberTo0x ( blockNumber ) , false ) ;
688+
689+ restMock . onGet ( `blocks/${ blockNumber } ` ) . reply ( 404 ) ;
690+ const resAfterCache = await ethImpl . getBlockByNumber ( EthImpl . numberTo0x ( blockNumber ) , false ) ;
691+
692+ expect ( resBeforeCache ) . to . eq ( resAfterCache ) ;
693+ } ) ;
694+
680695 it ( 'eth_getBlockByNumber with zero transactions' , async function ( ) {
681696 // mirror node request mocks
682697 restMock . onGet ( `blocks/${ blockNumber } ` ) . reply ( 200 , { ...defaultBlock , gas_used : 0 } ) ;
You can’t perform that action at this time.
0 commit comments