Skip to content

Commit 88dd359

Browse files
committed
Check signatures before respend relay
Check that all inputs are completely valid before actually relaying a double-spend.
1 parent f04f123 commit 88dd359

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

src/main.cpp

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ namespace {
127127

128128
// Forward reference functions defined here:
129129
static const unsigned int MAX_DOUBLESPEND_BLOOM = 1000;
130-
static void RelayDoubleSpend(const COutPoint& outPoint, const CTransaction& doubleSpend, bool fInBlock, CBloomFilter& filter);
130+
static bool RelayableRespend(const COutPoint& outPoint, const CTransaction& doubleSpend, bool fInBlock, CBloomFilter& filter);
131131

132132
//////////////////////////////////////////////////////////////////////////////
133133
//
@@ -156,7 +156,7 @@ struct CMainSignals {
156156
// transaction was first seen in a block.
157157
// Note: only notifies if the previous transaction is in the memory pool; if previous transction was in a block,
158158
// then the double-spend simply fails when we try to lookup the inputs in the current UTXO set.
159-
boost::signals2::signal<void (const COutPoint&, const CTransaction&, bool)> DetectedDoubleSpend;
159+
boost::signals2::signal<bool (const COutPoint&, const CTransaction&, bool)> DetectedDoubleSpend;
160160
} g_signals;
161161

162162
} // anon namespace
@@ -166,7 +166,7 @@ void RegisterInternalSignals() {
166166
seed_insecure_rand();
167167
doubleSpendFilter = CBloomFilter(MAX_DOUBLESPEND_BLOOM, 0.01, insecure_rand(), BLOOM_UPDATE_NONE);
168168

169-
g_signals.DetectedDoubleSpend.connect(boost::bind(RelayDoubleSpend, _1, _2, _3, doubleSpendFilter));
169+
g_signals.DetectedDoubleSpend.connect(boost::bind(RelayableRespend, _1, _2, _3, doubleSpendFilter));
170170
}
171171

172172

@@ -936,6 +936,7 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa
936936
return false;
937937

938938
// Check for conflicts with in-memory transactions
939+
bool relayableRespend = false;
939940
{
940941
LOCK(pool.cs); // protect pool.mapNextTx
941942
for (unsigned int i = 0; i < tx.vin.size(); i++)
@@ -944,8 +945,9 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa
944945
// Does tx conflict with a member of the pool, and is it not equivalent to that member?
945946
if (pool.mapNextTx.count(outpoint) && !tx.IsEquivalentTo(*pool.mapNextTx[outpoint].ptx))
946947
{
947-
g_signals.DetectedDoubleSpend(outpoint, tx, false);
948-
return false;
948+
relayableRespend = g_signals.DetectedDoubleSpend(outpoint, tx, false);
949+
if (!relayableRespend)
950+
return false;
949951
}
950952
}
951953
}
@@ -1038,16 +1040,24 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa
10381040
{
10391041
return error("AcceptToMemoryPool: : ConnectInputs failed %s", hash.ToString());
10401042
}
1041-
// Store transaction in memory
1042-
pool.addUnchecked(hash, entry);
1043+
1044+
if (relayableRespend)
1045+
{
1046+
RelayTransaction(tx);
1047+
}
1048+
else
1049+
{
1050+
// Store transaction in memory
1051+
pool.addUnchecked(hash, entry);
1052+
}
10431053
}
10441054

10451055
g_signals.SyncTransaction(tx, NULL);
10461056

1047-
return true;
1057+
return !relayableRespend;
10481058
}
10491059

1050-
static void RelayDoubleSpend(const COutPoint& outPoint, const CTransaction& doubleSpend, bool fInBlock, CBloomFilter& filter)
1060+
static bool RelayableRespend(const COutPoint& outPoint, const CTransaction& doubleSpend, bool fInBlock, CBloomFilter& filter)
10511061
{
10521062
// Relaying double-spend attempts to our peers lets them detect when
10531063
// somebody might be trying to cheat them. However, blindly relaying
@@ -1060,7 +1070,7 @@ static void RelayDoubleSpend(const COutPoint& outPoint, const CTransaction& doub
10601070
// from us they are very likely to hear about it from another peer, since
10611071
// each peer uses a different, randomized bloom filter.
10621072

1063-
if (fInBlock || filter.contains(outPoint)) return;
1073+
if (fInBlock || filter.contains(outPoint)) return false;
10641074

10651075
// Apply an independent rate limit to double-spend relays
10661076
static double dRespendCount;
@@ -1071,7 +1081,7 @@ static void RelayDoubleSpend(const COutPoint& outPoint, const CTransaction& doub
10711081
if (RateLimitExceeded(dRespendCount, nLastRespendTime, nRespendLimit, nSize))
10721082
{
10731083
LogPrint("mempool", "Double-spend relay rejected by rate limiter\n");
1074-
return;
1084+
return false;
10751085
}
10761086

10771087
LogPrint("mempool", "Rate limit dRespendCount: %g => %g\n", dRespendCount, dRespendCount+nSize);
@@ -1083,10 +1093,7 @@ static void RelayDoubleSpend(const COutPoint& outPoint, const CTransaction& doub
10831093

10841094
filter.insert(outPoint);
10851095

1086-
RelayTransaction(doubleSpend);
1087-
1088-
// Share conflict with wallet
1089-
g_signals.SyncTransaction(doubleSpend, NULL);
1096+
return true;
10901097
}
10911098

10921099

0 commit comments

Comments
 (0)