Skip to content

Commit 1cb4e33

Browse files
author
MarcoFalke
committed
Merge #20039: test: Convert amounts from float to decimal
5aadd4b Convert amounts from float to decimal (Prayank) Pull request description: > decimal is preferred in accounting applications https://docs.python.org/3.8/library/decimal.html Decimal type saves an exact value so better than using float. ~~3 variables declared with type as 'Decimal' in [test/functional/mempool_accept.py](https://github.com/bitcoin/bitcoin/blob/master/test/functional/mempool_accept.py): fee, fee_expected, output_amount~~ ~~Not required to convert to string anymore for using the above variables as decimal~~ + fee, fee_expected, output_amount ~~+ 8 decimal places~~ + Using value of coin['amount'] as decimal and removed 'int' + Removed unnecessary parentheses + Remove str() and use quotes Fixes bitcoin/bitcoin#20011 ACKs for top commit: guggero: ACK 5aadd4b Tree-SHA512: 5877cf3837e5b65bec0fc8909de141a720bfa02a747513e21d20f3c41ec0cfecc524d2c347a96596b0a1a97900da2acf08b799f26b11d537e4dcddc6ce45f38e
2 parents dda18e7 + 5aadd4b commit 1cb4e33

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

test/functional/mempool_accept.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,31 +83,31 @@ def run_test(self):
8383
)
8484

8585
self.log.info('A transaction not in the mempool')
86-
fee = 0.00000700
86+
fee = Decimal('0.000007')
8787
raw_tx_0 = node.signrawtransactionwithwallet(node.createrawtransaction(
8888
inputs=[{"txid": txid_in_block, "vout": 0, "sequence": BIP125_SEQUENCE_NUMBER}], # RBF is used later
89-
outputs=[{node.getnewaddress(): 0.3 - fee}],
89+
outputs=[{node.getnewaddress(): Decimal('0.3') - fee}],
9090
))['hex']
9191
tx = CTransaction()
9292
tx.deserialize(BytesIO(hex_str_to_bytes(raw_tx_0)))
9393
txid_0 = tx.rehash()
9494
self.check_mempool_result(
95-
result_expected=[{'txid': txid_0, 'allowed': True, 'vsize': tx.get_vsize(), 'fees': {'base': Decimal(str(fee))}}],
95+
result_expected=[{'txid': txid_0, 'allowed': True, 'vsize': tx.get_vsize(), 'fees': {'base': fee}}],
9696
rawtxs=[raw_tx_0],
9797
)
9898

9999
self.log.info('A final transaction not in the mempool')
100100
coin = coins.pop() # Pick a random coin(base) to spend
101-
output_amount = 0.025
101+
output_amount = Decimal('0.025')
102102
raw_tx_final = node.signrawtransactionwithwallet(node.createrawtransaction(
103103
inputs=[{'txid': coin['txid'], 'vout': coin['vout'], "sequence": 0xffffffff}], # SEQUENCE_FINAL
104104
outputs=[{node.getnewaddress(): output_amount}],
105105
locktime=node.getblockcount() + 2000, # Can be anything
106106
))['hex']
107107
tx.deserialize(BytesIO(hex_str_to_bytes(raw_tx_final)))
108-
fee_expected = int(coin['amount']) - output_amount
108+
fee_expected = coin['amount'] - output_amount
109109
self.check_mempool_result(
110-
result_expected=[{'txid': tx.rehash(), 'allowed': True, 'vsize': tx.get_vsize(), 'fees': {'base': Decimal(str(fee_expected))}}],
110+
result_expected=[{'txid': tx.rehash(), 'allowed': True, 'vsize': tx.get_vsize(), 'fees': {'base': fee_expected}}],
111111
rawtxs=[tx.serialize().hex()],
112112
maxfeerate=0,
113113
)
@@ -130,7 +130,7 @@ def run_test(self):
130130
tx.deserialize(BytesIO(hex_str_to_bytes(raw_tx_0)))
131131
txid_0 = tx.rehash()
132132
self.check_mempool_result(
133-
result_expected=[{'txid': txid_0, 'allowed': True, 'vsize': tx.get_vsize(), 'fees': {'base': Decimal(str(2 * fee))}}],
133+
result_expected=[{'txid': txid_0, 'allowed': True, 'vsize': tx.get_vsize(), 'fees': {'base': (2 * fee)}}],
134134
rawtxs=[raw_tx_0],
135135
)
136136

@@ -190,7 +190,7 @@ def run_test(self):
190190
tx.deserialize(BytesIO(hex_str_to_bytes(raw_tx_reference)))
191191
# Reference tx should be valid on itself
192192
self.check_mempool_result(
193-
result_expected=[{'txid': tx.rehash(), 'allowed': True, 'vsize': tx.get_vsize(), 'fees': { 'base': Decimal(str(0.1 - 0.05))}}],
193+
result_expected=[{'txid': tx.rehash(), 'allowed': True, 'vsize': tx.get_vsize(), 'fees': { 'base': Decimal('0.1') - Decimal('0.05')}}],
194194
rawtxs=[tx.serialize().hex()],
195195
maxfeerate=0,
196196
)

0 commit comments

Comments
 (0)