@@ -303,6 +303,7 @@ type BlockChain struct {
303
303
blockProcCounter int32
304
304
scope event.SubscriptionScope
305
305
genesisBlock * types.Block
306
+ indexServers indexServers
306
307
307
308
// This mutex synchronizes chain write operations.
308
309
// Readers don't need to take it, they can just read the database.
@@ -523,9 +524,15 @@ func NewBlockChain(db ethdb.Database, genesis *Genesis, engine consensus.Engine,
523
524
if bc .cfg .TxLookupLimit >= 0 {
524
525
bc .txIndexer = newTxIndexer (uint64 (bc .cfg .TxLookupLimit ), bc )
525
526
}
527
+ bc .indexServers .init (bc )
526
528
return bc , nil
527
529
}
528
530
531
+ // RegisterIndexer registers a new indexer to the chain.
532
+ func (bc * BlockChain ) RegisterIndexer (indexer Indexer ) {
533
+ bc .indexServers .register (indexer )
534
+ }
535
+
529
536
func (bc * BlockChain ) setupSnapshot () {
530
537
// Short circuit if the chain is established with path scheme, as the
531
538
// state snapshot has been integrated into path database natively.
@@ -638,6 +645,7 @@ func (bc *BlockChain) loadLastState() error {
638
645
if head := rawdb .ReadFinalizedBlockHash (bc .db ); head != (common.Hash {}) {
639
646
if block := bc .GetBlockByHash (head ); block != nil {
640
647
bc .currentFinalBlock .Store (block .Header ())
648
+ bc .indexServers .setFinalBlock (block .NumberU64 ())
641
649
headFinalizedBlockGauge .Update (int64 (block .NumberU64 ()))
642
650
bc .currentSafeBlock .Store (block .Header ())
643
651
headSafeBlockGauge .Update (int64 (block .NumberU64 ()))
@@ -685,6 +693,7 @@ func (bc *BlockChain) initializeHistoryPruning(latest uint64) error {
685
693
return errors .New ("unexpected database tail" )
686
694
}
687
695
bc .historyPrunePoint .Store (predefinedPoint )
696
+ bc .indexServers .setHistoryCutoff (predefinedPoint .BlockNumber )
688
697
return nil
689
698
690
699
case history .KeepPostMerge :
@@ -706,6 +715,7 @@ func (bc *BlockChain) initializeHistoryPruning(latest uint64) error {
706
715
return errors .New ("unexpected database tail" )
707
716
}
708
717
bc .historyPrunePoint .Store (predefinedPoint )
718
+ bc .indexServers .setHistoryCutoff (predefinedPoint .BlockNumber )
709
719
return nil
710
720
711
721
default :
@@ -768,9 +778,11 @@ func (bc *BlockChain) SetFinalized(header *types.Header) {
768
778
if header != nil {
769
779
rawdb .WriteFinalizedBlockHash (bc .db , header .Hash ())
770
780
headFinalizedBlockGauge .Update (int64 (header .Number .Uint64 ()))
781
+ bc .indexServers .setFinalBlock (header .Number .Uint64 ())
771
782
} else {
772
783
rawdb .WriteFinalizedBlockHash (bc .db , common.Hash {})
773
784
headFinalizedBlockGauge .Update (0 )
785
+ bc .indexServers .setFinalBlock (0 )
774
786
}
775
787
}
776
788
@@ -1133,6 +1145,7 @@ func (bc *BlockChain) Reset() error {
1133
1145
// ResetWithGenesisBlock purges the entire blockchain, restoring it to the
1134
1146
// specified genesis state.
1135
1147
func (bc * BlockChain ) ResetWithGenesisBlock (genesis * types.Block ) error {
1148
+ bc .indexServers .revert (genesis .Header ())
1136
1149
// Dump the entire block chain and purge the caches
1137
1150
if err := bc .SetHead (0 ); err != nil {
1138
1151
return err
@@ -1149,6 +1162,7 @@ func (bc *BlockChain) ResetWithGenesisBlock(genesis *types.Block) error {
1149
1162
log .Crit ("Failed to write genesis block" , "err" , err )
1150
1163
}
1151
1164
bc .writeHeadBlock (genesis )
1165
+ bc .indexServers .broadcast (genesis .Header (), true )
1152
1166
1153
1167
// Last update all in-memory chain markers
1154
1168
bc .genesisBlock = genesis
@@ -1261,6 +1275,7 @@ func (bc *BlockChain) stopWithoutSaving() {
1261
1275
// Stop stops the blockchain service. If any imports are currently in progress
1262
1276
// it will abort them using the procInterrupt.
1263
1277
func (bc * BlockChain ) Stop () {
1278
+ bc .indexServers .stop ()
1264
1279
bc .stopWithoutSaving ()
1265
1280
1266
1281
// Ensure that the entirety of the state snapshot is journaled to disk.
@@ -1562,6 +1577,7 @@ func (bc *BlockChain) writeKnownBlock(block *types.Block) error {
1562
1577
}
1563
1578
}
1564
1579
bc .writeHeadBlock (block )
1580
+ bc .indexServers .broadcast (block .Header (), true )
1565
1581
return nil
1566
1582
}
1567
1583
@@ -1577,7 +1593,10 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
1577
1593
// should be written atomically. BlockBatch is used for containing all components.
1578
1594
blockBatch := bc .db .NewBatch ()
1579
1595
rawdb .WriteBlock (blockBatch , block )
1580
- rawdb .WriteReceipts (blockBatch , block .Hash (), block .NumberU64 (), receipts )
1596
+ blockHash := block .Hash ()
1597
+ bc .blockCache .Add (blockHash , block )
1598
+ rawdb .WriteReceipts (blockBatch , blockHash , block .NumberU64 (), receipts )
1599
+ bc .receiptsCache .Add (blockHash , receipts )
1581
1600
rawdb .WritePreimages (blockBatch , statedb .Preimages ())
1582
1601
if err := blockBatch .Write (); err != nil {
1583
1602
log .Crit ("Failed to write block into disk" , "err" , err )
@@ -1664,6 +1683,7 @@ func (bc *BlockChain) writeBlockAndSetHead(block *types.Block, receipts []*types
1664
1683
1665
1684
// Set new head.
1666
1685
bc .writeHeadBlock (block )
1686
+ bc .indexServers .broadcast (block .Header (), true )
1667
1687
1668
1688
bc .chainFeed .Send (ChainEvent {Header : block .Header ()})
1669
1689
if len (logs ) > 0 {
@@ -2373,6 +2393,7 @@ func (bc *BlockChain) reorg(oldHead *types.Header, newHead *types.Header) error
2373
2393
return errInvalidNewChain
2374
2394
}
2375
2395
}
2396
+ bc .indexServers .revert (commonBlock )
2376
2397
// Ensure the user sees large reorgs
2377
2398
if len (oldChain ) > 0 && len (newChain ) > 0 {
2378
2399
logFn := log .Info
@@ -2470,6 +2491,7 @@ func (bc *BlockChain) reorg(oldHead *types.Header, newHead *types.Header) error
2470
2491
}
2471
2492
// Update the head block
2472
2493
bc .writeHeadBlock (block )
2494
+ bc .indexServers .broadcast (block .Header (), false )
2473
2495
}
2474
2496
if len (rebirthLogs ) > 0 {
2475
2497
bc .logsFeed .Send (rebirthLogs )
@@ -2545,6 +2567,7 @@ func (bc *BlockChain) SetCanonical(head *types.Block) (common.Hash, error) {
2545
2567
}
2546
2568
}
2547
2569
bc .writeHeadBlock (head )
2570
+ bc .indexServers .broadcast (head .Header (), true )
2548
2571
2549
2572
// Emit events
2550
2573
logs := bc .collectLogs (head , false )
0 commit comments