Skip to content

Commit

Permalink
Add and guard new max time window for mnb and mnp sigtime.
Browse files Browse the repository at this point in the history
  • Loading branch information
furszy committed Aug 3, 2021
1 parent 7f8fb12 commit e98c1e8
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 25 deletions.
22 changes: 14 additions & 8 deletions src/masternode-payments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,11 @@ std::string CMasternodePaymentWinner::GetStrMessage() const
return vinMasternode.prevout.ToStringShort() + std::to_string(nBlockHeight) + HexStr(payee);
}

bool CMasternodePaymentWinner::IsValid(CNode* pnode, std::string& strError)
bool CMasternodePaymentWinner::IsValid(CNode* pnode, std::string& strError, int chainHeight)
{
int n = mnodeman.GetMasternodeRank(vinMasternode, nBlockHeight - 100);

if (n < 1 || n > MNPAYMENTS_SIGNATURES_TOTAL) {
bool guard = Params().GetConsensus().NetworkUpgradeActive(chainHeight, Consensus::UPGRADE_V5_3);
if ((guard && n < 1) || n > MNPAYMENTS_SIGNATURES_TOTAL) {
//It's common to have masternodes mistakenly think they are in the top 10
// We don't want to print all of these messages, or punish them unless they're way off
if (n > MNPAYMENTS_SIGNATURES_TOTAL * 2) {
Expand All @@ -176,6 +176,12 @@ bool CMasternodePaymentWinner::IsValid(CNode* pnode, std::string& strError)
return false;
}

// Must be a P2PKH
if (guard && !payee.IsPayToPublicKeyHash()) {
LogPrint(BCLog::MASTERNODE, "%s - payee must be a P2PKH\n", __func__);
return false;
}

return true;
}

Expand Down Expand Up @@ -219,9 +225,9 @@ bool IsBlockValueValid(int nHeight, CAmount& nExpectedValue, CAmount nMinted, CA
}
}

// !todo: remove after V6 enforcement and default it to true
const bool isV6UpgradeEnforced = consensus.NetworkUpgradeActive(nHeight, Consensus::UPGRADE_V6_0);
return (!isV6UpgradeEnforced || nMinted >= 0) && nMinted <= nExpectedValue;
// !todo: remove after V5.3 enforcement and default it to true
const bool isUpgradeEnforced = consensus.NetworkUpgradeActive(nHeight, Consensus::UPGRADE_V5_3);
return (!isUpgradeEnforced || nMinted >= 0) && nMinted <= nExpectedValue;
}

bool IsBlockPayeeValid(const CBlock& block, const CBlockIndex* pindexPrev)
Expand Down Expand Up @@ -326,7 +332,7 @@ bool CMasternodePayments::GetLegacyMasternodeTxOut(int nHeight, std::vector<CTxO
if (!GetBlockPayee(nHeight, payee)) {
//no masternode detected
const Consensus::Params& consensus = Params().GetConsensus();
const uint256& hash = consensus.NetworkUpgradeActive(nHeight, Consensus::UPGRADE_V6_0) ?
const uint256& hash = consensus.NetworkUpgradeActive(nHeight, Consensus::UPGRADE_V5_3) ?
mnodeman.GetHashAtHeight(nHeight - 1) : consensus.hashGenesisBlock;
MasternodeRef winningNode = mnodeman.GetCurrentMasterNode(hash);
if (winningNode) {
Expand Down Expand Up @@ -457,7 +463,7 @@ void CMasternodePayments::ProcessMessageMasternodePayments(CNode* pfrom, std::st
}

std::string strError = "";
if (!winner.IsValid(pfrom, strError)) {
if (!winner.IsValid(pfrom, strError, nHeight)) {
// if(strError != "") LogPrint(BCLog::MASTERNODE,"mnw - invalid message - %s\n", strError);
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/masternode-payments.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class CMasternodePaymentWinner : public CSignedMessage
std::string GetStrMessage() const override;
CTxIn GetVin() const { return vinMasternode; };

bool IsValid(CNode* pnode, std::string& strError);
bool IsValid(CNode* pnode, std::string& strError, int chainHeight);
void Relay();

void AddPayee(const CScript& payeeIn)
Expand Down
25 changes: 16 additions & 9 deletions src/masternode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ int MasternodeRemovalSeconds()
return Params().IsRegTestNet() ? MASTERNODE_REMOVAL_SECONDS_REGTEST : MASTERNODE_REMOVAL_SECONDS;
}

// Used for sigTime < maxTimeWindow
int64_t GetMaxTimeWindow(int chainHeight)
{
bool isV5_3 = Params().GetConsensus().NetworkUpgradeActive(chainHeight, Consensus::UPGRADE_V5_3);
return GetAdjustedTime() + (isV5_3 ? (60 * 2) : (60 * 60));
}


CMasternode::CMasternode() :
CSignedMessage()
Expand Down Expand Up @@ -136,7 +143,7 @@ std::string CMasternode::GetStrMessage() const
//
// When a new masternode broadcast is sent, update our information
//
bool CMasternode::UpdateFromNewBroadcast(CMasternodeBroadcast& mnb)
bool CMasternode::UpdateFromNewBroadcast(CMasternodeBroadcast& mnb, int chainHeight)
{
if (mnb.sigTime > sigTime) {
// TODO: lock cs. Need to be careful as mnb.lastPing.CheckAndUpdate locks cs_main internally.
Expand All @@ -147,7 +154,7 @@ bool CMasternode::UpdateFromNewBroadcast(CMasternodeBroadcast& mnb)
protocolVersion = mnb.protocolVersion;
addr = mnb.addr;
int nDoS = 0;
if (mnb.lastPing.IsNull() || (!mnb.lastPing.IsNull() && mnb.lastPing.CheckAndUpdate(nDoS, false))) {
if (mnb.lastPing.IsNull() || (!mnb.lastPing.IsNull() && mnb.lastPing.CheckAndUpdate(nDoS, chainHeight, false))) {
lastPing = mnb.lastPing;
mnodeman.mapSeenMasternodePing.emplace(lastPing.GetHash(), lastPing);
}
Expand Down Expand Up @@ -393,10 +400,10 @@ bool CMasternodeBroadcast::CheckDefaultPort(CService service, std::string& strEr
return true;
}

bool CMasternodeBroadcast::CheckAndUpdate(int& nDos)
bool CMasternodeBroadcast::CheckAndUpdate(int& nDos, int nChainHeight)
{
// make sure signature isn't in the future (past is OK)
if (sigTime > GetAdjustedTime() + 60 * 60) {
if (sigTime > GetMaxTimeWindow(nChainHeight)) {
LogPrint(BCLog::MASTERNODE,"mnb - Signature rejected, too far into the future %s\n", vin.prevout.hash.ToString());
nDos = 1;
return false;
Expand Down Expand Up @@ -444,7 +451,7 @@ bool CMasternodeBroadcast::CheckAndUpdate(int& nDos)
return false;

// incorrect ping or its sigTime
if(lastPing.IsNull() || !lastPing.CheckAndUpdate(nDos, false, true)) {
if(lastPing.IsNull() || !lastPing.CheckAndUpdate(nDos, nChainHeight, false, true)) {
return false;
}

Expand All @@ -470,7 +477,7 @@ bool CMasternodeBroadcast::CheckAndUpdate(int& nDos)
if (pmn->pubKeyCollateralAddress == pubKeyCollateralAddress && !pmn->IsBroadcastedWithin(MasternodeBroadcastSeconds())) {
//take the newest entry
LogPrint(BCLog::MASTERNODE,"mnb - Got updated entry for %s\n", vin.prevout.hash.ToString());
if (pmn->UpdateFromNewBroadcast((*this))) {
if (pmn->UpdateFromNewBroadcast((*this), nChainHeight)) {
if (pmn->IsEnabled()) Relay();
}
masternodeSync.AddedMasternodeList(GetHash());
Expand All @@ -491,7 +498,7 @@ bool CMasternodeBroadcast::CheckInputsAndAdd(int nChainHeight, int& nDoS)
}

// incorrect ping or its sigTime
if(lastPing.IsNull() || !lastPing.CheckAndUpdate(nDoS, false, true)) {
if(lastPing.IsNull() || !lastPing.CheckAndUpdate(nDoS, nChainHeight, false, true)) {
return false;
}

Expand Down Expand Up @@ -589,9 +596,9 @@ std::string CMasternodePing::GetStrMessage() const
return vin.ToString() + blockHash.ToString() + std::to_string(sigTime);
}

bool CMasternodePing::CheckAndUpdate(int& nDos, bool fRequireAvailable, bool fCheckSigTimeOnly)
bool CMasternodePing::CheckAndUpdate(int& nDos, int nChainHeight, bool fRequireAvailable, bool fCheckSigTimeOnly)
{
if (sigTime > GetAdjustedTime() + 60 * 60) {
if (sigTime > GetMaxTimeWindow(nChainHeight)) {
LogPrint(BCLog::MNPING,"%s: Signature rejected, too far into the future %s\n", __func__, vin.prevout.hash.ToString());
nDos = 30;
return false;
Expand Down
6 changes: 3 additions & 3 deletions src/masternode.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class CMasternodePing : public CSignedMessage
const CTxIn GetVin() const { return vin; };
bool IsNull() const { return blockHash.IsNull() || vin.prevout.IsNull(); }

bool CheckAndUpdate(int& nDos, bool fRequireAvailable = true, bool fCheckSigTimeOnly = false);
bool CheckAndUpdate(int& nDos, int nChainHeight, bool fRequireAvailable = true, bool fCheckSigTimeOnly = false);
void Relay();

CMasternodePing& operator=(const CMasternodePing& other) = default;
Expand Down Expand Up @@ -156,7 +156,7 @@ class CMasternode : public CSignedMessage
Unserialize(s);
}

bool UpdateFromNewBroadcast(CMasternodeBroadcast& mnb);
bool UpdateFromNewBroadcast(CMasternodeBroadcast& mnb, int chainHeight);

CMasternode::state GetActiveState() const;

Expand Down Expand Up @@ -243,7 +243,7 @@ class CMasternodeBroadcast : public CMasternode
CMasternodeBroadcast(CService newAddr, CTxIn newVin, CPubKey newPubkey, CPubKey newPubkey2, int protocolVersionIn, const CMasternodePing& _lastPing);
CMasternodeBroadcast(const CMasternode& mn);

bool CheckAndUpdate(int& nDoS);
bool CheckAndUpdate(int& nDoS, int nChainHeight);
bool CheckInputsAndAdd(int chainHeight, int& nDos);

uint256 GetHash() const;
Expand Down
6 changes: 3 additions & 3 deletions src/masternodeman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ int CMasternodeMan::ProcessMNBroadcast(CNode* pfrom, CMasternodeBroadcast& mnb)
}

int nDoS = 0;
if (!mnb.CheckAndUpdate(nDoS)) {
if (!mnb.CheckAndUpdate(nDoS, GetBestHeight())) {
return nDoS;
}

Expand Down Expand Up @@ -747,7 +747,7 @@ int CMasternodeMan::ProcessMNPing(CNode* pfrom, CMasternodePing& mnp)
if (mapSeenMasternodePing.count(mnpHash)) return 0; //seen

int nDoS = 0;
if (mnp.CheckAndUpdate(nDoS)) return 0;
if (mnp.CheckAndUpdate(nDoS, GetBestHeight())) return 0;

if (nDoS > 0) {
// if anything significant failed, mark that node
Expand Down Expand Up @@ -894,7 +894,7 @@ void CMasternodeMan::UpdateMasternodeList(CMasternodeBroadcast& mnb)
CMasternode mn(mnb);
Add(mn);
} else {
pmn->UpdateFromNewBroadcast(mnb);
pmn->UpdateFromNewBroadcast(mnb, GetBestHeight());
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/budget_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ BOOST_FIXTURE_TEST_CASE(block_value_undermint, RegTestingSetup)
CAmount nBudgetAmtRet = 0;
// under-minting blocks are invalid after v6
BOOST_CHECK(IsBlockValueValid(nHeight, nExpectedRet, -1, nBudgetAmtRet));
UpdateNetworkUpgradeParameters(Consensus::UPGRADE_V6_0, Consensus::NetworkUpgrade::ALWAYS_ACTIVE);
UpdateNetworkUpgradeParameters(Consensus::UPGRADE_V5_3, Consensus::NetworkUpgrade::ALWAYS_ACTIVE);
BOOST_CHECK(!IsBlockValueValid(nHeight, nExpectedRet, -1, nBudgetAmtRet));
}

Expand Down

0 comments on commit e98c1e8

Please sign in to comment.