Skip to content

Commit 6b60848

Browse files
committed
assumeutxo: Add network magic ctor param to SnapshotMetadata
This prevents SnapshotMetadata from using any globals implicitly.
1 parent 1f1f998 commit 6b60848

File tree

7 files changed

+22
-12
lines changed

7 files changed

+22
-12
lines changed

src/kernel/chainparams.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ std::vector<int> CChainParams::GetAvailableSnapshotHeights() const
554554
return heights;
555555
}
556556

557-
std::optional<ChainType> GetNetworkForMagic(MessageStartChars& message)
557+
std::optional<ChainType> GetNetworkForMagic(const MessageStartChars& message)
558558
{
559559
const auto mainnet_msg = CChainParams::Main()->MessageStart();
560560
const auto testnet_msg = CChainParams::TestNet()->MessageStart();

src/kernel/chainparams.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,6 @@ class CChainParams
184184
ChainTxData chainTxData;
185185
};
186186

187-
std::optional<ChainType> GetNetworkForMagic(MessageStartChars& pchMessageStart);
187+
std::optional<ChainType> GetNetworkForMagic(const MessageStartChars& pchMessageStart);
188188

189189
#endif // BITCOIN_KERNEL_CHAINPARAMS_H

src/node/utxo_snapshot.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <sync.h>
1414
#include <uint256.h>
1515
#include <util/chaintype.h>
16+
#include <util/check.h>
1617
#include <util/fs.h>
1718

1819
#include <cstdint>
@@ -31,6 +32,7 @@ class SnapshotMetadata
3132
{
3233
const uint16_t m_version{1};
3334
const std::set<uint16_t> m_supported_versions{1};
35+
const MessageStartChars m_network_magic;
3436
public:
3537
//! The hash of the block that reflects the tip of the chain for the
3638
//! UTXO set contained in this snapshot.
@@ -42,11 +44,15 @@ class SnapshotMetadata
4244
//! during snapshot load to estimate progress of UTXO set reconstruction.
4345
uint64_t m_coins_count = 0;
4446

45-
SnapshotMetadata() { }
4647
SnapshotMetadata(
48+
const MessageStartChars network_magic) :
49+
m_network_magic(network_magic) { }
50+
SnapshotMetadata(
51+
const MessageStartChars network_magic,
4752
const uint256& base_blockhash,
4853
const int base_blockheight,
4954
uint64_t coins_count) :
55+
m_network_magic(network_magic),
5056
m_base_blockhash(base_blockhash),
5157
m_base_blockheight(base_blockheight),
5258
m_coins_count(coins_count) { }
@@ -55,7 +61,7 @@ class SnapshotMetadata
5561
inline void Serialize(Stream& s) const {
5662
s << SNAPSHOT_MAGIC_BYTES;
5763
s << m_version;
58-
s << Params().MessageStart();
64+
s << m_network_magic;
5965
s << m_base_blockheight;
6066
s << m_base_blockhash;
6167
s << m_coins_count;
@@ -80,11 +86,13 @@ class SnapshotMetadata
8086
// Read the network magic (pchMessageStart)
8187
MessageStartChars message;
8288
s >> message;
83-
if (!std::equal(message.begin(), message.end(), Params().MessageStart().data())) {
84-
auto metadata_network = GetNetworkForMagic(message);
89+
if (!std::equal(message.begin(), message.end(), m_network_magic.data())) {
90+
auto metadata_network{GetNetworkForMagic(message)};
8591
if (metadata_network) {
8692
std::string network_string{ChainTypeToString(metadata_network.value())};
87-
throw std::ios_base::failure(strprintf("The network of the snapshot (%s) does not match the network of this node (%s).", network_string, Params().GetChainTypeString()));
93+
auto node_network{GetNetworkForMagic(m_network_magic)};
94+
std::string node_network_string{ChainTypeToString(node_network.value())};
95+
throw std::ios_base::failure(strprintf("The network of the snapshot (%s) does not match the network of this node (%s).", network_string, node_network_string));
8896
} else {
8997
throw std::ios_base::failure("This snapshot has been created for an unrecognized network. This could be a custom signet, a new testnet or possibly caused by data corruption.");
9098
}

src/rpc/blockchain.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2697,7 +2697,7 @@ UniValue CreateUTXOSnapshot(
26972697
tip->nHeight, tip->GetBlockHash().ToString(),
26982698
fs::PathToString(path), fs::PathToString(temppath)));
26992699

2700-
SnapshotMetadata metadata{tip->GetBlockHash(), tip->nHeight, maybe_stats->coins_count};
2700+
SnapshotMetadata metadata{chainstate.m_chainman.GetParams().MessageStart(), tip->GetBlockHash(), tip->nHeight, maybe_stats->coins_count};
27012701

27022702
afile << metadata;
27032703

@@ -2809,7 +2809,7 @@ static RPCHelpMan loadtxoutset()
28092809
"Couldn't open file " + path.utf8string() + " for reading.");
28102810
}
28112811

2812-
SnapshotMetadata metadata;
2812+
SnapshotMetadata metadata{chainman.GetParams().MessageStart()};
28132813
try {
28142814
afile >> metadata;
28152815
} catch (const std::ios_base::failure& e) {

src/test/fuzz/deserialize.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,8 @@ FUZZ_TARGET_DESERIALIZE(blocktransactionsrequest_deserialize, {
316316
DeserializeFromFuzzingInput(buffer, btr);
317317
})
318318
FUZZ_TARGET_DESERIALIZE(snapshotmetadata_deserialize, {
319-
SnapshotMetadata snapshot_metadata;
319+
auto msg_start = Params().MessageStart();
320+
SnapshotMetadata snapshot_metadata{msg_start};
320321
DeserializeFromFuzzingInput(buffer, snapshot_metadata);
321322
})
322323
FUZZ_TARGET_DESERIALIZE(uint160_deserialize, {

src/test/fuzz/utxo_snapshot.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ FUZZ_TARGET(utxo_snapshot, .init = initialize_chain)
4747

4848
const auto ActivateFuzzedSnapshot{[&] {
4949
AutoFile infile{fsbridge::fopen(snapshot_path, "rb")};
50-
SnapshotMetadata metadata;
50+
auto msg_start = Params().MessageStart();
51+
SnapshotMetadata metadata{msg_start};
5152
try {
5253
infile >> metadata;
5354
} catch (const std::ios_base::failure&) {

src/test/util/chainstate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ CreateAndActivateUTXOSnapshot(
5656
//
5757
FILE* infile{fsbridge::fopen(snapshot_path, "rb")};
5858
AutoFile auto_infile{infile};
59-
node::SnapshotMetadata metadata;
59+
node::SnapshotMetadata metadata{node.chainman->GetParams().MessageStart()};
6060
auto_infile >> metadata;
6161

6262
malleation(auto_infile, metadata);

0 commit comments

Comments
 (0)