@@ -43,7 +43,7 @@ import (
4343 "github.com/ethereum/go-ethereum/eth/ethconfig"
4444 "github.com/ethereum/go-ethereum/ethdb"
4545 "github.com/ethereum/go-ethereum/internal/debug"
46- era2 "github.com/ethereum/go-ethereum/internal/era/execdb "
46+ "github.com/ethereum/go-ethereum/internal/era"
4747 "github.com/ethereum/go-ethereum/internal/era/onedb"
4848 "github.com/ethereum/go-ethereum/log"
4949 "github.com/ethereum/go-ethereum/node"
@@ -251,11 +251,11 @@ 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 ) error {
254+ func ImportHistory (chain * core.BlockChain , dir string , network string , from era. FromFn , iterator era. NewIteratorFn ) error {
255255 if chain .CurrentSnapBlock ().Number .BitLen () != 0 {
256256 return errors .New ("history import only supported when starting from genesis" )
257257 }
258- entries , err := onedb .ReadDir (dir , network )
258+ entries , err := era .ReadDir (dir , network )
259259 if err != nil {
260260 return fmt .Errorf ("error reading %s: %w" , dir , err )
261261 }
@@ -299,11 +299,11 @@ func ImportHistory(chain *core.BlockChain, dir string, network string) error {
299299 return fmt .Errorf ("%s checksum mismatch: have %s want %s" , file , got , want )
300300 }
301301 // Import all block data from Era1.
302- e , err := onedb . From (f )
302+ e , err := from (f )
303303 if err != nil {
304304 return fmt .Errorf ("error opening era: %w" , err )
305305 }
306- it , err := onedb . NewIterator (e )
306+ it , err := iterator (e )
307307 if err != nil {
308308 return fmt .Errorf ("error creating iterator: %w" , err )
309309 }
@@ -345,105 +345,6 @@ func ImportHistory(chain *core.BlockChain, dir string, network string) error {
345345 return nil
346346}
347347
348- // ImportHistoryEraE imports Era-E files containing historical block information,
349- // starting from genesis. Currently this function follows the assumptions that the provided chain
350- // segment in Era-E file should all be canonical and verified. In the future, this function will be
351- // extended to support importing Era-E files with proof verification.
352- func ImportHistoryEraE (chain * core.BlockChain , dir , network string ) error {
353- if chain .CurrentSnapBlock ().Number .Sign () != 0 {
354- return errors .New ("history import only supported when starting from genesis" )
355- }
356-
357- entries , err := era2 .ReadDir (dir , network )
358- if err != nil {
359- return fmt .Errorf ("reading %q: %w" , dir , err )
360- }
361- checksums , err := readList (filepath .Join (dir , "checksums.txt" ))
362- if err != nil {
363- return fmt .Errorf ("reading checksums.txt: %w" , err )
364- }
365- if len (entries ) != len (checksums ) {
366- return fmt .Errorf ("mismatch: %d erae files, %d checksums" , len (entries ), len (checksums ))
367- }
368-
369- var (
370- start = time .Now ()
371- reported = time .Now ()
372- imported int
373- h = sha256 .New ()
374- scratch bytes.Buffer
375- )
376-
377- for i , file := range entries {
378- path := filepath .Join (dir , file )
379-
380- // validate against checksum file in directory
381- f , err := os .Open (path )
382- if err != nil {
383- return fmt .Errorf ("open %s: %w" , path , err )
384- }
385- if _ , err := io .Copy (h , f ); err != nil {
386- f .Close ()
387- return fmt .Errorf ("checksum %s: %w" , path , err )
388- }
389- got := common .BytesToHash (h .Sum (scratch .Bytes ()[:])).Hex ()
390- want := checksums [i ]
391- h .Reset ()
392- scratch .Reset ()
393- if got != want {
394- f .Close ()
395- return fmt .Errorf ("%s checksum mismatch: have %s want %s" , file , got , want )
396- }
397- // rewind for reading
398- if _ , err := f .Seek (0 , io .SeekStart ); err != nil {
399- f .Close ()
400- return fmt .Errorf ("rewind %s: %w" , file , err )
401- }
402- // import archive
403- e , err := era2 .From (f )
404- if err != nil {
405- f .Close ()
406- return fmt .Errorf ("open erae %s: %w" , file , err )
407- }
408- blockCount := e .Count ()
409-
410- for j := uint64 (0 ); j < blockCount ; j ++ {
411- hdr , err := e .GetHeader (e .Start () + j )
412- if err != nil {
413- return fmt .Errorf ("header #%d: %w" , hdr .Number .Uint64 (), err )
414- }
415- if hdr .Number .Sign () == 0 { // skip genesis
416- continue
417- }
418- body , err := e .GetBody (hdr .Number .Uint64 ())
419- if err != nil {
420- return fmt .Errorf ("body #%d: %w" , hdr .Number .Uint64 (), err )
421- }
422- rcpts , err := e .GetReceipts (hdr .Number .Uint64 (), 1 )
423- if err != nil {
424- return fmt .Errorf ("receipts #%d: %w" , hdr .Number .Uint64 (), err )
425- }
426- blk := types .NewBlockWithHeader (hdr ).WithBody (* body )
427-
428- enc := types .EncodeBlockReceiptLists (rcpts )
429- if _ , err := chain .InsertReceiptChain ([]* types.Block {blk }, enc , ^ uint64 (0 )); err != nil {
430- return fmt .Errorf ("insert #%d: %w" , hdr .Number .Uint64 (), err )
431- }
432-
433- imported ++
434- if time .Since (reported ) >= 8 * time .Second {
435- log .Info ("Importing Era‑E" , "head" , hdr .Number , "imported" , imported , "elapsed" , common .PrettyDuration (time .Since (start )))
436- reported = time .Now ()
437- imported = 0
438- }
439- }
440- f .Close ()
441- }
442-
443- log .Info ("Era‑E import complete" , "duration" , common .PrettyDuration (time .Since (start )))
444- return nil
445- }
446-
447348func missingBlocks (chain * core.BlockChain , blocks []* types.Block ) []* types.Block {
448349 head := chain .CurrentBlock ()
449350 for i , block := range blocks {
@@ -514,7 +415,7 @@ func ExportAppendChain(blockchain *core.BlockChain, fn string, first uint64, las
514415
515416// ExportHistory exports blockchain history into the specified directory,
516417// following the Era format.
517- func ExportHistory (bc * core.BlockChain , dir string , first , last , step uint64 ) error {
418+ func ExportHistory (bc * core.BlockChain , dir string , first , last , step uint64 , builderfn era. NewBuilderFn , filename era. FilenameFn ) error {
518419 log .Info ("Exporting blockchain history" , "dir" , dir )
519420 if head := bc .CurrentBlock ().Number .Uint64 (); head < last {
520421 log .Warn ("Last block beyond head, setting last = head" , "head" , head , "last" , last )
@@ -543,7 +444,7 @@ func ExportHistory(bc *core.BlockChain, dir string, first, last, step uint64) er
543444
544445 for batch := first ; batch <= last ; batch += step {
545446 idx := int (batch / step )
546- tmpPath := filepath .Join (dir , onedb . Filename (network , idx , common.Hash {}))
447+ tmpPath := filepath .Join (dir , filename (network , idx , common.Hash {}))
547448
548449 if err := func () error {
549450 fh , err := os .Create (tmpPath )
@@ -552,7 +453,7 @@ func ExportHistory(bc *core.BlockChain, dir string, first, last, step uint64) er
552453 }
553454 defer fh .Close ()
554455
555- bldr := onedb . NewBuilder (fh )
456+ bldr := builderfn (fh )
556457
557458 for j := uint64 (0 ); j < step && batch + j <= last ; j ++ {
558459 n := batch + j
0 commit comments