Skip to content

Commit f2adedc

Browse files
committed
refactor: use std::chrono for governance time constants
1 parent 82db198 commit f2adedc

File tree

4 files changed

+20
-22
lines changed

4 files changed

+20
-22
lines changed

src/governance/governance.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@
2929
#include <validation.h>
3030

3131
const std::string GovernanceStore::SERIALIZATION_VERSION_STRING = "CGovernanceManager-Version-16";
32-
const int CGovernanceManager::MAX_TIME_FUTURE_DEVIATION = 60 * 60;
33-
const int CGovernanceManager::RELIABLE_PROPAGATION_TIME = 60;
3432

3533
namespace {
34+
constexpr std::chrono::seconds GOVERNANCE_DELETION_DELAY{10min};
35+
constexpr std::chrono::seconds GOVERNANCE_ORPHAN_EXPIRATION_TIME{10min};
36+
constexpr std::chrono::seconds MAX_TIME_FUTURE_DEVIATION{1h};
37+
constexpr std::chrono::seconds RELIABLE_PROPAGATION_TIME{1min};
38+
3639
class ScopedLockBool
3740
{
3841
bool& ref;
@@ -485,7 +488,7 @@ void CGovernanceManager::CheckAndRemove()
485488
strHash, pObj->GetDeletionTime(), nTimeSinceDeletion, pObj->IsSetCachedDelete(), pObj->IsSetExpired());
486489

487490
if ((pObj->IsSetCachedDelete() || pObj->IsSetExpired()) &&
488-
(nTimeSinceDeletion >= GOVERNANCE_DELETION_DELAY)) {
491+
(nTimeSinceDeletion >= count_seconds(GOVERNANCE_DELETION_DELAY))) {
489492
LogPrint(BCLog::GOBJECT, "CGovernanceManager::UpdateCachesAndClean -- erase obj %s\n", (*it).first.ToString());
490493
m_mn_metaman.RemoveGovernanceObject(pObj->GetHash());
491494

@@ -509,7 +512,7 @@ void CGovernanceManager::CheckAndRemove()
509512
nTimeExpired = std::numeric_limits<int64_t>::max();
510513
} else {
511514
int64_t nSuperblockCycleSeconds = Params().GetConsensus().nSuperblockCycle * Params().GetConsensus().nPowTargetSpacing;
512-
nTimeExpired = pObj->GetCreationTime() + 2 * nSuperblockCycleSeconds + GOVERNANCE_DELETION_DELAY;
515+
nTimeExpired = pObj->GetCreationTime() + 2 * nSuperblockCycleSeconds + count_seconds(GOVERNANCE_DELETION_DELAY);
513516
}
514517

515518
mapErasedGovernanceObjects.insert(std::make_pair(nHash, nTimeExpired));
@@ -678,7 +681,7 @@ bool CGovernanceManager::ConfirmInventoryRequest(const CInv& inv)
678681
return false;
679682
}
680683

681-
const auto valid_until = GetTime<std::chrono::seconds>() + std::chrono::seconds(RELIABLE_PROPAGATION_TIME);
684+
const auto valid_until = GetTime<std::chrono::seconds>() + RELIABLE_PROPAGATION_TIME;
682685
const auto& [_itr, inserted] = m_requested_hash_time.emplace(inv.hash, valid_until);
683686

684687
if (inserted) {
@@ -804,7 +807,7 @@ void CGovernanceManager::MasternodeRateUpdate(const CGovernanceObject& govobj)
804807
int64_t nTimestamp = govobj.GetCreationTime();
805808
it->second.triggerBuffer.AddTimestamp(nTimestamp);
806809

807-
if (nTimestamp > GetTime() + MAX_TIME_FUTURE_DEVIATION - RELIABLE_PROPAGATION_TIME) {
810+
if (nTimestamp > GetTime() + count_seconds(MAX_TIME_FUTURE_DEVIATION) - count_seconds(RELIABLE_PROPAGATION_TIME)) {
808811
// schedule additional relay for the object
809812
setAdditionalRelayObjects.insert(govobj.GetHash());
810813
}
@@ -845,7 +848,7 @@ bool CGovernanceManager::MasternodeRateCheck(const CGovernanceObject& govobj, bo
845848
return false;
846849
}
847850

848-
if (nTimestamp > nNow + MAX_TIME_FUTURE_DEVIATION) {
851+
if (nTimestamp > nNow + count_seconds(MAX_TIME_FUTURE_DEVIATION)) {
849852
LogPrint(BCLog::GOBJECT, "CGovernanceManager::MasternodeRateCheck -- object %s rejected due to too new (future) timestamp, masternode = %s, timestamp = %d, current time = %d\n",
850853
strHash, masternodeOutpoint.ToStringShort(), nTimestamp, nNow);
851854
return false;
@@ -919,7 +922,7 @@ bool CGovernanceManager::ProcessVote(CNode* pfrom, const CGovernanceVote& vote,
919922
std::string msg{strprintf("CGovernanceManager::%s -- Unknown parent object %s, MN outpoint = %s", __func__,
920923
nHashGovobj.ToString(), vote.GetMasternodeOutpoint().ToStringShort())};
921924
exception = CGovernanceException(msg, GOVERNANCE_EXCEPTION_WARNING);
922-
if (cmmapOrphanVotes.Insert(nHashGovobj, vote_time_pair_t(vote, GetTime<std::chrono::seconds>().count() + GOVERNANCE_ORPHAN_EXPIRATION_TIME))) {
925+
if (cmmapOrphanVotes.Insert(nHashGovobj, vote_time_pair_t(vote, count_seconds(GetTime<std::chrono::seconds>() + GOVERNANCE_ORPHAN_EXPIRATION_TIME)))) {
923926
LEAVE_CRITICAL_SECTION(cs);
924927
RequestGovernanceObject(pfrom, nHashGovobj, connman);
925928
LogPrint(BCLog::GOBJECT, "%s\n", msg);
@@ -990,8 +993,8 @@ void CGovernanceManager::CheckPostponedObjects()
990993

991994
int64_t nTimestamp = govobj.GetCreationTime();
992995

993-
bool fValid = (nTimestamp <= nNow + MAX_TIME_FUTURE_DEVIATION) && (nTimestamp >= nNow - 2 * nSuperblockCycleSeconds);
994-
bool fReady = (nTimestamp <= nNow + MAX_TIME_FUTURE_DEVIATION - RELIABLE_PROPAGATION_TIME);
996+
bool fValid = (nTimestamp <= nNow + count_seconds(MAX_TIME_FUTURE_DEVIATION)) && (nTimestamp >= nNow - 2 * nSuperblockCycleSeconds);
997+
bool fReady = (nTimestamp <= nNow + count_seconds(MAX_TIME_FUTURE_DEVIATION) - count_seconds(RELIABLE_PROPAGATION_TIME));
995998

996999
if (fValid) {
9971000
if (fReady) {

src/governance/governance.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,6 @@ class CGovernanceManager : public GovernanceStore, public GovernanceSignerParent
246246
private:
247247
using db_type = CFlatDB<GovernanceStore>;
248248

249-
private:
250-
static const int MAX_TIME_FUTURE_DEVIATION;
251-
static const int RELIABLE_PROPAGATION_TIME;
252-
253249
private:
254250
const std::unique_ptr<db_type> m_db;
255251
bool is_valid{false};

src/governance/object.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,10 @@ class CNode;
2727
extern RecursiveMutex cs_main;
2828

2929
static constexpr double GOVERNANCE_FILTER_FP_RATE = 0.001;
30-
31-
3230
static constexpr CAmount GOVERNANCE_PROPOSAL_FEE_TX = (1 * COIN);
33-
3431
static constexpr int64_t GOVERNANCE_FEE_CONFIRMATIONS = 6;
3532
static constexpr int64_t GOVERNANCE_MIN_RELAY_FEE_CONFIRMATIONS = 1;
3633
static constexpr int64_t GOVERNANCE_UPDATE_MIN = 60 * 60;
37-
static constexpr int64_t GOVERNANCE_DELETION_DELAY = 10 * 60;
38-
static constexpr int64_t GOVERNANCE_ORPHAN_EXPIRATION_TIME = 10 * 60;
39-
static constexpr int64_t GOVERNANCE_FUDGE_WINDOW = 60 * 60 * 2;
4034

4135
// FOR SEEN MAP ARRAYS - GOVERNANCE OBJECTS AND VOTES
4236
enum class SeenObjectStatus {

src/governance/signing.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,18 @@
88
#include <logging.h>
99
#include <timedata.h>
1010
#include <util/check.h>
11+
#include <util/time.h>
1112
#include <validation.h>
1213

1314
#include <evo/deterministicmns.h>
1415
#include <governance/classes.h>
1516
#include <masternode/node.h>
1617
#include <masternode/sync.h>
1718

19+
namespace {
20+
constexpr std::chrono::seconds GOVERNANCE_FUDGE_WINDOW{2h};
21+
} // anonymous namespace
22+
1823
GovernanceSigner::GovernanceSigner(CConnman& connman, CDeterministicMNManager& dmnman, GovernanceSignerParent& govman,
1924
const CActiveMasternodeManager& mn_activeman, const ChainstateManager& chainman,
2025
const CMasternodeSync& mn_sync) :
@@ -74,8 +79,8 @@ std::optional<const CSuperblock> GovernanceSigner::CreateSuperblockCandidate(int
7479
// Skip proposals that are too expensive
7580
if (budgetAllocated + payment.nAmount > governanceBudget) continue;
7681

77-
int64_t windowStart = jproposal["start_epoch"].getInt<int64_t>() - GOVERNANCE_FUDGE_WINDOW;
78-
int64_t windowEnd = jproposal["end_epoch"].getInt<int64_t>() + GOVERNANCE_FUDGE_WINDOW;
82+
int64_t windowStart = jproposal["start_epoch"].getInt<int64_t>() - count_seconds(GOVERNANCE_FUDGE_WINDOW);
83+
int64_t windowEnd = jproposal["end_epoch"].getInt<int64_t>() + count_seconds(GOVERNANCE_FUDGE_WINDOW);
7984

8085
// Skip proposals if the SB isn't within the proposal time window
8186
if (SBEpochTime < windowStart) {

0 commit comments

Comments
 (0)