mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-06-04 02:02:42 +02:00
Merge bitcoin/bitcoin#32631: refactor: Convert GenTxid to std::variant
a60f863d3escripted-diff: Replace GenTxidVariant with GenTxid (marcofleon)c8ba199598Remove old GenTxid class (marcofleon)072a198ea4Convert remaining instances of GenTxid to GenTxidVariant (marcofleon)1b528391c7Convert `txrequest` to GenTxidVariant (marcofleon)bde4579b07Convert `txdownloadman_impl` to GenTxidVariant (marcofleon)c876a892ecReplace GenTxid with Txid/Wtxid overloads in `txmempool` (marcofleon)de858ce2bemove-only: make GetInfo a private CTxMemPool member (stickies-v)eee473d9f3Convert `CompareInvMempoolOrder` to GenTxidVariant (marcofleon)243553d590refactor: replace get_iter_from_wtxid with GetIter(const Wtxid&) (stickies-v)fcf92fd640refactor: make CTxMemPool::GetIter strongly typed (marcofleon)11d28f21bbImplement GenTxid as a variant (marcofleon) Pull request description: Part of the [type safety refactor](https://github.com/bitcoin/bitcoin/pull/32189). This PR changes the GenTxid class to a variant, which holds both Txids and Wtxids. This provides compile-time type safety and eliminates the manual type check (bool m_is_wtxid). Variables that can be either a Txid or a Wtxid are now using the new GenTxid variant, instead of uint256. ACKs for top commit: w0xlt: ACKa60f863d3edergoegge: Code review ACKa60f863d3emaflcko: review ACKa60f863d3e🎽 theStack: Code-review ACKa60f863d3eTree-SHA512: da9b73b7bdffee2eb9281a409205519ac330d3336094d17681896703fbca8099608782c9c85801e388e4d90af5af8abf1f34931f57bbbe6e9674d802d6066047
This commit is contained in:
@@ -173,7 +173,7 @@ FUZZ_TARGET(mini_miner_selection, .init = initialize_miner)
|
||||
if (!pool.GetConflictTx(coin)) outpoints.push_back(coin);
|
||||
}
|
||||
for (const auto& tx : transactions) {
|
||||
assert(pool.exists(GenTxid::Txid(tx->GetHash())));
|
||||
assert(pool.exists(tx->GetHash()));
|
||||
for (uint32_t n{0}; n < tx->vout.size(); ++n) {
|
||||
COutPoint coin{tx->GetHash(), n};
|
||||
if (!pool.GetConflictTx(coin)) outpoints.push_back(coin);
|
||||
|
||||
@@ -311,8 +311,8 @@ FUZZ_TARGET(ephemeral_package_eval, .init = initialize_tx_pool)
|
||||
const auto delta = fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(-50 * COIN, +50 * COIN);
|
||||
// We only prioritise out of mempool transactions since PrioritiseTransaction doesn't
|
||||
// filter for ephemeral dust
|
||||
if (tx_pool.exists(GenTxid::Txid(txid))) {
|
||||
const auto tx_info{tx_pool.info(GenTxid::Txid(txid))};
|
||||
if (tx_pool.exists(txid)) {
|
||||
const auto tx_info{tx_pool.info(txid)};
|
||||
if (GetDust(*tx_info.tx, tx_pool.m_opts.dust_relay_feerate).empty()) {
|
||||
tx_pool.PrioritiseTransaction(txid.ToUint256(), delta);
|
||||
}
|
||||
|
||||
@@ -79,7 +79,7 @@ FUZZ_TARGET(partially_downloaded_block, .init = initialize_pdb)
|
||||
available.insert(i);
|
||||
}
|
||||
|
||||
if (add_to_mempool && !pool.exists(GenTxid::Txid(tx->GetHash()))) {
|
||||
if (add_to_mempool && !pool.exists(tx->GetHash())) {
|
||||
LOCK2(cs_main, pool.cs);
|
||||
AddToMempool(pool, ConsumeTxMemPoolEntry(fuzzed_data_provider, *tx));
|
||||
available.insert(i);
|
||||
|
||||
@@ -316,8 +316,8 @@ FUZZ_TARGET(tx_pool_standard, .init = initialize_tx_pool)
|
||||
node.validation_signals->SyncWithValidationInterfaceQueue();
|
||||
node.validation_signals->UnregisterSharedValidationInterface(txr);
|
||||
|
||||
bool txid_in_mempool = tx_pool.exists(GenTxid::Txid(tx->GetHash()));
|
||||
bool wtxid_in_mempool = tx_pool.exists(GenTxid::Wtxid(tx->GetWitnessHash()));
|
||||
bool txid_in_mempool = tx_pool.exists(tx->GetHash());
|
||||
bool wtxid_in_mempool = tx_pool.exists(tx->GetWitnessHash());
|
||||
CheckATMPInvariants(res, txid_in_mempool, wtxid_in_mempool);
|
||||
|
||||
Assert(accepted != added.empty());
|
||||
|
||||
@@ -227,9 +227,9 @@ FUZZ_TARGET(txdownloadman, .init = initialize)
|
||||
Assert(first_time_failure || !todo.m_should_add_extra_compact_tx);
|
||||
},
|
||||
[&] {
|
||||
GenTxid gtxid = fuzzed_data_provider.ConsumeBool() ?
|
||||
GenTxid::Txid(rand_tx->GetHash()) :
|
||||
GenTxid::Wtxid(rand_tx->GetWitnessHash());
|
||||
auto gtxid = fuzzed_data_provider.ConsumeBool() ?
|
||||
GenTxid{rand_tx->GetHash()} :
|
||||
GenTxid{rand_tx->GetWitnessHash()};
|
||||
txdownloadman.AddTxAnnouncement(rand_peer, gtxid, time);
|
||||
},
|
||||
[&] {
|
||||
@@ -260,8 +260,7 @@ FUZZ_TARGET(txdownloadman, .init = initialize)
|
||||
// returned true.
|
||||
Assert(expect_work);
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
// Jump forwards or backwards
|
||||
auto time_skip = fuzzed_data_provider.PickValueInArray(TIME_SKIPS);
|
||||
if (fuzzed_data_provider.ConsumeBool()) time_skip *= -1;
|
||||
@@ -373,9 +372,9 @@ FUZZ_TARGET(txdownloadman_impl, .init = initialize)
|
||||
if (!reject_contains_wtxid) Assert(todo.m_unique_parents.size() <= rand_tx->vin.size());
|
||||
},
|
||||
[&] {
|
||||
GenTxid gtxid = fuzzed_data_provider.ConsumeBool() ?
|
||||
GenTxid::Txid(rand_tx->GetHash()) :
|
||||
GenTxid::Wtxid(rand_tx->GetWitnessHash());
|
||||
auto gtxid = fuzzed_data_provider.ConsumeBool() ?
|
||||
GenTxid{rand_tx->GetHash()} :
|
||||
GenTxid{rand_tx->GetWitnessHash()};
|
||||
txdownload_impl.AddTxAnnouncement(rand_peer, gtxid, time);
|
||||
},
|
||||
[&] {
|
||||
@@ -395,7 +394,7 @@ FUZZ_TARGET(txdownloadman_impl, .init = initialize)
|
||||
// The only combination that doesn't make sense is validate both tx and package.
|
||||
Assert(!(should_validate && maybe_package.has_value()));
|
||||
if (should_validate) {
|
||||
Assert(!txdownload_impl.AlreadyHaveTx(GenTxid::Wtxid(rand_tx->GetWitnessHash()), /*include_reconsiderable=*/true));
|
||||
Assert(!txdownload_impl.AlreadyHaveTx(rand_tx->GetWitnessHash(), /*include_reconsiderable=*/true));
|
||||
}
|
||||
if (maybe_package.has_value()) {
|
||||
CheckPackageToValidate(*maybe_package, rand_peer);
|
||||
@@ -424,7 +423,7 @@ FUZZ_TARGET(txdownloadman_impl, .init = initialize)
|
||||
// However, if there was a non-null tx in the workset, HaveMoreWork should have
|
||||
// returned true.
|
||||
Assert(expect_work);
|
||||
Assert(txdownload_impl.AlreadyHaveTx(GenTxid::Wtxid(ptx->GetWitnessHash()), /*include_reconsiderable=*/false));
|
||||
Assert(txdownload_impl.AlreadyHaveTx(ptx->GetWitnessHash(), /*include_reconsiderable=*/false));
|
||||
// Presumably we have validated this tx. Use "missing inputs" to keep it in the
|
||||
// orphanage longer. Later iterations might call MempoolAcceptedTx or
|
||||
// MempoolRejectedTx with a different error.
|
||||
@@ -432,8 +431,7 @@ FUZZ_TARGET(txdownloadman_impl, .init = initialize)
|
||||
state_missing_inputs.Invalid(TxValidationResult::TX_MISSING_INPUTS, "");
|
||||
txdownload_impl.MempoolRejectedTx(ptx, state_missing_inputs, rand_peer, fuzzed_data_provider.ConsumeBool());
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
auto time_skip = fuzzed_data_provider.PickValueInArray(TIME_SKIPS);
|
||||
if (fuzzed_data_provider.ConsumeBool()) time_skip *= -1;
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace {
|
||||
constexpr int MAX_TXHASHES = 16;
|
||||
constexpr int MAX_PEERS = 16;
|
||||
|
||||
//! Randomly generated GenTxids used in this test (length is MAX_TXHASHES).
|
||||
//! Randomly generated txhashes used in this test (length is MAX_TXHASHES).
|
||||
uint256 TXHASHES[MAX_TXHASHES];
|
||||
|
||||
//! Precomputed random durations (positive and negative, each ~exponentially distributed).
|
||||
@@ -204,7 +204,8 @@ public:
|
||||
}
|
||||
|
||||
// Call TxRequestTracker's implementation.
|
||||
m_tracker.ReceivedInv(peer, is_wtxid ? GenTxid::Wtxid(TXHASHES[txhash]) : GenTxid::Txid(TXHASHES[txhash]), preferred, reqtime);
|
||||
auto gtxid = is_wtxid ? GenTxid{Wtxid::FromUint256(TXHASHES[txhash])} : GenTxid{Txid::FromUint256(TXHASHES[txhash])};
|
||||
m_tracker.ReceivedInv(peer, gtxid, preferred, reqtime);
|
||||
}
|
||||
|
||||
void RequestedTx(int peer, int txhash, std::chrono::microseconds exptime)
|
||||
@@ -252,7 +253,8 @@ public:
|
||||
for (int peer2 = 0; peer2 < MAX_PEERS; ++peer2) {
|
||||
Announcement& ann2 = m_announcements[txhash][peer2];
|
||||
if (ann2.m_state == State::REQUESTED && ann2.m_time <= m_now) {
|
||||
expected_expired.emplace_back(peer2, ann2.m_is_wtxid ? GenTxid::Wtxid(TXHASHES[txhash]) : GenTxid::Txid(TXHASHES[txhash]));
|
||||
auto gtxid = ann2.m_is_wtxid ? GenTxid{Wtxid::FromUint256(TXHASHES[txhash])} : GenTxid{Txid::FromUint256(TXHASHES[txhash])};
|
||||
expected_expired.emplace_back(peer2, gtxid);
|
||||
ann2.m_state = State::COMPLETED;
|
||||
break;
|
||||
}
|
||||
@@ -278,7 +280,7 @@ public:
|
||||
m_tracker.PostGetRequestableSanityCheck(m_now);
|
||||
assert(result.size() == actual.size());
|
||||
for (size_t pos = 0; pos < actual.size(); ++pos) {
|
||||
assert(TXHASHES[std::get<1>(result[pos])] == actual[pos].GetHash());
|
||||
assert(TXHASHES[std::get<1>(result[pos])] == actual[pos].ToUint256());
|
||||
assert(std::get<2>(result[pos]) == actual[pos].IsWtxid());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user