Line data Source code
1 : // Copyright (c) 2014-2020 The Dash Core developers 2 : // Copyright (c) 2022 The PIVX Core developers 3 : // Distributed under the MIT software license, see the accompanying 4 : // file COPYING or https://www.opensource.org/licenses/mit-license.php. 5 : 6 : #ifndef PIVX_TIERTWO_NETFULFILLEDMAN_H 7 : #define PIVX_TIERTWO_NETFULFILLEDMAN_H 8 : 9 : #include "bloom.h" 10 : #include "serialize.h" 11 : #include "sync.h" 12 : 13 : #include <map> 14 : 15 : class CBloomFilter; 16 : class CService; 17 : 18 : static const std::string NET_REQUESTS_CACHE_FILENAME = "netrequests.dat"; 19 : static const std::string NET_REQUESTS_CACHE_FILE_ID = "magicNetRequestsCache"; 20 : 21 : static const unsigned int DEFAULT_ITEMS_FILTER_SIZE = 250; 22 : static const unsigned int DEFAULT_ITEMS_FILTER_CLEANUP = 60 * 60; 23 : 24 : // Fulfilled requests are used to prevent nodes from asking the same data on sync 25 : // and being banned for doing it too often. 26 : class CNetFulfilledRequestManager 27 : { 28 : private: 29 : typedef std::map<std::string, int64_t> fulfilledreqmapentry_t; 30 : typedef std::map<CService, fulfilledreqmapentry_t> fulfilledreqmap_t; 31 : 32 : // Keep track of what node has/was asked for and when 33 : fulfilledreqmap_t mapFulfilledRequests GUARDED_BY(cs_mapFulfilledRequests); 34 : mutable Mutex cs_mapFulfilledRequests; 35 : 36 : std::unique_ptr<CBloomFilter> itemsFilter GUARDED_BY(cs_mapFulfilledRequests){nullptr}; 37 : unsigned int itemsFilterSize{0}; 38 : unsigned int itemsFilterCount{0}; 39 : int64_t filterCleanupTime{DEFAULT_ITEMS_FILTER_CLEANUP}; // for now, fixed cleanup time 40 : int64_t lastFilterCleanup{0}; 41 : 42 : public: 43 : explicit CNetFulfilledRequestManager(unsigned int itemsFilterSize); 44 : 45 922 : SERIALIZE_METHODS(CNetFulfilledRequestManager, obj) { 46 461 : LOCK(obj.cs_mapFulfilledRequests); 47 922 : READWRITE(obj.mapFulfilledRequests); 48 461 : } 49 : 50 : void AddFulfilledRequest(const CService& addr, const std::string& strRequest); 51 : bool HasFulfilledRequest(const CService& addr, const std::string& strRequest) const; 52 : 53 : // Faster lookup using bloom filter 54 : void AddItemRequest(const CService& addr, const uint256& itemHash); 55 : bool HasItemRequest(const CService& addr, const uint256& itemHash) const; 56 : 57 : void CheckAndRemove(); 58 : void Clear(); 59 : 60 : std::string ToString() const; 61 4 : int Size() const { return WITH_LOCK(cs_mapFulfilledRequests, return mapFulfilledRequests.size();); } 62 : 63 : void DoMaintenance(); 64 : }; 65 : 66 : extern CNetFulfilledRequestManager g_netfulfilledman; 67 : 68 : #endif // PIVX_TIERTWO_NETFULFILLEDMAN_H