Permalink
Browse files

limit pos by temperature

  • Loading branch information...
EvgenijM86 committed Oct 22, 2018
1 parent af491ca commit ec32762b99cc68fb9abb2909dda96bc7a13bd819
Showing with 21 additions and 1 deletion.
  1. +1 −0 src/net.cpp
  2. +5 −0 src/net.h
  3. +15 −1 src/net_processing.cpp
@@ -2743,6 +2743,7 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn
fPauseSend = false;
nProcessQueueSize = 0;
temperature = 0;
nPoSTemperature = MAX_CONSECUTIVE_POS_HEADERS / 4; // by default we don't trust new peer complemetely

BOOST_FOREACH(const std::string &msg, getAllNetMessageTypes())
mapRecvBytesPerMsgCmd[msg] = 0;
@@ -92,6 +92,9 @@ static const ServiceFlags REQUIRED_SERVICES = NODE_NETWORK;
// NOTE: When adjusting this, update rpcnet:setban's help ("24h")
static const unsigned int DEFAULT_MISBEHAVING_BANTIME = 60 * 60 * 24; // Default 24-hour ban

/** emercoin: Number of consecutive PoS headers are allowed from a single peer. Used to prevent out of memory attack. */
static const unsigned int MAX_CONSECUTIVE_POS_HEADERS = 1000;

typedef int64_t NodeId;

struct AddedNodeInfo
@@ -684,6 +687,8 @@ class CNode

// emercoin: temperature to measure how many tx has been sent by this client.
uint32_t temperature;
// emercoin: temperature to measure how many PoS headers have been sent by this client
uint32_t nPoSTemperature;

CNode(NodeId id, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn, SOCKET hSocketIn, const CAddress &addrIn, uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn, const std::string &addrNameIn = "", bool fInboundIn = false);
~CNode();
@@ -2269,6 +2269,16 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
vRecv >> headers[n];
ReadCompactSize(vRecv); // ignore tx count; assume it is 0.
ReadCompactSize(vRecv); // ignore vchBlockSig.

// emercoin: ban peers that spam PoS headers without any PoW headers
pfrom->nPoSTemperature = headers[n].nFlags & BLOCK_PROOF_OF_STAKE ? pfrom->nPoSTemperature + 1 : 0;
if (pfrom->nPoSTemperature >= MAX_CONSECUTIVE_POS_HEADERS)
{
pfrom->nPoSTemperature = (MAX_CONSECUTIVE_POS_HEADERS / 4) * 3;
LOCK(cs_main);
Misbehaving(pfrom->GetId(), 100);
return error("too many consecutive pos headers");
}
}

if (nCount == 0) {
@@ -2324,7 +2334,11 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
if (state.IsInvalid(nDoS)) {
if (nDoS > 0) {
LOCK(cs_main);
Misbehaving(pfrom->GetId(), nDoS);
if (pfrom->nPoSTemperature >= 100) { // recieved 100+ pos headers in a row, probably out of memory attack
pfrom->nPoSTemperature = (MAX_CONSECUTIVE_POS_HEADERS / 4) * 3;
Misbehaving(pfrom->GetId(), 100);
} else
Misbehaving(pfrom->GetId(), nDoS);
}
return error("invalid header received");
}

0 comments on commit ec32762

Please sign in to comment.