mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-01-19 06:43:45 +01:00
Merge bitcoin/bitcoin#30598: assumeutxo: Drop block height from metadata
00618e8745assumeutxo: Drop block height from metadata (Fabian Jahr) Pull request description: Fixes https://github.com/bitcoin/bitcoin/issues/30514 which has more context and shows how the issue can be reproduced. Since the value in question is removed, there is no test to add to reproduce anything. This is an alternative approach to #30516 with much of the [code being suggested there](https://github.com/bitcoin/bitcoin/pull/30516#discussion_r1689146902). ACKs for top commit: maflcko: re-ACK00618e8745🎌 achow101: ACK00618e8745theStack: Code-review ACK00618e8745ismaelsadeeq: Re-ACK00618e8745mzumsande: ACK00618e8745Tree-SHA512: db9575247bae838ad7742a27a216faaf55bb11e022f9afdd05752bb09bbf9614717d0ad64304ff5722a16bf41d8dea888af544e4ae26dcaa528c1add0269a4a8
This commit is contained in:
@@ -28,16 +28,17 @@ class Chainstate;
|
||||
namespace node {
|
||||
//! Metadata describing a serialized version of a UTXO set from which an
|
||||
//! assumeutxo Chainstate can be constructed.
|
||||
//! All metadata fields come from an untrusted file, so must be validated
|
||||
//! before being used. Thus, new fields should be added only if needed.
|
||||
class SnapshotMetadata
|
||||
{
|
||||
const uint16_t m_version{1};
|
||||
const std::set<uint16_t> m_supported_versions{1};
|
||||
inline static const uint16_t VERSION{2};
|
||||
const std::set<uint16_t> m_supported_versions{VERSION};
|
||||
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.
|
||||
uint256 m_base_blockhash;
|
||||
uint32_t m_base_blockheight;
|
||||
|
||||
|
||||
//! The number of coins in the UTXO set contained in this snapshot. Used
|
||||
@@ -50,19 +51,16 @@ public:
|
||||
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) { }
|
||||
|
||||
template <typename Stream>
|
||||
inline void Serialize(Stream& s) const {
|
||||
s << SNAPSHOT_MAGIC_BYTES;
|
||||
s << m_version;
|
||||
s << VERSION;
|
||||
s << m_network_magic;
|
||||
s << m_base_blockheight;
|
||||
s << m_base_blockhash;
|
||||
s << m_coins_count;
|
||||
}
|
||||
@@ -98,7 +96,6 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
s >> m_base_blockheight;
|
||||
s >> m_base_blockhash;
|
||||
s >> m_coins_count;
|
||||
}
|
||||
|
||||
@@ -2741,7 +2741,7 @@ UniValue CreateUTXOSnapshot(
|
||||
tip->nHeight, tip->GetBlockHash().ToString(),
|
||||
fs::PathToString(path), fs::PathToString(temppath)));
|
||||
|
||||
SnapshotMetadata metadata{chainstate.m_chainman.GetParams().MessageStart(), tip->GetBlockHash(), tip->nHeight, maybe_stats->coins_count};
|
||||
SnapshotMetadata metadata{chainstate.m_chainman.GetParams().MessageStart(), tip->GetBlockHash(), maybe_stats->coins_count};
|
||||
|
||||
afile << metadata;
|
||||
|
||||
@@ -2865,10 +2865,12 @@ static RPCHelpMan loadtxoutset()
|
||||
throw JSONRPCError(RPC_INTERNAL_ERROR, strprintf("Unable to load UTXO snapshot: %s. (%s)", util::ErrorString(activation_result).original, path.utf8string()));
|
||||
}
|
||||
|
||||
CBlockIndex& snapshot_index{*CHECK_NONFATAL(*activation_result)};
|
||||
|
||||
UniValue result(UniValue::VOBJ);
|
||||
result.pushKV("coins_loaded", metadata.m_coins_count);
|
||||
result.pushKV("tip_hash", metadata.m_base_blockhash.ToString());
|
||||
result.pushKV("base_height", metadata.m_base_blockheight);
|
||||
result.pushKV("tip_hash", snapshot_index.GetBlockHash().ToString());
|
||||
result.pushKV("base_height", snapshot_index.nHeight);
|
||||
result.pushKV("path", fs::PathToString(path));
|
||||
return result;
|
||||
},
|
||||
|
||||
@@ -57,7 +57,7 @@ FUZZ_TARGET(utxo_snapshot, .init = initialize_chain)
|
||||
int base_blockheight{fuzzed_data_provider.ConsumeIntegralInRange<int>(1, 2 * COINBASE_MATURITY)};
|
||||
uint256 base_blockhash{g_chain->at(base_blockheight - 1)->GetHash()};
|
||||
uint64_t m_coins_count{fuzzed_data_provider.ConsumeIntegralInRange<uint64_t>(1, 3 * COINBASE_MATURITY)};
|
||||
SnapshotMetadata metadata{msg_start, base_blockhash, base_blockheight, m_coins_count};
|
||||
SnapshotMetadata metadata{msg_start, base_blockhash, m_coins_count};
|
||||
outfile << metadata;
|
||||
}
|
||||
// Coins
|
||||
|
||||
@@ -5672,31 +5672,31 @@ Chainstate& ChainstateManager::InitializeChainstate(CTxMemPool* mempool)
|
||||
return destroyed && !fs::exists(db_path);
|
||||
}
|
||||
|
||||
util::Result<void> ChainstateManager::ActivateSnapshot(
|
||||
util::Result<CBlockIndex*> ChainstateManager::ActivateSnapshot(
|
||||
AutoFile& coins_file,
|
||||
const SnapshotMetadata& metadata,
|
||||
bool in_memory)
|
||||
{
|
||||
uint256 base_blockhash = metadata.m_base_blockhash;
|
||||
int base_blockheight = metadata.m_base_blockheight;
|
||||
|
||||
if (this->SnapshotBlockhash()) {
|
||||
return util::Error{Untranslated("Can't activate a snapshot-based chainstate more than once")};
|
||||
}
|
||||
|
||||
CBlockIndex* snapshot_start_block{};
|
||||
|
||||
{
|
||||
LOCK(::cs_main);
|
||||
|
||||
if (!GetParams().AssumeutxoForBlockhash(base_blockhash).has_value()) {
|
||||
auto available_heights = GetParams().GetAvailableSnapshotHeights();
|
||||
std::string heights_formatted = util::Join(available_heights, ", ", [&](const auto& i) { return util::ToString(i); });
|
||||
return util::Error{strprintf(Untranslated("assumeutxo block hash in snapshot metadata not recognized (hash: %s, height: %s). The following snapshot heights are available: %s"),
|
||||
return util::Error{strprintf(Untranslated("assumeutxo block hash in snapshot metadata not recognized (hash: %s). The following snapshot heights are available: %s"),
|
||||
base_blockhash.ToString(),
|
||||
base_blockheight,
|
||||
heights_formatted)};
|
||||
}
|
||||
|
||||
CBlockIndex* snapshot_start_block = m_blockman.LookupBlockIndex(base_blockhash);
|
||||
snapshot_start_block = m_blockman.LookupBlockIndex(base_blockhash);
|
||||
if (!snapshot_start_block) {
|
||||
return util::Error{strprintf(Untranslated("The base block header (%s) must appear in the headers chain. Make sure all headers are syncing, and call loadtxoutset again"),
|
||||
base_blockhash.ToString())};
|
||||
@@ -5707,7 +5707,7 @@ util::Result<void> ChainstateManager::ActivateSnapshot(
|
||||
return util::Error{strprintf(Untranslated("The base block header (%s) is part of an invalid chain"), base_blockhash.ToString())};
|
||||
}
|
||||
|
||||
if (!m_best_header || m_best_header->GetAncestor(base_blockheight) != snapshot_start_block) {
|
||||
if (!m_best_header || m_best_header->GetAncestor(snapshot_start_block->nHeight) != snapshot_start_block) {
|
||||
return util::Error{Untranslated("A forked headers-chain with more work than the chain with the snapshot base block header exists. Please proceed to sync without AssumeUtxo.")};
|
||||
}
|
||||
|
||||
@@ -5821,7 +5821,7 @@ util::Result<void> ChainstateManager::ActivateSnapshot(
|
||||
m_snapshot_chainstate->CoinsTip().DynamicMemoryUsage() / (1000 * 1000));
|
||||
|
||||
this->MaybeRebalanceCaches();
|
||||
return {};
|
||||
return snapshot_start_block;
|
||||
}
|
||||
|
||||
static void FlushSnapshotToDisk(CCoinsViewCache& coins_cache, bool snapshot_loaded)
|
||||
|
||||
@@ -1098,7 +1098,7 @@ public:
|
||||
//! faking nTx* block index data along the way.
|
||||
//! - Move the new chainstate to `m_snapshot_chainstate` and make it our
|
||||
//! ChainstateActive().
|
||||
[[nodiscard]] util::Result<void> ActivateSnapshot(
|
||||
[[nodiscard]] util::Result<CBlockIndex*> ActivateSnapshot(
|
||||
AutoFile& coins_file, const node::SnapshotMetadata& metadata, bool in_memory);
|
||||
|
||||
//! Once the background validation chainstate has reached the height which
|
||||
|
||||
Reference in New Issue
Block a user