Skip to content

Commit 57225c2

Browse files
committed
refactor: use std::chrono for governance time constants
1 parent 9ca9714 commit 57225c2

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
@@ -31,10 +31,13 @@
3131
int nSubmittedFinalBudget;
3232

3333
const std::string GovernanceStore::SERIALIZATION_VERSION_STRING = "CGovernanceManager-Version-16";
34-
const int CGovernanceManager::MAX_TIME_FUTURE_DEVIATION = 60 * 60;
35-
const int CGovernanceManager::RELIABLE_PROPAGATION_TIME = 60;
3634

3735
namespace {
36+
constexpr std::chrono::seconds GOVERNANCE_DELETION_DELAY{10min};
37+
constexpr std::chrono::seconds GOVERNANCE_ORPHAN_EXPIRATION_TIME{10min};
38+
constexpr std::chrono::seconds MAX_TIME_FUTURE_DEVIATION{1h};
39+
constexpr std::chrono::seconds RELIABLE_PROPAGATION_TIME{1min};
40+
3841
class ScopedLockBool
3942
{
4043
bool& ref;
@@ -490,7 +493,7 @@ void CGovernanceManager::CheckAndRemove()
490493
strHash, pObj->GetDeletionTime(), nTimeSinceDeletion, pObj->IsSetCachedDelete(), pObj->IsSetExpired());
491494

492495
if ((pObj->IsSetCachedDelete() || pObj->IsSetExpired()) &&
493-
(nTimeSinceDeletion >= GOVERNANCE_DELETION_DELAY)) {
496+
(nTimeSinceDeletion >= count_seconds(GOVERNANCE_DELETION_DELAY))) {
494497
LogPrint(BCLog::GOBJECT, "CGovernanceManager::UpdateCachesAndClean -- erase obj %s\n", (*it).first.ToString());
495498
m_mn_metaman.RemoveGovernanceObject(pObj->GetHash());
496499

@@ -514,7 +517,7 @@ void CGovernanceManager::CheckAndRemove()
514517
nTimeExpired = std::numeric_limits<int64_t>::max();
515518
} else {
516519
int64_t nSuperblockCycleSeconds = Params().GetConsensus().nSuperblockCycle * Params().GetConsensus().nPowTargetSpacing;
517-
nTimeExpired = pObj->GetCreationTime() + 2 * nSuperblockCycleSeconds + GOVERNANCE_DELETION_DELAY;
520+
nTimeExpired = pObj->GetCreationTime() + 2 * nSuperblockCycleSeconds + count_seconds(GOVERNANCE_DELETION_DELAY);
518521
}
519522

520523
mapErasedGovernanceObjects.insert(std::make_pair(nHash, nTimeExpired));
@@ -694,7 +697,7 @@ bool CGovernanceManager::ConfirmInventoryRequest(const CInv& inv)
694697
return false;
695698
}
696699

697-
const auto valid_until = GetTime<std::chrono::seconds>() + std::chrono::seconds(RELIABLE_PROPAGATION_TIME);
700+
const auto valid_until = GetTime<std::chrono::seconds>() + RELIABLE_PROPAGATION_TIME;
698701
const auto& [_itr, inserted] = m_requested_hash_time.emplace(inv.hash, valid_until);
699702

700703
if (inserted) {
@@ -820,7 +823,7 @@ void CGovernanceManager::MasternodeRateUpdate(const CGovernanceObject& govobj)
820823
int64_t nTimestamp = govobj.GetCreationTime();
821824
it->second.triggerBuffer.AddTimestamp(nTimestamp);
822825

823-
if (nTimestamp > GetTime() + MAX_TIME_FUTURE_DEVIATION - RELIABLE_PROPAGATION_TIME) {
826+
if (nTimestamp > GetTime() + count_seconds(MAX_TIME_FUTURE_DEVIATION) - count_seconds(RELIABLE_PROPAGATION_TIME)) {
824827
// schedule additional relay for the object
825828
setAdditionalRelayObjects.insert(govobj.GetHash());
826829
}
@@ -861,7 +864,7 @@ bool CGovernanceManager::MasternodeRateCheck(const CGovernanceObject& govobj, bo
861864
return false;
862865
}
863866

864-
if (nTimestamp > nNow + MAX_TIME_FUTURE_DEVIATION) {
867+
if (nTimestamp > nNow + count_seconds(MAX_TIME_FUTURE_DEVIATION)) {
865868
LogPrint(BCLog::GOBJECT, "CGovernanceManager::MasternodeRateCheck -- object %s rejected due to too new (future) timestamp, masternode = %s, timestamp = %d, current time = %d\n",
866869
strHash, masternodeOutpoint.ToStringShort(), nTimestamp, nNow);
867870
return false;
@@ -935,7 +938,7 @@ bool CGovernanceManager::ProcessVote(CNode* pfrom, const CGovernanceVote& vote,
935938
std::string msg{strprintf("CGovernanceManager::%s -- Unknown parent object %s, MN outpoint = %s", __func__,
936939
nHashGovobj.ToString(), vote.GetMasternodeOutpoint().ToStringShort())};
937940
exception = CGovernanceException(msg, GOVERNANCE_EXCEPTION_WARNING);
938-
if (cmmapOrphanVotes.Insert(nHashGovobj, vote_time_pair_t(vote, GetTime<std::chrono::seconds>().count() + GOVERNANCE_ORPHAN_EXPIRATION_TIME))) {
941+
if (cmmapOrphanVotes.Insert(nHashGovobj, vote_time_pair_t(vote, count_seconds(GetTime<std::chrono::seconds>() + GOVERNANCE_ORPHAN_EXPIRATION_TIME)))) {
939942
LEAVE_CRITICAL_SECTION(cs);
940943
RequestGovernanceObject(pfrom, nHashGovobj, connman);
941944
LogPrint(BCLog::GOBJECT, "%s\n", msg);
@@ -1006,8 +1009,8 @@ void CGovernanceManager::CheckPostponedObjects()
10061009

10071010
int64_t nTimestamp = govobj.GetCreationTime();
10081011

1009-
bool fValid = (nTimestamp <= nNow + MAX_TIME_FUTURE_DEVIATION) && (nTimestamp >= nNow - 2 * nSuperblockCycleSeconds);
1010-
bool fReady = (nTimestamp <= nNow + MAX_TIME_FUTURE_DEVIATION - RELIABLE_PROPAGATION_TIME);
1012+
bool fValid = (nTimestamp <= nNow + count_seconds(MAX_TIME_FUTURE_DEVIATION)) && (nTimestamp >= nNow - 2 * nSuperblockCycleSeconds);
1013+
bool fReady = (nTimestamp <= nNow + count_seconds(MAX_TIME_FUTURE_DEVIATION) - count_seconds(RELIABLE_PROPAGATION_TIME));
10111014

10121015
if (fValid) {
10131016
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)