Skip to content

Commit

Permalink
Create fulfilled requests manager.
Browse files Browse the repository at this point in the history
And unify peer full sync request blockage time in chainparams.
  • Loading branch information
furszy committed Feb 10, 2022
1 parent f79e009 commit ad60ad7
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ set(COMMON_SOURCES
./src/tiertwo/masternode_meta_manager.cpp
./src/evo/mnauth.cpp
./src/tiertwo/net_masternodes.cpp
./src/tiertwo/netfulfilledman.cpp
./src/tiertwo/tiertwo_sync_state.cpp
./src/warnings.cpp
)
Expand Down
2 changes: 2 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ BITCOIN_CORE_H = \
threadinterrupt.h \
timedata.h \
tinyformat.h \
tiertwo/netfulfilledman.h \
tiertwo/tiertwo_sync_state.h \
torcontrol.h \
txdb.h \
Expand Down Expand Up @@ -556,6 +557,7 @@ libbitcoin_common_a_SOURCES = \
script/sign.cpp \
script/standard.cpp \
tiertwo_networksync.cpp \
tiertwo/netfulfilledman.cpp \
tiertwo/tiertwo_sync_state.cpp \
warnings.cpp \
script/script_error.cpp \
Expand Down
9 changes: 9 additions & 0 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,9 @@ class CMainParams : public CChainParams
consensus.llmqs[Consensus::LLMQ_400_85] = llmq400_85;

nLLMQConnectionRetryTimeout = 60;

// Tier two
nFulfilledRequestExpireTime = 60 * 60; // fulfilled requests expire in 1 hour
}

const CCheckpointData& Checkpoints() const
Expand Down Expand Up @@ -490,6 +493,9 @@ class CTestNetParams : public CChainParams
consensus.llmqs[Consensus::LLMQ_400_85] = llmq400_85;

nLLMQConnectionRetryTimeout = 60;

// Tier two
nFulfilledRequestExpireTime = 60 * 60; // fulfilled requests expire in 1 hour
}

const CCheckpointData& Checkpoints() const
Expand Down Expand Up @@ -625,6 +631,9 @@ class CRegTestParams : public CChainParams
// long living quorum params
consensus.llmqs[Consensus::LLMQ_TEST] = llmq_test;
nLLMQConnectionRetryTimeout = 5;

// Tier two
nFulfilledRequestExpireTime = 60 * 60; // fulfilled requests expire in 1 hour
}

const CCheckpointData& Checkpoints() const
Expand Down
4 changes: 4 additions & 0 deletions src/chainparams.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ class CChainParams
bool IsRegTestNet() const { return NetworkIDString() == CBaseChainParams::REGTEST; }
bool IsTestnet() const { return NetworkIDString() == CBaseChainParams::TESTNET; }

/** Tier two requests blockage mark expiration time */
int FulfilledRequestExpireTime() const { return nFulfilledRequestExpireTime; }

void UpdateNetworkUpgradeParameters(Consensus::UpgradeIndex idx, int nActivationHeight);
protected:
CChainParams() {}
Expand All @@ -114,6 +117,7 @@ class CChainParams

// Tier two
int nLLMQConnectionRetryTimeout;
int nFulfilledRequestExpireTime;
};

/**
Expand Down
71 changes: 71 additions & 0 deletions src/tiertwo/netfulfilledman.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (c) 2014-2020 The Dash Core developers
// Copyright (c) 2022 The PIVX Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or https://www.opensource.org/licenses/mit-license.php.

#include "tiertwo/netfulfilledman.h"
#include "chainparams.h"
#include "netaddress.h"
#include "shutdown.h"
#include "utiltime.h"

CNetFulfilledRequestManager g_netfulfilledman;

void CNetFulfilledRequestManager::AddFulfilledRequest(const CService& addr, const std::string& strRequest)
{
LOCK(cs_mapFulfilledRequests);
mapFulfilledRequests[addr][strRequest] = GetTime() + Params().FulfilledRequestExpireTime();
}

bool CNetFulfilledRequestManager::HasFulfilledRequest(const CService& addr, const std::string& strRequest) const
{
LOCK(cs_mapFulfilledRequests);
auto it = mapFulfilledRequests.find(addr);
if (it != mapFulfilledRequests.end()) {
auto itReq = it->second.find(strRequest);
if (itReq != it->second.end()) {
return itReq->second > GetTime();
}
}
return false;
}

void CNetFulfilledRequestManager::CheckAndRemove()
{
LOCK(cs_mapFulfilledRequests);
int64_t now = GetTime();
for (auto it = mapFulfilledRequests.begin(); it != mapFulfilledRequests.end();) {
for (auto it_entry = it->second.begin(); it_entry != it->second.end();) {
if (now > it_entry->second) {
it_entry = it->second.erase(it_entry);
} else {
it_entry++;
}
}
if (it->second.empty()) {
it = mapFulfilledRequests.erase(it);
} else {
it++;
}
}
}

void CNetFulfilledRequestManager::Clear()
{
LOCK(cs_mapFulfilledRequests);
mapFulfilledRequests.clear();
}

std::string CNetFulfilledRequestManager::ToString() const
{
LOCK(cs_mapFulfilledRequests);
std::ostringstream info;
info << "Nodes with fulfilled requests: " << (int)mapFulfilledRequests.size();
return info.str();
}

void CNetFulfilledRequestManager::DoMaintenance()
{
if (ShutdownRequested()) return;
CheckAndRemove();
}
52 changes: 52 additions & 0 deletions src/tiertwo/netfulfilledman.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) 2014-2020 The Dash Core developers
// Copyright (c) 2022 The PIVX Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or https://www.opensource.org/licenses/mit-license.php.

#ifndef PIVX_NETFULFILLEDMAN_H
#define PIVX_NETFULFILLEDMAN_H

#include "serialize.h"
#include "sync.h"

#include <map>

class CService;

static const std::string NET_REQUESTS_CACHE_FILENAME = "netrequests.dat";
static const std::string NET_REQUESTS_CACHE_FILE_ID = "magicNetRequestsCache";

// Fulfilled requests are used to prevent nodes from asking the same data on sync
// and being banned for doing it too often.
class CNetFulfilledRequestManager
{
private:
typedef std::map<std::string, int64_t> fulfilledreqmapentry_t;
typedef std::map<CService, fulfilledreqmapentry_t> fulfilledreqmap_t;

// Keep track of what node has/was asked for and when
fulfilledreqmap_t mapFulfilledRequests;
mutable Mutex cs_mapFulfilledRequests;

public:
CNetFulfilledRequestManager() = default;

SERIALIZE_METHODS(CNetFulfilledRequestManager, obj) {
LOCK(obj.cs_mapFulfilledRequests);
READWRITE(obj.mapFulfilledRequests);
}

void AddFulfilledRequest(const CService& addr, const std::string& strRequest);
bool HasFulfilledRequest(const CService& addr, const std::string& strRequest) const;

void CheckAndRemove();
void Clear();

std::string ToString() const;

void DoMaintenance();
};

extern CNetFulfilledRequestManager g_netfulfilledman;

#endif // PIVX_NETFULFILLEDMAN_H

0 comments on commit ad60ad7

Please sign in to comment.