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