From dd68fb077b75ce31220b72bd0f4d88d78a008de0 Mon Sep 17 00:00:00 2001 From: Lex Berezhny Date: Wed, 2 Feb 2022 10:57:10 -0500 Subject: [PATCH] prevent creation of change which is below the dust threshold of 1000 dewies --- lbry/wallet/constants.py | 1 + lbry/wallet/transaction.py | 4 ++-- .../transactions/test_transaction_commands.py | 12 ++++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lbry/wallet/constants.py b/lbry/wallet/constants.py index 34b8baa88..775ba4bbc 100644 --- a/lbry/wallet/constants.py +++ b/lbry/wallet/constants.py @@ -2,6 +2,7 @@ NULL_HASH32 = b'\x00'*32 CENT = 1000000 COIN = 100*CENT +DUST = 1000 TIMEOUT = 30.0 diff --git a/lbry/wallet/transaction.py b/lbry/wallet/transaction.py index a6f3bf376..b1a45dda8 100644 --- a/lbry/wallet/transaction.py +++ b/lbry/wallet/transaction.py @@ -14,7 +14,7 @@ from lbry.schema.purchase import Purchase from lbry.schema.support import Support from .script import InputScript, OutputScript -from .constants import COIN, NULL_HASH32 +from .constants import COIN, DUST, NULL_HASH32 from .bcd_data_stream import BCDataStream from .hash import TXRef, TXRefImmutable from .util import ReadOnlyList @@ -818,7 +818,7 @@ class Transaction: ) if payment > cost: change = payment - cost - if change > cost_of_change: + if change > cost_of_change and change > DUST: change_address = await change_account.change.get_or_create_usable_address() change_hash160 = change_account.ledger.address_to_hash160(change_address) change_amount = change - cost_of_change diff --git a/tests/integration/transactions/test_transaction_commands.py b/tests/integration/transactions/test_transaction_commands.py index 66810b30f..609d80e8f 100644 --- a/tests/integration/transactions/test_transaction_commands.py +++ b/tests/integration/transactions/test_transaction_commands.py @@ -3,6 +3,18 @@ from lbry.testcase import CommandTestCase class TransactionCommandsTestCase(CommandTestCase): + async def test_txo_dust_prevention(self): + address = await self.daemon.jsonrpc_address_unused(self.account.id) + tx = await self.account_send('9.9998758', address) + # dust prevention threshold not reached, small txo created + self.assertEqual(2, len(tx['outputs'])) + self.assertEqual(tx['outputs'][1]['amount'], '0.0000002') + tx = await self.account_send('8.9998759', address) + # prior to dust prevention this produced a '0.0000001' change txo + # dust prevention prevented dust + self.assertEqual(1, len(tx['outputs'])) + self.assertEqual(tx['outputs'][0]['amount'], '8.9998759') + async def test_transaction_show(self): # local tx result = await self.out(self.daemon.jsonrpc_account_send(