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 : #ifndef BITCOIN_NETFULFILLEDMAN_H 6 : #define BITCOIN_NETFULFILLEDMAN_H 7 : 8 : #include <netaddress.h> 9 : #include <serialize.h> 10 : #include <sync.h> 11 : 12 : #include <memory> 13 : 14 : template<typename T> 15 : class CFlatDB; 16 : class CNetFulfilledRequestManager; 17 : 18 7316 : class NetFulfilledRequestStore 19 : { 20 : protected: 21 : typedef std::map<std::string, int64_t> fulfilledreqmapentry_t; 22 : typedef std::map<CService, fulfilledreqmapentry_t> fulfilledreqmap_t; 23 : 24 : protected: 25 : //keep track of what node has/was asked for and when 26 : fulfilledreqmap_t mapFulfilledRequests; 27 : mutable RecursiveMutex cs_mapFulfilledRequests; 28 : 29 : public: 30 21621 : SERIALIZE_METHODS(NetFulfilledRequestStore, obj) 31 : { 32 7207 : LOCK(obj.cs_mapFulfilledRequests); 33 7207 : READWRITE(obj.mapFulfilledRequests); 34 7207 : } 35 : 36 : void Clear(); 37 : 38 : std::string ToString() const; 39 : }; 40 : 41 : // Fulfilled requests are used to prevent nodes from asking for the same data on sync 42 : // and from being banned for doing so too often. 43 : class CNetFulfilledRequestManager : public NetFulfilledRequestStore 44 : { 45 : private: 46 : using db_type = CFlatDB<NetFulfilledRequestStore>; 47 : 48 : private: 49 : const std::unique_ptr<db_type> m_db; 50 : bool is_valid{false}; 51 : 52 : public: 53 : CNetFulfilledRequestManager(const CNetFulfilledRequestManager&) = delete; 54 : CNetFulfilledRequestManager& operator=(const CNetFulfilledRequestManager&) = delete; 55 : CNetFulfilledRequestManager(); 56 : ~CNetFulfilledRequestManager(); 57 : 58 : bool LoadCache(bool load_cache); 59 : 60 95750 : bool IsValid() const { return is_valid; } 61 : void CheckAndRemove(); 62 : 63 : void AddFulfilledRequest(const CService& addr, const std::string& strRequest); 64 : bool HasFulfilledRequest(const CService& addr, const std::string& strRequest); 65 : 66 : void RemoveAllFulfilledRequests(const CService& addr); 67 : 68 : void DoMaintenance(); 69 : }; 70 : 71 : #endif // BITCOIN_NETFULFILLEDMAN_H