From f4efd4b09d5da70b3925891fccb442d689438c63 Mon Sep 17 00:00:00 2001 From: jeevan-sid Date: Wed, 18 Feb 2026 13:42:33 +0530 Subject: [PATCH 1/5] feat: enable tx indexing for era files --- cmd/geth/chaincmd.go | 87 +++++++++++++++++++++++ cmd/utils/cmd.go | 118 ++++++++++++++++++++++++++++++++ core/rawdb/accessors_indexes.go | 41 ++++++++++- core/rawdb/schema.go | 8 +++ 4 files changed, 253 insertions(+), 1 deletion(-) diff --git a/cmd/geth/chaincmd.go b/cmd/geth/chaincmd.go index 1ccb78d622..7efbe60680 100644 --- a/cmd/geth/chaincmd.go +++ b/cmd/geth/chaincmd.go @@ -171,6 +171,19 @@ from Era archives. Description: ` The export-history command will export blocks and their corresponding receipts into Era archives. Eras are typically packaged in steps of 8192 blocks. +`, + } + importEraIndexCommand = &cli.Command{ + Action: importEraIndex, + Name: "import-era-index", + Usage: "Import transaction index from era archive files", + ArgsUsage: "", + Flags: slices.Concat(utils.DatabaseFlags, utils.NetworkFlags, []cli.Flag{utils.EraFormatFlag}), + Description: ` +The import-era-index command indexes transactions from era files to enable +transaction lookups by hash +for pruned block ranges. Era files must be present in the specified directory. +The command is idempotent and can be re-run to index newly added era files. `, } importPreimagesCommand = &cli.Command{ @@ -589,6 +602,80 @@ func exportHistory(ctx *cli.Context) error { return nil } +func importEraIndex(ctx *cli.Context) error { + if ctx.Args().Len() != 1 { + utils.Fatalf("usage: %s", ctx.Command.ArgsUsage) + } + + stack, _ := makeConfigNode(ctx) + defer stack.Close() + + db := utils.MakeChainDatabase(ctx, stack, false) + defer db.Close() + + var ( + start = time.Now() + dir = ctx.Args().Get(0) + network string + ) + + // Determine network. + if utils.IsNetworkPreset(ctx) { + switch { + case ctx.Bool(utils.MainnetFlag.Name): + network = "mainnet" + case ctx.Bool(utils.SepoliaFlag.Name): + network = "sepolia" + case ctx.Bool(utils.HoleskyFlag.Name): + network = "holesky" + case ctx.Bool(utils.HoodiFlag.Name): + network = "hoodi" + } + } else { + // No network flag set, try to determine network based on files + // present in directory. + var networks []string + for _, n := range params.NetworkNames { + entries, err := era.ReadDir(dir, n) + if err != nil { + return fmt.Errorf("error reading %s: %w", dir, err) + } + if len(entries) > 0 { + networks = append(networks, n) + } + } + if len(networks) == 0 { + return fmt.Errorf("no era files found in %s", dir) + } + if len(networks) > 1 { + return errors.New("multiple networks found, use a network flag to specify network") + } + network = networks[0] + } + + // Determine era format. + var ( + format = ctx.String(utils.EraFormatFlag.Name) + from func(era.ReadAtSeekCloser) (era.Era, error) + ) + switch format { + case "era1", "era": + from = onedb.From + case "erae": + from = execdb.From + default: + return fmt.Errorf("unknown --era.format %q (expected 'era1' or 'erae')", format) + } + + if err := utils.ImportEraIndex(db, dir, network, from); err != nil { + return err + } + + fmt.Printf("Era indexing done in %v\n", time.Since(start)) + return nil + +} + // importPreimages imports preimage data from the specified file. // it is deprecated, and the export function has been removed, but // the import function is kept around for the time being so that diff --git a/cmd/utils/cmd.go b/cmd/utils/cmd.go index 995724e6fc..febfc1e07d 100644 --- a/cmd/utils/cmd.go +++ b/cmd/utils/cmd.go @@ -345,6 +345,124 @@ func ImportHistory(chain *core.BlockChain, dir string, network string, from func return nil } +// ImportEraIndex indexes transactions from era files into the database to enable +// transaction lookups by hash for pruned block ranges. +func ImportEraIndex(db ethdb.Database, dir string, network string, from func(f era.ReadAtSeekCloser) (era.Era, error)) error { + entries, err := era.ReadDir(dir, network) + if err != nil { + return fmt.Errorf("error reading era directory: %w", err) + } + if len(entries) == 0 { + return fmt.Errorf("no era files found for network %s in %s", network, dir) + } + + // Get the last indexed epoch to support resume. + tail := rawdb.ReadEraIndexTail(db) + startEpoch := uint64(0) + if tail != nil { + startEpoch = *tail + 1 + log.Info("Resuming era indexing", "lastEpoch", *tail, "nextEpoch", startEpoch) + } + + var ( + start = time.Now() + reported = time.Now() + batch = db.NewBatch() + totalBlocks uint64 + totalTxs uint64 + ) + + // Index each era file. + for epoch, entry := range entries { + if uint64(epoch) < startEpoch { + continue + } + + err := func() error { + path := filepath.Join(dir, entry) + f, err := os.Open(path) + if err != nil { + return fmt.Errorf("error opening era file %s: %w", path, err) + } + defer f.Close() + + e, err := from(f) + if err != nil { + return fmt.Errorf("error opening era: %w", err) + } + + it, err := e.Iterator() + if err != nil { + return fmt.Errorf("error creating iterator: %w", err) + } + + epochBlocks := uint64(0) + epochTxs := uint64(0) + + // Iterate over all blocks in this epoch. + for it.Next() { + if it.Error() != nil { + return fmt.Errorf("error iterating era file: %w", it.Error()) + } + + block, err := it.Block() + if err != nil { + return fmt.Errorf("error reading block: %w", err) + } + + // Index all transactions in this block. + txHashes := make([]common.Hash, len(block.Transactions())) + for i, tx := range block.Transactions() { + txHashes[i] = tx.Hash() + } + + if len(txHashes) > 0 { + rawdb.WriteEraTxLookupEntries(batch, block.NumberU64(), txHashes) + epochTxs += uint64(len(txHashes)) + } + + epochBlocks++ + totalBlocks++ + + // Write batch if it's getting large. + if batch.ValueSize() >= ethdb.IdealBatchSize { + if err := batch.Write(); err != nil { + return fmt.Errorf("error writing index batch: %w", err) + } + batch.Reset() + } + } + + // Flush remaining batch for this epoch. + if batch.ValueSize() > 0 { + if err := batch.Write(); err != nil { + return fmt.Errorf("error writing index batch: %w", err) + } + batch.Reset() + } + + // Mark this epoch as fully indexed. + rawdb.WriteEraIndexTail(db, uint64(epoch)) + + totalTxs += epochTxs + + if time.Since(reported) >= 8*time.Second { + log.Info("Indexing era files", "epoch", epoch, "blocks", epochBlocks, "txs", epochTxs, + "totalBlocks", totalBlocks, "totalTxs", totalTxs, "elapsed", common.PrettyDuration(time.Since(start))) + reported = time.Now() + } + + return nil + }() + if err != nil { + return err + } + } + + log.Info("Era indexing complete", "totalBlocks", totalBlocks, "totalTxs", totalTxs, "elapsed", common.PrettyDuration(time.Since(start))) + return nil +} + func missingBlocks(chain *core.BlockChain, blocks []*types.Block) []*types.Block { head := chain.CurrentBlock() for i, block := range blocks { diff --git a/core/rawdb/accessors_indexes.go b/core/rawdb/accessors_indexes.go index 8c8c3ec9bb..e8dcbad741 100644 --- a/core/rawdb/accessors_indexes.go +++ b/core/rawdb/accessors_indexes.go @@ -61,7 +61,8 @@ func DecodeTxLookupEntry(data []byte, db ethdb.Reader) *uint64 { func ReadTxLookupEntry(db ethdb.Reader, hash common.Hash) *uint64 { data, _ := db.Get(txLookupKey(hash)) if len(data) == 0 { - return nil + // Fallback: check era-derived index + return ReadEraTxLookupEntry(db, hash) } return DecodeTxLookupEntry(data, db) } @@ -134,6 +135,44 @@ func DeleteAllTxLookupEntries(db ethdb.KeyValueStore, condition func(common.Hash } } +// ReadEraTxLookupEntry retrieves the positional metadata associated with a transaction +// hash from the era1-derived index. +func ReadEraTxLookupEntry(db ethdb.Reader, hash common.Hash) *uint64 { + data, _ := db.Get(eraTxLookupKey(hash)) + if len(data) == 0 { + return nil + } + return DecodeTxLookupEntry(data, db) +} + +// WriteEraTxLookupEntries stores positional metadata for transactions from era1 files, +// enabling hash based transaction and receipt lookups for pruned history. +func WriteEraTxLookupEntries(db ethdb.KeyValueWriter, number uint64, hashes []common.Hash) { + numberBytes := new(big.Int).SetUint64(number).Bytes() + for _, hash := range hashes { + if err := db.Put(eraTxLookupKey(hash), numberBytes); err != nil { + log.Crit("Failed to store era1 transaction lookup entry", "err", err) + } + } +} + +// ReadEraIndexTail retrieves the last fully indexed era1 epoch. +func ReadEraIndexTail(db ethdb.Reader) *uint64 { + data, _ := db.Get(eraIndexTailKey) + if len(data) == 0 { + return nil + } + epoch := binary.BigEndian.Uint64(data) + return &epoch +} + +// WriteEraIndexTail stores the last fully indexed era1 epoch. +func WriteEraIndexTail(db ethdb.KeyValueWriter, epoch uint64) { + if err := db.Put(eraIndexTailKey, encodeBlockNumber(epoch)); err != nil { + log.Crit("Failed to store era1 index tail", "err", err) + } +} + // findTxInBlockBody traverses the given RLP-encoded block body, searching for // the transaction specified by its hash. func findTxInBlockBody(blockbody rlp.RawValue, target common.Hash) (*types.Transaction, uint64, error) { diff --git a/core/rawdb/schema.go b/core/rawdb/schema.go index d9140c5fd6..db402ff751 100644 --- a/core/rawdb/schema.go +++ b/core/rawdb/schema.go @@ -120,6 +120,9 @@ var ( CodePrefix = []byte("c") // CodePrefix + code hash -> account code skeletonHeaderPrefix = []byte("S") // skeletonHeaderPrefix + num (uint64 big endian) -> header + eraTxLookupPrefix = []byte("e") // eraTxLookupPrefix + hash -> transaction/receipt lookup metadata + eraIndexTailKey = []byte("eraIndexTail") // eraIndexTailKey -> last fully indexed epoch + // Path-based storage scheme of merkle patricia trie. TrieNodeAccountPrefix = []byte("A") // TrieNodeAccountPrefix + hexPath -> trie node TrieNodeStoragePrefix = []byte("O") // TrieNodeStoragePrefix + accountHash + hexPath -> trie node @@ -219,6 +222,11 @@ func txLookupKey(hash common.Hash) []byte { return append(txLookupPrefix, hash.Bytes()...) } +// eraTxLookupKey = eraTxLookupPrefix + hash +func eraTxLookupKey(hash common.Hash) []byte { + return append(eraTxLookupPrefix, hash.Bytes()...) +} + // accountSnapshotKey = SnapshotAccountPrefix + hash func accountSnapshotKey(hash common.Hash) []byte { return append(SnapshotAccountPrefix, hash.Bytes()...) From e8a656cc95528401994e4fa7473f1cdeb9aa9d0e Mon Sep 17 00:00:00 2001 From: jeevan-sid Date: Wed, 18 Feb 2026 13:45:32 +0530 Subject: [PATCH 2/5] add era import in geth --- cmd/geth/chaincmd.go | 4 +--- cmd/geth/main.go | 1 + 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/cmd/geth/chaincmd.go b/cmd/geth/chaincmd.go index 7efbe60680..65399bdacc 100644 --- a/cmd/geth/chaincmd.go +++ b/cmd/geth/chaincmd.go @@ -181,8 +181,7 @@ into Era archives. Eras are typically packaged in steps of 8192 blocks. Flags: slices.Concat(utils.DatabaseFlags, utils.NetworkFlags, []cli.Flag{utils.EraFormatFlag}), Description: ` The import-era-index command indexes transactions from era files to enable -transaction lookups by hash -for pruned block ranges. Era files must be present in the specified directory. +transaction lookups by hash for pruned block ranges. Era files must be present in the specified directory. The command is idempotent and can be re-run to index newly added era files. `, } @@ -673,7 +672,6 @@ func importEraIndex(ctx *cli.Context) error { fmt.Printf("Era indexing done in %v\n", time.Since(start)) return nil - } // importPreimages imports preimage data from the specified file. diff --git a/cmd/geth/main.go b/cmd/geth/main.go index 2291e0aafa..26642f9583 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -236,6 +236,7 @@ func init() { exportCommand, importHistoryCommand, exportHistoryCommand, + importEraIndexCommand, importPreimagesCommand, removedbCommand, dumpCommand, From f13740a14670515cd38ee5d6f535d3bef1e1e026 Mon Sep 17 00:00:00 2001 From: jeevan-sid Date: Wed, 18 Feb 2026 16:07:13 +0530 Subject: [PATCH 3/5] feat: add interrupts --- cmd/utils/cmd.go | 57 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 9 deletions(-) diff --git a/cmd/utils/cmd.go b/cmd/utils/cmd.go index febfc1e07d..3bc20452b3 100644 --- a/cmd/utils/cmd.go +++ b/cmd/utils/cmd.go @@ -364,12 +364,17 @@ func ImportEraIndex(db ethdb.Database, dir string, network string, from func(f e log.Info("Resuming era indexing", "lastEpoch", *tail, "nextEpoch", startEpoch) } + interrupt := make(chan os.Signal, 1) + signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM) + defer signal.Stop(interrupt) + var ( start = time.Now() reported = time.Now() batch = db.NewBatch() totalBlocks uint64 totalTxs uint64 + interrupted = false ) // Index each era file. @@ -378,6 +383,18 @@ func ImportEraIndex(db ethdb.Database, dir string, network string, from func(f e continue } + select { + case <-interrupt: + log.Warn("Interrupted, flushing and shutting down gracefully...") + interrupted = true + break + default: + } + + if interrupted { + break + } + err := func() error { path := filepath.Join(dir, entry) f, err := os.Open(path) @@ -401,6 +418,14 @@ func ImportEraIndex(db ethdb.Database, dir string, network string, from func(f e // Iterate over all blocks in this epoch. for it.Next() { + select { + case <-interrupt: + log.Warn("Interrupted during epoch processing, flushing current progress...") + interrupted = true + return nil + default: + } + if it.Error() != nil { return fmt.Errorf("error iterating era file: %w", it.Error()) } @@ -441,15 +466,22 @@ func ImportEraIndex(db ethdb.Database, dir string, network string, from func(f e batch.Reset() } - // Mark this epoch as fully indexed. - rawdb.WriteEraIndexTail(db, uint64(epoch)) + // Only mark epoch as complete if we weren't interrupted mid-epoch + if !interrupted { + // Mark this epoch as fully indexed. + rawdb.WriteEraIndexTail(batch, uint64(epoch)) + if err := batch.Write(); err != nil { + return fmt.Errorf("error writing tail marker: %w", err) + } + batch.Reset() - totalTxs += epochTxs + totalTxs += epochTxs - if time.Since(reported) >= 8*time.Second { - log.Info("Indexing era files", "epoch", epoch, "blocks", epochBlocks, "txs", epochTxs, - "totalBlocks", totalBlocks, "totalTxs", totalTxs, "elapsed", common.PrettyDuration(time.Since(start))) - reported = time.Now() + if time.Since(reported) >= 8*time.Second { + log.Info("Indexing era files", "epoch", epoch, "blocks", epochBlocks, "txs", epochTxs, + "totalBlocks", totalBlocks, "totalTxs", totalTxs, "elapsed", common.PrettyDuration(time.Since(start))) + reported = time.Now() + } } return nil @@ -457,12 +489,19 @@ func ImportEraIndex(db ethdb.Database, dir string, network string, from func(f e if err != nil { return err } + + if interrupted { + break + } } - log.Info("Era indexing complete", "totalBlocks", totalBlocks, "totalTxs", totalTxs, "elapsed", common.PrettyDuration(time.Since(start))) + if interrupted { + log.Info("Era indexing interrupted", "totalBlocks", totalBlocks, "totalTxs", totalTxs, "elapsed", common.PrettyDuration(time.Since(start))) + } else { + log.Info("Era indexing complete", "totalBlocks", totalBlocks, "totalTxs", totalTxs, "elapsed", common.PrettyDuration(time.Since(start))) + } return nil } - func missingBlocks(chain *core.BlockChain, blocks []*types.Block) []*types.Block { head := chain.CurrentBlock() for i, block := range blocks { From 41d6664b0c9142e267c0063eb27db2d4f7aa843b Mon Sep 17 00:00:00 2001 From: 0xjvn Date: Mon, 13 Apr 2026 14:39:57 +0530 Subject: [PATCH 4/5] feat: enable txn queries on pruned node --- cmd/geth/chaincmd.go | 87 +----------------- cmd/geth/main.go | 1 - cmd/utils/cmd.go | 186 ++++++-------------------------------- cmd/utils/history_test.go | 2 +- 4 files changed, 32 insertions(+), 244 deletions(-) diff --git a/cmd/geth/chaincmd.go b/cmd/geth/chaincmd.go index 2e3c772fbc..9dcd2caee3 100644 --- a/cmd/geth/chaincmd.go +++ b/cmd/geth/chaincmd.go @@ -172,18 +172,6 @@ from Era archives. Description: ` The export-history command will export blocks and their corresponding receipts into Era archives. Eras are typically packaged in steps of 8192 blocks. -`, - } - importEraIndexCommand = &cli.Command{ - Action: importEraIndex, - Name: "import-era-index", - Usage: "Import transaction index from era archive files", - ArgsUsage: "", - Flags: slices.Concat(utils.DatabaseFlags, utils.NetworkFlags, []cli.Flag{utils.EraFormatFlag}), - Description: ` -The import-era-index command indexes transactions from era files to enable -transaction lookups by hash for pruned block ranges. Era files must be present in the specified directory. -The command is idempotent and can be re-run to index newly added era files. `, } importPreimagesCommand = &cli.Command{ @@ -550,7 +538,7 @@ func importHistory(ctx *cli.Context) error { default: return fmt.Errorf("unknown --era.format %q (expected 'era1' or 'erae')", format) } - if err := utils.ImportHistory(chain, dir, network, from); err != nil { + if err := utils.ImportHistory(chain, db, dir, network, from); err != nil { return err } @@ -608,79 +596,6 @@ func exportHistory(ctx *cli.Context) error { return nil } -func importEraIndex(ctx *cli.Context) error { - if ctx.Args().Len() != 1 { - utils.Fatalf("usage: %s", ctx.Command.ArgsUsage) - } - - stack, _ := makeConfigNode(ctx) - defer stack.Close() - - db := utils.MakeChainDatabase(ctx, stack, false) - defer db.Close() - - var ( - start = time.Now() - dir = ctx.Args().Get(0) - network string - ) - - // Determine network. - if utils.IsNetworkPreset(ctx) { - switch { - case ctx.Bool(utils.MainnetFlag.Name): - network = "mainnet" - case ctx.Bool(utils.SepoliaFlag.Name): - network = "sepolia" - case ctx.Bool(utils.HoleskyFlag.Name): - network = "holesky" - case ctx.Bool(utils.HoodiFlag.Name): - network = "hoodi" - } - } else { - // No network flag set, try to determine network based on files - // present in directory. - var networks []string - for _, n := range params.NetworkNames { - entries, err := era.ReadDir(dir, n) - if err != nil { - return fmt.Errorf("error reading %s: %w", dir, err) - } - if len(entries) > 0 { - networks = append(networks, n) - } - } - if len(networks) == 0 { - return fmt.Errorf("no era files found in %s", dir) - } - if len(networks) > 1 { - return errors.New("multiple networks found, use a network flag to specify network") - } - network = networks[0] - } - - // Determine era format. - var ( - format = ctx.String(utils.EraFormatFlag.Name) - from func(era.ReadAtSeekCloser) (era.Era, error) - ) - switch format { - case "era1", "era": - from = onedb.From - case "erae": - from = execdb.From - default: - return fmt.Errorf("unknown --era.format %q (expected 'era1' or 'erae')", format) - } - - if err := utils.ImportEraIndex(db, dir, network, from); err != nil { - return err - } - - fmt.Printf("Era indexing done in %v\n", time.Since(start)) - return nil -} - // importPreimages imports preimage data from the specified file. // it is deprecated, and the export function has been removed, but // the import function is kept around for the time being so that diff --git a/cmd/geth/main.go b/cmd/geth/main.go index af85aedaf6..e196ac8688 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -236,7 +236,6 @@ func init() { exportCommand, importHistoryCommand, exportHistoryCommand, - importEraIndexCommand, importPreimagesCommand, removedbCommand, dumpCommand, diff --git a/cmd/utils/cmd.go b/cmd/utils/cmd.go index dd41efcb9a..a6d4b64ef5 100644 --- a/cmd/utils/cmd.go +++ b/cmd/utils/cmd.go @@ -252,7 +252,7 @@ func readList(filename string) ([]string, error) { // ImportHistory imports Era1 files containing historical block information, // starting from genesis. The assumption is held that the provided chain // segment in Era1 file should all be canonical and verified. -func ImportHistory(chain *core.BlockChain, dir string, network string, from func(f era.ReadAtSeekCloser) (era.Era, error)) error { +func ImportHistory(chain *core.BlockChain, db ethdb.Database, dir string, network string, from func(f era.ReadAtSeekCloser) (era.Era, error)) error { if chain.CurrentSnapBlock().Number.BitLen() != 0 { return errors.New("history import only supported when starting from genesis") } @@ -275,6 +275,7 @@ func ImportHistory(chain *core.BlockChain, dir string, network string, from func imported = 0 h = sha256.New() buf = bytes.NewBuffer(nil) + idxBatch = db.NewBatch() ) for i, file := range entries { @@ -297,6 +298,7 @@ func ImportHistory(chain *core.BlockChain, dir string, network string, from func if got != checksums[i] { return fmt.Errorf("%s checksum mismatch: have %s want %s", file, got, checksums[i]) } + // Import all block data from Era1. e, err := from(f) if err != nil { @@ -321,6 +323,24 @@ func ImportHistory(chain *core.BlockChain, dir string, network string, from func return fmt.Errorf("error inserting blocks %d-%d: %w", blocks[0].NumberU64(), blocks[len(blocks)-1].NumberU64(), err) } + + // Index tx lookups for the same batch we just inserted. + for _, block := range blocks { + txHashes := make([]common.Hash, len(block.Transactions())) + for j, tx := range block.Transactions() { + txHashes[j] = tx.Hash() + } + if len(txHashes) > 0 { + rawdb.WriteEraTxLookupEntries(idxBatch, block.NumberU64(), txHashes) + } + } + if idxBatch.ValueSize() >= ethdb.IdealBatchSize { + if err := idxBatch.Write(); err != nil { + return fmt.Errorf("error writing tx index batch: %w", err) + } + idxBatch.Reset() + } + imported += len(blocks) if time.Since(reported) >= 8*time.Second { head := blocks[len(blocks)-1].NumberU64() @@ -334,6 +354,7 @@ func ImportHistory(chain *core.BlockChain, dir string, network string, from func return nil } ) + for it.Next() { block, err := it.Block() if err != nil { @@ -357,172 +378,25 @@ func ImportHistory(chain *core.BlockChain, dir string, network string, from func if err := it.Error(); err != nil { return err } - return flush() - }() - if err != nil { - return err - } - } - return nil -} - -// ImportEraIndex indexes transactions from era files into the database to enable -// transaction lookups by hash for pruned block ranges. -func ImportEraIndex(db ethdb.Database, dir string, network string, from func(f era.ReadAtSeekCloser) (era.Era, error)) error { - entries, err := era.ReadDir(dir, network) - if err != nil { - return fmt.Errorf("error reading era directory: %w", err) - } - if len(entries) == 0 { - return fmt.Errorf("no era files found for network %s in %s", network, dir) - } - - // Get the last indexed epoch to support resume. - tail := rawdb.ReadEraIndexTail(db) - startEpoch := uint64(0) - if tail != nil { - startEpoch = *tail + 1 - log.Info("Resuming era indexing", "lastEpoch", *tail, "nextEpoch", startEpoch) - } - - interrupt := make(chan os.Signal, 1) - signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM) - defer signal.Stop(interrupt) - - var ( - start = time.Now() - reported = time.Now() - batch = db.NewBatch() - totalBlocks uint64 - totalTxs uint64 - interrupted = false - ) - - // Index each era file. - for epoch, entry := range entries { - if uint64(epoch) < startEpoch { - continue - } - - select { - case <-interrupt: - log.Warn("Interrupted, flushing and shutting down gracefully...") - interrupted = true - break - default: - } - - if interrupted { - break - } - - err := func() error { - path := filepath.Join(dir, entry) - f, err := os.Open(path) - if err != nil { - return fmt.Errorf("error opening era file %s: %w", path, err) - } - defer f.Close() - - e, err := from(f) - if err != nil { - return fmt.Errorf("error opening era: %w", err) + if err := flush(); err != nil { + return err } - it, err := e.Iterator() - if err != nil { - return fmt.Errorf("error creating iterator: %w", err) + // Flush any remaining index writes and mark this epoch fully indexed. + rawdb.WriteEraIndexTail(idxBatch, uint64(i)) + if err := idxBatch.Write(); err != nil { + return fmt.Errorf("error writing era index tail for epoch %d: %w", i, err) } - - epochBlocks := uint64(0) - epochTxs := uint64(0) - - // Iterate over all blocks in this epoch. - for it.Next() { - select { - case <-interrupt: - log.Warn("Interrupted during epoch processing, flushing current progress...") - interrupted = true - return nil - default: - } - - if it.Error() != nil { - return fmt.Errorf("error iterating era file: %w", it.Error()) - } - - block, err := it.Block() - if err != nil { - return fmt.Errorf("error reading block: %w", err) - } - - // Index all transactions in this block. - txHashes := make([]common.Hash, len(block.Transactions())) - for i, tx := range block.Transactions() { - txHashes[i] = tx.Hash() - } - - if len(txHashes) > 0 { - rawdb.WriteEraTxLookupEntries(batch, block.NumberU64(), txHashes) - epochTxs += uint64(len(txHashes)) - } - - epochBlocks++ - totalBlocks++ - - // Write batch if it's getting large. - if batch.ValueSize() >= ethdb.IdealBatchSize { - if err := batch.Write(); err != nil { - return fmt.Errorf("error writing index batch: %w", err) - } - batch.Reset() - } - } - - // Flush remaining batch for this epoch. - if batch.ValueSize() > 0 { - if err := batch.Write(); err != nil { - return fmt.Errorf("error writing index batch: %w", err) - } - batch.Reset() - } - - // Only mark epoch as complete if we weren't interrupted mid-epoch - if !interrupted { - // Mark this epoch as fully indexed. - rawdb.WriteEraIndexTail(batch, uint64(epoch)) - if err := batch.Write(); err != nil { - return fmt.Errorf("error writing tail marker: %w", err) - } - batch.Reset() - - totalTxs += epochTxs - - if time.Since(reported) >= 8*time.Second { - log.Info("Indexing era files", "epoch", epoch, "blocks", epochBlocks, "txs", epochTxs, - "totalBlocks", totalBlocks, "totalTxs", totalTxs, "elapsed", common.PrettyDuration(time.Since(start))) - reported = time.Now() - } - } - + idxBatch.Reset() return nil }() if err != nil { return err } - - if interrupted { - break - } - } - - if interrupted { - log.Info("Era indexing interrupted", "totalBlocks", totalBlocks, "totalTxs", totalTxs, "elapsed", common.PrettyDuration(time.Since(start))) - } else { - log.Info("Era indexing complete", "totalBlocks", totalBlocks, "totalTxs", totalTxs, "elapsed", common.PrettyDuration(time.Since(start))) } return nil } + func missingBlocks(chain *core.BlockChain, blocks []*types.Block) []*types.Block { head := chain.CurrentBlock() for i, block := range blocks { diff --git a/cmd/utils/history_test.go b/cmd/utils/history_test.go index 6631946129..4b94117587 100644 --- a/cmd/utils/history_test.go +++ b/cmd/utils/history_test.go @@ -182,7 +182,7 @@ func TestHistoryImportAndExport(t *testing.T) { if err != nil { t.Fatalf("unable to initialize chain: %v", err) } - if err := ImportHistory(imported, dir, "mainnet", tt.from); err != nil { + if err := ImportHistory(imported, db2, dir, "mainnet", tt.from); err != nil { t.Fatalf("failed to import chain: %v", err) } if have, want := imported.CurrentHeader(), chain.CurrentHeader(); have.Hash() != want.Hash() { From 9e80c3d7c58e5bf1bd07ed6ba5d5fc56dc71f176 Mon Sep 17 00:00:00 2001 From: jeevan-sid Date: Mon, 13 Apr 2026 15:36:23 +0530 Subject: [PATCH 5/5] feat: lint --- cmd/geth/chaincmd.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd/geth/chaincmd.go b/cmd/geth/chaincmd.go index 9dcd2caee3..030c45a2f6 100644 --- a/cmd/geth/chaincmd.go +++ b/cmd/geth/chaincmd.go @@ -541,7 +541,6 @@ func importHistory(ctx *cli.Context) error { if err := utils.ImportHistory(chain, db, dir, network, from); err != nil { return err } - fmt.Printf("Import done in %v\n", time.Since(start)) return nil }