@@ -2409,17 +2409,10 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, const CoinEligibil
2409
2409
effective_feerate = coin_selection_params.m_effective_feerate ;
2410
2410
}
2411
2411
2412
- // Cost of change is the cost of creating the change output + cost of spending the change output in the future.
2413
- // For creating the change output now, we use the effective feerate.
2414
- // For spending the change output in the future, we use the discard feerate for now.
2415
- // So cost of change = (change output size * effective feerate) + (size of spending change output * discard feerate)
2416
- const CAmount change_fee = coin_selection_params.m_effective_feerate .GetFee (coin_selection_params.change_output_size );
2417
- const CAmount cost_of_change = coin_selection_params.m_discard_feerate .GetFee (coin_selection_params.change_spend_size ) + change_fee;
2418
-
2419
2412
if (coin_selection_params.use_bnb ) {
2420
2413
std::vector<OutputGroup> positive_groups = GroupOutputs (coins, !coin_selection_params.m_avoid_partial_spends , effective_feerate, coin_selection_params.m_long_term_feerate , eligibility_filter, true /* positive_only */ );
2421
2414
bnb_used = true ;
2422
- return SelectCoinsBnB (positive_groups, nTargetValue, cost_of_change , setCoinsRet, nValueRet, not_input_fees);
2415
+ return SelectCoinsBnB (positive_groups, nTargetValue, coin_selection_params. m_cost_of_change , setCoinsRet, nValueRet, not_input_fees);
2423
2416
} else {
2424
2417
// 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.
2425
2418
// The knapsack solver currently does not use effective values, so we give GroupOutputs feerates of 0 so it sets the effective values to be the same as the real value.
@@ -2885,6 +2878,16 @@ bool CWallet::CreateTransactionInternal(
2885
2878
CTxOut change_prototype_txout (0 , scriptChange);
2886
2879
coin_selection_params.change_output_size = GetSerializeSize (change_prototype_txout);
2887
2880
2881
+ // Get size of spending the change output
2882
+ int change_spend_size = CalculateMaximumSignedInputSize (change_prototype_txout, this );
2883
+ // If the wallet doesn't know how to sign change output, assume p2sh-p2wpkh
2884
+ // as lower-bound to allow BnB to do it's thing
2885
+ if (change_spend_size == -1 ) {
2886
+ coin_selection_params.change_spend_size = DUMMY_NESTED_P2WPKH_INPUT_SIZE;
2887
+ } else {
2888
+ coin_selection_params.change_spend_size = (size_t )change_spend_size;
2889
+ }
2890
+
2888
2891
// Set discard feerate
2889
2892
coin_selection_params.m_discard_feerate = GetDiscardRate (*this );
2890
2893
@@ -2907,6 +2910,14 @@ bool CWallet::CreateTransactionInternal(
2907
2910
cc_temp.m_confirm_target = chain ().estimateMaxBlocks ();
2908
2911
coin_selection_params.m_long_term_feerate = GetMinimumFeeRate (*this , cc_temp, nullptr );
2909
2912
2913
+ // Calculate the cost of change
2914
+ // Cost of change is the cost of creating the change output + cost of spending the change output in the future.
2915
+ // For creating the change output now, we use the effective feerate.
2916
+ // For spending the change output in the future, we use the discard feerate for now.
2917
+ // So cost of change = (change output size * effective feerate) + (size of spending change output * discard feerate)
2918
+ coin_selection_params.m_change_fee = coin_selection_params.m_effective_feerate .GetFee (coin_selection_params.change_output_size );
2919
+ 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 ;
2920
+
2910
2921
nFeeRet = 0 ;
2911
2922
bool pick_new_inputs = true ;
2912
2923
CAmount nValueIn = 0 ;
@@ -2972,14 +2983,6 @@ bool CWallet::CreateTransactionInternal(
2972
2983
if (pick_new_inputs) {
2973
2984
nValueIn = 0 ;
2974
2985
setCoins.clear ();
2975
- int change_spend_size = CalculateMaximumSignedInputSize (change_prototype_txout, this );
2976
- // If the wallet doesn't know how to sign change output, assume p2sh-p2wpkh
2977
- // as lower-bound to allow BnB to do it's thing
2978
- if (change_spend_size == -1 ) {
2979
- coin_selection_params.change_spend_size = DUMMY_NESTED_P2WPKH_INPUT_SIZE;
2980
- } else {
2981
- coin_selection_params.change_spend_size = (size_t )change_spend_size;
2982
- }
2983
2986
if (!SelectCoins (vAvailableCoins, nValueToSelect, setCoins, nValueIn, coin_control, coin_selection_params, bnb_used))
2984
2987
{
2985
2988
// If BnB was used, it was the first pass. No longer the first pass and continue loop with knapsack.
0 commit comments