LCOV - code coverage report
Current view: top level - src/rpc - fees.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 127 142 89.4 %
Date: 2023-10-05 15:40:34 Functions: 9 9 100.0 %
Branches: 236 528 44.7 %

           Branch data     Line data    Source code
       1                 :            : // Copyright (c) 2010 Satoshi Nakamoto
       2                 :            : // Copyright (c) 2009-2022 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 <core_io.h>
       7                 :            : #include <policy/feerate.h>
       8                 :            : #include <policy/fees.h>
       9                 :            : #include <rpc/protocol.h>
      10                 :            : #include <rpc/request.h>
      11                 :            : #include <rpc/server.h>
      12                 :            : #include <rpc/server_util.h>
      13                 :            : #include <rpc/util.h>
      14                 :            : #include <txmempool.h>
      15                 :            : #include <univalue.h>
      16                 :            : #include <util/fees.h>
      17         [ +  - ]:        173 : 
      18         [ +  - ]:        173 : #include <algorithm>
      19                 :            : #include <array>
      20                 :            : #include <cmath>
      21                 :            : #include <string>
      22                 :            : 
      23                 :            : namespace node {
      24                 :            : struct NodeContext;
      25                 :            : }
      26                 :            : 
      27                 :        173 : using node::NodeContext;
      28                 :            : 
      29                 :         56 : static RPCHelpMan estimatesmartfee()
      30                 :            : {
      31   [ +  -  -  +  :        112 :     return RPCHelpMan{"estimatesmartfee",
          #  #  #  #  #  
                      # ]
      32         [ +  - ]:         56 :         "\nEstimates the approximate fee per kilobyte needed for a transaction to begin\n"
      33                 :            :         "confirmation within conf_target blocks if possible and return the number of blocks\n"
      34                 :            :         "for which the estimate is valid. Uses virtual transaction size as defined\n"
      35                 :            :         "in BIP 141 (witness data is discounted).\n",
      36         [ +  - ]:        168 :         {
      37   [ +  -  +  -  :         56 :             {"conf_target", RPCArg::Type::NUM, RPCArg::Optional::NO, "Confirmation target in blocks (1 - 1008)"},
                   +  - ]
      38   [ +  -  +  -  :        112 :             {"estimate_mode", RPCArg::Type::STR, RPCArg::Default{"conservative"}, "The fee estimate mode.\n"
                   +  - ]
      39                 :            :             "Whether to return a more conservative estimate which also satisfies\n"
      40                 :            :             "a longer history. A conservative estimate potentially returns a\n"
      41                 :            :             "higher feerate and is more likely to be sufficient for the desired\n"
      42                 :            :             "target, but is not as responsive to short term drops in the\n"
      43                 :            :             "prevailing fee market. Must be one of (case insensitive):\n"
      44   [ +  -  +  -  :         56 :              "\"" + FeeModes("\"\n\"") + "\""},
             +  -  +  - ]
      45                 :            :         },
      46   [ +  -  +  - ]:         56 :         RPCResult{
      47   [ +  -  +  - ]:         56 :             RPCResult::Type::OBJ, "", "",
      48         [ +  - ]:        224 :             {
      49   [ +  -  +  -  :         56 :                 {RPCResult::Type::NUM, "feerate", /*optional=*/true, "estimate fee rate in " + CURRENCY_UNIT + "/kvB (only present if no errors were encountered)"},
             +  -  +  - ]
      50   [ +  -  +  -  :        112 :                 {RPCResult::Type::ARR, "errors", /*optional=*/true, "Errors encountered during processing (if there are any)",
                   +  - ]
      51         [ +  - ]:        112 :                     {
      52   [ +  -  +  -  :         56 :                         {RPCResult::Type::STR, "", "error"},
                   +  - ]
      53                 :            :                     }},
      54   [ +  -  +  -  :         56 :                 {RPCResult::Type::NUM, "blocks", "block number where estimate was found\n"
                   +  - ]
      55                 :            :                 "The request target will be clamped between 2 and the highest target\n"
      56                 :            :                 "fee estimation is able to return based on how long it has been running.\n"
      57                 :            :                 "An error is returned if not enough transactions and blocks\n"
      58                 :            :                 "have been observed to make an estimate for any number of blocks."},
      59                 :            :         }},
      60         [ +  - ]:         56 :         RPCExamples{
      61   [ +  -  +  -  :        112 :             HelpExampleCli("estimatesmartfee", "6") +
             +  -  +  - ]
      62   [ +  -  +  -  :         56 :             HelpExampleRpc("estimatesmartfee", "6")
                   +  - ]
      63                 :            :         },
      64         [ +  - ]:         76 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
      65                 :            :         {
      66                 :         20 :             CBlockPolicyEstimator& fee_estimator = EnsureAnyFeeEstimator(request.context);
      67                 :         20 :             const NodeContext& node = EnsureAnyNodeContext(request.context);
      68                 :         20 :             const CTxMemPool& mempool = EnsureMemPool(node);
      69                 :            : 
      70                 :         20 :             unsigned int max_target = fee_estimator.HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE);
      71                 :         20 :             unsigned int conf_target = ParseConfirmTarget(request.params[0], max_target);
      72                 :         20 :             bool conservative = true;
      73         [ +  + ]:         20 :             if (!request.params[1].isNull()) {
      74                 :        173 :                 FeeEstimateMode fee_mode;
      75         [ -  + ]:         18 :                 if (!FeeModeFromString(request.params[1].get_str(), fee_mode)) {
      76   [ +  -  +  -  :         18 :                     throw JSONRPCError(RPC_INVALID_PARAMETER, InvalidEstimateModeErrorMessage());
             -  +  +  - ]
      77                 :            :                 }
      78         [ #  # ]:          0 :                 if (fee_mode == FeeEstimateMode::ECONOMICAL) conservative = false;
      79                 :          0 :             }
      80                 :            : 
      81         [ +  - ]:          2 :             UniValue result(UniValue::VOBJ);
      82         [ +  - ]:          2 :             UniValue errors(UniValue::VARR);
      83                 :          2 :             FeeCalculation feeCalc;
      84         [ +  - ]:          2 :             CFeeRate feeRate{fee_estimator.estimateSmartFee(conf_target, &feeCalc, conservative)};
      85   [ +  -  +  -  :          2 :             if (feeRate != CFeeRate(0)) {
                   -  + ]
      86         [ #  # ]:          0 :                 CFeeRate min_mempool_feerate{mempool.GetMinFee()};
      87                 :          0 :                 CFeeRate min_relay_feerate{mempool.m_min_relay_feerate};
      88         [ #  # ]:          0 :                 feeRate = std::max({feeRate, min_mempool_feerate, min_relay_feerate});
      89   [ #  #  #  #  :          0 :                 result.pushKV("feerate", ValueFromAmount(feeRate.GetFeePerK()));
             #  #  #  # ]
      90                 :          0 :             } else {
      91   [ +  -  +  - ]:          2 :                 errors.push_back("Insufficient data or no feerate found");
      92   [ +  -  +  -  :          2 :                 result.pushKV("errors", errors);
                   +  - ]
      93                 :            :             }
      94   [ +  -  +  -  :          2 :             result.pushKV("blocks", feeCalc.returnedTarget);
                   +  - ]
      95                 :          2 :             return result;
      96         [ +  - ]:         20 :         },
      97                 :            :     };
      98                 :          0 : }
      99                 :            : 
     100                 :         44 : static RPCHelpMan estimaterawfee()
     101                 :            : {
     102   [ +  -  -  +  :         88 :     return RPCHelpMan{"estimaterawfee",
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
     103         [ +  - ]:         44 :         "\nWARNING: This interface is unstable and may disappear or change!\n"
     104                 :            :         "\nWARNING: This is an advanced API call that is tightly coupled to the specific\n"
     105                 :            :         "implementation of fee estimation. The parameters it can be called with\n"
     106                 :            :         "and the results it returns will change if the internal implementation changes.\n"
     107                 :            :         "\nEstimates the approximate fee per kilobyte needed for a transaction to begin\n"
     108                 :            :         "confirmation within conf_target blocks if possible. Uses virtual transaction size as\n"
     109                 :            :         "defined in BIP 141 (witness data is discounted).\n",
     110         [ +  - ]:        132 :         {
     111   [ +  -  +  -  :         44 :             {"conf_target", RPCArg::Type::NUM, RPCArg::Optional::NO, "Confirmation target in blocks (1 - 1008)"},
                   +  - ]
     112   [ +  -  +  -  :         44 :             {"threshold", RPCArg::Type::NUM, RPCArg::Default{0.95}, "The proportion of transactions in a given feerate range that must have been\n"
             +  -  +  - ]
     113                 :            :             "confirmed within conf_target in order to consider those feerates as high enough and proceed to check\n"
     114                 :            :             "lower buckets."},
     115                 :            :         },
     116   [ +  -  +  - ]:         44 :         RPCResult{
     117   [ +  -  +  - ]:         44 :             RPCResult::Type::OBJ, "", "Results are returned for any horizon which tracks blocks up to the confirmation target",
     118         [ +  - ]:        176 :             {
     119   [ +  -  +  -  :         88 :                 {RPCResult::Type::OBJ, "short", /*optional=*/true, "estimate for short time horizon",
                   +  - ]
     120         [ +  - ]:        308 :                     {
     121   [ +  -  +  -  :         44 :                         {RPCResult::Type::NUM, "feerate", /*optional=*/true, "estimate fee rate in " + CURRENCY_UNIT + "/kvB"},
             +  -  +  - ]
     122   [ +  -  +  -  :         44 :                         {RPCResult::Type::NUM, "decay", "exponential decay (per block) for historical moving average of confirmation data"},
                   +  - ]
     123   [ +  -  +  -  :         44 :                         {RPCResult::Type::NUM, "scale", "The resolution of confirmation targets at this time horizon"},
                   +  - ]
     124   [ +  -  +  -  :         88 :                         {RPCResult::Type::OBJ, "pass", /*optional=*/true, "information about the lowest range of feerates to succeed in meeting the threshold",
                   +  - ]
     125         [ +  - ]:        308 :                         {
     126   [ +  -  +  -  :         44 :                                 {RPCResult::Type::NUM, "startrange", "start of feerate range"},
                   +  - ]
     127   [ +  -  +  -  :         44 :                                 {RPCResult::Type::NUM, "endrange", "end of feerate range"},
                   +  - ]
     128   [ +  -  +  -  :         44 :                                 {RPCResult::Type::NUM, "withintarget", "number of txs over history horizon in the feerate range that were confirmed within target"},
                   +  - ]
     129   [ +  -  +  -  :         44 :                                 {RPCResult::Type::NUM, "totalconfirmed", "number of txs over history horizon in the feerate range that were confirmed at any point"},
                   +  - ]
     130   [ +  -  +  -  :         44 :                                 {RPCResult::Type::NUM, "inmempool", "current number of txs in mempool in the feerate range unconfirmed for at least target blocks"},
                   +  - ]
     131   [ +  -  +  -  :         44 :                                 {RPCResult::Type::NUM, "leftmempool", "number of txs over history horizon in the feerate range that left mempool unconfirmed after target"},
                   +  - ]
     132                 :            :                         }},
     133   [ +  -  +  -  :         88 :                         {RPCResult::Type::OBJ, "fail", /*optional=*/true, "information about the highest range of feerates to fail to meet the threshold",
                   +  - ]
     134         [ +  - ]:         88 :                         {
     135   [ +  -  +  -  :         44 :                             {RPCResult::Type::ELISION, "", ""},
                   +  - ]
     136                 :            :                         }},
     137   [ +  -  +  -  :         88 :                         {RPCResult::Type::ARR, "errors", /*optional=*/true, "Errors encountered during processing (if there are any)",
                   +  - ]
     138         [ +  - ]:         88 :                         {
     139   [ +  -  +  -  :         44 :                             {RPCResult::Type::STR, "error", ""},
                   +  - ]
     140                 :            :                         }},
     141                 :            :                 }},
     142   [ +  -  +  -  :         88 :                 {RPCResult::Type::OBJ, "medium", /*optional=*/true, "estimate for medium time horizon",
                   +  - ]
     143         [ +  - ]:         88 :                 {
     144   [ +  -  +  -  :         44 :                     {RPCResult::Type::ELISION, "", ""},
                   +  - ]
     145                 :            :                 }},
     146   [ +  -  +  -  :         88 :                 {RPCResult::Type::OBJ, "long", /*optional=*/true, "estimate for long time horizon",
                   +  - ]
     147         [ +  - ]:         88 :                 {
     148   [ +  -  +  -  :         44 :                     {RPCResult::Type::ELISION, "", ""},
                   +  - ]
     149                 :            :                 }},
     150                 :            :             }},
     151         [ +  - ]:         44 :         RPCExamples{
     152   [ +  -  +  -  :         44 :             HelpExampleCli("estimaterawfee", "6 0.9")
                   +  - ]
     153                 :            :         },
     154         [ +  - ]:         52 :         [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
     155                 :            :         {
     156                 :          8 :             CBlockPolicyEstimator& fee_estimator = EnsureAnyFeeEstimator(request.context);
     157                 :            : 
     158                 :          8 :             unsigned int max_target = fee_estimator.HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE);
     159                 :          8 :             unsigned int conf_target = ParseConfirmTarget(request.params[0], max_target);
     160                 :          8 :             double threshold = 0.95;
     161         [ +  + ]:          8 :             if (!request.params[1].isNull()) {
     162                 :          2 :                 threshold = request.params[1].get_real();
     163                 :          2 :             }
     164         [ +  + ]:          8 :             if (threshold < 0 || threshold > 1) {
     165   [ +  -  +  -  :          1 :                 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid threshold");
             -  +  +  - ]
     166                 :            :             }
     167                 :            : 
     168         [ +  - ]:          7 :             UniValue result(UniValue::VOBJ);
     169                 :            : 
     170         [ +  + ]:         25 :             for (const FeeEstimateHorizon horizon : ALL_FEE_ESTIMATE_HORIZONS) {
     171         [ +  - ]:         18 :                 CFeeRate feeRate;
     172                 :         18 :                 EstimationResult buckets;
     173                 :            : 
     174                 :            :                 // Only output results for horizons which track the target
     175   [ +  -  +  + ]:         18 :                 if (conf_target > fee_estimator.HighestTargetTracked(horizon)) continue;
     176                 :            : 
     177         [ +  - ]:         12 :                 feeRate = fee_estimator.estimateRawFee(conf_target, threshold, horizon, &buckets);
     178         [ -  + ]:         12 :                 UniValue horizon_result(UniValue::VOBJ);
     179         [ -  + ]:         12 :                 UniValue errors(UniValue::VARR);
     180         [ -  + ]:         12 :                 UniValue passbucket(UniValue::VOBJ);
     181   [ -  +  -  +  :         12 :                 passbucket.pushKV("startrange", round(buckets.pass.start));
                   -  + ]
     182   [ -  +  -  +  :         12 :                 passbucket.pushKV("endrange", round(buckets.pass.end));
                   -  + ]
     183   [ -  +  -  +  :         12 :                 passbucket.pushKV("withintarget", round(buckets.pass.withinTarget * 100.0) / 100.0);
                   -  + ]
     184   [ -  +  -  +  :         12 :                 passbucket.pushKV("totalconfirmed", round(buckets.pass.totalConfirmed * 100.0) / 100.0);
                   -  + ]
     185   [ -  +  -  +  :         12 :                 passbucket.pushKV("inmempool", round(buckets.pass.inMempool * 100.0) / 100.0);
                   -  + ]
     186   [ -  +  -  +  :         12 :                 passbucket.pushKV("leftmempool", round(buckets.pass.leftMempool * 100.0) / 100.0);
                   -  + ]
     187         [ -  + ]:         12 :                 UniValue failbucket(UniValue::VOBJ);
     188   [ -  +  -  +  :         12 :                 failbucket.pushKV("startrange", round(buckets.fail.start));
                   -  + ]
     189   [ -  +  -  +  :         12 :                 failbucket.pushKV("endrange", round(buckets.fail.end));
                   -  + ]
     190   [ -  +  -  +  :         12 :                 failbucket.pushKV("withintarget", round(buckets.fail.withinTarget * 100.0) / 100.0);
                   -  + ]
     191   [ +  -  +  -  :         12 :                 failbucket.pushKV("totalconfirmed", round(buckets.fail.totalConfirmed * 100.0) / 100.0);
                   +  - ]
     192   [ +  -  +  -  :         12 :                 failbucket.pushKV("inmempool", round(buckets.fail.inMempool * 100.0) / 100.0);
                   +  - ]
     193   [ +  -  +  -  :         12 :                 failbucket.pushKV("leftmempool", round(buckets.fail.leftMempool * 100.0) / 100.0);
                   +  - ]
     194                 :            : 
     195                 :            :                 // CFeeRate(0) is used to indicate error as a return value from estimateRawFee
     196   [ +  -  -  + ]:         12 :                 if (feeRate != CFeeRate(0)) {
     197   [ #  #  #  #  :          0 :                     horizon_result.pushKV("feerate", ValueFromAmount(feeRate.GetFeePerK()));
                   #  # ]
     198   [ #  #  #  #  :          0 :                     horizon_result.pushKV("decay", buckets.decay);
                   #  # ]
     199   [ #  #  #  #  :          0 :                     horizon_result.pushKV("scale", (int)buckets.scale);
                   #  # ]
     200   [ #  #  #  #  :          0 :                     horizon_result.pushKV("pass", passbucket);
                   #  # ]
     201                 :            :                     // buckets.fail.start == -1 indicates that all buckets passed, there is no fail bucket to output
     202   [ #  #  #  #  :          0 :                     if (buckets.fail.start != -1) horizon_result.pushKV("fail", failbucket);
             #  #  #  # ]
     203                 :          0 :                 } else {
     204                 :            :                     // Output only information that is still meaningful in the event of error
     205   [ -  +  -  +  :         12 :                     horizon_result.pushKV("decay", buckets.decay);
                   -  + ]
     206   [ -  +  -  +  :         12 :                     horizon_result.pushKV("scale", (int)buckets.scale);
                   -  + ]
     207   [ -  +  -  +  :         12 :                     horizon_result.pushKV("fail", failbucket);
                   +  - ]
     208   [ +  -  -  + ]:         12 :                     errors.push_back("Insufficient data or no feerate found which meets threshold");
     209   [ -  +  -  +  :         12 :                     horizon_result.pushKV("errors", errors);
                   -  + ]
     210                 :            :                 }
     211   [ +  -  +  -  :         12 :                 result.pushKV(StringForFeeEstimateHorizon(horizon), horizon_result);
                   +  - ]
     212                 :         12 :             }
     213                 :          7 :             return result;
     214         [ +  - ]:          8 :         },
     215                 :            :     };
     216                 :          0 : }
     217                 :            : 
     218                 :        224 : void RegisterFeeRPCCommands(CRPCTable& t)
     219                 :            : {
     220   [ +  +  -  +  :        258 :     static const CRPCCommand commands[]{
                   #  # ]
     221   [ +  -  +  - ]:         17 :         {"util", &estimatesmartfee},
     222   [ +  -  -  + ]:         17 :         {"hidden", &estimaterawfee},
     223                 :            :     };
     224         [ +  + ]:        672 :     for (const auto& c : commands) {
     225                 :        448 :         t.appendCommand(c.name, &c);
     226                 :            :     }
     227                 :        224 : }

Generated by: LCOV version 1.14