@@ -88,7 +88,6 @@ import {
88
88
runWithRetries ,
89
89
runWithTiming ,
90
90
sendTx ,
91
- sleep ,
92
91
sortObjByKey ,
93
92
subqlReceiptAdapter ,
94
93
throwNotImplemented ,
@@ -341,14 +340,17 @@ export abstract class BaseProvider extends AbstractProvider {
341
340
}
342
341
343
342
get bestBlockHash ( ) {
344
- return firstValueFrom ( this . best$ ) . then ( ( { hash } ) => hash ) ;
343
+ return this . blockCache . lastCachedBlock . hash ;
345
344
}
345
+
346
346
get bestBlockNumber ( ) {
347
- return firstValueFrom ( this . best$ ) . then ( ( { number } ) => number ) ;
347
+ return this . blockCache . lastCachedBlock . number ;
348
348
}
349
+
349
350
get finalizedBlockHash ( ) {
350
351
return firstValueFrom ( this . finalized$ ) . then ( ( { hash } ) => hash ) ;
351
352
}
353
+
352
354
get finalizedBlockNumber ( ) {
353
355
return firstValueFrom ( this . finalized$ ) . then ( ( { number } ) => number ) ;
354
356
}
@@ -362,15 +364,21 @@ export abstract class BaseProvider extends AbstractProvider {
362
364
this . finalizedHead$ = this . api . rx . rpc . chain . subscribeFinalizedHeads ( ) ;
363
365
364
366
const headSub = this . head$ . subscribe ( header => {
365
- this . best$ . next ( { hash : header . hash . toHex ( ) , number : header . number . toNumber ( ) } ) ;
367
+ this . best$ . next ( {
368
+ hash : header . hash . toHex ( ) ,
369
+ number : header . number . toNumber ( ) ,
370
+ } ) ;
366
371
} ) ;
367
372
368
373
const finalizedSub = this . finalizedHead$ . subscribe ( header => {
369
374
this . finalizedBlockHashes . add ( header . hash . toHex ( ) ) ;
370
- this . finalized$ . next ( { hash : header . hash . toHex ( ) , number : header . number . toNumber ( ) } ) ;
375
+ this . finalized$ . next ( {
376
+ hash : header . hash . toHex ( ) ,
377
+ number : header . number . toNumber ( ) ,
378
+ } ) ;
371
379
} ) ;
372
380
373
- await firstValueFrom ( this . head$ ) ;
381
+ const firstBlock = await firstValueFrom ( this . head$ ) ;
374
382
await firstValueFrom ( this . finalizedHead$ ) ;
375
383
376
384
const safeHead$ = this . safeMode
@@ -393,6 +401,11 @@ export abstract class BaseProvider extends AbstractProvider {
393
401
this . #finalizedHeadTasks. set ( header . hash . toHex ( ) , task ) ;
394
402
} ) ;
395
403
404
+ this . blockCache . setlastCachedBlock ( {
405
+ hash : firstBlock . hash . toHex ( ) ,
406
+ number : firstBlock . number . toNumber ( ) ,
407
+ } ) ;
408
+
396
409
return ( ) => {
397
410
headSub . unsubscribe ( ) ;
398
411
finalizedSub . unsubscribe ( ) ;
@@ -410,7 +423,7 @@ export abstract class BaseProvider extends AbstractProvider {
410
423
const receipts = await getAllReceiptsAtBlock ( this . api , blockHash ) ;
411
424
// update block cache
412
425
this . blockCache . addReceipts ( blockHash , receipts ) ;
413
- this . blockCache . setlastCachedHeight ( blockNumber ) ;
426
+ this . blockCache . setlastCachedBlock ( { hash : blockHash , number : blockNumber } ) ;
414
427
415
428
// eth_subscribe
416
429
await this . _notifySubscribers ( header , receipts ) ;
@@ -558,11 +571,9 @@ export abstract class BaseProvider extends AbstractProvider {
558
571
isReady = async ( ) : Promise < void > => {
559
572
try {
560
573
await this . api . isReadyOrError ;
561
- await this . getNetwork ( ) ;
562
574
563
- if ( ! this . #subscription) {
564
- this . #subscription = this . startSubscriptions ( ) ;
565
- }
575
+ this . #subscription ??= this . startSubscriptions ( ) ;
576
+
566
577
// wait for subscription to happen
567
578
await this . #subscription;
568
579
} catch ( e ) {
@@ -593,27 +604,27 @@ export abstract class BaseProvider extends AbstractProvider {
593
604
} ;
594
605
595
606
getNetwork = async ( ) : Promise < Network > => {
596
- if ( ! this . network ) {
597
- this . network = {
598
- name : this . api . runtimeVersion . specName . toString ( ) ,
599
- chainId : await this . chainId ( ) ,
600
- } ;
601
- }
607
+ await this . isReady ( ) ;
608
+
609
+ this . network ??= {
610
+ name : this . api . runtimeVersion . specName . toString ( ) ,
611
+ chainId : await this . chainId ( ) ,
612
+ } ;
602
613
603
614
return this . network ;
604
615
} ;
605
616
606
- netVersion = async ( ) : Promise < string > => {
607
- return this . api . consts . evmAccounts . chainId . toString ( ) ;
608
- } ;
617
+ netVersion = async ( ) : Promise < string > =>
618
+ this . api . consts . evmAccounts . chainId . toString ( ) ;
609
619
610
- chainId = async ( ) : Promise < number > => {
611
- return this . api . consts . evmAccounts . chainId . toNumber ( ) ;
612
- } ;
620
+ chainId = async ( ) : Promise < number > =>
621
+ this . api . consts . evmAccounts . chainId . toNumber ( ) ;
613
622
614
- getBlockNumber = async ( ) : Promise < number > => {
615
- return this . safeMode ? this . finalizedBlockNumber : this . bestBlockNumber ;
616
- } ;
623
+ getBlockNumber = async ( ) : Promise < number > => (
624
+ this . safeMode
625
+ ? this . finalizedBlockNumber
626
+ : this . bestBlockNumber
627
+ ) ;
617
628
618
629
getBlockData = async ( _blockTag : BlockTag | Promise < BlockTag > , full ?: boolean ) : Promise < BlockData > => {
619
630
const blockTag = await this . _ensureSafeModeBlockTagFinalization ( _blockTag ) ;
@@ -1820,16 +1831,14 @@ export abstract class BaseProvider extends AbstractProvider {
1820
1831
1821
1832
_getSubqlMissedLogs = async ( toBlock : number , filter : SanitizedLogFilter ) : Promise < Log [ ] > => {
1822
1833
const targetBlock = await this . _getMaxTargetBlock ( toBlock ) ;
1823
- const lastProcessedHeight = await this . _checkSubqlHeight ( ) ; // all missed logs should be in cache
1834
+ const lastProcessedHeight = await this . _checkSubqlHeight ( ) ;
1824
1835
const missedBlockCount = targetBlock - lastProcessedHeight ;
1825
1836
if ( missedBlockCount <= 0 ) return [ ] ;
1826
1837
1827
1838
const firstMissedBlock = lastProcessedHeight + 1 ;
1828
1839
const missedBlocks = Array . from ( { length : missedBlockCount } , ( _ , i ) => firstMissedBlock + i ) ;
1829
1840
const missedBlockHashes = await Promise . all ( missedBlocks . map ( this . _getBlockHash . bind ( this ) ) ) ;
1830
1841
1831
- await this . _waitForCache ( targetBlock ) ;
1832
-
1833
1842
// no need to filter by blocknumber anymore, since these logs are from missedBlocks directly
1834
1843
return missedBlockHashes
1835
1844
. map ( this . blockCache . getLogsAtBlock . bind ( this ) )
@@ -1855,25 +1864,6 @@ export abstract class BaseProvider extends AbstractProvider {
1855
1864
. map ( log => this . formatter . filterLog ( log ) ) ;
1856
1865
} ;
1857
1866
1858
- _waitForCache = async ( _targetBlock : number ) => {
1859
- const CACHE_MAX_WAIT_BLOCKS = 2 ;
1860
-
1861
- const targetBlock = await this . _getMaxTargetBlock ( _targetBlock ) ;
1862
- let lastCachedHeight = await this . subql . getLastProcessedHeight ( ) ;
1863
- if ( targetBlock - lastCachedHeight > CACHE_MAX_WAIT_BLOCKS ) {
1864
- return logger . throwError (
1865
- 'blockCache is not synced to target block, please wait for it to catch up' ,
1866
- Logger . errors . SERVER_ERROR ,
1867
- { targetBlock, lastCachedHeight }
1868
- ) ;
1869
- }
1870
-
1871
- while ( lastCachedHeight < targetBlock ) {
1872
- await sleep ( 1000 ) ;
1873
- lastCachedHeight = this . blockCache . lastCachedHeight ;
1874
- }
1875
- } ;
1876
-
1877
1867
getIndexerMetadata = async ( ) : Promise < _Metadata | undefined > => {
1878
1868
return this . subql ?. getIndexerMetadata ( ) ;
1879
1869
} ;
0 commit comments