LCOV - code coverage report
Current view: top level - src/wallet - coincontrol.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 67.9 % 78 53
Test Date: 2024-08-28 04:44:32 Functions: 65.4 % 26 17
Branches: 42.9 % 28 12

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

Generated by: LCOV version 2.0-1