diff --git a/src/instantx.cpp b/src/instantx.cpp index be4148b39..ad5f1691f 100644 --- a/src/instantx.cpp +++ b/src/instantx.cpp @@ -54,13 +54,12 @@ const std::string CInstantSend::SERIALIZATION_VERSION_STRING = "CInstantSend-Ver void CInstantSend::ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStream& vRecv, CConnman& connman) { - if(fLiteMode) return; // disable all Dash specific functionality - if(!sporkManager.IsSporkActive(SPORK_2_INSTANTSEND_ENABLED)) return; + if (fLiteMode) return; // disable all Dash specific functionality + if (!sporkManager.IsSporkActive(SPORK_2_INSTANTSEND_ENABLED)) return; // NOTE: NetMsgType::TXLOCKREQUEST is handled via ProcessMessage() in net_processing.cpp - if (strCommand == NetMsgType::TXLOCKVOTE) // InstantSend Transaction Lock Consensus Votes - { + if (strCommand == NetMsgType::TXLOCKVOTE) { // InstantSend Transaction Lock Consensus Votes if(pfrom->nVersion < MIN_INSTANTSEND_PROTO_VERSION) { LogPrint("instantsend", "TXLOCKVOTE -- peer=%d using obsolete version %i\n", pfrom->id, pfrom->nVersion); connman.PushMessage(pfrom, CNetMsgMaker(pfrom->GetSendVersion()).Make(NetMsgType::REJECT, strCommand, REJECT_OBSOLETE, @@ -77,7 +76,7 @@ void CInstantSend::ProcessMessage(CNode* pfrom, const std::string& strCommand, C pfrom->setAskFor.erase(nVoteHash); // Ignore any InstantSend messages until masternode list is synced - if(!masternodeSync.IsMasternodeListSynced()) return; + if (!masternodeSync.IsMasternodeListSynced()) return; { LOCK(cs_instantsend); @@ -104,7 +103,7 @@ bool CInstantSend::ProcessTxLockRequest(const CTxLockRequest& txLockRequest, CCo // Check to see if we conflict with existing completed lock for (const auto& txin : txLockRequest.tx->vin) { std::map::iterator it = mapLockedOutpoints.find(txin.prevout); - if(it != mapLockedOutpoints.end() && it->second != txLockRequest.GetHash()) { + if (it != mapLockedOutpoints.end() && it->second != txLockRequest.GetHash()) { // Conflicting with complete lock, proceed to see if we should cancel them both LogPrintf("CInstantSend::ProcessTxLockRequest -- WARNING: Found conflicting completed Transaction Lock, txid=%s, completed lock txid=%s\n", txLockRequest.GetHash().ToString(), it->second.ToString()); @@ -115,9 +114,9 @@ bool CInstantSend::ProcessTxLockRequest(const CTxLockRequest& txLockRequest, CCo // if so - do not fail, just warn user for (const auto& txin : txLockRequest.tx->vin) { std::map >::iterator it = mapVotedOutpoints.find(txin.prevout); - if(it != mapVotedOutpoints.end()) { + if (it != mapVotedOutpoints.end()) { for (const auto& hash : it->second) { - if(hash != txLockRequest.GetHash()) { + if (hash != txLockRequest.GetHash()) { LogPrint("instantsend", "CInstantSend::ProcessTxLockRequest -- Double spend attempt! %s\n", txin.prevout.ToStringShort()); // do not fail here, let it go and see which one will get the votes to be locked // NOTIFY ZMQ @@ -132,7 +131,7 @@ bool CInstantSend::ProcessTxLockRequest(const CTxLockRequest& txLockRequest, CCo } } - if(!CreateTxLockCandidate(txLockRequest)) { + if (!CreateTxLockCandidate(txLockRequest)) { // smth is not right LogPrintf("CInstantSend::ProcessTxLockRequest -- CreateTxLockCandidate failed, txid=%s\n", txHash.ToString()); return false; @@ -151,19 +150,19 @@ bool CInstantSend::ProcessTxLockRequest(const CTxLockRequest& txLockRequest, CCo bool CInstantSend::CreateTxLockCandidate(const CTxLockRequest& txLockRequest) { - if(!txLockRequest.IsValid()) return false; + if (!txLockRequest.IsValid()) return false; LOCK(cs_instantsend); uint256 txHash = txLockRequest.GetHash(); std::map::iterator itLockCandidate = mapTxLockCandidates.find(txHash); - if(itLockCandidate == mapTxLockCandidates.end()) { + if (itLockCandidate == mapTxLockCandidates.end()) { LogPrintf("CInstantSend::CreateTxLockCandidate -- new, txid=%s\n", txHash.ToString()); CTxLockCandidate txLockCandidate(txLockRequest); // all inputs should already be checked by txLockRequest.IsValid() above, just use them now - for(const auto& txin : txLockRequest.tx->vin) { + for (const auto& txin : txLockRequest.tx->vin) { txLockCandidate.AddOutPointLock(txin.prevout); } mapTxLockCandidates.insert(std::make_pair(txHash, txLockCandidate)); @@ -177,7 +176,7 @@ bool CInstantSend::CreateTxLockCandidate(const CTxLockRequest& txLockRequest) LogPrintf("CInstantSend::CreateTxLockCandidate -- update empty, txid=%s\n", txHash.ToString()); // all inputs should already be checked by txLockRequest.IsValid() above, just use them now - for(const auto& txin : txLockRequest.tx->vin) { + for (const auto& txin : txLockRequest.tx->vin) { itLockCandidate->second.AddOutPointLock(txin.prevout); } } else { @@ -220,20 +219,20 @@ void CInstantSend::Vote(const uint256& txHash, CConnman& connman) void CInstantSend::Vote(CTxLockCandidate& txLockCandidate, CConnman& connman) { - if(!fMasternodeMode) return; - if(!sporkManager.IsSporkActive(SPORK_2_INSTANTSEND_ENABLED)) return; + if (!fMasternodeMode) return; + if (!sporkManager.IsSporkActive(SPORK_2_INSTANTSEND_ENABLED)) return; AssertLockHeld(cs_main); AssertLockHeld(cs_instantsend); uint256 txHash = txLockCandidate.GetHash(); // We should never vote on a Transaction Lock Request that was not (yet) accepted by the mempool - if(mapLockRequestAccepted.find(txHash) == mapLockRequestAccepted.end()) return; + if (mapLockRequestAccepted.find(txHash) == mapLockRequestAccepted.end()) return; // check if we need to vote on this candidate's outpoints, // it's possible that we need to vote for several of them for (auto& outpointLockPair : txLockCandidate.mapOutPointLocks) { int nPrevoutHeight = GetUTXOHeight(outpointLockPair.first); - if(nPrevoutHeight == -1) { + if (nPrevoutHeight == -1) { LogPrint("instantsend", "CInstantSend::Vote -- Failed to find UTXO %s\n", outpointLockPair.first.ToStringShort()); return; } @@ -242,13 +241,13 @@ void CInstantSend::Vote(CTxLockCandidate& txLockCandidate, CConnman& connman) int nRank; int nMinRequiredProtocol = std::max(MIN_INSTANTSEND_PROTO_VERSION, mnpayments.GetMinMasternodePaymentsProto()); - if(!mnodeman.GetMasternodeRank(activeMasternodeInfo.outpoint, nRank, nLockInputHeight, nMinRequiredProtocol)) { + if (!mnodeman.GetMasternodeRank(activeMasternodeInfo.outpoint, nRank, nLockInputHeight, nMinRequiredProtocol)) { LogPrint("instantsend", "CInstantSend::Vote -- Can't calculate rank for masternode %s\n", activeMasternodeInfo.outpoint.ToStringShort()); continue; } int nSignaturesTotal = COutPointLock::SIGNATURES_TOTAL; - if(nRank > nSignaturesTotal) { + if (nRank > nSignaturesTotal) { LogPrint("instantsend", "CInstantSend::Vote -- Masternode not in the top %d (%d)\n", nSignaturesTotal, nRank); continue; } @@ -260,10 +259,10 @@ void CInstantSend::Vote(CTxLockCandidate& txLockCandidate, CConnman& connman) // Check to see if we already voted for this outpoint, // refuse to vote twice or to include the same outpoint in another tx bool fAlreadyVoted = false; - if(itVoted != mapVotedOutpoints.end()) { + if (itVoted != mapVotedOutpoints.end()) { for (const auto& hash : itVoted->second) { std::map::iterator it2 = mapTxLockCandidates.find(hash); - if(it2->second.HasMasternodeVoted(outpointLockPair.first, activeMasternodeInfo.outpoint)) { + if (it2->second.HasMasternodeVoted(outpointLockPair.first, activeMasternodeInfo.outpoint)) { // we already voted for this outpoint to be included either in the same tx or in a competing one, // skip it anyway fAlreadyVoted = true; @@ -273,18 +272,18 @@ void CInstantSend::Vote(CTxLockCandidate& txLockCandidate, CConnman& connman) } } } - if(fAlreadyVoted) { + if (fAlreadyVoted) { continue; // skip to the next outpoint } // we haven't voted for this outpoint yet, let's try to do this now CTxLockVote vote(txHash, outpointLockPair.first, activeMasternodeInfo.outpoint); - if(!vote.Sign()) { + if (!vote.Sign()) { LogPrintf("CInstantSend::Vote -- Failed to sign consensus vote\n"); return; } - if(!vote.CheckSignature()) { + if (!vote.CheckSignature()) { LogPrintf("CInstantSend::Vote -- Signature invalid\n"); return; } @@ -292,17 +291,17 @@ void CInstantSend::Vote(CTxLockCandidate& txLockCandidate, CConnman& connman) // vote constructed sucessfully, let's store and relay it uint256 nVoteHash = vote.GetHash(); mapTxLockVotes.insert(std::make_pair(nVoteHash, vote)); - if(outpointLockPair.second.AddVote(vote)) { + if (outpointLockPair.second.AddVote(vote)) { LogPrintf("CInstantSend::Vote -- Vote created successfully, relaying: txHash=%s, outpoint=%s, vote=%s\n", txHash.ToString(), outpointLockPair.first.ToStringShort(), nVoteHash.ToString()); - if(itVoted == mapVotedOutpoints.end()) { + if (itVoted == mapVotedOutpoints.end()) { std::set setHashes; setHashes.insert(txHash); mapVotedOutpoints.insert(std::make_pair(outpointLockPair.first, setHashes)); } else { mapVotedOutpoints[outpointLockPair.first].insert(txHash); - if(mapVotedOutpoints[outpointLockPair.first].size() > 1) { + if (mapVotedOutpoints[outpointLockPair.first].size() > 1) { // it's ok to continue, just warn user LogPrintf("CInstantSend::Vote -- WARNING: Vote conflicts with some existing votes: txHash=%s, outpoint=%s, vote=%s\n", txHash.ToString(), outpointLockPair.first.ToStringShort(), nVoteHash.ToString()); @@ -319,7 +318,7 @@ bool CInstantSend::ProcessNewTxLockVote(CNode* pfrom, const CTxLockVote& vote, C uint256 txHash = vote.GetTxHash(); uint256 nVoteHash = vote.GetHash(); - if(!vote.IsValid(pfrom, connman)) { + if (!vote.IsValid(pfrom, connman)) { // could be because of missing MN LogPrint("instantsend", "CInstantSend::%s -- Vote is invalid, txid=%s\n", __func__, txHash.ToString()); return false; @@ -338,9 +337,9 @@ bool CInstantSend::ProcessNewTxLockVote(CNode* pfrom, const CTxLockVote& vote, C // will actually process only after the lock request itself has arrived std::map::iterator it = mapTxLockCandidates.find(txHash); - if(it == mapTxLockCandidates.end() || !it->second.txLockRequest) { + if (it == mapTxLockCandidates.end() || !it->second.txLockRequest) { // no or empty tx lock candidate - if(it == mapTxLockCandidates.end()) { + if (it == mapTxLockCandidates.end()) { // start timeout countdown after the very first vote CreateEmptyTxLockCandidate(txHash); } @@ -353,10 +352,10 @@ bool CInstantSend::ProcessNewTxLockVote(CNode* pfrom, const CTxLockVote& vote, C int nMasternodeOrphanExpireTime = GetTime() + 60*10; // keep time data for 10 minutes auto itMnOV = mapMasternodeOrphanVotes.find(vote.GetMasternodeOutpoint()); - if(itMnOV == mapMasternodeOrphanVotes.end()) { + if (itMnOV == mapMasternodeOrphanVotes.end()) { mapMasternodeOrphanVotes.emplace(vote.GetMasternodeOutpoint(), nMasternodeOrphanExpireTime); } else { - if(itMnOV->second > GetTime() && itMnOV->second > GetAverageMasternodeOrphanVoteTime()) { + if (itMnOV->second > GetTime() && itMnOV->second > GetAverageMasternodeOrphanVoteTime()) { LogPrint("instantsend", "CInstantSend::%s -- masternode is spamming orphan Transaction Lock Votes: txid=%s masternode=%s\n", __func__, txHash.ToString(), vote.GetMasternodeOutpoint().ToStringShort()); // Misbehaving(pfrom->id, 1); @@ -381,7 +380,7 @@ bool CInstantSend::ProcessNewTxLockVote(CNode* pfrom, const CTxLockVote& vote, C UpdateVotedOutpoints(vote, txLockCandidate); - if(!txLockCandidate.AddVote(vote)) { + if (!txLockCandidate.AddVote(vote)) { // this should never happen return false; } @@ -410,7 +409,7 @@ bool CInstantSend::ProcessOrphanTxLockVote(const CTxLockVote& vote) // We shouldn't process orphan votes without a valid tx lock candidate std::map::iterator it = mapTxLockCandidates.find(txHash); - if(it == mapTxLockCandidates.end() || !it->second.txLockRequest) + if (it == mapTxLockCandidates.end() || !it->second.txLockRequest) return false; // this shouldn never happen CTxLockCandidate& txLockCandidate = it->second; @@ -424,7 +423,7 @@ bool CInstantSend::ProcessOrphanTxLockVote(const CTxLockVote& vote) UpdateVotedOutpoints(vote, txLockCandidate); - if(!txLockCandidate.AddVote(vote)) { + if (!txLockCandidate.AddVote(vote)) { // this should never happen return false; } @@ -444,14 +443,14 @@ void CInstantSend::UpdateVotedOutpoints(const CTxLockVote& vote, CTxLockCandidat uint256 txHash = vote.GetTxHash(); std::map >::iterator it1 = mapVotedOutpoints.find(vote.GetOutpoint()); - if(it1 != mapVotedOutpoints.end()) { + if (it1 != mapVotedOutpoints.end()) { for (const auto& hash : it1->second) { - if(hash != txHash) { + if (hash != txHash) { // same outpoint was already voted to be locked by another tx lock request, // let's see if it was the same masternode who voted on this outpoint // for another tx lock request std::map::iterator it2 = mapTxLockCandidates.find(hash); - if(it2 !=mapTxLockCandidates.end() && it2->second.HasMasternodeVoted(vote.GetOutpoint(), vote.GetMasternodeOutpoint())) { + if (it2 !=mapTxLockCandidates.end() && it2->second.HasMasternodeVoted(vote.GetOutpoint(), vote.GetMasternodeOutpoint())) { // yes, it was the same masternode LogPrintf("CInstantSend::%s -- masternode sent conflicting votes! %s\n", __func__, vote.GetMasternodeOutpoint().ToStringShort()); // mark both Lock Candidates as attacked, none of them should complete, @@ -480,8 +479,8 @@ void CInstantSend::ProcessOrphanTxLockVotes() AssertLockHeld(cs_instantsend); std::map::iterator it = mapTxLockVotesOrphan.begin(); - while(it != mapTxLockVotesOrphan.end()) { - if(ProcessOrphanTxLockVote(it->second)) { + while (it != mapTxLockVotesOrphan.end()) { + if (ProcessOrphanTxLockVote(it->second)) { mapTxLockVotesOrphan.erase(it++); } else { ++it; @@ -491,16 +490,16 @@ void CInstantSend::ProcessOrphanTxLockVotes() void CInstantSend::TryToFinalizeLockCandidate(const CTxLockCandidate& txLockCandidate) { - if(!sporkManager.IsSporkActive(SPORK_2_INSTANTSEND_ENABLED)) return; + if (!sporkManager.IsSporkActive(SPORK_2_INSTANTSEND_ENABLED)) return; AssertLockHeld(cs_main); AssertLockHeld(cs_instantsend); uint256 txHash = txLockCandidate.txLockRequest.tx->GetHash(); - if(txLockCandidate.IsAllOutPointsReady() && !IsLockedInstantSendTransaction(txHash)) { + if (txLockCandidate.IsAllOutPointsReady() && !IsLockedInstantSendTransaction(txHash)) { // we have enough votes now LogPrint("instantsend", "CInstantSend::TryToFinalizeLockCandidate -- Transaction Lock is ready to complete, txid=%s\n", txHash.ToString()); - if(ResolveConflicts(txLockCandidate)) { + if (ResolveConflicts(txLockCandidate)) { LockTransactionInputs(txLockCandidate); UpdateLockedTransaction(txLockCandidate); } @@ -512,22 +511,23 @@ void CInstantSend::UpdateLockedTransaction(const CTxLockCandidate& txLockCandida // cs_main, cs_wallet and cs_instantsend should be already locked AssertLockHeld(cs_main); #ifdef ENABLE_WALLET - if (pwalletMain) + if (pwalletMain) { AssertLockHeld(pwalletMain->cs_wallet); + } #endif AssertLockHeld(cs_instantsend); uint256 txHash = txLockCandidate.GetHash(); - if(!IsLockedInstantSendTransaction(txHash)) return; // not a locked tx, do not update/notify + if (!IsLockedInstantSendTransaction(txHash)) return; // not a locked tx, do not update/notify #ifdef ENABLE_WALLET - if(pwalletMain && pwalletMain->UpdatedTransaction(txHash)) { + if (pwalletMain && pwalletMain->UpdatedTransaction(txHash)) { // bumping this to update UI nCompleteTXLocks++; // notify an external script once threshold is reached std::string strCmd = GetArg("-instantsendnotify", ""); - if(!strCmd.empty()) { + if (!strCmd.empty()) { boost::replace_all(strCmd, "%s", txHash.GetHex()); boost::thread t(runCommand, strCmd); // thread runs free } @@ -541,13 +541,13 @@ void CInstantSend::UpdateLockedTransaction(const CTxLockCandidate& txLockCandida void CInstantSend::LockTransactionInputs(const CTxLockCandidate& txLockCandidate) { - if(!sporkManager.IsSporkActive(SPORK_2_INSTANTSEND_ENABLED)) return; + if (!sporkManager.IsSporkActive(SPORK_2_INSTANTSEND_ENABLED)) return; LOCK(cs_instantsend); uint256 txHash = txLockCandidate.GetHash(); - if(!txLockCandidate.IsAllOutPointsReady()) return; + if (!txLockCandidate.IsAllOutPointsReady()) return; for (const auto& pair : txLockCandidate.mapOutPointLocks) { mapLockedOutpoints.insert(std::make_pair(pair.first, txHash)); @@ -559,7 +559,7 @@ bool CInstantSend::GetLockedOutPointTxHash(const COutPoint& outpoint, uint256& h { LOCK(cs_instantsend); std::map::iterator it = mapLockedOutpoints.find(outpoint); - if(it == mapLockedOutpoints.end()) return false; + if (it == mapLockedOutpoints.end()) return false; hashRet = it->second; return true; } @@ -572,18 +572,18 @@ bool CInstantSend::ResolveConflicts(const CTxLockCandidate& txLockCandidate) uint256 txHash = txLockCandidate.GetHash(); // make sure the lock is ready - if(!txLockCandidate.IsAllOutPointsReady()) return false; + if (!txLockCandidate.IsAllOutPointsReady()) return false; AssertLockHeld(mempool.cs); // protect mempool.mapNextTx for (const auto& txin : txLockCandidate.txLockRequest.tx->vin) { uint256 hashConflicting; - if(GetLockedOutPointTxHash(txin.prevout, hashConflicting) && txHash != hashConflicting) { + if (GetLockedOutPointTxHash(txin.prevout, hashConflicting) && txHash != hashConflicting) { // completed lock which conflicts with another completed one? // this means that majority of MNs in the quorum for this specific tx input are malicious! std::map::iterator itLockCandidate = mapTxLockCandidates.find(txHash); std::map::iterator itLockCandidateConflicting = mapTxLockCandidates.find(hashConflicting); - if(itLockCandidate == mapTxLockCandidates.end() || itLockCandidateConflicting == mapTxLockCandidates.end()) { + if (itLockCandidate == mapTxLockCandidates.end() || itLockCandidateConflicting == mapTxLockCandidates.end()) { // safety check, should never really happen LogPrintf("CInstantSend::ResolveConflicts -- ERROR: Found conflicting completed Transaction Lock, but one of txLockCandidate-s is missing, txid=%s, conflicting txid=%s\n", txHash.ToString(), hashConflicting.ToString()); @@ -613,7 +613,7 @@ bool CInstantSend::ResolveConflicts(const CTxLockCandidate& txLockCandidate) } else if (mempool.mapNextTx.count(txin.prevout)) { // check if it's in mempool hashConflicting = mempool.mapNextTx.find(txin.prevout)->second->GetHash(); - if(txHash == hashConflicting) continue; // matches current, not a conflict, skip to next txin + if (txHash == hashConflicting) continue; // matches current, not a conflict, skip to next txin // conflicts with tx in mempool LogPrintf("CInstantSend::ResolveConflicts -- ERROR: Failed to complete Transaction Lock, conflicts with mempool, txid=%s\n", txHash.ToString()); return false; @@ -622,14 +622,14 @@ bool CInstantSend::ResolveConflicts(const CTxLockCandidate& txLockCandidate) // No conflicts were found so far, check to see if it was already included in block CTransactionRef txTmp; uint256 hashBlock; - if(GetTransaction(txHash, txTmp, Params().GetConsensus(), hashBlock, true) && hashBlock != uint256()) { + if (GetTransaction(txHash, txTmp, Params().GetConsensus(), hashBlock, true) && hashBlock != uint256()) { LogPrint("instantsend", "CInstantSend::ResolveConflicts -- Done, %s is included in block %s\n", txHash.ToString(), hashBlock.ToString()); return true; } // Not in block yet, make sure all its inputs are still unspent for (const auto& txin : txLockCandidate.txLockRequest.tx->vin) { Coin coin; - if(!GetUTXOCoin(txin.prevout, coin)) { + if (!GetUTXOCoin(txin.prevout, coin)) { // Not in UTXO anymore? A conflicting tx was mined while we were waiting for votes. LogPrintf("CInstantSend::ResolveConflicts -- ERROR: Failed to find UTXO %s, can't complete Transaction Lock\n", txin.prevout.ToStringShort()); return false; @@ -644,7 +644,7 @@ int64_t CInstantSend::GetAverageMasternodeOrphanVoteTime() { LOCK(cs_instantsend); // NOTE: should never actually call this function when mapMasternodeOrphanVotes is empty - if(mapMasternodeOrphanVotes.empty()) return 0; + if (mapMasternodeOrphanVotes.empty()) return 0; int64_t total = 0; for (const auto& pair : mapMasternodeOrphanVotes) { @@ -656,17 +656,17 @@ int64_t CInstantSend::GetAverageMasternodeOrphanVoteTime() void CInstantSend::CheckAndRemove() { - if(!masternodeSync.IsMasternodeListSynced()) return; + if (!masternodeSync.IsMasternodeListSynced()) return; LOCK(cs_instantsend); std::map::iterator itLockCandidate = mapTxLockCandidates.begin(); // remove expired candidates - while(itLockCandidate != mapTxLockCandidates.end()) { + while (itLockCandidate != mapTxLockCandidates.end()) { CTxLockCandidate &txLockCandidate = itLockCandidate->second; uint256 txHash = txLockCandidate.GetHash(); - if(txLockCandidate.IsExpired(nCachedBlockHeight)) { + if (txLockCandidate.IsExpired(nCachedBlockHeight)) { LogPrintf("CInstantSend::CheckAndRemove -- Removing expired Transaction Lock Candidate: txid=%s\n", txHash.ToString()); for (const auto& pair : txLockCandidate.mapOutPointLocks) { @@ -683,8 +683,8 @@ void CInstantSend::CheckAndRemove() // remove expired votes std::map::iterator itVote = mapTxLockVotes.begin(); - while(itVote != mapTxLockVotes.end()) { - if(itVote->second.IsExpired(nCachedBlockHeight)) { + while (itVote != mapTxLockVotes.end()) { + if (itVote->second.IsExpired(nCachedBlockHeight)) { LogPrint("instantsend", "CInstantSend::CheckAndRemove -- Removing expired vote: txid=%s masternode=%s\n", itVote->second.GetTxHash().ToString(), itVote->second.GetMasternodeOutpoint().ToStringShort()); mapTxLockVotes.erase(itVote++); @@ -695,8 +695,8 @@ void CInstantSend::CheckAndRemove() // remove timed out orphan votes std::map::iterator itOrphanVote = mapTxLockVotesOrphan.begin(); - while(itOrphanVote != mapTxLockVotesOrphan.end()) { - if(itOrphanVote->second.IsTimedOut()) { + while (itOrphanVote != mapTxLockVotesOrphan.end()) { + if (itOrphanVote->second.IsTimedOut()) { LogPrint("instantsend", "CInstantSend::CheckAndRemove -- Removing timed out orphan vote: txid=%s masternode=%s\n", itOrphanVote->second.GetTxHash().ToString(), itOrphanVote->second.GetMasternodeOutpoint().ToStringShort()); mapTxLockVotes.erase(itOrphanVote->first); @@ -708,8 +708,8 @@ void CInstantSend::CheckAndRemove() // remove invalid votes and votes for failed lock attempts itVote = mapTxLockVotes.begin(); - while(itVote != mapTxLockVotes.end()) { - if(itVote->second.IsFailed()) { + while (itVote != mapTxLockVotes.end()) { + if (itVote->second.IsFailed()) { LogPrint("instantsend", "CInstantSend::CheckAndRemove -- Removing vote for failed lock attempt: txid=%s masternode=%s\n", itVote->second.GetTxHash().ToString(), itVote->second.GetMasternodeOutpoint().ToStringShort()); mapTxLockVotes.erase(itVote++); @@ -720,8 +720,8 @@ void CInstantSend::CheckAndRemove() // remove timed out masternode orphan votes (DOS protection) std::map::iterator itMasternodeOrphan = mapMasternodeOrphanVotes.begin(); - while(itMasternodeOrphan != mapMasternodeOrphanVotes.end()) { - if(itMasternodeOrphan->second < GetTime()) { + while (itMasternodeOrphan != mapMasternodeOrphanVotes.end()) { + if (itMasternodeOrphan->second < GetTime()) { LogPrint("instantsend", "CInstantSend::CheckAndRemove -- Removing timed out orphan masternode vote: masternode=%s\n", itMasternodeOrphan->first.ToStringShort()); mapMasternodeOrphanVotes.erase(itMasternodeOrphan++); @@ -763,7 +763,7 @@ bool CInstantSend::GetTxLockRequest(const uint256& txHash, CTxLockRequest& txLoc LOCK(cs_instantsend); std::map::iterator it = mapTxLockCandidates.find(txHash); - if(it == mapTxLockCandidates.end() || !it->second.txLockRequest) return false; + if (it == mapTxLockCandidates.end() || !it->second.txLockRequest) return false; txLockRequestRet = it->second.txLockRequest; return true; @@ -774,7 +774,7 @@ bool CInstantSend::GetTxLockVote(const uint256& hash, CTxLockVote& txLockVoteRet LOCK(cs_instantsend); std::map::iterator it = mapTxLockVotes.find(hash); - if(it == mapTxLockVotes.end()) return false; + if (it == mapTxLockVotes.end()) return false; txLockVoteRet = it->second; return true; @@ -797,22 +797,22 @@ void CInstantSend::Clear() bool CInstantSend::IsLockedInstantSendTransaction(const uint256& txHash) { - if(!fEnableInstantSend || GetfLargeWorkForkFound() || GetfLargeWorkInvalidChainFound() || + if (!fEnableInstantSend || GetfLargeWorkForkFound() || GetfLargeWorkInvalidChainFound() || !sporkManager.IsSporkActive(SPORK_3_INSTANTSEND_BLOCK_FILTERING)) return false; LOCK(cs_instantsend); // there must be a lock candidate std::map::iterator itLockCandidate = mapTxLockCandidates.find(txHash); - if(itLockCandidate == mapTxLockCandidates.end()) return false; + if (itLockCandidate == mapTxLockCandidates.end()) return false; // which should have outpoints - if(itLockCandidate->second.mapOutPointLocks.empty()) return false; + if (itLockCandidate->second.mapOutPointLocks.empty()) return false; // and all of these outputs must be included in mapLockedOutpoints with correct hash for (const auto& pair : itLockCandidate->second.mapOutPointLocks) { uint256 hashLocked; - if(!GetLockedOutPointTxHash(pair.first, hashLocked) || hashLocked != txHash) return false; + if (!GetLockedOutPointTxHash(pair.first, hashLocked) || hashLocked != txHash) return false; } return true; @@ -820,14 +820,14 @@ bool CInstantSend::IsLockedInstantSendTransaction(const uint256& txHash) int CInstantSend::GetTransactionLockSignatures(const uint256& txHash) { - if(!fEnableInstantSend) return -1; - if(GetfLargeWorkForkFound() || GetfLargeWorkInvalidChainFound()) return -2; - if(!sporkManager.IsSporkActive(SPORK_2_INSTANTSEND_ENABLED)) return -3; + if (!fEnableInstantSend) return -1; + if (GetfLargeWorkForkFound() || GetfLargeWorkInvalidChainFound()) return -2; + if (!sporkManager.IsSporkActive(SPORK_2_INSTANTSEND_ENABLED)) return -3; LOCK(cs_instantsend); std::map::iterator itLockCandidate = mapTxLockCandidates.find(txHash); - if(itLockCandidate != mapTxLockCandidates.end()) { + if (itLockCandidate != mapTxLockCandidates.end()) { return itLockCandidate->second.CountVotes(); } @@ -836,7 +836,7 @@ int CInstantSend::GetTransactionLockSignatures(const uint256& txHash) bool CInstantSend::IsTxLockCandidateTimedOut(const uint256& txHash) { - if(!fEnableInstantSend) return false; + if (!fEnableInstantSend) return false; LOCK(cs_instantsend); @@ -882,20 +882,19 @@ void CInstantSend::SyncTransaction(const CTransaction& tx, const CBlockIndex *pi // Check lock candidates std::map::iterator itLockCandidate = mapTxLockCandidates.find(txHash); - if(itLockCandidate != mapTxLockCandidates.end()) { + if (itLockCandidate != mapTxLockCandidates.end()) { LogPrint("instantsend", "CInstantSend::SyncTransaction -- txid=%s nHeightNew=%d lock candidate updated\n", txHash.ToString(), nHeightNew); itLockCandidate->second.SetConfirmedHeight(nHeightNew); // Loop through outpoint locks for (const auto& pair : itLockCandidate->second.mapOutPointLocks) { - // Check corresponding lock votes for (const auto& vote : pair.second.GetVotes()) { uint256 nVoteHash = vote.GetHash(); LogPrint("instantsend", "CInstantSend::SyncTransaction -- txid=%s nHeightNew=%d vote %s updated\n", txHash.ToString(), nHeightNew, nVoteHash.ToString()); const auto& it = mapTxLockVotes.find(nVoteHash); - if(it != mapTxLockVotes.end()) { + if (it != mapTxLockVotes.end()) { it->second.SetConfirmedHeight(nHeightNew); } } @@ -904,7 +903,7 @@ void CInstantSend::SyncTransaction(const CTransaction& tx, const CBlockIndex *pi // check orphan votes for (const auto& pair : mapTxLockVotesOrphan) { - if(pair.second.GetTxHash() == txHash) { + if (pair.second.GetTxHash() == txHash) { LogPrint("instantsend", "CInstantSend::SyncTransaction -- txid=%s nHeightNew=%d vote %s updated\n", txHash.ToString(), nHeightNew, pair.first.ToString()); mapTxLockVotes[pair.first].SetConfirmedHeight(nHeightNew); @@ -920,10 +919,12 @@ std::string CInstantSend::ToString() const bool CInstantSend::CanAutoLock() { - if(!isAutoLockBip9Active) + if (!isAutoLockBip9Active) { return false; - if(!sporkManager.IsSporkActive(SPORK_16_INSTANTSEND_AUTOLOCKS)) + } + if (!sporkManager.IsSporkActive(SPORK_16_INSTANTSEND_AUTOLOCKS)) { return false; + } return (mempool.UsedMemoryShare() < AUTO_IX_MEMPOOL_THRESHOLD); } @@ -933,14 +934,14 @@ bool CInstantSend::CanAutoLock() bool CTxLockRequest::IsValid() const { - if(tx->vout.size() < 1) return false; + if (tx->vout.size() < 1) return false; - if(tx->vin.size() > WARN_MANY_INPUTS) { + if (tx->vin.size() > WARN_MANY_INPUTS) { LogPrint("instantsend", "CTxLockRequest::IsValid -- WARNING: Too many inputs: tx=%s", ToString()); } AssertLockHeld(cs_main); - if(!CheckFinalTx(*tx)) { + if (!CheckFinalTx(*tx)) { LogPrint("instantsend", "CTxLockRequest::IsValid -- Transaction is not final: tx=%s", ToString()); return false; } @@ -953,7 +954,7 @@ bool CTxLockRequest::IsValid() const Coin coin; - if(!GetUTXOCoin(txin.prevout, coin)) { + if (!GetUTXOCoin(txin.prevout, coin)) { LogPrint("instantsend", "CTxLockRequest::IsValid -- Failed to find UTXO %s\n", txin.prevout.ToStringShort()); return false; } @@ -962,7 +963,7 @@ bool CTxLockRequest::IsValid() const // 1 less than the "send IX" gui requires, in case of a block propagating the network at the time int nConfirmationsRequired = nInstantSendConfirmationsRequired - 1; - if(nTxAge < nConfirmationsRequired) { + if (nTxAge < nConfirmationsRequired) { LogPrint("instantsend", "CTxLockRequest::IsValid -- outpoint %s too new: nTxAge=%d, nConfirmationsRequired=%d, txid=%s\n", txin.prevout.ToStringShort(), nTxAge, nConfirmationsRequired, GetHash().ToString()); return false; @@ -971,14 +972,14 @@ bool CTxLockRequest::IsValid() const nValueIn += coin.out.nValue; } - if(nValueIn > sporkManager.GetSporkValue(SPORK_5_INSTANTSEND_MAX_VALUE)*COIN) { + if (nValueIn > sporkManager.GetSporkValue(SPORK_5_INSTANTSEND_MAX_VALUE)*COIN) { LogPrint("instantsend", "CTxLockRequest::IsValid -- Transaction value too high: nValueIn=%d, tx=%s", nValueIn, ToString()); return false; } CAmount nValueOut = tx->GetValueOut(); - if(nValueIn - nValueOut < GetMinFee()) { + if (nValueIn - nValueOut < GetMinFee()) { LogPrint("instantsend", "CTxLockRequest::IsValid -- did not include enough fees in transaction: fees=%d, tx=%s", nValueOut - nValueIn, ToString()); return false; } @@ -988,8 +989,9 @@ bool CTxLockRequest::IsValid() const CAmount CTxLockRequest::GetMinFee() const { - if(IsSimple()) + if (IsSimple()) { return CAmount(); + } CAmount nMinFee = MIN_FEE; return std::max(nMinFee, CAmount(tx->vin.size() * nMinFee)); } @@ -1010,14 +1012,14 @@ bool CTxLockRequest::IsSimple() const bool CTxLockVote::IsValid(CNode* pnode, CConnman& connman) const { - if(!mnodeman.Has(outpointMasternode)) { + if (!mnodeman.Has(outpointMasternode)) { LogPrint("instantsend", "CTxLockVote::IsValid -- Unknown masternode %s\n", outpointMasternode.ToStringShort()); mnodeman.AskForMN(pnode, outpointMasternode, connman); return false; } Coin coin; - if(!GetUTXOCoin(outpoint, coin)) { + if (!GetUTXOCoin(outpoint, coin)) { LogPrint("instantsend", "CTxLockVote::IsValid -- Failed to find UTXO %s\n", outpoint.ToStringShort()); return false; } @@ -1026,7 +1028,7 @@ bool CTxLockVote::IsValid(CNode* pnode, CConnman& connman) const int nRank; int nMinRequiredProtocol = std::max(MIN_INSTANTSEND_PROTO_VERSION, mnpayments.GetMinMasternodePaymentsProto()); - if(!mnodeman.GetMasternodeRank(outpointMasternode, nRank, nLockInputHeight, nMinRequiredProtocol)) { + if (!mnodeman.GetMasternodeRank(outpointMasternode, nRank, nLockInputHeight, nMinRequiredProtocol)) { //can be caused by past versions trying to vote with an invalid protocol LogPrint("instantsend", "CTxLockVote::IsValid -- Can't calculate rank for masternode %s\n", outpointMasternode.ToStringShort()); return false; @@ -1034,13 +1036,13 @@ bool CTxLockVote::IsValid(CNode* pnode, CConnman& connman) const LogPrint("instantsend", "CTxLockVote::IsValid -- Masternode %s, rank=%d\n", outpointMasternode.ToStringShort(), nRank); int nSignaturesTotal = COutPointLock::SIGNATURES_TOTAL; - if(nRank > nSignaturesTotal) { + if (nRank > nSignaturesTotal) { LogPrint("instantsend", "CTxLockVote::IsValid -- Masternode %s is not in the top %d (%d), vote hash=%s\n", outpointMasternode.ToStringShort(), nSignaturesTotal, nRank, GetHash().ToString()); return false; } - if(!CheckSignature()) { + if (!CheckSignature()) { LogPrintf("CTxLockVote::IsValid -- Signature invalid\n"); return false; } @@ -1064,7 +1066,7 @@ bool CTxLockVote::CheckSignature() const masternode_info_t infoMn; - if(!mnodeman.GetMasternodeInfo(outpointMasternode, infoMn)) { + if (!mnodeman.GetMasternodeInfo(outpointMasternode, infoMn)) { LogPrintf("CTxLockVote::CheckSignature -- Unknown Masternode: masternode=%s\n", outpointMasternode.ToString()); return false; } @@ -1075,7 +1077,7 @@ bool CTxLockVote::CheckSignature() const if (!CHashSigner::VerifyHash(hash, infoMn.keyIDOperator, vchMasternodeSignature, strError)) { // could be a signature in old format std::string strMessage = txHash.ToString() + outpoint.ToStringShort(); - if(!CMessageSigner::VerifyMessage(infoMn.keyIDOperator, vchMasternodeSignature, strMessage, strError)) { + if (!CMessageSigner::VerifyMessage(infoMn.keyIDOperator, vchMasternodeSignature, strMessage, strError)) { // nope, not in old format either LogPrintf("CTxLockVote::CheckSignature -- VerifyMessage() failed, error: %s\n", strError); return false; @@ -1083,7 +1085,7 @@ bool CTxLockVote::CheckSignature() const } } else { std::string strMessage = txHash.ToString() + outpoint.ToStringShort(); - if(!CMessageSigner::VerifyMessage(infoMn.keyIDOperator, vchMasternodeSignature, strMessage, strError)) { + if (!CMessageSigner::VerifyMessage(infoMn.keyIDOperator, vchMasternodeSignature, strMessage, strError)) { LogPrintf("CTxLockVote::CheckSignature -- VerifyMessage() failed, error: %s\n", strError); return false; } @@ -1099,7 +1101,7 @@ bool CTxLockVote::Sign() if (sporkManager.IsSporkActive(SPORK_6_NEW_SIGS)) { uint256 hash = GetSignatureHash(); - if(!CHashSigner::SignHash(hash, activeMasternodeInfo.keyOperator, vchMasternodeSignature)) { + if (!CHashSigner::SignHash(hash, activeMasternodeInfo.keyOperator, vchMasternodeSignature)) { LogPrintf("CTxLockVote::Sign -- SignHash() failed\n"); return false; } @@ -1111,12 +1113,12 @@ bool CTxLockVote::Sign() } else { std::string strMessage = txHash.ToString() + outpoint.ToStringShort(); - if(!CMessageSigner::SignMessage(strMessage, vchMasternodeSignature, activeMasternodeInfo.keyOperator)) { + if (!CMessageSigner::SignMessage(strMessage, vchMasternodeSignature, activeMasternodeInfo.keyOperator)) { LogPrintf("CTxLockVote::Sign -- SignMessage() failed\n"); return false; } - if(!CMessageSigner::VerifyMessage(activeMasternodeInfo.keyIDOperator, vchMasternodeSignature, strMessage, strError)) { + if (!CMessageSigner::VerifyMessage(activeMasternodeInfo.keyIDOperator, vchMasternodeSignature, strMessage, strError)) { LogPrintf("CTxLockVote::Sign -- VerifyMessage() failed, error: %s\n", strError); return false; } @@ -1129,10 +1131,11 @@ void CTxLockVote::Relay(CConnman& connman) const { CInv inv(MSG_TXLOCK_VOTE, GetHash()); CTxLockRequest request; - if(instantsend.GetTxLockRequest(txHash, request)) + if (instantsend.GetTxLockRequest(txHash, request)) { connman.RelayInvFiltered(inv, *request.tx); - else + } else { connman.RelayInv(inv); + } } bool CTxLockVote::IsExpired(int nHeight) const @@ -1193,23 +1196,23 @@ void CTxLockCandidate::AddOutPointLock(const COutPoint& outpoint) void CTxLockCandidate::MarkOutpointAsAttacked(const COutPoint& outpoint) { std::map::iterator it = mapOutPointLocks.find(outpoint); - if(it != mapOutPointLocks.end()) + if (it != mapOutPointLocks.end()) it->second.MarkAsAttacked(); } bool CTxLockCandidate::AddVote(const CTxLockVote& vote) { std::map::iterator it = mapOutPointLocks.find(vote.GetOutpoint()); - if(it == mapOutPointLocks.end()) return false; + if (it == mapOutPointLocks.end()) return false; return it->second.AddVote(vote); } bool CTxLockCandidate::IsAllOutPointsReady() const { - if(mapOutPointLocks.empty()) return false; + if (mapOutPointLocks.empty()) return false; for (const auto& pair : mapOutPointLocks) { - if(!pair.second.IsReady()) return false; + if (!pair.second.IsReady()) return false; } return true; }