Bitcoin Core  24.99.0
P2P Digital Currency
Go to the documentation of this file.
1 // Copyright (c) 2021-2022 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or
8 #include <consensus/amount.h>
9 #include <policy/fees.h> // for FeeCalculation
10 #include <util/result.h>
11 #include <wallet/coinselection.h>
12 #include <wallet/transaction.h>
13 #include <wallet/wallet.h>
15 #include <optional>
17 namespace wallet {
20 int CalculateMaximumSignedInputSize(const CTxOut& txout, const CWallet* pwallet, const CCoinControl* coin_control);
21 int CalculateMaximumSignedInputSize(const CTxOut& txout, const COutPoint outpoint, const SigningProvider* pwallet, bool can_grind_r, const CCoinControl* coin_control);
22 struct TxSize {
23  int64_t vsize{-1};
24  int64_t weight{-1};
25 };
29 TxSize CalculateMaximumSignedTxSize(const CTransaction& tx, const CWallet* wallet, const std::vector<CTxOut>& txouts, const CCoinControl* coin_control = nullptr);
30 TxSize CalculateMaximumSignedTxSize(const CTransaction& tx, const CWallet* wallet, const CCoinControl* coin_control = nullptr) EXCLUSIVE_LOCKS_REQUIRED(wallet->cs_wallet);
40 struct CoinsResult {
41  std::map<OutputType, std::vector<COutput>> coins;
44  std::vector<COutput> All() const;
48  size_t Size() const;
50  size_t TypesCount() const { return coins.size(); }
51  void Clear();
52  void Erase(const std::unordered_set<COutPoint, SaltedOutpointHasher>& coins_to_remove);
53  void Shuffle(FastRandomContext& rng_fast);
54  void Add(OutputType type, const COutput& out);
56  CAmount GetTotalAmount() { return total_amount; }
57  std::optional<CAmount> GetEffectiveTotalAmount() {return total_effective_amount; }
59 private:
61  CAmount total_amount{0};
63  std::optional<CAmount> total_effective_amount{0};
64 };
67  // Outputs below the minimum amount will not get selected
69  // Outputs above the maximum amount will not get selected
71  // Return outputs until the minimum sum amount is covered
73  // Maximum number of outputs that can be returned
74  uint64_t max_count{0};
75  // By default, return only spendable outputs
76  bool only_spendable{true};
77  // By default, do not include immature coinbase outputs
79  // By default, skip locked UTXOs
80  bool skip_locked{true};
81 };
86 CoinsResult AvailableCoins(const CWallet& wallet,
87  const CCoinControl* coinControl = nullptr,
88  std::optional<CFeeRate> feerate = std::nullopt,
89  const CoinFilterParams& params = {}) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);
95 CoinsResult AvailableCoinsListUnspent(const CWallet& wallet, const CCoinControl* coinControl = nullptr, CoinFilterParams params = {}) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);
97 CAmount GetAvailableBalance(const CWallet& wallet, const CCoinControl* coinControl = nullptr);
102 const CTxOut& FindNonChangeParentOutput(const CWallet& wallet, const COutPoint& outpoint) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);
107 std::map<CTxDestination, std::vector<COutput>> ListCoins(const CWallet& wallet) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);
112 };
118  const CoinsResult& coins,
119  const CoinSelectionParams& coin_sel_params,
120  const std::vector<SelectionFilter>& filters);
137 util::Result<SelectionResult> AttemptSelection(const CAmount& nTargetValue, OutputGroupTypeMap& groups,
138  const CoinSelectionParams& coin_selection_params, bool allow_mixed_output_types);
153 util::Result<SelectionResult> ChooseSelectionResult(const CAmount& nTargetValue, Groups& groups, const CoinSelectionParams& coin_selection_params);
155 // User manually selected inputs that must be part of the transaction
157 {
158  std::set<std::shared_ptr<COutput>> coins;
159  // If subtract fee from outputs is disabled, the 'total_amount'
160  // will be the sum of each output effective value
161  // instead of the sum of the outputs amount
164  void Insert(const COutput& output, bool subtract_fee_outputs)
165  {
166  if (subtract_fee_outputs) {
167  total_amount += output.txout.nValue;
168  } else {
169  total_amount += output.GetEffectiveValue();
170  }
171  coins.insert(std::make_shared<COutput>(output));
172  }
173 };
179 util::Result<PreSelectedInputs> FetchSelectedInputs(const CWallet& wallet, const CCoinControl& coin_control,
180  const CoinSelectionParams& coin_selection_params) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);
194 util::Result<SelectionResult> AutomaticCoinSelection(const CWallet& wallet, CoinsResult& available_coins, const CAmount& nTargetValue,
195  const CoinSelectionParams& coin_selection_params) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);
201 util::Result<SelectionResult> SelectCoins(const CWallet& wallet, CoinsResult& available_coins, const PreSelectedInputs& pre_set_inputs,
202  const CAmount& nTargetValue, const CCoinControl& coin_control,
203  const CoinSelectionParams& coin_selection_params) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);
206 {
212  CreatedTransactionResult(CTransactionRef _tx, CAmount _fee, int _change_pos, const FeeCalculation& _fee_calc)
213  : tx(_tx), fee(_fee), fee_calc(_fee_calc), change_pos(_change_pos) {}
214 };
221 util::Result<CreatedTransactionResult> CreateTransaction(CWallet& wallet, const std::vector<CRecipient>& vecSend, int change_pos, const CCoinControl& coin_control, bool sign = true);
227 bool FundTransaction(CWallet& wallet, CMutableTransaction& tx, CAmount& nFeeRet, int& nChangePosInOut, bilingual_str& error, bool lockUnspents, const std::set<int>& setSubtractFeeFromOutputs, CCoinControl);
228 } // namespace wallet
static constexpr CAmount MAX_MONEY
No amount larger than this (in satoshi) is valid.
Definition: amount.h:26
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:36
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:295
An output of a transaction.
Definition: transaction.h:158
CAmount nValue
Definition: transaction.h:160
Fast randomness source.
Definition: random.h:144
An interface to be implemented by keystores that support signing.
bool error(const char *fmt, const Args &... args)
Definition: logging.h:261
Definition: node.h:39
const CTxOut & FindNonChangeParentOutput(const CWallet &wallet, const COutPoint &outpoint)
Find non-change parent output.
Definition: spend.cpp:365
util::Result< SelectionResult > AutomaticCoinSelection(const CWallet &wallet, CoinsResult &available_coins, const CAmount &value_to_select, const CoinSelectionParams &coin_selection_params)
Select a set of coins such that nTargetValue is met; never select unconfirmed coins if they are not o...
Definition: spend.cpp:651
CAmount GetAvailableBalance(const CWallet &wallet, const CCoinControl *coinControl)
Definition: spend.cpp:359
FilteredOutputGroups GroupOutputs(const CWallet &wallet, const CoinsResult &coins, const CoinSelectionParams &coin_sel_params, const std::vector< SelectionFilter > &filters, std::vector< OutputGroup > &ret_discarded_groups)
Definition: spend.cpp:407
util::Result< PreSelectedInputs > FetchSelectedInputs(const CWallet &wallet, const CCoinControl &coin_control, const CoinSelectionParams &coin_selection_params) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
Fetch and validate coin control selected inputs.
Definition: spend.cpp:160
util::Result< CreatedTransactionResult > CreateTransaction(CWallet &wallet, const std::vector< CRecipient > &vecSend, int change_pos, const CCoinControl &coin_control, bool sign)
Create a new transaction paying the recipients with a set of coins selected by SelectCoins(); Also cr...
Definition: spend.cpp:1136
util::Result< SelectionResult > SelectCoins(const CWallet &wallet, CoinsResult &available_coins, const PreSelectedInputs &pre_set_inputs, const CAmount &nTargetValue, const CCoinControl &coin_control, const CoinSelectionParams &coin_selection_params)
Select all coins from coin_control, and if coin_control 'm_allow_other_inputs=true',...
Definition: spend.cpp:606
std::map< CTxDestination, std::vector< COutput > > ListCoins(const CWallet &wallet)
Return list of available coins and locked coins grouped by non-change output address.
Definition: spend.cpp:385
CoinsResult AvailableCoinsListUnspent(const CWallet &wallet, const CCoinControl *coinControl, CoinFilterParams params)
Wrapper function for AvailableCoins which skips the feerate and CoinFilterParams::only_spendable para...
Definition: spend.cpp:353
void FundTransaction(CWallet &wallet, CMutableTransaction &tx, CAmount &fee_out, int &change_position, const UniValue &options, CCoinControl &coinControl, bool override_min_fee)
Definition: spend.cpp:492
std::map< CoinEligibilityFilter, OutputGroupTypeMap > FilteredOutputGroups
TxSize CalculateMaximumSignedTxSize(const CTransaction &tx, const CWallet *wallet, const std::vector< CTxOut > &txouts, const CCoinControl *coin_control)
Calculate the size of the transaction using CoinControl to determine whether to expect signature grin...
Definition: spend.cpp:50
int CalculateMaximumSignedInputSize(const CTxOut &txout, const COutPoint outpoint, const SigningProvider *provider, bool can_grind_r, const CCoinControl *coin_control)
Definition: spend.cpp:33
util::Result< SelectionResult > ChooseSelectionResult(const CAmount &nTargetValue, Groups &groups, const CoinSelectionParams &coin_selection_params)
Attempt to find a valid input set that meets the provided eligibility filter and target.
Definition: spend.cpp:564
util::Result< SelectionResult > AttemptSelection(const CAmount &nTargetValue, OutputGroupTypeMap &groups, const CoinSelectionParams &coin_selection_params, bool allow_mixed_output_types)
Attempt to find a valid input set that preserves privacy by not mixing OutputTypes.
Definition: spend.cpp:537
CoinsResult AvailableCoins(const CWallet &wallet, const CCoinControl *coinControl, std::optional< CFeeRate > feerate, const CoinFilterParams &params)
Populate the CoinsResult struct with vectors of available COutputs, organized by OutputType.
Definition: spend.cpp:204
Definition: outputtype.h:17
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:421
void Shuffle(I first, I last, R &&rng)
More efficient than using std::shuffle on a FastRandomContext.
Definition: random.h:260
A mutable version of CTransaction.
Definition: transaction.h:380
Bilingual messages:
Definition: translation.h:18
A UTXO under consideration for use in funding a new transaction.
Definition: coinselection.h:26
CTxOut txout
The output itself.
Definition: coinselection.h:39
CAmount GetEffectiveValue() const
Parameters for filtering which OutputGroups we may use in coin selection.
uint64_t max_count
Definition: spend.h:74
bool include_immature_coinbase
Definition: spend.h:78
CAmount min_sum_amount
Definition: spend.h:72
COutputs available for spending, stored by OutputType.
Definition: spend.h:40
std::optional< CAmount > GetEffectiveTotalAmount()
Definition: spend.h:57
size_t TypesCount() const
Return how many different output types this struct stores.
Definition: spend.h:50
std::map< OutputType, std::vector< COutput > > coins
Definition: spend.h:41
CAmount GetTotalAmount()
Definition: spend.h:56
CreatedTransactionResult(CTransactionRef _tx, CAmount _fee, int _change_pos, const FeeCalculation &_fee_calc)
Definition: spend.h:212
std::set< std::shared_ptr< COutput > > coins
Definition: spend.h:158
void Insert(const COutput &output, bool subtract_fee_outputs)
Definition: spend.h:164
bool allow_mixed_output_types
Definition: spend.h:111
CoinEligibilityFilter filter
Definition: spend.h:110
int64_t vsize
Definition: spend.h:23
int64_t weight
Definition: spend.h:24
Definition: threadsafety.h:49