Skip to content

Commit 85fa648

Browse files
committed
Merge #19596: Deduplicate parent txid loop of requested transactions and missing parents of orphan transactions
4c0731f Deduplicate missing parents of orphan transactions (Suhas Daftuar) 8196176 Rewrite parent txid loop of requested transactions (Suhas Daftuar) Pull request description: I noticed a couple of places recently where we loop over all inputs of a transaction in order to do some processing on the txids we find in those inputs. There may be thousands of inputs in a transaction, and the same txid may appear many times. In a couple of places in particular, we loop over those txids and add them to a rolling bloom filter; doing that multiple times for the same txid wastes entries in that filter. This PR fixes that in two places relating to transaction relay: one on the server side, where we look for parent transactions of a tx that we are delivering to a peer to ensure that getdata requests for those parents will succeed; and the other on the client side, where when we process an orphan tx we want to loop over the parent txids and ensure that all are eventually requested from the peer who provided the orphan. This addresses a couple of [related](bitcoin/bitcoin#19109 (comment)) [comments](bitcoin/bitcoin#19109 (comment)) left in #19109. ACKs for top commit: laanwj: Code review ACK 4c0731f jonatack: ACK 4c0731f ajtowns: ACK 4c0731f Tree-SHA512: 8af9df7f56c6e54b5915519d7d5465e081473ceb1bcc89bbebf83e78722cf51ff58145e588cf57126bce17071a8053273f4bcef0ad8166bec83ba14352e40f5d
2 parents 6d5a9fe + 4c0731f commit 85fa648

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

src/net_processing.cpp

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,16 +1741,27 @@ void static ProcessGetData(CNode& pfrom, const CChainParams& chainparams, CConnm
17411741
connman.PushMessage(&pfrom, msgMaker.Make(nSendFlags, NetMsgType::TX, *tx));
17421742
mempool.RemoveUnbroadcastTx(tx->GetHash());
17431743
// As we're going to send tx, make sure its unconfirmed parents are made requestable.
1744-
for (const auto& txin : tx->vin) {
1745-
auto txinfo = mempool.info(txin.prevout.hash);
1746-
if (txinfo.tx && txinfo.m_time > now - UNCONDITIONAL_RELAY_DELAY) {
1747-
// Relaying a transaction with a recent but unconfirmed parent.
1748-
if (WITH_LOCK(pfrom.m_tx_relay->cs_tx_inventory, return !pfrom.m_tx_relay->filterInventoryKnown.contains(txin.prevout.hash))) {
1749-
LOCK(cs_main);
1750-
State(pfrom.GetId())->m_recently_announced_invs.insert(txin.prevout.hash);
1744+
std::vector<uint256> parent_ids_to_add;
1745+
{
1746+
LOCK(mempool.cs);
1747+
auto txiter = mempool.GetIter(tx->GetHash());
1748+
if (txiter) {
1749+
const CTxMemPool::setEntries& parents = mempool.GetMemPoolParents(*txiter);
1750+
parent_ids_to_add.reserve(parents.size());
1751+
for (CTxMemPool::txiter parent_iter : parents) {
1752+
if (parent_iter->GetTime() > now - UNCONDITIONAL_RELAY_DELAY) {
1753+
parent_ids_to_add.push_back(parent_iter->GetTx().GetHash());
1754+
}
17511755
}
17521756
}
17531757
}
1758+
for (const uint256& parent_txid : parent_ids_to_add) {
1759+
// Relaying a transaction with a recent but unconfirmed parent.
1760+
if (WITH_LOCK(pfrom.m_tx_relay->cs_tx_inventory, return !pfrom.m_tx_relay->filterInventoryKnown.contains(parent_txid))) {
1761+
LOCK(cs_main);
1762+
State(pfrom.GetId())->m_recently_announced_invs.insert(parent_txid);
1763+
}
1764+
}
17541765
} else {
17551766
vNotFound.push_back(inv);
17561767
}
@@ -2999,8 +3010,19 @@ void ProcessMessage(
29993010
else if (state.GetResult() == TxValidationResult::TX_MISSING_INPUTS)
30003011
{
30013012
bool fRejectedParents = false; // It may be the case that the orphans parents have all been rejected
3013+
3014+
// Deduplicate parent txids, so that we don't have to loop over
3015+
// the same parent txid more than once down below.
3016+
std::vector<uint256> unique_parents;
3017+
unique_parents.reserve(tx.vin.size());
30023018
for (const CTxIn& txin : tx.vin) {
3003-
if (recentRejects->contains(txin.prevout.hash)) {
3019+
// We start with all parents, and then remove duplicates below.
3020+
unique_parents.push_back(txin.prevout.hash);
3021+
}
3022+
std::sort(unique_parents.begin(), unique_parents.end());
3023+
unique_parents.erase(std::unique(unique_parents.begin(), unique_parents.end()), unique_parents.end());
3024+
for (const uint256& parent_txid : unique_parents) {
3025+
if (recentRejects->contains(parent_txid)) {
30043026
fRejectedParents = true;
30053027
break;
30063028
}
@@ -3009,14 +3031,14 @@ void ProcessMessage(
30093031
uint32_t nFetchFlags = GetFetchFlags(pfrom);
30103032
const auto current_time = GetTime<std::chrono::microseconds>();
30113033

3012-
for (const CTxIn& txin : tx.vin) {
3034+
for (const uint256& parent_txid : unique_parents) {
30133035
// Here, we only have the txid (and not wtxid) of the
30143036
// inputs, so we only request in txid mode, even for
30153037
// wtxidrelay peers.
30163038
// Eventually we should replace this with an improved
30173039
// protocol for getting all unconfirmed parents.
3018-
CInv _inv(MSG_TX | nFetchFlags, txin.prevout.hash);
3019-
pfrom.AddKnownTx(txin.prevout.hash);
3040+
CInv _inv(MSG_TX | nFetchFlags, parent_txid);
3041+
pfrom.AddKnownTx(parent_txid);
30203042
if (!AlreadyHave(_inv, mempool)) RequestTx(State(pfrom.GetId()), ToGenTxid(_inv), current_time);
30213043
}
30223044
AddOrphanTx(ptx, pfrom.GetId());

0 commit comments

Comments
 (0)