From 2e31250027ac580a7a72221fe2ff505b30836175 Mon Sep 17 00:00:00 2001 From: Sebastian Falbesoner Date: Tue, 10 Oct 2023 09:28:22 +0200 Subject: test: check that loading snapshot not matching AssumeUTXO parameters fails --- test/functional/feature_assumeutxo.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/functional/feature_assumeutxo.py b/test/functional/feature_assumeutxo.py index 6c09a6f5e7..ea93b7ab88 100755 --- a/test/functional/feature_assumeutxo.py +++ b/test/functional/feature_assumeutxo.py @@ -36,7 +36,11 @@ Interesting starting states could be loading a snapshot when the current chain t """ from test_framework.test_framework import BitcoinTestFramework -from test_framework.util import assert_equal +from test_framework.util import ( + assert_equal, + assert_raises_rpc_error, +) + START_HEIGHT = 199 SNAPSHOT_BASE_HEIGHT = 299 @@ -62,6 +66,25 @@ class AssumeutxoTest(BitcoinTestFramework): self.add_nodes(3) self.start_nodes(extra_args=self.extra_args) + def test_invalid_snapshot_scenarios(self, valid_snapshot_path): + self.log.info("Test different scenarios of loading invalid snapshot files") + self.log.info(" - snapshot file refering to a block that is not in the assumeutxo parameters") + with open(valid_snapshot_path, 'rb') as f: + valid_snapshot_contents = f.read() + + # we can only test this with a block that is already known, as otherwise the `loadtxoutset` RPC + # would time out (waiting to see the hash in the headers chain), rather than error immediately + bad_snapshot_height = SNAPSHOT_BASE_HEIGHT - 1 + bad_snapshot_path = valid_snapshot_path + '.mod' + with open(bad_snapshot_path, 'wb') as f: + bad_snapshot_block_hash = self.nodes[0].getblockhash(bad_snapshot_height) + # block hash of the snapshot base is stored right at the start (first 32 bytes) + f.write(bytes.fromhex(bad_snapshot_block_hash)[::-1] + valid_snapshot_contents[32:]) + + expected_log = f"assumeutxo height in snapshot metadata not recognized ({bad_snapshot_height}) - refusing to load snapshot" + with self.nodes[1].assert_debug_log(expected_log): + assert_raises_rpc_error(-32603, "Unable to load UTXO snapshot", self.nodes[1].loadtxoutset, bad_snapshot_path) + def run_test(self): """ Bring up two (disconnected) nodes, mine some new blocks on the first, @@ -120,6 +143,8 @@ class AssumeutxoTest(BitcoinTestFramework): assert_equal(n0.getblockchaininfo()["blocks"], FINAL_HEIGHT) + self.test_invalid_snapshot_scenarios(dump_output['path']) + self.log.info(f"Loading snapshot into second node from {dump_output['path']}") loaded = n1.loadtxoutset(dump_output['path']) assert_equal(loaded['coins_loaded'], SNAPSHOT_BASE_HEIGHT) -- cgit v1.2.3