Skip to content

Commit 55b6d7b

Browse files
committed
validation: Don't load a snapshot if it's not in the best header chain.
If the snapshot is not an ancestor of the most-work header (m_best_header), syncing from that alternative chain should be prioritised. Therefore don't accept loading a snapshot in this situation. If that other chain turns out to be invalid, m_best_header would be reset and loading the snapshot should be possible again. Because of the work required to generate a conflicting headers chain, this should only be possible under extreme circumstances, such as major forks.
1 parent e5a5497 commit 55b6d7b

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/validation.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5683,6 +5683,10 @@ util::Result<void> ChainstateManager::ActivateSnapshot(
56835683
return util::Error{strprintf(Untranslated("The base block header (%s) is part of an invalid chain"), base_blockhash.ToString())};
56845684
}
56855685

5686+
if (!m_best_header || m_best_header->GetAncestor(base_blockheight) != snapshot_start_block) {
5687+
return util::Error{_("A forked headers-chain with more work than the chain with the snapshot base block header exists. Please proceed to sync without AssumeUtxo.")};
5688+
}
5689+
56865690
auto mempool{m_active_chainstate->GetMempool()};
56875691
if (mempool && mempool->size() > 0) {
56885692
return util::Error{Untranslated("Can't activate a snapshot when mempool not empty")};

test/functional/feature_assumeutxo.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,21 @@
1515
1616
- TODO: Valid snapshot file, but referencing a snapshot block that turns out to be
1717
invalid, or has an invalid parent
18-
- TODO: Valid snapshot file and snapshot block, but the block is not on the
19-
most-work chain
2018
2119
Interesting starting states could be loading a snapshot when the current chain tip is:
2220
2321
- TODO: An ancestor of snapshot block
2422
- TODO: The snapshot block
2523
- TODO: A descendant of the snapshot block
26-
- TODO: Not an ancestor or a descendant of the snapshot block and has more work
2724
2825
"""
2926
from shutil import rmtree
3027

3128
from dataclasses import dataclass
29+
from test_framework.blocktools import (
30+
create_block,
31+
create_coinbase
32+
)
3233
from test_framework.messages import tx_from_hex
3334
from test_framework.test_framework import BitcoinTestFramework
3435
from test_framework.util import (
@@ -241,6 +242,30 @@ def test_snapshot_in_a_divergent_chain(self, dump_output_path):
241242
self.sync_blocks(nodes=(n0, n3))
242243
self.wait_until(lambda: len(n3.getchainstates()['chainstates']) == 1)
243244

245+
def test_snapshot_not_on_most_work_chain(self, dump_output_path):
246+
self.log.info("Test snapshot is not loaded when the node knows the headers of another chain with more work.")
247+
node0 = self.nodes[0]
248+
node1 = self.nodes[1]
249+
# Create an alternative chain of 2 new blocks, forking off the main chain at the block before the snapshot block.
250+
# This simulates a longer chain than the main chain when submitting these two block headers to node 1 because it is only aware of
251+
# the main chain headers up to the snapshot height.
252+
parent_block_hash = node0.getblockhash(SNAPSHOT_BASE_HEIGHT - 1)
253+
block_time = node0.getblock(node0.getbestblockhash())['time'] + 1
254+
fork_block1 = create_block(int(parent_block_hash, 16), create_coinbase(SNAPSHOT_BASE_HEIGHT), block_time)
255+
fork_block1.solve()
256+
fork_block2 = create_block(fork_block1.sha256, create_coinbase(SNAPSHOT_BASE_HEIGHT + 1), block_time + 1)
257+
fork_block2.solve()
258+
node1.submitheader(fork_block1.serialize().hex())
259+
node1.submitheader(fork_block2.serialize().hex())
260+
msg = "A forked headers-chain with more work than the chain with the snapshot base block header exists. Please proceed to sync without AssumeUtxo."
261+
assert_raises_rpc_error(-32603, msg, node1.loadtxoutset, dump_output_path)
262+
# Cleanup: submit two more headers of the snapshot chain to node 1, so that it is the most-work chain again and loading
263+
# the snapshot in future subtests succeeds
264+
main_block1 = node0.getblock(node0.getblockhash(SNAPSHOT_BASE_HEIGHT + 1), 0)
265+
main_block2 = node0.getblock(node0.getblockhash(SNAPSHOT_BASE_HEIGHT + 2), 0)
266+
node1.submitheader(main_block1)
267+
node1.submitheader(main_block2)
268+
244269
def run_test(self):
245270
"""
246271
Bring up two (disconnected) nodes, mine some new blocks on the first,
@@ -330,6 +355,7 @@ def run_test(self):
330355
self.test_invalid_chainstate_scenarios()
331356
self.test_invalid_file_path()
332357
self.test_snapshot_block_invalidated(dump_output['path'])
358+
self.test_snapshot_not_on_most_work_chain(dump_output['path'])
333359

334360
self.log.info(f"Loading snapshot into second node from {dump_output['path']}")
335361
loaded = n1.loadtxoutset(dump_output['path'])

0 commit comments

Comments
 (0)