Skip to content

Commit

Permalink
fix state sync tx indices
Browse files Browse the repository at this point in the history
  • Loading branch information
ceyonur committed Mar 25, 2024
1 parent 2e2beea commit 2ac56a4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
18 changes: 13 additions & 5 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,6 @@ func (bc *BlockChain) dispatchTxUnindexer() {
defer sub.Unsubscribe()
log.Info("Initialized transaction unindexer", "limit", txLookupLimit)

// XXX
// Launch the initial processing if chain is not empty. This step is
// useful in these scenarios that chain has no progress and indexer
// is never triggered.
Expand Down Expand Up @@ -545,15 +544,22 @@ func (bc *BlockChain) dispatchTxUnindexer() {
// - updating the acceptor tip index
func (bc *BlockChain) writeBlockAcceptedIndices(b *types.Block) error {
batch := bc.db.NewBatch()
if err := bc.batchBlockAcceptedIndices(batch, b); err != nil {
return err
}
if err := batch.Write(); err != nil {
return fmt.Errorf("%w: failed to write accepted indices entries batch", err)
}
return nil
}

func (bc *BlockChain) batchBlockAcceptedIndices(batch ethdb.Batch, b *types.Block) error {
if !bc.cacheConfig.SkipTxIndexing {
rawdb.WriteTxLookupEntriesByBlock(batch, b)
}
if err := rawdb.WriteAcceptorTip(batch, b.Hash()); err != nil {
return fmt.Errorf("%w: failed to write acceptor tip key", err)
}
if err := batch.Write(); err != nil {
return fmt.Errorf("%w: failed to write tx lookup entries batch", err)
}
return nil
}

Expand Down Expand Up @@ -2174,7 +2180,9 @@ func (bc *BlockChain) ResetToStateSyncedBlock(block *types.Block) error {

// Update head block and snapshot pointers on disk
batch := bc.db.NewBatch()
rawdb.WriteAcceptorTip(batch, block.Hash())
if err := bc.batchBlockAcceptedIndices(batch, block); err != nil {
return err
}
rawdb.WriteHeadBlockHash(batch, block.Hash())
rawdb.WriteHeadHeaderHash(batch, block.Hash())
rawdb.WriteSnapshotBlockHash(batch, block.Hash())
Expand Down
11 changes: 11 additions & 0 deletions plugin/evm/syncervm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,17 @@ func testSyncerVM(t *testing.T, vmSetup *syncVMSetup, test syncTest) {
require.True(syncerVM.blockChain.HasState(syncerVM.blockChain.LastAcceptedBlock().Root()), "unavailable state for last accepted block")
assertSyncPerformedHeights(t, syncerVM.chaindb, map[uint64]struct{}{retrievedSummary.Height(): {}})

lastNumber := syncerVM.blockChain.LastAcceptedBlock().NumberU64()
// check the last block is indexed
block := rawdb.ReadBlock(syncerVM.chaindb, rawdb.ReadCanonicalHash(syncerVM.chaindb, lastNumber), lastNumber)
if block.Transactions().Len() == 0 {
return
}
for _, tx := range block.Transactions() {
index := rawdb.ReadTxLookupEntry(syncerVM.chaindb, tx.Hash())
require.NotNilf(index, "Miss transaction indices, number %d hash %s", lastNumber, tx.Hash().Hex())
}

blocksToBuild := 10
txsPerBlock := 10
toAddress := testEthAddrs[1] // arbitrary choice
Expand Down

0 comments on commit 2ac56a4

Please sign in to comment.