diff --git a/src/coinjoin/client.cpp b/src/coinjoin/client.cpp index a7e76fd10c100..e83954dc9f33c 100644 --- a/src/coinjoin/client.cpp +++ b/src/coinjoin/client.cpp @@ -106,10 +106,9 @@ PeerMsgRet CCoinJoinClientQueueManager::ProcessDSQueue(const CNode& peer, CConnm // if the queue is ready, submit if we can if (dsq.fReady && m_walletman.ForAnyCJClientMan([this, &connman, &dmn](std::unique_ptr& clientman) { - return clientman->TrySubmitDenominate(dmn->pdmnState->addr, connman); + return clientman->TrySubmitDenominate(dmn->proTxHash, connman); })) { - LogPrint(BCLog::COINJOIN, "DSQUEUE -- CoinJoin queue (%s) is ready on masternode %s\n", dsq.ToString(), - dmn->pdmnState->addr.ToStringAddrPort()); + LogPrint(BCLog::COINJOIN, "DSQUEUE -- CoinJoin queue is ready, masternode=%s, queue=%s\n", dmn->proTxHash.ToString(), dsq.ToString()); return {}; } else { int64_t nLastDsq = m_mn_metaman.GetMetaInfo(dmn->proTxHash)->GetLastDsq(); @@ -125,8 +124,7 @@ PeerMsgRet CCoinJoinClientQueueManager::ProcessDSQueue(const CNode& peer, CConnm m_mn_metaman.AllowMixing(dmn->proTxHash); - LogPrint(BCLog::COINJOIN, "DSQUEUE -- new CoinJoin queue (%s) from masternode %s\n", dsq.ToString(), - dmn->pdmnState->addr.ToStringAddrPort()); + LogPrint(BCLog::COINJOIN, "DSQUEUE -- new CoinJoin queue, masternode=%s, queue=%s\n", dmn->proTxHash.ToString(), dsq.ToString()); m_walletman.ForAnyCJClientMan([&dsq](const std::unique_ptr& clientman) { return clientman->MarkAlreadyJoinedQueueAsTried(dsq); @@ -186,23 +184,15 @@ void CCoinJoinClientSession::ProcessMessage(CNode& peer, CChainState& active_cha if (!CCoinJoinClientOptions::IsEnabled()) return; if (!m_mn_sync.IsBlockchainSynced()) return; - if (msg_type == NetMsgType::DSSTATUSUPDATE) { - if (!mixingMasternode) return; - if (mixingMasternode->pdmnState->addr != peer.addr) { - return; - } + if (!mixingMasternode) return; + if (mixingMasternode->pdmnState->addr != peer.addr) return; + if (msg_type == NetMsgType::DSSTATUSUPDATE) { CCoinJoinStatusUpdate psssup; vRecv >> psssup; ProcessPoolStateUpdate(psssup); - } else if (msg_type == NetMsgType::DSFINALTX) { - if (!mixingMasternode) return; - if (mixingMasternode->pdmnState->addr != peer.addr) { - return; - } - int nMsgSessionID; vRecv >> nMsgSessionID; CTransaction txNew(deserialize, vRecv); @@ -216,15 +206,7 @@ void CCoinJoinClientSession::ProcessMessage(CNode& peer, CChainState& active_cha // check to see if input is spent already? (and probably not confirmed) SignFinalTransaction(peer, active_chainstate, connman, mempool, txNew); - } else if (msg_type == NetMsgType::DSCOMPLETE) { - if (!mixingMasternode) return; - if (mixingMasternode->pdmnState->addr != peer.addr) { - WalletCJLogPrint(m_wallet, "DSCOMPLETE -- message doesn't match current Masternode: infoMixingMasternode=%s addr=%s\n", - mixingMasternode->pdmnState->addr.ToStringAddrPort(), peer.addr.ToStringAddrPort()); - return; - } - int nMsgSessionID; PoolMessage nMsgMessageID; vRecv >> nMsgSessionID >> nMsgMessageID; @@ -1126,22 +1108,19 @@ bool CCoinJoinClientSession::JoinExistingQueue(CAmount nBalanceNeedsAnonymized, if (connman.IsMasternodeOrDisconnectRequested(dmn->pdmnState->addr)) { WalletCJLogPrint(m_wallet, /* Continued */ - "CCoinJoinClientSession::JoinExistingQueue -- skipping masternode connection, addr=%s\n", - dmn->pdmnState->addr.ToStringAddrPort()); + "CCoinJoinClientSession::JoinExistingQueue -- skipping connection, masternode=%s\n", dmn->proTxHash.ToString()); continue; } nSessionDenom = dsq.nDenom; mixingMasternode = dmn; - pendingDsaRequest = CPendingDsaRequest(dmn->pdmnState->addr, CCoinJoinAccept(nSessionDenom, txMyCollateral)); + pendingDsaRequest = CPendingDsaRequest(dmn->proTxHash, CCoinJoinAccept(nSessionDenom, txMyCollateral)); connman.AddPendingMasternode(dmn->proTxHash); SetState(POOL_STATE_QUEUE); nTimeLastSuccessfulStep = GetTime(); WalletCJLogPrint(m_wallet, /* Continued */ - "CCoinJoinClientSession::JoinExistingQueue -- pending connection (from queue): nSessionDenom: " - "%d (%s), addr=%s\n", - nSessionDenom, CoinJoin::DenominationToString(nSessionDenom), - dmn->pdmnState->addr.ToStringAddrPort()); + "CCoinJoinClientSession::JoinExistingQueue -- pending connection, masternode=%s, nSessionDenom=%d (%s)\n", + dmn->proTxHash.ToString(), nSessionDenom, CoinJoin::DenominationToString(nSessionDenom)); strAutoDenomResult = _("Trying to connect…"); return true; } @@ -1192,23 +1171,22 @@ bool CCoinJoinClientSession::StartNewQueue(CAmount nBalanceNeedsAnonymized, CCon int64_t nDsqThreshold = m_mn_metaman.GetDsqThreshold(dmn->proTxHash, nMnCount); if (nLastDsq != 0 && nDsqThreshold > m_mn_metaman.GetDsqCount()) { WalletCJLogPrint(m_wallet, /* Continued */ - "CCoinJoinClientSession::StartNewQueue -- Too early to mix on this masternode!" /* Continued */ - " masternode=%s addr=%s nLastDsq=%d nDsqThreshold=%d nDsqCount=%d\n", - dmn->proTxHash.ToString(), dmn->pdmnState->addr.ToStringAddrPort(), nLastDsq, - nDsqThreshold, m_mn_metaman.GetDsqCount()); + "CCoinJoinClientSession::StartNewQueue -- too early to mix with node," /* Continued */ + " masternode=%s, nLastDsq=%d, nDsqThreshold=%d, nDsqCount=%d\n", + dmn->proTxHash.ToString(), nLastDsq, nDsqThreshold, m_mn_metaman.GetDsqCount()); nTries++; continue; } if (connman.IsMasternodeOrDisconnectRequested(dmn->pdmnState->addr)) { - WalletCJLogPrint(m_wallet, "CCoinJoinClientSession::StartNewQueue -- skipping masternode connection, addr=%s\n", - dmn->pdmnState->addr.ToStringAddrPort()); + WalletCJLogPrint(m_wallet, "CCoinJoinClientSession::StartNewQueue -- skipping connection, masternode=%s\n", + dmn->proTxHash.ToString()); nTries++; continue; } - WalletCJLogPrint(m_wallet, "CCoinJoinClientSession::StartNewQueue -- attempt %d connection to Masternode %s\n", - nTries, dmn->pdmnState->addr.ToStringAddrPort()); + WalletCJLogPrint(m_wallet, "CCoinJoinClientSession::StartNewQueue -- attempting connection, masternode=%s, tries=%s\n", + dmn->proTxHash.ToString(), nTries); // try to get a single random denom out of setAmounts while (nSessionDenom == 0) { @@ -1221,12 +1199,12 @@ bool CCoinJoinClientSession::StartNewQueue(CAmount nBalanceNeedsAnonymized, CCon mixingMasternode = dmn; connman.AddPendingMasternode(dmn->proTxHash); - pendingDsaRequest = CPendingDsaRequest(dmn->pdmnState->addr, CCoinJoinAccept(nSessionDenom, txMyCollateral)); + pendingDsaRequest = CPendingDsaRequest(dmn->proTxHash, CCoinJoinAccept(nSessionDenom, txMyCollateral)); SetState(POOL_STATE_QUEUE); nTimeLastSuccessfulStep = GetTime(); WalletCJLogPrint( /* Continued */ - m_wallet, "CCoinJoinClientSession::StartNewQueue -- pending connection, nSessionDenom: %d (%s), addr=%s\n", - nSessionDenom, CoinJoin::DenominationToString(nSessionDenom), dmn->pdmnState->addr.ToStringAddrPort()); + m_wallet, "CCoinJoinClientSession::StartNewQueue -- pending connection, masternode=%s, nSessionDenom=%d (%s)\n", + dmn->proTxHash.ToString(), nSessionDenom, CoinJoin::DenominationToString(nSessionDenom)); strAutoDenomResult = _("Trying to connect…"); return true; } @@ -1238,7 +1216,17 @@ bool CCoinJoinClientSession::ProcessPendingDsaRequest(CConnman& connman) { if (!pendingDsaRequest) return false; - bool fDone = connman.ForNode(pendingDsaRequest.GetAddr(), [this, &connman](CNode* pnode) { + CService mn_addr; + if (auto dmn = m_dmnman.GetListAtChainTip().GetMN(pendingDsaRequest.GetProTxHash())) { + mn_addr = Assert(dmn->pdmnState)->addr; + } else { + WalletCJLogPrint(m_wallet, "CCoinJoinClientSession::%s -- cannot find address to connect, masternode=%s\n", __func__, + pendingDsaRequest.GetProTxHash().ToString()); + WITH_LOCK(cs_coinjoin, SetNull()); + return false; + } + + bool fDone = connman.ForNode(mn_addr, [this, &connman](CNode* pnode) { WalletCJLogPrint(m_wallet, "-- processing dsa queue for addr=%s\n", pnode->addr.ToStringAddrPort()); nTimeLastSuccessfulStep = GetTime(); CNetMsgMaker msgMaker(pnode->GetCommonVersion()); @@ -1249,8 +1237,8 @@ bool CCoinJoinClientSession::ProcessPendingDsaRequest(CConnman& connman) if (fDone) { pendingDsaRequest = CPendingDsaRequest(); } else if (pendingDsaRequest.IsExpired()) { - WalletCJLogPrint(m_wallet, "CCoinJoinClientSession::%s -- failed to connect to %s\n", __func__, - pendingDsaRequest.GetAddr().ToStringAddrPort()); + WalletCJLogPrint(m_wallet, "CCoinJoinClientSession::%s -- failed to connect, masternode=%s\n", __func__, + pendingDsaRequest.GetProTxHash().ToString()); WITH_LOCK(cs_coinjoin, SetNull()); } @@ -1268,13 +1256,13 @@ void CCoinJoinClientManager::ProcessPendingDsaRequest(CConnman& connman) } } -bool CCoinJoinClientManager::TrySubmitDenominate(const CService& mnAddr, CConnman& connman) +bool CCoinJoinClientManager::TrySubmitDenominate(const uint256& proTxHash, CConnman& connman) { AssertLockNotHeld(cs_deqsessions); LOCK(cs_deqsessions); for (auto& session : deqSessions) { CDeterministicMNCPtr mnMixing; - if (session.GetMixingMasternodeInfo(mnMixing) && mnMixing->pdmnState->addr == mnAddr && session.GetState() == POOL_STATE_QUEUE) { + if (session.GetMixingMasternodeInfo(mnMixing) && mnMixing->proTxHash == proTxHash && session.GetState() == POOL_STATE_QUEUE) { session.SubmitDenominate(connman); return true; } diff --git a/src/coinjoin/client.h b/src/coinjoin/client.h index 6c7e3205ee3b0..26fd65008ba95 100644 --- a/src/coinjoin/client.h +++ b/src/coinjoin/client.h @@ -39,27 +39,27 @@ class CPendingDsaRequest private: static constexpr int TIMEOUT = 15; - CService addr; + uint256 proTxHash; CCoinJoinAccept dsa; int64_t nTimeCreated{0}; public: CPendingDsaRequest() = default; - CPendingDsaRequest(CService addr_, CCoinJoinAccept dsa_) : - addr(std::move(addr_)), + CPendingDsaRequest(uint256 proTxHash_, CCoinJoinAccept dsa_) : + proTxHash(std::move(proTxHash_)), dsa(std::move(dsa_)), nTimeCreated(GetTime()) { } - [[nodiscard]] CService GetAddr() const { return addr; } + [[nodiscard]] uint256 GetProTxHash() const { return proTxHash; } [[nodiscard]] CCoinJoinAccept GetDSA() const { return dsa; } [[nodiscard]] bool IsExpired() const { return GetTime() - nTimeCreated > TIMEOUT; } friend bool operator==(const CPendingDsaRequest& a, const CPendingDsaRequest& b) { - return a.addr == b.addr && a.dsa == b.dsa; + return a.proTxHash == b.proTxHash && a.dsa == b.dsa; } friend bool operator!=(const CPendingDsaRequest& a, const CPendingDsaRequest& b) { @@ -336,7 +336,7 @@ class CCoinJoinClientManager bool DoAutomaticDenominating(ChainstateManager& chainman, CConnman& connman, const CTxMemPool& mempool, bool fDryRun = false) EXCLUSIVE_LOCKS_REQUIRED(!cs_deqsessions); - bool TrySubmitDenominate(const CService& mnAddr, CConnman& connman) EXCLUSIVE_LOCKS_REQUIRED(!cs_deqsessions); + bool TrySubmitDenominate(const uint256& proTxHash, CConnman& connman) EXCLUSIVE_LOCKS_REQUIRED(!cs_deqsessions); bool MarkAlreadyJoinedQueueAsTried(CCoinJoinQueue& dsq) const EXCLUSIVE_LOCKS_REQUIRED(!cs_deqsessions); void CheckTimeout() EXCLUSIVE_LOCKS_REQUIRED(!cs_deqsessions); diff --git a/src/coinjoin/server.cpp b/src/coinjoin/server.cpp index 181060d77c204..1a329606ec05a 100644 --- a/src/coinjoin/server.cpp +++ b/src/coinjoin/server.cpp @@ -174,14 +174,12 @@ PeerMsgRet CCoinJoinServer::ProcessDSQUEUE(const CNode& peer, CDataStream& vRecv LogPrint(BCLog::COINJOIN, "DSQUEUE -- nLastDsq: %d nDsqThreshold: %d nDsqCount: %d\n", nLastDsq, nDsqThreshold, m_mn_metaman.GetDsqCount()); //don't allow a few nodes to dominate the queuing process if (nLastDsq != 0 && nDsqThreshold > m_mn_metaman.GetDsqCount()) { - LogPrint(BCLog::COINJOIN, "DSQUEUE -- Masternode %s is sending too many dsq messages\n", - dmn->pdmnState->addr.ToStringAddrPort()); + LogPrint(BCLog::COINJOIN, "DSQUEUE -- node sending too many dsq messages, masternode=%s\n", dmn->proTxHash.ToString()); return {}; } m_mn_metaman.AllowMixing(dmn->proTxHash); - LogPrint(BCLog::COINJOIN, "DSQUEUE -- new CoinJoin queue (%s) from masternode %s\n", dsq.ToString(), - dmn->pdmnState->addr.ToStringAddrPort()); + LogPrint(BCLog::COINJOIN, "DSQUEUE -- new CoinJoin queue, masternode=%s, queue=%s\n", dmn->proTxHash.ToString(), dsq.ToString()); TRY_LOCK(cs_vecqueue, lockRecv); if (!lockRecv) return {}; diff --git a/src/evo/dmnstate.h b/src/evo/dmnstate.h index bd8c1e28050f6..e3c9fcaad5f78 100644 --- a/src/evo/dmnstate.h +++ b/src/evo/dmnstate.h @@ -33,7 +33,7 @@ class CDeterministicMNState friend class CDeterministicMNStateDiff; public: - int nVersion{CProRegTx::LEGACY_BLS_VERSION}; + int nVersion{ProTxVersion::LegacyBLS}; int nRegisteredHeight{-1}; int nLastPaidHeight{0}; @@ -94,7 +94,7 @@ class CDeterministicMNState obj.confirmedHash, obj.confirmedHashWithProRegTxHash, obj.keyIDOwner); - READWRITE(CBLSLazyPublicKeyVersionWrapper(const_cast(obj.pubKeyOperator), obj.nVersion == CProRegTx::LEGACY_BLS_VERSION)); + READWRITE(CBLSLazyPublicKeyVersionWrapper(const_cast(obj.pubKeyOperator), obj.nVersion == ProTxVersion::LegacyBLS)); READWRITE( obj.keyIDVoting, obj.addr, @@ -107,7 +107,7 @@ class CDeterministicMNState void ResetOperatorFields() { - nVersion = CProRegTx::LEGACY_BLS_VERSION; + nVersion = ProTxVersion::LegacyBLS; pubKeyOperator = CBLSLazyPublicKey(); addr = CService(); scriptOperatorPayout = CScript(); @@ -219,14 +219,14 @@ class CDeterministicMNStateDiff #define DMN_STATE_DIFF_LINE(f) \ if (strcmp(#f, "pubKeyOperator") == 0 && (obj.fields & Field_pubKeyOperator)) {\ SER_READ(obj, read_pubkey = true); \ - READWRITE(CBLSLazyPublicKeyVersionWrapper(const_cast(obj.state.pubKeyOperator), obj.state.nVersion == CProRegTx::LEGACY_BLS_VERSION)); \ + READWRITE(CBLSLazyPublicKeyVersionWrapper(const_cast(obj.state.pubKeyOperator), obj.state.nVersion == ProTxVersion::LegacyBLS)); \ } else if (obj.fields & Field_##f) READWRITE(obj.state.f); DMN_STATE_DIFF_ALL_FIELDS #undef DMN_STATE_DIFF_LINE if (read_pubkey) { SER_READ(obj, obj.fields |= Field_nVersion); - SER_READ(obj, obj.state.pubKeyOperator.SetLegacy(obj.state.nVersion == CProRegTx::LEGACY_BLS_VERSION)); + SER_READ(obj, obj.state.pubKeyOperator.SetLegacy(obj.state.nVersion == ProTxVersion::LegacyBLS)); } } diff --git a/src/evo/providertx.cpp b/src/evo/providertx.cpp index 69193fe3c07a2..4763bf04dd7a9 100644 --- a/src/evo/providertx.cpp +++ b/src/evo/providertx.cpp @@ -14,10 +14,10 @@ bool CProRegTx::IsTriviallyValid(bool is_basic_scheme_active, TxValidationState& state) const { - if (nVersion == 0 || nVersion > GetVersion(is_basic_scheme_active)) { + if (nVersion == 0 || nVersion > GetMaxVersion(is_basic_scheme_active)) { return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-protx-version"); } - if (nVersion != BASIC_BLS_VERSION && nType == MnType::Evo) { + if (nVersion < ProTxVersion::BasicBLS && nType == MnType::Evo) { return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-protx-evo-version"); } if (!IsValidMnType(nType)) { @@ -30,7 +30,7 @@ bool CProRegTx::IsTriviallyValid(bool is_basic_scheme_active, TxValidationState& if (keyIDOwner.IsNull() || !pubKeyOperator.Get().IsValid() || keyIDVoting.IsNull()) { return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-protx-key-null"); } - if (pubKeyOperator.IsLegacy() != (nVersion == LEGACY_BLS_VERSION)) { + if (pubKeyOperator.IsLegacy() != (nVersion == ProTxVersion::LegacyBLS)) { return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-protx-operator-pubkey"); } if (!scriptPayout.IsPayToPublicKeyHash() && !scriptPayout.IsPayToScriptHash()) { @@ -98,10 +98,10 @@ std::string CProRegTx::ToString() const bool CProUpServTx::IsTriviallyValid(bool is_basic_scheme_active, TxValidationState& state) const { - if (nVersion == 0 || nVersion > GetVersion(is_basic_scheme_active)) { + if (nVersion == 0 || nVersion > GetMaxVersion(is_basic_scheme_active)) { return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-protx-version"); } - if (nVersion != BASIC_BLS_VERSION && nType == MnType::Evo) { + if (nVersion < ProTxVersion::BasicBLS && nType == MnType::Evo) { return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-protx-evo-version"); } @@ -124,7 +124,7 @@ std::string CProUpServTx::ToString() const bool CProUpRegTx::IsTriviallyValid(bool is_basic_scheme_active, TxValidationState& state) const { - if (nVersion == 0 || nVersion > GetVersion(is_basic_scheme_active)) { + if (nVersion == 0 || nVersion > GetMaxVersion(is_basic_scheme_active)) { return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-protx-version"); } if (nMode != 0) { @@ -134,7 +134,7 @@ bool CProUpRegTx::IsTriviallyValid(bool is_basic_scheme_active, TxValidationStat if (!pubKeyOperator.Get().IsValid() || keyIDVoting.IsNull()) { return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-protx-key-null"); } - if (pubKeyOperator.IsLegacy() != (nVersion == LEGACY_BLS_VERSION)) { + if (pubKeyOperator.IsLegacy() != (nVersion == ProTxVersion::LegacyBLS)) { return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-protx-operator-pubkey"); } if (!scriptPayout.IsPayToPublicKeyHash() && !scriptPayout.IsPayToScriptHash()) { @@ -157,7 +157,7 @@ std::string CProUpRegTx::ToString() const bool CProUpRevTx::IsTriviallyValid(bool is_basic_scheme_active, TxValidationState& state) const { - if (nVersion == 0 || nVersion > GetVersion(is_basic_scheme_active)) { + if (nVersion == 0 || nVersion > GetMaxVersion(is_basic_scheme_active)) { return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-protx-version"); } diff --git a/src/evo/providertx.h b/src/evo/providertx.h index 73ec27ba61f68..037dc3c24bc9f 100644 --- a/src/evo/providertx.h +++ b/src/evo/providertx.h @@ -19,19 +19,24 @@ class TxValidationState; +namespace ProTxVersion { +enum : uint16_t { + LegacyBLS = 1, + BasicBLS = 2, +}; +} // namespace ProTxVersion + class CProRegTx { public: static constexpr auto SPECIALTX_TYPE = TRANSACTION_PROVIDER_REGISTER; - static constexpr uint16_t LEGACY_BLS_VERSION = 1; - static constexpr uint16_t BASIC_BLS_VERSION = 2; - [[nodiscard]] static constexpr auto GetVersion(const bool is_basic_scheme_active) -> uint16_t + [[nodiscard]] static constexpr uint16_t GetMaxVersion(const bool is_basic_scheme_active) { - return is_basic_scheme_active ? BASIC_BLS_VERSION : LEGACY_BLS_VERSION; + return is_basic_scheme_active ? ProTxVersion::BasicBLS : ProTxVersion::LegacyBLS; } - uint16_t nVersion{LEGACY_BLS_VERSION}; // message version + uint16_t nVersion{ProTxVersion::LegacyBLS}; // message version MnType nType{MnType::Regular}; uint16_t nMode{0}; // only 0 supported for now COutPoint collateralOutpoint{uint256(), (uint32_t)-1}; // if hash is null, we refer to a ProRegTx output @@ -52,7 +57,7 @@ class CProRegTx READWRITE( obj.nVersion ); - if (obj.nVersion == 0 || obj.nVersion > BASIC_BLS_VERSION) { + if (obj.nVersion == 0 || obj.nVersion > GetMaxVersion(/*is_basic_scheme_active=*/true)) { // unknown version, bail out early return; } @@ -63,7 +68,7 @@ class CProRegTx obj.collateralOutpoint, obj.addr, obj.keyIDOwner, - CBLSLazyPublicKeyVersionWrapper(const_cast(obj.pubKeyOperator), (obj.nVersion == LEGACY_BLS_VERSION)), + CBLSLazyPublicKeyVersionWrapper(const_cast(obj.pubKeyOperator), (obj.nVersion == ProTxVersion::LegacyBLS)), obj.keyIDVoting, obj.nOperatorReward, obj.scriptPayout, @@ -119,15 +124,13 @@ class CProUpServTx { public: static constexpr auto SPECIALTX_TYPE = TRANSACTION_PROVIDER_UPDATE_SERVICE; - static constexpr uint16_t LEGACY_BLS_VERSION = 1; - static constexpr uint16_t BASIC_BLS_VERSION = 2; - [[nodiscard]] static constexpr auto GetVersion(const bool is_basic_scheme_active) -> uint16_t + [[nodiscard]] static constexpr uint16_t GetMaxVersion(const bool is_basic_scheme_active) { - return is_basic_scheme_active ? BASIC_BLS_VERSION : LEGACY_BLS_VERSION; + return is_basic_scheme_active ? ProTxVersion::BasicBLS : ProTxVersion::LegacyBLS; } - uint16_t nVersion{LEGACY_BLS_VERSION}; // message version + uint16_t nVersion{ProTxVersion::LegacyBLS}; // message version MnType nType{MnType::Regular}; uint256 proTxHash; CService addr; @@ -143,11 +146,11 @@ class CProUpServTx READWRITE( obj.nVersion ); - if (obj.nVersion == 0 || obj.nVersion > BASIC_BLS_VERSION) { + if (obj.nVersion == 0 || obj.nVersion > GetMaxVersion(/*is_basic_scheme_active=*/true)) { // unknown version, bail out early return; } - if (obj.nVersion == BASIC_BLS_VERSION) { + if (obj.nVersion >= ProTxVersion::BasicBLS) { READWRITE( obj.nType); } @@ -165,7 +168,7 @@ class CProUpServTx } if (!(s.GetType() & SER_GETHASH)) { READWRITE( - CBLSSignatureVersionWrapper(const_cast(obj.sig), (obj.nVersion == LEGACY_BLS_VERSION)) + CBLSSignatureVersionWrapper(const_cast(obj.sig), (obj.nVersion == ProTxVersion::LegacyBLS)) ); } } @@ -199,15 +202,13 @@ class CProUpRegTx { public: static constexpr auto SPECIALTX_TYPE = TRANSACTION_PROVIDER_UPDATE_REGISTRAR; - static constexpr uint16_t LEGACY_BLS_VERSION = 1; - static constexpr uint16_t BASIC_BLS_VERSION = 2; - [[nodiscard]] static constexpr auto GetVersion(const bool is_basic_scheme_active) -> uint16_t + [[nodiscard]] static constexpr uint16_t GetMaxVersion(const bool is_basic_scheme_active) { - return is_basic_scheme_active ? BASIC_BLS_VERSION : LEGACY_BLS_VERSION; + return is_basic_scheme_active ? ProTxVersion::BasicBLS : ProTxVersion::LegacyBLS; } - uint16_t nVersion{LEGACY_BLS_VERSION}; // message version + uint16_t nVersion{ProTxVersion::LegacyBLS}; // message version uint256 proTxHash; uint16_t nMode{0}; // only 0 supported for now CBLSLazyPublicKey pubKeyOperator; @@ -221,14 +222,14 @@ class CProUpRegTx READWRITE( obj.nVersion ); - if (obj.nVersion == 0 || obj.nVersion > BASIC_BLS_VERSION) { + if (obj.nVersion == 0 || obj.nVersion > GetMaxVersion(/*is_basic_scheme_active=*/true)) { // unknown version, bail out early return; } READWRITE( obj.proTxHash, obj.nMode, - CBLSLazyPublicKeyVersionWrapper(const_cast(obj.pubKeyOperator), (obj.nVersion == LEGACY_BLS_VERSION)), + CBLSLazyPublicKeyVersionWrapper(const_cast(obj.pubKeyOperator), (obj.nVersion == ProTxVersion::LegacyBLS)), obj.keyIDVoting, obj.scriptPayout, obj.inputsHash @@ -264,12 +265,10 @@ class CProUpRevTx { public: static constexpr auto SPECIALTX_TYPE = TRANSACTION_PROVIDER_UPDATE_REVOKE; - static constexpr uint16_t LEGACY_BLS_VERSION = 1; - static constexpr uint16_t BASIC_BLS_VERSION = 2; - [[nodiscard]] static constexpr auto GetVersion(const bool is_basic_scheme_active) -> uint16_t + [[nodiscard]] static constexpr uint16_t GetMaxVersion(const bool is_basic_scheme_active) { - return is_basic_scheme_active ? BASIC_BLS_VERSION : LEGACY_BLS_VERSION; + return is_basic_scheme_active ? ProTxVersion::BasicBLS : ProTxVersion::LegacyBLS; } // these are just informational and do not have any effect on the revocation @@ -281,7 +280,7 @@ class CProUpRevTx REASON_LAST = REASON_CHANGE_OF_KEYS }; - uint16_t nVersion{LEGACY_BLS_VERSION}; // message version + uint16_t nVersion{ProTxVersion::LegacyBLS}; // message version uint256 proTxHash; uint16_t nReason{REASON_NOT_SPECIFIED}; uint256 inputsHash; // replay protection @@ -292,7 +291,7 @@ class CProUpRevTx READWRITE( obj.nVersion ); - if (obj.nVersion == 0 || obj.nVersion > BASIC_BLS_VERSION) { + if (obj.nVersion == 0 || obj.nVersion > GetMaxVersion(/*is_basic_scheme_active=*/true)) { // unknown version, bail out early return; } @@ -303,7 +302,7 @@ class CProUpRevTx ); if (!(s.GetType() & SER_GETHASH)) { READWRITE( - CBLSSignatureVersionWrapper(const_cast(obj.sig), (obj.nVersion == LEGACY_BLS_VERSION)) + CBLSSignatureVersionWrapper(const_cast(obj.sig), (obj.nVersion == ProTxVersion::LegacyBLS)) ); } } diff --git a/src/evo/simplifiedmns.cpp b/src/evo/simplifiedmns.cpp index c1acc31ea7c87..58c0fc0824c12 100644 --- a/src/evo/simplifiedmns.cpp +++ b/src/evo/simplifiedmns.cpp @@ -38,7 +38,7 @@ CSimplifiedMNListEntry::CSimplifiedMNListEntry(const CDeterministicMN& dmn) : platformNodeID(dmn.pdmnState->platformNodeID), scriptPayout(dmn.pdmnState->scriptPayout), scriptOperatorPayout(dmn.pdmnState->scriptOperatorPayout), - nVersion(dmn.pdmnState->nVersion == CProRegTx::LEGACY_BLS_VERSION ? LEGACY_BLS_VERSION : BASIC_BLS_VERSION), + nVersion(dmn.pdmnState->nVersion), nType(dmn.nType) { } diff --git a/src/evo/simplifiedmns.h b/src/evo/simplifiedmns.h index 1de5effb408c8..0a1fa4c4633e5 100644 --- a/src/evo/simplifiedmns.h +++ b/src/evo/simplifiedmns.h @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -31,9 +32,6 @@ class CQuorumManager; class CSimplifiedMNListEntry { public: - static constexpr uint16_t LEGACY_BLS_VERSION = 1; - static constexpr uint16_t BASIC_BLS_VERSION = 2; - uint256 proRegTxHash; uint256 confirmedHash; CService service; @@ -44,7 +42,7 @@ class CSimplifiedMNListEntry uint160 platformNodeID{}; CScript scriptPayout; // mem-only CScript scriptOperatorPayout; // mem-only - uint16_t nVersion{LEGACY_BLS_VERSION}; + uint16_t nVersion{ProTxVersion::LegacyBLS}; MnType nType{MnType::Regular}; CSimplifiedMNListEntry() = default; @@ -78,14 +76,14 @@ class CSimplifiedMNListEntry obj.proRegTxHash, obj.confirmedHash, obj.service, - CBLSLazyPublicKeyVersionWrapper(const_cast(obj.pubKeyOperator), (obj.nVersion == LEGACY_BLS_VERSION)), + CBLSLazyPublicKeyVersionWrapper(const_cast(obj.pubKeyOperator), (obj.nVersion == ProTxVersion::LegacyBLS)), obj.keyIDVoting, obj.isValid ); if ((s.GetType() & SER_NETWORK) && s.GetVersion() < DMN_TYPE_PROTO_VERSION) { return; } - if (obj.nVersion == BASIC_BLS_VERSION) { + if (obj.nVersion >= ProTxVersion::BasicBLS) { READWRITE(obj.nType); if (obj.nType == MnType::Evo) { READWRITE(obj.platformHTTPPort); diff --git a/src/rpc/evo.cpp b/src/rpc/evo.cpp index 88752728793a4..217c0f39a6c05 100644 --- a/src/rpc/evo.cpp +++ b/src/rpc/evo.cpp @@ -655,6 +655,7 @@ static UniValue protx_register_common_wrapper(const JSONRPCRequest& request, CProRegTx ptx; ptx.nType = mnType; + ptx.nVersion = CProRegTx::GetMaxVersion(/*is_basic_scheme_active=*/!use_legacy); if (action == ProTxRegisterAction::Fund) { CTxDestination collateralDest = DecodeDestination(request.params[paramIdx].get_str()); @@ -689,8 +690,7 @@ static UniValue protx_register_common_wrapper(const JSONRPCRequest& request, ptx.keyIDOwner = ParsePubKeyIDFromAddress(request.params[paramIdx + 1].get_str(), "owner address"); ptx.pubKeyOperator.Set(ParseBLSPubKey(request.params[paramIdx + 2].get_str(), "operator BLS address", use_legacy), use_legacy); - ptx.nVersion = use_legacy ? CProRegTx::LEGACY_BLS_VERSION : CProRegTx::BASIC_BLS_VERSION; - CHECK_NONFATAL(ptx.pubKeyOperator.IsLegacy() == (ptx.nVersion == CProRegTx::LEGACY_BLS_VERSION)); + CHECK_NONFATAL(ptx.pubKeyOperator.IsLegacy() == (ptx.nVersion == ProTxVersion::LegacyBLS)); CKeyID keyIDVoting = ptx.keyIDOwner; @@ -963,8 +963,18 @@ static UniValue protx_update_service_common_wrapper(const JSONRPCRequest& reques EnsureWalletIsUnlocked(*wallet); CProUpServTx ptx; - ptx.nType = mnType; ptx.proTxHash = ParseHashV(request.params[0], "proTxHash"); + auto dmn = dmnman.GetListAtChainTip().GetMN(ptx.proTxHash); + if (!dmn) { + throw std::runtime_error(strprintf("masternode with proTxHash %s not found", ptx.proTxHash.ToString())); + } + + ptx.nType = mnType; + if (dmn->nType != mnType) { + throw std::runtime_error(strprintf("masternode with proTxHash %s is not a %s", ptx.proTxHash.ToString(), GetMnType(mnType).description)); + } + + ptx.nVersion = dmn->pdmnState->nVersion; if (auto addr = Lookup(request.params[1].get_str().c_str(), Params().GetDefaultPort(), false); addr.has_value()) { ptx.addr = addr.value(); @@ -996,15 +1006,6 @@ static UniValue protx_update_service_common_wrapper(const JSONRPCRequest& reques paramIdx += 3; } - auto dmn = dmnman.GetListAtChainTip().GetMN(ptx.proTxHash); - if (!dmn) { - throw std::runtime_error(strprintf("masternode with proTxHash %s not found", ptx.proTxHash.ToString())); - } - if (dmn->nType != mnType) { - throw std::runtime_error(strprintf("masternode with proTxHash %s is not a %s", ptx.proTxHash.ToString(), GetMnType(mnType).description)); - } - ptx.nVersion = dmn->pdmnState->nVersion; - if (keyOperator.GetPublicKey() != dmn->pdmnState->pubKeyOperator.Get()) { throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("the operator key does not belong to the registered public key")); } @@ -1094,12 +1095,14 @@ static RPCHelpMan protx_update_registrar_wrapper(const bool specific_legacy_bls_ EnsureWalletIsUnlocked(*wallet); CProUpRegTx ptx; - ptx.proTxHash = ParseHashV(request.params[0], "proTxHash"); + ptx.nVersion = CProUpRegTx::GetMaxVersion(/*is_basic_scheme_active=*/!use_legacy); + ptx.proTxHash = ParseHashV(request.params[0], "proTxHash"); auto dmn = dmnman.GetListAtChainTip().GetMN(ptx.proTxHash); if (!dmn) { throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("masternode %s not found", ptx.proTxHash.ToString())); } + ptx.keyIDVoting = dmn->pdmnState->keyIDVoting; ptx.scriptPayout = dmn->pdmnState->scriptPayout; @@ -1111,8 +1114,7 @@ static RPCHelpMan protx_update_registrar_wrapper(const bool specific_legacy_bls_ ptx.pubKeyOperator = dmn->pdmnState->pubKeyOperator; } - ptx.nVersion = use_legacy ? CProUpRegTx::LEGACY_BLS_VERSION : CProUpRegTx::BASIC_BLS_VERSION; - CHECK_NONFATAL(ptx.pubKeyOperator.IsLegacy() == (ptx.nVersion == CProUpRegTx::LEGACY_BLS_VERSION)); + CHECK_NONFATAL(ptx.pubKeyOperator.IsLegacy() == (ptx.nVersion == ProTxVersion::LegacyBLS)); if (request.params[2].get_str() != "") { ptx.keyIDVoting = ParsePubKeyIDFromAddress(request.params[2].get_str(), "voting address"); @@ -1204,8 +1206,13 @@ static RPCHelpMan protx_revoke() const bool isV19active{DeploymentActiveAfter(WITH_LOCK(cs_main, return chainman.ActiveChain().Tip();), Params().GetConsensus(), Consensus::DEPLOYMENT_V19)}; CProUpRevTx ptx; - ptx.nVersion = CProUpRevTx::GetVersion(isV19active); + ptx.nVersion = CProUpRevTx::GetMaxVersion(/*is_basic_scheme_active=*/isV19active); + ptx.proTxHash = ParseHashV(request.params[0], "proTxHash"); + auto dmn = dmnman.GetListAtChainTip().GetMN(ptx.proTxHash); + if (!dmn) { + throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("masternode %s not found", ptx.proTxHash.ToString())); + } CBLSSecretKey keyOperator = ParseBLSSecretKey(request.params[1].get_str(), "operatorKey"); @@ -1217,11 +1224,6 @@ static RPCHelpMan protx_revoke() ptx.nReason = (uint16_t)nReason; } - auto dmn = dmnman.GetListAtChainTip().GetMN(ptx.proTxHash); - if (!dmn) { - throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("masternode %s not found", ptx.proTxHash.ToString())); - } - if (keyOperator.GetPublicKey() != dmn->pdmnState->pubKeyOperator.Get()) { throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("the operator key does not belong to the registered public key")); } diff --git a/src/rpc/index_util.h b/src/rpc/index_util.h index 5f92de755f6d8..2624ae4578bde 100644 --- a/src/rpc/index_util.h +++ b/src/rpc/index_util.h @@ -27,7 +27,7 @@ extern RecursiveMutex cs_main; //! throws JSONRPCError if address index is unavailable bool GetAddressIndex(CBlockTreeDB& block_tree_db, const uint160& addressHash, const AddressType type, std::vector& addressIndex, - const int32_t start = 0, const int32_t end = 0) + const int32_t start, const int32_t end) EXCLUSIVE_LOCKS_REQUIRED(::cs_main); //! throws JSONRPCError if address index is unavailable bool GetAddressUnspentIndex(CBlockTreeDB& block_tree_db, const uint160& addressHash, const AddressType type, diff --git a/src/rpc/misc.cpp b/src/rpc/misc.cpp index 89e8cca2e1ff9..80f7c1da6c2a7 100644 --- a/src/rpc/misc.cpp +++ b/src/rpc/misc.cpp @@ -818,18 +818,10 @@ static RPCHelpMan getaddressdeltas() { LOCK(::cs_main); for (const auto& address : addresses) { - if (start > 0 && end > 0) { - if (!GetAddressIndex(*chainman.m_blockman.m_block_tree_db, address.first, address.second, - addressIndex, start, end)) - { - throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); - } - } else { - if (!GetAddressIndex(*chainman.m_blockman.m_block_tree_db, address.first, address.second, - addressIndex)) - { - throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); - } + if (start <= 0 || end <= 0) { start = 0; end = 0; } + if (!GetAddressIndex(*chainman.m_blockman.m_block_tree_db, address.first, address.second, + addressIndex, start, end)) { + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); } } } @@ -896,7 +888,8 @@ static RPCHelpMan getaddressbalance() { LOCK(::cs_main); for (const auto& address : addresses) { - if (!GetAddressIndex(*chainman.m_blockman.m_block_tree_db, address.first, address.second, addressIndex)) { + if (!GetAddressIndex(*chainman.m_blockman.m_block_tree_db, address.first, address.second, addressIndex, + /*start=*/0, /*end=*/0)) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); } } @@ -977,16 +970,10 @@ static RPCHelpMan getaddresstxids() { LOCK(::cs_main); for (const auto& address : addresses) { - if (start > 0 && end > 0) { - if (!GetAddressIndex(*chainman.m_blockman.m_block_tree_db, address.first, address.second, - addressIndex, start, end)) { - throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); - } - } else { - if (!GetAddressIndex(*chainman.m_blockman.m_block_tree_db, address.first, address.second, - addressIndex)) { - throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); - } + if (start <= 0 || end <= 0) { start = 0; end = 0; } + if (!GetAddressIndex(*chainman.m_blockman.m_block_tree_db, address.first, address.second, + addressIndex, start, end)) { + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address"); } } } diff --git a/src/test/block_reward_reallocation_tests.cpp b/src/test/block_reward_reallocation_tests.cpp index 542505afa3e55..b3cb5a95ffcc4 100644 --- a/src/test/block_reward_reallocation_tests.cpp +++ b/src/test/block_reward_reallocation_tests.cpp @@ -114,7 +114,7 @@ static CMutableTransaction CreateProRegTx(const CChain& active_chain, const CTxM operatorKeyRet.MakeNewKey(); CProRegTx proTx; - proTx.nVersion = CProRegTx::GetVersion(!bls::bls_legacy_scheme); + proTx.nVersion = CProRegTx::GetMaxVersion(!bls::bls_legacy_scheme); proTx.collateralOutpoint.n = 0; proTx.addr = LookupNumeric("1.1.1.1", port); proTx.keyIDOwner = ownerKeyRet.GetPubKey().GetID(); diff --git a/src/test/evo_deterministicmns_tests.cpp b/src/test/evo_deterministicmns_tests.cpp index 7b24a84f335ba..567b1114a20cf 100644 --- a/src/test/evo_deterministicmns_tests.cpp +++ b/src/test/evo_deterministicmns_tests.cpp @@ -103,7 +103,7 @@ static CMutableTransaction CreateProRegTx(const CChain& active_chain, const CTxM operatorKeyRet.MakeNewKey(); CProRegTx proTx; - proTx.nVersion = CProRegTx::GetVersion(!bls::bls_legacy_scheme); + proTx.nVersion = CProRegTx::GetMaxVersion(!bls::bls_legacy_scheme); proTx.collateralOutpoint.n = 0; proTx.addr = LookupNumeric("1.1.1.1", port); proTx.keyIDOwner = ownerKeyRet.GetPubKey().GetID(); @@ -125,7 +125,7 @@ static CMutableTransaction CreateProRegTx(const CChain& active_chain, const CTxM static CMutableTransaction CreateProUpServTx(const CChain& active_chain, const CTxMemPool& mempool, SimpleUTXOMap& utxos, const uint256& proTxHash, const CBLSSecretKey& operatorKey, int port, const CScript& scriptOperatorPayout, const CKey& coinbaseKey) { CProUpServTx proTx; - proTx.nVersion = CProUpRevTx::GetVersion(!bls::bls_legacy_scheme); + proTx.nVersion = CProUpServTx::GetMaxVersion(!bls::bls_legacy_scheme); proTx.proTxHash = proTxHash; proTx.addr = LookupNumeric("1.1.1.1", port); proTx.scriptOperatorPayout = scriptOperatorPayout; @@ -145,7 +145,7 @@ static CMutableTransaction CreateProUpServTx(const CChain& active_chain, const C static CMutableTransaction CreateProUpRegTx(const CChain& active_chain, const CTxMemPool& mempool, SimpleUTXOMap& utxos, const uint256& proTxHash, const CKey& mnKey, const CBLSPublicKey& pubKeyOperator, const CKeyID& keyIDVoting, const CScript& scriptPayout, const CKey& coinbaseKey) { CProUpRegTx proTx; - proTx.nVersion = CProUpRegTx::GetVersion(!bls::bls_legacy_scheme); + proTx.nVersion = CProUpRegTx::GetMaxVersion(!bls::bls_legacy_scheme); proTx.proTxHash = proTxHash; proTx.pubKeyOperator.Set(pubKeyOperator, bls::bls_legacy_scheme.load()); proTx.keyIDVoting = keyIDVoting; @@ -166,7 +166,7 @@ static CMutableTransaction CreateProUpRegTx(const CChain& active_chain, const CT static CMutableTransaction CreateProUpRevTx(const CChain& active_chain, const CTxMemPool& mempool, SimpleUTXOMap& utxos, const uint256& proTxHash, const CBLSSecretKey& operatorKey, const CKey& coinbaseKey) { CProUpRevTx proTx; - proTx.nVersion = CProUpRevTx::GetVersion(!bls::bls_legacy_scheme); + proTx.nVersion = CProUpRevTx::GetMaxVersion(!bls::bls_legacy_scheme); proTx.proTxHash = proTxHash; CMutableTransaction tx; @@ -632,7 +632,7 @@ void FuncTestMempoolReorg(TestChainSetup& setup) BOOST_CHECK_EQUAL(block->GetHash(), chainman.ActiveChain().Tip()->GetBlockHash()); CProRegTx payload; - payload.nVersion = CProRegTx::GetVersion(!bls::bls_legacy_scheme); + payload.nVersion = CProRegTx::GetMaxVersion(!bls::bls_legacy_scheme); payload.addr = LookupNumeric("1.1.1.1", 1); payload.keyIDOwner = ownerKey.GetPubKey().GetID(); payload.pubKeyOperator.Set(operatorKey.GetPublicKey(), bls::bls_legacy_scheme.load()); @@ -774,7 +774,7 @@ void FuncVerifyDB(TestChainSetup& setup) BOOST_CHECK_EQUAL(block->GetHash(), chainman.ActiveChain().Tip()->GetBlockHash()); CProRegTx payload; - payload.nVersion = CProRegTx::GetVersion(!bls::bls_legacy_scheme); + payload.nVersion = CProRegTx::GetMaxVersion(!bls::bls_legacy_scheme); payload.addr = LookupNumeric("1.1.1.1", 1); payload.keyIDOwner = ownerKey.GetPubKey().GetID(); payload.pubKeyOperator.Set(operatorKey.GetPublicKey(), bls::bls_legacy_scheme.load()); diff --git a/src/txdb.cpp b/src/txdb.cpp index d518d7c419c48..2f7ceb657ce1c 100644 --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -291,11 +291,11 @@ bool CBlockTreeDB::ReadSpentIndex(const CSpentIndexKey key, CSpentIndexValue& va bool CBlockTreeDB::UpdateSpentIndex(const std::vector& vect) { CDBBatch batch(*this); - for (std::vector>::const_iterator it=vect.begin(); it!=vect.end(); it++) { - if (it->second.IsNull()) { - batch.Erase(std::make_pair(DB_SPENTINDEX, it->first)); + for (const auto& [key, value] : vect) { + if (value.IsNull()) { + batch.Erase(std::make_pair(DB_SPENTINDEX, key)); } else { - batch.Write(std::make_pair(DB_SPENTINDEX, it->first), it->second); + batch.Write(std::make_pair(DB_SPENTINDEX, key), value); } } return WriteBatch(batch); @@ -303,11 +303,11 @@ bool CBlockTreeDB::UpdateSpentIndex(const std::vector& vect) { bool CBlockTreeDB::UpdateAddressUnspentIndex(const std::vector& vect) { CDBBatch batch(*this); - for (std::vector::const_iterator it=vect.begin(); it!=vect.end(); it++) { - if (it->second.IsNull()) { - batch.Erase(std::make_pair(DB_ADDRESSUNSPENTINDEX, it->first)); + for (const auto& [key, value] : vect) { + if (value.IsNull()) { + batch.Erase(std::make_pair(DB_ADDRESSUNSPENTINDEX, key)); } else { - batch.Write(std::make_pair(DB_ADDRESSUNSPENTINDEX, it->first), it->second); + batch.Write(std::make_pair(DB_ADDRESSUNSPENTINDEX, key), value); } } return WriteBatch(batch); @@ -340,15 +340,17 @@ bool CBlockTreeDB::ReadAddressUnspentIndex(const uint160& addressHash, const Add bool CBlockTreeDB::WriteAddressIndex(const std::vector& vect) { CDBBatch batch(*this); - for (std::vector::const_iterator it=vect.begin(); it!=vect.end(); it++) - batch.Write(std::make_pair(DB_ADDRESSINDEX, it->first), it->second); + for (const auto& [key, value] : vect) { + batch.Write(std::make_pair(DB_ADDRESSINDEX, key), value); + } return WriteBatch(batch); } bool CBlockTreeDB::EraseAddressIndex(const std::vector& vect) { CDBBatch batch(*this); - for (std::vector::const_iterator it=vect.begin(); it!=vect.end(); it++) - batch.Erase(std::make_pair(DB_ADDRESSINDEX, it->first)); + for (const auto& [key, _] : vect) { + batch.Erase(std::make_pair(DB_ADDRESSINDEX, key)); + } return WriteBatch(batch); } diff --git a/src/txmempool.cpp b/src/txmempool.cpp index 33da28e3a350c..b81ee020d5ba5 100644 --- a/src/txmempool.cpp +++ b/src/txmempool.cpp @@ -609,7 +609,7 @@ bool CTxMemPool::getAddressIndex(const std::vector& add { LOCK(cs); for (const auto& address : addresses) { - addressDeltaMap::const_iterator ait = mapAddress.lower_bound(address); + auto ait = mapAddress.lower_bound(address); while (ait != mapAddress.end() && (*ait).first.m_address_bytes == address.m_address_bytes && (*ait).first.m_address_type == address.m_address_type) { results.push_back(*ait); @@ -619,20 +619,15 @@ bool CTxMemPool::getAddressIndex(const std::vector& add return true; } -bool CTxMemPool::removeAddressIndex(const uint256 txhash) +void CTxMemPool::removeAddressIndex(const uint256 txhash) { LOCK(cs); - addressDeltaMapInserted::iterator it = mapAddressInserted.find(txhash); - - if (it != mapAddressInserted.end()) { - std::vector keys = (*it).second; - for (std::vector::iterator mit = keys.begin(); mit != keys.end(); mit++) { - mapAddress.erase(*mit); + if (auto it = mapAddressInserted.find(txhash); it != mapAddressInserted.end()) { + for (const auto& key : (*it).second) { + mapAddress.erase(key); } mapAddressInserted.erase(it); } - - return true; } void CTxMemPool::addSpentIndex(const CTxMemPoolEntry& entry, const CCoinsViewCache& view) @@ -668,29 +663,22 @@ void CTxMemPool::addSpentIndex(const CTxMemPoolEntry& entry, const CCoinsViewCac bool CTxMemPool::getSpentIndex(const CSpentIndexKey& key, CSpentIndexValue& value) const { LOCK(cs); - mapSpentIndex::const_iterator it = mapSpent.find(key); - - if (it != mapSpent.end()) { + if (auto it = mapSpent.find(key); it != mapSpent.end()) { value = it->second; return true; } return false; } -bool CTxMemPool::removeSpentIndex(const uint256 txhash) +void CTxMemPool::removeSpentIndex(const uint256 txhash) { LOCK(cs); - mapSpentIndexInserted::iterator it = mapSpentInserted.find(txhash); - - if (it != mapSpentInserted.end()) { - std::vector keys = (*it).second; - for (std::vector::iterator mit = keys.begin(); mit != keys.end(); mit++) { - mapSpent.erase(*mit); + if (auto it = mapSpentInserted.find(txhash); it != mapSpentInserted.end()) { + for (const auto& key : (*it).second) { + mapSpent.erase(key); } mapSpentInserted.erase(it); } - - return true; } void CTxMemPool::addUncheckedProTx(indexed_transaction_set::iterator& newit, const CTransaction& tx) diff --git a/src/txmempool.h b/src/txmempool.h index cd25da6d37359..9bdc667dc4d0c 100644 --- a/src/txmempool.h +++ b/src/txmempool.h @@ -519,18 +519,10 @@ class CTxMemPool private: typedef std::map cacheMap; - - typedef std::map addressDeltaMap; - addressDeltaMap mapAddress; - - typedef std::map > addressDeltaMapInserted; - addressDeltaMapInserted mapAddressInserted; - - typedef std::map mapSpentIndex; - mapSpentIndex mapSpent; - - typedef std::map > mapSpentIndexInserted; - mapSpentIndexInserted mapSpentInserted; + std::map mapAddress; + std::map> mapAddressInserted; + std::map mapSpent; + std::map> mapSpentInserted; std::multimap mapProTxRefs; // proTxHash -> transaction (all TXs that refer to an existing proTx) std::map mapProTxAddresses; @@ -618,11 +610,11 @@ class CTxMemPool void addAddressIndex(const CTxMemPoolEntry& entry, const CCoinsViewCache& view); bool getAddressIndex(const std::vector& addresses, std::vector& results) const; - bool removeAddressIndex(const uint256 txhash); + void removeAddressIndex(const uint256 txhash); void addSpentIndex(const CTxMemPoolEntry& entry, const CCoinsViewCache& view); bool getSpentIndex(const CSpentIndexKey& key, CSpentIndexValue& value) const; - bool removeSpentIndex(const uint256 txhash); + void removeSpentIndex(const uint256 txhash); void removeRecursive(const CTransaction& tx, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs); /** After reorg, filter the entries that would no longer be valid in the next block, and update diff --git a/src/wallet/test/coinjoin_tests.cpp b/src/wallet/test/coinjoin_tests.cpp index e194416e06c4d..d67b3cb0dc7e6 100644 --- a/src/wallet/test/coinjoin_tests.cpp +++ b/src/wallet/test/coinjoin_tests.cpp @@ -67,16 +67,16 @@ BOOST_AUTO_TEST_CASE(coinjoin_collateral_tests) BOOST_AUTO_TEST_CASE(coinjoin_pending_dsa_request_tests) { CPendingDsaRequest dsa_request; - BOOST_CHECK(dsa_request.GetAddr() == CService()); + BOOST_CHECK(dsa_request.GetProTxHash() == uint256()); BOOST_CHECK(dsa_request.GetDSA() == CCoinJoinAccept()); BOOST_CHECK_EQUAL(dsa_request.IsExpired(), true); CPendingDsaRequest dsa_request_2; BOOST_CHECK(dsa_request == dsa_request_2); CCoinJoinAccept cja; cja.nDenom = 4; - CService cserv(CNetAddr(), 1111); - CPendingDsaRequest custom_request(cserv, cja); - BOOST_CHECK(custom_request.GetAddr() == cserv); + uint256 proTxHash{uint256::ONE}; + CPendingDsaRequest custom_request(proTxHash, cja); + BOOST_CHECK(custom_request.GetProTxHash() == proTxHash); BOOST_CHECK(custom_request.GetDSA() == cja); BOOST_CHECK_EQUAL(custom_request.IsExpired(), false); SetMockTime(GetTime() + 15);