LCOV - code coverage report
Current view: top level - src/common - messages.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 72.7 % 99 72
Test Date: 2026-10-15 06:25:54 Functions: 80.0 % 15 12
Branches: 42.3 % 137 58

             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 <tinyformat.h>
      11                 :             : #include <util/check.h>
      12                 :             : #include <util/fees.h>
      13                 :             : #include <util/strencodings.h>
      14                 :             : #include <util/string.h>
      15                 :             : #include <util/translation.h>
      16                 :             : 
      17                 :             : #include <map>
      18                 :             : #include <string>
      19                 :             : #include <string_view>
      20                 :             : #include <utility>
      21                 :             : #include <vector>
      22                 :             : 
      23                 :             : using node::TransactionError;
      24                 :             : using util::Join;
      25                 :             : 
      26                 :             : namespace common {
      27                 :         771 : const std::map<FeeReason, std::string>& FeeReasonMap()
      28                 :             : {
      29                 :         771 :     static const std::map<FeeReason, std::string> fee_reason_strings = {
      30         [ +  - ]:           2 :         {FeeReason::FEE_RATE_ESTIMATOR, "Fee Rate Estimator"},
      31                 :           2 :         {FeeReason::MEMPOOL_MIN, "Mempool Min Fee"},
      32                 :           2 :         {FeeReason::USER_SPECIFIED, "User Specified Fee"},
      33                 :           2 :         {FeeReason::FALLBACK, "Fallback fee"},
      34                 :           2 :         {FeeReason::REQUIRED, "Minimum Required Fee"},
      35   [ +  +  +  -  :         814 :     };
             +  +  -  - ]
      36                 :         771 :     return fee_reason_strings;
      37                 :          10 : }
           [ +  -  +  -  
          +  -  +  -  -  
                +  -  - ]
      38                 :             : 
      39                 :         771 : std::string StringForFeeReason(FeeReason reason)
      40                 :             : {
      41                 :         771 :     const auto& fee_reason_strings = FeeReasonMap();
      42                 :         771 :     auto reason_string = fee_reason_strings.find(reason);
      43                 :             : 
      44         [ -  + ]:         771 :     if (reason_string == fee_reason_strings.end()) return "Unknown";
      45                 :             : 
      46         [ -  + ]:         771 :     return reason_string->second;
      47                 :             : }
      48                 :             : 
      49                 :           0 : std::string FeeReasons(const std::string& delimiter)
      50                 :             : {
      51   [ #  #  #  # ]:           0 :     return Join(FeeReasonMap(), delimiter, [](const auto& reason) { return reason.second; });
      52                 :             : }
      53                 :             : 
      54                 :        1044 : const std::vector<std::pair<std::string, FeeEstimateMode>>& FeeModeMap()
      55                 :             : {
      56                 :        1044 :     static const std::vector<std::pair<std::string, FeeEstimateMode>> FEE_MODES = {
      57         [ +  - ]:          31 :         {"unset", FeeEstimateMode::UNSET},
      58                 :          31 :         {"economical", FeeEstimateMode::ECONOMICAL},
      59                 :          31 :         {"conservative", FeeEstimateMode::CONSERVATIVE},
      60   [ +  +  +  -  :        1168 :     };
             +  +  -  - ]
      61                 :        1044 :     return FEE_MODES;
      62   [ +  -  +  -  :          31 : }
             -  +  -  - ]
      63                 :             : 
      64                 :         243 : std::string FeeModeInfo(const std::pair<std::string, FeeEstimateMode>& mode, std::string& default_info)
      65                 :             : {
      66   [ +  +  +  - ]:         243 :     switch (mode.second) {
      67                 :          81 :         case FeeEstimateMode::UNSET:
      68                 :          81 :             return strprintf("%s means no mode set (%s). \n", mode.first, default_info);
      69                 :          81 :         case FeeEstimateMode::ECONOMICAL:
      70                 :          81 :             return strprintf("%s mode potentially returns a lower fee rate estimate.\n", mode.first);
      71                 :          81 :         case FeeEstimateMode::CONSERVATIVE:
      72                 :          81 :             return strprintf("%s potentially returns a higher fee rate estimate.\n", mode.first);
      73                 :             :     } // no default case, so the compiler can warn about missing cases
      74                 :           0 :     assert(false);
      75                 :             : }
      76                 :             : 
      77                 :          81 : std::string FeeModesDetail(std::string default_info)
      78                 :             : {
      79         [ +  - ]:          81 :     std::string info;
      80   [ +  -  +  + ]:         324 :     for (const auto& fee_mode : FeeModeMap()) {
      81         [ +  - ]:         486 :         info += FeeModeInfo(fee_mode, default_info);
      82                 :             :     }
      83   [ +  -  +  -  :         162 :     return strprintf("%s \n%s", FeeModes(", "), info);
                   +  - ]
      84                 :          81 : }
      85                 :             : 
      86                 :          81 : std::string FeeModes(const std::string& delimiter)
      87                 :             : {
      88   [ -  +  -  + ]:         567 :     return Join(FeeModeMap(), delimiter, [&](const std::pair<std::string, FeeEstimateMode>& i) { return i.first; });
      89                 :             : }
      90                 :             : 
      91                 :           0 : std::string InvalidEstimateModeErrorMessage()
      92                 :             : {
      93   [ #  #  #  # ]:           0 :     return "Invalid estimate_mode parameter, must be one of: \"" + FeeModes("\", \"") + "\"";
      94                 :             : }
      95                 :             : 
      96                 :         882 : bool FeeModeFromString(std::string_view mode_string, FeeEstimateMode& fee_estimate_mode)
      97                 :             : {
      98                 :         882 :     auto searchkey = ToUpper(mode_string);
      99   [ +  -  +  + ]:        3522 :     for (const auto& pair : FeeModeMap()) {
     100   [ -  +  +  -  :        2643 :         if (ToUpper(pair.first) == searchkey) {
                   +  + ]
     101                 :           3 :             fee_estimate_mode = pair.second;
     102                 :           3 :             return true;
     103                 :             :         }
     104                 :             :     }
     105                 :             :     return false;
     106                 :         882 : }
     107                 :             : 
     108                 :           9 : bilingual_str PSBTErrorString(PSBTError err)
     109                 :             : {
     110   [ -  +  -  -  :           9 :     switch (err) {
             -  -  -  - ]
     111                 :           0 :         case PSBTError::MISSING_INPUTS:
     112         [ #  # ]:           0 :             return Untranslated("Inputs missing or spent");
     113                 :           9 :         case PSBTError::SIGHASH_MISMATCH:
     114         [ +  - ]:          18 :             return Untranslated("Specified sighash value does not match value stored in PSBT");
     115                 :           0 :         case PSBTError::EXTERNAL_SIGNER_NOT_FOUND:
     116         [ #  # ]:           0 :             return Untranslated("External signer not found");
     117                 :           0 :         case PSBTError::EXTERNAL_SIGNER_FAILED:
     118         [ #  # ]:           0 :             return Untranslated("External signer failed to sign");
     119                 :           0 :         case PSBTError::UNSUPPORTED:
     120         [ #  # ]:           0 :             return Untranslated("Signer does not support PSBT");
     121                 :           0 :         case PSBTError::INCOMPLETE:
     122         [ #  # ]:           0 :             return Untranslated("Input needs additional signatures or other data");
     123                 :           0 :         case PSBTError::INVALID_TX:
     124         [ #  # ]:           0 :             return Untranslated("The transaction cannot be valid");
     125                 :             :     } // no default case, so the compiler can warn about missing cases
     126                 :           0 :     assert(false);
     127                 :             : }
     128                 :             : 
     129                 :         401 : bilingual_str TransactionErrorString(const TransactionError err)
     130                 :             : {
     131                 :         401 :     switch (err) {
           [ -  +  +  +  
          +  +  +  +  -  
                   -  - ]
     132                 :           0 :         case TransactionError::OK:
     133         [ #  # ]:           0 :             return Untranslated("No error");
     134                 :          20 :         case TransactionError::MISSING_INPUTS:
     135         [ +  - ]:          40 :             return Untranslated("Inputs missing or spent");
     136                 :           8 :         case TransactionError::ALREADY_IN_UTXO_SET:
     137         [ +  - ]:          16 :             return Untranslated("Transaction outputs already in utxo set");
     138                 :          14 :         case TransactionError::MEMPOOL_REJECTED:
     139         [ +  - ]:          28 :             return Untranslated("Transaction rejected by mempool");
     140                 :           8 :         case TransactionError::MEMPOOL_ERROR:
     141         [ +  - ]:          16 :             return Untranslated("Mempool internal error");
     142                 :         233 :         case TransactionError::MAX_FEE_EXCEEDED:
     143         [ +  - ]:         466 :             return Untranslated("Fee exceeds maximum configured by user (maxtxfee)");
     144                 :         108 :         case TransactionError::MAX_FEE_RATE_EXCEEDED:
     145         [ +  - ]:         216 :             return Untranslated("Fee rate exceeds maximum configured by user (maxfeerate)");
     146                 :          10 :         case TransactionError::MAX_BURN_EXCEEDED:
     147         [ +  - ]:          20 :             return Untranslated("Unspendable output exceeds maximum configured by user (maxburnamount)");
     148                 :           0 :         case TransactionError::INVALID_PACKAGE:
     149         [ #  # ]:           0 :             return Untranslated("Transaction rejected due to invalid package");
     150                 :           0 :         case TransactionError::PRIVATE_BROADCAST_FULL:
     151         [ #  # ]:           0 :             return Untranslated("Private broadcast queue is full");
     152                 :             :     } // no default case, so the compiler can warn about missing cases
     153                 :           0 :     assert(false);
     154                 :             : }
     155                 :             : 
     156                 :        1100 : bilingual_str ResolveErrMsg(const std::string& optname, const std::string& strBind)
     157                 :             : {
     158                 :        1100 :     return strprintf(_("Cannot resolve -%s address: '%s'"), optname, strBind);
     159                 :             : }
     160                 :             : 
     161                 :           0 : bilingual_str InvalidPortErrMsg(const std::string& optname, const std::string& invalid_value)
     162                 :             : {
     163                 :           0 :     return strprintf(_("Invalid port specified in %s: '%s'"), optname, invalid_value);
     164                 :             : }
     165                 :             : 
     166                 :         882 : bilingual_str AmountHighWarn(const std::string& optname)
     167                 :             : {
     168                 :         882 :     return strprintf(_("%s is set very high!"), optname);
     169                 :             : }
     170                 :             : 
     171                 :         882 : bilingual_str AmountErrMsg(const std::string& optname, const std::string& strValue)
     172                 :             : {
     173                 :         882 :     return strprintf(_("Invalid amount for -%s=<amount>: '%s'"), optname, strValue);
     174                 :             : }
     175                 :             : } // namespace common
        

Generated by: LCOV version 2.5.0-full