|
1 | 1 | #!/usr/bin/env python3
|
2 |
| -# Copyright (c) 2021 The Bitcoin Core developers |
| 2 | +# Copyright (c) 2021-present The Bitcoin Core developers |
3 | 3 | # Distributed under the MIT software license, see the accompanying
|
4 | 4 | # file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
5 | 5 | """Test for assumeutxo, a means of quickly bootstrapping a node using
|
|
17 | 17 |
|
18 | 18 | Interesting test cases could be loading an assumeutxo snapshot file with:
|
19 | 19 |
|
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 |
22 | 21 | bad other serialization)
|
23 | 22 | - TODO: Valid snapshot file, but referencing an unknown block
|
24 | 23 | - TODO: Valid snapshot file, but referencing a snapshot block that turns out to be
|
|
40 | 39 | assert_equal,
|
41 | 40 | assert_raises_rpc_error,
|
42 | 41 | )
|
43 |
| -import struct |
44 | 42 |
|
45 | 43 | START_HEIGHT = 199
|
46 | 44 | SNAPSHOT_BASE_HEIGHT = 299
|
@@ -81,17 +79,25 @@ def test_invalid_snapshot_scenarios(self, valid_snapshot_path):
|
81 | 79 | assert_raises_rpc_error(-32603, f"Unable to load UTXO snapshot, {error_details}", self.nodes[1].loadtxoutset, bad_snapshot_path)
|
82 | 80 |
|
83 | 81 | 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") |
85 | 83 | for off in [-1, +1]:
|
86 | 84 | with open(bad_snapshot_path, 'wb') as f:
|
87 | 85 | 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:]) |
91 | 88 | 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"
|
92 | 89 | with self.nodes[1].assert_debug_log([expected_log]):
|
93 | 90 | assert_raises_rpc_error(-32603, "Unable to load UTXO snapshot", self.nodes[1].loadtxoutset, bad_snapshot_path)
|
94 | 91 |
|
| 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 | + |
95 | 101 | def run_test(self):
|
96 | 102 | """
|
97 | 103 | Bring up two (disconnected) nodes, mine some new blocks on the first,
|
|
0 commit comments