Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions node/test_utxo_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,33 @@ def test_transfer_with_fee(self):
# 100 - 90 - 1 fee = 9 change
self.assertEqual(data['change_rtc'], 9.0)

def test_transfer_float_precision(self):
"""0.1 RTC must convert to exactly 10_000_000 nanoRTC.

Without Decimal: int(0.1 * 100_000_000) = 9_999_999 (truncation)
With Decimal: int(Decimal('0.1') * 100_000_000) = 10_000_000
(bounty #2819 MED-3)
"""
self._seed_coinbase('RTC_test_aabbccdd', 100 * UNIT)

r = self.client.post('/utxo/transfer', json={
'from_address': 'RTC_test_aabbccdd',
'to_address': 'bob',
'amount_rtc': 0.1,
'public_key': 'aabbccdd' * 8,
'signature': 'sig' * 22,
'nonce': int(time.time() * 1000),
})
data = r.get_json()
self.assertEqual(r.status_code, 200)
self.assertTrue(data['ok'])

# Bob must have exactly 0.1 RTC = 10_000_000 nanoRTC
bob_bal = self.utxo_db.get_balance('bob')
self.assertEqual(bob_bal, 10_000_000,
f"Expected 10_000_000 nanoRTC, got {bob_bal} "
f"(float truncation bug)")


if __name__ == '__main__':
unittest.main()
9 changes: 7 additions & 2 deletions node/utxo_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import json
import sqlite3
import time
from decimal import Decimal, ROUND_DOWN

from flask import Blueprint, request, jsonify

Expand Down Expand Up @@ -273,8 +274,12 @@ def utxo_transfer():

# --- UTXO transaction ---------------------------------------------------

amount_nrtc = int(amount_rtc * UNIT)
fee_nrtc = int(fee_rtc * UNIT)
# Use Decimal for exact RTC → nanoRTC conversion.
# float multiplication truncates: int(0.1 * 100_000_000) = 9_999_999
# instead of the correct 10_000_000. Over thousands of transactions
# the cumulative error is non-trivial.
amount_nrtc = int(Decimal(str(amount_rtc)) * UNIT)
fee_nrtc = int(Decimal(str(fee_rtc)) * UNIT)
target_nrtc = amount_nrtc + fee_nrtc

# Select UTXOs
Expand Down
Loading