Line data Source code
1 : // Copyright (c) 2014-2025 The Dash Core developers 2 : // Distributed under the MIT/X11 software license, see the accompanying 3 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 : 5 : #include <chainparams.h> 6 : #include <flat-database.h> 7 : #include <netfulfilledman.h> 8 : #include <shutdown.h> 9 : 10 1254 : CNetFulfilledRequestManager::CNetFulfilledRequestManager() : 11 627 : m_db{std::make_unique<db_type>("netfulfilled.dat", "magicFulfilledCache")} 12 627 : { 13 627 : } 14 : 15 1252 : CNetFulfilledRequestManager::~CNetFulfilledRequestManager() 16 626 : { 17 626 : if (!is_valid) return; 18 0 : m_db->Store(*this); 19 1252 : } 20 : 21 0 : bool CNetFulfilledRequestManager::LoadCache(bool load_cache) 22 : { 23 0 : assert(m_db != nullptr); 24 0 : is_valid = load_cache ? m_db->Load(*this) : m_db->Store(*this); 25 0 : if (is_valid && load_cache) { 26 0 : CheckAndRemove(); 27 0 : } 28 0 : return is_valid; 29 : } 30 : 31 0 : void CNetFulfilledRequestManager::AddFulfilledRequest(const CService& addr, const std::string& strRequest) 32 : { 33 0 : LOCK(cs_mapFulfilledRequests); 34 0 : mapFulfilledRequests[addr][strRequest] = GetTime() + Params().FulfilledRequestExpireTime(); 35 0 : } 36 : 37 0 : bool CNetFulfilledRequestManager::HasFulfilledRequest(const CService& addr, const std::string& strRequest) 38 : { 39 0 : LOCK(cs_mapFulfilledRequests); 40 0 : fulfilledreqmap_t::iterator it = mapFulfilledRequests.find(addr); 41 : 42 0 : return it != mapFulfilledRequests.end() && 43 0 : it->second.find(strRequest) != it->second.end() && 44 0 : it->second[strRequest] > GetTime(); 45 0 : } 46 : 47 0 : void CNetFulfilledRequestManager::RemoveAllFulfilledRequests(const CService& addr) 48 : { 49 0 : LOCK(cs_mapFulfilledRequests); 50 0 : fulfilledreqmap_t::iterator it = mapFulfilledRequests.find(addr); 51 : 52 0 : if (it != mapFulfilledRequests.end()) { 53 0 : mapFulfilledRequests.erase(it++); 54 0 : } 55 0 : } 56 : 57 0 : void CNetFulfilledRequestManager::CheckAndRemove() 58 : { 59 0 : LOCK(cs_mapFulfilledRequests); 60 : 61 0 : int64_t now = GetTime(); 62 0 : fulfilledreqmap_t::iterator it = mapFulfilledRequests.begin(); 63 : 64 0 : while(it != mapFulfilledRequests.end()) { 65 0 : fulfilledreqmapentry_t::iterator it_entry = it->second.begin(); 66 0 : while(it_entry != it->second.end()) { 67 0 : if(now > it_entry->second) { 68 0 : it->second.erase(it_entry++); 69 0 : } else { 70 0 : ++it_entry; 71 : } 72 : } 73 0 : if(it->second.size() == 0) { 74 0 : mapFulfilledRequests.erase(it++); 75 0 : } else { 76 0 : ++it; 77 : } 78 : } 79 0 : } 80 : 81 0 : void NetFulfilledRequestStore::Clear() 82 : { 83 0 : LOCK(cs_mapFulfilledRequests); 84 0 : mapFulfilledRequests.clear(); 85 0 : } 86 : 87 0 : std::string NetFulfilledRequestStore::ToString() const 88 : { 89 0 : return strprintf("Nodes with fulfilled requests: %d", mapFulfilledRequests.size()); 90 : } 91 : 92 0 : void CNetFulfilledRequestManager::DoMaintenance() 93 : { 94 0 : if (ShutdownRequested()) return; 95 : 96 0 : CheckAndRemove(); 97 0 : }