Skip to content

Commit 4766b11

Browse files
core: remove lock in BlockChain.ExportN (#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 <[email protected]>
1 parent 434ca02 commit 4766b11

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
@@ -739,22 +739,25 @@ func (bc *BlockChain) Export(w io.Writer) error {
739739

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

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

0 commit comments

Comments
 (0)