Merge pull request #3556 from lbryio/txo_dust_prevention

prevent creation of change which is below the dust threshold of 1000 dewies
This commit is contained in:
Lex Berezhny 2022-02-04 12:08:16 -05:00 committed by GitHub
commit bcec5dc2ae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 3 deletions

View file

@ -2,6 +2,7 @@ NULL_HASH32 = b'\x00'*32
CENT = 1000000 CENT = 1000000
COIN = 100*CENT COIN = 100*CENT
DUST = 1000
TIMEOUT = 30.0 TIMEOUT = 30.0

View file

@ -14,7 +14,7 @@ from lbry.schema.purchase import Purchase
from lbry.schema.support import Support from lbry.schema.support import Support
from .script import InputScript, OutputScript 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 .bcd_data_stream import BCDataStream
from .hash import TXRef, TXRefImmutable from .hash import TXRef, TXRefImmutable
from .util import ReadOnlyList from .util import ReadOnlyList
@ -818,10 +818,10 @@ class Transaction:
) )
if payment > cost: if payment > cost:
change = payment - cost change = payment - cost
if change > cost_of_change: change_amount = change - cost_of_change
if change_amount > DUST:
change_address = await change_account.change.get_or_create_usable_address() change_address = await change_account.change.get_or_create_usable_address()
change_hash160 = change_account.ledger.address_to_hash160(change_address) change_hash160 = change_account.ledger.address_to_hash160(change_address)
change_amount = change - cost_of_change
change_output = Output.pay_pubkey_hash(change_amount, change_hash160) change_output = Output.pay_pubkey_hash(change_amount, change_hash160)
change_output.is_internal_transfer = True change_output.is_internal_transfer = True
tx.add_outputs([Output.pay_pubkey_hash(change_amount, change_hash160)]) tx.add_outputs([Output.pay_pubkey_hash(change_amount, change_hash160)])

View file

@ -3,6 +3,17 @@ from lbry.testcase import CommandTestCase
class TransactionCommandsTestCase(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.9997758', address)
# dust prevention threshold not reached, small txo created
self.assertEqual(2, len(tx['outputs']))
self.assertEqual(tx['outputs'][1]['amount'], '0.0001002')
tx = await self.account_send('9.999706', address)
# dust prevention prevented dust
self.assertEqual(1, len(tx['outputs']))
self.assertEqual(tx['outputs'][0]['amount'], '9.999706')
async def test_transaction_show(self): async def test_transaction_show(self):
# local tx # local tx
result = await self.out(self.daemon.jsonrpc_account_send( result = await self.out(self.daemon.jsonrpc_account_send(