forked from dashpay/dash
-
Notifications
You must be signed in to change notification settings - Fork 718
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
And unify peer full sync request blockage time in chainparams.
- Loading branch information
Showing
6 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright (c) 2014-2020 The Dash Core developers | ||
// Copyright (c) 2022 The PIVX Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or https://www.opensource.org/licenses/mit-license.php. | ||
|
||
#include "tiertwo/netfulfilledman.h" | ||
#include "chainparams.h" | ||
#include "netaddress.h" | ||
#include "shutdown.h" | ||
#include "utiltime.h" | ||
|
||
CNetFulfilledRequestManager g_netfulfilledman; | ||
|
||
void CNetFulfilledRequestManager::AddFulfilledRequest(const CService& addr, const std::string& strRequest) | ||
{ | ||
LOCK(cs_mapFulfilledRequests); | ||
mapFulfilledRequests[addr][strRequest] = GetTime() + Params().FulfilledRequestExpireTime(); | ||
} | ||
|
||
bool CNetFulfilledRequestManager::HasFulfilledRequest(const CService& addr, const std::string& strRequest) const | ||
{ | ||
LOCK(cs_mapFulfilledRequests); | ||
auto it = mapFulfilledRequests.find(addr); | ||
if (it != mapFulfilledRequests.end()) { | ||
auto itReq = it->second.find(strRequest); | ||
if (itReq != it->second.end()) { | ||
return itReq->second > GetTime(); | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
void CNetFulfilledRequestManager::CheckAndRemove() | ||
{ | ||
LOCK(cs_mapFulfilledRequests); | ||
int64_t now = GetTime(); | ||
for (auto it = mapFulfilledRequests.begin(); it != mapFulfilledRequests.end();) { | ||
for (auto it_entry = it->second.begin(); it_entry != it->second.end();) { | ||
if (now > it_entry->second) { | ||
it_entry = it->second.erase(it_entry); | ||
} else { | ||
it_entry++; | ||
} | ||
} | ||
if (it->second.empty()) { | ||
it = mapFulfilledRequests.erase(it); | ||
} else { | ||
it++; | ||
} | ||
} | ||
} | ||
|
||
void CNetFulfilledRequestManager::Clear() | ||
{ | ||
LOCK(cs_mapFulfilledRequests); | ||
mapFulfilledRequests.clear(); | ||
} | ||
|
||
std::string CNetFulfilledRequestManager::ToString() const | ||
{ | ||
LOCK(cs_mapFulfilledRequests); | ||
std::ostringstream info; | ||
info << "Nodes with fulfilled requests: " << (int)mapFulfilledRequests.size(); | ||
return info.str(); | ||
} | ||
|
||
void CNetFulfilledRequestManager::DoMaintenance() | ||
{ | ||
if (ShutdownRequested()) return; | ||
CheckAndRemove(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright (c) 2014-2020 The Dash Core developers | ||
// Copyright (c) 2022 The PIVX Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or https://www.opensource.org/licenses/mit-license.php. | ||
|
||
#ifndef PIVX_NETFULFILLEDMAN_H | ||
#define PIVX_NETFULFILLEDMAN_H | ||
|
||
#include "serialize.h" | ||
#include "sync.h" | ||
|
||
#include <map> | ||
|
||
class CService; | ||
|
||
static const std::string NET_REQUESTS_CACHE_FILENAME = "netrequests.dat"; | ||
static const std::string NET_REQUESTS_CACHE_FILE_ID = "magicNetRequestsCache"; | ||
|
||
// Fulfilled requests are used to prevent nodes from asking the same data on sync | ||
// and being banned for doing it too often. | ||
class CNetFulfilledRequestManager | ||
{ | ||
private: | ||
typedef std::map<std::string, int64_t> fulfilledreqmapentry_t; | ||
typedef std::map<CService, fulfilledreqmapentry_t> fulfilledreqmap_t; | ||
|
||
// Keep track of what node has/was asked for and when | ||
fulfilledreqmap_t mapFulfilledRequests; | ||
mutable Mutex cs_mapFulfilledRequests; | ||
|
||
public: | ||
CNetFulfilledRequestManager() = default; | ||
|
||
SERIALIZE_METHODS(CNetFulfilledRequestManager, obj) { | ||
LOCK(obj.cs_mapFulfilledRequests); | ||
READWRITE(obj.mapFulfilledRequests); | ||
} | ||
|
||
void AddFulfilledRequest(const CService& addr, const std::string& strRequest); | ||
bool HasFulfilledRequest(const CService& addr, const std::string& strRequest) const; | ||
|
||
void CheckAndRemove(); | ||
void Clear(); | ||
|
||
std::string ToString() const; | ||
|
||
void DoMaintenance(); | ||
}; | ||
|
||
extern CNetFulfilledRequestManager g_netfulfilledman; | ||
|
||
#endif // PIVX_NETFULFILLEDMAN_H |