From 216409ed060c071ae75c53a169e2a40b168684c4 Mon Sep 17 00:00:00 2001 From: shantichanal <158101918+shantichanal@users.noreply.github.com> Date: Tue, 29 Jul 2025 13:12:31 +0200 Subject: [PATCH] fixed td and accumulator behaviour --- cmd/utils/flags.go | 11 ++++++----- internal/era/execdb/builder.go | 29 +++++++++++++++++++---------- internal/era/execdb/era_test.go | 2 +- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 3dd619e80a..b10f6ebd52 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -114,11 +114,6 @@ var ( Usage: "Root directory for era1 history (default = inside ancient/chain)", Category: flags.EthCategory, } - EraFormatFlag = &cli.StringFlag{ - Name: "eraformat", - Usage: "Archive format: 'era1' or 'erae'", - Category: flags.EthCategory, - } MinFreeDiskSpaceFlag = &cli.IntFlag{ Name: "datadir.minfreedisk", Usage: "Minimum free disk space in MB, once reached triggers auto shut down (default = --cache.gc converted to MB, 0 = disabled)", @@ -1041,6 +1036,12 @@ Please note that --` + MetricsHTTPFlag.Name + ` must be set to start the server. Value: metrics.DefaultConfig.InfluxDBOrganization, Category: flags.MetricsCategory, } + + // Era flags are a group of flags related to the era archive format. + EraFormatFlag = &cli.StringFlag{ + Name: "era.format", + Usage: "Archive format: 'era1' or 'erae'", + } ) var ( diff --git a/internal/era/execdb/builder.go b/internal/era/execdb/builder.go index 101e09cd56..047986a17c 100644 --- a/internal/era/execdb/builder.go +++ b/internal/era/execdb/builder.go @@ -51,11 +51,9 @@ import ( "math/big" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/history" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/internal/era" "github.com/ethereum/go-ethereum/internal/era/e2store" - "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rlp" "github.com/golang/snappy" ) @@ -90,6 +88,9 @@ type Builder struct { startNum *uint64 written uint64 expectsProofs bool + isPreMerge bool + numPreMerge int // number of pre-merge blocks + finalTD *big.Int // final total difficulty, used for pre-merge and merge straddling files } // NewBuilder returns a new Builder instance. @@ -151,19 +152,27 @@ func (b *Builder) AddRLP(headerRLP []byte, bodyRLP []byte, receipts []byte, proo if len(b.buff.headers) >= era.MaxSize { return fmt.Errorf("exceeds max size %d", era.MaxSize) } + if len(b.buff.headers) == 0 && td != nil { + if td.Sign() > 0 { + b.isPreMerge = true + } + } b.buff.headers = append(b.buff.headers, headerRLP) b.buff.bodies = append(b.buff.bodies, bodyRLP) b.buff.receipts = append(b.buff.receipts, receipts) - b.buff.tds = append(b.buff.tds, new(big.Int).Set(td)) - b.hashes = append(b.hashes, blockHash) - if proof != nil { - b.buff.proofs = append(b.buff.proofs, proof) + if td != nil { + if b.isPreMerge && td.Sign() > 0 { + b.buff.tds = append(b.buff.tds, new(big.Int).Set(td)) + b.finalTD = new(big.Int).Set(td) + b.hashes = append(b.hashes, blockHash) + } else if b.isPreMerge { + b.buff.tds = append(b.buff.tds, new(big.Int).Set(b.finalTD)) + } } - mergeblock := history.PrunePoints[params.MainnetGenesisHash] - if mergeblock != nil && blockNum <= mergeblock.BlockNumber { - b.buff.tds = append(b.buff.tds, new(big.Int).Set(td)) + if proof != nil { + b.buff.proofs = append(b.buff.proofs, proof) } // Write Era2 version before writing any blocks. @@ -233,7 +242,7 @@ func (b *Builder) Finalize() (common.Hash, error) { var accRoot common.Hash if len(b.hashes) > 0 { var err error - accRoot, err = era.ComputeAccumulator(b.hashes, b.buff.tds) + accRoot, err = era.ComputeAccumulator(b.hashes, b.buff.tds[:len(b.hashes)]) if err != nil { return common.Hash{}, fmt.Errorf("compute accumulator: %w", err) } diff --git a/internal/era/execdb/era_test.go b/internal/era/execdb/era_test.go index f9ccc35254..a841820e15 100644 --- a/internal/era/execdb/era_test.go +++ b/internal/era/execdb/era_test.go @@ -54,7 +54,7 @@ func TestEra2Builder(t *testing.T) { chain.headers = append(chain.headers, types.Header{Number: big.NewInt(int64(i))}) chain.bodies = append(chain.bodies, types.Body{Transactions: []*types.Transaction{types.NewTransaction(0, common.Address{byte(i)}, nil, 0, nil, nil)}}) chain.receipts = append(chain.receipts, types.Receipts{{CumulativeGasUsed: uint64(i)}}) - chain.tds = append(chain.tds, big.NewInt(int64(i))) + chain.tds = append(chain.tds, big.NewInt(int64(i+1))) } // Write blocks to Era1.