diff --git a/chia/wallet/coin_selection.py b/chia/wallet/coin_selection.py index d26e1f5cd7..d9b5696040 100644 --- a/chia/wallet/coin_selection.py +++ b/chia/wallet/coin_selection.py @@ -73,28 +73,28 @@ async def select_coins( if coin.amount < amount: smaller_coin_sum += coin.amount smaller_coins.append(coin) - if smaller_coin_sum == amount: - if len(smaller_coins) < 500: - log.debug( - f"Selected all smaller coins because they equate to an exact match of the target.: {smaller_coins}" - ) - return set(smaller_coins) - else: - smallest_coin = select_smallest_coin_over_target(smaller_coins, valid_spendable_coins) - assert smallest_coin is not None - log.debug(f"Selected closest greater coin due to the large amount of coins. {smallest_coin.name()}") - return {smallest_coin} - + if smaller_coin_sum == amount and len(smaller_coins) < 500: + log.debug(f"Selected all smaller coins because they equate to an exact match of the target.: {smaller_coins}") + return set(smaller_coins) elif smaller_coin_sum < amount: smallest_coin = select_smallest_coin_over_target(smaller_coins, valid_spendable_coins) assert smallest_coin is not None log.debug(f"Selected closest greater coin: {smallest_coin.name()}") return {smallest_coin} else: - knapsack_coin_set = knapsack_coin_algorithm(smaller_coins, amount, max_coin_amount) - assert knapsack_coin_set is not None - log.debug(f"Selected coins from knapsack algorithm: {knapsack_coin_set}") - return knapsack_coin_set + coin_set = knapsack_coin_algorithm(smaller_coins, amount, max_coin_amount) + log.debug(f"Selected coins from knapsack algorithm: {coin_set}") + if coin_set is None or len(coin_set) > 500: # lots of dust + coin = select_smallest_coin_over_target(smaller_coins, valid_spendable_coins) + if coin is None or coin.amount < amount: + raise ValueError( + "Can't make this transaction because the transaction would use over 500 " + "coins which is unlikely to get confirmed. Try making a smaller transaction to condense the dust." + ) + else: + coin_set = {coin} + log.debug(f"Resorted to selecting smallest coin over target due to dust.: {coin_set}") + return coin_set # These algorithms were based off of the algorithms in: