Skip to content

Commit ce2cc44

Browse files
committed
tests: Test for assertion when feerate is rounded down
When calculating a txs absolute fee, if the fee is rounded down to the nearest satoshi, it is possible for the coin selection algorithms to undercalculate the fee needed. This can lead to an assertion error in some situations. One such scenario is added to rpc_fundrawtransaction.py.
1 parent 0fbaef9 commit ce2cc44

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

test/functional/rpc_fundrawtransaction.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ def run_test(self):
136136
self.test_include_unsafe()
137137
self.test_external_inputs()
138138
self.test_22670()
139+
self.test_feerate_rounding()
139140

140141
def test_change_position(self):
141142
"""Ensure setting changePosition in fundraw with an exact match is handled properly."""
@@ -1129,6 +1130,33 @@ def do_fund_send(target):
11291130
do_fund_send(upper_bound)
11301131

11311132
self.restart_node(0)
1133+
self.connect_nodes(0, 1)
1134+
self.connect_nodes(0, 2)
1135+
self.connect_nodes(0, 3)
1136+
1137+
def test_feerate_rounding(self):
1138+
self.log.info("Test that rounding of GetFee does not result in an assertion")
1139+
1140+
self.nodes[1].createwallet("roundtest")
1141+
w = self.nodes[1].get_wallet_rpc("roundtest")
1142+
1143+
addr = w.getnewaddress(address_type="bech32")
1144+
self.nodes[0].sendtoaddress(addr, 1)
1145+
self.generate(self.nodes[0], 1)
1146+
self.sync_all()
1147+
1148+
# A P2WPKH input costs 68 vbytes; With a single P2WPKH output, the rest of the tx is 42 vbytes for a total of 110 vbytes.
1149+
# At a feerate of 1.85 sat/vb, the input will need a fee of 125.8 sats and the rest 77.7 sats
1150+
# The entire tx fee should be 203.5 sats.
1151+
# Coin selection rounds the fee individually instead of at the end (due to how CFeeRate::GetFee works).
1152+
# If rounding down (which is the incorrect behavior), then the calculated fee will be 125 + 77 = 202.
1153+
# If rounding up, then the calculated fee will be 126 + 78 = 204.
1154+
# In the former case, the calculated needed fee is higher than the actual fee being paid, so an assertion is reached
1155+
# To test this does not happen, we subtract 202 sats from the input value. If working correctly, this should
1156+
# fail with insufficient funds rather than bitcoind asserting.
1157+
rawtx = w.createrawtransaction(inputs=[], outputs=[{self.nodes[0].getnewaddress(address_type="bech32"): 1 - 0.00000202}])
1158+
assert_raises_rpc_error(-4, "Insufficient funds", w.fundrawtransaction, rawtx, {"fee_rate": 1.85})
1159+
11321160

11331161
if __name__ == '__main__':
11341162
RawTransactionsTest().main()

0 commit comments

Comments
 (0)