From 6c8c2e4874a4a5f68046a6a1d7b6b8b0c6ce961c Mon Sep 17 00:00:00 2001 From: Jack Nelson Date: Thu, 31 Mar 2022 15:06:00 -0400 Subject: [PATCH] remove duplicate code. --- chia/wallet/coin_selection.py | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/chia/wallet/coin_selection.py b/chia/wallet/coin_selection.py index 38d552b304db..fe23c416a555 100644 --- a/chia/wallet/coin_selection.py +++ b/chia/wallet/coin_selection.py @@ -78,8 +78,7 @@ async def select_coins( 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 + smallest_coin = select_smallest_coin_over_target(smaller_coins, valid_spendable_coins, amount) log.debug(f"Selected closest greater coin: {smallest_coin.name()}") return {smallest_coin} elif smaller_coin_sum > amount: @@ -88,21 +87,11 @@ async def select_coins( if coin_set is None: raise ValueError("Knapsack algorithm failed to find a solution.") if len(coin_set) > max_num_coins: - 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." - ) + coin = select_smallest_coin_over_target(smaller_coins, valid_spendable_coins, amount) coin_set = {coin} return coin_set else: - 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." - ) + coin = select_smallest_coin_over_target(smaller_coins, valid_spendable_coins, amount) log.debug(f"Resorted to selecting smallest coin over target due to dust.: {coin}") return {coin} @@ -118,12 +107,20 @@ def check_for_exact_match(coin_list: List[Coin], target: uint64) -> Optional[Coi return None -def select_smallest_coin_over_target(smaller_coin_list: List[Coin], valid_spendable_coin_list: List[Coin]) -> Coin: +def select_smallest_coin_over_target( + smaller_coin_list: List[Coin], valid_spendable_coin_list: List[Coin], target_amount: uint128 +) -> Coin: if len(smaller_coin_list) > 0: # in case we only have bigger coins. greater_coins = valid_spendable_coin_list[: -len(smaller_coin_list)] else: greater_coins = valid_spendable_coin_list - return greater_coins[len(greater_coins) - 1] # select the coin with the least value. + coin = greater_coins[len(greater_coins) - 1] # select the coin with the least value. + if coin is None or coin.amount < target_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." + ) + return coin # we use this to find the set of coins which have total value closest to the target, but at least the target.