From 6f4da19cc3b1b7cd23cb4be95a6bb9acb79eb3bf Mon Sep 17 00:00:00 2001 From: glozow Date: Wed, 13 Mar 2024 11:25:24 +0000 Subject: [PATCH 1/9] guard against MempoolAcceptResult::m_replaced_transactions It should never be a nullopt when the transaction result is valid - Assume() this is the case. However, as a belt-and-suspenders just in case it is nullopt, use an empty list. --- src/net_processing.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/net_processing.cpp b/src/net_processing.cpp index 39ffff97d2..07ac1cfec7 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -4354,7 +4354,9 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type, const TxValidationState& state = result.m_state; if (result.m_result_type == MempoolAcceptResult::ResultType::VALID) { - ProcessValidTx(pfrom.GetId(), ptx, result.m_replaced_transactions.value()); + Assume(result.m_replaced_transactions.has_value()); + std::list empty_replacement_list; + ProcessValidTx(pfrom.GetId(), ptx, result.m_replaced_transactions.value_or(empty_replacement_list)); pfrom.m_last_tx_time = GetTime(); } else if (state.GetResult() == TxValidationResult::TX_MISSING_INPUTS) From c3c1e15831c463df7968b028a77e787da7e6256d Mon Sep 17 00:00:00 2001 From: glozow Date: Wed, 13 Mar 2024 11:45:33 +0000 Subject: [PATCH 2/9] [doc] restore comment about why we check if ptx HasWitness before caching rejected txid --- src/net_processing.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/net_processing.cpp b/src/net_processing.cpp index 07ac1cfec7..577ed5cd3f 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -3107,6 +3107,8 @@ void PeerManagerImpl::ProcessInvalidTx(NodeId nodeid, const CTransactionRef& ptx // processing of this transaction in the event that child // transactions are later received (resulting in // parent-fetching by txid via the orphan-handling logic). + // We only add the txid if it differs from the wtxid, to avoid wasting entries in the + // rolling bloom filter. if (state.GetResult() == TxValidationResult::TX_INPUTS_NOT_STANDARD && ptx->HasWitness()) { m_recent_rejects.insert(ptx->GetHash().ToUint256()); m_txrequest.ForgetTxHash(ptx->GetHash()); From 092c978a42e8f4a02291b994713505ba8aac8b28 Mon Sep 17 00:00:00 2001 From: glozow Date: Tue, 16 Apr 2024 11:58:25 +0100 Subject: [PATCH 3/9] [txpackages] add canonical way to get hash of package --- src/policy/packages.cpp | 18 +++++++ src/policy/packages.h | 5 ++ src/test/txpackage_tests.cpp | 98 +++++++++++++++++++++++++++++++++++- 3 files changed, 120 insertions(+), 1 deletion(-) diff --git a/src/policy/packages.cpp b/src/policy/packages.cpp index 3a63a9fe46..99d2a6d514 100644 --- a/src/policy/packages.cpp +++ b/src/policy/packages.cpp @@ -147,3 +147,21 @@ bool IsChildWithParentsTree(const Package& package) return true; }); } + +uint256 GetPackageHash(const std::vector& transactions) +{ + // Create a vector of the wtxids. + std::vector wtxids_copy; + std::transform(transactions.cbegin(), transactions.cend(), std::back_inserter(wtxids_copy), + [](const auto& tx){ return tx->GetWitnessHash(); }); + + // Sort in ascending order + std::sort(wtxids_copy.begin(), wtxids_copy.end(), [](const auto& lhs, const auto& rhs) { return lhs.GetHex() < rhs.GetHex(); }); + + // Get sha256 hash of the wtxids concatenated in this order + HashWriter hashwriter; + for (const auto& wtxid : wtxids_copy) { + hashwriter << wtxid; + } + return hashwriter.GetSHA256(); +} diff --git a/src/policy/packages.h b/src/policy/packages.h index 537d8476e2..3050320122 100644 --- a/src/policy/packages.h +++ b/src/policy/packages.h @@ -88,4 +88,9 @@ bool IsChildWithParents(const Package& package); * other (the package is a "tree"). */ bool IsChildWithParentsTree(const Package& package); + +/** Get the hash of these transactions' wtxids, concatenated in lexicographical order (treating the + * wtxids as little endian encoded uint256, smallest to largest). */ +uint256 GetPackageHash(const std::vector& transactions); + #endif // BITCOIN_POLICY_PACKAGES_H diff --git a/src/test/txpackage_tests.cpp b/src/test/txpackage_tests.cpp index b948ea8acb..8112f5f685 100644 --- a/src/test/txpackage_tests.cpp +++ b/src/test/txpackage_tests.cpp @@ -8,9 +8,12 @@ #include #include #include