aboutsummaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorPéter Szilágyi <peterke@gmail.com>2019-05-10 22:04:10 +0800
committerGitHub <noreply@github.com>2019-05-10 22:04:10 +0800
commit6ec6b290511a7433a36a40d1e8c2cca3d9613d6f (patch)
tree617c1877b16ed53e116ec4481c9bac3d7c100905 /core
parent494f5d448a1685d5de4cb1524b863cd1fc9a13b0 (diff)
downloadgo-tangerine-6ec6b290511a7433a36a40d1e8c2cca3d9613d6f.tar
go-tangerine-6ec6b290511a7433a36a40d1e8c2cca3d9613d6f.tar.gz
go-tangerine-6ec6b290511a7433a36a40d1e8c2cca3d9613d6f.tar.bz2
go-tangerine-6ec6b290511a7433a36a40d1e8c2cca3d9613d6f.tar.lz
go-tangerine-6ec6b290511a7433a36a40d1e8c2cca3d9613d6f.tar.xz
go-tangerine-6ec6b290511a7433a36a40d1e8c2cca3d9613d6f.tar.zst
go-tangerine-6ec6b290511a7433a36a40d1e8c2cca3d9613d6f.zip
core: fix import errors on clique crashes + empty blocks (#19544)
* core: fix import errors on clique crashes + empty blocks * cosensus/clique, core: add test for the mirrored state issue * core: address todo question wrt log count * core: raise a louder warning for non-clique known blocks
Diffstat (limited to 'core')
-rw-r--r--core/blockchain.go42
-rw-r--r--core/chain_makers.go7
2 files changed, 47 insertions, 2 deletions
diff --git a/core/blockchain.go b/core/blockchain.go
index 8cbef7173..52d1a7c29 100644
--- a/core/blockchain.go
+++ b/core/blockchain.go
@@ -1169,7 +1169,9 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, []
if localTd.Cmp(externTd) < 0 {
break
}
+ log.Debug("Ignoring already known block", "number", block.Number(), "hash", block.Hash())
stats.ignored++
+
block, err = it.next()
}
// The remaining blocks are still known blocks, the only scenario here is:
@@ -1181,6 +1183,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, []
// `insertChain` while a part of them have higher total difficulty than current
// head full block(new pivot point).
for block != nil && err == ErrKnownBlock {
+ log.Debug("Writing previously known block", "number", block.Number(), "hash", block.Hash())
if err := bc.writeKnownBlock(block); err != nil {
return it.index, nil, nil, err
}
@@ -1190,15 +1193,16 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, []
}
// Falls through to the block import
}
-
switch {
// First block is pruned, insert as sidechain and reorg only if TD grows enough
case err == consensus.ErrPrunedAncestor:
+ log.Debug("Pruned ancestor, inserting as sidechain", "number", block.Number(), "hash", block.Hash())
return bc.insertSideChain(block, it)
// First block is future, shove it (and all children) to the future queue (unknown ancestor)
case err == consensus.ErrFutureBlock || (err == consensus.ErrUnknownAncestor && bc.futureBlocks.Contains(it.first().ParentHash())):
for block != nil && (it.index == 0 || err == consensus.ErrUnknownAncestor) {
+ log.Debug("Future block, postponing import", "number", block.Number(), "hash", block.Hash())
if err := bc.addFutureBlock(block); err != nil {
return it.index, events, coalescedLogs, err
}
@@ -1217,7 +1221,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, []
return it.index, events, coalescedLogs, err
}
// No validation errors for the first block (or chain prefix skipped)
- for ; block != nil && err == nil; block, err = it.next() {
+ for ; block != nil && err == nil || err == ErrKnownBlock; block, err = it.next() {
// If the chain is terminating, stop processing blocks
if atomic.LoadInt32(&bc.procInterrupt) == 1 {
log.Debug("Premature abort during blocks processing")
@@ -1228,6 +1232,32 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, []
bc.reportBlock(block, nil, ErrBlacklistedHash)
return it.index, events, coalescedLogs, ErrBlacklistedHash
}
+ // If the block is known (in the middle of the chain), it's a special case for
+ // Clique blocks where they can share state among each other, so importing an
+ // older block might complete the state of the subsequent one. In this case,
+ // just skip the block (we already validated it once fully (and crashed), since
+ // its header and body was already in the database).
+ if err == ErrKnownBlock {
+ logger := log.Debug
+ if bc.chainConfig.Clique == nil {
+ logger = log.Warn
+ }
+ logger("Inserted known block", "number", block.Number(), "hash", block.Hash(),
+ "uncles", len(block.Uncles()), "txs", len(block.Transactions()), "gas", block.GasUsed(),
+ "root", block.Root())
+
+ if err := bc.writeKnownBlock(block); err != nil {
+ return it.index, nil, nil, err
+ }
+ stats.processed++
+
+ // We can assume that logs are empty here, since the only way for consecutive
+ // Clique blocks to have the same state is if there are no transactions.
+ events = append(events, ChainEvent{block, block.Hash(), nil})
+ lastCanon = block
+
+ continue
+ }
// Retrieve the parent block and it's state to execute on top
start := time.Now()
@@ -1327,6 +1357,14 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, []
"txs", len(block.Transactions()), "gas", block.GasUsed(), "uncles", len(block.Uncles()),
"root", block.Root())
events = append(events, ChainSideEvent{block})
+
+ default:
+ // This in theory is impossible, but lets be nice to our future selves and leave
+ // a log, instead of trying to track down blocks imports that don't emit logs.
+ log.Warn("Inserted block with unknown status", "number", block.Number(), "hash", block.Hash(),
+ "diff", block.Difficulty(), "elapsed", common.PrettyDuration(time.Since(start)),
+ "txs", len(block.Transactions()), "gas", block.GasUsed(), "uncles", len(block.Uncles()),
+ "root", block.Root())
}
stats.processed++
stats.usedGas += usedGas
diff --git a/core/chain_makers.go b/core/chain_makers.go
index 754b543c7..17f404211 100644
--- a/core/chain_makers.go
+++ b/core/chain_makers.go
@@ -71,6 +71,13 @@ func (b *BlockGen) SetNonce(nonce types.BlockNonce) {
b.header.Nonce = nonce
}
+// SetDifficulty sets the difficulty field of the generated block. This method is
+// useful for Clique tests where the difficulty does not depend on time. For the
+// ethash tests, please use OffsetTime, which implicitly recalculates the diff.
+func (b *BlockGen) SetDifficulty(diff *big.Int) {
+ b.header.Difficulty = diff
+}
+
// AddTx adds a transaction to the generated block. If no coinbase has
// been set, the block's coinbase is set to the zero address.
//