Skip to content

Commit

Permalink
Ban forked peers who are stuck in a getblocks loop (algo #2)
Browse files Browse the repository at this point in the history
  • Loading branch information
almightyruler committed Jul 6, 2015
1 parent da8cdb1 commit fdecc06
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/clientversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// These need to be macros, as version.cpp's and cloudcoin-qt.rc's voodoo requires it
#define CLIENT_VERSION_MAJOR 1
#define CLIENT_VERSION_MINOR 3
#define CLIENT_VERSION_REVISION 2
#define CLIENT_VERSION_REVISION 3
#define CLIENT_VERSION_BUILD 0

// Converts the parameter X to a string after macro replacement on X has been performed.
Expand Down
32 changes: 32 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3021,6 +3021,12 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
// ppcoin: ask for pending sync-checkpoint if any
if (!IsInitialBlockDownload())
Checkpoints::AskForPendingSyncCheckpoint(pfrom);

// CDC v1.3.3: reset counts to detect forked peers
pfrom->nHighestHeightRequested = 0; /* CDC v1.3.3 */
pfrom->nHeightBackwards = 0; /* CDC v1.3.3 */
pfrom->nHeightBackwardsLast = GetTime(); /* CDC v1.3.3 */

}


Expand Down Expand Up @@ -3242,6 +3248,32 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
pindex = pindex->pnext;
int nLimit = 500;
printf("getblocks %d to %s limit %d from %s\n", (pindex ? pindex->nHeight : -1), hashStop.ToString().substr(0,20).c_str(), nLimit, pfrom->addr.ToString().c_str());


// CDC v1.3.3: detect when peers go backwards in the height of blocks that they request, which is a sign of a forked client stuck in an endless loop. if this happens too many times, ban the peer.
if (pindex) // only act when an explicit height is requested
{
if (pindex->nHeight > pfrom->nHighestHeightRequested)
{
pfrom->nHighestHeightRequested = pindex->nHeight;
} else {
if (pindex->nHeight < (pfrom->nHighestHeightRequested - 500)) // peer has gone backwards in height when requesting blocks
{
if (pfrom->nHeightBackwardsLast < (GetTime() - 3600)) pfrom->nHeightBackwards = 0; // reset penalty count if last backward leap was more than an hour ago
pfrom->nHeightBackwards++;
printf("peer %s: leap backwards in height request, from max %d to current %d. penalty=%d/100\n", pfrom->addr.ToString().c_str(), pfrom->nHighestHeightRequested, pindex->nHeight,
pfrom->nHeightBackwards);
pfrom->nHeightBackwardsLast = GetTime();
pfrom->nHighestHeightRequested = pindex->nHeight; // reset maximum to height of current request
}
}
if (pfrom->nHeightBackwards >= 100)
{
pfrom->Misbehaving(100);
return error("peer %s: sync repeatedly leaping backwards to lower height", pfrom->addr.ToString().c_str());
}
}

for (; pindex; pindex = pindex->pnext)
{
if (pindex->GetBlockHash() == hashStop)
Expand Down
6 changes: 6 additions & 0 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ class CNode
int64 nLastRecv;
int64 nLastSendEmpty;
int64 nTimeConnected;
int nHighestHeightRequested; /* CDC v1.3.3 */
int nHeightBackwards; /* CDC v1.3.3 */
int64 nHeightBackwardsLast; /* CDC v1.3.3 */
int nHeaderStart;
unsigned int nMessageStart;
CAddress addr;
Expand Down Expand Up @@ -216,6 +219,9 @@ class CNode
hSocket = hSocketIn;
nLastSend = 0;
nLastRecv = 0;
nHighestHeightRequested = 0; /* CDC v1.3.3 */
nHeightBackwards = 0; /* CDC v1.3.3 */
nHeightBackwardsLast = GetTime(); /* CDC v1.3.3 */
nLastSendEmpty = GetTime();
nTimeConnected = GetTime();
nHeaderStart = -1;
Expand Down
2 changes: 1 addition & 1 deletion src/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static const int MEMPOOL_GD_VERSION = 60002;

#define DISPLAY_VERSION_MAJOR 1
#define DISPLAY_VERSION_MINOR 3
#define DISPLAY_VERSION_REVISION 2
#define DISPLAY_VERSION_REVISION 3
#define DISPLAY_VERSION_BUILD 0

#endif

0 comments on commit fdecc06

Please sign in to comment.