From 96d7817fddc3989a2db7db64dd146f62daa99b73 Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 21 Jan 2025 07:42:51 -0800 Subject: [PATCH] Allow coin selection of 0 value coins --- chia/_tests/wallet/test_coin_selection.py | 18 ++++++++++++++++++ chia/wallet/coin_selection.py | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/chia/_tests/wallet/test_coin_selection.py b/chia/_tests/wallet/test_coin_selection.py index ce8f05e71277..d874e137896a 100644 --- a/chia/_tests/wallet/test_coin_selection.py +++ b/chia/_tests/wallet/test_coin_selection.py @@ -97,6 +97,24 @@ async def test_coin_selection_randomly(self, a_hash: bytes32) -> None: assert sum(coin.amount for coin in result) >= target_amount assert len(result) <= 500 + @pytest.mark.anyio + async def test_coin_selection_zero_coins(self, a_hash: bytes32) -> None: + coin_list: list[WalletCoinRecord] = [ + WalletCoinRecord(Coin(a_hash, a_hash, uint64(0)), uint32(1), uint32(1), False, True, WalletType(0), 1) + for _ in range(0, 100) + ] + + result: set[Coin] = await select_coins( + uint128(0), + DEFAULT_COIN_SELECTION_CONFIG, + coin_list, + {}, + logging.getLogger("test"), + uint128(0), + ) + + assert len(result) > 0 + @pytest.mark.anyio async def test_coin_selection_with_dust(self, a_hash: bytes32) -> None: spendable_amount = uint128(5000000000000 + 10000) diff --git a/chia/wallet/coin_selection.py b/chia/wallet/coin_selection.py index 8d83ee36057a..1b884b694d57 100644 --- a/chia/wallet/coin_selection.py +++ b/chia/wallet/coin_selection.py @@ -58,7 +58,7 @@ async def select_coins( f"Transaction for {amount} is greater than max spendable balance in a block of {sum_spendable_coins}. " "There may be other transactions pending or our minimum coin amount is too high." ) - if amount == 0 and sum_spendable_coins == 0: + if amount == 0 and len(spendable_coins) == 0: raise ValueError( "No coins available to spend, you can not create a coin with an amount of 0," " without already having coins."