LCOV - code coverage report
Current view: top level - src/common - messages.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 66.7 % 99 66
Test Date: 2026-08-05 06:42:35 Functions: 76.9 % 13 10
Branches: 39.6 % 139 55

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2                 :             : // Copyright (c) 2009-present The Bitcoin Core developers
       3                 :             : // Distributed under the MIT software license, see the accompanying
       4                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       5                 :             : 
       6                 :             : #include <common/messages.h>
       7                 :             : 
       8                 :             : #include <common/types.h>
       9                 :             : #include <node/types.h>
      10                 :             : #include <policy/fees/block_policy_estimator.h>
      11                 :             : #include <tinyformat.h>
      12                 :             : #include <util/check.h>
      13                 :             : #include <util/fees.h>
      14                 :             : #include <util/strencodings.h>
      15                 :             : #include <util/string.h>
      16                 :             : #include <util/translation.h>
      17                 :             : 
      18                 :             : #include <map>
      19                 :             : #include <string>
      20                 :             : #include <string_view>
      21                 :             : #include <utility>
      22                 :             : #include <vector>
      23                 :             : 
      24                 :             : using node::TransactionError;
      25                 :             : using util::Join;
      26                 :             : 
      27                 :             : namespace common {
      28                 :         558 : std::string StringForFeeReason(FeeReason reason)
      29                 :             : {
      30                 :         558 :     static const std::map<FeeReason, std::string> fee_reason_strings = {
      31         [ +  - ]:           2 :         {FeeReason::NONE, "None"},
      32                 :           2 :         {FeeReason::HALF_ESTIMATE, "Half Target 60% Threshold"},
      33                 :           2 :         {FeeReason::FULL_ESTIMATE, "Target 85% Threshold"},
      34                 :           2 :         {FeeReason::DOUBLE_ESTIMATE, "Double Target 95% Threshold"},
      35                 :           2 :         {FeeReason::CONSERVATIVE, "Conservative Double Target longer horizon"},
      36                 :           2 :         {FeeReason::MEMPOOL_MIN, "Mempool Min Fee"},
      37                 :           2 :         {FeeReason::FALLBACK, "Fallback fee"},
      38                 :           2 :         {FeeReason::REQUIRED, "Minimum Required Fee"},
      39   [ +  +  +  -  :         605 :     };
             +  +  -  - ]
      40                 :         558 :     auto reason_string = fee_reason_strings.find(reason);
      41                 :             : 
      42         [ -  + ]:         558 :     if (reason_string == fee_reason_strings.end()) return "Unknown";
      43                 :             : 
      44         [ -  + ]:         558 :     return reason_string->second;
      45                 :          16 : }
           [ +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             -  +  -  - ]
      46                 :             : 
      47                 :        1671 : const std::vector<std::pair<std::string, FeeEstimateMode>>& FeeModeMap()
      48                 :             : {
      49                 :        1671 :     static const std::vector<std::pair<std::string, FeeEstimateMode>> FEE_MODES = {
      50         [ +  - ]:          29 :         {"unset", FeeEstimateMode::UNSET},
      51                 :          29 :         {"economical", FeeEstimateMode::ECONOMICAL},
      52                 :          29 :         {"conservative", FeeEstimateMode::CONSERVATIVE},
      53   [ +  +  +  -  :        1787 :     };
             +  +  -  - ]
      54                 :        1671 :     return FEE_MODES;
      55   [ +  -  +  -  :          29 : }
             -  +  -  - ]
      56                 :             : 
      57                 :         291 : std::string FeeModeInfo(const std::pair<std::string, FeeEstimateMode>& mode, std::string& default_info)
      58                 :             : {
      59   [ +  +  +  - ]:         291 :     switch (mode.second) {
      60                 :          97 :         case FeeEstimateMode::UNSET:
      61                 :          97 :             return strprintf("%s means no mode set (%s). \n", mode.first, default_info);
      62                 :          97 :         case FeeEstimateMode::ECONOMICAL:
      63                 :          97 :             return strprintf("%s estimates use a shorter time horizon, making them more\n"
      64                 :             :                    "responsive to short-term drops in the prevailing fee market. This mode\n"
      65                 :          97 :                    "potentially returns a lower fee rate estimate.\n", mode.first);
      66                 :          97 :         case FeeEstimateMode::CONSERVATIVE:
      67                 :          97 :             return strprintf("%s estimates use a longer time horizon, making them\n"
      68                 :             :                    "less responsive to short-term drops in the prevailing fee market. This mode\n"
      69                 :          97 :                    "potentially returns a higher fee rate estimate.\n", mode.first);
      70                 :             :     } // no default case, so the compiler can warn about missing cases
      71                 :           0 :     assert(false);
      72                 :             : }
      73                 :             : 
      74                 :          97 : std::string FeeModesDetail(std::string default_info)
      75                 :             : {
      76         [ +  - ]:          97 :     std::string info;
      77   [ +  -  +  + ]:         388 :     for (const auto& fee_mode : FeeModeMap()) {
      78         [ +  - ]:         582 :         info += FeeModeInfo(fee_mode, default_info);
      79                 :             :     }
      80   [ +  -  +  -  :         194 :     return strprintf("%s \n%s", FeeModes(", "), info);
                   +  - ]
      81                 :          97 : }
      82                 :             : 
      83                 :          97 : std::string FeeModes(const std::string& delimiter)
      84                 :             : {
      85   [ -  +  -  + ]:         679 :     return Join(FeeModeMap(), delimiter, [&](const std::pair<std::string, FeeEstimateMode>& i) { return i.first; });
      86                 :             : }
      87                 :             : 
      88                 :           0 : std::string InvalidEstimateModeErrorMessage()
      89                 :             : {
      90   [ #  #  #  # ]:           0 :     return "Invalid estimate_mode parameter, must be one of: \"" + FeeModes("\", \"") + "\"";
      91                 :             : }
      92                 :             : 
      93                 :        1477 : bool FeeModeFromString(std::string_view mode_string, FeeEstimateMode& fee_estimate_mode)
      94                 :             : {
      95                 :        1477 :     auto searchkey = ToUpper(mode_string);
      96   [ +  -  +  + ]:        5902 :     for (const auto& pair : FeeModeMap()) {
      97   [ -  +  +  -  :        4428 :         if (ToUpper(pair.first) == searchkey) {
                   +  + ]
      98                 :           3 :             fee_estimate_mode = pair.second;
      99                 :           3 :             return true;
     100                 :             :         }
     101                 :             :     }
     102                 :             :     return false;
     103                 :        1477 : }
     104                 :             : 
     105                 :           0 : bilingual_str PSBTErrorString(PSBTError err)
     106                 :             : {
     107   [ #  #  #  #  :           0 :     switch (err) {
             #  #  #  #  
                      # ]
     108                 :           0 :         case PSBTError::MISSING_INPUTS:
     109         [ #  # ]:           0 :             return Untranslated("Inputs missing or spent");
     110                 :           0 :         case PSBTError::SIGHASH_MISMATCH:
     111         [ #  # ]:           0 :             return Untranslated("Specified sighash value does not match value stored in PSBT");
     112                 :           0 :         case PSBTError::EXTERNAL_SIGNER_NOT_FOUND:
     113         [ #  # ]:           0 :             return Untranslated("External signer not found");
     114                 :           0 :         case PSBTError::EXTERNAL_SIGNER_FAILED:
     115         [ #  # ]:           0 :             return Untranslated("External signer failed to sign");
     116                 :           0 :         case PSBTError::UNSUPPORTED:
     117         [ #  # ]:           0 :             return Untranslated("Signer does not support PSBT");
     118                 :           0 :         case PSBTError::INCOMPLETE:
     119         [ #  # ]:           0 :             return Untranslated("Input needs additional signatures or other data");
     120                 :           0 :         case PSBTError::INVALID_TX:
     121         [ #  # ]:           0 :             return Untranslated("The transaction cannot be valid");
     122                 :           0 :         case PSBTError::OK:
     123         [ #  # ]:           0 :             return Untranslated("No errors");
     124                 :             :     } // no default case, so the compiler can warn about missing cases
     125                 :           0 :     assert(false);
     126                 :             : }
     127                 :             : 
     128                 :         268 : bilingual_str TransactionErrorString(const TransactionError err)
     129                 :             : {
     130   [ -  +  +  +  :         268 :     switch (err) {
          +  +  -  -  -  
                      - ]
     131                 :           0 :         case TransactionError::OK:
     132         [ #  # ]:           0 :             return Untranslated("No error");
     133                 :          40 :         case TransactionError::MISSING_INPUTS:
     134         [ +  - ]:          80 :             return Untranslated("Inputs missing or spent");
     135                 :          26 :         case TransactionError::ALREADY_IN_UTXO_SET:
     136         [ +  - ]:          52 :             return Untranslated("Transaction outputs already in utxo set");
     137                 :          24 :         case TransactionError::MEMPOOL_REJECTED:
     138         [ +  - ]:          48 :             return Untranslated("Transaction rejected by mempool");
     139                 :          18 :         case TransactionError::MEMPOOL_ERROR:
     140         [ +  - ]:          36 :             return Untranslated("Mempool internal error");
     141                 :         160 :         case TransactionError::MAX_FEE_EXCEEDED:
     142         [ +  - ]:         320 :             return Untranslated("Fee exceeds maximum configured by user (e.g. -maxtxfee, maxfeerate)");
     143                 :           0 :         case TransactionError::MAX_BURN_EXCEEDED:
     144         [ #  # ]:           0 :             return Untranslated("Unspendable output exceeds maximum configured by user (maxburnamount)");
     145                 :           0 :         case TransactionError::INVALID_PACKAGE:
     146         [ #  # ]:           0 :             return Untranslated("Transaction rejected due to invalid package");
     147                 :           0 :         case TransactionError::PRIVATE_BROADCAST_FULL:
     148         [ #  # ]:           0 :             return Untranslated("Private broadcast queue is full");
     149                 :             :     } // no default case, so the compiler can warn about missing cases
     150                 :           0 :     assert(false);
     151                 :             : }
     152                 :             : 
     153                 :        1775 : bilingual_str ResolveErrMsg(const std::string& optname, const std::string& strBind)
     154                 :             : {
     155                 :        1775 :     return strprintf(_("Cannot resolve -%s address: '%s'"), optname, strBind);
     156                 :             : }
     157                 :             : 
     158                 :           0 : bilingual_str InvalidPortErrMsg(const std::string& optname, const std::string& invalid_value)
     159                 :             : {
     160                 :           0 :     return strprintf(_("Invalid port specified in %s: '%s'"), optname, invalid_value);
     161                 :             : }
     162                 :             : 
     163                 :        1477 : bilingual_str AmountHighWarn(const std::string& optname)
     164                 :             : {
     165                 :        1477 :     return strprintf(_("%s is set very high!"), optname);
     166                 :             : }
     167                 :             : 
     168                 :        1477 : bilingual_str AmountErrMsg(const std::string& optname, const std::string& strValue)
     169                 :             : {
     170                 :        1477 :     return strprintf(_("Invalid amount for -%s=<amount>: '%s'"), optname, strValue);
     171                 :             : }
     172                 :             : } // namespace common
        

Generated by: LCOV version 2.5.0-full