Skip to content

Commit

Permalink
assumeutxo: Add network magic ctor param to SnapshotMetadata
Browse files Browse the repository at this point in the history
This prevents SnapshotMetadata from using any globals implicitly.
  • Loading branch information
fjahr committed May 24, 2024
1 parent d4856bc commit fe195cf
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/kernel/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ std::vector<int> CChainParams::GetAvailableSnapshotHeights() const
return heights;
}

std::optional<ChainType> GetNetworkForMagic(MessageStartChars& message)
std::optional<ChainType> GetNetworkForMagic(const MessageStartChars& message)
{
const auto mainnet_msg = CChainParams::Main()->MessageStart();
const auto testnet_msg = CChainParams::TestNet()->MessageStart();
Expand Down
2 changes: 1 addition & 1 deletion src/kernel/chainparams.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,6 @@ class CChainParams
ChainTxData chainTxData;
};

std::optional<ChainType> GetNetworkForMagic(MessageStartChars& pchMessageStart);
std::optional<ChainType> GetNetworkForMagic(const MessageStartChars& pchMessageStart);

#endif // BITCOIN_KERNEL_CHAINPARAMS_H
19 changes: 14 additions & 5 deletions src/node/utxo_snapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <sync.h>
#include <uint256.h>
#include <util/chaintype.h>
#include <util/check.h>
#include <util/fs.h>

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

SnapshotMetadata() { }
SnapshotMetadata(
const MessageStartChars network_magic) :
m_network_magic(network_magic) { }
SnapshotMetadata(
const MessageStartChars network_magic,
const uint256& base_blockhash,
const int base_blockheight,
uint64_t coins_count) :
m_network_magic(network_magic),
m_base_blockhash(base_blockhash),
m_base_blockheight(base_blockheight),
m_coins_count(coins_count) { }
Expand All @@ -55,7 +61,7 @@ class SnapshotMetadata
inline void Serialize(Stream& s) const {
s << SNAPSHOT_MAGIC_BYTES;
s << m_version;
s << Params().MessageStart();
s << m_network_magic;
s << m_base_blockheight;
s << m_base_blockhash;
s << m_coins_count;
Expand All @@ -80,11 +86,14 @@ class SnapshotMetadata
// Read the network magic (pchMessageStart)
MessageStartChars message;
s >> message;
if (!std::equal(message.begin(), message.end(), Params().MessageStart().data())) {
auto metadata_network = GetNetworkForMagic(message);
if (!std::equal(message.begin(), message.end(), m_network_magic.data())) {
auto metadata_network{GetNetworkForMagic(message)};
if (metadata_network) {
std::string network_string{ChainTypeToString(metadata_network.value())};
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()));
auto node_network{GetNetworkForMagic(m_network_magic)};
Assert(node_network);
std::string node_network_string{ChainTypeToString(node_network.value())};
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));
} else {
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.");
}
Expand Down
4 changes: 2 additions & 2 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2697,7 +2697,7 @@ UniValue CreateUTXOSnapshot(
tip->nHeight, tip->GetBlockHash().ToString(),
fs::PathToString(path), fs::PathToString(temppath)));

SnapshotMetadata metadata{tip->GetBlockHash(), tip->nHeight, maybe_stats->coins_count};
SnapshotMetadata metadata{Params().MessageStart(), tip->GetBlockHash(), tip->nHeight, maybe_stats->coins_count};

afile << metadata;

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

SnapshotMetadata metadata;
SnapshotMetadata metadata{Params().MessageStart()};
try {
afile >> metadata;
} catch (const std::ios_base::failure& e) {
Expand Down
3 changes: 2 additions & 1 deletion src/test/fuzz/deserialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,8 @@ FUZZ_TARGET_DESERIALIZE(blocktransactionsrequest_deserialize, {
DeserializeFromFuzzingInput(buffer, btr);
})
FUZZ_TARGET_DESERIALIZE(snapshotmetadata_deserialize, {
SnapshotMetadata snapshot_metadata;
auto msg_start = Params().MessageStart();
SnapshotMetadata snapshot_metadata{msg_start};
DeserializeFromFuzzingInput(buffer, snapshot_metadata);
})
FUZZ_TARGET_DESERIALIZE(uint160_deserialize, {
Expand Down
3 changes: 2 additions & 1 deletion src/test/fuzz/utxo_snapshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ FUZZ_TARGET(utxo_snapshot, .init = initialize_chain)

const auto ActivateFuzzedSnapshot{[&] {
AutoFile infile{fsbridge::fopen(snapshot_path, "rb")};
SnapshotMetadata metadata;
auto msg_start = Params().MessageStart();
SnapshotMetadata metadata{msg_start};
try {
infile >> metadata;
} catch (const std::ios_base::failure&) {
Expand Down
2 changes: 1 addition & 1 deletion src/test/util/chainstate.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ CreateAndActivateUTXOSnapshot(
//
FILE* infile{fsbridge::fopen(snapshot_path, "rb")};
AutoFile auto_infile{infile};
node::SnapshotMetadata metadata;
node::SnapshotMetadata metadata{Params().MessageStart()};
auto_infile >> metadata;

malleation(auto_infile, metadata);
Expand Down

0 comments on commit fe195cf

Please sign in to comment.