From 0f150d1e6c1660a93b14a34f6c1f3abd64415787 Mon Sep 17 00:00:00 2001 From: wdl1908 Date: Wed, 13 May 2015 11:54:06 +0200 Subject: [PATCH 1/3] Gather more inputs if the change is below the dust limit --- openassets/transactions.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/openassets/transactions.py b/openassets/transactions.py index ee45efb..71a0e5d 100644 --- a/openassets/transactions.py +++ b/openassets/transactions.py @@ -174,8 +174,7 @@ def asset_asset_swap( return self.transfer( [(asset1_id, asset1_transfer_spec), (asset2_id, asset2_transfer_spec)], btc_transfer_spec, fees) - @staticmethod - def _collect_uncolored_outputs(unspent_outputs, amount): + def _collect_uncolored_outputs(self, unspent_outputs, amount): """ Returns a list of uncolored outputs for the specified amount. @@ -191,10 +190,14 @@ def _collect_uncolored_outputs(unspent_outputs, amount): result.append(output) total_amount += output.output.value - if total_amount >= amount: + # Make sure we don't create dust outputs + if total_amount == amount or total_amount >= amount and (total_amount - amount) >= self._dust_amount: return result, total_amount - raise InsufficientFundsError + if total_amount >= amount and (total_amount - amount) < self._dust_amount: + raise DustOutputError + else: + raise InsufficientFundsError @staticmethod def _collect_colored_outputs(unspent_outputs, asset_id, asset_quantity): From 105fb71d48cae260d9c469bd31ee6dd09e5fe760 Mon Sep 17 00:00:00 2001 From: wdl1908 Date: Wed, 13 May 2015 22:45:29 +0200 Subject: [PATCH 2/3] Make condition more readable --- openassets/transactions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openassets/transactions.py b/openassets/transactions.py index 71a0e5d..85e858d 100644 --- a/openassets/transactions.py +++ b/openassets/transactions.py @@ -191,7 +191,7 @@ def _collect_uncolored_outputs(self, unspent_outputs, amount): total_amount += output.output.value # Make sure we don't create dust outputs - if total_amount == amount or total_amount >= amount and (total_amount - amount) >= self._dust_amount: + if total_amount == amount or (total_amount >= amount and (total_amount - amount) >= self._dust_amount): return result, total_amount if total_amount >= amount and (total_amount - amount) < self._dust_amount: From 46a8826db20a865b8731211c58c4fd7b0878905b Mon Sep 17 00:00:00 2001 From: wdl1908 Date: Wed, 13 May 2015 22:47:47 +0200 Subject: [PATCH 3/3] Add a unit test for gathering more inputs to avoid the dust limit --- tests/test_transactions.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/test_transactions.py b/tests/test_transactions.py index caae478..8b99e28 100644 --- a/tests/test_transactions.py +++ b/tests/test_transactions.py @@ -153,6 +153,17 @@ def test_transfer_bitcoin_dust_change(self): openassets.transactions.DustOutputError, self.target.transfer_bitcoin, spec, 10) + def test_transfer_bitcoin_dust_output_more_inputs(self): + outputs = self.generate_outputs([ + (10, b'source', None, 0), + (11, b'source', None, 0), + (10, b'source', None, 0) + ]) + + spec = openassets.transactions.TransferParameters(outputs, b'target', b'change', 10) + result = self.target.transfer_bitcoin(spec, 10) + self.assertEqual(3, len(result.vin)) + def test_transfer_assets_with_change(self): outputs = self.generate_outputs([ (10, b'source', b'a1', 50),