PIVX Core  5.6.99
P2P Digital Currency
netfulfilledman.h
Go to the documentation of this file.
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.
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
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 
46  LOCK(obj.cs_mapFulfilledRequests);
47  READWRITE(obj.mapFulfilledRequests);
48  }
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  int Size() const { return WITH_LOCK(cs_mapFulfilledRequests, return mapFulfilledRequests.size();); }
62 
63  void DoMaintenance();
64 };
65 
67 
68 #endif // PIVX_TIERTWO_NETFULFILLEDMAN_H
BloomFilter is a probabilistic filter which SPV clients provide so that we can filter the transaction...
Definition: bloom.h:45
void AddFulfilledRequest(const CService &addr, const std::string &strRequest)
CNetFulfilledRequestManager(unsigned int itemsFilterSize)
void AddItemRequest(const CService &addr, const uint256 &itemHash)
std::map< CService, fulfilledreqmapentry_t > fulfilledreqmap_t
std::string ToString() const
bool HasFulfilledRequest(const CService &addr, const std::string &strRequest) const
bool HasItemRequest(const CService &addr, const uint256 &itemHash) const
std::map< std::string, int64_t > fulfilledreqmapentry_t
SERIALIZE_METHODS(CNetFulfilledRequestManager, obj)
fulfilledreqmap_t mapFulfilledRequests GUARDED_BY(cs_mapFulfilledRequests)
std::unique_ptr< CBloomFilter > itemsFilter GUARDED_BY(cs_mapFulfilledRequests)
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:484
256-bit opaque blob.
Definition: uint256.h:138
@ LOCK
Definition: lockunlock.h:16
CNetFulfilledRequestManager g_netfulfilledman
#define READWRITE(...)
Definition: serialize.h:183
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:247