coincontrol.cpp raw

   1  // Copyright (c) 2018-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  #include <wallet/coincontrol.h>
   6  
   7  #include <common/args.h>
   8  
   9  namespace wallet {
  10  CCoinControl::CCoinControl()
  11  {
  12      m_avoid_partial_spends = gArgs.GetBoolArg("-avoidpartialspends", DEFAULT_AVOIDPARTIALSPENDS);
  13  }
  14  
  15  bool CCoinControl::HasSelected() const
  16  {
  17      return !m_selected.empty();
  18  }
  19  
  20  bool CCoinControl::IsSelected(const COutPoint& outpoint) const
  21  {
  22      return m_selected.contains(outpoint);
  23  }
  24  
  25  bool CCoinControl::IsExternalSelected(const COutPoint& outpoint) const
  26  {
  27      const auto it = m_selected.find(outpoint);
  28      return it != m_selected.end() && it->second.HasTxOut();
  29  }
  30  
  31  std::optional<CTxOut> CCoinControl::GetExternalOutput(const COutPoint& outpoint) const
  32  {
  33      const auto it = m_selected.find(outpoint);
  34      if (it == m_selected.end() || !it->second.HasTxOut()) {
  35          return std::nullopt;
  36      }
  37      return it->second.GetTxOut();
  38  }
  39  
  40  PreselectedInput& CCoinControl::Select(const COutPoint& outpoint)
  41  {
  42      auto& input = m_selected[outpoint];
  43      input.SetPosition(m_selection_pos);
  44      ++m_selection_pos;
  45      return input;
  46  }
  47  void CCoinControl::UnSelect(const COutPoint& outpoint)
  48  {
  49      m_selected.erase(outpoint);
  50  }
  51  
  52  void CCoinControl::UnSelectAll()
  53  {
  54      m_selected.clear();
  55  }
  56  
  57  std::vector<COutPoint> CCoinControl::ListSelected() const
  58  {
  59      std::vector<COutPoint> outpoints;
  60      std::transform(m_selected.begin(), m_selected.end(), std::back_inserter(outpoints),
  61          [](const std::map<COutPoint, PreselectedInput>::value_type& pair) {
  62              return pair.first;
  63          });
  64      return outpoints;
  65  }
  66  
  67  void CCoinControl::SetInputWeight(const COutPoint& outpoint, int64_t weight)
  68  {
  69      m_selected[outpoint].SetInputWeight(weight);
  70  }
  71  
  72  std::optional<int64_t> CCoinControl::GetInputWeight(const COutPoint& outpoint) const
  73  {
  74      const auto it = m_selected.find(outpoint);
  75      return it != m_selected.end() ? it->second.GetInputWeight() : std::nullopt;
  76  }
  77  
  78  std::optional<uint32_t> CCoinControl::GetSequence(const COutPoint& outpoint) const
  79  {
  80      const auto it = m_selected.find(outpoint);
  81      return it != m_selected.end() ? it->second.GetSequence() : std::nullopt;
  82  }
  83  
  84  std::pair<std::optional<CScript>, std::optional<CScriptWitness>> CCoinControl::GetScripts(const COutPoint& outpoint) const
  85  {
  86      const auto it = m_selected.find(outpoint);
  87      return it != m_selected.end() ? m_selected.at(outpoint).GetScripts() : std::make_pair(std::nullopt, std::nullopt);
  88  }
  89  
  90  void PreselectedInput::SetTxOut(const CTxOut& txout)
  91  {
  92      m_txout = txout;
  93  }
  94  
  95  CTxOut PreselectedInput::GetTxOut() const
  96  {
  97      assert(m_txout.has_value());
  98      return m_txout.value();
  99  }
 100  
 101  bool PreselectedInput::HasTxOut() const
 102  {
 103      return m_txout.has_value();
 104  }
 105  
 106  void PreselectedInput::SetInputWeight(int64_t weight)
 107  {
 108      m_weight = weight;
 109  }
 110  
 111  std::optional<int64_t> PreselectedInput::GetInputWeight() const
 112  {
 113      return m_weight;
 114  }
 115  
 116  void PreselectedInput::SetSequence(uint32_t sequence)
 117  {
 118      m_sequence = sequence;
 119  }
 120  
 121  std::optional<uint32_t> PreselectedInput::GetSequence() const
 122  {
 123      return m_sequence;
 124  }
 125  
 126  void PreselectedInput::SetScriptSig(const CScript& script)
 127  {
 128      m_script_sig = script;
 129  }
 130  
 131  void PreselectedInput::SetScriptWitness(const CScriptWitness& script_wit)
 132  {
 133      m_script_witness = script_wit;
 134  }
 135  
 136  bool PreselectedInput::HasScripts() const
 137  {
 138      return m_script_sig.has_value() || m_script_witness.has_value();
 139  }
 140  
 141  std::pair<std::optional<CScript>, std::optional<CScriptWitness>> PreselectedInput::GetScripts() const
 142  {
 143      return {m_script_sig, m_script_witness};
 144  }
 145  
 146  void PreselectedInput::SetPosition(unsigned int pos)
 147  {
 148      m_pos = pos;
 149  }
 150  
 151  std::optional<unsigned int> PreselectedInput::GetPosition() const
 152  {
 153      return m_pos;
 154  }
 155  } // namespace wallet
 156