Skip to content

Commit

Permalink
Merge bitcoin/bitcoin#28647: test: Add assumeutxo test for wrong hash
Browse files Browse the repository at this point in the history
fa68571 test: Add assumeutxo test for wrong hash (MarcoFalke)

Pull request description:

  Also:
  * Update test TODOs
  * Fix off-by-4 typo in test, remove `struct` import

ACKs for top commit:
  fjahr:
    utACK fa68571
  theStack:
    Code-review re-ACK fa68571
  pablomartin4btc:
    re ACK fa68571
  ryanofsky:
    Code review ACK fa68571

Tree-SHA512: 877653010efe4e20018827e8ec2801d036e1344457401f0c9e5d55907b817724201dd2e3f0f29505bbff619882c0c2cd731ecdcd209258bcefe11b86ff0205dd
  • Loading branch information
ryanofsky committed Oct 17, 2023
2 parents 738ef44 + fa68571 commit ff6be77
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions test/functional/feature_assumeutxo.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python3
# Copyright (c) 2021 The Bitcoin Core developers
# Copyright (c) 2021-present The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test for assumeutxo, a means of quickly bootstrapping a node using
Expand All @@ -17,8 +17,7 @@
Interesting test cases could be loading an assumeutxo snapshot file with:
- TODO: An invalid hash
- TODO: Valid hash but invalid snapshot file (bad coin height or truncated file or
- TODO: Valid hash but invalid snapshot file (bad coin height or
bad other serialization)
- TODO: Valid snapshot file, but referencing an unknown block
- TODO: Valid snapshot file, but referencing a snapshot block that turns out to be
Expand All @@ -40,7 +39,6 @@
assert_equal,
assert_raises_rpc_error,
)
import struct

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

self.log.info(" - snapshot file with wrong number of coins")
valid_num_coins = struct.unpack("<I", valid_snapshot_contents[32:32 + 4])[0]
valid_num_coins = int.from_bytes(valid_snapshot_contents[32:32 + 8], "little")
for off in [-1, +1]:
with open(bad_snapshot_path, 'wb') as f:
f.write(valid_snapshot_contents[:32])
f.write(struct.pack("<I", valid_num_coins + off))
f.write(valid_snapshot_contents[32 + 4:])

f.write((valid_num_coins + off).to_bytes(8, "little"))
f.write(valid_snapshot_contents[32 + 8:])
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"
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)

self.log.info(" - snapshot file with wrong outpoint hash")
with open(bad_snapshot_path, "wb") as f:
f.write(valid_snapshot_contents[:(32 + 8)])
f.write(b"\xff" * 32)
f.write(valid_snapshot_contents[(32 + 8 + 32):])
expected_log = "[snapshot] bad snapshot content hash: expected ef45ccdca5898b6c2145e4581d2b88c56564dd389e4bd75a1aaf6961d3edd3c0, got 29926acf3ac81f908cf4f22515713ca541c08bb0f0ef1b2c3443a007134d69b8"
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,
Expand Down

0 comments on commit ff6be77

Please sign in to comment.