Skip to content

Commit 4e17d2c

Browse files
committed
fixed td and accumulator behaviour
1 parent acd77a9 commit 4e17d2c

3 files changed

Lines changed: 27 additions & 16 deletions

File tree

cmd/utils/flags.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,7 @@ var (
116116
Usage: "Root directory for era1 history (default = inside ancient/chain)",
117117
Category: flags.EthCategory,
118118
}
119-
EraFormatFlag = &cli.StringFlag{
120-
Name: "eraformat",
121-
Usage: "Archive format: 'era1' or 'erae'",
122-
Category: flags.EthCategory,
123-
}
119+
124120
MinFreeDiskSpaceFlag = &flags.DirectoryFlag{
125121
Name: "datadir.minfreedisk",
126122
Usage: "Minimum free disk space in MB, once reached triggers auto shut down (default = --cache.gc converted to MB, 0 = disabled)",
@@ -971,6 +967,12 @@ Please note that --` + MetricsHTTPFlag.Name + ` must be set to start the server.
971967
Value: metrics.DefaultConfig.InfluxDBOrganization,
972968
Category: flags.MetricsCategory,
973969
}
970+
971+
// Era flags are a group of flags related to the era archive format.
972+
EraFormatFlag = &cli.StringFlag{
973+
Name: "era.format",
974+
Usage: "Archive format: 'era1' or 'erae'",
975+
}
974976
)
975977

976978
var (

internal/era/execdb/builder.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,9 @@ import (
5151
"math/big"
5252

5353
"github.com/ethereum/go-ethereum/common"
54-
"github.com/ethereum/go-ethereum/core/history"
5554
"github.com/ethereum/go-ethereum/core/types"
5655
"github.com/ethereum/go-ethereum/internal/era"
5756
"github.com/ethereum/go-ethereum/internal/era/e2store"
58-
"github.com/ethereum/go-ethereum/params"
5957
"github.com/ethereum/go-ethereum/rlp"
6058
"github.com/golang/snappy"
6159
)
@@ -90,6 +88,9 @@ type Builder struct {
9088
startNum *uint64
9189
written uint64
9290
expectsProofs bool
91+
isPreMerge bool
92+
numPreMerge int // number of pre-merge blocks
93+
finalTD *big.Int // final total difficulty, used for pre-merge and merge straddling files
9394
}
9495

9596
// NewBuilder returns a new Builder instance.
@@ -151,19 +152,27 @@ func (b *Builder) AddRLP(headerRLP []byte, bodyRLP []byte, receipts []byte, proo
151152
if len(b.buff.headers) >= era.MaxSize {
152153
return fmt.Errorf("exceeds max size %d", era.MaxSize)
153154
}
155+
if len(b.buff.headers) == 0 && td != nil {
156+
if td.Sign() > 0 {
157+
b.isPreMerge = true
158+
}
159+
}
154160

155161
b.buff.headers = append(b.buff.headers, headerRLP)
156162
b.buff.bodies = append(b.buff.bodies, bodyRLP)
157163
b.buff.receipts = append(b.buff.receipts, receipts)
158-
b.buff.tds = append(b.buff.tds, new(big.Int).Set(td))
159-
b.hashes = append(b.hashes, blockHash)
160-
if proof != nil {
161-
b.buff.proofs = append(b.buff.proofs, proof)
164+
if td != nil {
165+
if b.isPreMerge && td.Sign() > 0 {
166+
b.buff.tds = append(b.buff.tds, new(big.Int).Set(td))
167+
b.finalTD = new(big.Int).Set(td)
168+
b.hashes = append(b.hashes, blockHash)
169+
} else if b.isPreMerge {
170+
b.buff.tds = append(b.buff.tds, new(big.Int).Set(b.finalTD))
171+
}
162172
}
163173

164-
mergeblock := history.PrunePoints[params.MainnetGenesisHash]
165-
if mergeblock != nil && blockNum <= mergeblock.BlockNumber {
166-
b.buff.tds = append(b.buff.tds, new(big.Int).Set(td))
174+
if proof != nil {
175+
b.buff.proofs = append(b.buff.proofs, proof)
167176
}
168177

169178
// Write Era2 version before writing any blocks.
@@ -233,7 +242,7 @@ func (b *Builder) Finalize() (common.Hash, error) {
233242
var accRoot common.Hash
234243
if len(b.hashes) > 0 {
235244
var err error
236-
accRoot, err = era.ComputeAccumulator(b.hashes, b.buff.tds)
245+
accRoot, err = era.ComputeAccumulator(b.hashes, b.buff.tds[:len(b.hashes)])
237246
if err != nil {
238247
return common.Hash{}, fmt.Errorf("compute accumulator: %w", err)
239248
}

internal/era/execdb/era_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func TestEra2Builder(t *testing.T) {
5454
chain.headers = append(chain.headers, types.Header{Number: big.NewInt(int64(i))})
5555
chain.bodies = append(chain.bodies, types.Body{Transactions: []*types.Transaction{types.NewTransaction(0, common.Address{byte(i)}, nil, 0, nil, nil)}})
5656
chain.receipts = append(chain.receipts, types.Receipts{{CumulativeGasUsed: uint64(i)}})
57-
chain.tds = append(chain.tds, big.NewInt(int64(i)))
57+
chain.tds = append(chain.tds, big.NewInt(int64(i+1)))
5858
}
5959

6060
// Write blocks to Era1.

0 commit comments

Comments
 (0)