From 5de8b54c5138e47b6df40d31f282fb45243b29bf Mon Sep 17 00:00:00 2001 From: Gavin Andresen Date: Fri, 11 Mar 2011 11:50:16 -0500 Subject: [PATCH 1/2] Continuously rate-limit free transactions. Changed algorithm to use continuous exponential function instead of discrete 10-minute window. Changed -limitfreerelay to be kilobytes-per-minute instead of boolean. --- main.cpp | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/main.cpp b/main.cpp index c65cd72de..5cb1342b9 100644 --- a/main.cpp +++ b/main.cpp @@ -738,19 +738,22 @@ bool CTransaction::AcceptToMemoryPool(CTxDB& txdb, bool fCheckInputs, bool* pfMi if (nFees < GetMinFee(1000)) return error("AcceptToMemoryPool() : not enough fees"); - // Limit free transactions per 10 minutes - if (nFees < CENT && GetBoolArg("-limitfreerelay")) + // Continuously rate-limit free transactions + if (nFees < CENT) { - static int64 nNextReset; - static int64 nFreeCount; - if (GetTime() > nNextReset) - { - nNextReset = GetTime() + 10 * 60; - nFreeCount = 0; - } - if (nFreeCount > 150000 && !IsFromMe()) + static double dFreeCount; + static int64 nLastTime; + int64 nNow = GetTime(); + // Use an exponentially decaying ~10-minute window: + dFreeCount *= pow(1.0 - 1.0/600.0, (double)(nNow - nLastTime)); + nLastTime = nNow; + // -limitfreerelay unit is thousand-bytes-per-minute + // At default rate it would take over a month to fill 1GB + if (dFreeCount > GetArg("-limitfreerelay", 15)*10*1000 && !IsFromMe()) return error("AcceptToMemoryPool() : free transaction rejected by rate limiter"); - nFreeCount += nSize; + if (fDebug) + printf("Rate limit dFreeCount: %g => %g\n", dFreeCount, dFreeCount+nSize); + dFreeCount += nSize; } } From 88abf70386f18020bb624bca4a4cb7900ce47d0c Mon Sep 17 00:00:00 2001 From: Gavin Andresen Date: Sun, 13 Mar 2011 14:38:07 -0400 Subject: [PATCH 2/2] Make sure rate-limiting code is thread-safe --- main.cpp | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/main.cpp b/main.cpp index 5cb1342b9..4214f27ff 100644 --- a/main.cpp +++ b/main.cpp @@ -739,21 +739,28 @@ bool CTransaction::AcceptToMemoryPool(CTxDB& txdb, bool fCheckInputs, bool* pfMi return error("AcceptToMemoryPool() : not enough fees"); // 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) { + static CCriticalSection cs; static double dFreeCount; static int64 nLastTime; int64 nNow = GetTime(); - // Use an exponentially decaying ~10-minute window: - dFreeCount *= pow(1.0 - 1.0/600.0, (double)(nNow - nLastTime)); - nLastTime = nNow; - // -limitfreerelay unit is thousand-bytes-per-minute - // At default rate it would take over a month to fill 1GB - if (dFreeCount > GetArg("-limitfreerelay", 15)*10*1000 && !IsFromMe()) - return error("AcceptToMemoryPool() : free transaction rejected by rate limiter"); - if (fDebug) - printf("Rate limit dFreeCount: %g => %g\n", dFreeCount, dFreeCount+nSize); - dFreeCount += nSize; + + CRITICAL_BLOCK(cs) + { + // Use an exponentially decaying ~10-minute window: + dFreeCount *= pow(1.0 - 1.0/600.0, (double)(nNow - nLastTime)); + nLastTime = nNow; + // -limitfreerelay unit is thousand-bytes-per-minute + // At default rate it would take over a month to fill 1GB + if (dFreeCount > GetArg("-limitfreerelay", 15)*10*1000 && !IsFromMe()) + return error("AcceptToMemoryPool() : free transaction rejected by rate limiter"); + if (fDebug) + printf("Rate limit dFreeCount: %g => %g\n", dFreeCount, dFreeCount+nSize); + dFreeCount += nSize; + } } }