From 8b32520d7f60af4fb853398642dcb886c6a6984f Mon Sep 17 00:00:00 2001 From: henridf Date: Wed, 6 Jul 2022 14:20:55 +0200 Subject: [PATCH 1/5] Remove locking in (*BlockChain).ExportN Since ExportN is read-only, it shouldn't need the lock. (?) --- core/blockchain.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 5a4ae9e10fe2..92d3ebcbc019 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -739,11 +739,6 @@ 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) } From 8fc7da97e2223b2cb32376f6556feb3cebf63845 Mon Sep 17 00:00:00 2001 From: Henri DF Date: Thu, 14 Jul 2022 10:56:21 +0200 Subject: [PATCH 2/5] Add hash check to detect reorgs during export. --- core/blockchain.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/core/blockchain.go b/core/blockchain.go index 92d3ebcbc019..1f3c03a37579 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -745,8 +745,13 @@ func (bc *BlockChain) ExportN(w io.Writer, first uint64, last uint64) error { log.Info("Exporting batch of blocks", "count", last-first+1) start, reported := time.Now(), time.Now() + var parentHash common.Hash for nr := first; nr <= last; nr++ { block := bc.GetBlockByNumber(nr) + if nr > first && block.Hash() != parentHash { + return fmt.Errorf("export failed: chain reorg during export") + } + parentHash = block.Hash() if block == nil { return fmt.Errorf("export failed on #%d: not found", nr) } From 46e131e4c14d5d4987ff3c251e9222470aa50f15 Mon Sep 17 00:00:00 2001 From: Henri DF Date: Thu, 14 Jul 2022 12:13:29 +0200 Subject: [PATCH 3/5] fix check order --- core/blockchain.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 1f3c03a37579..2e8526a4f54d 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -748,13 +748,13 @@ func (bc *BlockChain) ExportN(w io.Writer, first uint64, last uint64) error { var parentHash common.Hash 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.Hash() != parentHash { return fmt.Errorf("export failed: chain reorg during export") } parentHash = block.Hash() - if block == nil { - return fmt.Errorf("export failed on #%d: not found", nr) - } if err := block.EncodeRLP(w); err != nil { return err } From 77522a0d4510e19e3aa7f94393cc00d909dbe071 Mon Sep 17 00:00:00 2001 From: rjl493456442 Date: Thu, 14 Jul 2022 20:17:42 +0800 Subject: [PATCH 4/5] Update blockchain.go --- core/blockchain.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 2e8526a4f54d..ea9c44bfccd5 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -744,8 +744,11 @@ func (bc *BlockChain) ExportN(w io.Writer, first uint64, last uint64) error { } log.Info("Exporting batch of blocks", "count", last-first+1) - start, reported := time.Now(), time.Now() - var parentHash common.Hash + var ( + parentHash common.Hash + start = time.Now() + reported = time.Now() + ) for nr := first; nr <= last; nr++ { block := bc.GetBlockByNumber(nr) if block == nil { From 3eb5c42a657a2651d00f83658fd0312420a5d98f Mon Sep 17 00:00:00 2001 From: rjl493456442 Date: Thu, 14 Jul 2022 20:19:10 +0800 Subject: [PATCH 5/5] Update blockchain.go --- core/blockchain.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/blockchain.go b/core/blockchain.go index ea9c44bfccd5..fbeddecdbb29 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -754,7 +754,7 @@ func (bc *BlockChain) ExportN(w io.Writer, first uint64, last uint64) error { if block == nil { return fmt.Errorf("export failed on #%d: not found", nr) } - if nr > first && block.Hash() != parentHash { + if nr > first && block.ParentHash() != parentHash { return fmt.Errorf("export failed: chain reorg during export") } parentHash = block.Hash()