assumeutxo: Add network magic ctor param to SnapshotMetadata

This prevents SnapshotMetadata from using any globals implicitly.
This commit is contained in:
Fabian Jahr
2024-05-24 11:41:42 +02:00
parent 1f1f998455
commit 6b6084850b
7 changed files with 22 additions and 12 deletions

View File

@@ -13,6 +13,7 @@
#include <sync.h>
#include <uint256.h>
#include <util/chaintype.h>
#include <util/check.h>
#include <util/fs.h>
#include <cstdint>
@@ -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.
@@ -42,11 +44,15 @@ public:
//! 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) { }
@@ -55,7 +61,7 @@ public:
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;
@@ -80,11 +86,13 @@ public:
// 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)};
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.");
}