Bitcoin Core  22.99.0
P2P Digital Currency
spend.h
Go to the documentation of this file.
1 // Copyright (c) 2021 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 <wallet/coinselection.h>
10 #include <wallet/transaction.h>
11 #include <wallet/wallet.h>
12 
13 namespace wallet {
15 int GetTxSpendSize(const CWallet& wallet, const CWalletTx& wtx, unsigned int out, bool use_max_sig = false);
16 
17 class COutput
18 {
19 public:
20  const CWalletTx *tx;
21 
23  int i;
24 
30  int nDepth;
31 
34 
36  bool fSpendable;
37 
39  bool fSolvable;
40 
43 
49  bool fSafe;
50 
51  COutput(const CWallet& wallet, const CWalletTx& wtx, int iIn, int nDepthIn, bool fSpendableIn, bool fSolvableIn, bool fSafeIn, bool use_max_sig_in = false)
52  {
53  tx = &wtx; i = iIn; nDepth = nDepthIn; fSpendable = fSpendableIn; fSolvable = fSolvableIn; fSafe = fSafeIn; nInputBytes = -1; use_max_sig = use_max_sig_in;
54  // If known and signable by the given wallet, compute nInputBytes
55  // Failure will keep this value -1
56  if (fSpendable) {
58  }
59  }
60 
61  std::string ToString() const;
62 
63  inline CInputCoin GetInputCoin() const
64  {
65  return CInputCoin(tx->tx, i, nInputBytes);
66  }
67 };
68 
69 //Get the marginal bytes of spending the specified output
70 int CalculateMaximumSignedInputSize(const CTxOut& txout, const CWallet* pwallet, bool use_max_sig = false);
71 int CalculateMaximumSignedInputSize(const CTxOut& txout, const SigningProvider* pwallet, bool use_max_sig = false);
72 
73 struct TxSize {
74  int64_t vsize{-1};
75  int64_t weight{-1};
76 };
77 
82 TxSize CalculateMaximumSignedTxSize(const CTransaction& tx, const CWallet* wallet, const std::vector<CTxOut>& txouts, const CCoinControl* coin_control = nullptr);
83 TxSize CalculateMaximumSignedTxSize(const CTransaction& tx, const CWallet* wallet, const CCoinControl* coin_control = nullptr) EXCLUSIVE_LOCKS_REQUIRED(wallet->cs_wallet);
84 
88 void AvailableCoins(const CWallet& wallet, std::vector<COutput>& vCoins, const CCoinControl* coinControl = nullptr, const CAmount& nMinimumAmount = 1, const CAmount& nMaximumAmount = MAX_MONEY, const CAmount& nMinimumSumAmount = MAX_MONEY, const uint64_t nMaximumCount = 0) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);
89 
90 CAmount GetAvailableBalance(const CWallet& wallet, const CCoinControl* coinControl = nullptr);
91 
95 const CTxOut& FindNonChangeParentOutput(const CWallet& wallet, const CTransaction& tx, int output) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);
96 
100 std::map<CTxDestination, std::vector<COutput>> ListCoins(const CWallet& wallet) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);
101 
102 std::vector<OutputGroup> GroupOutputs(const CWallet& wallet, const std::vector<COutput>& outputs, const CoinSelectionParams& coin_sel_params, const CoinEligibilityFilter& filter, bool positive_only);
103 
117 std::optional<SelectionResult> AttemptSelection(const CWallet& wallet, const CAmount& nTargetValue, const CoinEligibilityFilter& eligibility_filter, std::vector<COutput> coins,
118  const CoinSelectionParams& coin_selection_params);
119 
131 std::optional<SelectionResult> SelectCoins(const CWallet& wallet, const std::vector<COutput>& vAvailableCoins, const CAmount& nTargetValue, const CCoinControl& coin_control,
132  const CoinSelectionParams& coin_selection_params) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);
133 
139 bool CreateTransaction(CWallet& wallet, const std::vector<CRecipient>& vecSend, CTransactionRef& tx, CAmount& nFeeRet, int& nChangePosInOut, bilingual_str& error, const CCoinControl& coin_control, FeeCalculation& fee_calc_out, bool sign = true);
140 
145 bool FundTransaction(CWallet& wallet, CMutableTransaction& tx, CAmount& nFeeRet, int& nChangePosInOut, bilingual_str& error, bool lockUnspents, const std::set<int>& setSubtractFeeFromOutputs, CCoinControl);
146 } // namespace wallet
147 
148 #endif // BITCOIN_WALLET_SPEND_H
wallet.h
wallet::AttemptSelection
std::optional< SelectionResult > AttemptSelection(const CWallet &wallet, const CAmount &nTargetValue, const CoinEligibilityFilter &eligibility_filter, std::vector< COutput > coins, const CoinSelectionParams &coin_selection_params)
Attempt to find a valid input set that meets the provided eligibility filter and target.
Definition: spend.cpp:377
wallet::CalculateMaximumSignedInputSize
int CalculateMaximumSignedInputSize(const CTxOut &txout, const SigningProvider *provider, bool use_max_sig)
Definition: spend.cpp:37
wallet::GroupOutputs
std::vector< OutputGroup > GroupOutputs(const CWallet &wallet, const std::vector< COutput > &outputs, const CoinSelectionParams &coin_sel_params, const CoinEligibilityFilter &filter, bool positive_only)
Definition: spend.cpp:291
wallet::FindNonChangeParentOutput
const CTxOut & FindNonChangeParentOutput(const CWallet &wallet, const CTransaction &tx, int output)
Find non-change parent output.
Definition: spend.cpp:232
bilingual_str
Bilingual messages:
Definition: translation.h:16
wallet::COutput::i
int i
Index in tx->vout.
Definition: spend.h:23
wallet::COutput::tx
const CWalletTx * tx
Definition: spend.h:20
SigningProvider
An interface to be implemented by keystores that support signing.
Definition: signingprovider.h:17
wallet::GetAvailableBalance
CAmount GetAvailableBalance(const CWallet &wallet, const CCoinControl *coinControl)
Definition: spend.cpp:217
wallet
Definition: node.h:38
CTransactionRef
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:386
wallet::CWalletTx
A transaction with a bunch of additional info that only the owner cares about.
Definition: transaction.h:137
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:259
wallet::COutput::GetInputCoin
CInputCoin GetInputCoin() const
Definition: spend.h:63
wallet::COutput::fSolvable
bool fSolvable
Whether we know how to spend this output, ignoring the lack of keys.
Definition: spend.h:39
wallet::COutput::nInputBytes
int nInputBytes
Pre-computed estimated size of this output as a fully-signed input in a transaction.
Definition: spend.h:33
wallet::AvailableCoins
void AvailableCoins(const CWallet &wallet, std::vector< COutput > &vCoins, const CCoinControl *coinControl, const CAmount &nMinimumAmount, const CAmount &nMaximumAmount, const CAmount &nMinimumSumAmount, const uint64_t nMaximumCount)
populate vCoins with vector of available COutputs.
Definition: spend.cpp:89
wallet::COutput::use_max_sig
bool use_max_sig
Whether to use the maximum sized, 72 byte signature when calculating the size of the input spend.
Definition: spend.h:42
FeeCalculation
Definition: fees.h:75
wallet::COutput::fSafe
bool fSafe
Whether this output is considered safe to spend.
Definition: spend.h:49
coinselection.h
CTxDestination
std::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:157
CTxOut
An output of a transaction.
Definition: transaction.h:128
wallet::CWallet
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:232
wallet::COutput::nDepth
int nDepth
Depth in block chain.
Definition: spend.h:30
wallet::COutput::COutput
COutput(const CWallet &wallet, const CWalletTx &wtx, int iIn, int nDepthIn, bool fSpendableIn, bool fSolvableIn, bool fSafeIn, bool use_max_sig_in=false)
Definition: spend.h:51
CAmount
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
wallet::FundTransaction
void FundTransaction(CWallet &wallet, CMutableTransaction &tx, CAmount &fee_out, int &change_position, const UniValue &options, CCoinControl &coinControl, bool override_min_fee)
Definition: spend.cpp:389
wallet::TxSize::weight
int64_t weight
Definition: spend.h:75
wallet::CInputCoin
A UTXO under consideration for use in funding a new transaction.
Definition: coinselection.h:22
wallet::COutput::fSpendable
bool fSpendable
Whether we have the private keys to spend this output.
Definition: spend.h:36
wallet::CreateTransaction
bool CreateTransaction(CWallet &wallet, const std::vector< CRecipient > &vecSend, CTransactionRef &tx, CAmount &nFeeRet, int &nChangePosInOut, bilingual_str &error, const CCoinControl &coin_control, FeeCalculation &fee_calc_out, bool sign)
Create a new transaction paying the recipients with a set of coins selected by SelectCoins(); Also cr...
Definition: spend.cpp:933
transaction.h
wallet::ListCoins
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:250
EXCLUSIVE_LOCKS_REQUIRED
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
std
Definition: setup_common.h:33
wallet::COutput
Definition: spend.h:17
wallet::SelectCoins
std::optional< SelectionResult > SelectCoins(const CWallet &wallet, const std::vector< COutput > &vAvailableCoins, const CAmount &nTargetValue, const CCoinControl &coin_control, const CoinSelectionParams &coin_selection_params)
Select a set of coins such that nTargetValue is met and at least all coins from coin_control are sele...
Definition: spend.cpp:418
wallet::TxSize::vsize
int64_t vsize
Definition: spend.h:74
MAX_MONEY
static constexpr CAmount MAX_MONEY
No amount larger than this (in satoshi) is valid.
Definition: amount.h:26
wallet::GetTxSpendSize
int GetTxSpendSize(const CWallet &wallet, const CWalletTx &wtx, unsigned int out, bool use_max_sig)
Get the marginal bytes if spending the specified output from this transaction.
Definition: spend.cpp:27
error
bool error(const char *fmt, const Args &... args)
Definition: system.h:49
CMutableTransaction
A mutable version of CTransaction.
Definition: transaction.h:344
wallet::TxSize
Definition: spend.h:73
amount.h
wallet::COutput::ToString
std::string ToString() const
Definition: spend.cpp:32
wallet::CWalletTx::tx
CTransactionRef tx
Definition: transaction.h:219
wallet::CalculateMaximumSignedTxSize
TxSize CalculateMaximumSignedTxSize(const CTransaction &tx, const CWallet *wallet, const std::vector< CTxOut > &txouts, const CCoinControl *coin_control)
Calculate the size of the transaction assuming all signatures are max size Use DummySignatureCreator,...
Definition: spend.cpp:54