Skip to content

Commit fa68571

Browse files
author
MarcoFalke
committed
test: Add assumeutxo test for wrong hash
1 parent 738ef44 commit fa68571

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

test/functional/feature_assumeutxo.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python3
2-
# Copyright (c) 2021 The Bitcoin Core developers
2+
# Copyright (c) 2021-present The Bitcoin Core developers
33
# Distributed under the MIT software license, see the accompanying
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Test for assumeutxo, a means of quickly bootstrapping a node using
@@ -17,8 +17,7 @@
1717
1818
Interesting test cases could be loading an assumeutxo snapshot file with:
1919
20-
- TODO: An invalid hash
21-
- TODO: Valid hash but invalid snapshot file (bad coin height or truncated file or
20+
- TODO: Valid hash but invalid snapshot file (bad coin height or
2221
bad other serialization)
2322
- TODO: Valid snapshot file, but referencing an unknown block
2423
- TODO: Valid snapshot file, but referencing a snapshot block that turns out to be
@@ -40,7 +39,6 @@
4039
assert_equal,
4140
assert_raises_rpc_error,
4241
)
43-
import struct
4442

4543
START_HEIGHT = 199
4644
SNAPSHOT_BASE_HEIGHT = 299
@@ -81,17 +79,25 @@ def test_invalid_snapshot_scenarios(self, valid_snapshot_path):
8179
assert_raises_rpc_error(-32603, f"Unable to load UTXO snapshot, {error_details}", self.nodes[1].loadtxoutset, bad_snapshot_path)
8280

8381
self.log.info(" - snapshot file with wrong number of coins")
84-
valid_num_coins = struct.unpack("<I", valid_snapshot_contents[32:32 + 4])[0]
82+
valid_num_coins = int.from_bytes(valid_snapshot_contents[32:32 + 8], "little")
8583
for off in [-1, +1]:
8684
with open(bad_snapshot_path, 'wb') as f:
8785
f.write(valid_snapshot_contents[:32])
88-
f.write(struct.pack("<I", valid_num_coins + off))
89-
f.write(valid_snapshot_contents[32 + 4:])
90-
86+
f.write((valid_num_coins + off).to_bytes(8, "little"))
87+
f.write(valid_snapshot_contents[32 + 8:])
9188
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"
9289
with self.nodes[1].assert_debug_log([expected_log]):
9390
assert_raises_rpc_error(-32603, "Unable to load UTXO snapshot", self.nodes[1].loadtxoutset, bad_snapshot_path)
9491

92+
self.log.info(" - snapshot file with wrong outpoint hash")
93+
with open(bad_snapshot_path, "wb") as f:
94+
f.write(valid_snapshot_contents[:(32 + 8)])
95+
f.write(b"\xff" * 32)
96+
f.write(valid_snapshot_contents[(32 + 8 + 32):])
97+
expected_log = "[snapshot] bad snapshot content hash: expected ef45ccdca5898b6c2145e4581d2b88c56564dd389e4bd75a1aaf6961d3edd3c0, got 29926acf3ac81f908cf4f22515713ca541c08bb0f0ef1b2c3443a007134d69b8"
98+
with self.nodes[1].assert_debug_log([expected_log]):
99+
assert_raises_rpc_error(-32603, "Unable to load UTXO snapshot", self.nodes[1].loadtxoutset, bad_snapshot_path)
100+
95101
def run_test(self):
96102
"""
97103
Bring up two (disconnected) nodes, mine some new blocks on the first,

0 commit comments

Comments
 (0)