Bitcoin Core 29.99.0
P2P Digital Currency
spend.h
Go to the documentation of this file.
1// Copyright (c) 2021-present 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>
10#include <util/result.h>
12#include <wallet/transaction.h>
13#include <wallet/wallet.h>
14
15#include <map>
16#include <memory>
17#include <optional>
18#include <set>
19#include <unordered_set>
20#include <vector>
21
22namespace wallet {
25int CalculateMaximumSignedInputSize(const CTxOut& txout, const CWallet* pwallet, const CCoinControl* coin_control);
26int CalculateMaximumSignedInputSize(const CTxOut& txout, const COutPoint outpoint, const SigningProvider* pwallet, bool can_grind_r, const CCoinControl* coin_control);
27struct TxSize {
28 int64_t vsize{-1};
29 int64_t weight{-1};
30};
31
34TxSize CalculateMaximumSignedTxSize(const CTransaction& tx, const CWallet* wallet, const std::vector<CTxOut>& txouts, const CCoinControl* coin_control = nullptr);
35TxSize CalculateMaximumSignedTxSize(const CTransaction& tx, const CWallet* wallet, const CCoinControl* coin_control = nullptr) EXCLUSIVE_LOCKS_REQUIRED(wallet->cs_wallet);
36
46 std::map<OutputType, std::vector<COutput>> coins;
47
49 std::vector<COutput> All() const;
50
53 size_t Size() const;
55 size_t TypesCount() const { return coins.size(); }
56 void Clear();
57 void Erase(const std::unordered_set<COutPoint, SaltedOutpointHasher>& coins_to_remove);
58 void Shuffle(FastRandomContext& rng_fast);
59 void Add(OutputType type, const COutput& out);
60
61 CAmount GetTotalAmount() { return total_amount; }
62 std::optional<CAmount> GetEffectiveTotalAmount() {return total_effective_amount; }
63
64private:
66 CAmount total_amount{0};
68 std::optional<CAmount> total_effective_amount{0};
69};
70
72 // Outputs below the minimum amount will not get selected
74 // Outputs above the maximum amount will not get selected
76 // Return outputs until the minimum sum amount is covered
78 // Maximum number of outputs that can be returned
79 uint64_t max_count{0};
80 // By default, do not include immature coinbase outputs
82 // By default, skip locked UTXOs
83 bool skip_locked{true};
84 // When true, filter unconfirmed coins by whether their
85 // version's TRUCness matches what is set by CCoinControl.
87};
88
92CoinsResult AvailableCoins(const CWallet& wallet,
93 const CCoinControl* coinControl = nullptr,
94 std::optional<CFeeRate> feerate = std::nullopt,
95 const CoinFilterParams& params = {}) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);
96
100const CTxOut& FindNonChangeParentOutput(const CWallet& wallet, const COutPoint& outpoint) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);
101
105std::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
136util::Result<SelectionResult> AttemptSelection(interfaces::Chain& chain, const CAmount& nTargetValue, OutputGroupTypeMap& groups,
137 const CoinSelectionParams& coin_selection_params, bool allow_mixed_output_types);
138
153util::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 {
170 }
171 coins.insert(std::make_shared<COutput>(output));
172 }
173};
174
179util::Result<PreSelectedInputs> FetchSelectedInputs(const CWallet& wallet, const CCoinControl& coin_control,
180 const CoinSelectionParams& coin_selection_params) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);
181
194util::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
201util::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
220void DiscourageFeeSniping(CMutableTransaction& tx, FastRandomContext& rng_fast, interfaces::Chain& chain, const uint256& block_hash, int block_height);
221
227util::Result<CreatedTransactionResult> CreateTransaction(CWallet& wallet, const std::vector<CRecipient>& vecSend, std::optional<unsigned int> change_pos, const CCoinControl& coin_control, bool sign = true);
228
233util::Result<CreatedTransactionResult> FundTransaction(CWallet& wallet, const CMutableTransaction& tx, const std::vector<CRecipient>& recipients, std::optional<unsigned int> change_pos, bool lockUnspents, CCoinControl);
234} // namespace wallet
235
236#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:386
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:130
256-bit opaque blob.
Definition: uint256.h:196
static int sign(const secp256k1_context *ctx, struct signer_secrets *signer_secrets, struct signer *signer, const secp256k1_musig_keyagg_cache *cache, const unsigned char *msg32, unsigned char *sig64)
Definition: musig.c:106
const CTxOut & FindNonChangeParentOutput(const CWallet &wallet, const COutPoint &outpoint)
Find non-change parent output.
Definition: spend.cpp:524
CreatedTransactionResult FundTransaction(CWallet &wallet, const CMutableTransaction &tx, const std::vector< CRecipient > &recipients, const UniValue &options, CCoinControl &coinControl, bool override_min_fee)
Definition: spend.cpp:518
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:862
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:568
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:1406
util::Result< PreSelectedInputs > FetchSelectedInputs(const CWallet &wallet, const CCoinControl &coin_control, const CoinSelectionParams &coin_selection_params)
Fetch and validate coin control selected inputs.
Definition: spend.cpp:269
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:698
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:725
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:810
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:544
void DiscourageFeeSniping(CMutableTransaction &tx, FastRandomContext &rng_fast, interfaces::Chain &chain, const uint256 &block_hash, int block_height)
Set a height-based locktime for new transactions (uses the height of the current chain tip unless we ...
Definition: spend.cpp:981
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:144
int CalculateMaximumSignedInputSize(const CTxOut &txout, const COutPoint outpoint, const SigningProvider *provider, bool can_grind_r, const CCoinControl *coin_control)
Definition: spend.cpp:92
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:320
OutputType
Definition: outputtype.h:17
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:423
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:79
bool check_version_trucness
Definition: spend.h:86
bool include_immature_coinbase
Definition: spend.h:81
CAmount min_sum_amount
Definition: spend.h:77
COutputs available for spending, stored by OutputType.
Definition: spend.h:45
std::optional< CAmount > GetEffectiveTotalAmount()
Definition: spend.h:62
size_t TypesCount() const
Return how many different output types this struct stores.
Definition: spend.h:55
std::map< OutputType, std::vector< COutput > > coins
Definition: spend.h:46
CAmount GetTotalAmount()
Definition: spend.h:61
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:28
int64_t weight
Definition: spend.h:29
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:51