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 7126 : CNetFulfilledRequestManager::CNetFulfilledRequestManager() : 11 3563 : m_db{std::make_unique<db_type>("netfulfilled.dat", "magicFulfilledCache")} 12 3563 : { 13 3563 : } 14 : 15 7124 : CNetFulfilledRequestManager::~CNetFulfilledRequestManager() 16 3562 : { 17 3562 : if (!is_valid) return; 18 2858 : m_db->Store(*this); 19 7124 : } 20 : 21 2858 : bool CNetFulfilledRequestManager::LoadCache(bool load_cache) 22 : { 23 2858 : assert(m_db != nullptr); 24 2858 : is_valid = load_cache ? m_db->Load(*this) : m_db->Store(*this); 25 2858 : if (is_valid && load_cache) { 26 1963 : CheckAndRemove(); 27 1963 : } 28 2858 : return is_valid; 29 : } 30 : 31 3111 : void CNetFulfilledRequestManager::AddFulfilledRequest(const CService& addr, const std::string& strRequest) 32 : { 33 3111 : LOCK(cs_mapFulfilledRequests); 34 3111 : mapFulfilledRequests[addr][strRequest] = GetTime() + Params().FulfilledRequestExpireTime(); 35 3111 : } 36 : 37 11598 : bool CNetFulfilledRequestManager::HasFulfilledRequest(const CService& addr, const std::string& strRequest) 38 : { 39 11598 : LOCK(cs_mapFulfilledRequests); 40 11598 : fulfilledreqmap_t::iterator it = mapFulfilledRequests.find(addr); 41 : 42 11598 : return it != mapFulfilledRequests.end() && 43 9454 : it->second.find(strRequest) != it->second.end() && 44 4478 : it->second[strRequest] > GetTime(); 45 11598 : } 46 : 47 689 : void CNetFulfilledRequestManager::RemoveAllFulfilledRequests(const CService& addr) 48 : { 49 689 : LOCK(cs_mapFulfilledRequests); 50 689 : fulfilledreqmap_t::iterator it = mapFulfilledRequests.find(addr); 51 : 52 689 : if (it != mapFulfilledRequests.end()) { 53 158 : mapFulfilledRequests.erase(it++); 54 158 : } 55 689 : } 56 : 57 7322 : void CNetFulfilledRequestManager::CheckAndRemove() 58 : { 59 7322 : LOCK(cs_mapFulfilledRequests); 60 : 61 7322 : int64_t now = GetTime(); 62 7322 : fulfilledreqmap_t::iterator it = mapFulfilledRequests.begin(); 63 : 64 11066 : while(it != mapFulfilledRequests.end()) { 65 3744 : fulfilledreqmapentry_t::iterator it_entry = it->second.begin(); 66 11251 : while(it_entry != it->second.end()) { 67 7507 : if(now > it_entry->second) { 68 1047 : it->second.erase(it_entry++); 69 1047 : } else { 70 6460 : ++it_entry; 71 : } 72 : } 73 3744 : if(it->second.size() == 0) { 74 495 : mapFulfilledRequests.erase(it++); 75 495 : } else { 76 3249 : ++it; 77 : } 78 : } 79 7322 : } 80 : 81 0 : void NetFulfilledRequestStore::Clear() 82 : { 83 0 : LOCK(cs_mapFulfilledRequests); 84 0 : mapFulfilledRequests.clear(); 85 0 : } 86 : 87 7207 : std::string NetFulfilledRequestStore::ToString() const 88 : { 89 7207 : return strprintf("Nodes with fulfilled requests: %d", mapFulfilledRequests.size()); 90 : } 91 : 92 5380 : void CNetFulfilledRequestManager::DoMaintenance() 93 : { 94 5380 : if (ShutdownRequested()) return; 95 : 96 5359 : CheckAndRemove(); 97 5380 : }