mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-06-11 15:19:19 +02:00
Merge bitcoin/bitcoin#29640: Fix tiebreak when loading blocks from disk (and add tests for comparing chain ties)
0465574c12test: Fixes send_blocks_and_test docs (Sergi Delgado Segura)09c95f21e7test: Adds block tiebreak over restarts tests (Sergi Delgado Segura)18524b072eMake nSequenceId init value constants (Sergi Delgado Segura)8b91883a23Set the same best tip on restart if two candidates have the same work (Sergi Delgado Segura)5370bed21etest: add functional test for complex reorgs (Pieter Wuille)ab145cb3b4Updates CBlockIndexWorkComparator outdated comment (Sergi Delgado Segura) Pull request description: This PR grabs some interesting bits from https://github.com/bitcoin/bitcoin/pull/29284 and fixes some edge cases in how block tiebreaks are dealt with. ## Regarding #29284 The main functionality from the PR was dropped given it was not an issue anymore, however, reviewers pointed out some comments were outdated https://github.com/bitcoin/bitcoin/pull/29284#discussion_r1522023578 (which to my understanding may have led to thinking that there was still an issue) it also added test coverage for the aforementioned case which was already passing on master and is useful to keep. ## New functionality While reviewing the superseded PR, it was noticed that blocks that are loaded from disk may face a similar issue (check https://github.com/bitcoin/bitcoin/pull/29284#issuecomment-1994317785 for more context). The issue comes from how tiebreaks for equal work blocks are handled: if two blocks have the same amount of work, the one that is activatable first wins, that is, the one for which we have all its data (and all of its ancestors'). The variable that keeps track of this, within `CBlockIndex` is `nSequenceId`, which is not persisted over restarts. This means that when a node is restarted, all blocks loaded from disk are defaulted the same `nSequenceId`: 0. Now, when trying to decide what chain is best on loading blocks from disk, the previous tiebreaker rule is not decisive anymore, so the `CBlockIndexWorkComparator` has to default to its last rule: whatever block is loaded first (has a smaller memory address). This means that if multiple same work tip candidates were available before restarting the node, it could be the case that the selected chain tip after restarting does not match the one before. Therefore, the way `nSequenceId` is initialized is changed to: - 0 for blocks that belong to the previously known best chain - 1 to all other blocks loaded from disk ACKs for top commit: sipa: utACK0465574c12TheCharlatan: ACK0465574c12furszy: Tested ACK0465574c12. Tree-SHA512: 161da814da03ce10c34d27d79a315460a9c98d019b85ee35bc5daa991ed3b6a2e69a829e421fc70d093a83cf7a2e403763041e594df39ed1991445e54c16532a
This commit is contained in:
151
test/functional/feature_chain_tiebreaks.py
Executable file
151
test/functional/feature_chain_tiebreaks.py
Executable file
@@ -0,0 +1,151 @@
|
||||
#!/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 test_chain_split_in_memory(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)
|
||||
|
||||
# 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()
|
||||
@@ -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:
|
||||
|
||||
@@ -324,6 +324,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',
|
||||
|
||||
Reference in New Issue
Block a user