Skip to content

Commit 828377d

Browse files
committed
interna/era,cmd: simplify era interace, fix era.ReadDir, fix lints
1 parent 4e17d2c commit 828377d

8 files changed

Lines changed: 55 additions & 49 deletions

File tree

cmd/geth/chaincmd.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"encoding/json"
2121
"errors"
2222
"fmt"
23+
"io"
2324
"os"
2425
"path/filepath"
2526
"regexp"
@@ -504,19 +505,21 @@ func importHistory(ctx *cli.Context) error {
504505
network = networks[0]
505506
}
506507

507-
format := ctx.String(utils.EraFormatFlag.Name)
508+
var (
509+
format = ctx.String(utils.EraFormatFlag.Name)
510+
from func(era.ReadAtSeekCloser) (era.Era, error)
511+
)
508512
switch format {
509513
case "era1", "era":
510-
if err := utils.ImportHistory(chain, dir, network, onedb.From, onedb.NewIterator); err != nil {
511-
return err
512-
}
514+
from = onedb.From
513515
case "erae":
514-
if err := utils.ImportHistory(chain, dir, network, execdb.From, execdb.NewIterator); err != nil {
515-
return err
516-
}
516+
from = execdb.From
517517
default:
518518
return fmt.Errorf("unknown --era.format %q (expected 'era1' or 'erae')", format)
519519
}
520+
if err := utils.ImportHistory(chain, dir, network, from); err != nil {
521+
return err
522+
}
520523

521524
fmt.Printf("Import done in %v\n", time.Since(start))
522525
return nil
@@ -549,19 +552,24 @@ func exportHistory(ctx *cli.Context) error {
549552
utils.Fatalf("Export error: block number %d larger than head block %d\n", uint64(last), head.Number.Uint64())
550553
}
551554

552-
format := ctx.String(utils.EraFormatFlag.Get(ctx))
555+
var (
556+
format = ctx.String(utils.EraFormatFlag.Get(ctx))
557+
filename func(network string, epoch int, root common.Hash) string
558+
newBuilder func(w io.Writer) era.Builder
559+
)
553560
switch format {
554561
case "era1", "era":
555-
if err := utils.ExportHistory(chain, dir, uint64(first), uint64(last), uint64(era.MaxSize), onedb.NewBuilder, onedb.Filename); err != nil {
556-
utils.Fatalf("Export error: %v\n", err)
557-
}
562+
newBuilder = func(w io.Writer) era.Builder { return onedb.NewBuilder(w) }
563+
filename = func(network string, epoch int, root common.Hash) string { return onedb.Filename(network, epoch, root) }
558564
case "erae":
559-
if err := utils.ExportHistory(chain, dir, uint64(first), uint64(last), uint64(era.MaxSize), execdb.NewBuilder, execdb.Filename); err != nil {
560-
utils.Fatalf("Export error: %v\n", err)
561-
}
565+
newBuilder = func(w io.Writer) era.Builder { return execdb.NewBuilder(w) }
566+
filename = func(network string, epoch int, root common.Hash) string { return execdb.Filename(network, epoch, root) }
562567
default:
563568
return fmt.Errorf("unknown archive format %q (use 'era1' or 'erae')", format)
564569
}
570+
if err := utils.ExportHistory(chain, dir, uint64(first), uint64(last), uint64(era.MaxSize), newBuilder, filename); err != nil {
571+
utils.Fatalf("Export error: %v\n", err)
572+
}
565573

566574
fmt.Printf("Export done in %v\n", time.Since(start))
567575
return nil

cmd/utils/cmd.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ func readList(filename string) ([]string, error) {
251251
// ImportHistory imports Era1 files containing historical block information,
252252
// starting from genesis. The assumption is held that the provided chain
253253
// segment in Era1 file should all be canonical and verified.
254-
func ImportHistory(chain *core.BlockChain, dir string, network string, from era.FromFn, iterator era.NewIteratorFn) error {
254+
func ImportHistory(chain *core.BlockChain, dir string, network string, from func(f era.ReadAtSeekCloser) (era.Era, error)) error {
255255
if chain.CurrentSnapBlock().Number.BitLen() != 0 {
256256
return errors.New("history import only supported when starting from genesis")
257257
}
@@ -303,7 +303,7 @@ func ImportHistory(chain *core.BlockChain, dir string, network string, from era.
303303
if err != nil {
304304
return fmt.Errorf("error opening era: %w", err)
305305
}
306-
it, err := iterator(e)
306+
it, err := e.Iterator()
307307
if err != nil {
308308
return fmt.Errorf("error creating iterator: %w", err)
309309
}
@@ -415,7 +415,7 @@ func ExportAppendChain(blockchain *core.BlockChain, fn string, first uint64, las
415415

416416
// ExportHistory exports blockchain history into the specified directory,
417417
// following the Era format.
418-
func ExportHistory(bc *core.BlockChain, dir string, first, last, step uint64, builderfn era.NewBuilderFn, filename era.FilenameFn) error {
418+
func ExportHistory(bc *core.BlockChain, dir string, first, last, step uint64, newBuilder func(io.Writer) era.Builder, filename func(network string, epoch int, root common.Hash) string) error {
419419
log.Info("Exporting blockchain history", "dir", dir)
420420
if head := bc.CurrentBlock().Number.Uint64(); head < last {
421421
log.Warn("Last block beyond head, setting last = head", "head", head, "last", last)
@@ -453,7 +453,7 @@ func ExportHistory(bc *core.BlockChain, dir string, first, last, step uint64, bu
453453
}
454454
defer fh.Close()
455455

456-
bldr := builderfn(fh)
456+
bldr := newBuilder(fh)
457457

458458
for j := uint64(0); j < step && batch+j <= last; j++ {
459459
n := batch + j

cmd/utils/history_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/ethereum/go-ethereum/core/rawdb"
3333
"github.com/ethereum/go-ethereum/core/types"
3434
"github.com/ethereum/go-ethereum/crypto"
35+
"github.com/ethereum/go-ethereum/internal/era"
3536
"github.com/ethereum/go-ethereum/internal/era/onedb"
3637
"github.com/ethereum/go-ethereum/params"
3738
"github.com/ethereum/go-ethereum/trie"
@@ -89,7 +90,7 @@ func TestHistoryImportAndExport(t *testing.T) {
8990
dir := t.TempDir()
9091

9192
// Export history to temp directory.
92-
if err := ExportHistory(chain, dir, 0, count, step); err != nil {
93+
if err := ExportHistory(chain, dir, 0, count, step, onedb.NewBuilder, onedb.Filename); err != nil {
9394
t.Fatalf("error exporting history: %v", err)
9495
}
9596

@@ -101,7 +102,7 @@ func TestHistoryImportAndExport(t *testing.T) {
101102
checksums := strings.Split(string(b), "\n")
102103

103104
// Verify each Era.
104-
entries, _ := onedb.ReadDir(dir, "mainnet")
105+
entries, _ := era.ReadDir(dir, "mainnet")
105106
for i, filename := range entries {
106107
func() {
107108
f, err := os.Open(filepath.Join(dir, filename))
@@ -170,7 +171,7 @@ func TestHistoryImportAndExport(t *testing.T) {
170171
if err != nil {
171172
t.Fatalf("unable to initialize chain: %v", err)
172173
}
173-
if err := ImportHistory(imported, dir, "mainnet"); err != nil {
174+
if err := ImportHistory(imported, dir, "mainnet", onedb.From); err != nil {
174175
t.Fatalf("failed to import chain: %v", err)
175176
}
176177
if have, want := imported.CurrentHeader(), chain.CurrentHeader(); have.Hash() != want.Hash() {

internal/era/era.go

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -59,45 +59,34 @@ type Era interface {
5959
Close() error
6060
Start() uint64
6161
Count() uint64
62+
Iterator() (Iterator, error)
6263
GetBlockByNumber(num uint64) (*types.Block, error)
6364
GetRawBodyByNumber(num uint64) ([]byte, error)
6465
GetRawReceiptsByNumber(num uint64) ([]byte, error)
6566
}
6667

67-
// NewBuilderFn defines a function type for creating a new Builder.
68-
type NewBuilderFn func(w io.Writer) Builder
69-
70-
// FilenameFn defines a function type for generating a filename based on network, epoch, and root hash.
71-
type FilenameFn func(network string, epoch int, root common.Hash) string
72-
73-
// FromFn defines a function type for creating an Era from a ReadAtSeekCloser.
74-
type FromFn func(f ReadAtSeekCloser) (Era, error)
75-
76-
// NewIteratorFn defines a function type for creating a new Iterator from an Era.
77-
type NewIteratorFn func(e Era) (Iterator, error)
78-
7968
// ReadDir reads all the era1 files in a directory for a given network.
8069
// Format: <network>-<epoch>-<hexroot>.erae or <network>-<epoch>-<hexroot>.era1
8170
func ReadDir(dir, network string) ([]string, error) {
8271
entries, err := os.ReadDir(dir)
83-
var directoryExtension string
72+
8473
if err != nil {
8574
return nil, fmt.Errorf("error reading directory %s: %w", dir, err)
8675
}
8776
var (
88-
next = uint64(0)
89-
eras []string
77+
next = uint64(0)
78+
eras []string
79+
dirType string
9080
)
91-
for i, entry := range entries {
92-
fileExtension := path.Ext(entry.Name())
93-
if i == 0 {
94-
directoryExtension = fileExtension
95-
} else if directoryExtension != fileExtension {
96-
return nil, fmt.Errorf("directory %s contains mixed era file formats", dir)
97-
}
98-
if fileExtension != ".erae" || fileExtension != ".era1" {
81+
fmt.Println("entries", entries)
82+
for _, entry := range entries {
83+
ext := path.Ext(entry.Name())
84+
if ext != ".erae" && ext != ".era1" {
9985
continue
10086
}
87+
if dirType == "" {
88+
dirType = ext
89+
}
10190
parts := strings.Split(entry.Name(), "-")
10291
if len(parts) != 3 || parts[0] != network {
10392
// Invalid era1 filename, skip.
@@ -108,6 +97,9 @@ func ReadDir(dir, network string) ([]string, error) {
10897
} else if epoch != next {
10998
return nil, fmt.Errorf("missing epoch %d", next)
11099
}
100+
if dirType != ext {
101+
return nil, fmt.Errorf("directory %s contains mixed era file formats: want %s, have %s", dir, dirType, ext)
102+
}
111103
next += 1
112104
eras = append(eras, entry.Name())
113105
}

internal/era/execdb/builder.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ type Builder struct {
8989
written uint64
9090
expectsProofs bool
9191
isPreMerge bool
92-
numPreMerge int // number of pre-merge blocks
9392
finalTD *big.Int // final total difficulty, used for pre-merge and merge straddling files
9493
}
9594

internal/era/execdb/reader.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ func (e *Era) Count() uint64 {
8888
return e.m.count
8989
}
9090

91+
// Iterator returns an iterator over the era file.
92+
func (e *Era) Iterator() (era.Iterator, error) {
93+
return NewIterator(e)
94+
}
95+
9196
// GetBlockByNumber retrieves the block if present within the era file.
9297
func (e *Era) GetBlockByNumber(blockNum uint64) (*types.Block, error) {
9398
h, err := e.GetHeader(blockNum)

internal/era/onedb/reader.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ func (e *Era) Close() error {
8181
return e.f.Close()
8282
}
8383

84+
// Iterator returns an iterator over the era file.
85+
func (e *Era) Iterator() (era.Iterator, error) {
86+
return NewIterator(e)
87+
}
88+
8489
// GetBlockByNumber returns the block for the given block number.
8590
func (e *Era) GetBlockByNumber(num uint64) (*types.Block, error) {
8691
if e.m.start > num || e.m.start+e.m.count <= num {

internal/era/proof.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,3 @@ func (p *BlockProofHistoricalSummariesDeneb) EncodeRLP(w io.Writer) error {
110110

111111
// Variant returns the variant type of the BlockProofHistoricalSummariesDeneb.
112112
func (*BlockProofHistoricalSummariesDeneb) Variant() variant { return proofDeneb }
113-
114-
func variantOf(p Proof) variant {
115-
return p.Variant()
116-
}

0 commit comments

Comments
 (0)