Throttle rate of nSPV requests

This commit is contained in:
jl777
2019-07-02 05:30:56 -11:00
parent 4411270346
commit 4b876b6118
2 changed files with 38 additions and 16 deletions

View File

@@ -26,22 +26,31 @@
#define NSPV_UTXOS 0x02 #define NSPV_UTXOS 0x02
#define NSPV_UTXOSRESP 0x03 #define NSPV_UTXOSRESP 0x03
uint32_t NSPV_lastinfo,NSPV_lastutxos;
void komodo_nSPVreq(CNode *pfrom,std::vector<uint8_t> request) // received a request void komodo_nSPVreq(CNode *pfrom,std::vector<uint8_t> request) // received a request
{ {
int32_t len; std::vector<uint8_t> response; int32_t len; std::vector<uint8_t> response; uint32_t timestamp = time(NULL);
if ( (len= request.size()) > 0 ) if ( (len= request.size()) > 0 )
{ {
response.resize(1); response.resize(1);
if ( len == 1 && request[0] == NSPV_INFO ) // info if ( len == 1 && request[0] == NSPV_INFO ) // info
{ {
response[0] = NSPV_INFORESP; if ( timestamp > pfrom->lastinfo + KOMODO_BLOCKTIME/2 )
pfrom->PushMessage("nSPV",response); {
response[0] = NSPV_INFORESP;
pfrom->lastinfo = timestamp;
pfrom->PushMessage("nSPV",response);
}
} }
else if ( request[0] == NSPV_UTXOS ) else if ( request[0] == NSPV_UTXOS )
{ {
response[0] = NSPV_UTXOSRESP; if ( timestamp > pfrom->lastutxos + KOMODO_BLOCKTIME/2 )
pfrom->PushMessage("nSPV",response); {
response[0] = NSPV_UTXOSRESP;
pfrom->lastutxos = timestamp;
pfrom->PushMessage("nSPV",response);
}
} }
} }
} }
@@ -67,21 +76,33 @@ void komodo_nSPVresp(CNode *pfrom,std::vector<uint8_t> response) // received a r
void komodo_nSPV(CNode *pto) void komodo_nSPV(CNode *pto)
{ {
std::vector<uint8_t> request; std::vector<uint8_t> request; uint32_t timestamp = time(NULL);
// limit frequency! // limit frequency!
if ( (pto->nServices & NODE_ADDRINDEX) != 0 ) if ( timestamp > NSPV_lastutxos + KOMODO_BLOCKTIME/2 )
{ {
// get utxo since lastheight if ( (pto->nServices & NODE_ADDRINDEX) != 0 && timestamp > ptr->lastutxos + KOMODO_BLOCKTIME )
request.resize(1); {
request[0] = NSPV_UTXOS; // get utxo since lastheight
pto->PushMessage("getnSPV",request); if ( (rand() % 100) < 10 )
{
request.resize(1);
request[0] = NSPV_UTXOS;
NSPV_lastutxos = pto->lastutxos = timestamp;
pto->PushMessage("getnSPV",request);
return;
}
}
} }
else if ( timestamp > NSPV_lastinfo + KOMODO_BLOCKTIME/2 && timestamp > ptr->lastinfo + KOMODO_BLOCKTIME )
{ {
// query current height, blockhash, notarization info if ( (rand() % 100) < 10 )
request.resize(1); {
request[0] = NSPV_INFO; // query current height, blockhash, notarization info
pto->PushMessage("getnSPV",request); request.resize(1);
request[0] = NSPV_INFO;
NSPV_lastinfo = pto->lastinfo = timestamp;
pto->PushMessage("getnSPV",request);
}
} }
} }

View File

@@ -204,6 +204,7 @@ public:
bool fWhitelisted; bool fWhitelisted;
double dPingTime; double dPingTime;
double dPingWait; double dPingWait;
uint32_t lastutxos,lastinfo;
std::string addrLocal; std::string addrLocal;
}; };