From 88dd3598d22197a22565e524cecdc08107cf76ac Mon Sep 17 00:00:00 2001 From: Tom Harding Date: Tue, 1 Jul 2014 14:26:57 -0700 Subject: [PATCH] Check signatures before respend relay Check that all inputs are completely valid before actually relaying a double-spend. --- src/main.cpp | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 04d9523e2..1294e5b2e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -127,7 +127,7 @@ namespace { // Forward reference functions defined here: static const unsigned int MAX_DOUBLESPEND_BLOOM = 1000; -static void RelayDoubleSpend(const COutPoint& outPoint, const CTransaction& doubleSpend, bool fInBlock, CBloomFilter& filter); +static bool RelayableRespend(const COutPoint& outPoint, const CTransaction& doubleSpend, bool fInBlock, CBloomFilter& filter); ////////////////////////////////////////////////////////////////////////////// // @@ -156,7 +156,7 @@ struct CMainSignals { // transaction was first seen in a block. // Note: only notifies if the previous transaction is in the memory pool; if previous transction was in a block, // then the double-spend simply fails when we try to lookup the inputs in the current UTXO set. - boost::signals2::signal DetectedDoubleSpend; + boost::signals2::signal DetectedDoubleSpend; } g_signals; } // anon namespace @@ -166,7 +166,7 @@ void RegisterInternalSignals() { seed_insecure_rand(); doubleSpendFilter = CBloomFilter(MAX_DOUBLESPEND_BLOOM, 0.01, insecure_rand(), BLOOM_UPDATE_NONE); - g_signals.DetectedDoubleSpend.connect(boost::bind(RelayDoubleSpend, _1, _2, _3, doubleSpendFilter)); + g_signals.DetectedDoubleSpend.connect(boost::bind(RelayableRespend, _1, _2, _3, doubleSpendFilter)); } @@ -936,6 +936,7 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa return false; // Check for conflicts with in-memory transactions + bool relayableRespend = false; { LOCK(pool.cs); // protect pool.mapNextTx for (unsigned int i = 0; i < tx.vin.size(); i++) @@ -944,8 +945,9 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa // Does tx conflict with a member of the pool, and is it not equivalent to that member? if (pool.mapNextTx.count(outpoint) && !tx.IsEquivalentTo(*pool.mapNextTx[outpoint].ptx)) { - g_signals.DetectedDoubleSpend(outpoint, tx, false); - return false; + relayableRespend = g_signals.DetectedDoubleSpend(outpoint, tx, false); + if (!relayableRespend) + return false; } } } @@ -1038,16 +1040,24 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa { return error("AcceptToMemoryPool: : ConnectInputs failed %s", hash.ToString()); } - // Store transaction in memory - pool.addUnchecked(hash, entry); + + if (relayableRespend) + { + RelayTransaction(tx); + } + else + { + // Store transaction in memory + pool.addUnchecked(hash, entry); + } } g_signals.SyncTransaction(tx, NULL); - return true; + return !relayableRespend; } -static void RelayDoubleSpend(const COutPoint& outPoint, const CTransaction& doubleSpend, bool fInBlock, CBloomFilter& filter) +static bool RelayableRespend(const COutPoint& outPoint, const CTransaction& doubleSpend, bool fInBlock, CBloomFilter& filter) { // Relaying double-spend attempts to our peers lets them detect when // somebody might be trying to cheat them. However, blindly relaying @@ -1060,7 +1070,7 @@ static void RelayDoubleSpend(const COutPoint& outPoint, const CTransaction& doub // from us they are very likely to hear about it from another peer, since // each peer uses a different, randomized bloom filter. - if (fInBlock || filter.contains(outPoint)) return; + if (fInBlock || filter.contains(outPoint)) return false; // Apply an independent rate limit to double-spend relays static double dRespendCount; @@ -1071,7 +1081,7 @@ static void RelayDoubleSpend(const COutPoint& outPoint, const CTransaction& doub if (RateLimitExceeded(dRespendCount, nLastRespendTime, nRespendLimit, nSize)) { LogPrint("mempool", "Double-spend relay rejected by rate limiter\n"); - return; + return false; } LogPrint("mempool", "Rate limit dRespendCount: %g => %g\n", dRespendCount, dRespendCount+nSize); @@ -1083,10 +1093,7 @@ static void RelayDoubleSpend(const COutPoint& outPoint, const CTransaction& doub filter.insert(outPoint); - RelayTransaction(doubleSpend); - - // Share conflict with wallet - g_signals.SyncTransaction(doubleSpend, NULL); + return true; }