Browse Source

Make sure rate-limiting code is thread-safe

0.8
Gavin Andresen 14 years ago
parent
commit
88abf70386
  1. 7
      main.cpp

7
main.cpp

@ -739,11 +739,17 @@ bool CTransaction::AcceptToMemoryPool(CTxDB& txdb, bool fCheckInputs, bool* pfMi
return error("AcceptToMemoryPool() : not enough fees"); return error("AcceptToMemoryPool() : not enough fees");
// Continuously rate-limit free transactions // Continuously rate-limit free transactions
// This mitigates 'penny-flooding' -- sending thousands of free transactions just to
// be annoying or make other's transactions take longer to confirm.
if (nFees < CENT) if (nFees < CENT)
{ {
static CCriticalSection cs;
static double dFreeCount; static double dFreeCount;
static int64 nLastTime; static int64 nLastTime;
int64 nNow = GetTime(); int64 nNow = GetTime();
CRITICAL_BLOCK(cs)
{
// Use an exponentially decaying ~10-minute window: // Use an exponentially decaying ~10-minute window:
dFreeCount *= pow(1.0 - 1.0/600.0, (double)(nNow - nLastTime)); dFreeCount *= pow(1.0 - 1.0/600.0, (double)(nNow - nLastTime));
nLastTime = nNow; nLastTime = nNow;
@ -756,6 +762,7 @@ bool CTransaction::AcceptToMemoryPool(CTxDB& txdb, bool fCheckInputs, bool* pfMi
dFreeCount += nSize; dFreeCount += nSize;
} }
} }
}
// Store transaction in memory // Store transaction in memory
CRITICAL_BLOCK(cs_mapTransactions) CRITICAL_BLOCK(cs_mapTransactions)

Loading…
Cancel
Save