Skip to content

Commit 269553f

Browse files
mzumsandefanquake
authored andcommitted
test: Call ceildiv helper with integer
It returns an incorrect result when called with a Decimal, for which the "//" operator works differently. Also drop unnecessary call to satoshi_round. Github-Pull: #24239 Rebased-From: d1fab9d
1 parent 2f60fc6 commit 269553f

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

test/functional/test_framework/util.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def assert_approx(v, vexp, vspan=0.00001):
3737

3838
def assert_fee_amount(fee, tx_size, feerate_BTC_kvB):
3939
"""Assert the fee is in range."""
40+
assert isinstance(tx_size, int)
4041
target_fee = get_fee(tx_size, feerate_BTC_kvB)
4142
if fee < target_fee:
4243
raise AssertionError("Fee of %s BTC too low! (Should be %s BTC)" % (str(fee), str(target_fee)))
@@ -224,15 +225,21 @@ def str_to_b64str(string):
224225

225226

226227
def ceildiv(a, b):
227-
"""Divide 2 ints and round up to next int rather than round down"""
228+
"""
229+
Divide 2 ints and round up to next int rather than round down
230+
Implementation requires python integers, which have a // operator that does floor division.
231+
Other types like decimal.Decimal whose // operator truncates towards 0 will not work.
232+
"""
233+
assert isinstance(a, int)
234+
assert isinstance(b, int)
228235
return -(-a // b)
229236

230237

231238
def get_fee(tx_size, feerate_btc_kvb):
232239
"""Calculate the fee in BTC given a feerate is BTC/kvB. Reflects CFeeRate::GetFee"""
233240
feerate_sat_kvb = int(feerate_btc_kvb * Decimal(1e8)) # Fee in sat/kvb as an int to avoid float precision errors
234241
target_fee_sat = ceildiv(feerate_sat_kvb * tx_size, 1000) # Round calculated fee up to nearest sat
235-
return satoshi_round(target_fee_sat / Decimal(1e8)) # Truncate BTC result to nearest sat
242+
return target_fee_sat / Decimal(1e8) # Return result in BTC
236243

237244

238245
def satoshi_round(amount):

test/functional/wallet_send.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
assert_fee_amount,
1616
assert_greater_than,
1717
assert_raises_rpc_error,
18+
count_bytes,
1819
)
1920

2021
class WalletSendTest(BitcoinTestFramework):
@@ -318,20 +319,20 @@ def run_test(self):
318319

319320
res = self.test_send(from_wallet=w0, to_wallet=w1, amount=1, fee_rate=7, add_to_wallet=False)
320321
fee = self.nodes[1].decodepsbt(res["psbt"])["fee"]
321-
assert_fee_amount(fee, Decimal(len(res["hex"]) / 2), Decimal("0.00007"))
322+
assert_fee_amount(fee, count_bytes(res["hex"]), Decimal("0.00007"))
322323

323324
# "unset" and None are treated the same for estimate_mode
324325
res = self.test_send(from_wallet=w0, to_wallet=w1, amount=1, fee_rate=2, estimate_mode="unset", add_to_wallet=False)
325326
fee = self.nodes[1].decodepsbt(res["psbt"])["fee"]
326-
assert_fee_amount(fee, Decimal(len(res["hex"]) / 2), Decimal("0.00002"))
327+
assert_fee_amount(fee, count_bytes(res["hex"]), Decimal("0.00002"))
327328

328329
res = self.test_send(from_wallet=w0, to_wallet=w1, amount=1, arg_fee_rate=4.531, add_to_wallet=False)
329330
fee = self.nodes[1].decodepsbt(res["psbt"])["fee"]
330-
assert_fee_amount(fee, Decimal(len(res["hex"]) / 2), Decimal("0.00004531"))
331+
assert_fee_amount(fee, count_bytes(res["hex"]), Decimal("0.00004531"))
331332

332333
res = self.test_send(from_wallet=w0, to_wallet=w1, amount=1, arg_fee_rate=3, add_to_wallet=False)
333334
fee = self.nodes[1].decodepsbt(res["psbt"])["fee"]
334-
assert_fee_amount(fee, Decimal(len(res["hex"]) / 2), Decimal("0.00003"))
335+
assert_fee_amount(fee, count_bytes(res["hex"]), Decimal("0.00003"))
335336

336337
# Test that passing fee_rate as both an argument and an option raises.
337338
self.test_send(from_wallet=w0, to_wallet=w1, amount=1, arg_fee_rate=1, fee_rate=1, add_to_wallet=False,

0 commit comments

Comments
 (0)