Skip to content

Commit c265aad

Browse files
committed
Merge bitcoin/bitcoin#29434: rpc: Fixed signed integer overflow for large feerates
dddd7be doc: Clarify maxfeerate help (MarcoFalke) fa2a4fd rpc: Fixed signed integer overflow for large feerates (MarcoFalke) fade94d rpc: Add ParseFeeRate helper (MarcoFalke) fa0ff66 rpc: Implement RPCHelpMan::ArgValue<> for UniValue (MarcoFalke) Pull request description: Passing large BTC/kvB feerates to RPCs is problematic, because: * They are likely a typo. 1BTC/kvB (or larger) seems absurd. * They may cause signed integer overflow. * Anyone really wanting to pick such a large value can set `0` to disable the check. Fix all issues by rejecting anything more than 1BTC/kvB during parsing. ACKs for top commit: brunoerg: crACK dddd7be achow101: ACK dddd7be vasild: ACK dddd7be tdb3: Code review ACK and basic test ACK for dddd7be. fjahr: utACK dddd7be Tree-SHA512: 5dcce1f0abe059dc6b2ff56787e11081d73a45b4ddd6dcc2c1ea13709ebc13af5e7265e84fffb97ef32027b56b81955672a67ed7702e8fa30c2e849d67727bac
2 parents ddf1d72 + dddd7be commit c265aad

File tree

4 files changed

+27
-8
lines changed

4 files changed

+27
-8
lines changed

src/rpc/mempool.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static RPCHelpMan sendrawtransaction()
4545
{"hexstring", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The hex string of the raw transaction"},
4646
{"maxfeerate", RPCArg::Type::AMOUNT, RPCArg::Default{FormatMoney(DEFAULT_MAX_RAW_TX_FEE_RATE.GetFeePerK())},
4747
"Reject transactions whose fee rate is higher than the specified value, expressed in " + CURRENCY_UNIT +
48-
"/kvB.\nSet to 0 to accept any fee rate."},
48+
"/kvB.\nFee rates larger than 1BTC/kvB are rejected.\nSet to 0 to accept any fee rate."},
4949
{"maxburnamount", RPCArg::Type::AMOUNT, RPCArg::Default{FormatMoney(0)},
5050
"Reject transactions with provably unspendable outputs (e.g. 'datacarrier' outputs that use the OP_RETURN opcode) greater than the specified value, expressed in " + CURRENCY_UNIT + ".\n"
5151
"If burning funds through unspendable outputs is desired, increase this value.\n"
@@ -81,9 +81,7 @@ static RPCHelpMan sendrawtransaction()
8181

8282
CTransactionRef tx(MakeTransactionRef(std::move(mtx)));
8383

84-
const CFeeRate max_raw_tx_fee_rate = request.params[1].isNull() ?
85-
DEFAULT_MAX_RAW_TX_FEE_RATE :
86-
CFeeRate(AmountFromValue(request.params[1]));
84+
const CFeeRate max_raw_tx_fee_rate{ParseFeeRate(self.Arg<UniValue>(1))};
8785

8886
int64_t virtual_size = GetVirtualTransactionSize(*tx);
8987
CAmount max_raw_tx_fee = max_raw_tx_fee_rate.GetFee(virtual_size);
@@ -117,7 +115,8 @@ static RPCHelpMan testmempoolaccept()
117115
},
118116
},
119117
{"maxfeerate", RPCArg::Type::AMOUNT, RPCArg::Default{FormatMoney(DEFAULT_MAX_RAW_TX_FEE_RATE.GetFeePerK())},
120-
"Reject transactions whose fee rate is higher than the specified value, expressed in " + CURRENCY_UNIT + "/kvB\n"},
118+
"Reject transactions whose fee rate is higher than the specified value, expressed in " + CURRENCY_UNIT +
119+
"/kvB.\nFee rates larger than 1BTC/kvB are rejected.\nSet to 0 to accept any fee rate."},
121120
},
122121
RPCResult{
123122
RPCResult::Type::ARR, "", "The result of the mempool acceptance test for each raw transaction in the input array.\n"
@@ -162,9 +161,7 @@ static RPCHelpMan testmempoolaccept()
162161
"Array must contain between 1 and " + ToString(MAX_PACKAGE_COUNT) + " transactions.");
163162
}
164163

165-
const CFeeRate max_raw_tx_fee_rate = request.params[1].isNull() ?
166-
DEFAULT_MAX_RAW_TX_FEE_RATE :
167-
CFeeRate(AmountFromValue(request.params[1]));
164+
const CFeeRate max_raw_tx_fee_rate{ParseFeeRate(self.Arg<UniValue>(1))};
168165

169166
std::vector<CTransactionRef> txns;
170167
txns.reserve(raw_transactions.size());

src/rpc/util.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ CAmount AmountFromValue(const UniValue& value, int decimals)
7575
return amount;
7676
}
7777

78+
CFeeRate ParseFeeRate(const UniValue& json)
79+
{
80+
CAmount val{AmountFromValue(json)};
81+
if (val >= COIN) throw JSONRPCError(RPC_INVALID_PARAMETER, "Fee rates larger than or equal to 1BTC/kvB are not accepted");
82+
return CFeeRate{val};
83+
}
84+
7885
uint256 ParseHashV(const UniValue& v, std::string_view name)
7986
{
8087
const std::string& strHex(v.get_str());
@@ -678,11 +685,13 @@ static void CheckRequiredOrDefault(const RPCArg& param)
678685
void force_semicolon(ret_type)
679686

680687
// Optional arg (without default). Can also be called on required args, if needed.
688+
TMPL_INST(nullptr, const UniValue*, maybe_arg;);
681689
TMPL_INST(nullptr, std::optional<double>, maybe_arg ? std::optional{maybe_arg->get_real()} : std::nullopt;);
682690
TMPL_INST(nullptr, std::optional<bool>, maybe_arg ? std::optional{maybe_arg->get_bool()} : std::nullopt;);
683691
TMPL_INST(nullptr, const std::string*, maybe_arg ? &maybe_arg->get_str() : nullptr;);
684692

685693
// Required arg or optional arg with default value.
694+
TMPL_INST(CheckRequiredOrDefault, const UniValue&, *CHECK_NONFATAL(maybe_arg););
686695
TMPL_INST(CheckRequiredOrDefault, bool, CHECK_NONFATAL(maybe_arg)->get_bool(););
687696
TMPL_INST(CheckRequiredOrDefault, int, CHECK_NONFATAL(maybe_arg)->getInt<int>(););
688697
TMPL_INST(CheckRequiredOrDefault, uint64_t, CHECK_NONFATAL(maybe_arg)->getInt<uint64_t>(););

src/rpc/util.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ std::vector<unsigned char> ParseHexO(const UniValue& o, std::string_view strKey)
103103
* @returns a CAmount if the various checks pass.
104104
*/
105105
CAmount AmountFromValue(const UniValue& value, int decimals = 8);
106+
/**
107+
* Parse a json number or string, denoting BTC/kvB, into a CFeeRate (sat/kvB).
108+
* Reject negative values or rates larger than 1BTC/kvB.
109+
*/
110+
CFeeRate ParseFeeRate(const UniValue& json);
106111

107112
using RPCArgList = std::vector<std::pair<std::string, UniValue>>;
108113
std::string HelpExampleCli(const std::string& methodname, const std::string& args);

test/functional/mempool_accept.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,17 @@ def run_test(self):
9090
txid_in_block = self.wallet.sendrawtransaction(from_node=node, tx_hex=raw_tx_in_block)
9191
self.generate(node, 1)
9292
self.mempool_size = 0
93+
# Also check feerate. 1BTC/kvB fails
94+
assert_raises_rpc_error(-8, "Fee rates larger than or equal to 1BTC/kvB are not accepted", lambda: self.check_mempool_result(
95+
result_expected=None,
96+
rawtxs=[raw_tx_in_block],
97+
maxfeerate=1,
98+
))
99+
# ... 0.99 passes
93100
self.check_mempool_result(
94101
result_expected=[{'txid': txid_in_block, 'allowed': False, 'reject-reason': 'txn-already-known'}],
95102
rawtxs=[raw_tx_in_block],
103+
maxfeerate=0.99,
96104
)
97105

98106
self.log.info('A transaction not in the mempool')

0 commit comments

Comments
 (0)