mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-06-01 20:48:38 +00:00
Merge 134be59d23 into 12eabbd76d
This commit is contained in:
commit
05a68e0a26
5 changed files with 67 additions and 14 deletions
|
|
@ -538,10 +538,9 @@ func importHistory(ctx *cli.Context) error {
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("unknown --era.format %q (expected 'era1' or 'erae')", format)
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Import done in %v\n", time.Since(start))
|
fmt.Printf("Import done in %v\n", time.Since(start))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -249,13 +249,10 @@ func readList(filename string) ([]string, error) {
|
||||||
return strings.Split(string(b), "\n"), nil
|
return strings.Split(string(b), "\n"), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ImportHistory imports Era1 files containing historical block information,
|
// ImportHistory imports Era1/Erae files containing historical block information,
|
||||||
// starting from genesis. The assumption is held that the provided chain
|
// starting from genesis. The assumption is held that the provided chain
|
||||||
// segment in Era1 file should all be canonical and verified.
|
// segment in Era1/Erae 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")
|
|
||||||
}
|
|
||||||
entries, err := era.ReadDir(dir, network)
|
entries, err := era.ReadDir(dir, network)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error reading %s: %w", dir, err)
|
return fmt.Errorf("error reading %s: %w", dir, err)
|
||||||
|
|
@ -269,6 +266,13 @@ func ImportHistory(chain *core.BlockChain, dir string, network string, from func
|
||||||
len(checksums), len(entries))
|
len(checksums), len(entries))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Determine resume point from last successfully imported block.
|
||||||
|
var resumeBlock uint64
|
||||||
|
if tail := rawdb.ReadEraImportTail(db); tail != nil {
|
||||||
|
resumeBlock = *tail
|
||||||
|
log.Info("Resuming era import", "lastBlock", resumeBlock)
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
start = time.Now()
|
start = time.Now()
|
||||||
reported = time.Now()
|
reported = time.Now()
|
||||||
|
|
@ -281,13 +285,33 @@ func ImportHistory(chain *core.BlockChain, dir string, network string, from func
|
||||||
err := func() error {
|
err := func() error {
|
||||||
path := filepath.Join(dir, file)
|
path := filepath.Join(dir, file)
|
||||||
|
|
||||||
// Validate against checksum file in directory.
|
|
||||||
f, err := os.Open(path)
|
f, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("open %s: %w", path, err)
|
return fmt.Errorf("open %s: %w", path, err)
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
|
// Peek at era block range to check if we can skip entirely.
|
||||||
|
e, err := from(f)
|
||||||
|
if err != nil {
|
||||||
|
f.Close()
|
||||||
|
return fmt.Errorf("error opening era: %w", err)
|
||||||
|
}
|
||||||
|
eraStart := e.Start()
|
||||||
|
eraEnd := eraStart + e.Count() - 1
|
||||||
|
e.Close()
|
||||||
|
|
||||||
|
// Skip era files fully behind resume point.
|
||||||
|
if resumeBlock > 0 && eraEnd <= resumeBlock {
|
||||||
|
log.Debug("Skipping already imported Era file", "file", file, "eraEnd", eraEnd, "resumeBlock", resumeBlock)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// reopen for checksum + import.
|
||||||
|
f, err = os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("open %s: %w", path, err)
|
||||||
|
}
|
||||||
if _, err := io.Copy(h, f); err != nil {
|
if _, err := io.Copy(h, f); err != nil {
|
||||||
return fmt.Errorf("checksum %s: %w", path, err)
|
return fmt.Errorf("checksum %s: %w", path, err)
|
||||||
}
|
}
|
||||||
|
|
@ -297,8 +321,12 @@ func ImportHistory(chain *core.BlockChain, dir string, network string, from func
|
||||||
if got != checksums[i] {
|
if got != checksums[i] {
|
||||||
return fmt.Errorf("%s checksum mismatch: have %s want %s", file, 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)
|
// Seek back for import.
|
||||||
|
if _, err := f.Seek(0, io.SeekStart); err != nil {
|
||||||
|
return fmt.Errorf("seek %s: %w", path, err)
|
||||||
|
}
|
||||||
|
e, err = from(f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error opening era: %w", err)
|
return fmt.Errorf("error opening era: %w", err)
|
||||||
}
|
}
|
||||||
|
|
@ -321,10 +349,14 @@ func ImportHistory(chain *core.BlockChain, dir string, network string, from func
|
||||||
return fmt.Errorf("error inserting blocks %d-%d: %w",
|
return fmt.Errorf("error inserting blocks %d-%d: %w",
|
||||||
blocks[0].NumberU64(), blocks[len(blocks)-1].NumberU64(), err)
|
blocks[0].NumberU64(), blocks[len(blocks)-1].NumberU64(), err)
|
||||||
}
|
}
|
||||||
|
// Persist tail after each successful batch.
|
||||||
|
lastBlock := blocks[len(blocks)-1].NumberU64()
|
||||||
|
rawdb.WriteEraImportTail(db, lastBlock)
|
||||||
|
resumeBlock = lastBlock
|
||||||
|
|
||||||
imported += len(blocks)
|
imported += len(blocks)
|
||||||
if time.Since(reported) >= 8*time.Second {
|
if time.Since(reported) >= 8*time.Second {
|
||||||
head := blocks[len(blocks)-1].NumberU64()
|
log.Info("Importing Era files", "head", lastBlock, "imported", imported,
|
||||||
log.Info("Importing Era files", "head", head, "imported", imported,
|
|
||||||
"elapsed", common.PrettyDuration(time.Since(start)))
|
"elapsed", common.PrettyDuration(time.Since(start)))
|
||||||
imported = 0
|
imported = 0
|
||||||
reported = time.Now()
|
reported = time.Now()
|
||||||
|
|
@ -334,6 +366,7 @@ func ImportHistory(chain *core.BlockChain, dir string, network string, from func
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
for it.Next() {
|
for it.Next() {
|
||||||
block, err := it.Block()
|
block, err := it.Block()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -342,6 +375,10 @@ func ImportHistory(chain *core.BlockChain, dir string, network string, from func
|
||||||
if block.Number().BitLen() == 0 {
|
if block.Number().BitLen() == 0 {
|
||||||
continue // skip genesis
|
continue // skip genesis
|
||||||
}
|
}
|
||||||
|
// Skip blocks already imported (mid-era resume).
|
||||||
|
if resumeBlock > 0 && block.Number().Uint64() <= resumeBlock {
|
||||||
|
continue
|
||||||
|
}
|
||||||
receipts, err := it.Receipts()
|
receipts, err := it.Receipts()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error reading receipts %d: %w", it.Number(), err)
|
return fmt.Errorf("error reading receipts %d: %w", it.Number(), err)
|
||||||
|
|
|
||||||
|
|
@ -182,7 +182,7 @@ func TestHistoryImportAndExport(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to initialize chain: %v", err)
|
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)
|
t.Fatalf("failed to import chain: %v", err)
|
||||||
}
|
}
|
||||||
if have, want := imported.CurrentHeader(), chain.CurrentHeader(); have.Hash() != want.Hash() {
|
if have, want := imported.CurrentHeader(), chain.CurrentHeader(); have.Hash() != want.Hash() {
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,21 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/rlp"
|
"github.com/ethereum/go-ethereum/rlp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func ReadEraImportTail(db ethdb.KeyValueReader) *uint64 {
|
||||||
|
data, _ := db.Get(eraImportTailKey)
|
||||||
|
if len(data) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
tail := binary.BigEndian.Uint64(data)
|
||||||
|
return &tail
|
||||||
|
}
|
||||||
|
|
||||||
|
func WriteEraImportTail(db ethdb.KeyValueWriter, blockNumber uint64) {
|
||||||
|
buf := make([]byte, 8)
|
||||||
|
binary.BigEndian.PutUint64(buf, blockNumber)
|
||||||
|
db.Put(eraImportTailKey, buf)
|
||||||
|
}
|
||||||
|
|
||||||
// DecodeTxLookupEntry decodes the supplied tx lookup data.
|
// DecodeTxLookupEntry decodes the supplied tx lookup data.
|
||||||
func DecodeTxLookupEntry(data []byte, db ethdb.Reader) *uint64 {
|
func DecodeTxLookupEntry(data []byte, db ethdb.Reader) *uint64 {
|
||||||
// Database v6 tx lookup just stores the block number
|
// Database v6 tx lookup just stores the block number
|
||||||
|
|
|
||||||
|
|
@ -87,6 +87,8 @@ var (
|
||||||
// txIndexTailKey tracks the oldest block whose transactions have been indexed.
|
// txIndexTailKey tracks the oldest block whose transactions have been indexed.
|
||||||
txIndexTailKey = []byte("TransactionIndexTail")
|
txIndexTailKey = []byte("TransactionIndexTail")
|
||||||
|
|
||||||
|
eraImportTailKey = []byte("eraImportTail") // eraImportTailKey -> last fully imported block
|
||||||
|
|
||||||
// fastTxLookupLimitKey tracks the transaction lookup limit during fast sync.
|
// fastTxLookupLimitKey tracks the transaction lookup limit during fast sync.
|
||||||
// This flag is deprecated, it's kept to avoid reporting errors when inspect
|
// This flag is deprecated, it's kept to avoid reporting errors when inspect
|
||||||
// database.
|
// database.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue