Skip to content

Commit 1a19a4d

Browse files
committed
Merge bitcoin#29656: chainparams: Change nChainTx type to uint64_t
bf0efb4 scripted-diff: Modernize naming of nChainTx and nTxCount (Fabian Jahr) 72e5d1b test: Add basic check for nChainTx type (Fabian Jahr) dc2938e chainparams: Change nChainTx to uint64_t (Fabian Jahr) Pull request description: This picks up the work from bitcoin#29331 and closes bitcoin#29258. This simply changes the type and addresses the comments from bitcoin#29331 by changing the type in all relevant places and removing unnecessary casts. This also adds an extremely simple unit test. Additionally this modernizes the name of `nChainTx` which helps reviewers check all use of the symbol and can make silent merge conflicts. ACKs for top commit: maflcko: only rebase in scripted-diff, re-ACK bf0efb4 🔈 glozow: reACK bf0efb4 via range-diff Tree-SHA512: ee4020926d0800236fe655d0c7b127215ab36b553b04d5f91494f4b7fac6e1cfe7ee298b07c0983db5a3f4786932acaa54f5fd2ccd45f2fcdcfa13427358dc3b
2 parents 5d682d4 + bf0efb4 commit 1a19a4d

12 files changed

+75
-69
lines changed

src/chain.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ enum BlockStatus : uint32_t {
102102
*
103103
* If a block's validity is at least VALID_TRANSACTIONS, CBlockIndex::nTx will be set. If a block and all previous
104104
* blocks back to the genesis block or an assumeutxo snapshot block are at least VALID_TRANSACTIONS,
105-
* CBlockIndex::nChainTx will be set.
105+
* CBlockIndex::m_chain_tx_count will be set.
106106
*/
107107
BLOCK_VALID_TRANSACTIONS = 3,
108108

@@ -173,8 +173,7 @@ class CBlockIndex
173173
//! This value will be non-zero if this block and all previous blocks back
174174
//! to the genesis block or an assumeutxo snapshot block have reached the
175175
//! VALID_TRANSACTIONS level.
176-
//! Change to 64-bit type before 2024 (assuming worst case of 60 byte transactions).
177-
unsigned int nChainTx{0};
176+
uint64_t m_chain_tx_count{0};
178177

179178
//! Verification status of this block. See enum BlockStatus
180179
//!
@@ -254,10 +253,10 @@ class CBlockIndex
254253
* Does not imply the transactions are consensus-valid (ConnectTip might fail)
255254
* Does not imply the transactions are still stored on disk. (IsBlockPruned might return true)
256255
*
257-
* Note that this will be true for the snapshot base block, if one is loaded, since its nChainTx value will have
256+
* Note that this will be true for the snapshot base block, if one is loaded, since its m_chain_tx_count value will have
258257
* been set manually based on the related AssumeutxoData entry.
259258
*/
260-
bool HaveNumChainTxs() const { return nChainTx != 0; }
259+
bool HaveNumChainTxs() const { return m_chain_tx_count != 0; }
261260

262261
NodeSeconds Time() const
263262
{

src/kernel/chainparams.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ class CMainParams : public CChainParams {
180180
chainTxData = ChainTxData{
181181
// Data from RPC: getchaintxstats 4096 000000000000000000026811d149d4d261995ec5b3f64f439a0a10e1a464af9a
182182
.nTime = 1704194835,
183-
.nTxCount = 946728933,
183+
.tx_count = 946728933,
184184
.dTxRate = 6.569290261471664,
185185
};
186186
}
@@ -272,15 +272,15 @@ class CTestNetParams : public CChainParams {
272272
{
273273
.height = 2'500'000,
274274
.hash_serialized = AssumeutxoHash{uint256S("0xf841584909f68e47897952345234e37fcd9128cd818f41ee6c3ca68db8071be7")},
275-
.nChainTx = 66484552,
275+
.m_chain_tx_count = 66484552,
276276
.blockhash = uint256S("0x0000000000000093bcb68c03a9a168ae252572d348a2eaeba2cdf9231d73206f")
277277
}
278278
};
279279

280280
chainTxData = ChainTxData{
281281
// Data from RPC: getchaintxstats 4096 000000000001323071f38f21ea5aae529ece491eadaccce506a59bcc2d968917
282282
.nTime = 1703579240,
283-
.nTxCount = 67845391,
283+
.tx_count = 67845391,
284284
.dTxRate = 1.464436832560951,
285285
};
286286
}
@@ -312,7 +312,7 @@ class SigNetParams : public CChainParams {
312312
chainTxData = ChainTxData{
313313
// Data from RPC: getchaintxstats 4096 0000000870f15246ba23c16e370a7ffb1fc8a3dcf8cb4492882ed4b0e3d4cd26
314314
.nTime = 1706331472,
315-
.nTxCount = 2425380,
315+
.tx_count = 2425380,
316316
.dTxRate = 0.008277759863833788,
317317
};
318318
} else {
@@ -382,7 +382,7 @@ class SigNetParams : public CChainParams {
382382
{
383383
.height = 160'000,
384384
.hash_serialized = AssumeutxoHash{uint256S("0xfe0a44309b74d6b5883d246cb419c6221bcccf0b308c9b59b7d70783dbdf928a")},
385-
.nChainTx = 2289496,
385+
.m_chain_tx_count = 2289496,
386386
.blockhash = uint256S("0x0000003ca3c99aff040f2563c2ad8f8ec88bd0fd6b8f0895cfaf1ef90353a62c")
387387
}
388388
};
@@ -498,21 +498,21 @@ class CRegTestParams : public CChainParams
498498
{ // For use by unit tests
499499
.height = 110,
500500
.hash_serialized = AssumeutxoHash{uint256S("0x6657b736d4fe4db0cbc796789e812d5dba7f5c143764b1b6905612f1830609d1")},
501-
.nChainTx = 111,
501+
.m_chain_tx_count = 111,
502502
.blockhash = uint256S("0x696e92821f65549c7ee134edceeeeaaa4105647a3c4fd9f298c0aec0ab50425c")
503503
},
504504
{
505505
// For use by fuzz target src/test/fuzz/utxo_snapshot.cpp
506506
.height = 200,
507507
.hash_serialized = AssumeutxoHash{uint256S("0x4f34d431c3e482f6b0d67b64609ece3964dc8d7976d02ac68dd7c9c1421738f2")},
508-
.nChainTx = 201,
508+
.m_chain_tx_count = 201,
509509
.blockhash = uint256S("0x5e93653318f294fb5aa339d00bbf8cf1c3515488ad99412c37608b139ea63b27"),
510510
},
511511
{
512512
// For use by test/functional/feature_assumeutxo.py
513513
.height = 299,
514514
.hash_serialized = AssumeutxoHash{uint256S("0xa4bf3407ccb2cc0145c49ebba8fa91199f8a3903daf0883875941497d2493c27")},
515-
.nChainTx = 334,
515+
.m_chain_tx_count = 334,
516516
.blockhash = uint256S("0x3bb7ce5eba0be48939b7a521ac1ba9316afee2c7bada3a0cca24188e6d7d96c0")
517517
},
518518
};

src/kernel/chainparams.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ struct AssumeutxoData {
5050
//! The expected hash of the deserialized UTXO set.
5151
AssumeutxoHash hash_serialized;
5252

53-
//! Used to populate the nChainTx value, which is used during BlockManager::LoadBlockIndex().
53+
//! Used to populate the m_chain_tx_count value, which is used during BlockManager::LoadBlockIndex().
5454
//!
5555
//! We need to hardcode the value here because this is computed cumulatively using block data,
5656
//! which we do not necessarily have at the time of snapshot load.
57-
unsigned int nChainTx;
57+
uint64_t m_chain_tx_count;
5858

5959
//! The hash of the base block for this snapshot. Used to refer to assumeutxo data
6060
//! prior to having a loaded blockindex.
@@ -69,7 +69,7 @@ struct AssumeutxoData {
6969
*/
7070
struct ChainTxData {
7171
int64_t nTime; //!< UNIX timestamp of last known number of transactions
72-
int64_t nTxCount; //!< total number of transactions between genesis and that timestamp
72+
uint64_t tx_count; //!< total number of transactions between genesis and that timestamp
7373
double dTxRate; //!< estimated number of transactions per second after that timestamp
7474
};
7575

src/node/blockstorage.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -410,11 +410,11 @@ bool BlockManager::LoadBlockIndex(const std::optional<uint256>& snapshot_blockha
410410
m_snapshot_height = au_data.height;
411411
CBlockIndex* base{LookupBlockIndex(*snapshot_blockhash)};
412412

413-
// Since nChainTx (responsible for estimated progress) isn't persisted
413+
// Since m_chain_tx_count (responsible for estimated progress) isn't persisted
414414
// to disk, we must bootstrap the value for assumedvalid chainstates
415415
// from the hardcoded assumeutxo chainparams.
416-
base->nChainTx = au_data.nChainTx;
417-
LogPrintf("[snapshot] set nChainTx=%d for %s\n", au_data.nChainTx, snapshot_blockhash->ToString());
416+
base->m_chain_tx_count = au_data.m_chain_tx_count;
417+
LogPrintf("[snapshot] set m_chain_tx_count=%d for %s\n", au_data.m_chain_tx_count, snapshot_blockhash->ToString());
418418
} else {
419419
// If this isn't called with a snapshot blockhash, make sure the cached snapshot height
420420
// is null. This is relevant during snapshot completion, when the blockman may be loaded
@@ -449,15 +449,15 @@ bool BlockManager::LoadBlockIndex(const std::optional<uint256>& snapshot_blockha
449449
if (m_snapshot_height && pindex->nHeight == *m_snapshot_height &&
450450
pindex->GetBlockHash() == *snapshot_blockhash) {
451451
// Should have been set above; don't disturb it with code below.
452-
Assert(pindex->nChainTx > 0);
453-
} else if (pindex->pprev->nChainTx > 0) {
454-
pindex->nChainTx = pindex->pprev->nChainTx + pindex->nTx;
452+
Assert(pindex->m_chain_tx_count > 0);
453+
} else if (pindex->pprev->m_chain_tx_count > 0) {
454+
pindex->m_chain_tx_count = pindex->pprev->m_chain_tx_count + pindex->nTx;
455455
} else {
456-
pindex->nChainTx = 0;
456+
pindex->m_chain_tx_count = 0;
457457
m_blocks_unlinked.insert(std::make_pair(pindex->pprev, pindex));
458458
}
459459
} else {
460-
pindex->nChainTx = pindex->nTx;
460+
pindex->m_chain_tx_count = pindex->nTx;
461461
}
462462
}
463463
if (!(pindex->nStatus & BLOCK_FAILED_MASK) && pindex->pprev && (pindex->pprev->nStatus & BLOCK_FAILED_MASK)) {

src/rpc/blockchain.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,16 +1725,16 @@ static RPCHelpMan getchaintxstats()
17251725

17261726
UniValue ret(UniValue::VOBJ);
17271727
ret.pushKV("time", (int64_t)pindex->nTime);
1728-
if (pindex->nChainTx) {
1729-
ret.pushKV("txcount", pindex->nChainTx);
1728+
if (pindex->m_chain_tx_count) {
1729+
ret.pushKV("txcount", pindex->m_chain_tx_count);
17301730
}
17311731
ret.pushKV("window_final_block_hash", pindex->GetBlockHash().GetHex());
17321732
ret.pushKV("window_final_block_height", pindex->nHeight);
17331733
ret.pushKV("window_block_count", blockcount);
17341734
if (blockcount > 0) {
17351735
ret.pushKV("window_interval", nTimeDiff);
1736-
if (pindex->nChainTx != 0 && past_block.nChainTx != 0) {
1737-
const auto window_tx_count = pindex->nChainTx - past_block.nChainTx;
1736+
if (pindex->m_chain_tx_count != 0 && past_block.m_chain_tx_count != 0) {
1737+
const auto window_tx_count = pindex->m_chain_tx_count - past_block.m_chain_tx_count;
17381738
ret.pushKV("window_tx_count", window_tx_count);
17391739
if (nTimeDiff > 0) {
17401740
ret.pushKV("txrate", double(window_tx_count) / nTimeDiff);
@@ -2799,7 +2799,7 @@ UniValue CreateUTXOSnapshot(
27992799
result.pushKV("base_height", tip->nHeight);
28002800
result.pushKV("path", path.utf8string());
28012801
result.pushKV("txoutset_hash", maybe_stats->hashSerialized.ToString());
2802-
result.pushKV("nchaintx", tip->nChainTx);
2802+
result.pushKV("nchaintx", tip->m_chain_tx_count);
28032803
return result;
28042804
}
28052805

src/test/blockchain_tests.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,11 @@ BOOST_FIXTURE_TEST_CASE(get_prune_height, TestChain100Setup)
110110
CheckGetPruneHeight(blockman, chain, 100);
111111
}
112112

113+
BOOST_AUTO_TEST_CASE(num_chain_tx_max)
114+
{
115+
CBlockIndex block_index{};
116+
block_index.m_chain_tx_count = std::numeric_limits<uint64_t>::max();
117+
BOOST_CHECK_EQUAL(block_index.m_chain_tx_count, std::numeric_limits<uint64_t>::max());
118+
}
119+
113120
BOOST_AUTO_TEST_SUITE_END()

src/test/fuzz/utxo_snapshot.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ FUZZ_TARGET(utxo_snapshot, .init = initialize_chain)
113113
if (index->nHeight == chainman.GetSnapshotBaseHeight()) {
114114
auto params{chainman.GetParams().AssumeutxoForHeight(index->nHeight)};
115115
Assert(params.has_value());
116-
Assert(params.value().nChainTx == index->nChainTx);
116+
Assert(params.value().m_chain_tx_count == index->m_chain_tx_count);
117117
} else {
118-
Assert(index->nChainTx == 0);
118+
Assert(index->m_chain_tx_count == 0);
119119
}
120120
}
121121
Assert(g_chain->size() == coinscache.GetCacheSize());

src/test/pmt_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ BOOST_FIXTURE_TEST_SUITE(pmt_tests, BasicTestingSetup)
2929

3030
BOOST_AUTO_TEST_CASE(pmt_test1)
3131
{
32-
static const unsigned int nTxCounts[] = {1, 4, 7, 17, 56, 100, 127, 256, 312, 513, 1000, 4095};
32+
static const unsigned int tx_counts[] = {1, 4, 7, 17, 56, 100, 127, 256, 312, 513, 1000, 4095};
3333

3434
for (int i = 0; i < 12; i++) {
35-
unsigned int nTx = nTxCounts[i];
35+
unsigned int nTx = tx_counts[i];
3636

3737
// build a block with some dummy transactions
3838
CBlock block;

src/test/util/chainstate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ CreateAndActivateUTXOSnapshot(
9999
assert(pindex->IsValid(BlockStatus::BLOCK_VALID_TREE));
100100
pindex->nStatus = BlockStatus::BLOCK_VALID_TREE;
101101
pindex->nTx = 0;
102-
pindex->nChainTx = 0;
102+
pindex->m_chain_tx_count = 0;
103103
pindex->nSequenceId = 0;
104104
pindex = pindex->pprev;
105105
}

src/test/validation_chainstatemanager_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ struct SnapshotTestSetup : TestChain100Setup {
285285
const auto& au_data = ::Params().AssumeutxoForHeight(snapshot_height);
286286
const CBlockIndex* tip = WITH_LOCK(chainman.GetMutex(), return chainman.ActiveTip());
287287

288-
BOOST_CHECK_EQUAL(tip->nChainTx, au_data->nChainTx);
288+
BOOST_CHECK_EQUAL(tip->m_chain_tx_count, au_data->m_chain_tx_count);
289289

290290
// To be checked against later when we try loading a subsequent snapshot.
291291
uint256 loaded_snapshot_blockhash{*chainman.SnapshotBlockhash()};
@@ -465,7 +465,7 @@ BOOST_FIXTURE_TEST_CASE(chainstatemanager_loadblockindex, TestChain100Setup)
465465
if (i < last_assumed_valid_idx && i >= assumed_valid_start_idx) {
466466
index->nStatus = BlockStatus::BLOCK_VALID_TREE;
467467
index->nTx = 0;
468-
index->nChainTx = 0;
468+
index->m_chain_tx_count = 0;
469469
}
470470

471471
++num_indexes;

0 commit comments

Comments
 (0)