Skip to content

Commit 210210c

Browse files
committed
Merge bitcoin/bitcoin#29566: test: update satoshi_round function
ec317bc test: update satoshi_round function (naiyoma) Pull request description: This PR refactors `satoshi_round` to accept different rounding modes and make rounding a required argument. Continuation of bitcoin/bitcoin#23225 ACKs for top commit: maflcko: review ACK ec317bc achow101: ACK ec317bc AngusP: ACK ec317bc Tree-SHA512: 070f0aa6f66e58bff7412cae6b71f5f4ab8c718c7b5eeba4bb604fe22c6416a1ced0474294f504e1c28943ddc073104466b5944b43bae27e42bee5ca85afa468
2 parents b0c3de6 + ec317bc commit 210210c

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

test/functional/feature_fee_estimation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Test fee estimation code."""
66
from copy import deepcopy
7-
from decimal import Decimal
7+
from decimal import Decimal, ROUND_DOWN
88
import os
99
import random
1010
import time
@@ -40,7 +40,7 @@ def small_txpuzzle_randfee(
4040
# Exponentially distributed from 1-128 * fee_increment
4141
rand_fee = float(fee_increment) * (1.1892 ** random.randint(0, 28))
4242
# Total fee ranges from min_fee to min_fee + 127*fee_increment
43-
fee = min_fee - fee_increment + satoshi_round(rand_fee)
43+
fee = min_fee - fee_increment + satoshi_round(rand_fee, rounding=ROUND_DOWN)
4444
utxos_to_spend = []
4545
total_in = Decimal("0.00000000")
4646
while total_in <= (amount + fee) and len(conflist) > 0:

test/functional/test_framework/util.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"""Helpful routines for regression testing."""
66

77
from base64 import b64encode
8-
from decimal import Decimal, ROUND_DOWN
8+
from decimal import Decimal
99
from subprocess import CalledProcessError
1010
import hashlib
1111
import inspect
@@ -21,7 +21,9 @@
2121
from . import coverage
2222
from .authproxy import AuthServiceProxy, JSONRPCException
2323
from collections.abc import Callable
24-
from typing import Optional
24+
from typing import Optional, Union
25+
26+
SATOSHI_PRECISION = Decimal('0.00000001')
2527

2628
logger = logging.getLogger("TestFramework.utils")
2729

@@ -261,8 +263,9 @@ def get_fee(tx_size, feerate_btc_kvb):
261263
return target_fee_sat / Decimal(1e8) # Return result in BTC
262264

263265

264-
def satoshi_round(amount):
265-
return Decimal(amount).quantize(Decimal('0.00000001'), rounding=ROUND_DOWN)
266+
def satoshi_round(amount: Union[int, float, str], *, rounding: str) -> Decimal:
267+
"""Rounds a Decimal amount to the nearest satoshi using the specified rounding mode."""
268+
return Decimal(amount).quantize(SATOSHI_PRECISION, rounding=rounding)
266269

267270

268271
def wait_until_helper_internal(predicate, *, attempts=float('inf'), timeout=float('inf'), lock=None, timeout_factor=1.0):

0 commit comments

Comments
 (0)