0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-03 09:56:38 -05:00

Merge bitcoin/bitcoin#28639: refactor: Remove unused nchaintx from SnapshotMetadata constructor, fix test, add test

fafde92f84 test: Check snapshot file with wrong number of coins (MarcoFalke)
faa90f6e7b refactor: Remove unused nchaintx from SnapshotMetadata constructor (MarcoFalke)

Pull request description:

  See commit messages

ACKs for top commit:
  Sjors:
    utACK fafde92f84
  theStack:
    ACK fafde92f84

Tree-SHA512: 9ed2720b50d1c0938f30543ba143e1a4c6af3a0ff166f8b3eb452e1d99ddee6e3443a4c99f77efe94b8c3eb2feff984bf5259807ee8085e1e0e1e0d1de98227e
This commit is contained in:
fanquake 2023-10-13 11:07:25 +02:00
commit 448790c00a
No known key found for this signature in database
GPG key ID: 2EEB9F5CC09526C1
3 changed files with 19 additions and 10 deletions

View file

@ -35,8 +35,7 @@ public:
SnapshotMetadata() { }
SnapshotMetadata(
const uint256& base_blockhash,
uint64_t coins_count,
unsigned int nchaintx) :
uint64_t coins_count) :
m_base_blockhash(base_blockhash),
m_coins_count(coins_count) { }

View file

@ -2667,7 +2667,7 @@ UniValue CreateUTXOSnapshot(
tip->nHeight, tip->GetBlockHash().ToString(),
fs::PathToString(path), fs::PathToString(temppath)));
SnapshotMetadata metadata{tip->GetBlockHash(), maybe_stats->coins_count, tip->nChainTx};
SnapshotMetadata metadata{tip->GetBlockHash(), maybe_stats->coins_count};
afile << metadata;
@ -2694,9 +2694,7 @@ UniValue CreateUTXOSnapshot(
result.pushKV("base_height", tip->nHeight);
result.pushKV("path", path.u8string());
result.pushKV("txoutset_hash", maybe_stats->hashSerialized.ToString());
// Cast required because univalue doesn't have serialization specified for
// `unsigned int`, nChainTx's type.
result.pushKV("nchaintx", uint64_t{tip->nChainTx});
result.pushKV("nchaintx", tip->nChainTx);
return result;
}

View file

@ -40,7 +40,7 @@ from test_framework.util import (
assert_equal,
assert_raises_rpc_error,
)
import struct
START_HEIGHT = 199
SNAPSHOT_BASE_HEIGHT = 299
@ -68,23 +68,35 @@ class AssumeutxoTest(BitcoinTestFramework):
def test_invalid_snapshot_scenarios(self, valid_snapshot_path):
self.log.info("Test different scenarios of loading invalid snapshot files")
self.log.info(" - snapshot file refering to a block that is not in the assumeutxo parameters")
with open(valid_snapshot_path, 'rb') as f:
valid_snapshot_contents = f.read()
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_path = valid_snapshot_path + '.mod'
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):
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 number of coins")
valid_num_coins = struct.unpack("<I", valid_snapshot_contents[32:32 + 4])[0]
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:])
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)
def run_test(self):
"""
Bring up two (disconnected) nodes, mine some new blocks on the first,