Skip to content

Commit

Permalink
Merge branch 'port-D5370' into 'master'
Browse files Browse the repository at this point in the history
[port] Don't park blocks when there is no actual reorg

See merge request bitcoin-cash-node/bitcoin-cash-node!56
  • Loading branch information
ftrader committed Mar 7, 2020
2 parents 34534d5 + 54d94d1 commit cdab28e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/validation.cpp
Expand Up @@ -4218,13 +4218,15 @@ bool CChainState::AcceptBlock(const Config &config,
block.GetHash().ToString());
}

// If this is a deep reorg (a regorg of more than one block), preemptively
// mark the chain as parked. If it has enough work, it'll unpark
// automatically. We mark the block as parked at the very last minute so we
// can make sure everything is ready to be reorged if needed.
// If connecting the new block would require rewinding more than one block
// from the active chain (i.e., a "deep reorg"), then mark the new block as
// parked. If it has enough work then it will be automatically unparked
// later, during FindMostWorkChain. We mark the block as parked at the very
// last minute so we can make sure everything is ready to be reorged if
// needed.
if (gArgs.GetBoolArg("-parkdeepreorg", true)) {
const CBlockIndex *pindexFork = chainActive.FindFork(pindex);
if (pindexFork && pindexFork->nHeight + 1 < pindex->nHeight) {
if (pindexFork && pindexFork->nHeight + 1 < chainActive.Height()) {
LogPrintf("Park block %s as it would cause a deep reorg.\n",
pindex->GetBlockHash().ToString());
pindex->nStatus = pindex->nStatus.withParked();
Expand Down
20 changes: 20 additions & 0 deletions test/functional/abc-parkedchain.py
Expand Up @@ -192,6 +192,26 @@ def check_reorg_protection(depth, extra_blocks):
check_reorg_protection(6, 6)
check_reorg_protection(100, 100)

# try deep reorg with a log check.
with parking_node.assert_debug_log(["Park block"]):
check_reorg_protection(3, 1)

self.log.info(
"Accepting many blocks at once (possibly out of order) should not park if there is no reorg.")
# rewind one block to make a reorg that is shallow.
node.invalidateblock(parking_node.getbestblockhash())
# generate a ton of blocks at once.
try:
with parking_node.assert_debug_log(["Park block"]):
node.generate(20)
wait_until(lambda: parking_node.getbestblockhash() ==
node.getbestblockhash())
except AssertionError as exc:
# good, we want an absence of "Park block" messages
assert "does not partially match log" in exc.args[0]
else:
raise AssertionError("Parked block when there was no deep reorg")

self.log.info("Test that unparking works when -parkdeepreorg=0")
# Set up parking node height = fork + 4, node height = fork + 5
node.invalidateblock(node.getbestblockhash())
Expand Down

0 comments on commit cdab28e

Please sign in to comment.