Skip to content

Commit bd6af83

Browse files
committed
refactor: separate masternode mode logic into dedicated signer class
1 parent 2b74e15 commit bd6af83

File tree

15 files changed

+204
-118
lines changed

15 files changed

+204
-118
lines changed

src/Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,12 @@ BITCOIN_CORE_H = \
222222
evo/specialtxman.h \
223223
evo/types.h \
224224
dsnotificationinterface.h \
225-
governance/governance.h \
226225
governance/classes.h \
227226
governance/common.h \
227+
governance/governance.h \
228228
governance/exceptions.h \
229229
governance/object.h \
230+
governance/signing.h \
230231
governance/validators.h \
231232
governance/vote.h \
232233
governance/votedb.h \

src/dsnotificationinterface.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ CDSNotificationInterface::CDSNotificationInterface(CConnman& connman,
2626
CGovernanceManager& govman,
2727
PeerManager& peerman,
2828
const ChainstateManager& chainman,
29-
const CActiveMasternodeManager* const mn_activeman,
3029
const std::unique_ptr<CDeterministicMNManager>& dmnman,
3130
const std::unique_ptr<LLMQContext>& llmq_ctx,
3231
const std::unique_ptr<CJContext>& cj_ctx)
@@ -35,7 +34,6 @@ CDSNotificationInterface::CDSNotificationInterface(CConnman& connman,
3534
m_govman(govman),
3635
m_peerman(peerman),
3736
m_chainman(chainman),
38-
m_mn_activeman(mn_activeman),
3937
m_dmnman(dmnman),
4038
m_llmq_ctx(llmq_ctx),
4139
m_cj_ctx(cj_ctx) {}
@@ -94,7 +92,7 @@ void CDSNotificationInterface::UpdatedBlockTip(const CBlockIndex *pindexNew, con
9492
m_llmq_ctx->qdkgsman->UpdatedBlockTip(pindexNew, fInitialDownload);
9593

9694
if (m_govman.IsValid()) {
97-
m_govman.UpdatedBlockTip(pindexNew, m_connman, m_peerman, m_mn_activeman);
95+
m_govman.UpdatedBlockTip(pindexNew, m_peerman);
9896
}
9997
}
10098

src/dsnotificationinterface.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
#include <validationinterface.h>
99

10-
class CActiveMasternodeManager;
1110
class CConnman;
1211
class CDeterministicMNManager;
1312
class CGovernanceManager;
@@ -25,7 +24,6 @@ class CDSNotificationInterface : public CValidationInterface
2524
CGovernanceManager& govman,
2625
PeerManager& peerman,
2726
const ChainstateManager& chainman,
28-
const CActiveMasternodeManager* const mn_activeman,
2927
const std::unique_ptr<CDeterministicMNManager>& dmnman,
3028
const std::unique_ptr<LLMQContext>& llmq_ctx,
3129
const std::unique_ptr<CJContext>& cj_ctx);
@@ -54,7 +52,6 @@ class CDSNotificationInterface : public CValidationInterface
5452
CGovernanceManager& m_govman;
5553
PeerManager& m_peerman;
5654
const ChainstateManager& m_chainman;
57-
const CActiveMasternodeManager* const m_mn_activeman;
5855
const std::unique_ptr<CDeterministicMNManager>& m_dmnman;
5956
const std::unique_ptr<LLMQContext>& m_llmq_ctx;
6057
const std::unique_ptr<CJContext>& m_cj_ctx;

src/governance/governance.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include <governance/common.h>
1515
#include <governance/validators.h>
1616
#include <masternode/meta.h>
17-
#include <masternode/node.h>
1817
#include <masternode/sync.h>
1918
#include <net_processing.h>
2019
#include <netfulfilledman.h>
@@ -75,6 +74,7 @@ CGovernanceManager::CGovernanceManager(CMasternodeMetaMan& mn_metaman, CNetFulfi
7574
m_mn_sync{mn_sync},
7675
nTimeLastDiff(0),
7776
nCachedBlockHeight(0),
77+
mapPostponedObjects(),
7878
fRateChecksEnabled(true),
7979
votedFundingYesTriggerHash(std::nullopt),
8080
mapTrigger{}
@@ -140,6 +140,12 @@ bool CGovernanceManager::SerializeVoteForHash(const uint256& nHash, CDataStream&
140140
return cmapVoteToObject.Get(nHash, pGovobj) && pGovobj->GetVoteFile().SerializeVoteToStream(nHash, ss);
141141
}
142142

143+
void CGovernanceManager::AddPostponedObject(const CGovernanceObject& govobj)
144+
{
145+
LOCK(cs);
146+
mapPostponedObjects.insert(std::make_pair(govobj.GetHash(), govobj));
147+
}
148+
143149
MessageProcessingResult CGovernanceManager::ProcessMessage(CNode& peer, CConnman& connman, PeerManager& peerman, std::string_view msg_type, CDataStream& vRecv)
144150
{
145151
if (!IsValid()) return {};
@@ -998,6 +1004,11 @@ void CGovernanceManager::RequestGovernanceObject(CNode* pfrom, const uint256& nH
9981004
connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::MNGOVERNANCESYNC, nHash, filter));
9991005
}
10001006

1007+
void CGovernanceManager::AddInvalidVote(const CGovernanceVote& vote)
1008+
{
1009+
cmapInvalidVotes.Insert(vote.GetHash(), vote);
1010+
}
1011+
10011012
int CGovernanceManager::RequestGovernanceObjectVotes(CNode& peer, CConnman& connman, const PeerManager& peerman) const
10021013
{
10031014
const std::vector<CNode*> vNodeCopy{&peer};
@@ -1246,7 +1257,7 @@ UniValue CGovernanceManager::ToJson() const
12461257
return jsonObj;
12471258
}
12481259

1249-
void CGovernanceManager::UpdatedBlockTip(const CBlockIndex* pindex, CConnman& connman, PeerManager& peerman, const CActiveMasternodeManager* const mn_activeman)
1260+
void CGovernanceManager::UpdatedBlockTip(const CBlockIndex* pindex, PeerManager& peerman)
12501261
{
12511262
// Note this gets called from ActivateBestChain without cs_main being held
12521263
// so it should be safe to lock our mutex here without risking a deadlock
@@ -1257,12 +1268,6 @@ void CGovernanceManager::UpdatedBlockTip(const CBlockIndex* pindex, CConnman& co
12571268
return;
12581269
}
12591270

1260-
if (mn_activeman) {
1261-
const auto sb_opt = CreateSuperblockCandidate(pindex->nHeight);
1262-
const auto trigger_opt = CreateGovernanceTrigger(sb_opt, peerman, *mn_activeman);
1263-
VoteGovernanceTriggers(trigger_opt, connman, peerman, *mn_activeman);
1264-
}
1265-
12661271
nCachedBlockHeight = pindex->nHeight;
12671272
LogPrint(BCLog::GOBJECT, "CGovernanceManager::UpdatedBlockTip -- nCachedBlockHeight: %d\n", nCachedBlockHeight);
12681273

@@ -1655,11 +1660,9 @@ void CGovernanceManager::ExecuteBestSuperblock(const CDeterministicMNList& tip_m
16551660
// All checks are done in CSuperblock::IsValid via IsBlockValueValid and IsBlockPayeeValid,
16561661
// tip wouldn't be updated if anything was wrong. Mark this trigger as executed.
16571662
pSuperblock->SetExecuted();
1658-
ResetVotedFundingTrigger();
16591663
}
16601664
}
16611665

1662-
16631666
bool AreSuperblocksEnabled(const CSporkManager& sporkman)
16641667
{
16651668
return sporkman.IsSporkActive(SPORK_9_SUPERBLOCKS_ENABLED);

src/governance/governance.h

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,48 @@
55
#ifndef BITCOIN_GOVERNANCE_GOVERNANCE_H
66
#define BITCOIN_GOVERNANCE_GOVERNANCE_H
77

8-
#include <governance/classes.h>
9-
#include <governance/object.h>
10-
118
#include <cachemap.h>
129
#include <cachemultimap.h>
1310
#include <protocol.h>
14-
#include <util/check.h>
11+
#include <sync.h>
1512

16-
#include <optional>
13+
#include <limits>
14+
#include <map>
15+
#include <memory>
16+
#include <set>
17+
#include <string>
1718
#include <string_view>
19+
#include <vector>
1820

1921
class CBloomFilter;
2022
class CBlockIndex;
23+
class CChain;
2124
class CConnman;
25+
class ChainstateManager;
2226
template<typename T>
2327
class CFlatDB;
2428
class CInv;
29+
class CNode;
2530
class PeerManager;
2631

2732
class CDeterministicMNList;
2833
class CDeterministicMNManager;
34+
class CGovernanceException;
2935
class CGovernanceManager;
3036
class CGovernanceObject;
3137
class CGovernanceVote;
3238
class CMasternodeMetaMan;
3339
class CMasternodeSync;
3440
class CNetFulfilledRequestManager;
3541
class CSporkManager;
42+
class CSuperblock;
43+
class GovernanceSigner;
44+
45+
class UniValue;
46+
47+
using CDeterministicMNListPtr = std::shared_ptr<CDeterministicMNList>;
48+
using CSuperblock_sptr = std::shared_ptr<CSuperblock>;
49+
using vote_time_pair_t = std::pair<CGovernanceVote, int64_t>;
3650

3751
static constexpr int RATE_BUFFER_SIZE = 5;
3852
static constexpr bool DEFAULT_GOVERNANCE_ENABLE{true};
@@ -275,10 +289,6 @@ class CGovernanceManager : public GovernanceStore
275289

276290
[[nodiscard]] MessageProcessingResult ProcessMessage(CNode& peer, CConnman& connman, PeerManager& peerman, std::string_view msg_type, CDataStream& vRecv);
277291

278-
private:
279-
void ResetVotedFundingTrigger();
280-
281-
public:
282292
void DoMaintenance(CConnman& connman);
283293

284294
const CGovernanceObject* FindConstGovernanceObject(const uint256& nHash) const EXCLUSIVE_LOCKS_REQUIRED(cs);
@@ -296,7 +306,7 @@ class CGovernanceManager : public GovernanceStore
296306

297307
UniValue ToJson() const;
298308

299-
void UpdatedBlockTip(const CBlockIndex* pindex, CConnman& connman, PeerManager& peerman, const CActiveMasternodeManager* const mn_activeman);
309+
void UpdatedBlockTip(const CBlockIndex* pindex, PeerManager& peerman);
300310
int64_t GetLastDiffTime() const { return nTimeLastDiff; }
301311
void UpdateLastDiffTime(int64_t nTimeIn) { nTimeLastDiff = nTimeIn; }
302312

@@ -313,11 +323,7 @@ class CGovernanceManager : public GovernanceStore
313323

314324
bool SerializeVoteForHash(const uint256& nHash, CDataStream& ss) const;
315325

316-
void AddPostponedObject(const CGovernanceObject& govobj)
317-
{
318-
LOCK(cs);
319-
mapPostponedObjects.insert(std::make_pair(govobj.GetHash(), govobj));
320-
}
326+
void AddPostponedObject(const CGovernanceObject& govobj);
321327

322328
void MasternodeRateUpdate(const CGovernanceObject& govobj);
323329

@@ -375,21 +381,9 @@ class CGovernanceManager : public GovernanceStore
375381
bool GetBestSuperblock(const CDeterministicMNList& tip_mn_list, CSuperblock_sptr& pSuperblockRet, int nBlockHeight)
376382
EXCLUSIVE_LOCKS_REQUIRED(cs);
377383

378-
std::optional<const CSuperblock> CreateSuperblockCandidate(int nHeight) const;
379-
std::optional<const CGovernanceObject> CreateGovernanceTrigger(const std::optional<const CSuperblock>& sb_opt, PeerManager& peerman,
380-
const CActiveMasternodeManager& mn_activeman);
381-
void VoteGovernanceTriggers(const std::optional<const CGovernanceObject>& trigger_opt, CConnman& connman, PeerManager& peerman,
382-
const CActiveMasternodeManager& mn_activeman);
383-
bool VoteFundingTrigger(const uint256& nHash, const vote_outcome_enum_t outcome, CConnman& connman, PeerManager& peerman,
384-
const CActiveMasternodeManager& mn_activeman);
385-
bool HasAlreadyVotedFundingTrigger() const;
386-
387384
void RequestGovernanceObject(CNode* pfrom, const uint256& nHash, CConnman& connman, bool fUseFilter = false) const;
388385

389-
void AddInvalidVote(const CGovernanceVote& vote)
390-
{
391-
cmapInvalidVotes.Insert(vote.GetHash(), vote);
392-
}
386+
void AddInvalidVote(const CGovernanceVote& vote);
393387

394388
bool ProcessVote(CNode* pfrom, const CGovernanceVote& vote, CGovernanceException& exception, CConnman& connman);
395389

@@ -408,6 +402,7 @@ class CGovernanceManager : public GovernanceStore
408402

409403
void RemoveInvalidVotes();
410404

405+
friend class GovernanceSigner;
411406
};
412407

413408
bool AreSuperblocksEnabled(const CSporkManager& sporkman);

0 commit comments

Comments
 (0)