Skip to content

Commit 448790c

Browse files
committed
Merge bitcoin#28639: refactor: Remove unused nchaintx from SnapshotMetadata constructor, fix test, add test
fafde92 test: Check snapshot file with wrong number of coins (MarcoFalke) faa90f6 refactor: Remove unused nchaintx from SnapshotMetadata constructor (MarcoFalke) Pull request description: See commit messages ACKs for top commit: Sjors: utACK fafde92 theStack: ACK fafde92 Tree-SHA512: 9ed2720b50d1c0938f30543ba143e1a4c6af3a0ff166f8b3eb452e1d99ddee6e3443a4c99f77efe94b8c3eb2feff984bf5259807ee8085e1e0e1e0d1de98227e
2 parents 9e068f9 + fafde92 commit 448790c

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

src/node/utxo_snapshot.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ class SnapshotMetadata
3535
SnapshotMetadata() { }
3636
SnapshotMetadata(
3737
const uint256& base_blockhash,
38-
uint64_t coins_count,
39-
unsigned int nchaintx) :
38+
uint64_t coins_count) :
4039
m_base_blockhash(base_blockhash),
4140
m_coins_count(coins_count) { }
4241

src/rpc/blockchain.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2667,7 +2667,7 @@ UniValue CreateUTXOSnapshot(
26672667
tip->nHeight, tip->GetBlockHash().ToString(),
26682668
fs::PathToString(path), fs::PathToString(temppath)));
26692669

2670-
SnapshotMetadata metadata{tip->GetBlockHash(), maybe_stats->coins_count, tip->nChainTx};
2670+
SnapshotMetadata metadata{tip->GetBlockHash(), maybe_stats->coins_count};
26712671

26722672
afile << metadata;
26732673

@@ -2694,9 +2694,7 @@ UniValue CreateUTXOSnapshot(
26942694
result.pushKV("base_height", tip->nHeight);
26952695
result.pushKV("path", path.u8string());
26962696
result.pushKV("txoutset_hash", maybe_stats->hashSerialized.ToString());
2697-
// Cast required because univalue doesn't have serialization specified for
2698-
// `unsigned int`, nChainTx's type.
2699-
result.pushKV("nchaintx", uint64_t{tip->nChainTx});
2697+
result.pushKV("nchaintx", tip->nChainTx);
27002698
return result;
27012699
}
27022700

test/functional/feature_assumeutxo.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
assert_equal,
4141
assert_raises_rpc_error,
4242
)
43-
43+
import struct
4444

4545
START_HEIGHT = 199
4646
SNAPSHOT_BASE_HEIGHT = 299
@@ -68,23 +68,35 @@ def setup_network(self):
6868

6969
def test_invalid_snapshot_scenarios(self, valid_snapshot_path):
7070
self.log.info("Test different scenarios of loading invalid snapshot files")
71-
self.log.info(" - snapshot file refering to a block that is not in the assumeutxo parameters")
7271
with open(valid_snapshot_path, 'rb') as f:
7372
valid_snapshot_contents = f.read()
73+
bad_snapshot_path = valid_snapshot_path + '.mod'
7474

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

8484
expected_log = f"assumeutxo height in snapshot metadata not recognized ({bad_snapshot_height}) - refusing to load snapshot"
85-
with self.nodes[1].assert_debug_log(expected_log):
85+
with self.nodes[1].assert_debug_log([expected_log]):
8686
assert_raises_rpc_error(-32603, "Unable to load UTXO snapshot", self.nodes[1].loadtxoutset, bad_snapshot_path)
8787

88+
self.log.info(" - snapshot file with wrong number of coins")
89+
valid_num_coins = struct.unpack("<I", valid_snapshot_contents[32:32 + 4])[0]
90+
for off in [-1, +1]:
91+
with open(bad_snapshot_path, 'wb') as f:
92+
f.write(valid_snapshot_contents[:32])
93+
f.write(struct.pack("<I", valid_num_coins + off))
94+
f.write(valid_snapshot_contents[32 + 4:])
95+
96+
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"
97+
with self.nodes[1].assert_debug_log([expected_log]):
98+
assert_raises_rpc_error(-32603, "Unable to load UTXO snapshot", self.nodes[1].loadtxoutset, bad_snapshot_path)
99+
88100
def run_test(self):
89101
"""
90102
Bring up two (disconnected) nodes, mine some new blocks on the first,

0 commit comments

Comments
 (0)