Skip to content

Commit 543384d

Browse files
henridfrjl493456442
authored andcommitted
core: remove lock in BlockChain.ExportN (ethereum#25254)
* 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>
1 parent 2473170 commit 543384d

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

core/blockchain.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -740,22 +740,25 @@ func (bc *BlockChain) Export(w io.Writer) error {
740740

741741
// ExportN writes a subset of the active chain to the given writer.
742742
func (bc *BlockChain) ExportN(w io.Writer, first uint64, last uint64) error {
743-
if !bc.chainmu.TryLock() {
744-
return errChainStopped
745-
}
746-
defer bc.chainmu.Unlock()
747-
748743
if first > last {
749744
return fmt.Errorf("export failed: first (%d) is greater than last (%d)", first, last)
750745
}
751746
log.Info("Exporting batch of blocks", "count", last-first+1)
752747

753-
start, reported := time.Now(), time.Now()
748+
var (
749+
parentHash common.Hash
750+
start = time.Now()
751+
reported = time.Now()
752+
)
754753
for nr := first; nr <= last; nr++ {
755754
block := bc.GetBlockByNumber(nr)
756755
if block == nil {
757756
return fmt.Errorf("export failed on #%d: not found", nr)
758757
}
758+
if nr > first && block.ParentHash() != parentHash {
759+
return fmt.Errorf("export failed: chain reorg during export")
760+
}
761+
parentHash = block.Hash()
759762
if err := block.EncodeRLP(w); err != nil {
760763
return err
761764
}

0 commit comments

Comments
 (0)