Skip to content

Commit

Permalink
Refactor masternode management (#1611)
Browse files Browse the repository at this point in the history
* Store masternodes in a map instead of a vector, drop unused functions in CMasternodeMan

* CTxIn vin -> COutPoint outpoint

* do not use CMasternodeMan::Find outside of the class

* update GetMasternodeInfo

* safe version of GetNextMasternodeInQueueForPayment

* fix ProcessMasternodeConnections

* bump CMasternodeMan::SERIALIZATION_VERSION_STRING
  • Loading branch information
UdjinM6 committed Sep 11, 2017
1 parent cd262bf commit 05da455
Show file tree
Hide file tree
Showing 31 changed files with 503 additions and 573 deletions.
28 changes: 14 additions & 14 deletions src/activemasternode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,14 @@ bool CActiveMasternode::SendMasternodePing()
return false;
}

if(!mnodeman.Has(vin)) {
if(!mnodeman.Has(outpoint)) {
strNotCapableReason = "Masternode not in masternode list";
nState = ACTIVE_MASTERNODE_NOT_CAPABLE;
LogPrintf("CActiveMasternode::SendMasternodePing -- %s: %s\n", GetStateString(), strNotCapableReason);
return false;
}

CMasternodePing mnp(vin);
CMasternodePing mnp(outpoint);
mnp.nSentinelVersion = nSentinelVersion;
mnp.fSentinelIsCurrent =
(abs(GetAdjustedTime() - nSentinelPingTime) < MASTERNODE_WATCHDOG_MAX_SECONDS);
Expand All @@ -117,14 +117,14 @@ bool CActiveMasternode::SendMasternodePing()
}

// Update lastPing for our masternode in Masternode list
if(mnodeman.IsMasternodePingedWithin(vin, MASTERNODE_MIN_MNP_SECONDS, mnp.sigTime)) {
if(mnodeman.IsMasternodePingedWithin(outpoint, MASTERNODE_MIN_MNP_SECONDS, mnp.sigTime)) {
LogPrintf("CActiveMasternode::SendMasternodePing -- Too early to send Masternode Ping\n");
return false;
}

mnodeman.SetMasternodeLastPing(vin, mnp);
mnodeman.SetMasternodeLastPing(outpoint, mnp);

LogPrintf("CActiveMasternode::SendMasternodePing -- Relaying ping, collateral=%s\n", vin.ToString());
LogPrintf("CActiveMasternode::SendMasternodePing -- Relaying ping, collateral=%s\n", outpoint.ToStringShort());
mnp.Relay();

return true;
Expand Down Expand Up @@ -227,7 +227,7 @@ void CActiveMasternode::ManageStateInitial()
CKey keyCollateral;

// If collateral is found switch to LOCAL mode
if(pwalletMain->GetMasternodeVinAndKeys(vin, pubKeyCollateral, keyCollateral)) {
if(pwalletMain->GetMasternodeOutpointAndKeys(outpoint, pubKeyCollateral, keyCollateral)) {
eType = MASTERNODE_LOCAL;
}

Expand All @@ -240,8 +240,8 @@ void CActiveMasternode::ManageStateRemote()
GetStatus(), GetTypeString(), fPingerEnabled, pubKeyMasternode.GetID().ToString());

mnodeman.CheckMasternode(pubKeyMasternode, true);
masternode_info_t infoMn = mnodeman.GetMasternodeInfo(pubKeyMasternode);
if(infoMn.fInfoValid) {
masternode_info_t infoMn;
if(mnodeman.GetMasternodeInfo(pubKeyMasternode, infoMn)) {
if(infoMn.nProtocolVersion != PROTOCOL_VERSION) {
nState = ACTIVE_MASTERNODE_NOT_CAPABLE;
strNotCapableReason = "Invalid protocol version";
Expand All @@ -262,7 +262,7 @@ void CActiveMasternode::ManageStateRemote()
}
if(nState != ACTIVE_MASTERNODE_STARTED) {
LogPrintf("CActiveMasternode::ManageStateRemote -- STARTED!\n");
vin = infoMn.vin;
outpoint = infoMn.vin.prevout;
service = infoMn.addr;
fPingerEnabled = true;
nState = ACTIVE_MASTERNODE_STARTED;
Expand All @@ -286,8 +286,8 @@ void CActiveMasternode::ManageStateLocal()
CPubKey pubKeyCollateral;
CKey keyCollateral;

if(pwalletMain->GetMasternodeVinAndKeys(vin, pubKeyCollateral, keyCollateral)) {
int nPrevoutAge = GetUTXOConfirmations(vin.prevout);
if(pwalletMain->GetMasternodeOutpointAndKeys(outpoint, pubKeyCollateral, keyCollateral)) {
int nPrevoutAge = GetUTXOConfirmations(outpoint);
if(nPrevoutAge < Params().GetConsensus().nMasternodeMinimumConfirmations){
nState = ACTIVE_MASTERNODE_INPUT_TOO_NEW;
strNotCapableReason = strprintf(_("%s - %d confirmations"), GetStatus(), nPrevoutAge);
Expand All @@ -297,12 +297,12 @@ void CActiveMasternode::ManageStateLocal()

{
LOCK(pwalletMain->cs_wallet);
pwalletMain->LockCoin(vin.prevout);
pwalletMain->LockCoin(outpoint);
}

CMasternodeBroadcast mnb;
std::string strError;
if(!CMasternodeBroadcast::Create(vin, service, keyCollateral, pubKeyCollateral, keyMasternode, pubKeyMasternode, strError, mnb)) {
if(!CMasternodeBroadcast::Create(outpoint, service, keyCollateral, pubKeyCollateral, keyMasternode, pubKeyMasternode, strError, mnb)) {
nState = ACTIVE_MASTERNODE_NOT_CAPABLE;
strNotCapableReason = "Error creating mastenode broadcast: " + strError;
LogPrintf("CActiveMasternode::ManageStateLocal -- %s: %s\n", GetStateString(), strNotCapableReason);
Expand All @@ -318,7 +318,7 @@ void CActiveMasternode::ManageStateLocal()
mnodeman.NotifyMasternodeUpdates();

//send to all peers
LogPrintf("CActiveMasternode::ManageStateLocal -- Relay broadcast, vin=%s\n", vin.ToString());
LogPrintf("CActiveMasternode::ManageStateLocal -- Relay broadcast, collateral=%s\n", outpoint.ToStringShort());
mnb.Relay();
}
}
4 changes: 2 additions & 2 deletions src/activemasternode.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class CActiveMasternode
CKey keyMasternode;

// Initialized while registering Masternode
CTxIn vin;
COutPoint outpoint;
CService service;

int nState; // should be one of ACTIVE_MASTERNODE_XXXX
Expand All @@ -62,7 +62,7 @@ class CActiveMasternode
fPingerEnabled(false),
pubKeyMasternode(),
keyMasternode(),
vin(),
outpoint(),
service(),
nState(ACTIVE_MASTERNODE_INITIAL)
{}
Expand Down
38 changes: 19 additions & 19 deletions src/governance-object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,13 @@ bool CGovernanceObject::ProcessVote(CNode* pfrom,
const CGovernanceVote& vote,
CGovernanceException& exception)
{
if(!mnodeman.Has(vote.GetVinMasternode())) {
if(!mnodeman.Has(vote.GetMasternodeOutpoint())) {
std::ostringstream ostr;
ostr << "CGovernanceObject::ProcessVote -- Masternode index not found";
exception = CGovernanceException(ostr.str(), GOVERNANCE_EXCEPTION_WARNING);
if(mapOrphanVotes.Insert(vote.GetVinMasternode(), vote_time_pair_t(vote, GetAdjustedTime() + GOVERNANCE_ORPHAN_EXPIRATION_TIME))) {
if(mapOrphanVotes.Insert(vote.GetMasternodeOutpoint(), vote_time_pair_t(vote, GetAdjustedTime() + GOVERNANCE_ORPHAN_EXPIRATION_TIME))) {
if(pfrom) {
mnodeman.AskForMN(pfrom, vote.GetVinMasternode());
mnodeman.AskForMN(pfrom, vote.GetMasternodeOutpoint());
}
LogPrintf("%s\n", ostr.str());
}
Expand All @@ -115,9 +115,9 @@ bool CGovernanceObject::ProcessVote(CNode* pfrom,
return false;
}

vote_m_it it = mapCurrentMNVotes.find(vote.GetVinMasternode());
vote_m_it it = mapCurrentMNVotes.find(vote.GetMasternodeOutpoint());
if(it == mapCurrentMNVotes.end()) {
it = mapCurrentMNVotes.insert(vote_m_t::value_type(vote.GetVinMasternode(), vote_rec_t())).first;
it = mapCurrentMNVotes.insert(vote_m_t::value_type(vote.GetMasternodeOutpoint(), vote_rec_t())).first;
}
vote_rec_t& recVote = it->second;
vote_signal_enum_t eSignal = vote.GetSignal();
Expand Down Expand Up @@ -157,7 +157,7 @@ bool CGovernanceObject::ProcessVote(CNode* pfrom,
if(nTimeDelta < GOVERNANCE_UPDATE_MIN) {
std::ostringstream ostr;
ostr << "CGovernanceObject::ProcessVote -- Masternode voting too often"
<< ", MN outpoint = " << vote.GetVinMasternode().prevout.ToStringShort()
<< ", MN outpoint = " << vote.GetMasternodeOutpoint().ToStringShort()
<< ", governance object hash = " << GetHash().ToString()
<< ", time delta = " << nTimeDelta;
LogPrint("gobject", "%s\n", ostr.str());
Expand All @@ -170,18 +170,18 @@ bool CGovernanceObject::ProcessVote(CNode* pfrom,
if(!vote.IsValid(true)) {
std::ostringstream ostr;
ostr << "CGovernanceObject::ProcessVote -- Invalid vote"
<< ", MN outpoint = " << vote.GetVinMasternode().prevout.ToStringShort()
<< ", MN outpoint = " << vote.GetMasternodeOutpoint().ToStringShort()
<< ", governance object hash = " << GetHash().ToString()
<< ", vote hash = " << vote.GetHash().ToString();
LogPrintf("%s\n", ostr.str());
exception = CGovernanceException(ostr.str(), GOVERNANCE_EXCEPTION_PERMANENT_ERROR, 20);
governance.AddInvalidVote(vote);
return false;
}
if(!mnodeman.AddGovernanceVote(vote.GetVinMasternode(), vote.GetParentHash())) {
if(!mnodeman.AddGovernanceVote(vote.GetMasternodeOutpoint(), vote.GetParentHash())) {
std::ostringstream ostr;
ostr << "CGovernanceObject::ProcessVote -- Unable to add governance vote"
<< ", MN outpoint = " << vote.GetVinMasternode().prevout.ToStringShort()
<< ", MN outpoint = " << vote.GetMasternodeOutpoint().ToStringShort()
<< ", governance object hash = " << GetHash().ToString();
LogPrint("gobject", "%s\n", ostr.str());
exception = CGovernanceException(ostr.str(), GOVERNANCE_EXCEPTION_PERMANENT_ERROR);
Expand Down Expand Up @@ -222,9 +222,9 @@ std::string CGovernanceObject::GetSignatureMessage() const
return strMessage;
}

void CGovernanceObject::SetMasternodeInfo(const CTxIn& vin)
void CGovernanceObject::SetMasternodeVin(const COutPoint& outpoint)
{
vinMasternode = vin;
vinMasternode = CTxIn(outpoint);
}

bool CGovernanceObject::Sign(CKey& keyMasternode, CPubKey& pubKeyMasternode)
Expand Down Expand Up @@ -442,17 +442,17 @@ bool CGovernanceObject::IsValidLocally(std::string& strError, bool& fMissingMast
if(fCheckCollateral) {
if((nObjectType == GOVERNANCE_OBJECT_TRIGGER) || (nObjectType == GOVERNANCE_OBJECT_WATCHDOG)) {
std::string strOutpoint = vinMasternode.prevout.ToStringShort();
masternode_info_t infoMn = mnodeman.GetMasternodeInfo(vinMasternode);
if(!infoMn.fInfoValid) {
masternode_info_t infoMn;
if(!mnodeman.GetMasternodeInfo(vinMasternode.prevout, infoMn)) {

CMasternode::CollateralStatus err = CMasternode::CheckCollateral(GetMasternodeVin());
CMasternode::CollateralStatus err = CMasternode::CheckCollateral(vinMasternode.prevout);
if (err == CMasternode::COLLATERAL_OK) {
fMissingMasternode = true;
strError = "Masternode not found: " + strOutpoint;
} else if (err == CMasternode::COLLATERAL_UTXO_NOT_FOUND) {
strError = "Failed to find Masternode UTXO, missing masternode=" + GetMasternodeVin().prevout.ToStringShort() + "\n";
strError = "Failed to find Masternode UTXO, missing masternode=" + strOutpoint + "\n";
} else if (err == CMasternode::COLLATERAL_INVALID_AMOUNT) {
strError = "Masternode UTXO should have 1000 DASH, missing masternode=" + GetMasternodeVin().prevout.ToStringShort() + "\n";
strError = "Masternode UTXO should have 1000 DASH, missing masternode=" + strOutpoint + "\n";
}

return false;
Expand Down Expand Up @@ -638,7 +638,7 @@ int CGovernanceObject::GetAbstainCount(vote_signal_enum_t eVoteSignalIn) const
return CountMatchingVotes(eVoteSignalIn, VOTE_OUTCOME_ABSTAIN);
}

bool CGovernanceObject::GetCurrentMNVotes(const CTxIn& mnCollateralOutpoint, vote_rec_t& voteRecord)
bool CGovernanceObject::GetCurrentMNVotes(const COutPoint& mnCollateralOutpoint, vote_rec_t& voteRecord)
{
vote_m_it it = mapCurrentMNVotes.find(mnCollateralOutpoint);
if (it == mapCurrentMNVotes.end()) {
Expand Down Expand Up @@ -722,13 +722,13 @@ void CGovernanceObject::CheckOrphanVotes()
vote_mcache_t::list_cit it = listVotes.begin();
while(it != listVotes.end()) {
bool fRemove = false;
const CTxIn& key = it->key;
const COutPoint& key = it->key;
const vote_time_pair_t& pairVote = it->value;
const CGovernanceVote& vote = pairVote.first;
if(pairVote.second < nNow) {
fRemove = true;
}
else if(!mnodeman.Has(vote.GetVinMasternode())) {
else if(!mnodeman.Has(vote.GetMasternodeOutpoint())) {
++it;
continue;
}
Expand Down
8 changes: 4 additions & 4 deletions src/governance-object.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ class CGovernanceObject
friend class CGovernanceTriggerManager;

public: // Types
typedef std::map<CTxIn, vote_rec_t> vote_m_t;
typedef std::map<COutPoint, vote_rec_t> vote_m_t;

typedef vote_m_t::iterator vote_m_it;

typedef vote_m_t::const_iterator vote_m_cit;

typedef CacheMultiMap<CTxIn, vote_time_pair_t> vote_mcache_t;
typedef CacheMultiMap<COutPoint, vote_time_pair_t> vote_mcache_t;

private:
/// critical section to protect the inner data structures
Expand Down Expand Up @@ -254,7 +254,7 @@ class CGovernanceObject

// Signature related functions

void SetMasternodeInfo(const CTxIn& vin);
void SetMasternodeVin(const COutPoint& outpoint);
bool Sign(CKey& keyMasternode, CPubKey& pubKeyMasternode);
bool CheckSignature(CPubKey& pubKeyMasternode);

Expand Down Expand Up @@ -293,7 +293,7 @@ class CGovernanceObject
int GetNoCount(vote_signal_enum_t eVoteSignalIn) const;
int GetAbstainCount(vote_signal_enum_t eVoteSignalIn) const;

bool GetCurrentMNVotes(const CTxIn& mnCollateralOutpoint, vote_rec_t& voteRecord);
bool GetCurrentMNVotes(const COutPoint& mnCollateralOutpoint, vote_rec_t& voteRecord);

// FUNCTIONS FOR DEALING WITH DATA STRING

Expand Down
8 changes: 4 additions & 4 deletions src/governance-vote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,11 @@ CGovernanceVote::CGovernanceVote()
vchSig()
{}

CGovernanceVote::CGovernanceVote(CTxIn vinMasternodeIn, uint256 nParentHashIn, vote_signal_enum_t eVoteSignalIn, vote_outcome_enum_t eVoteOutcomeIn)
CGovernanceVote::CGovernanceVote(COutPoint outpointMasternodeIn, uint256 nParentHashIn, vote_signal_enum_t eVoteSignalIn, vote_outcome_enum_t eVoteOutcomeIn)
: fValid(true),
fSynced(false),
nVoteSignal(eVoteSignalIn),
vinMasternode(vinMasternodeIn),
vinMasternode(outpointMasternodeIn),
nParentHash(nParentHashIn),
nVoteOutcome(eVoteOutcomeIn),
nTime(GetAdjustedTime()),
Expand Down Expand Up @@ -273,8 +273,8 @@ bool CGovernanceVote::IsValid(bool fSignatureCheck) const
return false;
}

masternode_info_t infoMn = mnodeman.GetMasternodeInfo(vinMasternode);
if(!infoMn.fInfoValid) {
masternode_info_t infoMn;
if(!mnodeman.GetMasternodeInfo(vinMasternode.prevout, infoMn)) {
LogPrint("gobject", "CGovernanceVote::IsValid -- Unknown Masternode - %s\n", vinMasternode.prevout.ToStringShort());
return false;
}
Expand Down
6 changes: 2 additions & 4 deletions src/governance-vote.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class CGovernanceVote

public:
CGovernanceVote();
CGovernanceVote(CTxIn vinMasternodeIn, uint256 nParentHashIn, vote_signal_enum_t eVoteSignalIn, vote_outcome_enum_t eVoteOutcomeIn);
CGovernanceVote(COutPoint outpointMasternodeIn, uint256 nParentHashIn, vote_signal_enum_t eVoteSignalIn, vote_outcome_enum_t eVoteOutcomeIn);

bool IsValid() const { return fValid; }

Expand All @@ -128,9 +128,7 @@ class CGovernanceVote
return CGovernanceVoting::ConvertOutcomeToString(GetOutcome());
}

CTxIn& GetVinMasternode() { return vinMasternode; }

const CTxIn& GetVinMasternode() const { return vinMasternode; }
const COutPoint& GetMasternodeOutpoint() const { return vinMasternode.prevout; }

/**
* GetHash()
Expand Down
4 changes: 2 additions & 2 deletions src/governance-votedb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ std::vector<CGovernanceVote> CGovernanceObjectVoteFile::GetVotes() const
return vecResult;
}

void CGovernanceObjectVoteFile::RemoveVotesFromMasternode(const CTxIn& vinMasternode)
void CGovernanceObjectVoteFile::RemoveVotesFromMasternode(const COutPoint& outpointMasternode)
{
vote_l_it it = listVotes.begin();
while(it != listVotes.end()) {
if(it->GetVinMasternode() == vinMasternode) {
if(it->GetMasternodeOutpoint() == outpointMasternode) {
--nMemoryVotes;
mapVoteIndex.erase(it->GetHash());
listVotes.erase(it++);
Expand Down
2 changes: 1 addition & 1 deletion src/governance-votedb.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class CGovernanceObjectVoteFile

CGovernanceObjectVoteFile& operator=(const CGovernanceObjectVoteFile& other);

void RemoveVotesFromMasternode(const CTxIn& vinMasternode);
void RemoveVotesFromMasternode(const COutPoint& outpointMasternode);

ADD_SERIALIZE_METHODS;

Expand Down
Loading

0 comments on commit 05da455

Please sign in to comment.