Skip to content

Commit e3eb3aa

Browse files
committed
Merge bitcoin#28625: test: check that loading snapshot not matching AssumeUTXO parameters fails
2e31250 test: check that loading snapshot not matching AssumeUTXO parameters fails (Sebastian Falbesoner) Pull request description: This PR adds test coverage for the failed loading of an AssumeUTXO snapshot in case the referenced block hash doesn't match the parameters in the chainparams. Right now, I expect this would be the most common error-case for `loadtxoutset` out in the wild, as for mainnet the `m_assumeutxo_data` map is empty and this error condition would obviously always be triggered for any (otherwise valid, correctly encoded) snapshot. Note that this test-case is the simplest scenario and doesn't cover any of the TODO ideas mentioned at the top of the functional test yet. ACKs for top commit: jamesob: ACK bitcoin@2e31250 Sjors: utACK 2e31250 achow101: ACK 2e31250 Tree-SHA512: 8bcb2d525c95fbc95f87d3e978ad717d95bddb1ff67cbe7d3b06e4783f0f1ffba32b17ef451468c39c23bc1b3ef1150baa71148c145275c386f2d4822d790d39
2 parents d98d88c + 2e31250 commit e3eb3aa

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

test/functional/feature_assumeutxo.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@
3636
3737
"""
3838
from test_framework.test_framework import BitcoinTestFramework
39-
from test_framework.util import assert_equal
39+
from test_framework.util import (
40+
assert_equal,
41+
assert_raises_rpc_error,
42+
)
43+
4044

4145
START_HEIGHT = 199
4246
SNAPSHOT_BASE_HEIGHT = 299
@@ -62,6 +66,25 @@ def setup_network(self):
6266
self.add_nodes(3)
6367
self.start_nodes(extra_args=self.extra_args)
6468

69+
def test_invalid_snapshot_scenarios(self, valid_snapshot_path):
70+
self.log.info("Test different scenarios of loading invalid snapshot files")
71+
self.log.info(" - snapshot file refering to a block that is not in the assumeutxo parameters")
72+
with open(valid_snapshot_path, 'rb') as f:
73+
valid_snapshot_contents = f.read()
74+
75+
# we can only test this with a block that is already known, as otherwise the `loadtxoutset` RPC
76+
# would time out (waiting to see the hash in the headers chain), rather than error immediately
77+
bad_snapshot_height = SNAPSHOT_BASE_HEIGHT - 1
78+
bad_snapshot_path = valid_snapshot_path + '.mod'
79+
with open(bad_snapshot_path, 'wb') as f:
80+
bad_snapshot_block_hash = self.nodes[0].getblockhash(bad_snapshot_height)
81+
# block hash of the snapshot base is stored right at the start (first 32 bytes)
82+
f.write(bytes.fromhex(bad_snapshot_block_hash)[::-1] + valid_snapshot_contents[32:])
83+
84+
expected_log = f"assumeutxo height in snapshot metadata not recognized ({bad_snapshot_height}) - refusing to load snapshot"
85+
with self.nodes[1].assert_debug_log(expected_log):
86+
assert_raises_rpc_error(-32603, "Unable to load UTXO snapshot", self.nodes[1].loadtxoutset, bad_snapshot_path)
87+
6588
def run_test(self):
6689
"""
6790
Bring up two (disconnected) nodes, mine some new blocks on the first,
@@ -120,6 +143,8 @@ def run_test(self):
120143

121144
assert_equal(n0.getblockchaininfo()["blocks"], FINAL_HEIGHT)
122145

146+
self.test_invalid_snapshot_scenarios(dump_output['path'])
147+
123148
self.log.info(f"Loading snapshot into second node from {dump_output['path']}")
124149
loaded = n1.loadtxoutset(dump_output['path'])
125150
assert_equal(loaded['coins_loaded'], SNAPSHOT_BASE_HEIGHT)

0 commit comments

Comments
 (0)