PIVX Core  5.6.99
P2P Digital Currency
netfulfilledman.cpp
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 
7 #include "chainparams.h"
8 #include "netaddress.h"
9 #include "shutdown.h"
10 #include "utiltime.h"
11 
13 
15 {
16  itemsFilterSize = _itemsFilterSize;
17  if (itemsFilterSize != 0) {
18  itemsFilter = std::make_unique<CBloomFilter>(itemsFilterSize, 0.001, 0, BLOOM_UPDATE_ALL);
19  }
20 }
21 
22 void CNetFulfilledRequestManager::AddFulfilledRequest(const CService& addr, const std::string& strRequest)
23 {
25  mapFulfilledRequests[addr][strRequest] = GetTime() + Params().FulfilledRequestExpireTime();
26 }
27 
28 bool CNetFulfilledRequestManager::HasFulfilledRequest(const CService& addr, const std::string& strRequest) const
29 {
31  auto it = mapFulfilledRequests.find(addr);
32  if (it != mapFulfilledRequests.end()) {
33  auto itReq = it->second.find(strRequest);
34  if (itReq != it->second.end()) {
35  return itReq->second > GetTime();
36  }
37  }
38  return false;
39 }
40 
41 static std::vector<unsigned char> convertElement(const CService& addr, const uint256& itemHash)
42 {
43  CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
44  stream << addr.GetAddrBytes();
45  stream << itemHash;
46  return {stream.begin(), stream.end()};
47 }
48 
50 {
52  assert(itemsFilter);
53  itemsFilter->insert(convertElement(addr, itemHash));
55 }
56 
57 bool CNetFulfilledRequestManager::HasItemRequest(const CService& addr, const uint256& itemHash) const
58 {
60  assert(itemsFilter);
61  return itemsFilter->contains(convertElement(addr, itemHash));
62 }
63 
65 {
67  int64_t now = GetTime();
68  for (auto it = mapFulfilledRequests.begin(); it != mapFulfilledRequests.end();) {
69  for (auto it_entry = it->second.begin(); it_entry != it->second.end();) {
70  if (now > it_entry->second) {
71  it_entry = it->second.erase(it_entry);
72  } else {
73  it_entry++;
74  }
75  }
76  if (it->second.empty()) {
77  it = mapFulfilledRequests.erase(it);
78  } else {
79  it++;
80  }
81  }
82 
84  itemsFilter->clear();
85  itemsFilterCount = 0;
87  }
88 }
89 
91 {
93  mapFulfilledRequests.clear();
94 }
95 
97 {
99  std::ostringstream info;
100  info << "Nodes with fulfilled requests: " << (int)mapFulfilledRequests.size();
101  return info.str();
102 }
103 
105 {
106  if (ShutdownRequested()) return;
107  CheckAndRemove();
108 }
@ BLOOM_UPDATE_ALL
Definition: bloom.h:27
const CChainParams & Params()
Return the currently selected parameters.
int FulfilledRequestExpireTime() const
Tier two requests blockage mark expiration time.
Definition: chainparams.h:102
std::vector< unsigned char > GetAddrBytes() const
Definition: netaddress.cpp:752
void AddFulfilledRequest(const CService &addr, const std::string &strRequest)
CNetFulfilledRequestManager(unsigned int itemsFilterSize)
void AddItemRequest(const CService &addr, const uint256 &itemHash)
std::string ToString() const
bool HasFulfilledRequest(const CService &addr, const std::string &strRequest) const
bool HasItemRequest(const CService &addr, const uint256 &itemHash) const
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:484
unsigned char * begin()
Definition: uint256.h:63
256-bit opaque blob.
Definition: uint256.h:138
@ LOCK
Definition: lockunlock.h:16
CNetFulfilledRequestManager g_netfulfilledman(DEFAULT_ITEMS_FILTER_SIZE)
@ SER_NETWORK
Definition: serialize.h:174
bool ShutdownRequested()
Definition: shutdown.cpp:22
int64_t GetTime()
DEPRECATED Use either GetSystemTimeInSeconds (not mockable) or GetTime<T> (mockable)
Definition: utiltime.cpp:27