From 94776621ba6a79f3197ec71250bc48e974ad5e4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A8le=20Oul=C3=A8s?= Date: Mon, 12 Sep 2022 12:13:17 +0200 Subject: wallet: Move CoinCointrol definitions to .cpp Move definitions to coincontrol.cpp and add documentation. --- src/wallet/coincontrol.cpp | 68 +++++++++++++++++++++++++ src/wallet/coincontrol.h | 120 ++++++++++++++++++++------------------------- 2 files changed, 121 insertions(+), 67 deletions(-) (limited to 'src') diff --git a/src/wallet/coincontrol.cpp b/src/wallet/coincontrol.cpp index ad2fab4b7d..d87b54e35c 100644 --- a/src/wallet/coincontrol.cpp +++ b/src/wallet/coincontrol.cpp @@ -11,4 +11,72 @@ CCoinControl::CCoinControl() { m_avoid_partial_spends = gArgs.GetBoolArg("-avoidpartialspends", DEFAULT_AVOIDPARTIALSPENDS); } + +bool CCoinControl::HasSelected() const +{ + return !m_selected_inputs.empty(); +} + +bool CCoinControl::IsSelected(const COutPoint& output) const +{ + return m_selected_inputs.count(output) > 0; +} + +bool CCoinControl::IsExternalSelected(const COutPoint& output) const +{ + return m_external_txouts.count(output) > 0; +} + +std::optional CCoinControl::GetExternalOutput(const COutPoint& outpoint) const +{ + const auto ext_it = m_external_txouts.find(outpoint); + if (ext_it == m_external_txouts.end()) { + return std::nullopt; + } + + return std::make_optional(ext_it->second); +} + +void CCoinControl::Select(const COutPoint& output) +{ + m_selected_inputs.insert(output); +} + +void CCoinControl::SelectExternal(const COutPoint& outpoint, const CTxOut& txout) +{ + m_selected_inputs.insert(outpoint); + m_external_txouts.emplace(outpoint, txout); +} + +void CCoinControl::UnSelect(const COutPoint& output) +{ + m_selected_inputs.erase(output); +} + +void CCoinControl::UnSelectAll() +{ + m_selected_inputs.clear(); +} + +void CCoinControl::ListSelected(std::vector& vOutpoints) const +{ + vOutpoints.assign(m_selected_inputs.begin(), m_selected_inputs.end()); +} + +void CCoinControl::SetInputWeight(const COutPoint& outpoint, int64_t weight) +{ + m_input_weights[outpoint] = weight; +} + +bool CCoinControl::HasInputWeight(const COutPoint& outpoint) const +{ + return m_input_weights.count(outpoint) > 0; +} + +int64_t CCoinControl::GetInputWeight(const COutPoint& outpoint) const +{ + auto it = m_input_weights.find(outpoint); + assert(it != m_input_weights.end()); + return it->second; +} } // namespace wallet diff --git a/src/wallet/coincontrol.h b/src/wallet/coincontrol.h index 04f524028f..0a860dd782 100644 --- a/src/wallet/coincontrol.h +++ b/src/wallet/coincontrol.h @@ -63,76 +63,62 @@ public: CCoinControl(); - bool HasSelected() const - { - return !m_selected_inputs.empty(); - } - - bool IsSelected(const COutPoint& output) const - { - return m_selected_inputs.count(output) > 0; - } - - bool IsExternalSelected(const COutPoint& output) const - { - return m_external_txouts.count(output) > 0; - } - - std::optional GetExternalOutput(const COutPoint& outpoint) const - { - const auto ext_it = m_external_txouts.find(outpoint); - if (ext_it == m_external_txouts.end()) { - return std::nullopt; - } - - return std::make_optional(ext_it->second); - } - - void Select(const COutPoint& output) - { - m_selected_inputs.insert(output); - } - - void SelectExternal(const COutPoint& outpoint, const CTxOut& txout) - { - m_selected_inputs.insert(outpoint); - m_external_txouts.emplace(outpoint, txout); - } - - void UnSelect(const COutPoint& output) - { - m_selected_inputs.erase(output); - } - - void UnSelectAll() - { - m_selected_inputs.clear(); - } - - void ListSelected(std::vector& vOutpoints) const - { - vOutpoints.assign(m_selected_inputs.begin(), m_selected_inputs.end()); - } - - void SetInputWeight(const COutPoint& outpoint, int64_t weight) - { - m_input_weights[outpoint] = weight; - } - - bool HasInputWeight(const COutPoint& outpoint) const - { - return m_input_weights.count(outpoint) > 0; - } - - int64_t GetInputWeight(const COutPoint& outpoint) const - { - auto it = m_input_weights.find(outpoint); - assert(it != m_input_weights.end()); - return it->second; - } + /** + * Returns true if there are pre-selected inputs. + */ + bool HasSelected() const; + /** + * Returns true if the given output is pre-selected. + */ + bool IsSelected(const COutPoint& output) const; + /** + * Returns true if the given output is selected as an external input. + */ + bool IsExternalSelected(const COutPoint& output) const; + /** + * Returns the external output for the given outpoint if it exists. + */ + std::optional GetExternalOutput(const COutPoint& outpoint) const; + /** + * Lock-in the given output for spending. + * The output will be included in the transaction even if it's not the most optimal choice. + */ + void Select(const COutPoint& output); + /** + * Lock-in the given output as an external input for spending because it is not in the wallet. + * The output will be included in the transaction even if it's not the most optimal choice. + */ + void SelectExternal(const COutPoint& outpoint, const CTxOut& txout); + /** + * Unselects the given output. + */ + void UnSelect(const COutPoint& output); + /** + * Unselects all outputs. + */ + void UnSelectAll(); + /** + * List the selected inputs. + */ + void ListSelected(std::vector& vOutpoints) const; + /** + * Set an input's weight. + */ + void SetInputWeight(const COutPoint& outpoint, int64_t weight); + /** + * Returns true if the input weight is set. + */ + bool HasInputWeight(const COutPoint& outpoint) const; + /** + * Returns the input weight. + */ + int64_t GetInputWeight(const COutPoint& outpoint) const; private: + //! Selected inputs (inputs that will be used, regardless of whether they're optimal or not) std::set m_selected_inputs; + //! Map of external inputs to include in the transaction + //! These are not in the wallet, so we need to track them separately std::map m_external_txouts; //! Map of COutPoints to the maximum weight for that input std::map m_input_weights; -- cgit v1.2.3