mirror of
https://github.com/Chia-Network/chia-blockchain.git
synced 2026-08-24 10:05:29 -05:00
simplify and de duplicate code
This commit is contained in:
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user