closure approach

This commit is contained in:
Sina Mahmoodi 2026-03-24 16:35:09 +00:00
parent f71d097a14
commit 858bc9b590

View file

@ -276,6 +276,7 @@ func ImportHistory(chain *core.BlockChain, dir string, network string, from func
) )
for i, file := range entries { for i, file := range entries {
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.
@ -283,29 +284,29 @@ func ImportHistory(chain *core.BlockChain, dir string, network string, from func
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()
h := sha256.New() h := sha256.New()
if _, err := io.Copy(h, f); err != nil { if _, err := io.Copy(h, f); err != nil {
f.Close()
return fmt.Errorf("checksum %s: %w", path, err) return fmt.Errorf("checksum %s: %w", path, err)
} }
got := common.BytesToHash(h.Sum(nil)).Hex() got := common.BytesToHash(h.Sum(nil)).Hex()
if got != checksums[i] { if got != checksums[i] {
f.Close()
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])
} }
if _, err := f.Seek(0, io.SeekStart); err != nil { if _, err := f.Seek(0, io.SeekStart); err != nil {
f.Close()
return fmt.Errorf("seek %s: %w", path, err) return fmt.Errorf("seek %s: %w", path, err)
} }
// Import all block data from Era1.
e, err := from(f) e, err := from(f)
if err != nil { if err != nil {
f.Close() // onedb.From does not close on metadata errors
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 {
e.Close()
return fmt.Errorf("error creating iterator: %w", err) return fmt.Errorf("error creating iterator: %w", err)
} }
@ -337,7 +338,6 @@ func ImportHistory(chain *core.BlockChain, dir string, network string, from func
for it.Next() { for it.Next() {
block, err := it.Block() block, err := it.Block()
if err != nil { if err != nil {
e.Close()
return fmt.Errorf("error reading block %d: %w", it.Number(), err) return fmt.Errorf("error reading block %d: %w", it.Number(), err)
} }
if block.Number().BitLen() == 0 { if block.Number().BitLen() == 0 {
@ -345,27 +345,22 @@ func ImportHistory(chain *core.BlockChain, dir string, network string, from func
} }
receipts, err := it.Receipts() receipts, err := it.Receipts()
if err != nil { if err != nil {
e.Close()
return fmt.Errorf("error reading receipts %d: %w", it.Number(), err) return fmt.Errorf("error reading receipts %d: %w", it.Number(), err)
} }
blocks = append(blocks, block) blocks = append(blocks, block)
receiptsList = append(receiptsList, receipts) receiptsList = append(receiptsList, receipts)
if len(blocks) == importBatchSize { if len(blocks) == importBatchSize {
if err := flush(); err != nil { if err := flush(); err != nil {
e.Close()
return err return err
} }
} }
} }
if err := it.Error(); err != nil { if err := it.Error(); err != nil {
e.Close()
return err return err
} }
if err := flush(); err != nil { return flush()
e.Close() }()
return err if err != nil {
}
if err := e.Close(); err != nil {
return err return err
} }
} }