From e83193069b79ab5c96a3e43481149a0062cc3894 Mon Sep 17 00:00:00 2001 From: Bushstar Date: Thu, 9 Mar 2023 10:17:27 +0800 Subject: [PATCH 1/4] Pass coinSelectOpts to AvailableCoin. Remove dead code. --- src/wallet/wallet.cpp | 89 +++++-------------------------------------- 1 file changed, 9 insertions(+), 80 deletions(-) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 37314661d09..f87bab139f7 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -3009,18 +3009,16 @@ bool CWallet::CreateTransaction(interfaces::Chain::Lock& locked_chain, const std strFailReason = _("Transaction amounts must not be negative").translated; return false; } - if (recipient.nTokenId == DCT_ID{0}) - { - nValue += recipient.nAmount; - if (recipient.fSubtractFeeFromAmount) - nSubtractFeeFromAmount++; - } - else - { - assert(!recipient.fSubtractFeeFromAmount); - vTokenValues[recipient.nTokenId] += recipient.nAmount; + if (recipient.nTokenId != DCT_ID{0}) { + strFailReason = "Unexpected token type set for recipient. Only native DFI token type expected."; + return false; } + + nValue += recipient.nAmount; + + if (recipient.fSubtractFeeFromAmount) + nSubtractFeeFromAmount++; } bool eagerSelect = coinSelectOpts.eagerSelect; @@ -3045,68 +3043,6 @@ bool CWallet::CreateTransaction(interfaces::Chain::Lock& locked_chain, const std const OutputType change_type = TransactionChangeType(coin_control.m_change_type ? *coin_control.m_change_type : m_default_change_type, vecSend); - std::map > tokensreservedest; - std::set tokenCoins; - { - auto locked_chain = chain().lock(); - LOCK2(cs_wallet, locked_chain->mutex()); - - for (auto it = vTokenValues.begin(); it != vTokenValues.end(); ++it) - { - std::vector vAvailableCoins; - - DCT_ID tokenId = it->first; - CCoinControl ccSingleToken(coin_control); - ccSingleToken.m_tokenFilter = { tokenId }; - - AvailableCoins(*locked_chain, vAvailableCoins, true, &ccSingleToken, 1, MAX_MONEY, sumAmountToSelect, 0); - - tokensreservedest.emplace(tokenId, std::unique_ptr(new ReserveDestination(this))); // used dynamic here due to strange bug with direct emplacement under mac - CScript scriptChange; - // coin control: send change to custom address - if (!std::get_if(&coin_control.destChange)) { - scriptChange = GetScriptForDestination(coin_control.destChange); - } else { // no coin control: send change to newly generated address - // Reserve a new key pair from key pool - if (!CanGetAddresses(true)) { - strFailReason = _("Can't generate a change-address key. No keys in the internal keypool and can't generate any keys.").translated; - return false; - } - CTxDestination dest; - if (!tokensreservedest.at(tokenId)->GetReservedDestination(change_type, dest, true)) - { - strFailReason = "Keypool ran out, please call keypoolrefill first"; - return false; - } - scriptChange = GetScriptForDestination(dest); - } - - CAmount nValueIn; - std::set setCoins; - bool bnb_used; - CoinSelectionParams coin_selection_params; // Parameters for coin selection, init with dummy - bool result = SelectCoins(vAvailableCoins, it->second, setCoins, nValueIn, ccSingleToken, coin_selection_params, bnb_used); - if (!result && bnb_used) - { - coin_selection_params.use_bnb = false; - result = SelectCoins(vAvailableCoins, it->second, setCoins, nValueIn, ccSingleToken, coin_selection_params, bnb_used); - } - if (!result) - { - strFailReason = _("Insufficient funds").translated; - return false; - } - tokenCoins.insert(setCoins.begin(), setCoins.end()); - - const CAmount nChange = nValueIn - it->second; - if (nChange > 0) - { - /// @todo tokens: ORDER??? - vecSend.push_back(CRecipient{scriptChange, nChange, tokenId, false}); - } - } - } - FeeCalculation feeCalc; CAmount nFeeNeeded; int nBytes; @@ -3116,7 +3052,7 @@ bool CWallet::CreateTransaction(interfaces::Chain::Lock& locked_chain, const std LOCK2(cs_wallet, locked_chain->mutex()); { std::vector vAvailableCoins; - AvailableCoins(*locked_chain, vAvailableCoins, true, &coin_control, 1, MAX_MONEY, sumAmountToSelect, 0); + AvailableCoins(*locked_chain, vAvailableCoins, true, &coin_control, 1, MAX_MONEY, sumAmountToSelect, 0, coinSelectOpts); CoinSelectionParams coin_selection_params; // Parameters for coin selection, init with dummy // Create change script that will be used if we need change @@ -3283,9 +3219,6 @@ bool CWallet::CreateTransaction(interfaces::Chain::Lock& locked_chain, const std for (const auto& coin : setCoins) { txNew.vin.push_back(CTxIn(coin.outpoint,CScript())); } - for (const auto& coin : tokenCoins) { - txNew.vin.push_back(CTxIn(coin.outpoint,CScript())); - } nBytes = CalculateMaximumSignedTxSize(CTransaction(txNew), this, coin_control.fAllowWatchOnly, coin_control.m_linkedCoins); if (nBytes < 0) { @@ -3368,7 +3301,6 @@ bool CWallet::CreateTransaction(interfaces::Chain::Lock& locked_chain, const std // Shuffle selected coins and fill in final vin txNew.vin.clear(); std::vector selected_coins(setCoins.begin(), setCoins.end()); - selected_coins.insert(selected_coins.end(), tokenCoins.begin(), tokenCoins.end()); Shuffle(selected_coins.begin(), selected_coins.end(), FastRandomContext()); // Note how the sequence number is set to non-maxint so that @@ -3431,9 +3363,6 @@ bool CWallet::CreateTransaction(interfaces::Chain::Lock& locked_chain, const std // Before we return success, we assume any change key will be used to prevent // accidental re-use. reservedest.KeepDestination(); - for (auto && pair : tokensreservedest) { - pair.second->KeepDestination(); - } WalletLogPrintf("Fee Calculation: Fee:%d Bytes:%u Needed:%d Tgt:%d (requested %d) Reason:\"%s\" Decay %.5f: Estimation: (%g - %g) %.2f%% %.1f/(%.1f %d mem %.1f out) Fail: (%g - %g) %.2f%% %.1f/(%.1f %d mem %.1f out)\n", nFeeRet, nBytes, nFeeNeeded, feeCalc.returnedTarget, feeCalc.desiredTarget, StringForFeeReason(feeCalc.reason), feeCalc.est.decay, From 4b2df782241d2d30defbfe5e87ccb2b3f9eb1df7 Mon Sep 17 00:00:00 2001 From: Bushstar Date: Thu, 9 Mar 2023 10:26:53 +0800 Subject: [PATCH 2/4] Pass opts from fund to CreateTransaction --- src/masternodes/mn_rpc.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/masternodes/mn_rpc.cpp b/src/masternodes/mn_rpc.cpp index 8e7d4beba41..7c13a3d2941 100644 --- a/src/masternodes/mn_rpc.cpp +++ b/src/masternodes/mn_rpc.cpp @@ -132,7 +132,7 @@ CMutableTransaction fund(CMutableTransaction & mtx, CWalletCoinsUnlocker& pwalle // we does not honor non locking spends anymore // it ensures auto auth not overlap regular tx inputs const bool lockUnspents = true; - if (!pwallet->FundTransaction(mtx, fee_out, change_position, strFailReason, lockUnspents, {} /*setSubtractFeeFromOutputs*/, coinControl)) { + if (!pwallet->FundTransaction(mtx, fee_out, change_position, strFailReason, lockUnspents, {} /*setSubtractFeeFromOutputs*/, coinControl, coinSelectOpts)) { throw JSONRPCError(RPC_WALLET_ERROR, strFailReason); } for (auto& txin : mtx.vin) { From 6f816f86d1e0c72929c73a4288bc8260f3be2a31 Mon Sep 17 00:00:00 2001 From: Bushstar Date: Thu, 9 Mar 2023 10:54:35 +0800 Subject: [PATCH 3/4] Pass opts to GetAuth for votegov --- src/masternodes/rpc_proposals.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/masternodes/rpc_proposals.cpp b/src/masternodes/rpc_proposals.cpp index 30e61cf59f0..be376e650a0 100644 --- a/src/masternodes/rpc_proposals.cpp +++ b/src/masternodes/rpc_proposals.cpp @@ -564,7 +564,7 @@ UniValue votegov(const JSONRPCRequest &request) { CTransactionRef optAuthTx; std::set auths = {GetScriptForDestination(ownerDest)}; rawTx.vin = - GetAuthInputsSmart(pwallet, rawTx.nVersion, auths, false /*needFoundersAuth*/, optAuthTx, request.params[3]); + GetAuthInputsSmart(pwallet, rawTx.nVersion, auths, false /*needFoundersAuth*/, optAuthTx, request.params[3], request.metadata.coinSelectOpts); rawTx.vout.emplace_back(CTxOut(0, scriptMeta)); From 5f40e5f06b4fc88c2135a23c5acfbe61da480486 Mon Sep 17 00:00:00 2001 From: Bushstar Date: Thu, 9 Mar 2023 11:00:53 +0800 Subject: [PATCH 4/4] Pass opts to AvailableCoins from GetAuth --- src/masternodes/mn_rpc.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/masternodes/mn_rpc.cpp b/src/masternodes/mn_rpc.cpp index 7c13a3d2941..52ebe59e811 100644 --- a/src/masternodes/mn_rpc.cpp +++ b/src/masternodes/mn_rpc.cpp @@ -304,7 +304,7 @@ static std::optional GetAuthInputOnly(CWalletCoinsUnlocker& pwallet, CTxD LOCK2(pwallet->cs_wallet, locked_chain->mutex()); // Note, for auth, we call this with 1 as max count, so should early exit - pwallet->AvailableCoins(*locked_chain, vecOutputs, true, &cctl, 1, MAX_MONEY, MAX_MONEY, 1); + pwallet->AvailableCoins(*locked_chain, vecOutputs, true, &cctl, 1, MAX_MONEY, MAX_MONEY, 1, coinSelectOpts); if (vecOutputs.empty()) { return {};