Commit da34bb78 authored by Ashot's avatar Ashot
Browse files

Merge branch 'make-functions-const' into 'Current-dev'

Refactored masternode/systemnode related functions to be const

See merge request crown/crown-core!119
parents cab97cbc 12e67e39
Showing with 104 additions and 99 deletions
+104 -99
......@@ -142,7 +142,8 @@ void CActiveMasternode::ManageStatus()
}
}
std::string CActiveMasternode::GetStatus() {
std::string CActiveMasternode::GetStatus() const
{
switch (status) {
case ACTIVE_MASTERNODE_INITIAL: return "Node just started, not yet activated";
case ACTIVE_MASTERNODE_SYNC_IN_PROCESS: return "Sync in progress. Must wait until sync is complete to start Masternode";
......@@ -210,7 +211,7 @@ bool CActiveMasternode::SendMasternodePing(std::string& errorMessage) {
}
// when starting a Masternode, this can enable to run as a hot wallet with no funds
bool CActiveMasternode::EnableHotColdMasterNode(CTxIn& newVin, CService& newService)
bool CActiveMasternode::EnableHotColdMasterNode(const CTxIn& newVin, const CService& newService)
{
if(!fMasterNode) return false;
......
......@@ -48,10 +48,10 @@ public:
/// Manage status of main Masternode
void ManageStatus();
std::string GetStatus();
std::string GetStatus() const;
/// Enable cold wallet mode (run a Masternode with no funds)
bool EnableHotColdMasterNode(CTxIn& vin, CService& addr);
bool EnableHotColdMasterNode(const CTxIn& vin, const CService& addr);
};
#endif
......@@ -212,7 +212,7 @@ bool CActiveSystemnode::SendSystemnodePing(std::string& errorMessage) {
}
// when starting a Systemnode, this can enable to run as a hot wallet with no funds
bool CActiveSystemnode::EnableHotColdSystemNode(CTxIn& newVin, CService& newService)
bool CActiveSystemnode::EnableHotColdSystemNode(const CTxIn& newVin, const CService& newService)
{
if(!fSystemNode) return false;
......
......@@ -56,7 +56,7 @@ public:
std::string GetStatus();
/// Enable cold wallet mode (run a Systemnode with no funds)
bool EnableHotColdSystemNode(CTxIn& vin, CService& addr);
bool EnableHotColdSystemNode(const CTxIn& vin, const CService& addr);
};
#endif
......@@ -123,7 +123,7 @@ CMasternode::CMasternode(const CMasternodeBroadcast& mnb)
//
// When a new masternode broadcast is sent, update our information
//
bool CMasternode::UpdateFromNewBroadcast(CMasternodeBroadcast& mnb)
bool CMasternode::UpdateFromNewBroadcast(const CMasternodeBroadcast& mnb)
{
if(mnb.sigTime > sigTime) {
pubkey2 = mnb.pubkey2;
......@@ -147,7 +147,7 @@ bool CMasternode::UpdateFromNewBroadcast(CMasternodeBroadcast& mnb)
// the proof of work for that block. The further away they are the better, the furthest will win the election
// and get paid this block
//
arith_uint256 CMasternode::CalculateScore(int64_t nBlockHeight)
arith_uint256 CMasternode::CalculateScore(int64_t nBlockHeight) const
{
if(chainActive.Tip() == NULL)
return arith_uint256();
......@@ -214,14 +214,15 @@ void CMasternode::Check(bool forceCheck)
activeState = MASTERNODE_ENABLED; // OK
}
bool CMasternode::IsValidNetAddr()
bool CMasternode::IsValidNetAddr() const
{
// TODO: regtest is fine with any addresses for now,
// should probably be a bit smarter if one day we start to implement tests for this
return (addr.IsIPv4() && addr.IsRoutable());
}
int64_t CMasternode::SecondsSincePayment() {
int64_t CMasternode::SecondsSincePayment() const
{
CScript pubkeyScript;
pubkeyScript = GetScriptForDestination(pubkey.GetID());
......@@ -238,7 +239,8 @@ int64_t CMasternode::SecondsSincePayment() {
return month + UintToArith256(hash).GetCompact(false);
}
int64_t CMasternode::GetLastPaid() {
int64_t CMasternode::GetLastPaid() const
{
CBlockIndex* pindexPrev = chainActive.Tip();
if(pindexPrev == NULL) return false;
......@@ -417,7 +419,7 @@ bool CMasternodeBroadcast::Create(CTxIn txin, CService service, CKey keyCollater
return true;
}
bool CMasternodeBroadcast::CheckAndUpdate(int& nDos)
bool CMasternodeBroadcast::CheckAndUpdate(int& nDos) const
{
nDos = 0;
......@@ -549,7 +551,7 @@ bool CMasternodeBroadcast::CheckAndUpdate(int& nDos)
return true;
}
bool CMasternodeBroadcast::CheckInputsAndAdd(int& nDoS)
bool CMasternodeBroadcast::CheckInputsAndAdd(int& nDoS) const
{
// we are a masternode with the same vin (i.e. already activated) and this mnb is ours (matches our Masternode privkey)
// so nothing to do here for us
......@@ -637,13 +639,13 @@ bool CMasternodeBroadcast::CheckInputsAndAdd(int& nDoS)
return true;
}
void CMasternodeBroadcast::Relay()
void CMasternodeBroadcast::Relay() const
{
CInv inv(MSG_MASTERNODE_ANNOUNCE, GetHash());
RelayInv(inv);
}
bool CMasternodeBroadcast::Sign(CKey& keyCollateralAddress)
bool CMasternodeBroadcast::Sign(const CKey& keyCollateralAddress)
{
std::string errorMessage;
......@@ -662,7 +664,7 @@ bool CMasternodeBroadcast::Sign(CKey& keyCollateralAddress)
return true;
}
bool CMasternodeBroadcast::VerifySignature()
bool CMasternodeBroadcast::VerifySignature() const
{
std::string errorMessage;
......@@ -687,7 +689,7 @@ CMasternodePing::CMasternodePing()
vchSig = std::vector<unsigned char>();
}
CMasternodePing::CMasternodePing(CTxIn& newVin)
CMasternodePing::CMasternodePing(const CTxIn& newVin)
{
vin = newVin;
blockHash = chainActive[chainActive.Height() - 12]->GetBlockHash();
......@@ -696,7 +698,7 @@ CMasternodePing::CMasternodePing(CTxIn& newVin)
}
bool CMasternodePing::Sign(CKey& keyMasternode, CPubKey& pubKeyMasternode)
bool CMasternodePing::Sign(const CKey& keyMasternode, const CPubKey& pubKeyMasternode)
{
std::string errorMessage;
std::string strMasterNodeSignMessage;
......@@ -717,7 +719,8 @@ bool CMasternodePing::Sign(CKey& keyMasternode, CPubKey& pubKeyMasternode)
return true;
}
bool CMasternodePing::VerifySignature(CPubKey& pubKeyMasternode, int &nDos) {
bool CMasternodePing::VerifySignature(const CPubKey& pubKeyMasternode, int &nDos) const
{
std::string strMessage = vin.ToString() + blockHash.ToString() + boost::lexical_cast<std::string>(sigTime);
std::string errorMessage = "";
......@@ -730,7 +733,7 @@ bool CMasternodePing::VerifySignature(CPubKey& pubKeyMasternode, int &nDos) {
return true;
}
bool CMasternodePing::CheckAndUpdate(int& nDos, bool fRequireEnabled, bool fCheckSigTimeOnly)
bool CMasternodePing::CheckAndUpdate(int& nDos, bool fRequireEnabled, bool fCheckSigTimeOnly) const
{
if (sigTime > GetAdjustedTime() + 60 * 60) {
LogPrintf("CMasternodePing::CheckAndUpdate - Signature rejected, too far into the future %s\n", vin.ToString());
......@@ -811,7 +814,7 @@ bool CMasternodePing::CheckAndUpdate(int& nDos, bool fRequireEnabled, bool fChec
return false;
}
void CMasternodePing::Relay()
void CMasternodePing::Relay() const
{
CInv inv(MSG_MASTERNODE_PING, GetHash());
RelayInv(inv);
......
......@@ -31,7 +31,6 @@ extern map<int64_t, uint256> mapCacheBlockHashes;
bool GetBlockHash(uint256& hash, int nBlockHeight);
//
// The Masternode Ping Class : Contains a different serialize method for sending pings from masternodes throughout the network
//
......@@ -47,24 +46,26 @@ public:
//removed stop
CMasternodePing();
CMasternodePing(CTxIn& newVin);
CMasternodePing(const CTxIn& newVin);
ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
{
READWRITE(vin);
READWRITE(blockHash);
READWRITE(sigTime);
READWRITE(vchSig);
}
bool CheckAndUpdate(int& nDos, bool fRequireEnabled = true, bool fCheckSigTimeOnly = false);
bool Sign(CKey& keyMasternode, CPubKey& pubKeyMasternode);
bool VerifySignature(CPubKey& pubKeyMasternode, int &nDos);
void Relay();
bool CheckAndUpdate(int& nDos, bool fRequireEnabled = true, bool fCheckSigTimeOnly = false) const;
bool Sign(const CKey& keyMasternode, const CPubKey& pubKeyMasternode);
bool VerifySignature(const CPubKey& pubKeyMasternode, int &nDos) const;
void Relay() const;
uint256 GetHash(){
uint256 GetHash() const
{
CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION);
ss << vin;
ss << sigTime;
......@@ -97,10 +98,8 @@ public:
{
return !(a == b);
}
};
//
// The Masternode Class. It contains the input of the 10000 CRW, signature to prove
// it's the one who own that ip address and code for calculating the payment election.
......@@ -112,7 +111,8 @@ private:
mutable CCriticalSection cs;
int64_t lastTimeChecked;
public:
enum state {
enum state
{
MASTERNODE_ENABLED = 1,
MASTERNODE_EXPIRED = 2,
MASTERNODE_VIN_SPENT = 3,
......@@ -181,12 +181,13 @@ public:
return !(a.vin == b.vin);
}
arith_uint256 CalculateScore(int64_t nBlockHeight=0);
arith_uint256 CalculateScore(int64_t nBlockHeight=0) const;
ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
{
LOCK(cs);
READWRITE(vin);
......@@ -207,25 +208,16 @@ public:
READWRITE(nLastScanningErrorBlockHeight);
}
int64_t SecondsSincePayment();
bool UpdateFromNewBroadcast(CMasternodeBroadcast& mnb);
inline uint64_t SliceHash(uint256& hash, int slice)
{
uint64_t n = 0;
memcpy(&n, &hash+slice*64, 64);
return n;
}
int64_t SecondsSincePayment() const;
bool UpdateFromNewBroadcast(const CMasternodeBroadcast& mnb);
void Check(bool forceCheck = false);
bool IsBroadcastedWithin(int seconds)
bool IsBroadcastedWithin(int seconds) const
{
return (GetAdjustedTime() - sigTime) < seconds;
}
bool IsPingedWithin(int seconds, int64_t now = -1)
bool IsPingedWithin(int seconds, int64_t now = -1) const
{
now == -1 ? now = GetAdjustedTime() : now;
......@@ -245,7 +237,7 @@ public:
return activeState == MASTERNODE_ENABLED;
}
bool IsValidNetAddr();
bool IsValidNetAddr() const;
int GetMasternodeInputAge()
{
......@@ -259,7 +251,8 @@ public:
return cacheInputAge+(chainActive.Tip()->nHeight-cacheInputAgeBlock);
}
std::string Status() {
std::string Status() const
{
std::string strStatus = "ACTIVE";
if(activeState == CMasternode::MASTERNODE_ENABLED) strStatus = "ENABLED";
......@@ -271,11 +264,9 @@ public:
return strStatus;
}
int64_t GetLastPaid();
int64_t GetLastPaid() const;
};
//
// The Masternode Broadcast Class : Contains a different serialize method for sending masternodes through the network
//
......@@ -292,16 +283,17 @@ public:
static bool Create(std::string strService, std::string strKey, std::string strTxHash, std::string strOutputIndex, std::string& strErrorMessage, CMasternodeBroadcast &mnb, bool fOffline = false);
bool CheckAndUpdate(int& nDoS);
bool CheckInputsAndAdd(int& nDos);
bool Sign(CKey& keyCollateralAddress);
bool VerifySignature();
void Relay();
bool CheckAndUpdate(int& nDoS) const;
bool CheckInputsAndAdd(int& nDos) const;
bool Sign(const CKey& keyCollateralAddress);
bool VerifySignature() const;
void Relay() const;
ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
{
READWRITE(vin);
READWRITE(addr);
READWRITE(pubkey);
......@@ -313,13 +305,13 @@ public:
READWRITE(nLastDsq);
}
uint256 GetHash(){
uint256 GetHash() const
{
CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION);
ss << sigTime;
ss << pubkey;
return ss.GetHash();
}
};
#endif
......@@ -24,7 +24,7 @@ CSystemnodePing::CSystemnodePing()
vchSig = std::vector<unsigned char>();
}
CSystemnodePing::CSystemnodePing(CTxIn& newVin)
CSystemnodePing::CSystemnodePing(const CTxIn& newVin)
{
vin = newVin;
blockHash = chainActive[chainActive.Height() - 12]->GetBlockHash();
......@@ -32,7 +32,7 @@ CSystemnodePing::CSystemnodePing(CTxIn& newVin)
vchSig = std::vector<unsigned char>();
}
bool CSystemnodePing::Sign(CKey& keySystemnode, CPubKey& pubKeySystemnode)
bool CSystemnodePing::Sign(const CKey& keySystemnode, const CPubKey& pubKeySystemnode)
{
std::string errorMessage;
std::string strThroNeSignMessage;
......@@ -53,13 +53,13 @@ bool CSystemnodePing::Sign(CKey& keySystemnode, CPubKey& pubKeySystemnode)
return true;
}
void CSystemnodePing::Relay()
void CSystemnodePing::Relay() const
{
CInv inv(MSG_SYSTEMNODE_PING, GetHash());
RelayInv(inv);
}
bool CSystemnodePing::CheckAndUpdate(int& nDos, bool fRequireEnabled, bool fCheckSigTimeOnly)
bool CSystemnodePing::CheckAndUpdate(int& nDos, bool fRequireEnabled, bool fCheckSigTimeOnly) const
{
if (sigTime > GetAdjustedTime() + 60 * 60) {
LogPrintf("CSystemnodePing::CheckAndUpdate - Signature rejected, too far into the future %s\n", vin.ToString());
......@@ -140,7 +140,8 @@ bool CSystemnodePing::CheckAndUpdate(int& nDos, bool fRequireEnabled, bool fChec
return false;
}
bool CSystemnodePing::VerifySignature(CPubKey& pubKeySystemnode, int &nDos) {
bool CSystemnodePing::VerifySignature(const CPubKey& pubKeySystemnode, int &nDos) const
{
std::string strMessage = vin.ToString() + blockHash.ToString() + boost::lexical_cast<std::string>(sigTime);
std::string errorMessage = "";
......@@ -217,7 +218,7 @@ bool CSystemnode::IsValidNetAddr()
// the proof of work for that block. The further away they are the better, the furthest will win the election
// and get paid this block
//
arith_uint256 CSystemnode::CalculateScore(int64_t nBlockHeight)
arith_uint256 CSystemnode::CalculateScore(int64_t nBlockHeight) const
{
if(chainActive.Tip() == NULL)
return arith_uint256();
......@@ -242,7 +243,7 @@ arith_uint256 CSystemnode::CalculateScore(int64_t nBlockHeight)
//
// When a new systemnode broadcast is sent, update our information
//
bool CSystemnode::UpdateFromNewBroadcast(CSystemnodeBroadcast& snb)
bool CSystemnode::UpdateFromNewBroadcast(const CSystemnodeBroadcast& snb)
{
if(snb.sigTime > sigTime) {
pubkey2 = snb.pubkey2;
......@@ -303,7 +304,8 @@ void CSystemnode::Check(bool forceCheck)
activeState = SYSTEMNODE_ENABLED; // OK
}
int64_t CSystemnode::SecondsSincePayment() {
int64_t CSystemnode::SecondsSincePayment() const
{
CScript pubkeyScript;
pubkeyScript = GetScriptForDestination(pubkey.GetID());
......@@ -320,7 +322,8 @@ int64_t CSystemnode::SecondsSincePayment() {
return month + UintToArith256(hash).GetCompact(false);
}
int64_t CSystemnode::GetLastPaid() {
int64_t CSystemnode::GetLastPaid() const
{
CBlockIndex* pindexPrev = chainActive.Tip();
if(pindexPrev == NULL) return false;
......@@ -368,7 +371,7 @@ int64_t CSystemnode::GetLastPaid() {
// CSystemnodeBroadcast
//
bool CSystemnodeBroadcast::CheckAndUpdate(int& nDos)
bool CSystemnodeBroadcast::CheckAndUpdate(int& nDos) const
{
nDos = 0;
......@@ -501,7 +504,7 @@ bool CSystemnodeBroadcast::CheckAndUpdate(int& nDos)
}
bool CSystemnodeBroadcast::CheckInputsAndAdd(int& nDoS)
bool CSystemnodeBroadcast::CheckInputsAndAdd(int& nDoS) const
{
// we are a systemnode with the same vin (i.e. already activated) and this snb is ours (matches our systemnode privkey)
// so nothing to do here for us
......@@ -590,7 +593,7 @@ bool CSystemnodeBroadcast::CheckInputsAndAdd(int& nDoS)
}
void CSystemnodeBroadcast::Relay()
void CSystemnodeBroadcast::Relay() const
{
CInv inv(MSG_SYSTEMNODE_ANNOUNCE, GetHash());
RelayInv(inv);
......@@ -713,7 +716,7 @@ bool CSystemnodeBroadcast::Create(CTxIn txin, CService service, CKey keyCollater
return true;
}
bool CSystemnodeBroadcast::Sign(CKey& keyCollateralAddress)
bool CSystemnodeBroadcast::Sign(const CKey& keyCollateralAddress)
{
std::string errorMessage;
......@@ -732,7 +735,7 @@ bool CSystemnodeBroadcast::Sign(CKey& keyCollateralAddress)
return true;
}
bool CSystemnodeBroadcast::VerifySignature()
bool CSystemnodeBroadcast::VerifySignature() const
{
std::string errorMessage;
......
......@@ -44,24 +44,26 @@ public:
std::vector<unsigned char> vchSig;
CSystemnodePing();
CSystemnodePing(CTxIn& newVin);
CSystemnodePing(const CTxIn& newVin);
ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
{
READWRITE(vin);
READWRITE(blockHash);
READWRITE(sigTime);
READWRITE(vchSig);
}
bool CheckAndUpdate(int& nDos, bool fRequireEnabled = true, bool fCheckSigTimeOnly = false);
bool Sign(CKey& keySystemnode, CPubKey& pubKeySystemnode);
bool VerifySignature(CPubKey& pubKeySystemnode, int &nDos);
void Relay();
bool CheckAndUpdate(int& nDos, bool fRequireEnabled = true, bool fCheckSigTimeOnly = false) const;
bool Sign(const CKey& keySystemnode, const CPubKey& pubKeySystemnode);
bool VerifySignature(const CPubKey& pubKeySystemnode, int &nDos) const;
void Relay() const;
uint256 GetHash(){
uint256 GetHash() const
{
CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION);
ss << vin;
ss << sigTime;
......@@ -108,7 +110,8 @@ private:
mutable CCriticalSection cs;
int64_t lastTimeChecked;
public:
enum state {
enum state
{
SYSTEMNODE_ENABLED = 1,
SYSTEMNODE_EXPIRED = 2,
SYSTEMNODE_VIN_SPENT = 3,
......@@ -166,12 +169,13 @@ public:
return !(a.vin == b.vin);
}
arith_uint256 CalculateScore(int64_t nBlockHeight=0);
arith_uint256 CalculateScore(int64_t nBlockHeight=0) const;
ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
{
LOCK(cs);
READWRITE(vin);
......@@ -186,15 +190,14 @@ public:
READWRITE(unitTest);
}
int64_t SecondsSincePayment();
bool UpdateFromNewBroadcast(CSystemnodeBroadcast& snb);
int64_t SecondsSincePayment() const;
bool UpdateFromNewBroadcast(const CSystemnodeBroadcast& snb);
void Check(bool forceCheck = false);
bool IsBroadcastedWithin(int seconds)
bool IsBroadcastedWithin(int seconds) const
{
return (GetAdjustedTime() - sigTime) < seconds;
}
bool IsEnabled()
bool IsEnabled() const
{
return activeState == SYSTEMNODE_ENABLED;
}
......@@ -210,7 +213,7 @@ public:
return cacheInputAge+(chainActive.Tip()->nHeight-cacheInputAgeBlock);
}
bool IsPingedWithin(int seconds, int64_t now = -1)
bool IsPingedWithin(int seconds, int64_t now = -1) const
{
now == -1 ? now = GetAdjustedTime() : now;
......@@ -218,7 +221,8 @@ public:
? false
: now - lastPing.sigTime < seconds;
}
std::string Status() {
std::string Status() const
{
std::string strStatus = "ACTIVE";
if(activeState == CSystemnode::SYSTEMNODE_ENABLED) strStatus = "ENABLED";
......@@ -229,7 +233,7 @@ public:
return strStatus;
}
int64_t GetLastPaid();
int64_t GetLastPaid() const;
};
//
......@@ -247,16 +251,17 @@ public:
static bool Create(CTxIn txin, CService service, CKey keyCollateral, CPubKey pubKeyCollateral, CKey keySystemnodeNew, CPubKey pubKeySystemnodeNew, std::string &strErrorMessage, CSystemnodeBroadcast &snb);
static bool Create(std::string strService, std::string strKey, std::string strTxHash, std::string strOutputIndex, std::string& strErrorMessage, CSystemnodeBroadcast &snb, bool fOffline = false);
bool CheckAndUpdate(int& nDoS);
bool CheckInputsAndAdd(int& nDos);
bool Sign(CKey& keyCollateralAddress);
bool VerifySignature();
void Relay();
bool CheckAndUpdate(int& nDoS) const;
bool CheckInputsAndAdd(int& nDos) const;
bool Sign(const CKey& keyCollateralAddress);
bool VerifySignature() const;
void Relay() const;
ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
{
READWRITE(vin);
READWRITE(addr);
READWRITE(pubkey);
......@@ -267,7 +272,8 @@ public:
READWRITE(lastPing);
}
uint256 GetHash(){
uint256 GetHash() const
{
CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION);
ss << sigTime;
ss << pubkey;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment