Skip to content

Commit 434495a

Browse files
committed
chainparams: add blockhash to AssumeutxoData
This allows us to reference assumeutxo configuration by blockhash as well as height; this is helpful in future changes when we want to reference assumeutxo configurations before the block index is loaded.
1 parent c711ca1 commit 434495a

File tree

7 files changed

+49
-49
lines changed

7 files changed

+49
-49
lines changed

src/kernel/chainparams.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ class CMainParams : public CChainParams {
172172
}
173173
};
174174

175-
m_assumeutxo_data = MapAssumeutxo{
176-
// TODO to be specified in a future patch.
175+
m_assumeutxo_data = {
176+
// TODO to be specified in a future patch.
177177
};
178178

179179
chainTxData = ChainTxData{
@@ -266,7 +266,7 @@ class CTestNetParams : public CChainParams {
266266
}
267267
};
268268

269-
m_assumeutxo_data = MapAssumeutxo{
269+
m_assumeutxo_data = {
270270
// TODO to be specified in a future patch.
271271
};
272272

@@ -477,14 +477,12 @@ class CRegTestParams : public CChainParams
477477
}
478478
};
479479

480-
m_assumeutxo_data = MapAssumeutxo{
481-
{
482-
110,
483-
{AssumeutxoHash{uint256S("0x1ebbf5850204c0bdb15bf030f47c7fe91d45c44c712697e4509ba67adb01c618")}, 110},
484-
},
480+
m_assumeutxo_data = {
485481
{
486-
200,
487-
{AssumeutxoHash{uint256S("0x51c8d11d8b5c1de51543c579736e786aa2736206d1e11e627568029ce092cf62")}, 200},
482+
.height = 110,
483+
.hash_serialized = AssumeutxoHash{uint256S("0x1ebbf5850204c0bdb15bf030f47c7fe91d45c44c712697e4509ba67adb01c618")},
484+
.nChainTx = 110,
485+
.blockhash = uint256S("0x696e92821f65549c7ee134edceeeeaaa4105647a3c4fd9f298c0aec0ab50425c")
488486
},
489487
};
490488

src/kernel/chainparams.h

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <uint256.h>
1313
#include <util/chaintype.h>
1414
#include <util/hash_type.h>
15+
#include <util/vector.h>
1516

1617
#include <cstdint>
1718
#include <iterator>
@@ -44,17 +45,21 @@ struct AssumeutxoHash : public BaseHash<uint256> {
4445
* as valid.
4546
*/
4647
struct AssumeutxoData {
48+
int height;
49+
4750
//! The expected hash of the deserialized UTXO set.
48-
const AssumeutxoHash hash_serialized;
51+
AssumeutxoHash hash_serialized;
4952

5053
//! Used to populate the nChainTx value, which is used during BlockManager::LoadBlockIndex().
5154
//!
5255
//! We need to hardcode the value here because this is computed cumulatively using block data,
5356
//! which we do not necessarily have at the time of snapshot load.
54-
const unsigned int nChainTx;
55-
};
57+
unsigned int nChainTx;
5658

57-
using MapAssumeutxo = std::map<int, const AssumeutxoData>;
59+
//! The hash of the base block for this snapshot. Used to refer to assumeutxo data
60+
//! prior to having a loaded blockindex.
61+
uint256 blockhash;
62+
};
5863

5964
/**
6065
* Holds various statistics on transactions within a chain. Used to estimate
@@ -114,9 +119,14 @@ class CChainParams
114119
const std::vector<uint8_t>& FixedSeeds() const { return vFixedSeeds; }
115120
const CCheckpointData& Checkpoints() const { return checkpointData; }
116121

117-
//! Get allowed assumeutxo configuration.
118-
//! @see ChainstateManager
119-
const MapAssumeutxo& Assumeutxo() const { return m_assumeutxo_data; }
122+
std::optional<AssumeutxoData> AssumeutxoForHeight(int height) const
123+
{
124+
return FindFirst(m_assumeutxo_data, [&](const auto& d) { return d.height == height; });
125+
}
126+
std::optional<AssumeutxoData> AssumeutxoForBlockhash(const uint256& blockhash) const
127+
{
128+
return FindFirst(m_assumeutxo_data, [&](const auto& d) { return d.blockhash == blockhash; });
129+
}
120130

121131
const ChainTxData& TxData() const { return chainTxData; }
122132

@@ -169,7 +179,7 @@ class CChainParams
169179
bool fDefaultConsistencyChecks;
170180
bool m_is_mockable_chain;
171181
CCheckpointData checkpointData;
172-
MapAssumeutxo m_assumeutxo_data;
182+
std::vector<AssumeutxoData> m_assumeutxo_data;
173183
ChainTxData chainTxData;
174184
};
175185

src/test/validation_chainstatemanager_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,10 @@ struct SnapshotTestSetup : TestChain100Setup {
289289
BOOST_CHECK(!chainman.ActiveChain().Genesis()->IsAssumedValid());
290290
}
291291

292-
const AssumeutxoData& au_data = *ExpectedAssumeutxo(snapshot_height, ::Params());
292+
const auto& au_data = ::Params().AssumeutxoForHeight(snapshot_height);
293293
const CBlockIndex* tip = WITH_LOCK(chainman.GetMutex(), return chainman.ActiveTip());
294294

295-
BOOST_CHECK_EQUAL(tip->nChainTx, au_data.nChainTx);
295+
BOOST_CHECK_EQUAL(tip->nChainTx, au_data->nChainTx);
296296

297297
// To be checked against later when we try loading a subsequent snapshot.
298298
uint256 loaded_snapshot_blockhash{*chainman.SnapshotBlockhash()};

src/test/validation_tests.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,17 @@ BOOST_AUTO_TEST_CASE(test_assumeutxo)
132132
std::vector<int> bad_heights{0, 100, 111, 115, 209, 211};
133133

134134
for (auto empty : bad_heights) {
135-
const auto out = ExpectedAssumeutxo(empty, *params);
135+
const auto out = params->AssumeutxoForHeight(empty);
136136
BOOST_CHECK(!out);
137137
}
138138

139-
const auto out110 = *ExpectedAssumeutxo(110, *params);
139+
const auto out110 = *params->AssumeutxoForHeight(110);
140140
BOOST_CHECK_EQUAL(out110.hash_serialized.ToString(), "1ebbf5850204c0bdb15bf030f47c7fe91d45c44c712697e4509ba67adb01c618");
141141
BOOST_CHECK_EQUAL(out110.nChainTx, 110U);
142142

143-
const auto out210 = *ExpectedAssumeutxo(200, *params);
144-
BOOST_CHECK_EQUAL(out210.hash_serialized.ToString(), "51c8d11d8b5c1de51543c579736e786aa2736206d1e11e627568029ce092cf62");
145-
BOOST_CHECK_EQUAL(out210.nChainTx, 200U);
143+
const auto out110_2 = *params->AssumeutxoForBlockhash(uint256S("0x696e92821f65549c7ee134edceeeeaaa4105647a3c4fd9f298c0aec0ab50425c"));
144+
BOOST_CHECK_EQUAL(out110_2.hash_serialized.ToString(), "1ebbf5850204c0bdb15bf030f47c7fe91d45c44c712697e4509ba67adb01c618");
145+
BOOST_CHECK_EQUAL(out110_2.nChainTx, 110U);
146146
}
147147

148148
BOOST_AUTO_TEST_SUITE_END()

src/util/vector.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
#ifndef BITCOIN_UTIL_VECTOR_H
66
#define BITCOIN_UTIL_VECTOR_H
77

8+
#include <functional>
89
#include <initializer_list>
10+
#include <optional>
911
#include <type_traits>
1012
#include <utility>
1113
#include <vector>
@@ -67,4 +69,15 @@ inline void ClearShrink(V& v) noexcept
6769
V{}.swap(v);
6870
}
6971

72+
template<typename V, typename L>
73+
inline std::optional<V> FindFirst(const std::vector<V>& vec, const L fnc)
74+
{
75+
for (const auto& el : vec) {
76+
if (fnc(el)) {
77+
return el;
78+
}
79+
}
80+
return std::nullopt;
81+
}
82+
7083
#endif // BITCOIN_UTIL_VECTOR_H

src/validation.cpp

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5099,18 +5099,6 @@ Chainstate& ChainstateManager::InitializeChainstate(CTxMemPool* mempool)
50995099
return *m_active_chainstate;
51005100
}
51015101

5102-
const AssumeutxoData* ExpectedAssumeutxo(
5103-
const int height, const CChainParams& chainparams)
5104-
{
5105-
const MapAssumeutxo& valid_assumeutxos_map = chainparams.Assumeutxo();
5106-
const auto assumeutxo_found = valid_assumeutxos_map.find(height);
5107-
5108-
if (assumeutxo_found != valid_assumeutxos_map.end()) {
5109-
return &assumeutxo_found->second;
5110-
}
5111-
return nullptr;
5112-
}
5113-
51145102
[[nodiscard]] static bool DeleteCoinsDBFromDisk(const fs::path db_path, bool is_snapshot)
51155103
EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
51165104
{
@@ -5295,15 +5283,15 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
52955283
CBlockIndex* snapshot_start_block = WITH_LOCK(::cs_main, return m_blockman.LookupBlockIndex(base_blockhash));
52965284

52975285
if (!snapshot_start_block) {
5298-
// Needed for ComputeUTXOStats and ExpectedAssumeutxo to determine the
5286+
// Needed for ComputeUTXOStats to determine the
52995287
// height and to avoid a crash when base_blockhash.IsNull()
53005288
LogPrintf("[snapshot] Did not find snapshot start blockheader %s\n",
53015289
base_blockhash.ToString());
53025290
return false;
53035291
}
53045292

53055293
int base_height = snapshot_start_block->nHeight;
5306-
auto maybe_au_data = ExpectedAssumeutxo(base_height, GetParams());
5294+
const auto& maybe_au_data = GetParams().AssumeutxoForHeight(base_height);
53075295

53085296
if (!maybe_au_data) {
53095297
LogPrintf("[snapshot] assumeutxo height in snapshot metadata not recognized "
@@ -5572,7 +5560,7 @@ SnapshotCompletionResult ChainstateManager::MaybeCompleteSnapshotValidation()
55725560
CCoinsViewDB& ibd_coins_db = m_ibd_chainstate->CoinsDB();
55735561
m_ibd_chainstate->ForceFlushStateToDisk();
55745562

5575-
auto maybe_au_data = ExpectedAssumeutxo(curr_height, m_options.chainparams);
5563+
const auto& maybe_au_data = m_options.chainparams.AssumeutxoForHeight(curr_height);
55765564
if (!maybe_au_data) {
55775565
LogPrintf("[snapshot] assumeutxo data not found for height "
55785566
"(%d) - refusing to validate snapshot\n", curr_height);

src/validation.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,15 +1242,6 @@ bool DeploymentEnabled(const ChainstateManager& chainman, DEP dep)
12421242
return DeploymentEnabled(chainman.GetConsensus(), dep);
12431243
}
12441244

1245-
/**
1246-
* Return the expected assumeutxo value for a given height, if one exists.
1247-
*
1248-
* @param[in] height Get the assumeutxo value for this height.
1249-
*
1250-
* @returns empty if no assumeutxo configuration exists for the given height.
1251-
*/
1252-
const AssumeutxoData* ExpectedAssumeutxo(const int height, const CChainParams& params);
1253-
12541245
/** Identifies blocks that overwrote an existing coinbase output in the UTXO set (see BIP30) */
12551246
bool IsBIP30Repeat(const CBlockIndex& block_index);
12561247

0 commit comments

Comments
 (0)