diff --git a/src/evo/deterministicmns.cpp b/src/evo/deterministicmns.cpp index ee6c4bba93cc..0b4e8b6761b7 100644 --- a/src/evo/deterministicmns.cpp +++ b/src/evo/deterministicmns.cpp @@ -44,9 +44,7 @@ std::string CDeterministicMN::ToString() const UniValue CDeterministicMN::ToJson() const { - UniValue obj; - obj.setObject(); - + UniValue obj(UniValue::VOBJ); obj.pushKV("type", std::string(GetMnType(nType).description)); obj.pushKV("proTxHash", proTxHash.ToString()); obj.pushKV("collateralHash", collateralOutpoint.hash.ToString()); diff --git a/src/evo/dmnstate.cpp b/src/evo/dmnstate.cpp index 1c389cc7db4f..8943640b8db3 100644 --- a/src/evo/dmnstate.cpp +++ b/src/evo/dmnstate.cpp @@ -37,8 +37,7 @@ std::string CDeterministicMNState::ToString() const UniValue CDeterministicMNState::ToJson(MnType nType) const { - UniValue obj; - obj.setObject(); + UniValue obj(UniValue::VOBJ); obj.pushKV("version", nVersion); obj.pushKV("service", addr.ToStringAddrPort()); obj.pushKV("registeredHeight", nRegisteredHeight); @@ -69,8 +68,7 @@ UniValue CDeterministicMNState::ToJson(MnType nType) const UniValue CDeterministicMNStateDiff::ToJson(MnType nType) const { - UniValue obj; - obj.setObject(); + UniValue obj(UniValue::VOBJ); if (fields & Field_nVersion) { obj.pushKV("version", state.nVersion); } diff --git a/src/evo/mnhftx.h b/src/evo/mnhftx.h index d6a3ca59bcb2..b59cd75dcf1e 100644 --- a/src/evo/mnhftx.h +++ b/src/evo/mnhftx.h @@ -49,9 +49,7 @@ class MNHFTx [[nodiscard]] UniValue ToJson() const { - UniValue obj; - obj.clear(); - obj.setObject(); + UniValue obj(UniValue::VOBJ); obj.pushKV("versionBit", (int)versionBit); obj.pushKV("quorumHash", quorumHash.ToString()); obj.pushKV("sig", sig.ToString()); diff --git a/src/evo/simplifiedmns.cpp b/src/evo/simplifiedmns.cpp index 58c0fc0824c1..f99dbe08166a 100644 --- a/src/evo/simplifiedmns.cpp +++ b/src/evo/simplifiedmns.cpp @@ -72,8 +72,7 @@ std::string CSimplifiedMNListEntry::ToString() const UniValue CSimplifiedMNListEntry::ToJson(bool extended) const { - UniValue obj; - obj.setObject(); + UniValue obj(UniValue::VOBJ); obj.pushKV("nVersion", nVersion); obj.pushKV("nType", ToUnderlying(nType)); obj.pushKV("proRegTxHash", proRegTxHash.ToString()); @@ -239,8 +238,7 @@ bool CSimplifiedMNListDiff::BuildQuorumChainlockInfo(const llmq::CQuorumManager& UniValue CSimplifiedMNListDiff::ToJson(bool extended) const { - UniValue obj; - obj.setObject(); + UniValue obj(UniValue::VOBJ); obj.pushKV("nVersion", nVersion); obj.pushKV("baseBlockHash", baseBlockHash.ToString()); diff --git a/src/governance/classes.cpp b/src/governance/classes.cpp index ea08265482fe..62be337be3d1 100644 --- a/src/governance/classes.cpp +++ b/src/governance/classes.cpp @@ -24,55 +24,41 @@ CAmount ParsePaymentAmount(const std::string& strAmount) { CAmount nAmount = 0; if (strAmount.empty()) { - std::ostringstream ostr; - ostr << "ParsePaymentAmount: Amount is empty"; - throw std::runtime_error(ostr.str()); + throw std::runtime_error(strprintf("%s -- Amount is empty", __func__)); } if (strAmount.size() > 20) { // String is much too long, the functions below impose stricter // requirements - std::ostringstream ostr; - ostr << "ParsePaymentAmount: Amount string too long"; - throw std::runtime_error(ostr.str()); + throw std::runtime_error(strprintf("%s -- Amount string too long", __func__)); } // Make sure the string makes sense as an amount // Note: No spaces allowed // Also note: No scientific notation size_t pos = strAmount.find_first_not_of("0123456789."); if (pos != std::string::npos) { - std::ostringstream ostr; - ostr << "ParsePaymentAmount: Amount string contains invalid character"; - throw std::runtime_error(ostr.str()); + throw std::runtime_error(strprintf("%s -- Amount string contains invalid character", __func__)); } pos = strAmount.find('.'); if (pos == 0) { // JSON doesn't allow values to start with a decimal point - std::ostringstream ostr; - ostr << "ParsePaymentAmount: Invalid amount string, leading decimal point not allowed"; - throw std::runtime_error(ostr.str()); + throw std::runtime_error(strprintf("%s -- Invalid amount string, leading decimal point not allowed", __func__)); } // Make sure there's no more than 1 decimal point if ((pos != std::string::npos) && (strAmount.find('.', pos + 1) != std::string::npos)) { - std::ostringstream ostr; - ostr << "ParsePaymentAmount: Invalid amount string, too many decimal points"; - throw std::runtime_error(ostr.str()); + throw std::runtime_error(strprintf("%s -- Invalid amount string, too many decimal points", __func__)); } // Note this code is taken from AmountFromValue in rpcserver.cpp // which is used for parsing the amounts in createrawtransaction. if (!ParseFixedPoint(strAmount, 8, &nAmount)) { nAmount = 0; - std::ostringstream ostr; - ostr << "ParsePaymentAmount: ParseFixedPoint failed for string: " << strAmount; - throw std::runtime_error(ostr.str()); + throw std::runtime_error(strprintf("%s -- ParseFixedPoint failed for string \"%s\"", __func__, strAmount)); } if (!MoneyRange(nAmount)) { nAmount = 0; - std::ostringstream ostr; - ostr << "ParsePaymentAmount: Invalid amount string, value outside of valid money range"; - throw std::runtime_error(ostr.str()); + throw std::runtime_error(strprintf("%s -- Invalid amount string, value outside of valid money range", __func__)); } return nAmount; @@ -188,17 +174,15 @@ void CSuperblock::ParsePaymentSchedule(const std::string& strPaymentAddresses, c // IF THESE DON'T MATCH, SOMETHING IS WRONG if (vecPaymentAddresses.size() != vecPaymentAmounts.size() || vecPaymentAddresses.size() != vecProposalHashes.size()) { - std::ostringstream ostr; - ostr << "CSuperblock::ParsePaymentSchedule -- Mismatched payments, amounts and proposalHashes"; - LogPrintf("%s\n", ostr.str()); - throw std::runtime_error(ostr.str()); + std::string msg{strprintf("CSuperblock::%s -- Mismatched payments, amounts and proposalHashes", __func__)}; + LogPrintf("%s\n", msg); + throw std::runtime_error(msg); } if (vecPaymentAddresses.empty()) { - std::ostringstream ostr; - ostr << "CSuperblock::ParsePaymentSchedule -- Error no payments"; - LogPrintf("%s\n", ostr.str()); - throw std::runtime_error(ostr.str()); + std::string msg{strprintf("CSuperblock::%s -- Error no payments", __func__)}; + LogPrintf("%s\n", msg); + throw std::runtime_error(msg); } // LOOP THROUGH THE ADDRESSES/AMOUNTS AND CREATE PAYMENTS @@ -210,24 +194,22 @@ void CSuperblock::ParsePaymentSchedule(const std::string& strPaymentAddresses, c for (int i = 0; i < (int)vecPaymentAddresses.size(); i++) { CTxDestination dest = DecodeDestination(vecPaymentAddresses[i]); if (!IsValidDestination(dest)) { - std::ostringstream ostr; - ostr << "CSuperblock::ParsePaymentSchedule -- Invalid Dash Address : " << vecPaymentAddresses[i]; - LogPrintf("%s\n", ostr.str()); - throw std::runtime_error(ostr.str()); + std::string msg{strprintf("CSuperblock::%s -- Invalid Dash Address: %s", __func__, vecPaymentAddresses[i])}; + LogPrintf("%s\n", msg); + throw std::runtime_error(msg); } CAmount nAmount = ParsePaymentAmount(vecPaymentAmounts[i]); uint256 proposalHash; if (!ParseHashStr(vecProposalHashes[i], proposalHash)) { - std::ostringstream ostr; - ostr << "CSuperblock::ParsePaymentSchedule -- Invalid proposal hash : " << vecProposalHashes[i]; - LogPrintf("%s\n", ostr.str()); - throw std::runtime_error(ostr.str()); + std::string msg{strprintf("CSuperblock::%s -- Invalid proposal hash: %s", __func__, vecProposalHashes[i])}; + LogPrintf("%s\n", msg); + throw std::runtime_error(msg); } LogPrint(BCLog::GOBJECT, /* Continued */ - "CSuperblock::ParsePaymentSchedule -- i = %d, amount string = %s, nAmount = %lld, proposalHash = %s\n", + "CSuperblock::%s -- i = %d, amount string = %s, nAmount = %lld, proposalHash = %s\n", __func__, i, vecPaymentAmounts[i], nAmount, proposalHash.ToString()); CGovernancePayment payment(dest, nAmount, proposalHash); @@ -235,11 +217,10 @@ void CSuperblock::ParsePaymentSchedule(const std::string& strPaymentAddresses, c vecPayments.push_back(payment); } else { vecPayments.clear(); - std::ostringstream ostr; - ostr << "CSuperblock::ParsePaymentSchedule -- Invalid payment found: address = " << EncodeDestination(dest) - << ", amount = " << nAmount; - LogPrintf("%s\n", ostr.str()); - throw std::runtime_error(ostr.str()); + std::string msg{strprintf("CSuperblock::%s -- Invalid payment found: address = %s, amount = %d", __func__, + EncodeDestination(dest), nAmount)}; + LogPrintf("%s\n", msg); + throw std::runtime_error(msg); } } } diff --git a/src/governance/exceptions.cpp b/src/governance/exceptions.cpp index 884a6490bcaf..f6ecc0e75f29 100644 --- a/src/governance/exceptions.cpp +++ b/src/governance/exceptions.cpp @@ -3,6 +3,7 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include +#include #include #include @@ -32,11 +33,8 @@ std::ostream& operator<<(std::ostream& os, governance_exception_type_enum_t eTyp CGovernanceException::CGovernanceException(const std::string& strMessageIn, governance_exception_type_enum_t eTypeIn, int nNodePenaltyIn) : - strMessage(), - eType(eTypeIn), - nNodePenalty(nNodePenaltyIn) + strMessage{strprintf("%s:%s", eTypeIn, strMessageIn)}, + eType{eTypeIn}, + nNodePenalty{nNodePenaltyIn} { - std::ostringstream ostr; - ostr << eType << ":" << strMessageIn; - strMessage = ostr.str(); } diff --git a/src/governance/governance.cpp b/src/governance/governance.cpp index 7dbd01164b82..2adee076d947 100644 --- a/src/governance/governance.cpp +++ b/src/governance/governance.cpp @@ -1131,36 +1131,34 @@ bool CGovernanceManager::ProcessVote(CNode* pfrom, const CGovernanceVote& vote, uint256 nHashGovobj = vote.GetParentHash(); if (cmapVoteToObject.HasKey(nHashVote)) { - LogPrint(BCLog::GOBJECT, "CGovernanceObject::ProcessVote -- skipping known valid vote %s for object %s\n", nHashVote.ToString(), nHashGovobj.ToString()); + LogPrint(BCLog::GOBJECT, "CGovernanceObject::%s -- skipping known valid vote %s for object %s\n", __func__, + nHashVote.ToString(), nHashGovobj.ToString()); LEAVE_CRITICAL_SECTION(cs); return false; } if (cmapInvalidVotes.HasKey(nHashVote)) { - std::ostringstream ostr; - ostr << "CGovernanceManager::ProcessVote -- Old invalid vote " - << ", MN outpoint = " << vote.GetMasternodeOutpoint().ToStringShort() - << ", governance object hash = " << nHashGovobj.ToString(); - LogPrint(BCLog::GOBJECT, "%s\n", ostr.str()); - exception = CGovernanceException(ostr.str(), GOVERNANCE_EXCEPTION_PERMANENT_ERROR, 20); + std::string msg{strprintf("CGovernanceManager::%s -- Old invalid vote, MN outpoint = %s, governance object hash = %s", + __func__, vote.GetMasternodeOutpoint().ToStringShort(), nHashGovobj.ToString())}; + LogPrint(BCLog::GOBJECT, "%s\n", msg); + exception = CGovernanceException(msg, GOVERNANCE_EXCEPTION_PERMANENT_ERROR, 20); LEAVE_CRITICAL_SECTION(cs); return false; } auto it = mapObjects.find(nHashGovobj); if (it == mapObjects.end()) { - std::ostringstream ostr; - ostr << "CGovernanceManager::ProcessVote -- Unknown parent object " << nHashGovobj.ToString() - << ", MN outpoint = " << vote.GetMasternodeOutpoint().ToStringShort(); - exception = CGovernanceException(ostr.str(), GOVERNANCE_EXCEPTION_WARNING); + std::string msg{strprintf("CGovernanceManager::%s -- Unknown parent object %s, MN outpoint = %s", __func__, + nHashGovobj.ToString(), vote.GetMasternodeOutpoint().ToStringShort())}; + exception = CGovernanceException(msg, GOVERNANCE_EXCEPTION_WARNING); if (cmmapOrphanVotes.Insert(nHashGovobj, vote_time_pair_t(vote, GetTime().count() + GOVERNANCE_ORPHAN_EXPIRATION_TIME))) { LEAVE_CRITICAL_SECTION(cs); RequestGovernanceObject(pfrom, nHashGovobj, connman); - LogPrint(BCLog::GOBJECT, "%s\n", ostr.str()); + LogPrint(BCLog::GOBJECT, "%s\n", msg); return false; } - LogPrint(BCLog::GOBJECT, "%s\n", ostr.str()); + LogPrint(BCLog::GOBJECT, "%s\n", msg); LEAVE_CRITICAL_SECTION(cs); return false; } @@ -1168,7 +1166,8 @@ bool CGovernanceManager::ProcessVote(CNode* pfrom, const CGovernanceVote& vote, CGovernanceObject& govobj = it->second; if (govobj.IsSetCachedDelete() || govobj.IsSetExpired()) { - LogPrint(BCLog::GOBJECT, "CGovernanceObject::ProcessVote -- ignoring vote for expired or deleted object, hash = %s\n", nHashGovobj.ToString()); + LogPrint(BCLog::GOBJECT, "CGovernanceObject::%s -- ignoring vote for expired or deleted object, hash = %s\n", + __func__, nHashGovobj.ToString()); LEAVE_CRITICAL_SECTION(cs); return false; } diff --git a/src/governance/object.cpp b/src/governance/object.cpp index 8dcf9015887a..7776fd09fb7c 100644 --- a/src/governance/object.cpp +++ b/src/governance/object.cpp @@ -66,18 +66,17 @@ bool CGovernanceObject::ProcessVote(CMasternodeMetaMan& mn_metaman, CGovernanceM // do not process already known valid votes twice if (fileVotes.HasVote(vote.GetHash())) { // nothing to do here, not an error - std::ostringstream ostr; - ostr << "CGovernanceObject::ProcessVote -- Already known valid vote"; - LogPrint(BCLog::GOBJECT, "%s\n", ostr.str()); - exception = CGovernanceException(ostr.str(), GOVERNANCE_EXCEPTION_NONE); + std::string msg{strprintf("CGovernanceObject::%s -- Already known valid vote", __func__)}; + LogPrint(BCLog::GOBJECT, "%s\n", msg); + exception = CGovernanceException(msg, GOVERNANCE_EXCEPTION_NONE); return false; } auto dmn = tip_mn_list.GetMNByCollateral(vote.GetMasternodeOutpoint()); if (!dmn) { - std::ostringstream ostr; - ostr << "CGovernanceObject::ProcessVote -- Masternode " << vote.GetMasternodeOutpoint().ToStringShort() << " not found"; - exception = CGovernanceException(ostr.str(), GOVERNANCE_EXCEPTION_PERMANENT_ERROR, 20); + std::string msg{strprintf("CGovernanceObject::%s -- Masternode %s not found", __func__, + vote.GetMasternodeOutpoint().ToStringShort())}; + exception = CGovernanceException(msg, GOVERNANCE_EXCEPTION_PERMANENT_ERROR, 20); return false; } @@ -85,17 +84,16 @@ bool CGovernanceObject::ProcessVote(CMasternodeMetaMan& mn_metaman, CGovernanceM vote_rec_t& voteRecordRef = it->second; vote_signal_enum_t eSignal = vote.GetSignal(); if (eSignal == VOTE_SIGNAL_NONE) { - std::ostringstream ostr; - ostr << "CGovernanceObject::ProcessVote -- Vote signal: none"; - LogPrint(BCLog::GOBJECT, "%s\n", ostr.str()); - exception = CGovernanceException(ostr.str(), GOVERNANCE_EXCEPTION_WARNING); + std::string msg{strprintf("CGovernanceObject::%s -- Vote signal: none", __func__)}; + LogPrint(BCLog::GOBJECT, "%s\n", msg); + exception = CGovernanceException(msg, GOVERNANCE_EXCEPTION_WARNING); return false; } if (eSignal < VOTE_SIGNAL_NONE || eSignal >= VOTE_SIGNAL_UNKNOWN) { - std::ostringstream ostr; - ostr << "CGovernanceObject::ProcessVote -- Unsupported vote signal: " << CGovernanceVoting::ConvertSignalToString(vote.GetSignal()); - LogPrintf("%s\n", ostr.str()); - exception = CGovernanceException(ostr.str(), GOVERNANCE_EXCEPTION_PERMANENT_ERROR, 20); + std::string msg{strprintf("CGovernanceObject::%s -- Unsupported vote signal: %s", __func__, + CGovernanceVoting::ConvertSignalToString(vote.GetSignal()))}; + LogPrintf("%s\n", msg); + exception = CGovernanceException(msg, GOVERNANCE_EXCEPTION_PERMANENT_ERROR, 20); return false; } auto it2 = voteRecordRef.mapInstances.emplace(vote_instance_m_t::value_type(int(eSignal), vote_instance_t())).first; @@ -103,26 +101,24 @@ bool CGovernanceObject::ProcessVote(CMasternodeMetaMan& mn_metaman, CGovernanceM // Reject obsolete votes if (vote.GetTimestamp() < voteInstanceRef.nCreationTime) { - std::ostringstream ostr; - ostr << "CGovernanceObject::ProcessVote -- Obsolete vote"; - LogPrint(BCLog::GOBJECT, "%s\n", ostr.str()); - exception = CGovernanceException(ostr.str(), GOVERNANCE_EXCEPTION_NONE); + std::string msg{strprintf("CGovernanceObject::%s -- Obsolete vote", __func__)}; + LogPrint(BCLog::GOBJECT, "%s\n", msg); + exception = CGovernanceException(msg, GOVERNANCE_EXCEPTION_NONE); return false; } else if (vote.GetTimestamp() == voteInstanceRef.nCreationTime) { // Someone is doing something fishy, there can be no two votes from the same masternode // with the same timestamp for the same object and signal and yet different hash/outcome. - std::ostringstream ostr; - ostr << "CGovernanceObject::ProcessVote -- Invalid vote, same timestamp for the different outcome"; + std::string msg{strprintf("CGovernanceObject::%s -- Invalid vote, same timestamp for the different outcome", __func__)}; if (vote.GetOutcome() < voteInstanceRef.eOutcome) { // This is an arbitrary comparison, we have to agree on some way // to pick the "winning" vote. - ostr << ", rejected"; - LogPrint(BCLog::GOBJECT, "%s\n", ostr.str()); - exception = CGovernanceException(ostr.str(), GOVERNANCE_EXCEPTION_NONE); + msg += ", rejected"; + LogPrint(BCLog::GOBJECT, "%s\n", msg); + exception = CGovernanceException(msg, GOVERNANCE_EXCEPTION_NONE); return false; } - ostr << ", accepted"; - LogPrint(BCLog::GOBJECT, "%s\n", ostr.str()); + msg += ", accepted"; + LogPrint(BCLog::GOBJECT, "%s\n", msg); } int64_t nNow = GetAdjustedTime(); @@ -130,13 +126,11 @@ bool CGovernanceObject::ProcessVote(CMasternodeMetaMan& mn_metaman, CGovernanceM if (govman.AreRateChecksEnabled()) { int64_t nTimeDelta = nNow - voteInstanceRef.nTime; if (nTimeDelta < GOVERNANCE_UPDATE_MIN) { - std::ostringstream ostr; - ostr << "CGovernanceObject::ProcessVote -- Masternode voting too often" - << ", MN outpoint = " << vote.GetMasternodeOutpoint().ToStringShort() - << ", governance object hash = " << GetHash().ToString() - << ", time delta = " << nTimeDelta; - LogPrint(BCLog::GOBJECT, "%s\n", ostr.str()); - exception = CGovernanceException(ostr.str(), GOVERNANCE_EXCEPTION_TEMPORARY_ERROR); + std::string msg{strprintf("CGovernanceObject::%s -- Masternode voting too often, MN outpoint = %s, " + "governance object hash = %s, time delta = %d", + __func__, vote.GetMasternodeOutpoint().ToStringShort(), GetHash().ToString(), nTimeDelta)}; + LogPrint(BCLog::GOBJECT, "%s\n", msg); + exception = CGovernanceException(msg, GOVERNANCE_EXCEPTION_TEMPORARY_ERROR); return false; } nVoteTimeUpdate = nNow; @@ -146,24 +140,21 @@ bool CGovernanceObject::ProcessVote(CMasternodeMetaMan& mn_metaman, CGovernanceM // Finally check that the vote is actually valid (done last because of cost of signature verification) if (!vote.IsValid(tip_mn_list, onlyVotingKeyAllowed)) { - std::ostringstream ostr; - ostr << "CGovernanceObject::ProcessVote -- Invalid vote" - << ", 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); + std::string msg{strprintf("CGovernanceObject::%s -- Invalid vote, MN outpoint = %s, governance object hash = %s, " + "vote hash = %s", + __func__, vote.GetMasternodeOutpoint().ToStringShort(), GetHash().ToString(), vote.GetHash().ToString())}; + LogPrintf("%s\n", msg); + exception = CGovernanceException(msg, GOVERNANCE_EXCEPTION_PERMANENT_ERROR, 20); govman.AddInvalidVote(vote); return false; } if (!mn_metaman.AddGovernanceVote(dmn->proTxHash, vote.GetParentHash())) { - std::ostringstream ostr; - ostr << "CGovernanceObject::ProcessVote -- Unable to add governance vote" - << ", MN outpoint = " << vote.GetMasternodeOutpoint().ToStringShort() - << ", governance object hash = " << GetHash().ToString(); - LogPrint(BCLog::GOBJECT, "%s\n", ostr.str()); - exception = CGovernanceException(ostr.str(), GOVERNANCE_EXCEPTION_PERMANENT_ERROR); + std::string msg{strprintf("CGovernanceObject::%s -- Unable to add governance vote, MN outpoint = %s, " + "governance object hash = %s", + __func__, vote.GetMasternodeOutpoint().ToStringShort(), GetHash().ToString())}; + LogPrint(BCLog::GOBJECT, "%s\n", msg); + exception = CGovernanceException(msg, GOVERNANCE_EXCEPTION_PERMANENT_ERROR); return false; } @@ -323,16 +314,11 @@ void CGovernanceObject::LoadData() m_obj.type = GovernanceObject(obj["type"].get_int()); } catch (std::exception& e) { fUnparsable = true; - std::ostringstream ostr; - ostr << "CGovernanceObject::LoadData Error parsing JSON" - << ", e.what() = " << e.what(); - LogPrintf("%s\n", ostr.str()); + LogPrintf("%s\n", strprintf("CGovernanceObject::LoadData -- Error parsing JSON, e.what() = %s", e.what())); return; } catch (...) { fUnparsable = true; - std::ostringstream ostr; - ostr << "CGovernanceObject::LoadData Unknown Error parsing JSON"; - LogPrintf("%s\n", ostr.str()); + LogPrintf("%s\n", strprintf("CGovernanceObject::LoadData -- Unknown Error parsing JSON")); return; } } diff --git a/src/governance/vote.cpp b/src/governance/vote.cpp index 3e4473be3c0b..aceeaf63ce5a 100644 --- a/src/governance/vote.cpp +++ b/src/governance/vote.cpp @@ -99,13 +99,10 @@ std::string CGovernanceVote::ToString(const CDeterministicMNList& tip_mn_list) c { auto dmn = tip_mn_list.GetMNByCollateral(masternodeOutpoint); int voteWeight = dmn != nullptr ? GetMnType(dmn->nType).voting_weight : 0; - std::ostringstream ostr; - ostr << masternodeOutpoint.ToStringShort() << ":" - << nTime << ":" - << CGovernanceVoting::ConvertOutcomeToString(GetOutcome()) << ":" - << CGovernanceVoting::ConvertSignalToString(GetSignal()) << ":" - << voteWeight; - return ostr.str(); + return strprintf("%s:%d:%s:%s:%d", + masternodeOutpoint.ToStringShort(), nTime, + CGovernanceVoting::ConvertOutcomeToString(GetOutcome()), CGovernanceVoting::ConvertSignalToString(GetSignal()), + voteWeight); } void CGovernanceVote::Relay(PeerManager& peerman, const CMasternodeSync& mn_sync, const CDeterministicMNList& tip_mn_list) const diff --git a/src/llmq/commitment.h b/src/llmq/commitment.h index 3c88256bbbe5..ca55131c9665 100644 --- a/src/llmq/commitment.h +++ b/src/llmq/commitment.h @@ -121,8 +121,7 @@ class CFinalCommitment [[nodiscard]] UniValue ToJson() const { - UniValue obj; - obj.setObject(); + UniValue obj(UniValue::VOBJ); obj.pushKV("version", int{nVersion}); obj.pushKV("llmqType", ToUnderlying(llmqType)); obj.pushKV("quorumHash", quorumHash.ToString()); diff --git a/src/llmq/snapshot.cpp b/src/llmq/snapshot.cpp index 52225ed620a5..e2cbf337197d 100644 --- a/src/llmq/snapshot.cpp +++ b/src/llmq/snapshot.cpp @@ -23,8 +23,7 @@ static const std::string DB_QUORUM_SNAPSHOT = "llmq_S"; UniValue CQuorumSnapshot::ToJson() const { - UniValue obj; - obj.setObject(); + UniValue obj(UniValue::VOBJ); UniValue activeQ(UniValue::VARR); for (const bool h : activeQuorumMembers) { // cppcheck-suppress useStlAlgorithm @@ -43,8 +42,7 @@ UniValue CQuorumSnapshot::ToJson() const UniValue CQuorumRotationInfo::ToJson() const { - UniValue obj; - obj.setObject(); + UniValue obj(UniValue::VOBJ); obj.pushKV("extraShare", extraShare); obj.pushKV("quorumSnapshotAtHMinusC", quorumSnapshotAtHMinusC.ToJson()); diff --git a/src/masternode/meta.cpp b/src/masternode/meta.cpp index 4c2ef7889c31..3cc3ffbb5932 100644 --- a/src/masternode/meta.cpp +++ b/src/masternode/meta.cpp @@ -129,9 +129,6 @@ std::vector CMasternodeMetaMan::GetAndClearDirtyGovernanceObjectHashes( std::string MasternodeMetaStore::ToString() const { - std::ostringstream info; LOCK(cs); - info << "Masternodes: meta infos object count: " << (int)metaInfos.size() << - ", nDsqCount: " << (int)nDsqCount; - return info.str(); + return strprintf("Masternodes: meta infos object count: %d, nDsqCount: %d", metaInfos.size(), nDsqCount); } diff --git a/src/netfulfilledman.cpp b/src/netfulfilledman.cpp index 2c52577a1a24..7bcbdb626127 100644 --- a/src/netfulfilledman.cpp +++ b/src/netfulfilledman.cpp @@ -87,9 +87,7 @@ void NetFulfilledRequestStore::Clear() std::string NetFulfilledRequestStore::ToString() const { - std::ostringstream info; - info << "Nodes with fulfilled requests: " << (int)mapFulfilledRequests.size(); - return info.str(); + return strprintf("Nodes with fulfilled requests: %d", mapFulfilledRequests.size()); } void CNetFulfilledRequestManager::DoMaintenance() diff --git a/src/rpc/evo.cpp b/src/rpc/evo.cpp index 9e877daf66ba..4ed1eade7ad7 100644 --- a/src/rpc/evo.cpp +++ b/src/rpc/evo.cpp @@ -680,7 +680,7 @@ static UniValue protx_register_common_wrapper(const JSONRPCRequest& request, paramIdx += 2; } - if (request.params[paramIdx].get_str() != "") { + if (!request.params[paramIdx].get_str().empty()) { if (auto addr = Lookup(request.params[paramIdx].get_str(), Params().GetDefaultPort(), false); addr.has_value()) { ptx.addr = addr.value(); } else { @@ -694,7 +694,7 @@ static UniValue protx_register_common_wrapper(const JSONRPCRequest& request, CKeyID keyIDVoting = ptx.keyIDOwner; - if (request.params[paramIdx + 3].get_str() != "") { + if (!request.params[paramIdx + 3].get_str().empty()) { keyIDVoting = ParsePubKeyIDFromAddress(request.params[paramIdx + 3].get_str(), "voting address"); } @@ -1106,7 +1106,7 @@ static RPCHelpMan protx_update_registrar_wrapper(const bool specific_legacy_bls_ ptx.keyIDVoting = dmn->pdmnState->keyIDVoting; ptx.scriptPayout = dmn->pdmnState->scriptPayout; - if (request.params[1].get_str() != "") { + if (!request.params[1].get_str().empty()) { // new pubkey ptx.pubKeyOperator.Set(ParseBLSPubKey(request.params[1].get_str(), "operator BLS address", use_legacy), use_legacy); } else { @@ -1116,13 +1116,13 @@ static RPCHelpMan protx_update_registrar_wrapper(const bool specific_legacy_bls_ CHECK_NONFATAL(ptx.pubKeyOperator.IsLegacy() == (ptx.nVersion == ProTxVersion::LegacyBLS)); - if (request.params[2].get_str() != "") { + if (!request.params[2].get_str().empty()) { ptx.keyIDVoting = ParsePubKeyIDFromAddress(request.params[2].get_str(), "voting address"); } CTxDestination payoutDest; ExtractDestination(ptx.scriptPayout, payoutDest); - if (request.params[3].get_str() != "") { + if (!request.params[3].get_str().empty()) { payoutDest = DecodeDestination(request.params[3].get_str()); if (!IsValidDestination(payoutDest)) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("invalid payout address: %s", request.params[3].get_str())); diff --git a/src/rpc/masternode.cpp b/src/rpc/masternode.cpp index e497a8b34b06..d3c99065d02c 100644 --- a/src/rpc/masternode.cpp +++ b/src/rpc/masternode.cpp @@ -263,7 +263,7 @@ static RPCHelpMan masternode_winners() auto payee = node.dmnman->GetListForBlock(pIndex).GetMNPayee(pIndex); if (payee) { std::string strPayments = GetRequiredPaymentsString(*CHECK_NONFATAL(node.govman), tip_mn_list, h, payee); - if (strFilter != "" && strPayments.find(strFilter) == std::string::npos) continue; + if (!strFilter.empty() && strPayments.find(strFilter) == std::string::npos) continue; obj.pushKV(strprintf("%d", h), strPayments); } } @@ -272,7 +272,7 @@ static RPCHelpMan masternode_winners() for (size_t i = 0; i < projection.size(); i++) { int h = nChainTipHeight + 1 + i; std::string strPayments = GetRequiredPaymentsString(*node.govman, tip_mn_list, h, projection[i]); - if (strFilter != "" && strPayments.find(strFilter) == std::string::npos) continue; + if (!strFilter.empty() && strPayments.find(strFilter) == std::string::npos) continue; obj.pushKV(strprintf("%d", h), strPayments); } @@ -566,48 +566,43 @@ static RPCHelpMan masternodelist_helper(bool is_composite) if (strMode == "addr") { std::string strAddress = dmn.pdmnState->addr.ToStringAddrPort(); - if (strFilter !="" && strAddress.find(strFilter) == std::string::npos && + if (!strFilter.empty() && strAddress.find(strFilter) == std::string::npos && strOutpoint.find(strFilter) == std::string::npos) return; obj.pushKV(strOutpoint, strAddress); } else if (strMode == "full") { - std::ostringstream streamFull; - streamFull << std::setw(18) << - dmnToStatus(dmn) << " " << - dmn.pdmnState->nPoSePenalty << " " << - payeeStr << " " << std::setw(10) << - dmnToLastPaidTime(dmn) << " " << std::setw(6) << - dmn.pdmnState->nLastPaidHeight << " " << - dmn.pdmnState->addr.ToStringAddrPort(); - std::string strFull = streamFull.str(); - if (strFilter !="" && strFull.find(strFilter) == std::string::npos && + std::string strFull = strprintf("%s %d %s %s %s %s", + PadString(dmnToStatus(dmn), 18), + dmn.pdmnState->nPoSePenalty, + payeeStr, + PadString(ToString(dmnToLastPaidTime(dmn)), 10), + PadString(ToString(dmn.pdmnState->nLastPaidHeight), 6), + dmn.pdmnState->addr.ToStringAddrPort()); + if (!strFilter.empty() && strFull.find(strFilter) == std::string::npos && strOutpoint.find(strFilter) == std::string::npos) return; obj.pushKV(strOutpoint, strFull); } else if (strMode == "info") { - std::ostringstream streamInfo; - streamInfo << std::setw(18) << - dmnToStatus(dmn) << " " << - dmn.pdmnState->nPoSePenalty << " " << - payeeStr << " " << - dmn.pdmnState->addr.ToStringAddrPort(); - std::string strInfo = streamInfo.str(); - if (strFilter !="" && strInfo.find(strFilter) == std::string::npos && + std::string strInfo = strprintf("%s %d %s %s", + PadString(dmnToStatus(dmn), 18), + dmn.pdmnState->nPoSePenalty, + payeeStr, + dmn.pdmnState->addr.ToStringAddrPort()); + if (!strFilter.empty() && strInfo.find(strFilter) == std::string::npos && strOutpoint.find(strFilter) == std::string::npos) return; obj.pushKV(strOutpoint, strInfo); } else if (strMode == "json" || strMode == "recent" || strMode == "evo") { - std::ostringstream streamInfo; - streamInfo << dmn.proTxHash.ToString() << " " << - dmn.pdmnState->addr.ToStringAddrPort() << " " << - payeeStr << " " << - dmnToStatus(dmn) << " " << - dmn.pdmnState->nPoSePenalty << " " << - dmnToLastPaidTime(dmn) << " " << - dmn.pdmnState->nLastPaidHeight << " " << - EncodeDestination(PKHash(dmn.pdmnState->keyIDOwner)) << " " << - EncodeDestination(PKHash(dmn.pdmnState->keyIDVoting)) << " " << - collateralAddressStr << " " << - dmn.pdmnState->pubKeyOperator.ToString(); - std::string strInfo = streamInfo.str(); - if (strFilter !="" && strInfo.find(strFilter) == std::string::npos && + std::string strInfo = strprintf("%s %s %s %s %d %d %d %s %s %s %s", + dmn.proTxHash.ToString(), + dmn.pdmnState->addr.ToStringAddrPort(), + payeeStr, + dmnToStatus(dmn), + dmn.pdmnState->nPoSePenalty, + dmnToLastPaidTime(dmn), + dmn.pdmnState->nLastPaidHeight, + EncodeDestination(PKHash(dmn.pdmnState->keyIDOwner)), + EncodeDestination(PKHash(dmn.pdmnState->keyIDVoting)), + collateralAddressStr, + dmn.pdmnState->pubKeyOperator.ToString()); + if (!strFilter.empty() && strInfo.find(strFilter) == std::string::npos && strOutpoint.find(strFilter) == std::string::npos) return; UniValue objMN(UniValue::VOBJ); objMN.pushKV("proTxHash", dmn.proTxHash.ToString()); @@ -630,28 +625,28 @@ static RPCHelpMan masternodelist_helper(bool is_composite) objMN.pushKV("pubkeyoperator", dmn.pdmnState->pubKeyOperator.ToString()); obj.pushKV(strOutpoint, objMN); } else if (strMode == "lastpaidblock") { - if (strFilter !="" && strOutpoint.find(strFilter) == std::string::npos) return; + if (!strFilter.empty() && strOutpoint.find(strFilter) == std::string::npos) return; obj.pushKV(strOutpoint, dmn.pdmnState->nLastPaidHeight); } else if (strMode == "lastpaidtime") { - if (strFilter !="" && strOutpoint.find(strFilter) == std::string::npos) return; + if (!strFilter.empty() && strOutpoint.find(strFilter) == std::string::npos) return; obj.pushKV(strOutpoint, dmnToLastPaidTime(dmn)); } else if (strMode == "payee") { - if (strFilter !="" && payeeStr.find(strFilter) == std::string::npos && + if (!strFilter.empty() && payeeStr.find(strFilter) == std::string::npos && strOutpoint.find(strFilter) == std::string::npos) return; obj.pushKV(strOutpoint, payeeStr); } else if (strMode == "owneraddress") { - if (strFilter !="" && strOutpoint.find(strFilter) == std::string::npos) return; + if (!strFilter.empty() && strOutpoint.find(strFilter) == std::string::npos) return; obj.pushKV(strOutpoint, EncodeDestination(PKHash(dmn.pdmnState->keyIDOwner))); } else if (strMode == "pubkeyoperator") { - if (strFilter !="" && strOutpoint.find(strFilter) == std::string::npos) return; + if (!strFilter.empty() && strOutpoint.find(strFilter) == std::string::npos) return; obj.pushKV(strOutpoint, dmn.pdmnState->pubKeyOperator.ToString()); } else if (strMode == "status") { std::string strStatus = dmnToStatus(dmn); - if (strFilter !="" && strStatus.find(strFilter) == std::string::npos && + if (!strFilter.empty() && strStatus.find(strFilter) == std::string::npos && strOutpoint.find(strFilter) == std::string::npos) return; obj.pushKV(strOutpoint, strStatus); } else if (strMode == "votingaddress") { - if (strFilter !="" && strOutpoint.find(strFilter) == std::string::npos) return; + if (!strFilter.empty() && strOutpoint.find(strFilter) == std::string::npos) return; obj.pushKV(strOutpoint, EncodeDestination(PKHash(dmn.pdmnState->keyIDVoting))); } }); diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp index 9fb41ed7f2fc..b7bfb880da9b 100644 --- a/src/test/util_tests.cpp +++ b/src/test/util_tests.cpp @@ -1828,4 +1828,26 @@ BOOST_AUTO_TEST_CASE(clearshrink_test) } } +BOOST_AUTO_TEST_CASE(padding_test) +{ + /* By default strings will be padded to the left */ + BOOST_CHECK_EQUAL(PadString("example", 8), " example"); + + /* Check padding works on the correct side */ + BOOST_CHECK_EQUAL(PadString("example", 8, /*left=*/true), " example"); + BOOST_CHECK_EQUAL(PadString("example", 8, /*left=*/false), "example "); + + /* Padding lesser than the string size should return the string */ + BOOST_CHECK_EQUAL(PadString("example", 6), "example"); + + /* Padding equal to the string size should return the string */ + BOOST_CHECK_EQUAL(PadString("example", 7), "example"); + + /* Padding an empty string with zero length should return an empty string */ + BOOST_CHECK(PadString("", 0).empty()); + + /* An empty string should be padded if non-zero length specified */ + BOOST_CHECK_EQUAL(PadString("", 1), " "); +} + BOOST_AUTO_TEST_SUITE_END() diff --git a/src/txmempool.cpp b/src/txmempool.cpp index b81ee020d5ba..a011ea8afc31 100644 --- a/src/txmempool.cpp +++ b/src/txmempool.cpp @@ -685,27 +685,28 @@ void CTxMemPool::addUncheckedProTx(indexed_transaction_set::iterator& newit, con { assert(m_dmnman); + const uint256 tx_hash{tx.GetHash()}; if (tx.nType == TRANSACTION_PROVIDER_REGISTER) { auto proTx = *Assert(GetTxPayload(tx)); if (!proTx.collateralOutpoint.hash.IsNull()) { - mapProTxRefs.emplace(tx.GetHash(), proTx.collateralOutpoint.hash); + mapProTxRefs.emplace(tx_hash, proTx.collateralOutpoint.hash); } - mapProTxAddresses.emplace(proTx.addr, tx.GetHash()); - mapProTxPubKeyIDs.emplace(proTx.keyIDOwner, tx.GetHash()); - mapProTxBlsPubKeyHashes.emplace(proTx.pubKeyOperator.GetHash(), tx.GetHash()); + mapProTxAddresses.emplace(proTx.addr, tx_hash); + mapProTxPubKeyIDs.emplace(proTx.keyIDOwner, tx_hash); + mapProTxBlsPubKeyHashes.emplace(proTx.pubKeyOperator.GetHash(), tx_hash); if (!proTx.collateralOutpoint.hash.IsNull()) { - mapProTxCollaterals.emplace(proTx.collateralOutpoint, tx.GetHash()); + mapProTxCollaterals.emplace(proTx.collateralOutpoint, tx_hash); } else { - mapProTxCollaterals.emplace(COutPoint(tx.GetHash(), proTx.collateralOutpoint.n), tx.GetHash()); + mapProTxCollaterals.emplace(COutPoint(tx_hash, proTx.collateralOutpoint.n), tx_hash); } } else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_SERVICE) { auto proTx = *Assert(GetTxPayload(tx)); - mapProTxRefs.emplace(proTx.proTxHash, tx.GetHash()); - mapProTxAddresses.emplace(proTx.addr, tx.GetHash()); + mapProTxRefs.emplace(proTx.proTxHash, tx_hash); + mapProTxAddresses.emplace(proTx.addr, tx_hash); } else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_REGISTRAR) { auto proTx = *Assert(GetTxPayload(tx)); - mapProTxRefs.emplace(proTx.proTxHash, tx.GetHash()); - mapProTxBlsPubKeyHashes.emplace(proTx.pubKeyOperator.GetHash(), tx.GetHash()); + mapProTxRefs.emplace(proTx.proTxHash, tx_hash); + mapProTxBlsPubKeyHashes.emplace(proTx.pubKeyOperator.GetHash(), tx_hash); auto dmn = Assert(m_dmnman->GetListAtChainTip().GetMN(proTx.proTxHash)); newit->validForProTxKey = ::SerializeHash(dmn->pdmnState->pubKeyOperator); if (dmn->pdmnState->pubKeyOperator != proTx.pubKeyOperator) { @@ -713,7 +714,7 @@ void CTxMemPool::addUncheckedProTx(indexed_transaction_set::iterator& newit, con } } else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_REVOKE) { auto proTx = *Assert(GetTxPayload(tx)); - mapProTxRefs.emplace(proTx.proTxHash, tx.GetHash()); + mapProTxRefs.emplace(proTx.proTxHash, tx_hash); auto dmn = Assert(m_dmnman->GetListAtChainTip().GetMN(proTx.proTxHash)); newit->validForProTxKey = ::SerializeHash(dmn->pdmnState->pubKeyOperator); if (dmn->pdmnState->pubKeyOperator.Get() != CBLSPublicKey()) { @@ -721,9 +722,9 @@ void CTxMemPool::addUncheckedProTx(indexed_transaction_set::iterator& newit, con } } else if (tx.nType == TRANSACTION_ASSET_UNLOCK) { auto assetUnlockTx = *Assert(GetTxPayload(tx)); - mapAssetUnlockExpiry.insert({tx.GetHash(), assetUnlockTx.getHeightToExpiry()}); + mapAssetUnlockExpiry.insert({tx_hash, assetUnlockTx.getHeightToExpiry()}); } else if (tx.nType == TRANSACTION_MNHF_SIGNAL) { - PrioritiseTransaction(tx.GetHash(), 0.1 * COIN); + PrioritiseTransaction(tx_hash, 0.1 * COIN); } } @@ -784,29 +785,30 @@ void CTxMemPool::removeUncheckedProTx(const CTransaction& tx) } }; + const uint256 tx_hash{tx.GetHash()}; if (tx.nType == TRANSACTION_PROVIDER_REGISTER) { auto proTx = *Assert(GetTxPayload(tx)); if (!proTx.collateralOutpoint.IsNull()) { - eraseProTxRef(tx.GetHash(), proTx.collateralOutpoint.hash); + eraseProTxRef(tx_hash, proTx.collateralOutpoint.hash); } mapProTxAddresses.erase(proTx.addr); mapProTxPubKeyIDs.erase(proTx.keyIDOwner); mapProTxBlsPubKeyHashes.erase(proTx.pubKeyOperator.GetHash()); mapProTxCollaterals.erase(proTx.collateralOutpoint); - mapProTxCollaterals.erase(COutPoint(tx.GetHash(), proTx.collateralOutpoint.n)); + mapProTxCollaterals.erase(COutPoint(tx_hash, proTx.collateralOutpoint.n)); } else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_SERVICE) { auto proTx = *Assert(GetTxPayload(tx)); - eraseProTxRef(proTx.proTxHash, tx.GetHash()); + eraseProTxRef(proTx.proTxHash, tx_hash); mapProTxAddresses.erase(proTx.addr); } else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_REGISTRAR) { auto proTx = *Assert(GetTxPayload(tx)); - eraseProTxRef(proTx.proTxHash, tx.GetHash()); + eraseProTxRef(proTx.proTxHash, tx_hash); mapProTxBlsPubKeyHashes.erase(proTx.pubKeyOperator.GetHash()); } else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_REVOKE) { auto proTx = *Assert(GetTxPayload(tx)); - eraseProTxRef(proTx.proTxHash, tx.GetHash()); + eraseProTxRef(proTx.proTxHash, tx_hash); } else if (tx.nType == TRANSACTION_ASSET_UNLOCK) { - mapAssetUnlockExpiry.erase(tx.GetHash()); + mapAssetUnlockExpiry.erase(tx_hash); } } @@ -1015,17 +1017,18 @@ void CTxMemPool::removeProTxConflicts(const CTransaction &tx) { removeProTxSpentCollateralConflicts(tx); + const uint256 tx_hash{tx.GetHash()}; if (tx.nType == TRANSACTION_PROVIDER_REGISTER) { const auto opt_proTx = GetTxPayload(tx); if (!opt_proTx) { - LogPrint(BCLog::MEMPOOL, "%s: ERROR: Invalid transaction payload, tx: %s\n", __func__, tx.GetHash().ToString()); + LogPrint(BCLog::MEMPOOL, "%s: ERROR: Invalid transaction payload, tx: %s\n", __func__, tx_hash.ToString()); return; } auto& proTx = *opt_proTx; if (mapProTxAddresses.count(proTx.addr)) { uint256 conflictHash = mapProTxAddresses[proTx.addr]; - if (conflictHash != tx.GetHash() && mapTx.count(conflictHash)) { + if (conflictHash != tx_hash && mapTx.count(conflictHash)) { removeRecursive(mapTx.find(conflictHash)->GetTx(), MemPoolRemovalReason::CONFLICT); } } @@ -1034,25 +1037,25 @@ void CTxMemPool::removeProTxConflicts(const CTransaction &tx) if (!proTx.collateralOutpoint.hash.IsNull()) { removeProTxCollateralConflicts(tx, proTx.collateralOutpoint); } else { - removeProTxCollateralConflicts(tx, COutPoint(tx.GetHash(), proTx.collateralOutpoint.n)); + removeProTxCollateralConflicts(tx, COutPoint(tx_hash, proTx.collateralOutpoint.n)); } } else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_SERVICE) { const auto opt_proTx = GetTxPayload(tx); if (!opt_proTx) { - LogPrint(BCLog::MEMPOOL, "%s: ERROR: Invalid transaction payload, tx: %s\n", __func__, tx.GetHash().ToString()); + LogPrint(BCLog::MEMPOOL, "%s: ERROR: Invalid transaction payload, tx: %s\n", __func__, tx_hash.ToString()); return; } if (mapProTxAddresses.count(opt_proTx->addr)) { uint256 conflictHash = mapProTxAddresses[opt_proTx->addr]; - if (conflictHash != tx.GetHash() && mapTx.count(conflictHash)) { + if (conflictHash != tx_hash && mapTx.count(conflictHash)) { removeRecursive(mapTx.find(conflictHash)->GetTx(), MemPoolRemovalReason::CONFLICT); } } } else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_REGISTRAR) { const auto opt_proTx = GetTxPayload(tx); if (!opt_proTx) { - LogPrint(BCLog::MEMPOOL, "%s: ERROR: Invalid transaction payload, tx: %s\n", __func__, tx.GetHash().ToString()); + LogPrint(BCLog::MEMPOOL, "%s: ERROR: Invalid transaction payload, tx: %s\n", __func__, tx_hash.ToString()); return; } @@ -1061,7 +1064,7 @@ void CTxMemPool::removeProTxConflicts(const CTransaction &tx) } else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_REVOKE) { const auto opt_proTx = GetTxPayload(tx); if (!opt_proTx) { - LogPrint(BCLog::MEMPOOL, "%s: ERROR: Invalid transaction payload, tx: %s\n", __func__, tx.GetHash().ToString()); + LogPrint(BCLog::MEMPOOL, "%s: ERROR: Invalid transaction payload, tx: %s\n", __func__, tx_hash.ToString()); return; } @@ -1371,10 +1374,11 @@ bool CTxMemPool::existsProviderTxConflict(const CTransaction &tx) const { return false; }; + const uint256 tx_hash{tx.GetHash()}; if (tx.nType == TRANSACTION_PROVIDER_REGISTER) { const auto opt_proTx = GetTxPayload(tx); if (!opt_proTx) { - LogPrint(BCLog::MEMPOOL, "%s: ERROR: Invalid transaction payload, tx: %s\n", __func__, tx.GetHash().ToString()); + LogPrint(BCLog::MEMPOOL, "%s: ERROR: Invalid transaction payload, tx: %s\n", __func__, tx_hash.ToString()); return true; // i.e. can't decode payload == conflict } auto& proTx = *opt_proTx; @@ -1394,7 +1398,7 @@ bool CTxMemPool::existsProviderTxConflict(const CTransaction &tx) const { } else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_SERVICE) { const auto opt_proTx = GetTxPayload(tx); if (!opt_proTx) { - LogPrint(BCLog::MEMPOOL, "%s: ERROR: Invalid transaction payload, tx: %s\n", __func__, tx.GetHash().ToString()); + LogPrint(BCLog::MEMPOOL, "%s: ERROR: Invalid transaction payload, tx: %s\n", __func__, tx_hash.ToString()); return true; // i.e. can't decode payload == conflict } auto it = mapProTxAddresses.find(opt_proTx->addr); @@ -1402,7 +1406,7 @@ bool CTxMemPool::existsProviderTxConflict(const CTransaction &tx) const { } else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_REGISTRAR) { const auto opt_proTx = GetTxPayload(tx); if (!opt_proTx) { - LogPrint(BCLog::MEMPOOL, "%s: ERROR: Invalid transaction payload, tx: %s\n", __func__, tx.GetHash().ToString()); + LogPrint(BCLog::MEMPOOL, "%s: ERROR: Invalid transaction payload, tx: %s\n", __func__, tx_hash.ToString()); return true; // i.e. can't decode payload == conflict } auto& proTx = *opt_proTx; @@ -1425,7 +1429,7 @@ bool CTxMemPool::existsProviderTxConflict(const CTransaction &tx) const { } else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_REVOKE) { const auto opt_proTx = GetTxPayload(tx); if (!opt_proTx) { - LogPrint(BCLog::MEMPOOL, "%s: ERROR: Invalid transaction payload, tx: %s\n", __func__, tx.GetHash().ToString()); + LogPrint(BCLog::MEMPOOL, "%s: ERROR: Invalid transaction payload, tx: %s\n", __func__, tx_hash.ToString()); return true; // i.e. can't decode payload == conflict } auto& proTx = *opt_proTx; diff --git a/src/util/string.h b/src/util/string.h index aee47cff13a1..e87d0a6e02ab 100644 --- a/src/util/string.h +++ b/src/util/string.h @@ -57,6 +57,12 @@ void ReplaceAll(std::string& in_out, const std::string& search, const std::strin return std::string(RemovePrefixView(str, prefix)); } +[[nodiscard]] inline std::string PadString(std::string str, std::string::size_type size, bool left = true) +{ + if (size <= str.size()) return str; + return left ? std::string(size - str.size(), ' ').append(str) : str.append(std::string(size - str.size(), ' ')); +} + /** * Join all container items. Typically used to concatenate strings but accepts * containers with elements of any type.