From 595f09d6de7f1b94428cdd1310777aa6a4c584e5 Mon Sep 17 00:00:00 2001 From: Jeremy Rubin Date: Fri, 30 Aug 2019 13:25:41 -0700 Subject: [PATCH] Cache tx Trust per-call to avoid DoS --- src/wallet/wallet.cpp | 12 +++++++++++- src/wallet/wallet.h | 1 + 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 5acd0dbf0c3..ae6c268484c 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -2294,6 +2294,12 @@ bool CWalletTx::InMempool() const } bool CWalletTx::IsTrusted(interfaces::Chain::Lock& locked_chain) const +{ + std::set s; + return IsTrusted(locked_chain, s); +} + +bool CWalletTx::IsTrusted(interfaces::Chain::Lock& locked_chain, std::set& trustedParents) const { // Quick answer in most cases if (!locked_chain.checkFinalTx(*tx)) { @@ -2322,9 +2328,13 @@ bool CWalletTx::IsTrusted(interfaces::Chain::Lock& locked_chain) const // Check that this specific input being spent is trusted if (pwallet->IsMine(parentOut) != ISMINE_SPENDABLE) return false; + // If we've already trusted this parent, continue + if (trustedParents.count(parent->GetHash())) + continue; // Recurse to check that the parent is also trusted - if (!parent->IsTrusted(locked_chain)) + if (!parent->IsTrusted(locked_chain, trustedParents)) return false; + trustedParents.insert(parent->GetHash()); } return true; } diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index f9e2230a6ff..986baa89ee8 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -616,6 +616,7 @@ public: bool InMempool() const; bool IsTrusted(interfaces::Chain::Lock& locked_chain) const; + bool IsTrusted(interfaces::Chain::Lock& locked_chain, std::set& trustedParents) const; int64_t GetTxTime() const;