mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-05-15 20:46:40 +00:00
cmd/utils: optimize history import with batched insertion (#33894)
Improve speed of import-history command by two orders of magnitude. Rework ImportHistory to collect up to 2500 blocks per flush instead of flushing after each block, reducing database commit overhead. --------- Co-authored-by: Sina Mahmoodi <itz.s1na@gmail.com>
This commit is contained in:
parent
e951bcbff7
commit
8f9061f937
1 changed files with 41 additions and 20 deletions
|
|
@ -274,40 +274,66 @@ func ImportHistory(chain *core.BlockChain, dir string, network string, from func
|
||||||
reported = time.Now()
|
reported = time.Now()
|
||||||
imported = 0
|
imported = 0
|
||||||
h = sha256.New()
|
h = sha256.New()
|
||||||
scratch = bytes.NewBuffer(nil)
|
buf = bytes.NewBuffer(nil)
|
||||||
)
|
)
|
||||||
|
|
||||||
for i, file := range entries {
|
for i, file := range entries {
|
||||||
err := func() error {
|
err := func() error {
|
||||||
path := filepath.Join(dir, file)
|
path := filepath.Join(dir, file)
|
||||||
|
|
||||||
// validate against checksum file in directory
|
// 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()
|
||||||
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
got := common.BytesToHash(h.Sum(scratch.Bytes()[:])).Hex()
|
got := common.BytesToHash(h.Sum(buf.Bytes()[:])).Hex()
|
||||||
want := checksums[i]
|
|
||||||
h.Reset()
|
h.Reset()
|
||||||
scratch.Reset()
|
buf.Reset()
|
||||||
|
if got != checksums[i] {
|
||||||
if got != want {
|
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, want)
|
|
||||||
}
|
}
|
||||||
// Import all block data from Era1.
|
// Import all block data from Era1.
|
||||||
e, err := from(f)
|
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)
|
||||||
}
|
}
|
||||||
|
defer e.Close()
|
||||||
|
|
||||||
it, err := e.Iterator()
|
it, err := e.Iterator()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error creating iterator: %w", err)
|
return fmt.Errorf("error creating iterator: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
blocks = make([]*types.Block, 0, importBatchSize)
|
||||||
|
receiptsList = make([]types.Receipts, 0, importBatchSize)
|
||||||
|
flush = func() error {
|
||||||
|
if len(blocks) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
enc := types.EncodeBlockReceiptLists(receiptsList)
|
||||||
|
if _, err := chain.InsertReceiptChain(blocks, enc, math.MaxUint64); err != nil {
|
||||||
|
return fmt.Errorf("error inserting blocks %d-%d: %w",
|
||||||
|
blocks[0].NumberU64(), blocks[len(blocks)-1].NumberU64(), err)
|
||||||
|
}
|
||||||
|
imported += len(blocks)
|
||||||
|
if time.Since(reported) >= 8*time.Second {
|
||||||
|
head := blocks[len(blocks)-1].NumberU64()
|
||||||
|
log.Info("Importing Era files", "head", head, "imported", imported,
|
||||||
|
"elapsed", common.PrettyDuration(time.Since(start)))
|
||||||
|
imported = 0
|
||||||
|
reported = time.Now()
|
||||||
|
}
|
||||||
|
blocks = blocks[:0]
|
||||||
|
receiptsList = receiptsList[:0]
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
)
|
||||||
for it.Next() {
|
for it.Next() {
|
||||||
block, err := it.Block()
|
block, err := it.Block()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -320,23 +346,18 @@ func ImportHistory(chain *core.BlockChain, dir string, network string, from func
|
||||||
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)
|
||||||
}
|
}
|
||||||
enc := types.EncodeBlockReceiptLists([]types.Receipts{receipts})
|
blocks = append(blocks, block)
|
||||||
if _, err := chain.InsertReceiptChain([]*types.Block{block}, enc, math.MaxUint64); err != nil {
|
receiptsList = append(receiptsList, receipts)
|
||||||
return fmt.Errorf("error inserting body %d: %w", it.Number(), err)
|
if len(blocks) == importBatchSize {
|
||||||
}
|
if err := flush(); err != nil {
|
||||||
imported++
|
return err
|
||||||
|
}
|
||||||
if time.Since(reported) >= 8*time.Second {
|
|
||||||
log.Info("Importing Era files", "head", it.Number(), "imported", imported,
|
|
||||||
"elapsed", common.PrettyDuration(time.Since(start)))
|
|
||||||
imported = 0
|
|
||||||
reported = time.Now()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err := it.Error(); err != nil {
|
if err := it.Error(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return flush()
|
||||||
}()
|
}()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue