2022-12-24 23:49:50 +00:00
// Copyright (c) 2021-2022 The Bitcoin Core developers
2021-02-10 16:06:01 -05:00
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2021-07-19 03:15:44 +00:00
# include <blind.h> // ELEMENTS: for MAX_RANGEPROOF_SIZE
2021-09-11 10:29:00 +08:00
# include <consensus/amount.h>
2021-02-10 16:06:01 -05:00
# include <consensus/validation.h>
# include <interfaces/chain.h>
2021-07-19 03:15:44 +00:00
# include <issuance.h> // ELEMENTS: for GenerateAssetEntropy and others
2025-03-18 11:33:11 +00:00
# include <algorithm>
2022-10-04 14:17:11 +02:00
# include <numeric>
2021-02-10 16:06:01 -05:00
# include <policy/policy.h>
2021-10-02 15:28:13 +00:00
# include <rpc/util.h> // for GetDestinationBlindingKey and IsBlindDestination
2024-09-10 23:33:20 +00:00
# include <script/pegins.h>
2022-12-05 14:33:35 -05:00
# include <primitives/transaction.h>
2019-10-18 17:17:17 -04:00
# include <script/signingprovider.h>
2021-02-10 16:06:01 -05:00
# include <util/check.h>
# include <util/fees.h>
# include <util/moneystr.h>
# include <util/rbf.h>
2022-03-21 14:33:29 -04:00
# include <util/trace.h>
2021-02-10 16:06:01 -05:00
# include <util/translation.h>
# include <wallet/coincontrol.h>
# include <wallet/fees.h>
# include <wallet/receive.h>
# include <wallet/spend.h>
# include <wallet/transaction.h>
# include <wallet/wallet.h>
2022-03-07 13:46:49 +00:00
# include <cmath>
2021-02-10 16:06:01 -05:00
using interfaces : : FoundBlock ;
2021-11-12 11:13:29 -05:00
namespace wallet {
2021-02-10 16:06:01 -05:00
static constexpr size_t OUTPUT_GROUP_MAX_ENTRIES { 100 } ;
2025-04-09 11:43:36 +02:00
int CalculateMaximumSignedInputSize ( const CTxOut & txout , const COutPoint outpoint , const SigningProvider * provider , bool can_grind_r , const CCoinControl * coin_control ) {
2021-02-10 16:06:01 -05:00
CMutableTransaction txn ;
2022-06-27 09:11:09 +02:00
txn . vin . push_back ( CTxIn ( outpoint ) ) ;
2025-04-09 11:43:36 +02:00
if ( ! provider | | ! DummySignInput ( * provider , txn , 0 , txout , can_grind_r , coin_control ) ) {
2021-02-10 16:06:01 -05:00
return - 1 ;
}
2021-07-19 03:15:44 +00:00
return GetVirtualTransactionInputSize ( CTransaction ( txn ) ) ;
2021-02-10 16:06:01 -05:00
}
2022-06-27 09:11:09 +02:00
int CalculateMaximumSignedInputSize ( const CTxOut & txout , const CWallet * wallet , const CCoinControl * coin_control )
2021-02-10 16:06:01 -05:00
{
2019-10-18 17:17:17 -04:00
const std : : unique_ptr < SigningProvider > provider = wallet - > GetSolvingProvider ( txout . scriptPubKey ) ;
2023-02-23 18:30:12 +01:00
return CalculateMaximumSignedInputSize ( txout , COutPoint ( ) , provider . get ( ) , wallet - > CanGrindR ( ) , coin_control ) ;
2021-02-10 16:06:01 -05:00
}
2021-07-19 03:15:44 +00:00
// Returns pair of vsize and weight
2023-09-06 06:42:38 +00:00
TxSize CalculateMaximumSignedTxSize ( const CTransaction & tx , const CWallet * wallet , const CCoinControl * coin_control ) EXCLUSIVE_LOCKS_REQUIRED ( wallet - > cs_wallet )
2021-02-10 16:06:01 -05:00
{
std : : vector < CTxOut > txouts ;
2021-07-19 03:15:44 +00:00
// Look up the inputs. The inputs are either in the wallet, or in coin_control.
2021-02-10 16:06:01 -05:00
for ( const CTxIn & input : tx . vin ) {
const auto mi = wallet - > mapWallet . find ( input . prevout . hash ) ;
2021-07-19 03:15:44 +00:00
if ( mi ! = wallet - > mapWallet . end ( ) ) {
assert ( input . prevout . n < mi - > second . tx - > vout . size ( ) ) ;
txouts . emplace_back ( mi - > second . tx - > vout [ input . prevout . n ] ) ;
} else if ( coin_control ) {
CTxOut txout ;
if ( ! coin_control - > GetExternalOutput ( input . prevout , txout ) ) {
return TxSize { - 1 , - 1 } ;
}
txouts . emplace_back ( txout ) ;
} else {
2021-02-10 16:06:01 -05:00
return TxSize { - 1 , - 1 } ;
}
}
2021-07-19 03:15:44 +00:00
return CalculateMaximumSignedTxSize ( tx , wallet , txouts , coin_control ) ;
2021-02-10 16:06:01 -05:00
}
2021-07-19 03:15:44 +00:00
// txouts needs to be in the order of tx.vin
TxSize CalculateMaximumSignedTxSize ( const CTransaction & tx , const CWallet * wallet , const std : : vector < CTxOut > & txouts , const CCoinControl * coin_control )
{
CMutableTransaction txNew ( tx ) ;
if ( ! wallet - > DummySignTx ( txNew , txouts , coin_control ) ) {
return TxSize { - 1 , - 1 } ;
}
CTransaction ctx ( txNew ) ;
int64_t vsize = GetVirtualTransactionSize ( ctx ) ;
int64_t weight = GetTransactionWeight ( ctx ) ;
2024-05-09 10:17:45 +02:00
// ELEMENTS: use discounted vsize for CTs if enabled
if ( Params ( ) . GetCreateDiscountCT ( ) ) {
vsize = GetDiscountVirtualTransactionSize ( ctx ) ;
}
2021-07-19 03:15:44 +00:00
return TxSize { vsize , weight } ;
}
2022-07-29 10:35:50 +02:00
size_t CoinsResult : : Size ( ) const
2022-03-11 16:30:04 +01:00
{
2022-07-29 10:35:50 +02:00
size_t size { 0 } ;
for ( const auto & it : coins ) {
size + = it . second . size ( ) ;
}
return size ;
2022-03-11 16:30:04 +01:00
}
2022-08-10 14:35:53 +02:00
std : : vector < COutput > CoinsResult : : All ( ) const
2022-03-11 16:30:04 +01:00
{
std : : vector < COutput > all ;
2022-07-29 10:35:50 +02:00
all . reserve ( coins . size ( ) ) ;
for ( const auto & it : coins ) {
all . insert ( all . end ( ) , it . second . begin ( ) , it . second . end ( ) ) ;
}
2022-03-11 16:30:04 +01:00
return all ;
}
2022-07-29 10:35:50 +02:00
void CoinsResult : : Clear ( ) {
coins . clear ( ) ;
2022-03-11 16:30:04 +01:00
}
2022-11-22 11:39:35 -03:00
void CoinsResult : : Erase ( const std : : unordered_set < COutPoint , SaltedOutpointHasher > & coins_to_remove )
2022-03-11 16:30:04 +01:00
{
2022-11-22 11:39:35 -03:00
for ( auto & [ type , vec ] : coins ) {
auto remove_it = std : : remove_if ( vec . begin ( ) , vec . end ( ) , [ & ] ( const COutput & coin ) {
2022-11-22 11:51:33 -03:00
// remove it if it's on the set
if ( coins_to_remove . count ( coin . outpoint ) = = 0 ) return false ;
// update cached amounts
2025-03-10 00:43:45 +00:00
total_amount [ coin . asset ] - = coin . value ;
if ( coin . HasEffectiveValue ( ) ) total_effective_amount [ coin . asset ] - = coin . GetEffectiveValue ( ) ;
2022-11-22 11:51:33 -03:00
return true ;
2022-11-22 11:39:35 -03:00
} ) ;
vec . erase ( remove_it , vec . end ( ) ) ;
2022-07-29 10:01:25 +02:00
}
}
void CoinsResult : : Shuffle ( FastRandomContext & rng_fast )
{
for ( auto & it : coins ) {
: : Shuffle ( it . second . begin ( ) , it . second . end ( ) , rng_fast ) ;
}
}
void CoinsResult : : Add ( OutputType type , const COutput & out )
2022-03-11 16:30:04 +01:00
{
2022-07-29 10:01:25 +02:00
coins [ type ] . emplace_back ( out ) ;
2025-03-10 00:43:45 +00:00
total_amount [ out . asset ] + = out . value ;
2022-11-22 11:51:33 -03:00
if ( out . HasEffectiveValue ( ) ) {
2025-04-17 09:19:21 +02:00
total_effective_amount [ out . asset ] + = out . GetEffectiveValue ( ) ;
2022-11-22 11:51:33 -03:00
}
2022-07-29 10:01:25 +02:00
}
static OutputType GetOutputType ( TxoutType type , bool is_from_p2sh )
{
switch ( type ) {
case TxoutType : : WITNESS_V1_TAPROOT :
return OutputType : : BECH32M ;
case TxoutType : : WITNESS_V0_KEYHASH :
case TxoutType : : WITNESS_V0_SCRIPTHASH :
if ( is_from_p2sh ) return OutputType : : P2SH_SEGWIT ;
else return OutputType : : BECH32 ;
case TxoutType : : SCRIPTHASH :
case TxoutType : : PUBKEYHASH :
return OutputType : : LEGACY ;
default :
return OutputType : : UNKNOWN ;
}
2022-03-11 16:30:04 +01:00
}
2022-07-22 16:16:44 -03:00
// Fetch and validate the coin control selected inputs.
// Coins could be internal (from the wallet) or external.
util : : Result < PreSelectedInputs > FetchSelectedInputs ( const CWallet & wallet , const CCoinControl & coin_control ,
const CoinSelectionParams & coin_selection_params ) EXCLUSIVE_LOCKS_REQUIRED ( wallet . cs_wallet )
{
PreSelectedInputs result ;
std : : vector < COutPoint > vPresetInputs ;
coin_control . ListSelected ( vPresetInputs ) ;
2023-02-23 18:30:12 +01:00
const bool can_grind_r = wallet . CanGrindR ( ) ;
2022-07-22 16:16:44 -03:00
for ( const COutPoint & outpoint : vPresetInputs ) {
int input_bytes = - 1 ;
CTxOut txout ;
if ( auto ptr_wtx = wallet . GetWalletTx ( outpoint . hash ) ) {
// Clearly invalid input, fail
if ( ptr_wtx - > tx - > vout . size ( ) < = outpoint . n ) {
return util : : Error { strprintf ( _ ( " Invalid pre-selected input %s " ) , outpoint . ToString ( ) ) } ;
}
txout = ptr_wtx - > tx - > vout . at ( outpoint . n ) ;
input_bytes = CalculateMaximumSignedInputSize ( txout , & wallet , & coin_control ) ;
} else {
// The input is external. We did not find the tx in mapWallet.
if ( ! coin_control . GetExternalOutput ( outpoint , txout ) ) {
return util : : Error { strprintf ( _ ( " Not found pre-selected input %s " ) , outpoint . ToString ( ) ) } ;
}
}
if ( input_bytes = = - 1 ) {
2023-02-23 18:30:12 +01:00
input_bytes = CalculateMaximumSignedInputSize ( txout , outpoint , & coin_control . m_external_provider , can_grind_r , & coin_control ) ;
2025-02-19 12:10:38 +02:00
// ELEMENTS: one more try to get a signed input size: for pegins,
// the outpoint is provided as external data but the information
// needed to spend is in the wallet (not the external provider,
// as the user is expecting the wallet to remember this information
// after they called getpeginaddress). So try estimating size with
// the wallet rather than the external provider.
if ( input_bytes = = - 1 ) {
input_bytes = CalculateMaximumSignedInputSize ( txout , & wallet , & coin_control ) ;
}
if ( ! txout . nValue . IsExplicit ( ) | | ! txout . nAsset . IsExplicit ( ) ) {
return util : : Error { strprintf ( _ ( " Value or asset is not explicit for pre-selected input %s " ) , outpoint . ToString ( ) ) } ;
}
2022-07-22 16:16:44 -03:00
}
// If available, override calculated size with coin control specified size
if ( coin_control . HasInputWeight ( outpoint ) ) {
input_bytes = GetVirtualTransactionSize ( coin_control . GetInputWeight ( outpoint ) , 0 , 0 ) ;
}
if ( input_bytes = = - 1 ) {
return util : : Error { strprintf ( _ ( " Not solvable pre-selected input %s " ) , outpoint . ToString ( ) ) } ; // Not solvable, can't estimate size for fee
}
/* Set some defaults for depth, spendable, solvable, safe, time, and from_me as these don't matter for preset inputs since no selection is being done. */
COutput output ( outpoint , txout , /*depth=*/ 0 , input_bytes , /*spendable=*/ true , /*solvable=*/ true , /*safe=*/ true , /*time=*/ 0 , /*from_me=*/ false , coin_selection_params . m_effective_feerate ) ;
2025-02-19 12:10:38 +02:00
// ELEMENTS: use the extended COutput constructor if possible
if ( auto wtx = wallet . GetWalletTx ( outpoint . hash ) ) {
output = COutput ( wallet , * wtx , outpoint , txout , /*depth=*/ 0 , input_bytes , /*spendable=*/ true , /*solvable=*/ true , /*safe=*/ true , /*time=*/ 0 , /*from_me=*/ false , coin_selection_params . m_effective_feerate ) ;
}
2022-07-22 16:16:44 -03:00
result . Insert ( output , coin_selection_params . m_subtract_fee_outputs ) ;
}
return result ;
}
2022-04-22 17:31:28 -03:00
CoinsResult AvailableCoins ( const CWallet & wallet ,
2024-09-13 04:30:16 +00:00
const CCoinControl * coinControl ,
2022-04-22 17:31:28 -03:00
std : : optional < CFeeRate > feerate ,
2025-02-28 12:05:54 +02:00
const CoinFilterParams & params )
2021-02-10 16:06:01 -05:00
{
2021-02-12 18:01:22 -05:00
AssertLockHeld ( wallet . cs_wallet ) ;
2021-02-10 16:06:01 -05:00
2022-04-22 17:31:28 -03:00
CoinsResult result ;
2021-02-10 16:06:01 -05:00
// Either the WALLET_FLAG_AVOID_REUSE flag is not set (in which case we always allow), or we default to avoiding, and only in the case where
// a coin control object is provided, and has the avoid address reuse flag set to false, do we allow already used addresses
2021-02-12 18:01:22 -05:00
bool allow_used_addresses = ! wallet . IsWalletFlagSet ( WALLET_FLAG_AVOID_REUSE ) | | ( coinControl & & ! coinControl - > m_avoid_address_reuse ) ;
2021-02-10 16:06:01 -05:00
const int min_depth = { coinControl ? coinControl - > m_min_depth : DEFAULT_MIN_DEPTH } ;
const int max_depth = { coinControl ? coinControl - > m_max_depth : DEFAULT_MAX_DEPTH } ;
const bool only_safe = { coinControl ? ! coinControl - > m_include_unsafe_inputs : true } ;
2023-02-23 18:30:12 +01:00
const bool can_grind_r = wallet . CanGrindR ( ) ;
2021-02-10 16:06:01 -05:00
std : : set < uint256 > trusted_parents ;
2021-02-12 18:01:22 -05:00
for ( const auto & entry : wallet . mapWallet )
2021-02-10 16:06:01 -05:00
{
const uint256 & wtxid = entry . first ;
const CWalletTx & wtx = entry . second ;
2025-02-28 12:05:54 +02:00
if ( wallet . IsTxImmatureCoinBase ( wtx ) & & ! params . include_immature_coinbase )
2021-02-10 16:06:01 -05:00
continue ;
2021-02-12 18:01:22 -05:00
int nDepth = wallet . GetTxDepthInMainChain ( wtx ) ;
2021-02-10 16:06:01 -05:00
if ( nDepth < 0 )
continue ;
// We should not consider coins which aren't at least in our mempool
// It's possible for these to be conflicted via ancestors which we may never be able to detect
if ( nDepth = = 0 & & ! wtx . InMempool ( ) )
continue ;
2021-02-12 18:01:22 -05:00
bool safeTx = CachedTxIsTrusted ( wallet , wtx , trusted_parents ) ;
2021-02-10 16:06:01 -05:00
// We should not consider coins from transactions that are replacing
// other transactions.
//
// Example: There is a transaction A which is replaced by bumpfee
// transaction B. In this case, we want to prevent creation of
// a transaction B' which spends an output of B.
//
// Reason: If transaction A were initially confirmed, transactions B
// and B' would no longer be valid, so the user would have to create
// a new transaction C to replace B'. However, in the case of a
// one-block reorg, transactions B' and C might BOTH be accepted,
// when the user only wanted one of them. Specifically, there could
// be a 1-block reorg away from the chain where transactions A and C
// were accepted to another chain where B, B', and C were all
// accepted.
if ( nDepth = = 0 & & wtx . mapValue . count ( " replaces_txid " ) ) {
safeTx = false ;
}
// Similarly, we should not consider coins from transactions that
// have been replaced. In the example above, we would want to prevent
// creation of a transaction A' spending an output of A, because if
// transaction B were initially confirmed, conflicting with A and
// A', we wouldn't want to the user to create a transaction D
// intending to replace A', but potentially resulting in a scenario
// where A, A', and D could all be accepted (instead of just B and
// D, or just A and A' like the user would want).
if ( nDepth = = 0 & & wtx . mapValue . count ( " replaced_by_txid " ) ) {
safeTx = false ;
}
if ( only_safe & & ! safeTx ) {
continue ;
}
if ( nDepth < min_depth | | nDepth > max_depth ) {
continue ;
}
2022-01-18 19:03:23 -05:00
bool tx_from_me = CachedTxIsFromMe ( wallet , wtx , ISMINE_ALL ) ;
2021-02-10 16:06:01 -05:00
for ( unsigned int i = 0 ; i < wtx . tx - > vout . size ( ) ; i + + ) {
2022-04-27 10:34:19 -03:00
const CTxOut & output = wtx . tx - > vout [ i ] ;
const COutPoint outpoint ( wtxid , i ) ;
2024-02-12 06:25:27 +00:00
2023-04-15 07:04:22 +00:00
CAmount outValue = wtx . GetOutputValueOut ( wallet , i ) ;
CAsset asset = wtx . GetOutputAsset ( wallet , i ) ;
2025-02-28 12:05:54 +02:00
if ( params . asset & & asset ! = * params . asset ) {
2021-07-19 03:15:44 +00:00
continue ;
}
2025-02-28 12:05:54 +02:00
if ( outValue < params . min_amount | | ( asset = = Params ( ) . GetConsensus ( ) . pegged_asset & & outValue > params . max_amount ) ) {
2021-02-10 16:06:01 -05:00
continue ;
2025-01-14 15:22:55 +02:00
}
2021-02-10 16:06:01 -05:00
2022-08-05 12:08:18 -03:00
// Skip manually selected coins (the caller can fetch them directly)
if ( coinControl & & coinControl - > HasSelected ( ) & & coinControl - > IsSelected ( outpoint ) )
2021-02-10 16:06:01 -05:00
continue ;
2022-07-28 16:57:58 -03:00
if ( wallet . IsLockedCoin ( outpoint ) & & params . skip_locked )
2021-02-10 16:06:01 -05:00
continue ;
2022-04-27 10:52:30 -03:00
if ( wallet . IsSpent ( outpoint ) )
2021-02-10 16:06:01 -05:00
continue ;
2022-04-27 10:34:19 -03:00
isminetype mine = wallet . IsMine ( output ) ;
2021-02-10 16:06:01 -05:00
if ( mine = = ISMINE_NO ) {
continue ;
}
2022-04-27 11:04:31 -03:00
if ( ! allow_used_addresses & & wallet . IsSpentKey ( output . scriptPubKey ) ) {
2021-02-10 16:06:01 -05:00
continue ;
}
2022-04-27 10:34:19 -03:00
std : : unique_ptr < SigningProvider > provider = wallet . GetSolvingProvider ( output . scriptPubKey ) ;
2021-02-10 16:06:01 -05:00
2023-02-23 18:30:12 +01:00
int input_bytes = CalculateMaximumSignedInputSize ( output , COutPoint ( ) , provider . get ( ) , can_grind_r , coinControl ) ;
2022-08-12 11:19:25 +02:00
bool solvable = provider ? InferDescriptor ( output . scriptPubKey , * provider ) - > IsSolvable ( ) : false ;
2021-02-10 16:06:01 -05:00
bool spendable = ( ( mine & ISMINE_SPENDABLE ) ! = ISMINE_NO ) | | ( ( ( mine & ISMINE_WATCH_ONLY ) ! = ISMINE_NO ) & & ( coinControl & & coinControl - > fAllowWatchOnly & & solvable ) ) ;
wallet: add 'only_spendable' filter to AvailableCoins
We are skipping the non-spendable coins that appear in vCoins ('AvailableCoins' result) later, in several parts of the CreateTransaction and GetBalance flows:
GetAvailableBalance (1) gets all the available coins calling AvailableCoins and, right away, walk through the entire vector, skipping the non-spendable coins, to calculate the total balance.
Inside CreateTransactionInternal —> SelectCoins(vCoins,...), we have several calls to AttemptSelection which, on each of them internally, we call twice to GroupOutputs which internally has two for-loops over the entire vCoins vector that skip the non-spendable coins.
So, Purpose is not add the non-spendable coins into the AvailableCoins result (vCoins) in the first place for the processes that aren’t using them at all, so we don’t waste resources skipping them later so many times.
Note: this speedup is for all the processes that call to CreateTransaction and GetBalance* internally.
2022-06-02 14:45:04 -03:00
// Filter by spendable outputs only
2025-02-28 12:05:54 +02:00
if ( ! spendable & & params . only_spendable ) continue ;
wallet: add 'only_spendable' filter to AvailableCoins
We are skipping the non-spendable coins that appear in vCoins ('AvailableCoins' result) later, in several parts of the CreateTransaction and GetBalance flows:
GetAvailableBalance (1) gets all the available coins calling AvailableCoins and, right away, walk through the entire vector, skipping the non-spendable coins, to calculate the total balance.
Inside CreateTransactionInternal —> SelectCoins(vCoins,...), we have several calls to AttemptSelection which, on each of them internally, we call twice to GroupOutputs which internally has two for-loops over the entire vCoins vector that skip the non-spendable coins.
So, Purpose is not add the non-spendable coins into the AvailableCoins result (vCoins) in the first place for the processes that aren’t using them at all, so we don’t waste resources skipping them later so many times.
Note: this speedup is for all the processes that call to CreateTransaction and GetBalance* internally.
2022-06-02 14:45:04 -03:00
2022-08-25 17:23:02 -03:00
// Obtain script type
std : : vector < std : : vector < uint8_t > > script_solutions ;
TxoutType type = Solver ( output . scriptPubKey , script_solutions ) ;
// If the output is P2SH and solvable, we want to know if it is
2022-03-11 16:30:04 +01:00
// a P2SH (legacy) or one of P2SH-P2WPKH, P2SH-P2WSH (P2SH-Segwit). We can determine
2022-08-25 17:23:02 -03:00
// this from the redeemScript. If the output is not solvable, it will be classified
2022-03-11 16:30:04 +01:00
// as a P2SH (legacy), since we have no way of knowing otherwise without the redeemScript
2022-07-29 10:35:50 +02:00
bool is_from_p2sh { false } ;
2022-08-25 17:23:02 -03:00
if ( type = = TxoutType : : SCRIPTHASH & & solvable ) {
CScript script ;
if ( ! provider - > GetCScript ( CScriptID ( uint160 ( script_solutions [ 0 ] ) ) , script ) ) continue ;
type = Solver ( script , script_solutions ) ;
2022-03-11 16:30:04 +01:00
is_from_p2sh = true ;
}
2022-08-25 17:23:02 -03:00
result . Add ( GetOutputType ( type , is_from_p2sh ) ,
2025-02-05 09:50:17 +02:00
COutput ( wallet , wtx , outpoint , output , nDepth , input_bytes , spendable , solvable , safeTx , wtx . GetTxTime ( ) , tx_from_me , feerate ) ) ;
2021-02-10 16:06:01 -05:00
// Checks the sum amount of all UTXO's.
2025-02-28 12:05:54 +02:00
if ( params . min_sum_amount ! = MAX_MONEY ) {
2025-04-17 09:19:21 +02:00
if ( result . GetTotalAmount ( ) [ : : policyAsset ] > = params . min_sum_amount ) { // ELEMENTS: only use minimum sum for policy asset
2025-02-28 12:05:54 +02:00
return result ;
2021-02-10 16:06:01 -05:00
}
}
// Checks the maximum number of UTXO's.
2025-02-28 12:05:54 +02:00
if ( params . max_count > 0 & & result . Size ( ) > = params . max_count ) {
2022-04-22 17:31:28 -03:00
return result ;
2021-02-10 16:06:01 -05:00
}
}
}
2022-04-22 17:31:28 -03:00
return result ;
2021-02-10 16:06:01 -05:00
}
2025-02-28 12:05:54 +02:00
CoinsResult AvailableCoinsListUnspent ( const CWallet & wallet , const CCoinControl * coinControl , CoinFilterParams params )
2022-04-24 18:01:58 -04:00
{
2025-02-28 12:05:54 +02:00
params . only_spendable = false ;
return AvailableCoins ( wallet , coinControl , /*feerate=*/ std : : nullopt , params ) ;
2022-04-24 18:01:58 -04:00
}
2022-07-20 13:24:05 -03:00
const CTxOut & FindNonChangeParentOutput ( const CWallet & wallet , const COutPoint & outpoint )
2021-02-10 16:06:01 -05:00
{
2021-02-12 18:01:22 -05:00
AssertLockHeld ( wallet . cs_wallet ) ;
2022-07-20 13:24:05 -03:00
const CWalletTx * wtx { Assert ( wallet . GetWalletTx ( outpoint . hash ) ) } ;
const CTransaction * ptx = wtx - > tx . get ( ) ;
int n = outpoint . n ;
2021-02-12 18:01:22 -05:00
while ( OutputIsChange ( wallet , ptx - > vout [ n ] ) & & ptx - > vin . size ( ) > 0 ) {
2021-02-10 16:06:01 -05:00
const COutPoint & prevout = ptx - > vin [ 0 ] . prevout ;
2022-07-20 13:24:05 -03:00
const CWalletTx * it = wallet . GetWalletTx ( prevout . hash ) ;
if ( ! it | | it - > tx - > vout . size ( ) < = prevout . n | |
! wallet . IsMine ( it - > tx - > vout [ prevout . n ] ) ) {
2021-02-10 16:06:01 -05:00
break ;
}
2022-07-20 13:24:05 -03:00
ptx = it - > tx . get ( ) ;
2021-02-10 16:06:01 -05:00
n = prevout . n ;
}
return ptx - > vout [ n ] ;
}
2023-09-06 06:42:38 +00:00
std : : map < CTxDestination , std : : vector < COutput > > ListCoins ( const CWallet & wallet ) EXCLUSIVE_LOCKS_REQUIRED ( wallet . cs_wallet )
2021-02-10 16:06:01 -05:00
{
2021-02-12 18:01:22 -05:00
AssertLockHeld ( wallet . cs_wallet ) ;
2021-02-10 16:06:01 -05:00
std : : map < CTxDestination , std : : vector < COutput > > result ;
2022-07-20 00:19:06 -03:00
CCoinControl coin_control ;
// Include watch-only for LegacyScriptPubKeyMan wallets without private keys
coin_control . fAllowWatchOnly = wallet . GetLegacyScriptPubKeyMan ( ) & & wallet . IsWalletFlagSet ( WALLET_FLAG_DISABLE_PRIVATE_KEYS ) ;
CoinFilterParams coins_params ;
coins_params . only_spendable = false ;
coins_params . skip_locked = false ;
for ( const COutput & coin : AvailableCoins ( wallet , & coin_control , /*feerate=*/ std : : nullopt , coins_params ) . All ( ) ) {
2021-02-10 16:06:01 -05:00
CTxDestination address ;
2024-09-13 04:30:16 +00:00
// Retrieve the transaction from the wallet
const CWalletTx * wtx = wallet . GetWalletTx ( coin . outpoint . hash ) ;
if ( wtx = = nullptr ) {
// Skip this coin if the transaction is not found in the wallet
continue ;
}
2022-03-16 14:38:34 -04:00
if ( ( coin . spendable | | ( wallet . IsWalletFlagSet ( WALLET_FLAG_DISABLE_PRIVATE_KEYS ) & & coin . solvable ) ) & &
2022-01-18 19:08:42 -05:00
ExtractDestination ( FindNonChangeParentOutput ( wallet , coin . outpoint ) . scriptPubKey , address ) ) {
2022-07-20 00:19:06 -03:00
result [ address ] . emplace_back ( coin ) ;
2021-02-10 16:06:01 -05:00
}
}
return result ;
}
2022-08-03 19:04:36 -03:00
FilteredOutputGroups GroupOutputs ( const CWallet & wallet ,
2022-08-02 11:54:20 -03:00
const CoinsResult & coins ,
const CoinSelectionParams & coin_sel_params ,
2023-03-07 18:47:28 -03:00
const std : : vector < SelectionFilter > & filters ,
std : : vector < OutputGroup > & ret_discarded_groups )
2021-02-10 16:06:01 -05:00
{
2022-08-03 19:04:36 -03:00
FilteredOutputGroups filtered_groups ;
2021-02-10 16:06:01 -05:00
if ( ! coin_sel_params . m_avoid_partial_spends ) {
2022-08-02 11:54:20 -03:00
// Allowing partial spends means no grouping. Each COutput gets its own OutputGroup
for ( const auto & [ type , outputs ] : coins . coins ) {
for ( const COutput & output : outputs ) {
// Get mempool info
size_t ancestors , descendants ;
wallet . chain ( ) . getTransactionAncestry ( output . outpoint . hash , ancestors , descendants ) ;
// Create a new group per output and add it to the all groups vector
OutputGroup group ( coin_sel_params ) ;
2022-08-01 16:15:36 -03:00
group . Insert ( std : : make_shared < COutput > ( output ) , ancestors , descendants ) ;
2021-02-10 16:06:01 -05:00
2022-08-03 19:04:36 -03:00
// Each filter maps to a different set of groups
2023-03-07 18:47:28 -03:00
bool accepted = false ;
2022-08-03 19:04:36 -03:00
for ( const auto & sel_filter : filters ) {
const auto & filter = sel_filter . filter ;
if ( ! group . EligibleForSpending ( filter ) ) continue ;
filtered_groups [ filter ] . Push ( group , type , /*insert_positive=*/ true , /*insert_mixed=*/ true ) ;
2023-03-07 18:47:28 -03:00
accepted = true ;
2022-08-03 19:04:36 -03:00
}
2023-03-07 18:47:28 -03:00
if ( ! accepted ) ret_discarded_groups . emplace_back ( group ) ;
2022-07-31 17:22:04 -03:00
}
2021-02-10 16:06:01 -05:00
}
2022-08-03 19:04:36 -03:00
return filtered_groups ;
2021-02-10 16:06:01 -05:00
}
// We want to combine COutputs that have the same scriptPubKey into single OutputGroups
// except when there are more than OUTPUT_GROUP_MAX_ENTRIES COutputs grouped in an OutputGroup.
// To do this, we maintain a map where the key is the scriptPubKey and the value is a vector of OutputGroups.
2022-01-17 17:18:31 -05:00
// For each COutput, we check if the scriptPubKey is in the map, and if it is, the COutput is added
2021-02-10 16:06:01 -05:00
// to the last OutputGroup in the vector for the scriptPubKey. When the last OutputGroup has
2022-01-17 17:18:31 -05:00
// OUTPUT_GROUP_MAX_ENTRIES COutputs, a new OutputGroup is added to the end of the vector.
2022-08-02 11:54:20 -03:00
typedef std : : map < std : : pair < CScript , OutputType > , std : : vector < OutputGroup > > ScriptPubKeyToOutgroup ;
2023-02-13 20:03:28 -03:00
const auto & insert_output = [ & ] (
2023-02-13 19:56:26 -03:00
const std : : shared_ptr < COutput > & output , OutputType type , size_t ancestors , size_t descendants ,
2023-02-13 20:03:28 -03:00
ScriptPubKeyToOutgroup & groups_map ) {
2023-02-13 19:56:26 -03:00
std : : vector < OutputGroup > & groups = groups_map [ std : : make_pair ( output - > txout . scriptPubKey , type ) ] ;
2021-02-10 16:06:01 -05:00
if ( groups . size ( ) = = 0 ) {
// No OutputGroups for this scriptPubKey yet, add one
groups . emplace_back ( coin_sel_params ) ;
}
2022-01-17 17:18:31 -05:00
// Get the last OutputGroup in the vector so that we can add the COutput to it
2021-02-10 16:06:01 -05:00
// A pointer is used here so that group can be reassigned later if it is full.
OutputGroup * group = & groups . back ( ) ;
// Check if this OutputGroup is full. We limit to OUTPUT_GROUP_MAX_ENTRIES when using -avoidpartialspends
// to avoid surprising users with very high fees.
if ( group - > m_outputs . size ( ) > = OUTPUT_GROUP_MAX_ENTRIES ) {
// The last output group is full, add a new group to the vector and use that group for the insertion
groups . emplace_back ( coin_sel_params ) ;
group = & groups . back ( ) ;
}
2023-02-13 20:03:28 -03:00
group - > Insert ( output , ancestors , descendants ) ;
2022-08-02 11:54:20 -03:00
} ;
ScriptPubKeyToOutgroup spk_to_groups_map ;
ScriptPubKeyToOutgroup spk_to_positive_groups_map ;
for ( const auto & [ type , outs ] : coins . coins ) {
for ( const COutput & output : outs ) {
size_t ancestors , descendants ;
wallet . chain ( ) . getTransactionAncestry ( output . outpoint . hash , ancestors , descendants ) ;
2023-02-13 19:56:26 -03:00
const auto & shared_output = std : : make_shared < COutput > ( output ) ;
2023-02-13 20:03:28 -03:00
// Filter for positive only before adding the output
if ( output . GetEffectiveValue ( ) > 0 ) {
insert_output ( shared_output , type , ancestors , descendants , spk_to_positive_groups_map ) ;
}
// 'All' groups
insert_output ( shared_output , type , ancestors , descendants , spk_to_groups_map ) ;
2022-08-02 11:54:20 -03:00
}
2021-02-10 16:06:01 -05:00
}
2022-08-02 11:54:20 -03:00
// Now we go through the entire maps and pull out the OutputGroups
const auto & push_output_groups = [ & ] ( const ScriptPubKeyToOutgroup & groups_map , bool positive_only ) {
for ( const auto & [ script , groups ] : groups_map ) {
// Go through the vector backwards. This allows for the first item we deal with being the partial group.
for ( auto group_it = groups . rbegin ( ) ; group_it ! = groups . rend ( ) ; group_it + + ) {
const OutputGroup & group = * group_it ;
2021-02-10 16:06:01 -05:00
2022-08-03 19:04:36 -03:00
// Each filter maps to a different set of groups
2023-03-07 18:47:28 -03:00
bool accepted = false ;
2022-08-03 19:04:36 -03:00
for ( const auto & sel_filter : filters ) {
const auto & filter = sel_filter . filter ;
if ( ! group . EligibleForSpending ( filter ) ) continue ;
2021-02-10 16:06:01 -05:00
2022-08-03 19:04:36 -03:00
// Don't include partial groups if there are full groups too and we don't want partial groups
if ( group_it = = groups . rbegin ( ) & & groups . size ( ) > 1 & & ! filter . m_include_partial_groups ) {
continue ;
}
2021-02-10 16:06:01 -05:00
2022-08-03 19:04:36 -03:00
OutputType type = script . second ;
// Either insert the group into the positive-only groups or the mixed ones.
filtered_groups [ filter ] . Push ( group , type , positive_only , /*insert_mixed=*/ ! positive_only ) ;
2023-03-07 18:47:28 -03:00
accepted = true ;
2022-08-03 19:04:36 -03:00
}
2023-03-07 18:47:28 -03:00
if ( ! accepted ) ret_discarded_groups . emplace_back ( group ) ;
2022-08-02 11:54:20 -03:00
}
2021-02-10 16:06:01 -05:00
}
2022-08-02 11:54:20 -03:00
} ;
2021-02-10 16:06:01 -05:00
2022-08-02 11:54:20 -03:00
push_output_groups ( spk_to_groups_map , /*positive_only=*/ false ) ;
push_output_groups ( spk_to_positive_groups_map , /*positive_only=*/ true ) ;
2021-02-10 16:06:01 -05:00
2022-08-03 19:04:36 -03:00
return filtered_groups ;
2021-02-10 16:06:01 -05:00
}
2023-03-07 18:47:28 -03:00
FilteredOutputGroups GroupOutputs ( const CWallet & wallet ,
const CoinsResult & coins ,
const CoinSelectionParams & params ,
const std : : vector < SelectionFilter > & filters )
{
std : : vector < OutputGroup > unused ;
return GroupOutputs ( wallet , coins , params , filters , unused ) ;
}
2022-12-07 14:35:46 -03:00
// Returns true if the result contains an error and the message is not empty
static bool HasErrorMsg ( const util : : Result < SelectionResult > & res ) { return ! util : : ErrorString ( res ) . empty ( ) ; }
2025-04-09 14:10:58 +00:00
util : : Result < SelectionResult > AttemptSelection ( const CAmountMap & mapTargetValue , OutputGroupTypeMap & groups ,
2022-03-25 20:57:40 +01:00
const CoinSelectionParams & coin_selection_params , bool allow_mixed_output_types )
2022-04-20 13:37:18 +02:00
{
2022-03-25 20:57:40 +01:00
// Run coin selection on each OutputType and compute the Waste Metric
std : : vector < SelectionResult > results ;
2022-08-03 19:04:36 -03:00
for ( auto & [ type , group ] : groups . groups_by_type ) {
2025-04-09 14:10:58 +00:00
auto result { ChooseSelectionResult ( mapTargetValue , group , coin_selection_params ) } ;
2022-12-07 14:35:46 -03:00
// If any specific error message appears here, then something particularly wrong happened.
if ( HasErrorMsg ( result ) ) return result ; // So let's return the specific error.
// Append the favorable result.
if ( result ) results . push_back ( * result ) ;
2022-03-25 20:57:40 +01:00
}
2022-07-29 12:42:40 +02:00
// If we have at least one solution for funding the transaction without mixing, choose the minimum one according to waste metric
// and return the result
if ( results . size ( ) > 0 ) return * std : : min_element ( results . begin ( ) , results . end ( ) ) ;
// If we can't fund the transaction from any individual OutputType, run coin selection one last time
// over all available coins, which would allow mixing
2022-12-08 15:55:41 -03:00
// If TypesCount() <= 1, there is nothing to mix.
2022-08-03 19:04:36 -03:00
if ( allow_mixed_output_types & & groups . TypesCount ( ) > 1 ) {
2025-04-09 14:10:58 +00:00
return ChooseSelectionResult ( mapTargetValue , groups . all_groups , coin_selection_params ) ;
2022-07-29 12:42:40 +02:00
}
// Either mixing is not allowed and we couldn't find a solution from any single OutputType, or mixing was allowed and we still couldn't
// find a solution using all available coins
2022-08-08 15:18:45 -03:00
return util : : Error ( ) ;
2022-04-20 13:37:18 +02:00
} ;
2025-04-09 14:10:58 +00:00
util : : Result < SelectionResult > ChooseSelectionResult ( const CAmountMap & mapTargetValue , Groups & groups , const CoinSelectionParams & coin_selection_params )
2021-02-10 16:06:01 -05:00
{
2021-05-21 18:15:55 -04:00
// Vector of results. We will choose the best one based on waste.
2023-06-07 12:07:55 +00:00
// std::vector<std::tuple<CAmount, std::set<CInputCoin>, CAmountMap>> results;
2021-05-21 18:15:55 -04:00
std : : vector < SelectionResult > results ;
2021-07-19 03:15:44 +00:00
2023-04-12 14:52:10 +02:00
// ELEMENTS: BnB only for policy asset?
2021-07-19 03:15:44 +00:00
if ( mapTargetValue . size ( ) = = 1 ) {
// Note that unlike KnapsackSolver, we do not include the fee for creating a change output as BnB will not create a change output.
// ELEMENTS:
CAsset asset = mapTargetValue . begin ( ) - > first ;
CAmount nTargetValue = mapTargetValue . begin ( ) - > second ;
2024-10-10 03:57:36 +00:00
CAmount target_with_change = nTargetValue ;
// While nTargetValue includes the transaction fees for non-input things, it does not include the fee for creating a change output.
// So we need to include that for KnapsackSolver and SRD as well, as we are expecting to create a change output.
if ( ! coin_selection_params . m_subtract_fee_outputs ) {
target_with_change + = coin_selection_params . m_change_fee ;
}
2021-07-19 03:15:44 +00:00
// Get output groups that only contain this asset.
std : : vector < OutputGroup > asset_groups ;
2025-04-09 14:10:58 +00:00
for ( const OutputGroup & g : groups . positive_group ) {
2021-07-19 03:15:44 +00:00
bool add = true ;
2025-04-09 14:10:58 +00:00
for ( const std : : shared_ptr < wallet : : COutput > & c : g . m_outputs ) {
if ( c - > asset ! = asset ) {
2021-07-19 03:15:44 +00:00
add = false ;
break ;
}
}
2021-02-10 16:06:01 -05:00
2021-07-19 03:15:44 +00:00
if ( add ) {
asset_groups . push_back ( g ) ;
}
}
// END ELEMENTS
2025-04-09 14:10:58 +00:00
if ( auto bnb_result { SelectCoinsBnB ( groups . positive_group , nTargetValue , coin_selection_params . m_cost_of_change ) } ) {
2023-06-07 12:07:55 +00:00
results . push_back ( * bnb_result ) ;
2021-07-19 03:15:44 +00:00
}
2021-02-10 16:06:01 -05:00
2024-04-08 05:13:29 +00:00
// Include change for SRD as we want to avoid making really small change if the selection just
// barely meets the target. Just use the lower bound change target instead of the randomly
// generated one, since SRD will result in a random change amount anyway; avoid making the
// target needlessly large.
2024-10-10 03:57:36 +00:00
const CAmount srd_target = target_with_change + CHANGE_LOWER ;
2025-04-09 14:10:58 +00:00
if ( auto srd_result { SelectCoinsSRD ( groups . positive_group , srd_target , coin_selection_params . rng_fast ) } ) {
2024-11-18 15:32:37 +02:00
srd_result - > ComputeAndSetWaste ( coin_selection_params . min_viable_change , coin_selection_params . m_cost_of_change , coin_selection_params . m_change_fee ) ;
2023-06-07 12:07:55 +00:00
results . push_back ( * srd_result ) ;
2023-04-24 22:29:06 +00:00
}
2021-02-10 16:06:01 -05:00
}
2021-05-20 19:52:18 -04:00
2021-02-10 16:06:01 -05:00
// The knapsack solver has some legacy behavior where it will spend dust outputs. We retain this behavior, so don't filter for positive only here.
2021-07-19 03:15:44 +00:00
// While mapTargetValue includes the transaction fees for non-input things, it does not include the fee for creating a change output.
2021-02-10 16:06:01 -05:00
// So we need to include that for KnapsackSolver as well, as we are expecting to create a change output.
2021-07-19 03:15:44 +00:00
CAmountMap mapTargetValue_copy = mapTargetValue ;
if ( ! coin_selection_params . m_subtract_fee_outputs ) {
mapTargetValue_copy [ : : policyAsset ] + = coin_selection_params . m_change_fee ;
}
2021-05-20 19:52:18 -04:00
2024-10-10 03:57:36 +00:00
CAmountMap map_target_with_change = mapTargetValue ;
2021-02-10 16:06:01 -05:00
// While nTargetValue includes the transaction fees for non-input things, it does not include the fee for creating a change output.
2022-04-05 08:23:49 +02:00
// So we need to include that for KnapsackSolver and SRD as well, as we are expecting to create a change output.
if ( ! coin_selection_params . m_subtract_fee_outputs ) {
2024-10-10 03:57:36 +00:00
map_target_with_change [ : : policyAsset ] + = coin_selection_params . m_change_fee ;
2022-04-05 08:23:49 +02:00
}
2025-04-09 14:10:58 +00:00
if ( auto knapsack_result { KnapsackSolver ( groups . mixed_group , mapTargetValue , coin_selection_params . m_min_change_target , coin_selection_params . rng_fast ) } ) {
2024-11-18 15:32:37 +02:00
knapsack_result - > ComputeAndSetWaste ( coin_selection_params . min_viable_change , coin_selection_params . m_cost_of_change , coin_selection_params . m_change_fee ) ;
results . push_back ( * knapsack_result ) ;
2021-05-20 19:52:18 -04:00
}
2022-10-04 14:17:11 +02:00
if ( results . empty ( ) ) {
2021-05-20 19:52:18 -04:00
// No solution found
2022-08-08 15:18:45 -03:00
return util : : Error ( ) ;
2021-05-20 19:52:18 -04:00
}
2022-10-04 14:17:11 +02:00
std : : vector < SelectionResult > eligible_results ;
std : : copy_if ( results . begin ( ) , results . end ( ) , std : : back_inserter ( eligible_results ) , [ coin_selection_params ] ( const SelectionResult & result ) {
const auto initWeight { coin_selection_params . tx_noinputs_size * WITNESS_SCALE_FACTOR } ;
return initWeight + result . GetWeight ( ) < = static_cast < int > ( MAX_STANDARD_TX_WEIGHT ) ;
} ) ;
if ( eligible_results . empty ( ) ) {
2022-12-07 14:35:46 -03:00
return util : : Error { _ ( " The inputs size exceeds the maximum weight. "
" Please try sending a smaller amount or manually consolidating your wallet's UTXOs " ) } ;
2022-10-04 14:17:11 +02:00
}
2021-05-20 19:52:18 -04:00
// Choose the result with the least waste
// If the waste is the same, choose the one which spends more inputs.
2022-10-04 14:17:11 +02:00
auto & best_result = * std : : min_element ( eligible_results . begin ( ) , eligible_results . end ( ) ) ;
2021-05-21 18:39:41 -04:00
return best_result ;
2021-02-10 16:06:01 -05:00
}
2022-08-08 15:18:45 -03:00
util : : Result < SelectionResult > SelectCoins ( const CWallet & wallet , CoinsResult & available_coins , const PreSelectedInputs & pre_set_inputs ,
2025-04-01 09:43:22 +02:00
const CAmountMap & mapTargetValue , const CCoinControl & coin_control ,
2022-08-08 15:18:45 -03:00
const CoinSelectionParams & coin_selection_params )
2021-02-10 16:06:01 -05:00
{
2023-04-15 07:04:22 +00:00
AssertLockHeld ( wallet . cs_wallet ) ;
2022-10-04 11:04:48 -03:00
// Deduct preset inputs amount from the search target
2025-02-19 12:10:38 +02:00
CAmountMap selection_target = mapTargetValue - pre_set_inputs . total_amount ;
2021-07-19 03:15:44 +00:00
2022-08-24 15:03:13 -03:00
// Return if automatic coin selection is disabled, and we don't cover the selection target
2025-04-01 09:43:22 +02:00
if ( ! coin_control . m_allow_other_inputs & & selection_target > CAmountMap { } ) {
2022-12-07 14:35:46 -03:00
return util : : Error { _ ( " The preselected coins total amount does not cover the transaction target. "
" Please allow other inputs to be automatically selected or include more coins manually " ) } ;
}
2021-02-10 16:06:01 -05:00
2022-08-24 15:03:13 -03:00
// Return if we can cover the target only with the preset inputs
2025-02-19 12:10:38 +02:00
if ( selection_target < = CAmountMap { } ) {
2024-09-16 01:08:02 +00:00
SelectionResult result ( mapTargetValue , SelectionAlgorithm : : MANUAL ) ;
2022-08-05 12:51:44 -03:00
result . AddInputs ( pre_set_inputs . coins , coin_selection_params . m_subtract_fee_outputs ) ;
2022-07-06 09:03:01 +02:00
result . ComputeAndSetWaste ( coin_selection_params . min_viable_change , coin_selection_params . m_cost_of_change , coin_selection_params . m_change_fee ) ;
2022-05-19 09:48:49 -03:00
return result ;
}
2025-03-10 00:43:45 +00:00
CAmountMap available_coins_total_amount = coin_selection_params . m_subtract_fee_outputs ? available_coins . GetTotalAmount ( ) : available_coins . GetEffectiveTotalAmount ( ) ;
2022-11-22 11:51:33 -03:00
if ( selection_target > available_coins_total_amount ) {
2022-08-08 15:18:45 -03:00
return util : : Error ( ) ; // Insufficient funds
2022-11-22 11:51:33 -03:00
}
2022-10-04 11:04:48 -03:00
// Start wallet Coin Selection procedure
2023-03-08 19:03:40 -03:00
auto op_selection_result = AutomaticCoinSelection ( wallet , available_coins , selection_target , coin_selection_params ) ;
2022-08-05 12:51:44 -03:00
if ( ! op_selection_result ) return op_selection_result ;
2022-08-24 15:03:13 -03:00
// If needed, add preset inputs to the automatic coin selection result
if ( ! pre_set_inputs . coins . empty ( ) ) {
SelectionResult preselected ( pre_set_inputs . total_amount , SelectionAlgorithm : : MANUAL ) ;
preselected . AddInputs ( pre_set_inputs . coins , coin_selection_params . m_subtract_fee_outputs ) ;
op_selection_result - > Merge ( preselected ) ;
op_selection_result - > ComputeAndSetWaste ( coin_selection_params . min_viable_change ,
coin_selection_params . m_cost_of_change ,
coin_selection_params . m_change_fee ) ;
2021-02-10 16:06:01 -05:00
}
2022-08-05 12:51:44 -03:00
return op_selection_result ;
}
2021-02-10 16:06:01 -05:00
2025-04-10 09:28:08 +02:00
util : : Result < SelectionResult > AutomaticCoinSelection ( const CWallet & wallet , CoinsResult & available_coins , const CAmountMap & value_to_select , const CoinSelectionParams & coin_selection_params )
2022-08-05 12:51:44 -03:00
{
2021-02-10 16:06:01 -05:00
unsigned int limit_ancestor_count = 0 ;
unsigned int limit_descendant_count = 0 ;
2021-02-12 18:01:22 -05:00
wallet . chain ( ) . getPackageLimits ( limit_ancestor_count , limit_descendant_count ) ;
2021-02-10 16:06:01 -05:00
const size_t max_ancestors = ( size_t ) std : : max < int64_t > ( 1 , limit_ancestor_count ) ;
const size_t max_descendants = ( size_t ) std : : max < int64_t > ( 1 , limit_descendant_count ) ;
const bool fRejectLongChains = gArgs . GetBoolArg ( " -walletrejectlongchains " , DEFAULT_WALLET_REJECT_LONG_CHAINS ) ;
2021-07-19 03:15:44 +00:00
// ELEMENTS: filter coins for assets we are interested in; always keep policyAsset for fees
2025-03-10 00:43:45 +00:00
std : : unordered_set < COutPoint , SaltedOutpointHasher > outpoints ;
2024-11-02 17:58:55 +02:00
for ( const auto & output : available_coins . All ( ) ) {
2025-02-19 12:10:38 +02:00
if ( output . asset ! = : : policyAsset & & value_to_select . find ( output . asset ) = = value_to_select . end ( ) ) {
2024-11-02 17:58:55 +02:00
outpoints . emplace ( output . outpoint ) ;
}
}
available_coins . Erase ( outpoints ) ;
2021-07-19 03:15:44 +00:00
2021-02-10 16:06:01 -05:00
// form groups from remaining coins; note that preset coins will not
// automatically have their associated (same address) coins included
2023-03-08 19:03:40 -03:00
if ( coin_selection_params . m_avoid_partial_spends & & available_coins . Size ( ) > OUTPUT_GROUP_MAX_ENTRIES ) {
2021-02-10 16:06:01 -05:00
// Cases where we have 101+ outputs all pointing to the same destination may result in
// privacy leaks as they will potentially be deterministically sorted. We solve that by
// explicitly shuffling the outputs before processing
2022-07-29 10:35:50 +02:00
available_coins . Shuffle ( coin_selection_params . rng_fast ) ;
2021-02-10 16:06:01 -05:00
}
// Coin Selection attempts to select inputs from a pool of eligible UTXOs to fund the
// transaction at a target feerate. If an attempt fails, more attempts may be made using a more
// permissive CoinEligibilityFilter.
2022-08-08 15:18:45 -03:00
util : : Result < SelectionResult > res = [ & ] {
2022-12-06 20:11:57 -03:00
// Place coins eligibility filters on a scope increasing order.
std : : vector < SelectionFilter > ordered_filters {
// If possible, fund the transaction with confirmed UTXOs only. Prefer at least six
// confirmations on outputs received from other wallets and only spend confirmed change.
{ CoinEligibilityFilter ( 1 , 6 , 0 ) , /*allow_mixed_output_types=*/ false } ,
{ CoinEligibilityFilter ( 1 , 1 , 0 ) } ,
} ;
2021-02-10 16:06:01 -05:00
// Fall back to using zero confirmation change (but with as few ancestors in the mempool as
// possible) if we cannot fund the transaction otherwise.
2023-04-15 07:04:22 +00:00
if ( wallet . m_spend_zero_conf_change ) {
2022-12-06 20:11:57 -03:00
ordered_filters . push_back ( { CoinEligibilityFilter ( 0 , 1 , 2 ) } ) ;
2021-12-18 12:50:58 -05:00
ordered_filters . push_back ( { CoinEligibilityFilter ( 0 , 1 , std : : min ( size_t { 4 } , max_ancestors / 3 ) , std : : min ( size_t { 4 } , max_descendants / 3 ) ) } ) ;
2022-12-06 20:11:57 -03:00
ordered_filters . push_back ( { CoinEligibilityFilter ( 0 , 1 , max_ancestors / 2 , max_descendants / 2 ) } ) ;
2021-02-10 16:06:01 -05:00
// If partial groups are allowed, relax the requirement of spending OutputGroups (groups
// of UTXOs sent to the same address, which are obviously controlled by a single wallet)
// in their entirety.
2022-12-06 20:11:57 -03:00
ordered_filters . push_back ( { CoinEligibilityFilter ( 0 , 1 , max_ancestors - 1 , max_descendants - 1 , /*include_partial=*/ true ) } ) ;
2021-02-10 16:06:01 -05:00
// Try with unsafe inputs if they are allowed. This may spend unconfirmed outputs
// received from other wallets.
2023-03-08 19:03:40 -03:00
if ( coin_selection_params . m_include_unsafe_inputs ) {
2022-12-06 20:11:57 -03:00
ordered_filters . push_back ( { CoinEligibilityFilter ( /*conf_mine=*/ 0 , /*conf_theirs*/ 0 , max_ancestors - 1 , max_descendants - 1 , /*include_partial=*/ true ) } ) ;
2021-02-10 16:06:01 -05:00
}
// Try with unlimited ancestors/descendants. The transaction will still need to meet
// mempool ancestor/descendant policy to be accepted to mempool and broadcasted, but
// OutputGroups use heuristics that may overestimate ancestor/descendant counts.
2021-05-21 18:39:41 -04:00
if ( ! fRejectLongChains ) {
2022-12-06 20:11:57 -03:00
ordered_filters . push_back ( { CoinEligibilityFilter ( 0 , 1 , std : : numeric_limits < uint64_t > : : max ( ) ,
std : : numeric_limits < uint64_t > : : max ( ) ,
/*include_partial=*/ true ) } ) ;
2021-02-10 16:06:01 -05:00
}
}
2022-12-06 20:11:57 -03:00
2022-08-03 19:04:36 -03:00
// Group outputs and map them by coin eligibility filter
2023-03-07 18:47:28 -03:00
std : : vector < OutputGroup > discarded_groups ;
FilteredOutputGroups filtered_groups = GroupOutputs ( wallet , available_coins , coin_selection_params , ordered_filters , discarded_groups ) ;
// Check if we still have enough balance after applying filters (some coins might be discarded)
CAmount total_discarded = 0 ;
CAmount total_unconf_long_chain = 0 ;
for ( const auto & group : discarded_groups ) {
total_discarded + = group . GetSelectionAmount ( ) ;
if ( group . m_ancestors > = max_ancestors | | group . m_descendants > = max_descendants ) total_unconf_long_chain + = group . GetSelectionAmount ( ) ;
}
2025-04-11 21:43:11 +02:00
if ( CAmount total_amount = available_coins . GetTotalAmount ( ) - CAmountMap { { : : policyAsset , total_discarded } } < value_to_select ) { // ELEMENTS FIXME: check this
2023-03-07 18:47:28 -03:00
// Special case, too-long-mempool cluster.
2025-04-11 21:43:11 +02:00
if ( CAmountMap { { : : policyAsset , total_amount + total_unconf_long_chain } } > value_to_select ) {
2023-03-07 18:47:28 -03:00
return util : : Result < SelectionResult > ( { _ ( " Unconfirmed UTXOs are available, but spending them creates a chain of transactions that will be rejected by the mempool " ) } ) ;
}
return util : : Result < SelectionResult > ( util : : Error ( ) ) ; // General "Insufficient Funds"
}
2022-08-03 19:04:36 -03:00
2022-12-07 14:35:46 -03:00
// Walk-through the filters until the solution gets found.
// If no solution is found, return the first detailed error (if any).
// future: add "error level" so the worst one can be picked instead.
std : : vector < util : : Result < SelectionResult > > res_detailed_errors ;
2022-12-06 20:11:57 -03:00
for ( const auto & select_filter : ordered_filters ) {
2022-08-03 19:04:36 -03:00
auto it = filtered_groups . find ( select_filter . filter ) ;
if ( it = = filtered_groups . end ( ) ) continue ;
if ( auto res { AttemptSelection ( value_to_select , it - > second ,
2022-12-07 14:35:46 -03:00
coin_selection_params , select_filter . allow_mixed_output_types ) } ) {
return res ; // result found
} else {
// If any specific error message appears here, then something particularly wrong might have happened.
// Save the error and continue the selection process. So if no solutions gets found, we can return
// the detailed error to the upper layers.
if ( HasErrorMsg ( res ) ) res_detailed_errors . emplace_back ( res ) ;
2021-02-10 16:06:01 -05:00
}
}
2023-03-07 18:47:28 -03:00
// Return right away if we have a detailed error
if ( ! res_detailed_errors . empty ( ) ) return res_detailed_errors . front ( ) ;
// General "Insufficient Funds"
return util : : Result < SelectionResult > ( util : : Error ( ) ) ;
2021-02-10 16:06:01 -05:00
} ( ) ;
return res ;
}
static bool IsCurrentForAntiFeeSniping ( interfaces : : Chain & chain , const uint256 & block_hash )
{
if ( chain . isInitialBlockDownload ( ) ) {
return false ;
}
constexpr int64_t MAX_ANTI_FEE_SNIPING_TIP_AGE = 8 * 60 * 60 ; // in seconds
int64_t block_time ;
CHECK_NONFATAL ( chain . findBlock ( block_hash , FoundBlock ( ) . time ( block_time ) ) ) ;
if ( block_time < ( GetTime ( ) - MAX_ANTI_FEE_SNIPING_TIP_AGE ) ) {
return false ;
}
return true ;
}
/**
2022-02-01 15:00:29 +01:00
* Set a height - based locktime for new transactions ( uses the height of the
2021-02-10 16:06:01 -05:00
* current chain tip unless we are not synced with the current chain
*/
2022-03-14 12:20:23 +01:00
static void DiscourageFeeSniping ( CMutableTransaction & tx , FastRandomContext & rng_fast ,
interfaces : : Chain & chain , const uint256 & block_hash , int block_height )
2021-02-10 16:06:01 -05:00
{
2022-02-01 15:00:29 +01:00
// All inputs must be added by now
assert ( ! tx . vin . empty ( ) ) ;
2021-02-10 16:06:01 -05:00
// Discourage fee sniping.
//
// For a large miner the value of the transactions in the best block and
// the mempool can exceed the cost of deliberately attempting to mine two
// blocks to orphan the current best block. By setting nLockTime such that
// only the next block can include the transaction, we discourage this
// practice as the height restricted and limited blocksize gives miners
// considering fee sniping fewer options for pulling off this attack.
//
// A simple way to think about this is from the wallet's point of view we
// always want the blockchain to move forward. By setting nLockTime this
// way we're basically making the statement that we only want this
// transaction to appear in the next block; we don't want to potentially
// encourage reorgs by allowing transactions to appear at lower heights
// than the next block in forks of the best chain.
//
// Of course, the subsidy is high enough, and transaction volume low
// enough, that fee sniping isn't a problem yet, but by implementing a fix
// now we ensure code won't be written that makes assumptions about
// nLockTime that preclude a fix later.
if ( IsCurrentForAntiFeeSniping ( chain , block_hash ) ) {
2022-02-01 15:00:29 +01:00
tx . nLockTime = block_height ;
2021-02-10 16:06:01 -05:00
// Secondly occasionally randomly pick a nLockTime even further back, so
// that transactions that are delayed after signing for whatever reason,
// e.g. high-latency mix networks and some CoinJoin implementations, have
// better privacy.
2022-03-14 12:20:23 +01:00
if ( rng_fast . randrange ( 10 ) = = 0 ) {
tx . nLockTime = std : : max ( 0 , int ( tx . nLockTime ) - int ( rng_fast . randrange ( 100 ) ) ) ;
2022-02-01 15:00:29 +01:00
}
2021-02-10 16:06:01 -05:00
} else {
// If our chain is lagging behind, we can't discourage fee sniping nor help
// the privacy of high-latency transactions. To avoid leaking a potentially
// unique "nLockTime fingerprint", set nLockTime to a constant.
2022-02-01 15:00:29 +01:00
tx . nLockTime = 0 ;
}
// Sanity check all values
assert ( tx . nLockTime < LOCKTIME_THRESHOLD ) ; // Type must be block height
assert ( tx . nLockTime < = uint64_t ( block_height ) ) ;
for ( const auto & in : tx . vin ) {
// Can not be FINAL for locktime to work
assert ( in . nSequence ! = CTxIn : : SEQUENCE_FINAL ) ;
// May be MAX NONFINAL to disable both BIP68 and BIP125
if ( in . nSequence = = CTxIn : : MAX_SEQUENCE_NONFINAL ) continue ;
// May be MAX BIP125 to disable BIP68 and enable BIP125
if ( in . nSequence = = MAX_BIP125_RBF_SEQUENCE ) continue ;
// The wallet does not support any other sequence-use right now.
assert ( false ) ;
2021-02-10 16:06:01 -05:00
}
}
2021-07-19 03:15:44 +00:00
// Reset all non-global blinding details.
2022-09-20 17:40:47 +00:00
static void resetBlindDetails ( BlindDetails * det , bool preserve_output_data = false ) {
2021-07-19 03:15:44 +00:00
det - > i_amount_blinds . clear ( ) ;
det - > i_asset_blinds . clear ( ) ;
det - > i_assets . clear ( ) ;
det - > i_amounts . clear ( ) ;
det - > o_amounts . clear ( ) ;
2022-09-20 17:40:47 +00:00
if ( ! preserve_output_data ) {
det - > o_pubkeys . clear ( ) ;
}
2021-07-19 03:15:44 +00:00
det - > o_amount_blinds . clear ( ) ;
det - > o_assets . clear ( ) ;
det - > o_asset_blinds . clear ( ) ;
2022-09-20 17:40:47 +00:00
if ( ! preserve_output_data ) {
det - > num_to_blind = 0 ;
det - > change_to_blind = 0 ;
det - > only_recipient_blind_index = - 1 ;
det - > only_change_pos = - 1 ;
}
2021-07-19 03:15:44 +00:00
}
2025-04-09 14:10:58 +00:00
static bool fillBlindDetails ( BlindDetails * det , CWallet * wallet , CMutableTransaction & txNew , std : : vector < std : : shared_ptr < COutput > > & selected_coins , bilingual_str & error ) {
2021-07-19 03:15:44 +00:00
int num_inputs_blinded = 0 ;
// Fill in input blinding details
2025-04-09 14:10:58 +00:00
for ( const std : : shared_ptr < wallet : : COutput > & coin : selected_coins ) {
det - > i_amount_blinds . push_back ( coin - > bf_value ) ;
det - > i_asset_blinds . push_back ( coin - > bf_asset ) ;
det - > i_assets . push_back ( coin - > asset ) ;
det - > i_amounts . push_back ( coin - > value ) ;
if ( coin - > txout . nValue . IsCommitment ( ) | | coin - > txout . nAsset . IsCommitment ( ) ) {
2021-07-19 03:15:44 +00:00
num_inputs_blinded + + ;
}
}
// Fill in output blinding details
for ( size_t nOut = 0 ; nOut < txNew . vout . size ( ) ; nOut + + ) {
//TODO(CA) consider removing all blind setting before BlindTransaction as they get cleared anyway
det - > o_amount_blinds . push_back ( uint256 ( ) ) ;
det - > o_asset_blinds . push_back ( uint256 ( ) ) ;
det - > o_assets . push_back ( txNew . vout [ nOut ] . nAsset . GetAsset ( ) ) ;
det - > o_amounts . push_back ( txNew . vout [ nOut ] . nValue . GetAmount ( ) ) ;
}
// There are a few edge-cases of blinding we need to take care of
//
// First, if there are blinded inputs but not outputs to blind
// We need this to go through, even though no privacy is gained.
if ( num_inputs_blinded > 0 & & det - > num_to_blind = = 0 ) {
// We need to make sure to dupe an asset that is in input set
//TODO Have blinding do some extremely minimal rangeproof
CTxOut newTxOut ( det - > o_assets . back ( ) , 0 , CScript ( ) < < OP_RETURN ) ;
2021-10-14 21:48:49 +00:00
CPubKey blind_pub = wallet - > GetBlindingPubKey ( newTxOut . scriptPubKey ) ; // irrelevant, just needs to be non-null
2021-10-02 15:28:13 +00:00
newTxOut . nNonce . vchCommitment = std : : vector < unsigned char > ( blind_pub . begin ( ) , blind_pub . end ( ) ) ;
2021-07-19 03:15:44 +00:00
txNew . vout . push_back ( newTxOut ) ;
det - > o_pubkeys . push_back ( wallet - > GetBlindingPubKey ( newTxOut . scriptPubKey ) ) ;
det - > o_amount_blinds . push_back ( uint256 ( ) ) ;
det - > o_asset_blinds . push_back ( uint256 ( ) ) ;
det - > o_amounts . push_back ( 0 ) ;
det - > o_assets . push_back ( det - > o_assets . back ( ) ) ;
det - > num_to_blind + + ;
wallet - > WalletLogPrintf ( " Adding OP_RETURN output to complete blinding since there are %d blinded inputs and no blinded outputs \n " , num_inputs_blinded ) ;
// No blinded inputs, but 1 blinded output
} else if ( num_inputs_blinded = = 0 & & det - > num_to_blind = = 1 ) {
if ( det - > change_to_blind = = 1 ) {
// Only 1 blinded change, unblind the change
//TODO Split up change instead if possible
if ( det - > ignore_blind_failure ) {
det - > num_to_blind - - ;
det - > change_to_blind - - ;
txNew . vout [ det - > only_change_pos ] . nNonce . SetNull ( ) ;
det - > o_pubkeys [ det - > only_change_pos ] = CPubKey ( ) ;
det - > o_amount_blinds [ det - > only_change_pos ] = uint256 ( ) ;
det - > o_asset_blinds [ det - > only_change_pos ] = uint256 ( ) ;
wallet - > WalletLogPrintf ( " Unblinding change at index %d due to lack of inputs and other outputs being blinded. \n " , det - > only_change_pos ) ;
} else {
error = _ ( " Change output could not be blinded as there are no blinded inputs and no other blinded outputs. " ) ;
return false ;
}
} else {
// 1 blinded destination
// TODO Attempt to get a blinded input, OR add unblinded coin to make blinded change
assert ( det - > only_recipient_blind_index ! = - 1 ) ;
if ( det - > ignore_blind_failure ) {
det - > num_to_blind - - ;
txNew . vout [ det - > only_recipient_blind_index ] . nNonce . SetNull ( ) ;
det - > o_pubkeys [ det - > only_recipient_blind_index ] = CPubKey ( ) ;
det - > o_amount_blinds [ det - > only_recipient_blind_index ] = uint256 ( ) ;
det - > o_asset_blinds [ det - > only_recipient_blind_index ] = uint256 ( ) ;
wallet - > WalletLogPrintf ( " Unblinding single blinded output at index %d due to lack of inputs and other outputs being blinded. \n " , det - > only_recipient_blind_index ) ;
} else {
error = _ ( " Transaction output could not be blinded as there are no blinded inputs and no other blinded outputs. " ) ;
return false ;
}
}
}
// All other combinations should work.
return true ;
}
2022-07-21 08:30:39 -04:00
static util : : Result < CreatedTransactionResult > CreateTransactionInternal (
2021-02-12 18:01:22 -05:00
CWallet & wallet ,
2021-02-10 16:06:01 -05:00
const std : : vector < CRecipient > & vecSend ,
2020-12-13 01:37:40 +01:00
int change_pos ,
2021-02-10 16:06:01 -05:00
const CCoinControl & coin_control ,
2021-07-19 03:15:44 +00:00
bool sign ,
BlindDetails * blind_details ,
2023-04-14 06:13:01 +00:00
const IssuanceDetails * issuance_details ) EXCLUSIVE_LOCKS_REQUIRED ( wallet . cs_wallet )
2021-02-10 16:06:01 -05:00
{
2021-07-19 03:15:44 +00:00
if ( blind_details | | issuance_details ) {
assert ( g_con_elementsmode ) ;
}
2021-07-23 20:42:38 +00:00
if ( blind_details ) {
// Clear out previous blinding/data info as needed
resetBlindDetails ( blind_details ) ;
}
2021-02-12 18:01:22 -05:00
AssertLockHeld ( wallet . cs_wallet ) ;
2021-05-20 14:51:50 -04:00
2020-12-13 01:37:40 +01:00
// out variables, to be packed into returned result structure
int nChangePosInOut = change_pos ;
2022-03-14 12:20:23 +01:00
FastRandomContext rng_fast ;
2021-05-17 16:26:40 -04:00
CMutableTransaction txNew ; // The resulting transaction that we make
2022-03-14 15:22:42 +01:00
CoinSelectionParams coin_selection_params { rng_fast } ; // Parameters for coin selection, init with dummy
2021-05-17 16:26:40 -04:00
coin_selection_params . m_avoid_partial_spends = coin_control . m_avoid_partial_spends ;
2023-03-08 19:03:40 -03:00
coin_selection_params . m_include_unsafe_inputs = coin_control . m_include_unsafe_inputs ;
2021-05-17 16:26:40 -04:00
2021-07-23 20:42:38 +00:00
CScript dummy_script = CScript ( ) < < 0x00 ;
CAmountMap map_recipients_sum ;
2021-07-19 03:15:44 +00:00
// Always assume that we are at least sending policyAsset.
2021-07-23 20:42:38 +00:00
map_recipients_sum [ : : policyAsset ] = 0 ;
2021-07-19 03:15:44 +00:00
std : : vector < std : : unique_ptr < ReserveDestination > > reservedest ;
2021-05-20 16:54:23 -04:00
// Set the long term feerate estimate to the wallet's consolidate feerate
2021-02-12 18:01:22 -05:00
coin_selection_params . m_long_term_feerate = wallet . m_consolidate_feerate ;
const OutputType change_type = wallet . TransactionChangeType ( coin_control . m_change_type ? * coin_control . m_change_type : wallet . m_default_change_type , vecSend ) ;
2023-04-14 06:13:01 +00:00
reservedest . emplace_back ( new ReserveDestination ( & wallet , change_type ) ) ; // policy asset
2021-07-19 03:15:44 +00:00
std : : set < CAsset > assets_seen ;
2021-05-17 16:22:30 -04:00
unsigned int outputs_to_subtract_fee_from = 0 ; // The number of outputs which we are subtracting the fee from
2021-02-10 16:06:01 -05:00
for ( const auto & recipient : vecSend )
{
2021-07-19 03:15:44 +00:00
// Pad change keys to cover total possible number of assets
// One already exists(for policyAsset), so one for each destination
if ( assets_seen . insert ( recipient . asset ) . second ) {
2023-04-15 07:04:22 +00:00
reservedest . emplace_back ( new ReserveDestination ( & wallet , change_type ) ) ;
2021-07-19 03:15:44 +00:00
}
// Skip over issuance outputs, no need to select those coins
if ( recipient . asset = = CAsset ( uint256S ( " 1 " ) ) | | recipient . asset = = CAsset ( uint256S ( " 2 " ) ) ) {
continue ;
}
2021-07-23 20:42:38 +00:00
map_recipients_sum [ recipient . asset ] + = recipient . nAmount ;
2021-07-19 03:15:44 +00:00
2021-05-17 16:29:31 -04:00
if ( recipient . fSubtractFeeFromAmount ) {
2021-05-17 16:22:30 -04:00
outputs_to_subtract_fee_from + + ;
2021-05-17 16:29:31 -04:00
coin_selection_params . m_subtract_fee_outputs = true ;
2021-02-10 16:06:01 -05:00
}
}
2021-05-20 14:53:36 -04:00
// Create change script that will be used if we need change
2021-07-23 20:42:38 +00:00
// ELEMENTS: A map that keeps track of the change script for each asset and also
// the index of the reservedest used for that script (-1 if none).
std : : map < CAsset , std : : pair < int , CScript > > mapScriptChange ;
2021-10-02 15:28:13 +00:00
// For manually set change, we need to use the blinding pubkey associated
// with the manually-set address rather than generating one from the wallet
2023-04-07 11:14:41 +00:00
std : : map < CAsset , std : : optional < CPubKey > > mapBlindingKeyChange ;
2022-04-08 16:43:10 -03:00
bilingual_str error ; // possible error str
2021-05-20 14:53:36 -04:00
// coin control: send change to custom address
2021-07-23 20:42:38 +00:00
if ( coin_control . destChange . size ( ) > 0 ) {
for ( const auto & dest : coin_control . destChange ) {
// No need to test we cover all assets. We produce error for that later.
mapScriptChange [ dest . first ] = std : : pair < int , CScript > ( - 1 , GetScriptForDestination ( dest . second ) ) ;
2021-10-02 15:28:13 +00:00
if ( IsBlindDestination ( dest . second ) ) {
mapBlindingKeyChange [ dest . first ] = GetDestinationBlindingKey ( dest . second ) ;
} else {
mapBlindingKeyChange [ dest . first ] = std : : nullopt ;
}
2021-07-23 20:42:38 +00:00
}
2021-05-20 14:53:36 -04:00
} else { // no coin control: send change to newly generated address
// Note: We use a new key here to keep it from being obvious which side is the change.
// The drawback is that by not reusing a previous key, the change may be lost if a
// backup is restored, if the backup doesn't have the new private key for the change.
// If we reused the old key, it would be possible to add code to look for and
// rediscover unknown transactions that were written with keys of ours to recover
// post-backup change.
2021-07-23 20:42:38 +00:00
// One change script per output asset.
size_t index = 0 ;
for ( const auto & value : map_recipients_sum ) {
// Reserve a new key pair from key pool. If it fails, provide a dummy
// destination in case we don't need change.
2024-10-31 09:26:04 +02:00
auto op_dest = reservedest [ index ] - > GetReservedDestination ( true ) ;
if ( index > = reservedest . size ( ) | | ! op_dest ) {
error = _ ( " Transaction needs a change address, but we can't generate it. " ) + Untranslated ( " " ) + util : : ErrorString ( op_dest ) ;
2021-07-23 20:42:38 +00:00
// ELEMENTS: We need to put a dummy destination here. Core uses an empty script
2021-10-14 21:48:49 +00:00
// but we can't because empty scripts indicate fees (which trigger assertion
2021-07-23 20:42:38 +00:00
// failures in `BlindTransaction`). We also set the index to -1, indicating
// that this destination is not actually used, and therefore should not be
// returned by the `ReturnDestination` loop below.
mapScriptChange [ value . first ] = std : : pair < int , CScript > ( - 1 , dummy_script ) ;
} else {
2024-10-31 09:26:04 +02:00
mapScriptChange [ value . first ] = std : : pair < int , CScript > ( index , GetScriptForDestination ( * op_dest ) ) ;
2021-07-23 20:42:38 +00:00
+ + index ;
2021-07-19 03:15:44 +00:00
}
2021-07-23 20:42:38 +00:00
}
2021-02-10 16:06:01 -05:00
2021-07-23 20:42:38 +00:00
// Also make sure we have change scripts for the pre-selected inputs.
std : : vector < COutPoint > vPresetInputs ;
coin_control . ListSelected ( vPresetInputs ) ;
for ( const COutPoint & presetInput : vPresetInputs ) {
CAsset asset ;
2024-10-29 11:06:56 +02:00
const auto & it = wallet . mapWallet . find ( presetInput . hash ) ;
2021-07-23 20:42:38 +00:00
CTxOut txout ;
2023-04-15 07:04:22 +00:00
if ( it ! = wallet . mapWallet . end ( ) ) {
asset = it - > second . GetOutputAsset ( wallet , presetInput . n ) ;
2021-07-23 20:42:38 +00:00
} else if ( coin_control . GetExternalOutput ( presetInput , txout ) ) {
asset = txout . nAsset . GetAsset ( ) ;
2021-02-10 16:06:01 -05:00
} else {
2021-07-23 20:42:38 +00:00
// Ignore this here, will fail more gracefully later.
continue ;
2021-02-10 16:06:01 -05:00
}
2021-07-23 20:42:38 +00:00
if ( mapScriptChange . find ( asset ) ! = mapScriptChange . end ( ) ) {
// This asset already has a change script.
continue ;
2021-02-10 16:06:01 -05:00
}
2021-07-23 20:42:38 +00:00
2024-10-31 09:26:04 +02:00
auto op_dest = reservedest [ index ] - > GetReservedDestination ( true ) ;
if ( index > = reservedest . size ( ) | | ! op_dest ) {
return util : : Error { _ ( " Transaction needs a change address, but we can't generate it. " ) + Untranslated ( " " ) + util : : ErrorString ( op_dest ) } ;
2021-02-10 16:06:01 -05:00
}
2024-10-31 09:26:04 +02:00
CScript scriptChange = GetScriptForDestination ( * op_dest ) ;
2021-07-23 20:42:38 +00:00
// A valid destination implies a change script (and
// vice-versa). An empty change script will abort later, if the
// change keypool ran out, but change is required.
2024-10-31 09:26:04 +02:00
CHECK_NONFATAL ( IsValidDestination ( * op_dest ) ! = ( scriptChange = = dummy_script ) ) ;
2021-07-23 20:42:38 +00:00
mapScriptChange [ asset ] = std : : pair < int , CScript > ( index , scriptChange ) ;
+ + index ;
2021-05-20 14:53:36 -04:00
}
}
2021-07-23 20:42:38 +00:00
assert ( mapScriptChange . size ( ) > 0 ) ;
CTxOut change_prototype_txout ( mapScriptChange . begin ( ) - > first , 0 , mapScriptChange . begin ( ) - > second . second ) ;
// TODO CA: Set this for each change output
2021-05-20 14:53:36 -04:00
coin_selection_params . change_output_size = GetSerializeSize ( change_prototype_txout ) ;
2021-07-23 20:42:38 +00:00
if ( g_con_elementsmode ) {
if ( blind_details ) {
change_prototype_txout . nAsset . vchCommitment . resize ( 33 ) ;
change_prototype_txout . nValue . vchCommitment . resize ( 33 ) ;
change_prototype_txout . nNonce . vchCommitment . resize ( 33 ) ;
coin_selection_params . change_output_size = GetSerializeSize ( change_prototype_txout ) ;
coin_selection_params . change_output_size + = ( MAX_RANGEPROOF_SIZE + DEFAULT_SURJECTIONPROOF_SIZE + WITNESS_SCALE_FACTOR - 1 ) / WITNESS_SCALE_FACTOR ;
} else {
change_prototype_txout . nAsset . vchCommitment . resize ( 33 ) ;
change_prototype_txout . nValue . vchCommitment . resize ( 9 ) ;
change_prototype_txout . nNonce . vchCommitment . resize ( 1 ) ;
coin_selection_params . change_output_size = GetSerializeSize ( change_prototype_txout ) ;
}
}
2021-07-19 03:15:44 +00:00
2021-05-20 14:53:36 -04:00
// Get size of spending the change output
2023-02-23 18:30:12 +01:00
int change_spend_size = CalculateMaximumSignedInputSize ( change_prototype_txout , & wallet , /*coin_control=*/ nullptr ) ;
2021-05-20 14:53:36 -04:00
// If the wallet doesn't know how to sign change output, assume p2sh-p2wpkh
// as lower-bound to allow BnB to do it's thing
if ( change_spend_size = = - 1 ) {
coin_selection_params . change_spend_size = DUMMY_NESTED_P2WPKH_INPUT_SIZE ;
} else {
coin_selection_params . change_spend_size = ( size_t ) change_spend_size ;
}
2021-07-19 03:15:44 +00:00
2021-05-20 14:53:36 -04:00
// Set discard feerate
2021-02-12 18:01:22 -05:00
coin_selection_params . m_discard_feerate = GetDiscardRate ( wallet ) ;
2021-02-10 16:06:01 -05:00
2021-05-20 14:53:36 -04:00
// Get the fee rate to use effective values in coin selection
2021-05-17 16:26:40 -04:00
FeeCalculation feeCalc ;
2021-02-12 18:01:22 -05:00
coin_selection_params . m_effective_feerate = GetMinimumFeeRate ( wallet , coin_control , & feeCalc ) ;
2021-05-20 14:53:36 -04:00
// Do not, ever, assume that it's fine to change the fee rate if the user has explicitly
// provided one
if ( coin_control . m_feerate & & coin_selection_params . m_effective_feerate > * coin_control . m_feerate ) {
2022-07-21 08:30:39 -04:00
return util : : Error { strprintf ( _ ( " Fee rate (%s) is lower than the minimum fee rate setting (%s) " ) , coin_control . m_feerate - > ToString ( FeeEstimateMode : : SAT_VB ) , coin_selection_params . m_effective_feerate . ToString ( FeeEstimateMode : : SAT_VB ) ) } ;
2021-05-20 14:53:36 -04:00
}
2021-02-12 18:01:22 -05:00
if ( feeCalc . reason = = FeeReason : : FALLBACK & & ! wallet . m_allow_fallback_fee ) {
2021-05-20 14:53:36 -04:00
// eventually allow a fallback fee
2022-07-21 11:10:47 -03:00
return util : : Error { strprintf ( _ ( " Fee estimation failed. Fallbackfee is disabled. Wait a few blocks or enable %s. " ) , " -fallbackfee " ) } ;
2021-05-20 14:53:36 -04:00
}
2021-02-10 16:06:01 -05:00
2021-05-20 14:53:36 -04:00
// Calculate the cost of change
// Cost of change is the cost of creating the change output + cost of spending the change output in the future.
// For creating the change output now, we use the effective feerate.
// For spending the change output in the future, we use the discard feerate for now.
// So cost of change = (change output size * effective feerate) + (size of spending change output * discard feerate)
coin_selection_params . m_change_fee = coin_selection_params . m_effective_feerate . GetFee ( coin_selection_params . change_output_size ) ;
coin_selection_params . m_cost_of_change = coin_selection_params . m_discard_feerate . GetFee ( coin_selection_params . change_spend_size ) + coin_selection_params . m_change_fee ;
2021-02-10 16:06:01 -05:00
2024-11-18 15:32:37 +02:00
// ELEMENTS FIXME: Please review the map_recipients_sum[::policyAsset] part.
// In bitcoin the line just says recipients_sum (it's not a map).
// I'm not sure if the policyAsset value is the right number to use.
coin_selection_params . m_min_change_target = GenerateChangeTarget ( std : : floor ( map_recipients_sum [ : : policyAsset ] / vecSend . size ( ) ) , coin_selection_params . m_change_fee , rng_fast ) ;
2022-08-15 09:30:31 +02:00
2022-07-06 09:00:57 +02:00
// The smallest change amount should be:
// 1. at least equal to dust threshold
// 2. at least 1 sat greater than fees to spend it at m_discard_feerate
const auto dust = GetDustThreshold ( change_prototype_txout , coin_selection_params . m_discard_feerate ) ;
const auto change_spend_fee = coin_selection_params . m_discard_feerate . GetFee ( coin_selection_params . change_spend_size ) ;
coin_selection_params . min_viable_change = std : : max ( change_spend_fee + 1 , dust ) ;
2022-10-04 14:17:11 +02:00
// Static vsize overhead + outputs vsize. 4 nVersion, 4 nLocktime, 1 input count, 1 witness overhead (dummy, flag, stack size)
coin_selection_params . tx_noinputs_size = 10 + GetSizeOfCompactSize ( vecSend . size ( ) ) ; // bytes for output count
2021-05-20 14:53:36 -04:00
// vouts to the payees
2025-03-18 11:33:11 +00:00
if ( g_con_elementsmode ) {
coin_selection_params . tx_noinputs_size + = 46 ; // fee output: 9 bytes value, 1 byte scriptPubKey, 33 bytes asset, 1 byte nonce, 1 byte each for null rangeproof/surjectionproof
2021-05-20 14:53:36 -04:00
}
2025-03-18 11:33:11 +00:00
2022-09-20 17:39:05 +00:00
// ELEMENTS: If we have blinded inputs but no blinded outputs (which, since the wallet
// makes an effort to not produce change, is a common case) then we need to add a
// dummy output.
bool may_need_blinded_dummy = ! ! blind_details ;
2021-05-20 14:53:36 -04:00
for ( const auto & recipient : vecSend )
{
2021-07-23 20:42:38 +00:00
CTxOut txout ( recipient . asset , recipient . nAmount , recipient . scriptPubKey ) ;
txout . nNonce . vchCommitment = std : : vector < unsigned char > ( recipient . confidentiality_key . begin ( ) , recipient . confidentiality_key . end ( ) ) ;
2021-02-10 16:06:01 -05:00
2021-05-20 14:53:36 -04:00
// Include the fee cost for outputs.
2022-10-04 14:17:11 +02:00
coin_selection_params . tx_noinputs_size + = : : GetSerializeSize ( txout , PROTOCOL_VERSION ) ;
2021-02-10 16:06:01 -05:00
2023-04-14 06:13:01 +00:00
if ( recipient . asset = = policyAsset & & IsDust ( txout , wallet . chain ( ) . relayDustFee ( ) ) )
2021-05-20 14:51:50 -04:00
{
2022-07-21 08:30:39 -04:00
return util : : Error { _ ( " Transaction amount too small " ) } ;
2021-05-20 14:53:36 -04:00
}
txNew . vout . push_back ( txout ) ;
2021-07-23 20:42:38 +00:00
// ELEMENTS
if ( blind_details ) {
blind_details - > o_pubkeys . push_back ( recipient . confidentiality_key ) ;
if ( blind_details - > o_pubkeys . back ( ) . IsFullyValid ( ) ) {
2022-09-20 17:39:05 +00:00
may_need_blinded_dummy = false ;
2021-07-23 20:42:38 +00:00
blind_details - > num_to_blind + + ;
blind_details - > only_recipient_blind_index = txNew . vout . size ( ) - 1 ;
if ( ! coin_selection_params . m_subtract_fee_outputs ) {
coin_selection_params . tx_noinputs_size + = ( MAX_RANGEPROOF_SIZE + DEFAULT_SURJECTIONPROOF_SIZE + WITNESS_SCALE_FACTOR - 1 ) / WITNESS_SCALE_FACTOR ;
2021-07-19 03:15:44 +00:00
}
2021-02-10 16:06:01 -05:00
}
2021-07-23 20:42:38 +00:00
}
2021-05-20 14:53:36 -04:00
}
2022-09-20 17:39:05 +00:00
if ( may_need_blinded_dummy & & ! coin_selection_params . m_subtract_fee_outputs ) {
// dummy output: 33 bytes value, 2 byte scriptPubKey, 33 bytes asset, 1 byte nonce, 66 bytes dummy rangeproof, 1 byte null surjectionproof
// FIXME actually, we currently just hand off to BlindTransaction which will put
// a full rangeproof and surjectionproof. We should fix this when we overhaul
// the blinding logic.
coin_selection_params . tx_noinputs_size + = 70 + 66 + ( MAX_RANGEPROOF_SIZE + DEFAULT_SURJECTIONPROOF_SIZE + WITNESS_SCALE_FACTOR - 1 ) / WITNESS_SCALE_FACTOR ;
}
2022-09-22 13:11:07 +00:00
// If we are going to issue an asset, add the issuance data to the noinputs_size so that
// we allocate enough coins for them.
if ( issuance_details ) {
size_t issue_count = 0 ;
for ( unsigned int i = 0 ; i < txNew . vout . size ( ) ; i + + ) {
if ( txNew . vout [ i ] . nAsset . IsExplicit ( ) & & txNew . vout [ i ] . nAsset . GetAsset ( ) = = CAsset ( uint256S ( " 1 " ) ) ) {
issue_count + + ;
} else if ( txNew . vout [ i ] . nAsset . IsExplicit ( ) & & txNew . vout [ i ] . nAsset . GetAsset ( ) = = CAsset ( uint256S ( " 2 " ) ) ) {
issue_count + + ;
}
}
if ( issue_count > 0 ) {
// Allocate space for blinding nonce, entropy, and whichever of nAmount/nInflationKeys is null
coin_selection_params . tx_noinputs_size + = 2 * 32 + 2 * ( 2 - issue_count ) ;
}
// Allocate non-null nAmount/nInflationKeys and rangeproofs
if ( issuance_details - > blind_issuance ) {
coin_selection_params . tx_noinputs_size + = issue_count * ( 33 * WITNESS_SCALE_FACTOR + MAX_RANGEPROOF_SIZE + WITNESS_SCALE_FACTOR - 1 ) / WITNESS_SCALE_FACTOR ;
} else {
coin_selection_params . tx_noinputs_size + = issue_count * 9 ;
}
}
2021-07-19 03:15:44 +00:00
2021-05-20 14:53:36 -04:00
// Include the fees for things that aren't inputs, excluding the change output
2022-10-04 14:17:11 +02:00
const CAmount not_input_fees = coin_selection_params . m_effective_feerate . GetFee ( coin_selection_params . m_subtract_fee_outputs ? 0 : coin_selection_params . tx_noinputs_size ) ;
2021-07-23 20:42:38 +00:00
CAmountMap map_selection_target = map_recipients_sum ;
map_selection_target [ policyAsset ] + = not_input_fees ;
2021-05-20 14:51:50 -04:00
2023-03-16 14:58:41 -04:00
// This can only happen if feerate is 0, and requested destinations are value of 0 (e.g. OP_RETURN)
// and no pre-selected inputs. This will result in 0-input transaction, which is consensus-invalid anyways
2025-04-11 20:50:17 +02:00
if ( map_selection_target = = CAmountMap { } & & ! coin_control . HasSelected ( ) ) {
2023-03-16 14:58:41 -04:00
return util : : Error { _ ( " Transaction requires one destination of non-0 value, a non-0 feerate, or a pre-selected input " ) } ;
}
2022-08-05 12:51:44 -03:00
// Fetch manually selected coins
PreSelectedInputs preset_inputs ;
if ( coin_control . HasSelected ( ) ) {
auto res_fetch_inputs = FetchSelectedInputs ( wallet , coin_control , coin_selection_params ) ;
if ( ! res_fetch_inputs ) return util : : Error { util : : ErrorString ( res_fetch_inputs ) } ;
preset_inputs = * res_fetch_inputs ;
}
2022-08-05 11:58:08 -03:00
// Fetch wallet available coins if "other inputs" are
// allowed (coins automatically selected by the wallet)
CoinsResult available_coins ;
if ( coin_control . m_allow_other_inputs ) {
2025-02-28 12:05:54 +02:00
available_coins = AvailableCoins ( wallet , & coin_control , coin_selection_params . m_effective_feerate ) ;
2022-08-05 11:58:08 -03:00
}
2021-05-17 16:26:40 -04:00
2021-05-20 14:53:36 -04:00
// Choose coins to use
2025-04-01 09:43:22 +02:00
auto select_coins_res = SelectCoins ( wallet , available_coins , preset_inputs , /*mapTargetValue=*/ map_selection_target , coin_control , coin_selection_params ) ;
2022-08-08 15:18:45 -03:00
if ( ! select_coins_res ) {
2022-12-07 14:35:46 -03:00
// 'SelectCoins' either returns a specific error message or, if empty, means a general "Insufficient funds".
const bilingual_str & err = util : : ErrorString ( select_coins_res ) ;
return util : : Error { err . empty ( ) ? _ ( " Insufficient funds " ) : err } ;
2021-05-20 14:53:36 -04:00
}
2022-08-08 15:18:45 -03:00
const SelectionResult & result = * select_coins_res ;
TRACE5 ( coin_selection , selected_coins , wallet . GetName ( ) . c_str ( ) , GetAlgorithmName ( result . GetAlgo ( ) ) . c_str ( ) , result . GetTarget ( ) , result . GetWaste ( ) , result . GetSelectedValue ( ) ) ;
2021-02-10 16:06:01 -05:00
2022-09-20 17:39:05 +00:00
// If all of our inputs are explicit, we don't need a blinded dummy
if ( may_need_blinded_dummy ) {
may_need_blinded_dummy = false ;
2025-04-01 09:43:22 +02:00
for ( const auto & coin : result . GetInputSet ( ) ) {
2025-04-09 14:10:58 +00:00
if ( ! coin - > txout . nValue . IsExplicit ( ) ) {
2022-09-20 17:39:05 +00:00
may_need_blinded_dummy = true ;
break ;
}
}
}
2021-05-20 14:53:36 -04:00
// Always make a change output
// We will reduce the fee from this change output later, and remove the output if it is too small.
2021-07-23 20:42:38 +00:00
// ELEMENTS: wrap this all in a loop, set nChangePosInOut specifically for policy asset
2025-04-01 09:43:22 +02:00
CAmountMap map_change_and_fee = result . GetSelectedValue ( ) - map_recipients_sum ;
2021-07-23 20:42:38 +00:00
// Zero out any non-policy assets which have zero change value
for ( auto it = map_change_and_fee . begin ( ) ; it ! = map_change_and_fee . end ( ) ; ) {
if ( it - > first ! = policyAsset & & it - > second = = 0 ) {
it = map_change_and_fee . erase ( it ) ;
} else {
+ + it ;
}
2021-05-20 14:53:36 -04:00
}
2021-07-19 03:15:44 +00:00
2021-07-23 20:42:38 +00:00
// Uniformly randomly place change outputs for all assets, except that the policy-asset
// change may have a fixed position.
2024-08-12 18:45:02 +00:00
std : : vector < std : : optional < CAsset > > fixed_change_pos { txNew . vout . size ( ) + map_change_and_fee . size ( ) } ;
2021-07-23 20:42:38 +00:00
if ( nChangePosInOut = = - 1 ) {
// randomly set policyasset change position
2024-08-12 18:45:02 +00:00
} else if ( ( unsigned int ) nChangePosInOut > = fixed_change_pos . size ( ) ) {
2022-07-21 08:30:39 -04:00
return util : : Error { _ ( " Transaction change output index out of range " ) } ;
2021-07-23 20:42:38 +00:00
} else {
2024-08-12 18:45:02 +00:00
fixed_change_pos [ nChangePosInOut ] = policyAsset ;
2021-05-20 14:53:36 -04:00
}
2021-07-19 03:15:44 +00:00
2021-07-23 20:42:38 +00:00
for ( const auto & asset_change_and_fee : map_change_and_fee ) {
// No need to randomly set the policyAsset change if has been set manually
if ( nChangePosInOut > = 0 & & asset_change_and_fee . first = = policyAsset ) {
continue ;
}
2021-07-19 03:15:44 +00:00
2021-07-23 20:42:38 +00:00
int index ;
do {
2024-08-12 18:45:02 +00:00
index = rng_fast . randrange ( fixed_change_pos . size ( ) ) ;
} while ( fixed_change_pos [ index ] ) ;
2021-07-19 03:15:44 +00:00
2024-08-12 18:45:02 +00:00
fixed_change_pos [ index ] = asset_change_and_fee . first ;
2021-07-23 20:42:38 +00:00
if ( asset_change_and_fee . first = = policyAsset ) {
nChangePosInOut = index ;
}
}
2021-07-19 03:15:44 +00:00
2021-07-23 20:42:38 +00:00
// Create all the change outputs in their respective places, inserting them
// in increasing order so that none of them affect each others' indices
2024-08-12 18:45:02 +00:00
for ( unsigned int i = 0 ; i < fixed_change_pos . size ( ) ; i + + ) {
if ( ! fixed_change_pos [ i ] ) {
2021-07-23 20:42:38 +00:00
continue ;
}
2021-07-19 03:15:44 +00:00
2024-08-12 18:45:02 +00:00
const CAsset & asset = * fixed_change_pos [ i ] ;
2021-07-23 20:42:38 +00:00
const CAmount & change_and_fee = map_change_and_fee . at ( asset ) ;
2021-07-19 03:15:44 +00:00
2021-07-23 20:42:38 +00:00
assert ( change_and_fee > = 0 ) ;
2021-07-19 03:15:44 +00:00
2021-07-23 20:42:38 +00:00
const std : : map < CAsset , std : : pair < int , CScript > > : : const_iterator itScript = mapScriptChange . find ( asset ) ;
if ( itScript = = mapScriptChange . end ( ) ) {
2024-10-25 12:52:06 +02:00
return util : : Error { Untranslated ( strprintf ( " No change destination provided for asset %s " , asset . GetHex ( ) ) ) } ;
2021-07-23 20:42:38 +00:00
}
CTxOut newTxOut ( asset , change_and_fee , itScript - > second . second ) ;
if ( blind_details ) {
2021-10-02 15:28:13 +00:00
std : : optional < CPubKey > blind_pub = std : : nullopt ;
// We cannot blind zero-valued outputs, and anyway they will be dropped
// later in this function during the dust check
2021-07-23 20:42:38 +00:00
if ( change_and_fee > 0 ) {
2021-10-02 15:28:13 +00:00
const auto itBlindingKey = mapBlindingKeyChange . find ( asset ) ;
if ( itBlindingKey ! = mapBlindingKeyChange . end ( ) ) {
// If the change output was specified, use the blinding key that
// came with the specified address (if any)
blind_pub = itBlindingKey - > second ;
} else {
// Otherwise, we generated it from our own wallet, so get the
// blinding key from our own wallet.
2023-04-15 07:04:22 +00:00
blind_pub = wallet . GetBlindingPubKey ( itScript - > second . second ) ;
2021-10-02 15:28:13 +00:00
}
} else {
assert ( asset = = policyAsset ) ;
}
if ( blind_pub ) {
blind_details - > o_pubkeys . insert ( blind_details - > o_pubkeys . begin ( ) + i , * blind_pub ) ;
assert ( blind_pub - > IsFullyValid ( ) ) ;
2021-07-23 20:42:38 +00:00
blind_details - > num_to_blind + + ;
blind_details - > change_to_blind + + ;
blind_details - > only_change_pos = i ;
// Place the blinding pubkey here in case of fundraw calls
2021-10-02 15:28:13 +00:00
newTxOut . nNonce . vchCommitment = std : : vector < unsigned char > ( blind_pub - > begin ( ) , blind_pub - > end ( ) ) ;
2021-07-23 20:42:38 +00:00
} else {
blind_details - > o_pubkeys . insert ( blind_details - > o_pubkeys . begin ( ) + i , CPubKey ( ) ) ;
2021-07-19 03:15:44 +00:00
}
2021-07-23 20:42:38 +00:00
}
// Insert change output
txNew . vout . insert ( txNew . vout . begin ( ) + i , newTxOut ) ;
}
2021-07-19 03:15:44 +00:00
2021-07-23 20:42:38 +00:00
// Add fee output.
if ( g_con_elementsmode ) {
CTxOut fee ( : : policyAsset , 0 , CScript ( ) ) ;
assert ( fee . IsFee ( ) ) ;
txNew . vout . push_back ( fee ) ;
if ( blind_details ) {
blind_details - > o_pubkeys . push_back ( CPubKey ( ) ) ;
}
}
2021-05-20 14:53:36 -04:00
assert ( nChangePosInOut ! = - 1 ) ;
2021-07-23 20:42:38 +00:00
auto change_position = txNew . vout . begin ( ) + nChangePosInOut ;
// end ELEMENTS
2021-07-19 03:15:44 +00:00
2021-07-23 20:42:38 +00:00
// Set token input if reissuing
int reissuance_index = - 1 ;
uint256 token_blinding ;
2021-02-10 16:06:01 -05:00
2021-07-23 20:42:38 +00:00
// Elements: Shuffle here to preserve random ordering for surjection proofs
2023-06-07 12:07:55 +00:00
// selected_coins = std::vector<CInputCoin>(setCoins.begin(), setCoins.end());
// Shuffle(selected_coins.begin(), selected_coins.end(), FastRandomContext());
2021-05-17 16:31:06 -04:00
// Shuffle selected coins and fill in final vin
2022-08-01 16:15:36 -03:00
std : : vector < std : : shared_ptr < COutput > > selected_coins = result . GetShuffledInputVector ( ) ;
2021-07-19 03:15:44 +00:00
2022-02-01 15:00:29 +01:00
// The sequence number is set to non-maxint so that DiscourageFeeSniping
// works.
2021-05-20 14:53:36 -04:00
//
2021-05-17 16:31:06 -04:00
// BIP125 defines opt-in RBF as any nSequence < maxint-1, so
// we use the highest possible value in that range (maxint-2)
// to avoid conflicting with other possible uses of nSequence,
// and in the spirit of "smallest possible change from prior
// behavior."
2022-01-10 14:48:13 +01:00
const uint32_t nSequence { coin_control . m_signal_bip125_rbf . value_or ( wallet . m_signal_rbf ) ? MAX_BIP125_RBF_SEQUENCE : CTxIn : : MAX_SEQUENCE_NONFINAL } ;
2021-05-17 16:31:06 -04:00
for ( const auto & coin : selected_coins ) {
2022-08-01 16:15:36 -03:00
txNew . vin . push_back ( CTxIn ( coin - > outpoint , CScript ( ) , nSequence ) ) ;
2021-07-23 20:42:38 +00:00
2025-04-09 14:10:58 +00:00
if ( issuance_details & & coin - > asset = = issuance_details - > reissuance_token ) {
2021-07-23 20:42:38 +00:00
reissuance_index = txNew . vin . size ( ) - 1 ;
2025-04-09 14:10:58 +00:00
token_blinding = coin - > bf_asset ;
2021-07-23 20:42:38 +00:00
}
}
2022-03-14 12:20:23 +01:00
DiscourageFeeSniping ( txNew , rng_fast , wallet . chain ( ) , wallet . GetLastBlockHash ( ) , wallet . GetLastBlockHeight ( ) ) ;
2021-02-10 16:06:01 -05:00
2021-07-23 20:42:38 +00:00
// ELEMENTS add issuance details and blinding details
std : : vector < CKey > issuance_asset_keys ;
std : : vector < CKey > issuance_token_keys ;
if ( issuance_details ) {
// Fill in issuances now that inputs are set
assert ( txNew . vin . size ( ) > 0 ) ;
int asset_index = - 1 ;
int token_index = - 1 ;
for ( unsigned int i = 0 ; i < txNew . vout . size ( ) ; i + + ) {
if ( txNew . vout [ i ] . nAsset . IsExplicit ( ) & & txNew . vout [ i ] . nAsset . GetAsset ( ) = = CAsset ( uint256S ( " 1 " ) ) ) {
asset_index = i ;
} else if ( txNew . vout [ i ] . nAsset . IsExplicit ( ) & & txNew . vout [ i ] . nAsset . GetAsset ( ) = = CAsset ( uint256S ( " 2 " ) ) ) {
token_index = i ;
}
}
// Initial issuance request
if ( issuance_details - > reissuance_asset . IsNull ( ) & & issuance_details - > reissuance_token . IsNull ( ) & & ( asset_index ! = - 1 | | token_index ! = - 1 ) ) {
uint256 entropy ;
CAsset asset ;
CAsset token ;
// Initial issuance always uses vin[0]
2021-09-18 15:46:10 +00:00
GenerateAssetEntropy ( entropy , txNew . vin [ 0 ] . prevout , issuance_details - > contract_hash ) ;
2021-07-23 20:42:38 +00:00
CalculateAsset ( asset , entropy ) ;
CalculateReissuanceToken ( token , entropy , issuance_details - > blind_issuance ) ;
CScript blindingScript ( CScript ( ) < < OP_RETURN < < std : : vector < unsigned char > ( txNew . vin [ 0 ] . prevout . hash . begin ( ) , txNew . vin [ 0 ] . prevout . hash . end ( ) ) < < txNew . vin [ 0 ] . prevout . n ) ;
2021-09-18 15:46:10 +00:00
txNew . vin [ 0 ] . assetIssuance . assetEntropy = issuance_details - > contract_hash ;
2021-07-23 20:42:38 +00:00
// We're making asset outputs, fill out asset type and issuance input
if ( asset_index ! = - 1 ) {
txNew . vin [ 0 ] . assetIssuance . nAmount = txNew . vout [ asset_index ] . nValue ;
txNew . vout [ asset_index ] . nAsset = asset ;
if ( issuance_details - > blind_issuance & & blind_details ) {
2023-04-15 07:04:22 +00:00
issuance_asset_keys . push_back ( wallet . GetBlindingKey ( & blindingScript ) ) ;
2021-07-23 20:42:38 +00:00
blind_details - > num_to_blind + + ;
2021-07-19 03:15:44 +00:00
}
2021-07-23 20:42:38 +00:00
}
// We're making reissuance token outputs
if ( token_index ! = - 1 ) {
txNew . vin [ 0 ] . assetIssuance . nInflationKeys = txNew . vout [ token_index ] . nValue ;
txNew . vout [ token_index ] . nAsset = token ;
if ( issuance_details - > blind_issuance & & blind_details ) {
2023-04-15 07:04:22 +00:00
issuance_token_keys . push_back ( wallet . GetBlindingKey ( & blindingScript ) ) ;
2021-07-23 20:42:38 +00:00
blind_details - > num_to_blind + + ;
// If we're blinding a token issuance and no assets, we must make
// the asset issuance a blinded commitment to 0
if ( asset_index = = - 1 ) {
txNew . vin [ 0 ] . assetIssuance . nAmount = 0 ;
2023-04-15 07:04:22 +00:00
issuance_asset_keys . push_back ( wallet . GetBlindingKey ( & blindingScript ) ) ;
2021-07-19 03:15:44 +00:00
blind_details - > num_to_blind + + ;
}
}
}
2021-07-23 20:42:38 +00:00
// Asset being reissued with explicitly named asset/token
} else if ( asset_index ! = - 1 ) {
assert ( reissuance_index ! = - 1 ) ;
// Fill in output with issuance
txNew . vout [ asset_index ] . nAsset = issuance_details - > reissuance_asset ;
// Fill in issuance
// Blinding revealing underlying asset
txNew . vin [ reissuance_index ] . assetIssuance . assetBlindingNonce = token_blinding ;
txNew . vin [ reissuance_index ] . assetIssuance . assetEntropy = issuance_details - > entropy ;
txNew . vin [ reissuance_index ] . assetIssuance . nAmount = txNew . vout [ asset_index ] . nValue ;
// If blinded token derivation, blind the issuance
CAsset temp_token ;
CalculateReissuanceToken ( temp_token , issuance_details - > entropy , true ) ;
if ( temp_token = = issuance_details - > reissuance_token & & blind_details ) {
CScript blindingScript ( CScript ( ) < < OP_RETURN < < std : : vector < unsigned char > ( txNew . vin [ reissuance_index ] . prevout . hash . begin ( ) , txNew . vin [ reissuance_index ] . prevout . hash . end ( ) ) < < txNew . vin [ reissuance_index ] . prevout . n ) ;
issuance_asset_keys . resize ( reissuance_index ) ;
2023-04-15 07:04:22 +00:00
issuance_asset_keys . push_back ( wallet . GetBlindingKey ( & blindingScript ) ) ;
2021-07-23 20:42:38 +00:00
blind_details - > num_to_blind + + ;
}
}
}
2021-07-19 03:15:44 +00:00
2021-07-23 20:42:38 +00:00
// Do "initial blinding" for fee estimation purposes
TxSize tx_sizes ;
CMutableTransaction tx_blinded = txNew ;
if ( blind_details ) {
2023-04-15 07:04:22 +00:00
if ( ! fillBlindDetails ( blind_details , & wallet , tx_blinded , selected_coins , error ) ) {
2024-10-25 12:52:06 +02:00
return util : : Error { error } ;
2021-07-23 20:42:38 +00:00
}
txNew = tx_blinded ; // sigh, `fillBlindDetails` may have modified txNew
2025-02-25 13:50:40 -08:00
// Update the change position to the new tx
change_position = txNew . vout . begin ( ) + nChangePosInOut ;
2021-07-19 03:15:44 +00:00
2021-07-23 20:42:38 +00:00
int ret = BlindTransaction ( blind_details - > i_amount_blinds , blind_details - > i_asset_blinds , blind_details - > i_assets , blind_details - > i_amounts , blind_details - > o_amount_blinds , blind_details - > o_asset_blinds , blind_details - > o_pubkeys , issuance_asset_keys , issuance_token_keys , tx_blinded ) ;
assert ( ret ! = - 1 ) ;
if ( ret ! = blind_details - > num_to_blind ) {
2024-10-25 12:52:06 +02:00
return util : : Error { _ ( " Unable to blind the transaction properly. This should not happen. " ) } ;
2021-07-23 20:42:38 +00:00
}
2021-07-19 03:15:44 +00:00
2023-04-15 07:04:22 +00:00
tx_sizes = CalculateMaximumSignedTxSize ( CTransaction ( tx_blinded ) , & wallet , & coin_control ) ;
2021-07-23 20:42:38 +00:00
} else {
2023-04-15 07:04:22 +00:00
tx_sizes = CalculateMaximumSignedTxSize ( CTransaction ( txNew ) , & wallet , & coin_control ) ;
2021-05-20 14:53:36 -04:00
}
2021-07-23 20:42:38 +00:00
// end ELEMENTS
2021-02-10 16:06:01 -05:00
2021-05-20 14:53:36 -04:00
// Calculate the transaction fee
2021-05-17 16:26:40 -04:00
int nBytes = tx_sizes . vsize ;
2021-10-05 21:17:22 +08:00
if ( nBytes = = - 1 ) {
2022-07-21 08:30:39 -04:00
return util : : Error { _ ( " Missing solving data for estimating transaction size " ) } ;
2021-05-20 14:53:36 -04:00
}
2021-02-10 16:06:01 -05:00
2021-05-20 14:53:36 -04:00
// Subtract fee from the change output if not subtracting it from recipient outputs
2022-07-13 08:54:44 +02:00
CAmount fee_needed = coin_selection_params . m_effective_feerate . GetFee ( nBytes ) ;
2025-03-28 14:38:15 +00:00
CAmount current_fee = fee_needed ;
2021-05-17 16:29:31 -04:00
if ( ! coin_selection_params . m_subtract_fee_outputs ) {
2021-07-23 20:42:38 +00:00
change_position - > nValue = change_position - > nValue . GetAmount ( ) - fee_needed ;
2021-05-20 14:53:36 -04:00
}
2021-02-10 16:06:01 -05:00
2021-05-20 14:53:36 -04:00
// We want to drop the change to fees if:
// 1. The change output would be dust
// 2. The change is within the (almost) exact match window, i.e. it is less than or equal to the cost of the change output (cost_of_change)
2021-07-23 20:42:38 +00:00
CAmount change_amount = change_position - > nValue . GetAmount ( ) ;
2021-05-20 14:53:36 -04:00
if ( IsDust ( * change_position , coin_selection_params . m_discard_feerate ) | | change_amount < = coin_selection_params . m_cost_of_change )
{
2022-09-20 17:39:05 +00:00
bool was_blinded = blind_details & & blind_details - > o_pubkeys [ nChangePosInOut ] . IsValid ( ) ;
// If the change was blinded, and was the only blinded output, we cannot drop it
// without causing the transaction to fail to balance. So keep it, and merely
// zero it out.
if ( was_blinded & & blind_details - > num_to_blind = = 1 ) {
assert ( may_need_blinded_dummy ) ;
change_position - > scriptPubKey = CScript ( ) < < OP_RETURN ;
change_position - > nValue = 0 ;
} else {
txNew . vout . erase ( change_position ) ;
2021-07-19 03:15:44 +00:00
2024-08-12 18:45:02 +00:00
fixed_change_pos [ nChangePosInOut ] = std : : nullopt ;
2022-09-20 17:39:05 +00:00
tx_blinded . vout . erase ( tx_blinded . vout . begin ( ) + nChangePosInOut ) ;
if ( tx_blinded . witness . vtxoutwit . size ( ) > ( unsigned ) nChangePosInOut ) {
tx_blinded . witness . vtxoutwit . erase ( tx_blinded . witness . vtxoutwit . begin ( ) + nChangePosInOut ) ;
}
if ( blind_details ) {
blind_details - > o_amounts . erase ( blind_details - > o_amounts . begin ( ) + nChangePosInOut ) ;
blind_details - > o_assets . erase ( blind_details - > o_assets . begin ( ) + nChangePosInOut ) ;
blind_details - > o_pubkeys . erase ( blind_details - > o_pubkeys . begin ( ) + nChangePosInOut ) ;
// If change_amount == 0, we did not increment num_to_blind initially
// and therefore do not need to decrement it here.
if ( was_blinded ) {
blind_details - > num_to_blind - - ;
blind_details - > change_to_blind - - ;
2022-09-20 17:40:47 +00:00
// FIXME: If we drop the change *and* this means we have only one
// blinded output *and* we have no blinded inputs, then this puts
// us in a situation where BlindTransaction will fail. This is
// prevented in fillBlindDetails, which adds an OP_RETURN output
// to handle this case. So do this ludicrous hack to accomplish
// this. This whole lump of un-followable-logic needs to be replaced
// by a complete rewriting of the wallet blinding logic.
2022-09-20 17:41:24 +00:00
if ( blind_details - > num_to_blind < 2 ) {
2022-09-20 17:40:47 +00:00
resetBlindDetails ( blind_details , true /* don't wipe output data */ ) ;
2023-04-15 07:04:22 +00:00
if ( ! fillBlindDetails ( blind_details , & wallet , txNew , selected_coins , error ) ) {
2024-10-25 12:52:06 +02:00
return util : : Error { error } ;
2022-09-20 17:39:05 +00:00
}
2021-09-17 21:06:20 +00:00
}
}
2021-02-10 16:06:01 -05:00
}
2021-07-23 20:42:38 +00:00
}
change_amount = 0 ;
nChangePosInOut = - 1 ;
2021-02-10 16:06:01 -05:00
2021-05-20 14:53:36 -04:00
// Because we have dropped this change, the tx size and required fee will be different, so let's recalculate those
2023-04-14 06:13:01 +00:00
tx_sizes = CalculateMaximumSignedTxSize ( CTransaction ( tx_blinded ) , & wallet , & coin_control ) ;
2021-05-20 14:51:50 -04:00
nBytes = tx_sizes . vsize ;
2021-05-20 14:53:36 -04:00
fee_needed = coin_selection_params . m_effective_feerate . GetFee ( nBytes ) ;
}
2021-02-10 16:06:01 -05:00
2022-09-20 17:39:05 +00:00
// The only time that fee_needed should be less than the amount available for fees (in change_and_fee - change_amount) is when
// we are subtracting the fee from the outputs. If this occurs at any other time, it is a bug.
2023-08-16 21:15:05 +02:00
if ( ! coin_selection_params . m_subtract_fee_outputs & & fee_needed > map_change_and_fee . at ( policyAsset ) - change_amount ) {
wallet . WalletLogPrintf ( " ERROR: not enough coins to cover for fee (needed: %d, total: %d, change: %d) \n " ,
fee_needed , map_change_and_fee . at ( policyAsset ) , change_amount ) ;
2022-11-30 10:48:05 -05:00
return util : : Error { Untranslated ( STR_INTERNAL_BUG ( " Fee needed > fee paid " ) ) } ;
2023-08-16 21:15:05 +02:00
}
2022-09-20 17:39:05 +00:00
2022-12-05 15:53:18 -05:00
// Sanity check that the fee cannot be negative as that means we have more output value than input value
if ( current_fee < 0 ) {
return util : : Error { Untranslated ( STR_INTERNAL_BUG ( " Fee paid < 0 " ) ) } ;
}
2022-07-13 08:54:44 +02:00
// If there is a change output and we overpay the fees then increase the change to match the fee needed
2021-07-23 20:42:38 +00:00
if ( fee_needed < = map_change_and_fee . at ( policyAsset ) - change_amount ) {
2025-03-28 14:38:15 +00:00
current_fee = map_change_and_fee . at ( policyAsset ) - change_amount ;
2021-05-20 14:53:36 -04:00
}
2021-02-10 16:06:01 -05:00
2021-05-20 14:53:36 -04:00
// Reduce output values for subtractFeeFromAmount
2021-05-17 16:29:31 -04:00
if ( coin_selection_params . m_subtract_fee_outputs ) {
2021-07-23 20:42:38 +00:00
CAmount to_reduce = fee_needed + change_amount - map_change_and_fee . at ( policyAsset ) ;
2021-05-20 14:53:36 -04:00
int i = 0 ;
bool fFirst = true ;
for ( const auto & recipient : vecSend )
2021-05-20 14:51:50 -04:00
{
2021-05-20 14:53:36 -04:00
if ( i = = nChangePosInOut ) {
+ + i ;
2021-07-19 03:15:44 +00:00
}
2021-05-20 14:53:36 -04:00
CTxOut & txout = txNew . vout [ i ] ;
2021-07-19 03:15:44 +00:00
2021-05-20 14:53:36 -04:00
if ( recipient . fSubtractFeeFromAmount )
2021-02-10 16:06:01 -05:00
{
2021-07-23 20:42:38 +00:00
CAmount value = txout . nValue . GetAmount ( ) ;
if ( recipient . asset ! = policyAsset ) {
2024-10-25 12:52:06 +02:00
return util : : Error { Untranslated ( strprintf ( " Wallet does not support more than one type of fee at a time, therefore can not subtract fee from address amount, which is of a different asset id. fee asset: %s recipient asset: %s " , policyAsset . GetHex ( ) , recipient . asset . GetHex ( ) ) ) } ;
2021-07-19 03:15:44 +00:00
}
2021-07-23 20:42:38 +00:00
value - = to_reduce / outputs_to_subtract_fee_from ; // Subtract fee equally from each selected recipient
2021-02-10 16:06:01 -05:00
2021-05-20 14:53:36 -04:00
if ( fFirst ) // first receiver pays the remainder not divisible by output count
2021-02-10 16:06:01 -05:00
{
2021-05-20 14:53:36 -04:00
fFirst = false ;
2021-07-23 20:42:38 +00:00
value - = to_reduce % outputs_to_subtract_fee_from ;
2021-07-19 03:15:44 +00:00
}
2021-05-20 14:53:36 -04:00
// Error if this output is reduced to be below dust
2021-02-12 18:01:22 -05:00
if ( IsDust ( txout , wallet . chain ( ) . relayDustFee ( ) ) ) {
2021-07-23 20:42:38 +00:00
if ( value < 0 ) {
2022-07-21 08:30:39 -04:00
return util : : Error { _ ( " The transaction amount is too small to pay the fee " ) } ;
2021-05-20 14:53:36 -04:00
} else {
2022-07-21 08:30:39 -04:00
return util : : Error { _ ( " The transaction amount is too small to send after the fee has been deducted " ) } ;
2021-07-19 03:15:44 +00:00
}
}
2021-07-23 20:42:38 +00:00
txout . nValue = value ;
2021-02-10 16:06:01 -05:00
}
2021-05-20 14:53:36 -04:00
+ + i ;
2021-02-10 16:06:01 -05:00
}
2025-04-01 09:43:22 +02:00
current_fee = result . GetSelectedValue ( ) [ : : policyAsset ] - CalculateOutputValue ( txNew , : : policyAsset ) ;
2022-12-05 14:33:35 -05:00
if ( fee_needed ! = current_fee ) {
return util : : Error { Untranslated ( STR_INTERNAL_BUG ( " SFFO: Fee needed != fee paid " ) ) } ;
}
}
// fee_needed should now always be less than or equal to the current fees that we pay.
// If it is not, it is a bug.
if ( fee_needed > current_fee ) {
return util : : Error { Untranslated ( STR_INTERNAL_BUG ( " Fee needed > fee paid " ) ) } ;
2021-05-20 14:53:36 -04:00
}
2021-02-10 16:06:01 -05:00
2021-07-23 20:42:38 +00:00
// ELEMENTS: Give up if change keypool ran out and change is required
2024-08-12 18:45:02 +00:00
for ( const auto & maybe_change_asset : fixed_change_pos ) {
2021-07-23 20:42:38 +00:00
if ( maybe_change_asset ) {
auto used = mapScriptChange . extract ( * maybe_change_asset ) ;
if ( used . mapped ( ) . second = = dummy_script ) {
2024-10-25 12:52:06 +02:00
return util : : Error { error } ;
2021-07-19 03:15:44 +00:00
}
2021-07-23 20:42:38 +00:00
}
2021-05-20 14:53:36 -04:00
}
2021-07-19 03:15:44 +00:00
2021-07-23 20:42:38 +00:00
// ELEMENTS update fee output
if ( g_con_elementsmode ) {
for ( auto & txout : txNew . vout ) {
if ( txout . IsFee ( ) ) {
2025-03-28 14:38:15 +00:00
txout . nValue = current_fee ;
2021-07-23 20:42:38 +00:00
break ;
}
2021-07-19 03:15:44 +00:00
}
2021-07-23 20:42:38 +00:00
}
2021-02-10 16:06:01 -05:00
2021-07-23 20:42:38 +00:00
// ELEMENTS do actual blinding
if ( blind_details ) {
// Print blinded transaction info before we possibly blow it away when !sign.
std : : string summary = " CreateTransaction created blinded transaction: \n IN: " ;
for ( unsigned int i = 0 ; i < selected_coins . size ( ) ; + + i ) {
if ( i > 0 ) {
summary + = " " ;
}
summary + = strprintf ( " #%d: %s [%s] (%s [%s]) \n " , i ,
2025-04-09 14:10:58 +00:00
selected_coins [ i ] - > value ,
selected_coins [ i ] - > txout . nValue . IsExplicit ( ) ? " explicit " : " blinded " ,
selected_coins [ i ] - > asset . GetHex ( ) ,
selected_coins [ i ] - > txout . nAsset . IsExplicit ( ) ? " explicit " : " blinded "
2021-07-23 20:42:38 +00:00
) ;
}
summary + = " OUT: " ;
for ( unsigned int i = 0 ; i < txNew . vout . size ( ) ; + + i ) {
if ( i > 0 ) {
summary + = " " ;
}
const CTxOut & unblinded = txNew . vout [ i ] ;
summary + = strprintf ( " #%d: %s%s [%s] (%s [%s]) \n " , i ,
txNew . vout [ i ] . IsFee ( ) ? " [fee] " : " " ,
unblinded . nValue . GetAmount ( ) ,
2022-09-20 17:39:05 +00:00
blind_details - > o_pubkeys [ i ] . IsValid ( ) ? " blinded " : " explicit " ,
2021-07-23 20:42:38 +00:00
unblinded . nAsset . GetAsset ( ) . GetHex ( ) ,
2022-09-20 17:39:05 +00:00
blind_details - > o_pubkeys [ i ] . IsValid ( ) ? " blinded " : " explicit "
2021-07-23 20:42:38 +00:00
) ;
}
2023-04-15 07:04:22 +00:00
wallet . WalletLogPrintf ( summary + " \n " ) ;
2021-07-23 20:42:38 +00:00
// Wipe output blinding factors and start over
blind_details - > o_amount_blinds . clear ( ) ;
blind_details - > o_asset_blinds . clear ( ) ;
for ( unsigned int i = 0 ; i < txNew . vout . size ( ) ; i + + ) {
blind_details - > o_amounts [ i ] = txNew . vout [ i ] . nValue . GetAmount ( ) ;
assert ( blind_details - > o_assets [ i ] = = txNew . vout [ i ] . nAsset . GetAsset ( ) ) ;
2021-02-10 16:06:01 -05:00
}
2021-07-19 03:15:44 +00:00
if ( sign ) {
2021-07-23 20:42:38 +00:00
int ret = BlindTransaction ( blind_details - > i_amount_blinds , blind_details - > i_asset_blinds , blind_details - > i_assets , blind_details - > i_amounts , blind_details - > o_amount_blinds , blind_details - > o_asset_blinds , blind_details - > o_pubkeys , issuance_asset_keys , issuance_token_keys , txNew ) ;
assert ( ret ! = - 1 ) ;
if ( ret ! = blind_details - > num_to_blind ) {
2023-04-15 07:04:22 +00:00
wallet . WalletLogPrintf ( " ERROR: tried to blind %d outputs but only blinded %d \n " , ( int ) blind_details - > num_to_blind , ( int ) ret ) ;
2024-10-25 12:52:06 +02:00
return util : : Error { _ ( " Unable to blind the transaction properly. This should not happen. " ) } ;
2021-07-19 03:15:44 +00:00
}
}
2021-07-23 20:42:38 +00:00
}
2021-07-19 03:15:44 +00:00
2021-07-23 20:42:38 +00:00
// Release any change keys that we didn't use.
for ( const auto & it : mapScriptChange ) {
int index = it . second . first ;
if ( index < 0 ) {
continue ;
2021-02-10 16:06:01 -05:00
}
2021-07-23 20:42:38 +00:00
reservedest [ index ] - > ReturnDestination ( ) ;
}
2021-02-10 16:06:01 -05:00
2021-07-23 20:42:38 +00:00
if ( sign ) {
2023-04-14 06:13:01 +00:00
if ( ! wallet . SignTransaction ( txNew ) ) {
2024-10-25 12:52:06 +02:00
return util : : Error { _ ( " Signing transaction failed " ) } ;
2021-02-10 16:06:01 -05:00
}
}
2021-07-23 20:42:38 +00:00
// Normalize the witness in case it is not serialized before mempool
if ( ! txNew . HasWitness ( ) ) {
txNew . witness . SetNull ( ) ;
2021-05-20 14:53:36 -04:00
}
2021-02-10 16:06:01 -05:00
2021-05-20 14:53:36 -04:00
// Return the constructed transaction data.
2022-04-08 16:43:10 -03:00
CTransactionRef tx = MakeTransactionRef ( std : : move ( txNew ) ) ;
2021-02-10 16:06:01 -05:00
2021-05-20 14:53:36 -04:00
// Limit size
if ( ( sign & & GetTransactionWeight ( * tx ) > MAX_STANDARD_TX_WEIGHT ) | |
( ! sign & & tx_sizes . weight > MAX_STANDARD_TX_WEIGHT ) )
{
2022-07-21 08:30:39 -04:00
return util : : Error { _ ( " Transaction too large " ) } ;
2021-02-10 16:06:01 -05:00
}
2022-12-05 15:31:30 -05:00
if ( current_fee > wallet . m_default_max_tx_fee ) {
2022-07-21 08:30:39 -04:00
return util : : Error { TransactionErrorString ( TransactionError : : MAX_FEE_EXCEEDED ) } ;
2021-02-10 16:06:01 -05:00
}
if ( gArgs . GetBoolArg ( " -walletrejectlongchains " , DEFAULT_WALLET_REJECT_LONG_CHAINS ) ) {
// Lastly, ensure this tx will pass the mempool's chain limits
2021-02-12 18:01:22 -05:00
if ( ! wallet . chain ( ) . checkChainLimits ( tx ) ) {
2022-07-21 08:30:39 -04:00
return util : : Error { _ ( " Transaction has too long of a mempool chain " ) } ;
2021-02-10 16:06:01 -05:00
}
}
// Before we return success, we assume any change key will be used to prevent
// accidental re-use.
2021-07-19 03:15:44 +00:00
for ( auto & reservedest_ : reservedest ) {
reservedest_ - > KeepDestination ( ) ;
}
2021-02-10 16:06:01 -05:00
2021-02-12 18:01:22 -05:00
wallet . WalletLogPrintf ( " Fee Calculation: Fee:%d Bytes:%u 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 " ,
2022-12-05 15:31:30 -05:00
current_fee , nBytes , feeCalc . returnedTarget , feeCalc . desiredTarget , StringForFeeReason ( feeCalc . reason ) , feeCalc . est . decay ,
2021-02-10 16:06:01 -05:00
feeCalc . est . pass . start , feeCalc . est . pass . end ,
( feeCalc . est . pass . totalConfirmed + feeCalc . est . pass . inMempool + feeCalc . est . pass . leftMempool ) > 0.0 ? 100 * feeCalc . est . pass . withinTarget / ( feeCalc . est . pass . totalConfirmed + feeCalc . est . pass . inMempool + feeCalc . est . pass . leftMempool ) : 0.0 ,
feeCalc . est . pass . withinTarget , feeCalc . est . pass . totalConfirmed , feeCalc . est . pass . inMempool , feeCalc . est . pass . leftMempool ,
feeCalc . est . fail . start , feeCalc . est . fail . end ,
( feeCalc . est . fail . totalConfirmed + feeCalc . est . fail . inMempool + feeCalc . est . fail . leftMempool ) > 0.0 ? 100 * feeCalc . est . fail . withinTarget / ( feeCalc . est . fail . totalConfirmed + feeCalc . est . fail . inMempool + feeCalc . est . fail . leftMempool ) : 0.0 ,
feeCalc . est . fail . withinTarget , feeCalc . est . fail . totalConfirmed , feeCalc . est . fail . inMempool , feeCalc . est . fail . leftMempool ) ;
2022-12-05 15:31:30 -05:00
return CreatedTransactionResult ( tx , current_fee , nChangePosInOut , feeCalc ) ;
2021-02-10 16:06:01 -05:00
}
2022-07-21 08:30:39 -04:00
util : : Result < CreatedTransactionResult > CreateTransaction (
2021-02-12 18:01:22 -05:00
CWallet & wallet ,
2021-02-10 16:06:01 -05:00
const std : : vector < CRecipient > & vecSend ,
2020-12-13 03:15:40 +01:00
int change_pos ,
2021-02-10 16:06:01 -05:00
const CCoinControl & coin_control ,
2021-07-19 03:15:44 +00:00
bool sign ,
BlindDetails * blind_details ,
const IssuanceDetails * issuance_details )
2021-02-10 16:06:01 -05:00
{
2021-05-17 16:23:08 -04:00
if ( vecSend . empty ( ) ) {
2022-07-21 08:30:39 -04:00
return util : : Error { _ ( " Transaction must have at least one recipient " ) } ;
2021-05-17 16:23:08 -04:00
}
if ( std : : any_of ( vecSend . cbegin ( ) , vecSend . cend ( ) , [ ] ( const auto & recipient ) { return recipient . nAmount < 0 ; } ) ) {
2022-07-21 08:30:39 -04:00
return util : : Error { _ ( " Transaction amounts must not be negative " ) } ;
2021-05-17 16:23:08 -04:00
}
2021-07-23 20:42:38 +00:00
// ELEMENTS
if ( g_con_elementsmode ) {
if ( std : : any_of ( vecSend . cbegin ( ) , vecSend . cend ( ) , [ ] ( const auto & recipient ) { return recipient . asset . IsNull ( ) ; } ) ) {
2024-10-25 12:52:06 +02:00
return util : : Error { _ ( " No asset provided for recipient " ) } ;
2021-07-23 20:42:38 +00:00
}
}
2021-02-12 18:01:22 -05:00
LOCK ( wallet . cs_wallet ) ;
2021-05-20 14:51:50 -04:00
2024-10-15 21:23:33 +00:00
auto res = CreateTransactionInternal ( wallet , vecSend , change_pos , coin_control , sign , blind_details , issuance_details ) ;
2022-07-21 08:30:39 -04:00
TRACE4 ( coin_selection , normal_create_tx_internal , wallet . GetName ( ) . c_str ( ) , bool ( res ) ,
res ? res - > fee : 0 , res ? res - > change_pos : 0 ) ;
2022-04-08 16:43:10 -03:00
if ( ! res ) return res ;
2022-07-21 08:30:39 -04:00
const auto & txr_ungrouped = * res ;
2021-02-10 16:06:01 -05:00
// try with avoidpartialspends unless it's enabled already
2022-04-08 16:43:10 -03:00
if ( txr_ungrouped . fee > 0 /* 0 means non-functional fee rate estimation */ & & wallet . m_max_aps_fee > - 1 & & ! coin_control . m_avoid_partial_spends ) {
2022-03-21 14:33:29 -04:00
TRACE1 ( coin_selection , attempting_aps_create_tx , wallet . GetName ( ) . c_str ( ) ) ;
2021-02-10 16:06:01 -05:00
CCoinControl tmp_cc = coin_control ;
tmp_cc . m_avoid_partial_spends = true ;
2023-02-06 15:27:57 -05:00
2025-04-08 10:24:50 +02:00
// ELEMENTS: only for unblinded transactions
2023-02-06 15:27:57 -05:00
// Re-use the change destination from the first creation attempt to avoid skipping BIP44 indexes
const int ungrouped_change_pos = txr_ungrouped . change_pos ;
2025-04-08 10:24:50 +02:00
if ( ungrouped_change_pos ! = - 1 & & ! blind_details ) {
const CAsset & asset = txr_ungrouped . tx - > vout [ ungrouped_change_pos ] . nAsset . GetAsset ( ) ;
ExtractDestination ( txr_ungrouped . tx - > vout [ ungrouped_change_pos ] . scriptPubKey , tmp_cc . destChange [ asset ] ) ;
2023-02-06 15:27:57 -05:00
}
2021-07-19 03:15:44 +00:00
BlindDetails blind_details2 ;
BlindDetails * blind_details2_ptr = blind_details ? & blind_details2 : nullptr ;
2024-10-25 12:52:06 +02:00
auto txr_grouped = CreateTransactionInternal ( wallet , vecSend , change_pos , tmp_cc , sign , blind_details2_ptr , issuance_details ) ;
2022-04-27 16:27:28 +02:00
// if fee of this alternative one is within the range of the max fee, we use this one
2022-04-08 16:43:10 -03:00
const bool use_aps { txr_grouped . has_value ( ) ? ( txr_grouped - > fee < = txr_ungrouped . fee + wallet . m_max_aps_fee ) : false } ;
2022-04-27 16:27:28 +02:00
TRACE5 ( coin_selection , aps_create_tx_internal , wallet . GetName ( ) . c_str ( ) , use_aps , txr_grouped . has_value ( ) ,
txr_grouped . has_value ( ) ? txr_grouped - > fee : 0 , txr_grouped . has_value ( ) ? txr_grouped - > change_pos : 0 ) ;
2020-12-13 01:37:40 +01:00
if ( txr_grouped ) {
2020-12-13 03:15:40 +01:00
wallet . WalletLogPrintf ( " Fee non-grouped = %lld, grouped = %lld, using %s \n " ,
2022-04-08 16:43:10 -03:00
txr_ungrouped . fee , txr_grouped - > fee , use_aps ? " grouped " : " non-grouped " ) ;
2021-02-10 16:06:01 -05:00
if ( use_aps ) {
2025-04-08 10:24:50 +02:00
if ( blind_details ) {
2021-07-19 03:15:44 +00:00
* blind_details = blind_details2 ;
}
2024-10-25 12:52:06 +02:00
return txr_grouped ;
2021-02-10 16:06:01 -05:00
}
}
}
2022-04-08 16:43:10 -03:00
return res ;
2021-02-10 16:06:01 -05:00
}
2021-02-12 18:01:22 -05:00
bool FundTransaction ( CWallet & wallet , CMutableTransaction & tx , CAmount & nFeeRet , int & nChangePosInOut , bilingual_str & error , bool lockUnspents , const std : : set < int > & setSubtractFeeFromOutputs , CCoinControl coinControl )
2021-02-10 16:06:01 -05:00
{
std : : vector < CRecipient > vecSend ;
// Turn the txout set into a CRecipient vector.
for ( size_t idx = 0 ; idx < tx . vout . size ( ) ; idx + + ) {
const CTxOut & txOut = tx . vout [ idx ] ;
2021-07-19 03:15:44 +00:00
// ELEMENTS:
if ( ! txOut . nValue . IsExplicit ( ) | | ! txOut . nAsset . IsExplicit ( ) ) {
error = _ ( " Pre-funded amounts must be non-blinded " ) ;
return false ;
}
// Fee outputs should not be added to avoid overpayment of fees
if ( txOut . IsFee ( ) ) {
continue ;
}
CRecipient recipient = { txOut . scriptPubKey , txOut . nValue . GetAmount ( ) , txOut . nAsset . GetAsset ( ) , CPubKey ( txOut . nNonce . vchCommitment ) , setSubtractFeeFromOutputs . count ( idx ) = = 1 } ;
2021-02-10 16:06:01 -05:00
vecSend . push_back ( recipient ) ;
}
// Acquire the locks to prevent races to the new locked unspents between the
// CreateTransaction call and LockCoin calls (when lockUnspents is true).
2021-02-12 18:01:22 -05:00
LOCK ( wallet . cs_wallet ) ;
2021-02-10 16:06:01 -05:00
2024-09-10 23:33:20 +00:00
// Check any existing inputs for peg-in data and add to external txouts if so
2022-05-18 08:25:04 +02:00
// Fetch specified UTXOs from the UTXO set to get the scriptPubKeys and values of the outputs being selected
// and to match with the given solving_data. Only used for non-wallet outputs.
2024-09-10 23:33:20 +00:00
const auto & fedpegscripts = GetValidFedpegScripts ( wallet . chain ( ) . getTip ( ) , Params ( ) . GetConsensus ( ) , true /* nextblock_validation */ ) ;
2022-05-18 08:25:04 +02:00
std : : map < COutPoint , Coin > coins ;
2024-09-10 23:33:20 +00:00
for ( unsigned int i = 0 ; i < tx . vin . size ( ) ; + + i ) {
const CTxIn & txin = tx . vin [ i ] ;
2022-05-18 08:25:04 +02:00
coins [ txin . prevout ] ; // Create empty map entry keyed by prevout.
2024-09-10 23:33:20 +00:00
if ( txin . m_is_pegin ) {
std : : string err ;
if ( tx . witness . vtxinwit . size ( ) ! = tx . vin . size ( ) | | ! IsValidPeginWitness ( tx . witness . vtxinwit [ i ] . m_pegin_witness , fedpegscripts , txin . prevout , err , false ) ) {
throw JSONRPCError ( RPC_INVALID_PARAMETER , strprintf ( " Transaction contains invalid peg-in input: %s " , err ) ) ;
}
CScriptWitness & pegin_witness = tx . witness . vtxinwit [ i ] . m_pegin_witness ;
CTxOut txout = GetPeginOutputFromWitness ( pegin_witness ) ;
coinControl . SelectExternal ( txin . prevout , txout ) ;
}
2022-05-18 08:25:04 +02:00
}
wallet . chain ( ) . findCoins ( coins ) ;
for ( const CTxIn & txin : tx . vin ) {
const auto & outPoint = txin . prevout ;
2022-07-22 15:30:10 -04:00
if ( wallet . IsMine ( outPoint ) ) {
// The input was found in the wallet, so select as internal
2022-05-18 08:25:04 +02:00
coinControl . Select ( outPoint ) ;
2024-11-04 09:18:59 +02:00
} else if ( txin . m_is_pegin ) {
// ELEMENTS: input is pegin so nothing to select
2022-07-22 15:30:10 -04:00
} else if ( coins [ outPoint ] . out . IsNull ( ) ) {
error = _ ( " Unable to find UTXO for external input " ) ;
return false ;
} else {
// The input was not in the wallet, but is in the UTXO set, so select as external
coinControl . SelectExternal ( outPoint , coins [ outPoint ] . out ) ;
2022-05-18 08:25:04 +02:00
}
}
2021-07-19 03:15:44 +00:00
auto blind_details = g_con_elementsmode ? std : : make_unique < BlindDetails > ( ) : nullptr ;
2024-10-15 21:23:33 +00:00
auto res = CreateTransaction ( wallet , vecSend , nChangePosInOut , coinControl , false , blind_details . get ( ) ) ;
2022-04-08 16:43:10 -03:00
if ( ! res ) {
2022-07-21 08:30:39 -04:00
error = util : : ErrorString ( res ) ;
2022-04-08 16:43:10 -03:00
return false ;
}
2022-07-21 08:30:39 -04:00
const auto & txr = * res ;
2022-04-08 16:43:10 -03:00
CTransactionRef tx_new = txr . tx ;
nFeeRet = txr . fee ;
nChangePosInOut = txr . change_pos ;
2021-02-10 16:06:01 -05:00
2021-07-19 03:15:44 +00:00
// Wipe outputs and output witness and re-add one by one
tx . vout . clear ( ) ;
tx . witness . vtxoutwit . clear ( ) ;
for ( unsigned int i = 0 ; i < tx_new - > vout . size ( ) ; i + + ) {
const CTxOut & out = tx_new - > vout [ i ] ;
tx . vout . push_back ( out ) ;
if ( tx_new - > witness . vtxoutwit . size ( ) > i ) {
// We want to re-add previously existing outwitnesses
// even though we don't create any new ones
const CTxOutWitness & outwit = tx_new - > witness . vtxoutwit [ i ] ;
tx . witness . vtxoutwit . push_back ( outwit ) ;
}
2021-02-10 16:06:01 -05:00
}
// Add new txins while keeping original txin scriptSig/order.
for ( const CTxIn & txin : tx_new - > vin ) {
if ( ! coinControl . IsSelected ( txin . prevout ) ) {
tx . vin . push_back ( txin ) ;
}
if ( lockUnspents ) {
2021-02-12 18:01:22 -05:00
wallet . LockCoin ( txin . prevout ) ;
2021-02-10 16:06:01 -05:00
}
}
return true ;
}
2021-11-12 11:13:29 -05:00
} // namespace wallet