Skip to content

Commit

Permalink
core: remove lock in BlockChain.ExportN (ethereum#25254)
Browse files Browse the repository at this point in the history
* Remove locking in (*BlockChain).ExportN

Since ExportN is read-only, it shouldn't need the lock. (?)

* Add hash check to detect reorgs during export.

* fix check order

* Update blockchain.go

* Update blockchain.go

Co-authored-by: rjl493456442 <garyrong0905@gmail.com>
  • Loading branch information
2 people authored and HanWang233 committed Sep 11, 2022
1 parent 9b71b74 commit d2dc188
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -739,22 +739,25 @@ func (bc *BlockChain) Export(w io.Writer) error {

// ExportN writes a subset of the active chain to the given writer.
func (bc *BlockChain) ExportN(w io.Writer, first uint64, last uint64) error {
if !bc.chainmu.TryLock() {
return errChainStopped
}
defer bc.chainmu.Unlock()

if first > last {
return fmt.Errorf("export failed: first (%d) is greater than last (%d)", first, last)
}
log.Info("Exporting batch of blocks", "count", last-first+1)

start, reported := time.Now(), time.Now()
var (
parentHash common.Hash
start = time.Now()
reported = time.Now()
)
for nr := first; nr <= last; nr++ {
block := bc.GetBlockByNumber(nr)
if block == nil {
return fmt.Errorf("export failed on #%d: not found", nr)
}
if nr > first && block.ParentHash() != parentHash {
return fmt.Errorf("export failed: chain reorg during export")
}
parentHash = block.Hash()
if err := block.EncodeRLP(w); err != nil {
return err
}
Expand Down

0 comments on commit d2dc188

Please sign in to comment.