Skip to content

Commit 58ea324

Browse files
glozowmurchandamus
andcommitted
[docs] add doxygen comments to wallet code
Co-authored-by: Xekyo <[email protected]>
1 parent 0c74716 commit 58ea324

File tree

2 files changed

+77
-3
lines changed

2 files changed

+77
-3
lines changed

src/wallet/coinselection.h

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ static constexpr CAmount MIN_CHANGE{COIN / 100};
1515
//! final minimum change amount after paying for fees
1616
static const CAmount MIN_FINAL_CHANGE = MIN_CHANGE/2;
1717

18+
/** A UTXO under consideration for use in funding a new transaction. */
1819
class CInputCoin {
1920
public:
2021
CInputCoin(const CTransactionRef& tx, unsigned int i)
@@ -56,31 +57,58 @@ class CInputCoin {
5657
}
5758
};
5859

60+
/** Parameters for filtering which OutputGroups we may use in coin selection.
61+
* We start by being very selective and requiring multiple confirmations and
62+
* then get more permissive if we cannot fund the transaction. */
5963
struct CoinEligibilityFilter
6064
{
65+
/** Minimum number of confirmations for outputs that we sent to ourselves.
66+
* We may use unconfirmed UTXOs sent from ourselves, e.g. change outputs. */
6167
const int conf_mine;
68+
/** Minimum number of confirmations for outputs received from a different
69+
* wallet. We never spend unconfirmed foreign outputs as we cannot rely on these funds yet. */
6270
const int conf_theirs;
71+
/** Maximum number of unconfirmed ancestors aggregated across all UTXOs in an OutputGroup. */
6372
const uint64_t max_ancestors;
73+
/** Maximum number of descendants that a single UTXO in the OutputGroup may have. */
6474
const uint64_t max_descendants;
65-
const bool m_include_partial_groups{false}; //! Include partial destination groups when avoid_reuse and there are full groups
75+
/** When avoid_reuse=true and there are full groups (OUTPUT_GROUP_MAX_ENTRIES), whether or not to use any partial groups.*/
76+
const bool m_include_partial_groups{false};
6677

6778
CoinEligibilityFilter(int conf_mine, int conf_theirs, uint64_t max_ancestors) : conf_mine(conf_mine), conf_theirs(conf_theirs), max_ancestors(max_ancestors), max_descendants(max_ancestors) {}
6879
CoinEligibilityFilter(int conf_mine, int conf_theirs, uint64_t max_ancestors, uint64_t max_descendants) : conf_mine(conf_mine), conf_theirs(conf_theirs), max_ancestors(max_ancestors), max_descendants(max_descendants) {}
6980
CoinEligibilityFilter(int conf_mine, int conf_theirs, uint64_t max_ancestors, uint64_t max_descendants, bool include_partial) : conf_mine(conf_mine), conf_theirs(conf_theirs), max_ancestors(max_ancestors), max_descendants(max_descendants), m_include_partial_groups(include_partial) {}
7081
};
7182

83+
/** A group of UTXOs paid to the same output script. */
7284
struct OutputGroup
7385
{
86+
/** The list of UTXOs contained in this output group. */
7487
std::vector<CInputCoin> m_outputs;
88+
/** Whether the UTXOs were sent by the wallet to itself. This is relevant because we may want at
89+
* least a certain number of confirmations on UTXOs received from outside wallets while trusting
90+
* our own UTXOs more. */
7591
bool m_from_me{true};
92+
/** The total value of the UTXOs in sum. */
7693
CAmount m_value{0};
94+
/** The minimum number of confirmations the UTXOs in the group have. Unconfirmed is 0. */
7795
int m_depth{999};
96+
/** The aggregated count of unconfirmed ancestors of all UTXOs in this
97+
* group. Not deduplicated and may overestimate when ancestors are shared. */
7898
size_t m_ancestors{0};
99+
/** The maximum count of descendants of a single UTXO in this output group. */
79100
size_t m_descendants{0};
101+
/** The value of the UTXOs after deducting the cost of spending them at the effective feerate. */
80102
CAmount effective_value{0};
103+
/** The fee to spend these UTXOs at the effective feerate. */
81104
CAmount fee{0};
105+
/** The target feerate of the transaction we're trying to build. */
82106
CFeeRate m_effective_feerate{0};
107+
/** The fee to spend these UTXOs at the long term feerate. */
83108
CAmount long_term_fee{0};
109+
/** The feerate for spending a created change output eventually (i.e. not urgently, and thus at
110+
* a lower feerate). Calculated using long term fee estimate. This is used to decide whether
111+
* it could be economical to create a change output. */
84112
CFeeRate m_long_term_feerate{0};
85113

86114
OutputGroup() {}

src/wallet/wallet.h

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,15 @@ class COutput
564564
{
565565
public:
566566
const CWalletTx *tx;
567+
568+
/** Index in tx->vout. */
567569
int i;
570+
571+
/**
572+
* Depth in block chain.
573+
* If > 0: the tx is on chain and has this many confirmations.
574+
* If = 0: the tx is waiting confirmation.
575+
* If < 0: a conflicting tx is on chain and has this many confirmations. */
568576
int nDepth;
569577

570578
/** Pre-computed estimated size of this output as a fully-signed input in a transaction. Can be -1 if it could not be calculated */
@@ -604,17 +612,30 @@ class COutput
604612
}
605613
};
606614

615+
/** Parameters for one iteration of Coin Selection. */
607616
struct CoinSelectionParams
608617
{
618+
/** Toggles use of Branch and Bound instead of Knapsack solver. */
609619
bool use_bnb = true;
620+
/** Size of a change output in bytes, determined by the output type. */
610621
size_t change_output_size = 0;
622+
/** Size of the input to spend a change output in virtual bytes. */
611623
size_t change_spend_size = 0;
624+
/** The targeted feerate of the transaction being built. */
612625
CFeeRate m_effective_feerate;
626+
/** The feerate estimate used to estimate an upper bound on what should be sufficient to spend
627+
* the change output sometime in the future. */
613628
CFeeRate m_long_term_feerate;
629+
/** If the cost to spend a change output at the discard feerate exceeds its value, drop it to fees. */
614630
CFeeRate m_discard_feerate;
631+
/** Size of the transaction before coin selection, consisting of the header and recipient
632+
* output(s), excluding the inputs and change output(s). */
615633
size_t tx_noinputs_size = 0;
616634
/** Indicate that we are subtracting the fee from outputs */
617635
bool m_subtract_fee_outputs = false;
636+
/** When true, always spend all (up to OUTPUT_GROUP_MAX_ENTRIES) or none of the outputs
637+
* associated with the same address. This helps reduce privacy leaks resulting from address
638+
* reuse. Dust outputs are not eligible to be added to output groups and thus not considered. */
618639
bool m_avoid_partial_spends = false;
619640

620641
CoinSelectionParams(bool use_bnb, size_t change_output_size, size_t change_spend_size, CFeeRate effective_feerate,
@@ -652,7 +673,10 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
652673
//! the current wallet version: clients below this version are not able to load the wallet
653674
int nWalletVersion GUARDED_BY(cs_wallet){FEATURE_BASE};
654675

676+
/** The next scheduled rebroadcast of wallet transactions. */
655677
int64_t nNextResend = 0;
678+
/** Whether this wallet will submit newly created transactions to the node's mempool and
679+
* prompt rebroadcasts (see ResendWalletTransactions()). */
656680
bool fBroadcastTransactions = false;
657681
// Local time that the tip block was received. Used to schedule wallet rebroadcasts.
658682
std::atomic<int64_t> m_best_block_time {0};
@@ -694,6 +718,7 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
694718
* Should be called with non-zero block_hash and posInBlock if this is for a transaction that is included in a block. */
695719
void SyncTransaction(const CTransactionRef& tx, CWalletTx::Confirmation confirm, bool update_tx = true) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
696720

721+
/** WalletFlags set on this wallet. */
697722
std::atomic<uint64_t> m_wallet_flags{0};
698723

699724
bool SetAddressBookWithDB(WalletBatch& batch, const CTxDestination& address, const std::string& strName, const std::string& strPurpose);
@@ -753,8 +778,11 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
753778

754779
/**
755780
* Select a set of coins such that nValueRet >= nTargetValue and at least
756-
* all coins from coinControl are selected; Never select unconfirmed coins
757-
* if they are not ours
781+
* all coins from coin_control are selected; never select unconfirmed coins if they are not ours
782+
* param@[out] setCoinsRet Populated with inputs including pre-selected inputs from
783+
* coin_control and Coin Selection if successful.
784+
* param@[out] nValueRet Total value of selected coins including pre-selected ones
785+
* from coin_control and Coin Selection if successful.
758786
*/
759787
bool SelectCoins(const std::vector<COutput>& vAvailableCoins, const CAmount& nTargetValue, std::set<CInputCoin>& setCoinsRet, CAmount& nValueRet,
760788
const CCoinControl& coin_control, CoinSelectionParams& coin_selection_params, bool& bnb_used) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
@@ -788,6 +816,8 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
788816
/** Interface to assert chain access */
789817
bool HaveChain() const { return m_chain ? true : false; }
790818

819+
/** Map from txid to CWalletTx for all transactions this wallet is
820+
* interested in, including received and sent transactions. */
791821
std::map<uint256, CWalletTx> mapWallet GUARDED_BY(cs_wallet);
792822

793823
typedef std::multimap<int64_t, CWalletTx*> TxItems;
@@ -799,6 +829,10 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
799829
std::map<CTxDestination, CAddressBookData> m_address_book GUARDED_BY(cs_wallet);
800830
const CAddressBookData* FindAddressBookEntry(const CTxDestination&, bool allow_change = false) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
801831

832+
/** Set of Coins owned by this wallet that we won't try to spend from. A
833+
* Coin may be locked if it has already been used to fund a transaction
834+
* that hasn't confirmed yet. We wouldn't consider the Coin spent already,
835+
* but also shouldn't try to use it again. */
802836
std::set<COutPoint> setLockedCoins GUARDED_BY(cs_wallet);
803837

804838
/** Registered interfaces::Chain::Notifications handler. */
@@ -833,6 +867,11 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
833867
* small change; This method is stochastic for some inputs and upon
834868
* completion the coin set and corresponding actual target value is
835869
* assembled
870+
* param@[in] coins Set of UTXOs to consider. These will be categorized into
871+
* OutputGroups and filtered using eligibility_filter before
872+
* selecting coins.
873+
* param@[out] setCoinsRet Populated with the coins selected if successful.
874+
* param@[out] nValueRet Used to return the total value of selected coins.
836875
*/
837876
bool SelectCoinsMinConf(const CAmount& nTargetValue, const CoinEligibilityFilter& eligibility_filter, std::vector<COutput> coins,
838877
std::set<CInputCoin>& setCoinsRet, CAmount& nValueRet, const CoinSelectionParams& coin_selection_params, bool& bnb_used) const;
@@ -1015,6 +1054,8 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
10151054

10161055
CFeeRate m_pay_tx_fee{DEFAULT_PAY_TX_FEE};
10171056
unsigned int m_confirm_target{DEFAULT_TX_CONFIRM_TARGET};
1057+
/** Allow Coin Selection to pick unconfirmed UTXOs that were sent from our own wallet if it
1058+
* cannot fund the transaction otherwise. */
10181059
bool m_spend_zero_conf_change{DEFAULT_SPEND_ZEROCONF_CHANGE};
10191060
bool m_signal_rbf{DEFAULT_WALLET_RBF};
10201061
bool m_allow_fallback_fee{true}; //!< will be false if -fallbackfee=0
@@ -1025,7 +1066,12 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
10251066
* Override with -fallbackfee
10261067
*/
10271068
CFeeRate m_fallback_fee{DEFAULT_FALLBACK_FEE};
1069+
1070+
/** If the cost to spend a change output at this feerate is greater than the value of the
1071+
* output itself, just drop it to fees. */
10281072
CFeeRate m_discard_rate{DEFAULT_DISCARD_FEE};
1073+
1074+
/** The maximum fee amount we're willing to pay to prioritize partial spend avoidance. */
10291075
CAmount m_max_aps_fee{DEFAULT_MAX_AVOIDPARTIALSPEND_FEE}; //!< note: this is absolute fee, not fee rate
10301076
OutputType m_default_address_type{DEFAULT_ADDRESS_TYPE};
10311077
/**

0 commit comments

Comments
 (0)