Skip to content

Commit c1106cf

Browse files
committed
Merge bitcoin#28669: test: check assumeutxo file for changed outpoint index + de-duplications
d322368 test: De-dublicate/optimize assumeutxo test for further extensions (Fabian Jahr) 0a576d6 test: check au file with changed outpoint index (Fabian Jahr) Pull request description: Also doing some de-duplications. I kept the second commit separate for now as I am not 100% if this is overdoing it and makes it harder to reason about. But it also makes it easier to add more cases where we change more data. ACKs for top commit: maflcko: lgtm ACK d322368 achow101: ACK d322368 Tree-SHA512: be950a34b0ed50cb58459df47cff6513df19d834bf81815572cd26b10dee26e6f80866f0c44023cf246aafbbd256e62d23ce903e8b07fdff2297bc7065799bb8
2 parents abfc8c9 + d322368 commit c1106cf

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

test/functional/feature_assumeutxo.py

+20-14
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,19 @@ def test_invalid_snapshot_scenarios(self, valid_snapshot_path):
6969
valid_snapshot_contents = f.read()
7070
bad_snapshot_path = valid_snapshot_path + '.mod'
7171

72+
def expected_error(log_msg="", rpc_details=""):
73+
with self.nodes[1].assert_debug_log([log_msg]):
74+
assert_raises_rpc_error(-32603, f"Unable to load UTXO snapshot{rpc_details}", self.nodes[1].loadtxoutset, bad_snapshot_path)
75+
7276
self.log.info(" - snapshot file refering to a block that is not in the assumeutxo parameters")
7377
prev_block_hash = self.nodes[0].getblockhash(SNAPSHOT_BASE_HEIGHT - 1)
7478
bogus_block_hash = "0" * 64 # Represents any unknown block hash
7579
for bad_block_hash in [bogus_block_hash, prev_block_hash]:
7680
with open(bad_snapshot_path, 'wb') as f:
7781
# block hash of the snapshot base is stored right at the start (first 32 bytes)
7882
f.write(bytes.fromhex(bad_block_hash)[::-1] + valid_snapshot_contents[32:])
79-
error_details = f"assumeutxo block hash in snapshot metadata not recognized ({bad_block_hash})"
80-
assert_raises_rpc_error(-32603, f"Unable to load UTXO snapshot, {error_details}", self.nodes[1].loadtxoutset, bad_snapshot_path)
83+
error_details = f", assumeutxo block hash in snapshot metadata not recognized ({bad_block_hash})"
84+
expected_error(rpc_details=error_details)
8185

8286
self.log.info(" - snapshot file with wrong number of coins")
8387
valid_num_coins = int.from_bytes(valid_snapshot_contents[32:32 + 8], "little")
@@ -86,18 +90,20 @@ def test_invalid_snapshot_scenarios(self, valid_snapshot_path):
8690
f.write(valid_snapshot_contents[:32])
8791
f.write((valid_num_coins + off).to_bytes(8, "little"))
8892
f.write(valid_snapshot_contents[32 + 8:])
89-
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"
90-
with self.nodes[1].assert_debug_log([expected_log]):
91-
assert_raises_rpc_error(-32603, "Unable to load UTXO snapshot", self.nodes[1].loadtxoutset, bad_snapshot_path)
92-
93-
self.log.info(" - snapshot file with wrong outpoint hash")
94-
with open(bad_snapshot_path, "wb") as f:
95-
f.write(valid_snapshot_contents[:(32 + 8)])
96-
f.write(b"\xff" * 32)
97-
f.write(valid_snapshot_contents[(32 + 8 + 32):])
98-
expected_log = "[snapshot] bad snapshot content hash: expected ef45ccdca5898b6c2145e4581d2b88c56564dd389e4bd75a1aaf6961d3edd3c0, got 29926acf3ac81f908cf4f22515713ca541c08bb0f0ef1b2c3443a007134d69b8"
99-
with self.nodes[1].assert_debug_log([expected_log]):
100-
assert_raises_rpc_error(-32603, "Unable to load UTXO snapshot", self.nodes[1].loadtxoutset, bad_snapshot_path)
93+
expected_error(log_msg=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")
94+
95+
self.log.info(" - snapshot file with alternated UTXO data")
96+
cases = [
97+
[b"\xff" * 32, 0, "29926acf3ac81f908cf4f22515713ca541c08bb0f0ef1b2c3443a007134d69b8"], # wrong outpoint hash
98+
[(1).to_bytes(4, "little"), 32, "798266c2e1f9a98fe5ce61f5951cbf47130743f3764cf3cbc254be129142cf9d"], # wrong outpoint index
99+
]
100+
101+
for content, offset, wrong_hash in cases:
102+
with open(bad_snapshot_path, "wb") as f:
103+
f.write(valid_snapshot_contents[:(32 + 8 + offset)])
104+
f.write(content)
105+
f.write(valid_snapshot_contents[(32 + 8 + offset + len(content)):])
106+
expected_error(log_msg=f"[snapshot] bad snapshot content hash: expected ef45ccdca5898b6c2145e4581d2b88c56564dd389e4bd75a1aaf6961d3edd3c0, got {wrong_hash}")
101107

102108
def run_test(self):
103109
"""

0 commit comments

Comments
 (0)