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_UTXOSRESP 0x03
uint32_t NSPV_lastinfo,NSPV_lastutxos;
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 )
{
response.resize(1);
if ( len == 1 && request[0] == NSPV_INFO ) // info
{
response[0] = NSPV_INFORESP;
pfrom->PushMessage("nSPV",response);
if ( timestamp > pfrom->lastinfo + KOMODO_BLOCKTIME/2 )
{
response[0] = NSPV_INFORESP;
pfrom->lastinfo = timestamp;
pfrom->PushMessage("nSPV",response);
}
}
else if ( request[0] == NSPV_UTXOS )
{
response[0] = NSPV_UTXOSRESP;
pfrom->PushMessage("nSPV",response);
if ( timestamp > pfrom->lastutxos + KOMODO_BLOCKTIME/2 )
{
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)
{
std::vector<uint8_t> request;
std::vector<uint8_t> request; uint32_t timestamp = time(NULL);
// limit frequency!
if ( (pto->nServices & NODE_ADDRINDEX) != 0 )
if ( timestamp > NSPV_lastutxos + KOMODO_BLOCKTIME/2 )
{
// get utxo since lastheight
request.resize(1);
request[0] = NSPV_UTXOS;
pto->PushMessage("getnSPV",request);
if ( (pto->nServices & NODE_ADDRINDEX) != 0 && timestamp > ptr->lastutxos + KOMODO_BLOCKTIME )
{
// get utxo since lastheight
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
request.resize(1);
request[0] = NSPV_INFO;
pto->PushMessage("getnSPV",request);
if ( (rand() % 100) < 10 )
{
// query current height, blockhash, notarization info
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;
double dPingTime;
double dPingWait;
uint32_t lastutxos,lastinfo;
std::string addrLocal;
};