Skip to content

Commit fafde92

Browse files
author
MarcoFalke
committed
test: Check snapshot file with wrong number of coins
Also, fix a bug in an assert_debug_log call.
1 parent faa90f6 commit fafde92

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

test/functional/feature_assumeutxo.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
assert_equal,
4141
assert_raises_rpc_error,
4242
)
43-
43+
import struct
4444

4545
START_HEIGHT = 199
4646
SNAPSHOT_BASE_HEIGHT = 299
@@ -68,23 +68,35 @@ def setup_network(self):
6868

6969
def test_invalid_snapshot_scenarios(self, valid_snapshot_path):
7070
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")
7271
with open(valid_snapshot_path, 'rb') as f:
7372
valid_snapshot_contents = f.read()
73+
bad_snapshot_path = valid_snapshot_path + '.mod'
7474

75+
self.log.info(" - snapshot file refering to a block that is not in the assumeutxo parameters")
7576
# we can only test this with a block that is already known, as otherwise the `loadtxoutset` RPC
7677
# would time out (waiting to see the hash in the headers chain), rather than error immediately
7778
bad_snapshot_height = SNAPSHOT_BASE_HEIGHT - 1
78-
bad_snapshot_path = valid_snapshot_path + '.mod'
7979
with open(bad_snapshot_path, 'wb') as f:
8080
bad_snapshot_block_hash = self.nodes[0].getblockhash(bad_snapshot_height)
8181
# block hash of the snapshot base is stored right at the start (first 32 bytes)
8282
f.write(bytes.fromhex(bad_snapshot_block_hash)[::-1] + valid_snapshot_contents[32:])
8383

8484
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):
85+
with self.nodes[1].assert_debug_log([expected_log]):
8686
assert_raises_rpc_error(-32603, "Unable to load UTXO snapshot", self.nodes[1].loadtxoutset, bad_snapshot_path)
8787

88+
self.log.info(" - snapshot file with wrong number of coins")
89+
valid_num_coins = struct.unpack("<I", valid_snapshot_contents[32:32 + 4])[0]
90+
for off in [-1, +1]:
91+
with open(bad_snapshot_path, 'wb') as f:
92+
f.write(valid_snapshot_contents[:32])
93+
f.write(struct.pack("<I", valid_num_coins + off))
94+
f.write(valid_snapshot_contents[32 + 4:])
95+
96+
expected_log = f"bad snapshot - coins left over after deserializing 298 coins" if off == -1 else f"bad snapshot format or truncated snapshot after deserializing 299 coins"
97+
with self.nodes[1].assert_debug_log([expected_log]):
98+
assert_raises_rpc_error(-32603, "Unable to load UTXO snapshot", self.nodes[1].loadtxoutset, bad_snapshot_path)
99+
88100
def run_test(self):
89101
"""
90102
Bring up two (disconnected) nodes, mine some new blocks on the first,

0 commit comments

Comments
 (0)