Skip to content

Commit 14d3c32

Browse files
committed
[Masternode] Use cached block hashes to create mn pings
1 parent f5349b9 commit 14d3c32

File tree

4 files changed

+30
-17
lines changed

4 files changed

+30
-17
lines changed

src/activemasternode.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ bool CActiveMasternode::SendMasternodePing(std::string& errorMessage)
138138

139139
LogPrintf("CActiveMasternode::SendMasternodePing() - Relay Masternode Ping vin = %s\n", vin->ToString());
140140

141-
CMasternodePing mnp(*vin);
141+
const uint256& nBlockHash = mnodeman.GetBlockHashToPing();
142+
CMasternodePing mnp(*vin, nBlockHash);
142143
if (!mnp.Sign(keyMasternode, pubKeyMasternode)) {
143144
errorMessage = "Couldn't sign Masternode Ping";
144145
return false;

src/masternode.cpp

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,13 @@ CMasternodeBroadcast::CMasternodeBroadcast(const CMasternode& mn) :
314314
CMasternode(mn)
315315
{ }
316316

317-
bool CMasternodeBroadcast::Create(std::string strService, std::string strKeyMasternode, std::string strTxHash, std::string strOutputIndex, std::string& strErrorRet, CMasternodeBroadcast& mnbRet, bool fOffline)
317+
bool CMasternodeBroadcast::Create(const std::string& strService,
318+
const std::string& strKeyMasternode,
319+
const std::string& strTxHash,
320+
const std::string& strOutputIndex,
321+
std::string& strErrorRet,
322+
CMasternodeBroadcast& mnbRet,
323+
bool fOffline)
318324
{
319325
CTxIn txin;
320326
CPubKey pubKeyCollateralAddressNew;
@@ -356,7 +362,14 @@ bool CMasternodeBroadcast::Create(std::string strService, std::string strKeyMast
356362
return Create(txin, _service, keyCollateralAddressNew, pubKeyCollateralAddressNew, keyMasternodeNew, pubKeyMasternodeNew, strErrorRet, mnbRet);
357363
}
358364

359-
bool CMasternodeBroadcast::Create(CTxIn txin, CService service, CKey keyCollateralAddressNew, CPubKey pubKeyCollateralAddressNew, CKey keyMasternodeNew, CPubKey pubKeyMasternodeNew, std::string& strErrorRet, CMasternodeBroadcast& mnbRet)
365+
bool CMasternodeBroadcast::Create(const CTxIn& txin,
366+
const CService& service,
367+
const CKey& keyCollateralAddressNew,
368+
const CPubKey& pubKeyCollateralAddressNew,
369+
const CKey& keyMasternodeNew,
370+
const CPubKey& pubKeyMasternodeNew,
371+
std::string& strErrorRet,
372+
CMasternodeBroadcast& mnbRet)
360373
{
361374
// wait for reindex and/or import to finish
362375
if (fImporting || fReindex) return false;
@@ -365,7 +378,9 @@ bool CMasternodeBroadcast::Create(CTxIn txin, CService service, CKey keyCollater
365378
EncodeDestination(pubKeyCollateralAddressNew.GetID()),
366379
pubKeyMasternodeNew.GetID().ToString());
367380

368-
CMasternodePing mnp(txin);
381+
// Get block hash to ping (TODO: move outside of this function)
382+
const uint256& nBlockHashToPing = mnodeman.GetBlockHashToPing();
383+
CMasternodePing mnp(txin, nBlockHashToPing);
369384
if (!mnp.Sign(keyMasternodeNew, pubKeyMasternodeNew)) {
370385
strErrorRet = strprintf("Failed to sign ping, masternode=%s", txin.prevout.hash.ToString());
371386
LogPrint(BCLog::MASTERNODE,"CMasternodeBroadcast::Create -- %s\n", strErrorRet);
@@ -640,19 +655,12 @@ CMasternodePing::CMasternodePing() :
640655
sigTime(GetAdjustedTime())
641656
{ }
642657

643-
CMasternodePing::CMasternodePing(CTxIn& newVin) :
658+
CMasternodePing::CMasternodePing(const CTxIn& newVin, const uint256& nBlockHash) :
644659
CSignedMessage(),
645660
vin(newVin),
661+
blockHash(nBlockHash),
646662
sigTime(GetAdjustedTime())
647-
{
648-
int nHeight;
649-
{
650-
LOCK(cs_main);
651-
nHeight = chainActive.Height();
652-
if (nHeight > 12)
653-
blockHash = chainActive[nHeight - 12]->GetBlockHash();
654-
}
655-
}
663+
{ }
656664

657665
uint256 CMasternodePing::GetHash() const
658666
{

src/masternode.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
#include "timedata.h"
1515
#include "util.h"
1616

17+
/* Depth of the block pinged by masternodes */
18+
static const unsigned int MNPING_DEPTH = 12;
19+
1720
class CMasternode;
1821
class CMasternodeBroadcast;
1922
class CMasternodePing;
@@ -37,7 +40,7 @@ class CMasternodePing : public CSignedMessage
3740
int64_t sigTime; //mnb message times
3841

3942
CMasternodePing();
40-
CMasternodePing(CTxIn& newVin);
43+
CMasternodePing(const CTxIn& newVin, const uint256& nBlockHash);
4144

4245
ADD_SERIALIZE_METHODS;
4346

@@ -307,8 +310,8 @@ class CMasternodeBroadcast : public CMasternode
307310
}
308311

309312
/// Create Masternode broadcast, needs to be relayed manually after that
310-
static bool Create(CTxIn vin, CService service, CKey keyCollateralAddressNew, CPubKey pubKeyCollateralAddressNew, CKey keyMasternodeNew, CPubKey pubKeyMasternodeNew, std::string& strErrorRet, CMasternodeBroadcast& mnbRet);
311-
static bool Create(std::string strService, std::string strKey, std::string strTxHash, std::string strOutputIndex, std::string& strErrorRet, CMasternodeBroadcast& mnbRet, bool fOffline = false);
313+
static bool Create(const CTxIn& vin, const CService& service, const CKey& keyCollateralAddressNew, const CPubKey& pubKeyCollateralAddressNew, const CKey& keyMasternodeNew, const CPubKey& pubKeyMasternodeNew, std::string& strErrorRet, CMasternodeBroadcast& mnbRet);
314+
static bool Create(const std::string& strService, const std::string& strKey, const std::string& strTxHash, const std::string& strOutputIndex, std::string& strErrorRet, CMasternodeBroadcast& mnbRet, bool fOffline = false);
312315
static bool CheckDefaultPort(CService service, std::string& strErrorRet, const std::string& strContext);
313316
};
314317

src/masternodeman.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ class CMasternodeMan
173173
void UncacheBlockHash(const CBlockIndex* pindex);
174174
uint256 GetHashAtHeight(int nHeight) const;
175175
bool IsWithinDepth(const uint256& nHash, int depth) const;
176+
uint256 GetBlockHashToPing() const { return GetHashAtHeight(GetBestHeight() - MNPING_DEPTH); }
176177
std::vector<uint256> GetCachedBlocks() const { return cvLastBlockHashes.GetCache(); }
177178
};
178179

0 commit comments

Comments
 (0)