2019-01-29 15:53:14 +01:00
|
|
|
// Copyright (c) 2014-2018 The Dash Core developers
|
2016-09-27 09:50:04 +02:00
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include "chainparams.h"
|
2018-11-01 22:58:17 +01:00
|
|
|
#include "init.h"
|
2016-09-27 09:50:04 +02:00
|
|
|
#include "netfulfilledman.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
CNetFulfilledRequestManager netfulfilledman;
|
|
|
|
|
2018-03-08 13:16:52 +01:00
|
|
|
void CNetFulfilledRequestManager::AddFulfilledRequest(const CService& addr, const std::string& strRequest)
|
2016-09-27 09:50:04 +02:00
|
|
|
{
|
|
|
|
LOCK(cs_mapFulfilledRequests);
|
2018-03-08 13:16:52 +01:00
|
|
|
CService addrSquashed = Params().AllowMultiplePorts() ? addr : CService(addr, 0);
|
|
|
|
mapFulfilledRequests[addrSquashed][strRequest] = GetTime() + Params().FulfilledRequestExpireTime();
|
2016-09-27 09:50:04 +02:00
|
|
|
}
|
|
|
|
|
2018-03-08 13:16:52 +01:00
|
|
|
bool CNetFulfilledRequestManager::HasFulfilledRequest(const CService& addr, const std::string& strRequest)
|
2016-09-27 09:50:04 +02:00
|
|
|
{
|
|
|
|
LOCK(cs_mapFulfilledRequests);
|
2018-03-08 13:16:52 +01:00
|
|
|
CService addrSquashed = Params().AllowMultiplePorts() ? addr : CService(addr, 0);
|
|
|
|
fulfilledreqmap_t::iterator it = mapFulfilledRequests.find(addrSquashed);
|
2016-09-27 09:50:04 +02:00
|
|
|
|
|
|
|
return it != mapFulfilledRequests.end() &&
|
|
|
|
it->second.find(strRequest) != it->second.end() &&
|
|
|
|
it->second[strRequest] > GetTime();
|
|
|
|
}
|
|
|
|
|
2018-03-08 13:16:52 +01:00
|
|
|
void CNetFulfilledRequestManager::RemoveFulfilledRequest(const CService& addr, const std::string& strRequest)
|
2016-09-27 09:50:04 +02:00
|
|
|
{
|
|
|
|
LOCK(cs_mapFulfilledRequests);
|
2018-03-08 13:16:52 +01:00
|
|
|
CService addrSquashed = Params().AllowMultiplePorts() ? addr : CService(addr, 0);
|
|
|
|
fulfilledreqmap_t::iterator it = mapFulfilledRequests.find(addrSquashed);
|
2016-09-27 09:50:04 +02:00
|
|
|
|
|
|
|
if (it != mapFulfilledRequests.end()) {
|
|
|
|
it->second.erase(strRequest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CNetFulfilledRequestManager::CheckAndRemove()
|
|
|
|
{
|
|
|
|
LOCK(cs_mapFulfilledRequests);
|
|
|
|
|
|
|
|
int64_t now = GetTime();
|
|
|
|
fulfilledreqmap_t::iterator it = mapFulfilledRequests.begin();
|
|
|
|
|
|
|
|
while(it != mapFulfilledRequests.end()) {
|
|
|
|
fulfilledreqmapentry_t::iterator it_entry = it->second.begin();
|
|
|
|
while(it_entry != it->second.end()) {
|
|
|
|
if(now > it_entry->second) {
|
|
|
|
it->second.erase(it_entry++);
|
|
|
|
} else {
|
|
|
|
++it_entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(it->second.size() == 0) {
|
|
|
|
mapFulfilledRequests.erase(it++);
|
|
|
|
} else {
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CNetFulfilledRequestManager::Clear()
|
|
|
|
{
|
|
|
|
LOCK(cs_mapFulfilledRequests);
|
|
|
|
mapFulfilledRequests.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CNetFulfilledRequestManager::ToString() const
|
|
|
|
{
|
|
|
|
std::ostringstream info;
|
|
|
|
info << "Nodes with fulfilled requests: " << (int)mapFulfilledRequests.size();
|
|
|
|
return info.str();
|
|
|
|
}
|
2018-11-01 22:58:17 +01:00
|
|
|
|
|
|
|
void CNetFulfilledRequestManager::DoMaintenance()
|
|
|
|
{
|
|
|
|
if (ShutdownRequested()) return;
|
|
|
|
|
|
|
|
CheckAndRemove();
|
|
|
|
}
|