From ab145cb3b471d07a2e8ee79edde46ec67f47d580 Mon Sep 17 00:00:00 2001 From: Sergi Delgado Segura Date: Tue, 12 Mar 2024 16:04:20 -0400 Subject: [PATCH 1/6] Updates CBlockIndexWorkComparator outdated comment --- src/node/blockstorage.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp index 3c01ff2e8d6..81b55c2440f 100644 --- a/src/node/blockstorage.cpp +++ b/src/node/blockstorage.cpp @@ -161,7 +161,7 @@ bool CBlockIndexWorkComparator::operator()(const CBlockIndex* pa, const CBlockIn if (pa->nChainWork > pb->nChainWork) return false; if (pa->nChainWork < pb->nChainWork) return true; - // ... then by earliest time received, ... + // ... then by earliest activatable time, ... if (pa->nSequenceId < pb->nSequenceId) return false; if (pa->nSequenceId > pb->nSequenceId) return true; From 5370bed21e0b04feca6ec09738ecbe792095a338 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Fri, 19 Jan 2024 16:02:57 -0500 Subject: [PATCH 2/6] test: add functional test for complex reorgs --- test/functional/feature_chain_tiebreaks.py | 103 +++++++++++++++++++++ test/functional/test_runner.py | 1 + 2 files changed, 104 insertions(+) create mode 100755 test/functional/feature_chain_tiebreaks.py diff --git a/test/functional/feature_chain_tiebreaks.py b/test/functional/feature_chain_tiebreaks.py new file mode 100755 index 00000000000..cb1f9ebec9c --- /dev/null +++ b/test/functional/feature_chain_tiebreaks.py @@ -0,0 +1,103 @@ +#!/usr/bin/env python3 +# Copyright (c) The Bitcoin Core developers +# Distributed under the MIT software license, see the accompanying +# file COPYING or http://www.opensource.org/licenses/mit-license.php. +"""Test that the correct active block is chosen in complex reorgs.""" + +from test_framework.blocktools import create_block +from test_framework.messages import CBlockHeader +from test_framework.p2p import P2PDataStore +from test_framework.test_framework import BitcoinTestFramework +from test_framework.util import assert_equal + +class ChainTiebreaksTest(BitcoinTestFramework): + def set_test_params(self): + self.num_nodes = 2 + self.setup_clean_chain = True + + @staticmethod + def send_headers(node, blocks): + """Submit headers for blocks to node.""" + for block in blocks: + # Use RPC rather than P2P, to prevent the message from being interpreted as a block + # announcement. + node.submitheader(hexdata=CBlockHeader(block).serialize().hex()) + + def run_test(self): + node = self.nodes[0] + # Add P2P connection to bitcoind + peer = node.add_p2p_connection(P2PDataStore()) + + self.log.info('Precomputing blocks') + # + # /- B3 -- B7 + # B1 \- B8 + # / \ + # / \ B4 -- B9 + # B0 \- B10 + # \ + # \ /- B5 + # B2 + # \- B6 + # + blocks = [] + + # Construct B0, building off genesis. + start_height = node.getblockcount() + blocks.append(create_block( + hashprev=int(node.getbestblockhash(), 16), + tmpl={"height": start_height + 1} + )) + blocks[-1].solve() + + # Construct B1-B10. + for i in range(1, 11): + blocks.append(create_block( + hashprev=blocks[(i - 1) >> 1].hash_int, + tmpl={ + "height": start_height + (i + 1).bit_length(), + # Make sure each block has a different hash. + "curtime": blocks[-1].nTime + 1, + } + )) + blocks[-1].solve() + + self.log.info('Make sure B0 is accepted normally') + peer.send_blocks_and_test([blocks[0]], node, success=True) + # B0 must be active chain now. + assert_equal(node.getbestblockhash(), blocks[0].hash_hex) + + self.log.info('Send B1 and B2 headers, and then blocks in opposite order') + self.send_headers(node, blocks[1:3]) + peer.send_blocks_and_test([blocks[2]], node, success=True) + peer.send_blocks_and_test([blocks[1]], node, success=False) + # B2 must be active chain now, as full data for B2 was received first. + assert_equal(node.getbestblockhash(), blocks[2].hash_hex) + + self.log.info('Send all further headers in order') + self.send_headers(node, blocks[3:]) + # B2 is still the active chain, headers don't change this. + assert_equal(node.getbestblockhash(), blocks[2].hash_hex) + + self.log.info('Send blocks B7-B10') + peer.send_blocks_and_test([blocks[7]], node, success=False) + peer.send_blocks_and_test([blocks[8]], node, success=False) + peer.send_blocks_and_test([blocks[9]], node, success=False) + peer.send_blocks_and_test([blocks[10]], node, success=False) + # B2 is still the active chain, as B7-B10 have missing parents. + assert_equal(node.getbestblockhash(), blocks[2].hash_hex) + + self.log.info('Send parents B3-B4 of B8-B10 in reverse order') + peer.send_blocks_and_test([blocks[4]], node, success=False, force_send=True) + peer.send_blocks_and_test([blocks[3]], node, success=False, force_send=True) + # B9 is now active. Despite B7 being received earlier, the missing parent. + assert_equal(node.getbestblockhash(), blocks[9].hash_hex) + + self.log.info('Invalidate B9-B10') + node.invalidateblock(blocks[9].hash_hex) + node.invalidateblock(blocks[10].hash_hex) + # B7 is now active. + assert_equal(node.getbestblockhash(), blocks[7].hash_hex) + +if __name__ == '__main__': + ChainTiebreaksTest(__file__).main() diff --git a/test/functional/test_runner.py b/test/functional/test_runner.py index 44c2885bdf8..3cf3e243600 100755 --- a/test/functional/test_runner.py +++ b/test/functional/test_runner.py @@ -320,6 +320,7 @@ BASE_SCRIPTS = [ 'feature_includeconf.py', 'feature_addrman.py', 'feature_asmap.py', + 'feature_chain_tiebreaks.py', 'feature_fastprune.py', 'feature_framework_miniwallet.py', 'mempool_unbroadcast.py', From 8b91883a23aac64a37d929eeae81325e221d177d Mon Sep 17 00:00:00 2001 From: Sergi Delgado Segura Date: Thu, 14 Mar 2024 13:48:43 -0400 Subject: [PATCH 3/6] Set the same best tip on restart if two candidates have the same work Before this, if we had two (or more) same work tip candidates and restarted our node, it could be the case that the block set as tip after bootstrap didn't match the one before stopping. That's because the work and `nSequenceId` of both block will be the same (the latter is only kept in memory), so the active chain after restart would have depended on what tip candidate was loaded first. This makes sure that we are consistent over reboots. --- src/chain.h | 4 +++- src/node/blockstorage.cpp | 5 +++-- src/validation.cpp | 18 ++++++++++++++++-- src/validation.h | 7 ++++--- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/chain.h b/src/chain.h index f5bfdb2fb4b..d348fefe2a2 100644 --- a/src/chain.h +++ b/src/chain.h @@ -191,7 +191,9 @@ public: uint32_t nNonce{0}; //! (memory only) Sequential id assigned to distinguish order in which blocks are received. - int32_t nSequenceId{0}; + //! Initialized to 1 when loading blocks from disk, except for blocks belonging to the best chain + //! which overwrite it to 0. + int32_t nSequenceId{1}; //! (memory only) Maximum nTime in the chain up to and including this block. unsigned int nTimeMax{0}; diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp index 81b55c2440f..ac8839854b8 100644 --- a/src/node/blockstorage.cpp +++ b/src/node/blockstorage.cpp @@ -166,7 +166,8 @@ bool CBlockIndexWorkComparator::operator()(const CBlockIndex* pa, const CBlockIn if (pa->nSequenceId > pb->nSequenceId) return true; // Use pointer address as tie breaker (should only happen with blocks - // loaded from disk, as those all have id 0). + // loaded from disk, as those share the same id: 0 for blocks on the + // best chain, 1 for all others). if (pa < pb) return false; if (pa > pb) return true; @@ -217,7 +218,7 @@ CBlockIndex* BlockManager::AddToBlockIndex(const CBlockHeader& block, CBlockInde // We assign the sequence id to blocks only when the full data is available, // to avoid miners withholding blocks but broadcasting headers, to get a // competitive advantage. - pindexNew->nSequenceId = 0; + pindexNew->nSequenceId = 1; pindexNew->phashBlock = &((*mi).first); BlockMap::iterator miPrev = m_block_index.find(block.hashPrevBlock); diff --git a/src/validation.cpp b/src/validation.cpp index b62691ca4c0..f9a50d84e6b 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -4680,7 +4680,7 @@ bool Chainstate::LoadChainTip() AssertLockHeld(cs_main); const CCoinsViewCache& coins_cache = CoinsTip(); assert(!coins_cache.GetBestBlock().IsNull()); // Never called when the coins view is empty - const CBlockIndex* tip = m_chain.Tip(); + CBlockIndex* tip = m_chain.Tip(); if (tip && tip->GetBlockHash() == coins_cache.GetBestBlock()) { return true; @@ -4692,6 +4692,20 @@ bool Chainstate::LoadChainTip() return false; } m_chain.SetTip(*pindex); + tip = m_chain.Tip(); + + // Make sure our chain tip before shutting down scores better than any other candidate + // to maintain a consistent best tip over reboots in case of a tie. + auto target = tip; + while (target) { + target->nSequenceId = 0; + target = target->pprev; + } + + // Block index candidates are loaded before the chain tip, so we need to replace this entry + // Otherwise the scoring will be based on the memory address location instead of the nSequenceId + setBlockIndexCandidates.erase(tip); + TryAddBlockIndexCandidate(tip); PruneBlockIndexCandidates(); tip = m_chain.Tip(); @@ -5343,7 +5357,7 @@ void ChainstateManager::CheckBlockIndex() const } } } - if (!pindex->HaveNumChainTxs()) assert(pindex->nSequenceId <= 0); // nSequenceId can't be set positive for blocks that aren't linked (negative is used for preciousblock) + if (!pindex->HaveNumChainTxs()) assert(pindex->nSequenceId <= 1); // nSequenceId can't be set higher than 1 for blocks that aren't linked (negative is used for preciousblock, 0 for active chain) // VALID_TRANSACTIONS is equivalent to nTx > 0 for all nodes (whether or not pruning has occurred). // HAVE_DATA is only equivalent to nTx > 0 (or VALID_TRANSACTIONS) if no pruning has occurred. if (!m_blockman.m_have_pruned) { diff --git a/src/validation.h b/src/validation.h index c25dd2de2d9..de3711b5c8c 100644 --- a/src/validation.h +++ b/src/validation.h @@ -1034,8 +1034,9 @@ public: * Every received block is assigned a unique and increasing identifier, so we * know which one to give priority in case of a fork. */ - /** Blocks loaded from disk are assigned id 0, so start the counter at 1. */ - int32_t nBlockSequenceId GUARDED_BY(::cs_main) = 1; + /** Blocks loaded from disk are assigned id 1 (0 if they belong to the best + * chain loaded from disk), so start the counter at 2. **/ + int32_t nBlockSequenceId GUARDED_BY(::cs_main) = 2; /** Decreasing counter (used by subsequent preciousblock calls). */ int32_t nBlockReverseSequenceId = -1; /** chainwork for the last block that preciousblock has been applied to. */ @@ -1046,7 +1047,7 @@ public: void ResetBlockSequenceCounters() EXCLUSIVE_LOCKS_REQUIRED(::cs_main) { AssertLockHeld(::cs_main); - nBlockSequenceId = 1; + nBlockSequenceId = 2; nBlockReverseSequenceId = -1; } From 18524b072e6bdd590a9f6badd15d897b5ef5ce54 Mon Sep 17 00:00:00 2001 From: Sergi Delgado Segura Date: Tue, 28 Jan 2025 12:12:36 -0500 Subject: [PATCH 4/6] Make nSequenceId init value constants Make it easier to follow what the values come without having to go over the comments, plus easier to maintain --- src/chain.h | 9 ++++++--- src/node/blockstorage.cpp | 2 +- src/validation.cpp | 6 ++++-- src/validation.h | 9 +++++---- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/chain.h b/src/chain.h index d348fefe2a2..8aa1ebb546a 100644 --- a/src/chain.h +++ b/src/chain.h @@ -35,6 +35,9 @@ static constexpr int64_t MAX_FUTURE_BLOCK_TIME = 2 * 60 * 60; * MAX_FUTURE_BLOCK_TIME. */ static constexpr int64_t TIMESTAMP_WINDOW = MAX_FUTURE_BLOCK_TIME; +//! Init values for CBlockIndex nSequenceId when loaded from disk +static constexpr int32_t SEQ_ID_BEST_CHAIN_FROM_DISK = 0; +static constexpr int32_t SEQ_ID_INIT_FROM_DISK = 1; /** * Maximum gap between node time and block time used @@ -191,9 +194,9 @@ public: uint32_t nNonce{0}; //! (memory only) Sequential id assigned to distinguish order in which blocks are received. - //! Initialized to 1 when loading blocks from disk, except for blocks belonging to the best chain - //! which overwrite it to 0. - int32_t nSequenceId{1}; + //! Initialized to SEQ_ID_INIT_FROM_DISK{1} when loading blocks from disk, except for blocks + //! belonging to the best chain which overwrite it to SEQ_ID_BEST_CHAIN_FROM_DISK{0}. + int32_t nSequenceId{SEQ_ID_INIT_FROM_DISK}; //! (memory only) Maximum nTime in the chain up to and including this block. unsigned int nTimeMax{0}; diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp index ac8839854b8..e1b54175dc2 100644 --- a/src/node/blockstorage.cpp +++ b/src/node/blockstorage.cpp @@ -218,7 +218,7 @@ CBlockIndex* BlockManager::AddToBlockIndex(const CBlockHeader& block, CBlockInde // We assign the sequence id to blocks only when the full data is available, // to avoid miners withholding blocks but broadcasting headers, to get a // competitive advantage. - pindexNew->nSequenceId = 1; + pindexNew->nSequenceId = SEQ_ID_INIT_FROM_DISK; pindexNew->phashBlock = &((*mi).first); BlockMap::iterator miPrev = m_block_index.find(block.hashPrevBlock); diff --git a/src/validation.cpp b/src/validation.cpp index f9a50d84e6b..c6137ddb137 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -4698,7 +4698,7 @@ bool Chainstate::LoadChainTip() // to maintain a consistent best tip over reboots in case of a tie. auto target = tip; while (target) { - target->nSequenceId = 0; + target->nSequenceId = SEQ_ID_BEST_CHAIN_FROM_DISK; target = target->pprev; } @@ -5357,7 +5357,9 @@ void ChainstateManager::CheckBlockIndex() const } } } - if (!pindex->HaveNumChainTxs()) assert(pindex->nSequenceId <= 1); // nSequenceId can't be set higher than 1 for blocks that aren't linked (negative is used for preciousblock, 0 for active chain) + // nSequenceId can't be set higher than SEQ_ID_INIT_FROM_DISK{1} for blocks that aren't linked + // (negative is used for preciousblock, SEQ_ID_BEST_CHAIN_FROM_DISK{0} for active chain when loaded from disk) + if (!pindex->HaveNumChainTxs()) assert(pindex->nSequenceId <= SEQ_ID_INIT_FROM_DISK); // VALID_TRANSACTIONS is equivalent to nTx > 0 for all nodes (whether or not pruning has occurred). // HAVE_DATA is only equivalent to nTx > 0 (or VALID_TRANSACTIONS) if no pruning has occurred. if (!m_blockman.m_have_pruned) { diff --git a/src/validation.h b/src/validation.h index de3711b5c8c..70c20ac135c 100644 --- a/src/validation.h +++ b/src/validation.h @@ -1034,9 +1034,10 @@ public: * Every received block is assigned a unique and increasing identifier, so we * know which one to give priority in case of a fork. */ - /** Blocks loaded from disk are assigned id 1 (0 if they belong to the best - * chain loaded from disk), so start the counter at 2. **/ - int32_t nBlockSequenceId GUARDED_BY(::cs_main) = 2; + /** Blocks loaded from disk are assigned id SEQ_ID_INIT_FROM_DISK{1} + * (SEQ_ID_BEST_CHAIN_FROM_DISK{0} if they belong to the best chain loaded from disk), + * so start the counter after that. **/ + int32_t nBlockSequenceId GUARDED_BY(::cs_main) = SEQ_ID_INIT_FROM_DISK + 1; /** Decreasing counter (used by subsequent preciousblock calls). */ int32_t nBlockReverseSequenceId = -1; /** chainwork for the last block that preciousblock has been applied to. */ @@ -1047,7 +1048,7 @@ public: void ResetBlockSequenceCounters() EXCLUSIVE_LOCKS_REQUIRED(::cs_main) { AssertLockHeld(::cs_main); - nBlockSequenceId = 2; + nBlockSequenceId = SEQ_ID_INIT_FROM_DISK + 1; nBlockReverseSequenceId = -1; } From 09c95f21e71d196120e6c9d0b1d1923a4927408d Mon Sep 17 00:00:00 2001 From: Sergi Delgado Segura Date: Thu, 14 Mar 2024 13:53:09 -0400 Subject: [PATCH 5/6] test: Adds block tiebreak over restarts tests Adds tests to make sure we are consistent on activating the same chain over a node restart if two or more candidates have the same work when the node is shutdown --- test/functional/feature_chain_tiebreaks.py | 50 +++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/test/functional/feature_chain_tiebreaks.py b/test/functional/feature_chain_tiebreaks.py index cb1f9ebec9c..707c99473cf 100755 --- a/test/functional/feature_chain_tiebreaks.py +++ b/test/functional/feature_chain_tiebreaks.py @@ -23,7 +23,7 @@ class ChainTiebreaksTest(BitcoinTestFramework): # announcement. node.submitheader(hexdata=CBlockHeader(block).serialize().hex()) - def run_test(self): + def test_chain_split_in_memory(self): node = self.nodes[0] # Add P2P connection to bitcoind peer = node.add_p2p_connection(P2PDataStore()) @@ -99,5 +99,53 @@ class ChainTiebreaksTest(BitcoinTestFramework): # B7 is now active. assert_equal(node.getbestblockhash(), blocks[7].hash_hex) + # Invalidate blocks to start fresh on the next test + node.invalidateblock(blocks[0].hash_hex) + + def test_chain_split_from_disk(self): + node = self.nodes[0] + peer = node.add_p2p_connection(P2PDataStore()) + + self.log.info('Precomputing blocks') + # + # A1 + # / + # G + # \ + # A2 + # + blocks = [] + + # Construct two blocks building from genesis. + start_height = node.getblockcount() + genesis_block = node.getblock(node.getblockhash(start_height)) + prev_time = genesis_block["time"] + + for i in range(0, 2): + blocks.append(create_block( + hashprev=int(genesis_block["hash"], 16), + tmpl={"height": start_height + 1, + # Make sure each block has a different hash. + "curtime": prev_time + i + 1, + } + )) + blocks[-1].solve() + + # Send blocks and test the last one is not connected + self.log.info('Send A1 and A2. Make sure that only the former connects') + peer.send_blocks_and_test([blocks[0]], node, success=True) + peer.send_blocks_and_test([blocks[1]], node, success=False) + + self.log.info('Restart the node and check that the best tip before restarting matched the ones afterwards') + # Restart and check enough times for this to eventually fail if the logic is broken + for _ in range(10): + self.restart_node(0) + assert_equal(blocks[0].hash_hex, node.getbestblockhash()) + + def run_test(self): + self.test_chain_split_in_memory() + self.test_chain_split_from_disk() + + if __name__ == '__main__': ChainTiebreaksTest(__file__).main() From 0465574c127907df9b764055a585e8281bae8d1d Mon Sep 17 00:00:00 2001 From: Sergi Delgado Segura Date: Fri, 20 Jun 2025 10:56:26 -0400 Subject: [PATCH 6/6] test: Fixes send_blocks_and_test docs It's not true that if success=False the tip doesn't advance. It doesn'test advance to the provided tip, but it can advance to a competing one --- test/functional/test_framework/p2p.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/functional/test_framework/p2p.py b/test/functional/test_framework/p2p.py index 610aa4ccca2..7aacf66d463 100755 --- a/test/functional/test_framework/p2p.py +++ b/test/functional/test_framework/p2p.py @@ -866,8 +866,8 @@ class P2PDataStore(P2PInterface): - the on_getheaders handler will ensure that any getheaders are responded to - if force_send is False: wait for getdata for each of the blocks. The on_getdata handler will ensure that any getdata messages are responded to. Otherwise send the full block unsolicited. - - if success is True: assert that the node's tip advances to the most recent block - - if success is False: assert that the node's tip doesn't advance + - if success is True: assert that the node's tip is the last block in blocks at the end of the operation. + - if success is False: assert that the node's tip isn't the last block in blocks at the end of the operation - if reject_reason is set: assert that the correct reject message is logged""" with p2p_lock: