Skip to content

Commit c2c22c5

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 <[email protected]>
1 parent 01b78d4 commit c2c22c5

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

899899
// ExportN writes a subset of the active chain to the given writer.
900900
func (bc *BlockChain) ExportN(w io.Writer, first uint64, last uint64) error {
901-
if !bc.chainmu.TryLock() {
902-
return errChainStopped
903-
}
904-
defer bc.chainmu.Unlock()
905-
906901
if first > last {
907902
return fmt.Errorf("export failed: first (%d) is greater than last (%d)", first, last)
908903
}
909904
log.Info("Exporting batch of blocks", "count", last-first+1)
910905

911-
start, reported := time.Now(), time.Now()
906+
var (
907+
parentHash common.Hash
908+
start = time.Now()
909+
reported = time.Now()
910+
)
912911
for nr := first; nr <= last; nr++ {
913912
block := bc.GetBlockByNumber(nr)
914913
if block == nil {
915914
return fmt.Errorf("export failed on #%d: not found", nr)
916915
}
916+
if nr > first && block.ParentHash() != parentHash {
917+
return fmt.Errorf("export failed: chain reorg during export")
918+
}
919+
parentHash = block.Hash()
917920
if err := block.EncodeRLP(w); err != nil {
918921
return err
919922
}

0 commit comments

Comments
 (0)