Line data Source code
1 : // Copyright (c) 2011-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_COINCONTROL_H 6 : #define BITCOIN_WALLET_COINCONTROL_H 7 : 8 : #include <key.h> 9 : #include <policy/feerate.h> 10 : #include <policy/fees.h> 11 : #include <primitives/transaction.h> 12 : #include <script/keyorigin.h> 13 : #include <script/signingprovider.h> 14 : #include <script/standard.h> 15 : 16 : #include <algorithm> 17 : #include <map> 18 : #include <optional> 19 : #include <set> 20 : 21 : namespace wallet { 22 : enum class CoinType : uint8_t 23 : { 24 : ALL_COINS, 25 : ONLY_FULLY_MIXED, 26 : ONLY_READY_TO_MIX, 27 : ONLY_NONDENOMINATED, 28 : ONLY_MASTERNODE_COLLATERAL, // find masternode outputs including locked ones (use with caution) 29 : ONLY_COINJOIN_COLLATERAL, 30 : // Attributes 31 : MIN_COIN_TYPE = ALL_COINS, 32 : MAX_COIN_TYPE = ONLY_COINJOIN_COLLATERAL, 33 : }; 34 : 35 : //! Default for -avoidpartialspends 36 : static constexpr bool DEFAULT_AVOIDPARTIALSPENDS = false; 37 : 38 : const int DEFAULT_MIN_DEPTH = 0; 39 : const int DEFAULT_MAX_DEPTH = 9999999; 40 : 41 : /** Coin Control Features. */ 42 8198 : class CCoinControl 43 : { 44 : public: 45 : //! Custom change destination, if not set an address is generated 46 : CTxDestination destChange = CNoDestination(); 47 : //! If false, only safe inputs will be used 48 : bool m_include_unsafe_inputs = false; 49 : //! If true, the selection process can add extra unselected inputs from the wallet 50 : //! while requires all selected inputs be used 51 : bool m_allow_other_inputs = false; 52 : //! If false, only include as many inputs as necessary to fulfill a coin selection request. Only usable together with m_allow_other_inputs 53 : bool fRequireAllInputs = true; 54 : //! Includes watch only addresses which are solvable 55 : bool fAllowWatchOnly = false; 56 : //! Override automatic min/max checks on fee, m_feerate must be set if true 57 : bool fOverrideFeeRate = false; 58 : //! Override the wallet's m_pay_tx_fee if set 59 : std::optional<CFeeRate> m_feerate; 60 : //! Override the discard feerate estimation with m_discard_feerate in CreateTransaction if set 61 : std::optional<CFeeRate> m_discard_feerate; 62 : //! Override the default confirmation target if set 63 : std::optional<unsigned int> m_confirm_target; 64 : //! Avoid partial use of funds sent to a given address 65 : bool m_avoid_partial_spends = DEFAULT_AVOIDPARTIALSPENDS; 66 : //! Forbids inclusion of dirty (previously used) addresses 67 : bool m_avoid_address_reuse = false; 68 : //! Fee estimation mode to control arguments to estimateSmartFee 69 : FeeEstimateMode m_fee_mode = FeeEstimateMode::UNSET; 70 : //! Minimum chain depth value for coin availability 71 : int m_min_depth = DEFAULT_MIN_DEPTH; 72 : //! Maximum chain depth value for coin availability 73 : int m_max_depth = DEFAULT_MAX_DEPTH; 74 : //! SigningProvider that has pubkeys and scripts to do spend size estimation for external inputs 75 : FlatSigningProvider m_external_provider; 76 : //! Controls which types of coins are allowed to be used (default: ALL_COINS) 77 : CoinType nCoinType = CoinType::ALL_COINS; 78 : 79 : CCoinControl(CoinType coin_type = CoinType::ALL_COINS); 80 : 81 : /** 82 : * Returns true if there are pre-selected inputs. 83 : */ 84 : bool HasSelected() const; 85 : /** 86 : * Returns true if the given output is pre-selected. 87 : */ 88 : bool IsSelected(const COutPoint& output) const; 89 : /** 90 : * Returns true if the given output is selected as an external input. 91 : */ 92 : bool IsExternalSelected(const COutPoint& output) const; 93 : /** 94 : * Returns the external output for the given outpoint if it exists. 95 : */ 96 : std::optional<CTxOut> GetExternalOutput(const COutPoint& outpoint) const; 97 : /** 98 : * Lock-in the given output for spending. 99 : * The output will be included in the transaction even if it's not the most optimal choice. 100 : */ 101 : void Select(const COutPoint& output); 102 : /** 103 : * Lock-in the given output as an external input for spending because it is not in the wallet. 104 : * The output will be included in the transaction even if it's not the most optimal choice. 105 : */ 106 : void SelectExternal(const COutPoint& outpoint, const CTxOut& txout); 107 : /** 108 : * Unselects the given output. 109 : */ 110 : void UnSelect(const COutPoint& output); 111 : /** 112 : * Unselects all outputs. 113 : */ 114 : void UnSelectAll(); 115 : /** 116 : * List the selected inputs. 117 : */ 118 : std::vector<COutPoint> ListSelected() const; 119 : /** 120 : * Set an input's weight. 121 : */ 122 : void SetInputWeight(const COutPoint& outpoint, int64_t weight); 123 : /** 124 : * Returns true if the input weight is set. 125 : */ 126 : bool HasInputWeight(const COutPoint& outpoint) const; 127 : /** 128 : * Returns the input weight. 129 : */ 130 : int64_t GetInputWeight(const COutPoint& outpoint) const; 131 : 132 : // Dash-specific helpers 133 0 : void UseCoinJoin(bool fUseCoinJoin) 134 : { 135 0 : nCoinType = fUseCoinJoin ? CoinType::ONLY_FULLY_MIXED : CoinType::ALL_COINS; 136 0 : } 137 : 138 5038 : bool IsUsingCoinJoin() const 139 : { 140 5038 : return nCoinType == CoinType::ONLY_FULLY_MIXED; 141 : } 142 : private: 143 : //! Selected inputs (inputs that will be used, regardless of whether they're optimal or not) 144 : std::set<COutPoint> m_selected_inputs; 145 : //! Map of external inputs to include in the transaction 146 : //! These are not in the wallet, so we need to track them separately 147 : std::map<COutPoint, CTxOut> m_external_txouts; 148 : //! Map of COutPoints to the maximum weight (equals to size) for that input 149 : std::map<COutPoint, int64_t> m_input_weights; 150 : }; 151 : } // namespace wallet 152 : 153 : #endif // BITCOIN_WALLET_COINCONTROL_H