Skip to content

Commit

Permalink
assumeutxo: fail early if snapshot block hash doesn't match AssumeUTX…
Browse files Browse the repository at this point in the history
…O parameters
  • Loading branch information
theStack committed Oct 15, 2023
1 parent 78b7e95 commit 164273c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
4 changes: 4 additions & 0 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2751,6 +2751,10 @@ static RPCHelpMan loadtxoutset()
afile >> metadata;

uint256 base_blockhash = metadata.m_base_blockhash;
if (!Params().AssumeutxoForBlockhash(base_blockhash).has_value()) {
throw JSONRPCError(RPC_INTERNAL_ERROR, strprintf("Unable to load UTXO snapshot, "
"assumeutxo block hash in snapshot metadata not recognized (%s)", base_blockhash.ToString()));
}
int max_secs_to_wait_for_headers = 60 * 10;
CBlockIndex* snapshot_start_block = nullptr;

Expand Down
11 changes: 3 additions & 8 deletions test/functional/feature_assumeutxo.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,12 @@ def test_invalid_snapshot_scenarios(self, valid_snapshot_path):
bad_snapshot_path = valid_snapshot_path + '.mod'

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

expected_log = f"assumeutxo height in snapshot metadata not recognized ({bad_snapshot_height}) - refusing to load snapshot"
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)
error_details = f"assumeutxo block hash in snapshot metadata not recognized ({bad_snapshot_block_hash})"
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]
Expand Down

0 comments on commit 164273c

Please sign in to comment.