diff --git a/src/rpc/coinjoin.cpp b/src/rpc/coinjoin.cpp index 69680e6bd5..ae13d4bee9 100644 --- a/src/rpc/coinjoin.cpp +++ b/src/rpc/coinjoin.cpp @@ -34,7 +34,6 @@ static UniValue coinjoin(const JSONRPCRequest& request) std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); if (!wallet) return NullUniValue; - CWallet* const pwallet = wallet.get(); if (fMasternodeMode) throw JSONRPCError(RPC_INTERNAL_ERROR, "Client-side mixing is not supported on masternodes"); @@ -50,12 +49,12 @@ static UniValue coinjoin(const JSONRPCRequest& request) } } - auto it = coinJoinClientManagers.find(pwallet->GetName()); + auto it = coinJoinClientManagers.find(wallet->GetName()); if (request.params[0].get_str() == "start") { { - LOCK(pwallet->cs_wallet); - if (pwallet->IsLocked(true)) + LOCK(wallet->cs_wallet); + if (wallet->IsLocked(true)) throw JSONRPCError(RPC_WALLET_UNLOCK_NEEDED, "Error: Please unlock wallet for mixing with walletpassphrase first."); } @@ -156,15 +155,14 @@ static UniValue getcoinjoininfo(const JSONRPCRequest& request) obj.pushKV("queue_size", coinJoinClientQueueManager->GetQueueSize()); std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); - CWallet* const pwallet = wallet.get(); - if (!pwallet) { + if (!wallet) { return obj; } - coinJoinClientManagers.at(pwallet->GetName())->GetJsonInfo(obj); + coinJoinClientManagers.at(wallet->GetName())->GetJsonInfo(obj); - obj.pushKV("keys_left", pwallet->nKeysLeftSinceAutoBackup); - obj.pushKV("warnings", pwallet->nKeysLeftSinceAutoBackup < COINJOIN_KEYS_THRESHOLD_WARNING + obj.pushKV("keys_left", wallet->nKeysLeftSinceAutoBackup); + obj.pushKV("warnings", wallet->nKeysLeftSinceAutoBackup < COINJOIN_KEYS_THRESHOLD_WARNING ? "WARNING: keypool is almost depleted!" : ""); #endif // ENABLE_WALLET diff --git a/src/rpc/governance.cpp b/src/rpc/governance.cpp index cb694792e5..3a948cfae9 100644 --- a/src/rpc/governance.cpp +++ b/src/rpc/governance.cpp @@ -149,11 +149,10 @@ static UniValue gobject_prepare(const JSONRPCRequest& request) { std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); if (!wallet) return NullUniValue; - CWallet* const pwallet = wallet.get(); gobject_prepare_help(request); - EnsureWalletIsUnlocked(pwallet); + EnsureWalletIsUnlocked(wallet.get()); // ASSEMBLE NEW GOVERNANCE OBJECT FROM USER PARAMETERS @@ -200,7 +199,7 @@ static UniValue gobject_prepare(const JSONRPCRequest& request) g_txindex->BlockUntilSyncedToCurrentChain(); } - LOCK(pwallet->cs_wallet); + LOCK(wallet->cs_wallet); { LOCK(cs_main); @@ -225,7 +224,7 @@ static UniValue gobject_prepare(const JSONRPCRequest& request) bool fork_active = WITH_LOCK(cs_main, return VersionBitsTipState(Params().GetConsensus(), Consensus::DEPLOYMENT_DIP0024) == ThresholdState::ACTIVE); - if (!pwallet->GetBudgetSystemCollateralTX(tx, govobj.GetHash(), govobj.GetMinCollateralFee(fork_active), outpoint)) { + if (!wallet->GetBudgetSystemCollateralTX(tx, govobj.GetHash(), govobj.GetMinCollateralFee(fork_active), outpoint)) { std::string err = "Error making collateral transaction for governance object. Please check your wallet balance and make sure your wallet is unlocked."; if (!request.params[5].isNull() && !request.params[6].isNull()) { err += "Please verify your specified output is valid and is enough for the combined proposal fee and transaction fee."; @@ -233,14 +232,14 @@ static UniValue gobject_prepare(const JSONRPCRequest& request) throw JSONRPCError(RPC_INTERNAL_ERROR, err); } - if (!pwallet->WriteGovernanceObject({hashParent, nRevision, nTime, tx->GetHash(), strDataHex})) { + if (!wallet->WriteGovernanceObject({hashParent, nRevision, nTime, tx->GetHash(), strDataHex})) { throw JSONRPCError(RPC_INTERNAL_ERROR, "WriteGovernanceObject failed"); } // -- send the tx to the network { LOCK(cs_main); - pwallet->CommitTransaction(tx, {}, {}); + wallet->CommitTransaction(tx, {}, {}); } LogPrint(BCLog::GOBJECT, "gobject_prepare -- GetDataAsPlainString = %s, hash = %s, txid = %s\n", @@ -268,17 +267,15 @@ static UniValue gobject_list_prepared(const JSONRPCRequest& request) std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); if (!wallet) return NullUniValue; - CWallet* const pwallet = wallet.get(); - - EnsureWalletIsUnlocked(pwallet); + EnsureWalletIsUnlocked(wallet.get()); int64_t nCount = request.params.empty() ? 10 : ParseInt64V(request.params[0], "count"); if (nCount < 0) { throw JSONRPCError(RPC_INVALID_PARAMETER, "Negative count"); } // Get a list of all prepared governance objects stored in the wallet - LOCK(pwallet->cs_wallet); - std::vector vecObjects = pwallet->GetGovernanceObjects(); + LOCK(wallet->cs_wallet); + std::vector vecObjects = wallet->GetGovernanceObjects(); // Sort the vector by the object creation time/hex data std::sort(vecObjects.begin(), vecObjects.end(), [](const CGovernanceObject* a, const CGovernanceObject* b) { bool fGreater = a->GetCreationTime() > b->GetCreationTime(); @@ -608,7 +605,6 @@ static UniValue gobject_vote_many(const JSONRPCRequest& request) std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); if (!wallet) return NullUniValue; - CWallet* const pwallet = wallet.get(); uint256 hash = ParseHashV(request.params[0], "Object hash"); std::string strVoteSignal = request.params[1].get_str(); @@ -626,9 +622,9 @@ static UniValue gobject_vote_many(const JSONRPCRequest& request) throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid vote outcome. Please use one of the following: 'yes', 'no' or 'abstain'"); } - EnsureWalletIsUnlocked(pwallet); + EnsureWalletIsUnlocked(wallet.get()); - LegacyScriptPubKeyMan* spk_man = pwallet->GetLegacyScriptPubKeyMan(); + LegacyScriptPubKeyMan* spk_man = wallet->GetLegacyScriptPubKeyMan(); if (!spk_man) { throw JSONRPCError(RPC_WALLET_ERROR, "This type of wallet does not support this command"); } @@ -668,7 +664,6 @@ static UniValue gobject_vote_alias(const JSONRPCRequest& request) std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); if (!wallet) return NullUniValue; - CWallet* const pwallet = wallet.get(); uint256 hash = ParseHashV(request.params[0], "Object hash"); std::string strVoteSignal = request.params[1].get_str(); @@ -686,7 +681,7 @@ static UniValue gobject_vote_alias(const JSONRPCRequest& request) throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid vote outcome. Please use one of the following: 'yes', 'no' or 'abstain'"); } - EnsureWalletIsUnlocked(pwallet); + EnsureWalletIsUnlocked(wallet.get()); uint256 proTxHash = ParseHashV(request.params[3], "protx-hash"); auto dmn = deterministicMNManager->GetListAtChainTip().GetValidMN(proTxHash); @@ -694,7 +689,7 @@ static UniValue gobject_vote_alias(const JSONRPCRequest& request) throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid or unknown proTxHash"); } - LegacyScriptPubKeyMan* spk_man = pwallet->GetLegacyScriptPubKeyMan(); + LegacyScriptPubKeyMan* spk_man = wallet->GetLegacyScriptPubKeyMan(); if (!spk_man) { throw JSONRPCError(RPC_WALLET_ERROR, "This type of wallet does not support this command"); } diff --git a/src/rpc/masternode.cpp b/src/rpc/masternode.cpp index f9f471eacb..180ca1d53b 100644 --- a/src/rpc/masternode.cpp +++ b/src/rpc/masternode.cpp @@ -196,15 +196,14 @@ static UniValue masternode_outputs(const JSONRPCRequest& request) std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); if (!wallet) return NullUniValue; - CWallet* const pwallet = wallet.get(); // Find possible candidates std::vector vPossibleCoins; CCoinControl coin_control; coin_control.nCoinType = CoinType::ONLY_MASTERNODE_COLLATERAL; { - LOCK(pwallet->cs_wallet); - pwallet->AvailableCoins(vPossibleCoins, true, &coin_control); + LOCK(wallet->cs_wallet); + wallet->AvailableCoins(vPossibleCoins, true, &coin_control); } UniValue outputsArr(UniValue::VARR); for (const auto& out : vPossibleCoins) { diff --git a/src/rpc/rpcevo.cpp b/src/rpc/rpcevo.cpp index b477efa846..e11f65e706 100644 --- a/src/rpc/rpcevo.cpp +++ b/src/rpc/rpcevo.cpp @@ -194,7 +194,7 @@ static void FundSpecialTx(CWallet* pwallet, CMutableTransaction& tx, const Speci ds << payload; tx.vExtraPayload.assign(ds.begin(), ds.end()); - static CTxOut dummyTxOut(0, CScript() << OP_RETURN); + static const CTxOut dummyTxOut(0, CScript() << OP_RETURN); std::vector vecSend; bool dummyTxOutAdded = false; @@ -446,10 +446,9 @@ static UniValue protx_register(const JSONRPCRequest& request) std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); if (!wallet) return NullUniValue; - CWallet* const pwallet = wallet.get(); if (isExternalRegister || isFundRegister) { - EnsureWalletIsUnlocked(pwallet); + EnsureWalletIsUnlocked(wallet.get()); } size_t paramIdx = 0; @@ -485,8 +484,8 @@ static UniValue protx_register(const JSONRPCRequest& request) paramIdx += 2; // TODO unlock on failure - LOCK(pwallet->cs_wallet); - pwallet->LockCoin(ptx.collateralOutpoint); + LOCK(wallet->cs_wallet); + wallet->LockCoin(ptx.collateralOutpoint); } if (request.params[paramIdx].get_str() != "") { @@ -533,7 +532,7 @@ static UniValue protx_register(const JSONRPCRequest& request) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, std::string("Invalid Dash address: ") + request.params[paramIdx + 6].get_str()); } - FundSpecialTx(pwallet, tx, ptx, fundDest); + FundSpecialTx(wallet.get(), tx, ptx, fundDest); UpdateSpecialTxInputsHash(tx, ptx); bool fSubmit{true}; @@ -580,7 +579,7 @@ static UniValue protx_register(const JSONRPCRequest& request) return ret; } else { // lets prove we own the collateral - LegacyScriptPubKeyMan* spk_man = pwallet->GetLegacyScriptPubKeyMan(); + LegacyScriptPubKeyMan* spk_man = wallet->GetLegacyScriptPubKeyMan(); if (!spk_man) { throw JSONRPCError(RPC_WALLET_ERROR, "This type of wallet does not support this command"); } @@ -602,9 +601,8 @@ static UniValue protx_register_submit(const JSONRPCRequest& request) std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); if (!wallet) return NullUniValue; - CWallet* const pwallet = wallet.get(); - EnsureWalletIsUnlocked(pwallet); + EnsureWalletIsUnlocked(wallet.get()); CMutableTransaction tx; if (!DecodeHexTx(tx, request.params[0].get_str())) { @@ -656,9 +654,8 @@ static UniValue protx_update_service(const JSONRPCRequest& request) std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); if (!wallet) return NullUniValue; - CWallet* const pwallet = wallet.get(); - EnsureWalletIsUnlocked(pwallet); + EnsureWalletIsUnlocked(wallet.get()); CProUpServTx ptx; ptx.nVersion = CProUpServTx::CURRENT_VERSION; @@ -715,7 +712,7 @@ static UniValue protx_update_service(const JSONRPCRequest& request) } } - FundSpecialTx(pwallet, tx, ptx, feeSource); + FundSpecialTx(wallet.get(), tx, ptx, feeSource); SignSpecialTxPayloadByHash(tx, ptx, keyOperator); SetTxPayload(tx, ptx); @@ -752,9 +749,8 @@ static UniValue protx_update_registrar(const JSONRPCRequest& request) std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); if (!wallet) return NullUniValue; - CWallet* const pwallet = wallet.get(); - EnsureWalletIsUnlocked(pwallet); + EnsureWalletIsUnlocked(wallet.get()); CProUpRegTx ptx; ptx.nVersion = CProUpRegTx::CURRENT_VERSION; @@ -785,7 +781,7 @@ static UniValue protx_update_registrar(const JSONRPCRequest& request) ptx.scriptPayout = GetScriptForDestination(payoutDest); } - LegacyScriptPubKeyMan* spk_man = pwallet->GetLegacyScriptPubKeyMan(); + LegacyScriptPubKeyMan* spk_man = wallet->GetLegacyScriptPubKeyMan(); if (!spk_man) { throw JSONRPCError(RPC_WALLET_ERROR, "This type of wallet does not support this command"); } @@ -809,7 +805,7 @@ static UniValue protx_update_registrar(const JSONRPCRequest& request) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, std::string("Invalid Dash address: ") + request.params[4].get_str()); } - FundSpecialTx(pwallet, tx, ptx, feeSourceDest); + FundSpecialTx(wallet.get(), tx, ptx, feeSourceDest); SignSpecialTxPayloadByHash(tx, ptx, keyOwner); SetTxPayload(tx, ptx); @@ -845,9 +841,8 @@ static UniValue protx_revoke(const JSONRPCRequest& request) std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); if (!wallet) return NullUniValue; - CWallet* const pwallet = wallet.get(); - EnsureWalletIsUnlocked(pwallet); + EnsureWalletIsUnlocked(wallet.get()); CProUpRevTx ptx; ptx.nVersion = CProUpRevTx::CURRENT_VERSION; @@ -880,17 +875,17 @@ static UniValue protx_revoke(const JSONRPCRequest& request) CTxDestination feeSourceDest = DecodeDestination(request.params[3].get_str()); if (!IsValidDestination(feeSourceDest)) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, std::string("Invalid Dash address: ") + request.params[3].get_str()); - FundSpecialTx(pwallet, tx, ptx, feeSourceDest); + FundSpecialTx(wallet.get(), tx, ptx, feeSourceDest); } else if (dmn->pdmnState->scriptOperatorPayout != CScript()) { // Using funds from previousely specified operator payout address CTxDestination txDest; ExtractDestination(dmn->pdmnState->scriptOperatorPayout, txDest); - FundSpecialTx(pwallet, tx, ptx, txDest); + FundSpecialTx(wallet.get(), tx, ptx, txDest); } else if (dmn->pdmnState->scriptPayout != CScript()) { // Using funds from previousely specified masternode payout address CTxDestination txDest; ExtractDestination(dmn->pdmnState->scriptPayout, txDest); - FundSpecialTx(pwallet, tx, ptx, txDest); + FundSpecialTx(wallet.get(), tx, ptx, txDest); } else { throw JSONRPCError(RPC_INTERNAL_ERROR, "No payout or fee source addresses found, can't revoke"); } @@ -1002,16 +997,12 @@ static UniValue protx_list(const JSONRPCRequest& request) { protx_list_help(request); - CWallet* pwallet; + std::shared_ptr wallet{nullptr}; #ifdef ENABLE_WALLET try { - std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); - pwallet = wallet.get(); + wallet = GetWalletForJSONRPCRequest(request); } catch (...) { - pwallet = nullptr; } -#else - pwallet = nullptr; #endif std::string type = "registered"; @@ -1026,11 +1017,11 @@ static UniValue protx_list(const JSONRPCRequest& request) } if (type == "wallet") { - if (!pwallet) { + if (!wallet) { throw std::runtime_error("\"protx list wallet\" not supported when wallet is disabled"); } #ifdef ENABLE_WALLET - LOCK2(pwallet->cs_wallet, cs_main); + LOCK2(wallet->cs_wallet, cs_main); if (request.params.size() > 4) { protx_list_help(request); @@ -1044,7 +1035,7 @@ static UniValue protx_list(const JSONRPCRequest& request) } std::vector vOutpts; - pwallet->ListProTxCoins(vOutpts); + wallet->ListProTxCoins(vOutpts); std::set setOutpts; for (const auto& outpt : vOutpts) { setOutpts.emplace(outpt); @@ -1053,11 +1044,11 @@ static UniValue protx_list(const JSONRPCRequest& request) CDeterministicMNList mnList = deterministicMNManager->GetListForBlock(::ChainActive()[height]); mnList.ForEachMN(false, [&](const auto& dmn) { if (setOutpts.count(dmn.collateralOutpoint) || - CheckWalletOwnsKey(pwallet, dmn.pdmnState->keyIDOwner) || - CheckWalletOwnsKey(pwallet, dmn.pdmnState->keyIDVoting) || - CheckWalletOwnsScript(pwallet, dmn.pdmnState->scriptPayout) || - CheckWalletOwnsScript(pwallet, dmn.pdmnState->scriptOperatorPayout)) { - ret.push_back(BuildDMNListEntry(pwallet, dmn, detailed)); + CheckWalletOwnsKey(wallet.get(), dmn.pdmnState->keyIDOwner) || + CheckWalletOwnsKey(wallet.get(), dmn.pdmnState->keyIDVoting) || + CheckWalletOwnsScript(wallet.get(), dmn.pdmnState->scriptPayout) || + CheckWalletOwnsScript(wallet.get(), dmn.pdmnState->scriptOperatorPayout)) { + ret.push_back(BuildDMNListEntry(wallet.get(), dmn, detailed)); } }); #endif @@ -1078,7 +1069,7 @@ static UniValue protx_list(const JSONRPCRequest& request) CDeterministicMNList mnList = deterministicMNManager->GetListForBlock(::ChainActive()[height]); bool onlyValid = type == "valid"; mnList.ForEachMN(onlyValid, [&](const auto& dmn) { - ret.push_back(BuildDMNListEntry(pwallet, dmn, detailed)); + ret.push_back(BuildDMNListEntry(wallet.get(), dmn, detailed)); }); } else { throw JSONRPCError(RPC_INVALID_PARAMETER, "invalid type specified"); @@ -1110,16 +1101,12 @@ static UniValue protx_info(const JSONRPCRequest& request) { protx_info_help(request); - CWallet* pwallet; + std::shared_ptr wallet{nullptr}; #ifdef ENABLE_WALLET try { - std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); - pwallet = wallet.get(); + wallet = GetWalletForJSONRPCRequest(request); } catch (...) { - pwallet = nullptr; } -#else - pwallet = nullptr; #endif if (g_txindex) { @@ -1132,7 +1119,7 @@ static UniValue protx_info(const JSONRPCRequest& request) if (!dmn) { throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s not found", proTxHash.ToString())); } - return BuildDMNListEntry(pwallet, *dmn, true); + return BuildDMNListEntry(wallet.get(), *dmn, true); } static void protx_diff_help(const JSONRPCRequest& request)