Skip to content

Commit 449b730

Browse files
wallet: provide valid values if invalid estimate mode passed
Co-authored-by: Murch <[email protected]>
1 parent 6da3afb commit 449b730

File tree

10 files changed

+20
-16
lines changed

10 files changed

+20
-16
lines changed

src/rpc/mining.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,7 @@ static RPCHelpMan estimatesmartfee()
10701070
if (!request.params[1].isNull()) {
10711071
FeeEstimateMode fee_mode;
10721072
if (!FeeModeFromString(request.params[1].get_str(), fee_mode)) {
1073-
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid estimate_mode parameter");
1073+
throw JSONRPCError(RPC_INVALID_PARAMETER, InvalidEstimateModeErrorMessage());
10741074
}
10751075
if (fee_mode == FeeEstimateMode::ECONOMICAL) conservative = false;
10761076
}

src/util/fees.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ std::string FeeModes(const std::string& delimiter)
4949
return Join(FeeModeMap(), delimiter, [&](const std::pair<std::string, FeeEstimateMode>& i) { return i.first; });
5050
}
5151

52+
const std::string InvalidEstimateModeErrorMessage()
53+
{
54+
return "Invalid estimate_mode parameter, must be one of: \"" + FeeModes("\", \"") + "\"";
55+
}
56+
5257
bool FeeModeFromString(const std::string& mode_string, FeeEstimateMode& fee_estimate_mode)
5358
{
5459
auto searchkey = ToUpper(mode_string);

src/util/fees.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ enum class FeeReason;
1313
bool FeeModeFromString(const std::string& mode_string, FeeEstimateMode& fee_estimate_mode);
1414
std::string StringForFeeReason(FeeReason reason);
1515
std::string FeeModes(const std::string& delimiter);
16+
const std::string InvalidEstimateModeErrorMessage();
1617

1718
#endif // BITCOIN_UTIL_FEES_H

src/wallet/rpcwallet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ static void SetFeeEstimateMode(const CWallet* pwallet, CCoinControl& cc, const U
221221
return;
222222
}
223223
if (!estimate_mode.isNull() && !FeeModeFromString(estimate_mode.get_str(), cc.m_fee_mode)) {
224-
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid estimate_mode parameter");
224+
throw JSONRPCError(RPC_INVALID_PARAMETER, InvalidEstimateModeErrorMessage());
225225
}
226226
if (!conf_target.isNull()) {
227227
cc.m_confirm_target = ParseConfirmTarget(conf_target, pwallet->chain().estimateMaxBlocks());

test/functional/rpc_estimatefee.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def run_test(self):
2828

2929
# wrong type for estimatesmartfee(estimate_mode)
3030
assert_raises_rpc_error(-3, "Expected type string, got number", self.nodes[0].estimatesmartfee, 1, 1)
31-
assert_raises_rpc_error(-8, "Invalid estimate_mode parameter", self.nodes[0].estimatesmartfee, 1, 'foo')
31+
assert_raises_rpc_error(-8, 'Invalid estimate_mode parameter, must be one of: "unset", "economical", "conservative"', self.nodes[0].estimatesmartfee, 1, 'foo')
3232

3333
# wrong type for estimaterawfee(threshold)
3434
assert_raises_rpc_error(-3, "Expected type number, got string", self.nodes[0].estimaterawfee, 1, 'foo')

test/functional/rpc_fundrawtransaction.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ def test_option_feerate(self):
738738
assert_raises_rpc_error(-3, "Expected type string for estimate_mode, got {}".format(k),
739739
node.fundrawtransaction, rawtx, {"estimate_mode": v, "conf_target": 0.1, "add_inputs": True})
740740
for mode in ["", "foo", Decimal("3.141592")]:
741-
assert_raises_rpc_error(-8, "Invalid estimate_mode parameter",
741+
assert_raises_rpc_error(-8, 'Invalid estimate_mode parameter, must be one of: "unset", "economical", "conservative"',
742742
node.fundrawtransaction, rawtx, {"estimate_mode": mode, "conf_target": 0.1, "add_inputs": True})
743743

744744
self.log.info("Test fundrawtxn with invalid conf_target settings")

test/functional/rpc_psbt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def run_test(self):
231231
assert_raises_rpc_error(-3, "Expected type string for estimate_mode, got {}".format(k),
232232
self.nodes[1].walletcreatefundedpsbt, inputs, outputs, 0, {"estimate_mode": v, "conf_target": 0.1, "add_inputs": True})
233233
for mode in ["", "foo", Decimal("3.141592")]:
234-
assert_raises_rpc_error(-8, "Invalid estimate_mode parameter",
234+
assert_raises_rpc_error(-8, 'Invalid estimate_mode parameter, must be one of: "unset", "economical", "conservative"',
235235
self.nodes[1].walletcreatefundedpsbt, inputs, outputs, 0, {"estimate_mode": mode, "conf_target": 0.1, "add_inputs": True})
236236

237237
self.log.info("- raises RPC error with invalid conf_target settings")

test/functional/wallet_basic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ def run_test(self):
263263
assert_raises_rpc_error(-8, "Invalid conf_target, must be between 1 and 1008", # max value of 1008 per src/policy/fees.h
264264
self.nodes[2].sendmany, amounts={address: 1}, conf_target=target, estimate_mode=mode)
265265
for target, mode in product([-1, 0], ["btc/kb", "sat/b"]):
266-
assert_raises_rpc_error(-8, "Invalid estimate_mode parameter",
266+
assert_raises_rpc_error(-8, 'Invalid estimate_mode parameter, must be one of: "unset", "economical", "conservative"',
267267
self.nodes[2].sendmany, amounts={address: 1}, conf_target=target, estimate_mode=mode)
268268

269269
self.start_node(3, self.nodes[3].extra_args)
@@ -434,7 +434,7 @@ def run_test(self):
434434
assert_raises_rpc_error(-8, "Invalid conf_target, must be between 1 and 1008", # max value of 1008 per src/policy/fees.h
435435
self.nodes[2].sendtoaddress, address=address, amount=1, conf_target=target, estimate_mode=mode)
436436
for target, mode in product([-1, 0], ["btc/kb", "sat/b"]):
437-
assert_raises_rpc_error(-8, "Invalid estimate_mode parameter",
437+
assert_raises_rpc_error(-8, 'Invalid estimate_mode parameter, must be one of: "unset", "economical", "conservative"',
438438
self.nodes[2].sendtoaddress, address=address, amount=1, conf_target=target, estimate_mode=mode)
439439

440440
# 2. Import address from node2 to node1

test/functional/wallet_bumpfee.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ def test_invalid_parameters(self, rbf_node, peer_node, dest_address):
138138
assert_raises_rpc_error(-3, "Expected type string for estimate_mode, got {}".format(k),
139139
rbf_node.bumpfee, rbfid, {"estimate_mode": v})
140140
for mode in ["foo", Decimal("3.1415"), "sat/B", "BTC/kB"]:
141-
assert_raises_rpc_error(-8, "Invalid estimate_mode parameter", rbf_node.bumpfee, rbfid, {"estimate_mode": mode})
141+
assert_raises_rpc_error(-8, 'Invalid estimate_mode parameter, must be one of: "unset", "economical", "conservative"',
142+
rbf_node.bumpfee, rbfid, {"estimate_mode": mode})
142143

143144
self.clear_mempool()
144145

test/functional/wallet_send.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -289,16 +289,13 @@ def run_test(self):
289289
for target, mode in product([-1, 0, 1009], ["economical", "conservative"]):
290290
self.test_send(from_wallet=w0, to_wallet=w1, amount=1, conf_target=target, estimate_mode=mode,
291291
expect_error=(-8, "Invalid conf_target, must be between 1 and 1008")) # max value of 1008 per src/policy/fees.h
292+
msg = 'Invalid estimate_mode parameter, must be one of: "unset", "economical", "conservative"'
292293
for target, mode in product([-1, 0], ["btc/kb", "sat/b"]):
293-
self.test_send(from_wallet=w0, to_wallet=w1, amount=1, conf_target=target, estimate_mode=mode,
294-
expect_error=(-8, "Invalid estimate_mode parameter"))
295-
294+
self.test_send(from_wallet=w0, to_wallet=w1, amount=1, conf_target=target, estimate_mode=mode, expect_error=(-8, msg))
296295
for mode in ["", "foo", Decimal("3.141592")]:
297-
self.test_send(from_wallet=w0, to_wallet=w1, amount=1, conf_target=0.1, estimate_mode=mode,
298-
expect_error=(-8, "Invalid estimate_mode parameter"))
299-
self.test_send(from_wallet=w0, to_wallet=w1, amount=1, arg_conf_target=0.1, arg_estimate_mode=mode,
300-
expect_error=(-8, "Invalid estimate_mode parameter"))
301-
assert_raises_rpc_error(-8, "Invalid estimate_mode parameter", w0.send, {w1.getnewaddress(): 1}, 0.1, mode)
296+
self.test_send(from_wallet=w0, to_wallet=w1, amount=1, conf_target=0.1, estimate_mode=mode, expect_error=(-8, msg))
297+
self.test_send(from_wallet=w0, to_wallet=w1, amount=1, arg_conf_target=0.1, arg_estimate_mode=mode, expect_error=(-8, msg))
298+
assert_raises_rpc_error(-8, msg, w0.send, {w1.getnewaddress(): 1}, 0.1, mode)
302299

303300
for mode in ["economical", "conservative", "btc/kb", "sat/b"]:
304301
self.log.debug("{}".format(mode))

0 commit comments

Comments
 (0)