From 4b876b61188674924ee51ac1156b04069cbaf65a Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 2 Jul 2019 05:30:56 -1100 Subject: [PATCH] Throttle rate of nSPV requests --- src/komodo_nSPV.h | 53 +++++++++++++++++++++++++++++++++-------------- src/net.h | 1 + 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/src/komodo_nSPV.h b/src/komodo_nSPV.h index 6d2efcac2..bb796b167 100644 --- a/src/komodo_nSPV.h +++ b/src/komodo_nSPV.h @@ -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 request) // received a request { - int32_t len; std::vector response; + int32_t len; std::vector 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 response) // received a r void komodo_nSPV(CNode *pto) { - std::vector request; + std::vector 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); + } } } diff --git a/src/net.h b/src/net.h index bc0352bd6..fbe756c85 100644 --- a/src/net.h +++ b/src/net.h @@ -204,6 +204,7 @@ public: bool fWhitelisted; double dPingTime; double dPingWait; + uint32_t lastutxos,lastinfo; std::string addrLocal; };