Bitcoin Core  27.99.0
P2P Digital Currency
spend.h
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 http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef BITCOIN_WALLET_SPEND_H
6 #define BITCOIN_WALLET_SPEND_H
7 
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>
14 
15 #include <optional>
16 
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 };
26 
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);
31 
40 struct CoinsResult {
41  std::map<OutputType, std::vector<COutput>> coins;
42 
44  std::vector<COutput> All() const;
45 
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);
55 
56  CAmount GetTotalAmount() { return total_amount; }
57  std::optional<CAmount> GetEffectiveTotalAmount() {return total_effective_amount; }
58 
59 private:
61  CAmount total_amount{0};
63  std::optional<CAmount> total_effective_amount{0};
64 };
65 
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 };
82 
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);
90 
95 CoinsResult AvailableCoinsListUnspent(const CWallet& wallet, const CCoinControl* coinControl = nullptr, CoinFilterParams params = {}) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);
96 
100 const CTxOut& FindNonChangeParentOutput(const CWallet& wallet, const COutPoint& outpoint) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);
101 
105 std::map<CTxDestination, std::vector<COutput>> ListCoins(const CWallet& wallet) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);
106 
110 };
111 
116  const CoinsResult& coins,
117  const CoinSelectionParams& coin_sel_params,
118  const std::vector<SelectionFilter>& filters);
119 
136 util::Result<SelectionResult> AttemptSelection(interfaces::Chain& chain, const CAmount& nTargetValue, OutputGroupTypeMap& groups,
137  const CoinSelectionParams& coin_selection_params, bool allow_mixed_output_types);
138 
153 util::Result<SelectionResult> ChooseSelectionResult(interfaces::Chain& chain, const CAmount& nTargetValue, Groups& groups, const CoinSelectionParams& coin_selection_params);
154 
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
163 
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 };
174 
179 util::Result<PreSelectedInputs> FetchSelectedInputs(const CWallet& wallet, const CCoinControl& coin_control,
180  const CoinSelectionParams& coin_selection_params) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);
181 
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);
196 
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);
204 
206 {
210  std::optional<unsigned int> change_pos;
211 
212  CreatedTransactionResult(CTransactionRef _tx, CAmount _fee, std::optional<unsigned int> _change_pos, const FeeCalculation& _fee_calc)
213  : tx(_tx), fee(_fee), fee_calc(_fee_calc), change_pos(_change_pos) {}
214 };
215 
221 util::Result<CreatedTransactionResult> CreateTransaction(CWallet& wallet, const std::vector<CRecipient>& vecSend, std::optional<unsigned int> change_pos, const CCoinControl& coin_control, bool sign = true);
222 
227 util::Result<CreatedTransactionResult> FundTransaction(CWallet& wallet, const CMutableTransaction& tx, const std::vector<CRecipient>& recipients, std::optional<unsigned int> change_pos, bool lockUnspents, CCoinControl);
228 } // namespace wallet
229 
230 #endif // BITCOIN_WALLET_SPEND_H
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:29
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:296
An output of a transaction.
Definition: transaction.h:150
CAmount nValue
Definition: transaction.h:152
Fast randomness source.
Definition: random.h:145
An interface to be implemented by keystores that support signing.
Interface giving clients (wallet processes, maybe other analysis tools in the future) ability to acce...
Definition: chain.h:124
const CTxOut & FindNonChangeParentOutput(const CWallet &wallet, const COutPoint &outpoint)
Find non-change parent output.
Definition: spend.cpp:479
CreatedTransactionResult FundTransaction(CWallet &wallet, const CMutableTransaction &tx, const std::vector< CRecipient > &recipients, const UniValue &options, CCoinControl &coinControl, bool override_min_fee)
Definition: spend.cpp:500
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:806
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:528
util::Result< CreatedTransactionResult > CreateTransaction(CWallet &wallet, const std::vector< CRecipient > &vecSend, std::optional< unsigned 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:1337
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:262
util::Result< SelectionResult > AttemptSelection(interfaces::Chain &chain, 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:658
util::Result< SelectionResult > ChooseSelectionResult(interfaces::Chain &chain, 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:685
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:761
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:499
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:473
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:137
int CalculateMaximumSignedInputSize(const CTxOut &txout, const COutPoint outpoint, const SigningProvider *provider, bool can_grind_r, const CCoinControl *coin_control)
Definition: spend.cpp:85
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:309
OutputType
Definition: outputtype.h:17
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:423
void Shuffle(I first, I last, R &&rng)
More efficient than using std::shuffle on a FastRandomContext.
Definition: random.h:265
A mutable version of CTransaction.
Definition: transaction.h:378
A UTXO under consideration for use in funding a new transaction.
Definition: coinselection.h:28
CTxOut txout
The output itself.
Definition: coinselection.h:41
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, std::optional< unsigned int > _change_pos, const FeeCalculation &_fee_calc)
Definition: spend.h:212
std::optional< unsigned int > change_pos
Definition: spend.h:210
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:109
CoinEligibilityFilter filter
Definition: spend.h:108
int64_t vsize
Definition: spend.h:23
int64_t weight
Definition: spend.h:24
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49