Skip to content

Commit ec317bc

Browse files
naiyomakatesalazar
andcommitted
test: update satoshi_round function
Refactor satoshi_round function to accept different rounding modes. Updated call site to use the revised `satoshi_round` function. Co-authored-by: Kate Salazar <52637275+katesalazar@users.noreply.github.com>
1 parent 98005b6 commit ec317bc

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
@@ -20,7 +20,9 @@
2020
from . import coverage
2121
from .authproxy import AuthServiceProxy, JSONRPCException
2222
from collections.abc import Callable
23-
from typing import Optional
23+
from typing import Optional, Union
24+
25+
SATOSHI_PRECISION = Decimal('0.00000001')
2426

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

@@ -237,8 +239,9 @@ def get_fee(tx_size, feerate_btc_kvb):
237239
return target_fee_sat / Decimal(1e8) # Return result in BTC
238240

239241

240-
def satoshi_round(amount):
241-
return Decimal(amount).quantize(Decimal('0.00000001'), rounding=ROUND_DOWN)
242+
def satoshi_round(amount: Union[int, float, str], *, rounding: str) -> Decimal:
243+
"""Rounds a Decimal amount to the nearest satoshi using the specified rounding mode."""
244+
return Decimal(amount).quantize(SATOSHI_PRECISION, rounding=rounding)
242245

243246

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

0 commit comments

Comments
 (0)