From 2ac56a4f79a1764757b74a930f9f5ee711ef0f5f Mon Sep 17 00:00:00 2001 From: Ceyhun Onur Date: Mon, 25 Mar 2024 15:31:25 +0300 Subject: [PATCH] fix state sync tx indices --- core/blockchain.go | 18 +++++++++++++----- plugin/evm/syncervm_test.go | 11 +++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 6b06ecd35a..ce6917344e 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -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. @@ -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 } @@ -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()) diff --git a/plugin/evm/syncervm_test.go b/plugin/evm/syncervm_test.go index a498a2e951..4774f7a86a 100644 --- a/plugin/evm/syncervm_test.go +++ b/plugin/evm/syncervm_test.go @@ -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