Skip to content

Commit

Permalink
Extend coins without building lists on each step in combine_coins.
Browse files Browse the repository at this point in the history
  • Loading branch information
AmineKhaldi committed Sep 23, 2024
1 parent 111b0e5 commit 8518654
Showing 1 changed file with 23 additions and 30 deletions.
53 changes: 23 additions & 30 deletions chia/rpc/wallet_rpc_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1177,20 +1177,18 @@ async def combine_coins(
if not isinstance(wallet, (Wallet, CATWallet)):
raise ValueError("Cannot combine coins from non-fungible wallet types")

coins = []
coins: List[Coin] = []

# First get the coin IDs specified
if request.target_coin_ids != []:
coins.extend(
[
cr.coin
for cr in (
await self.service.wallet_state_manager.coin_store.get_coin_records(
wallet_id=request.wallet_id,
coin_id_filter=HashFilter(request.target_coin_ids, mode=uint8(FilterMode.include.value)),
)
).records
]
cr.coin
for cr in (
await self.service.wallet_state_manager.coin_store.get_coin_records(
wallet_id=request.wallet_id,
coin_id_filter=HashFilter(request.target_coin_ids, mode=uint8(FilterMode.include.value)),
)
).records
)

async with action_scope.use() as interface:
Expand All @@ -1204,33 +1202,28 @@ async def combine_coins(
amount_selected = sum(c.amount for c in coins)
if amount_selected < fungible_amount_needed:
coins.extend(
list(
await wallet.select_coins(
amount=uint64(fungible_amount_needed - amount_selected),
action_scope=action_scope,
)
await wallet.select_coins(
amount=uint64(fungible_amount_needed - amount_selected), action_scope=action_scope
)
)

# Now let's select enough coins to get to the target number to combine
if len(coins) < request.number_of_coins:
async with action_scope.use() as interface:
coins.extend(
[
cr.coin
for cr in (
await self.service.wallet_state_manager.coin_store.get_coin_records(
wallet_id=request.wallet_id,
limit=uint32(request.number_of_coins - len(coins)),
order=CoinRecordOrder.amount,
coin_id_filter=HashFilter(
[c.name() for c in interface.side_effects.selected_coins],
mode=uint8(FilterMode.exclude.value),
),
reverse=request.largest_first,
)
).records
]
cr.coin
for cr in (
await self.service.wallet_state_manager.coin_store.get_coin_records(
wallet_id=request.wallet_id,
limit=uint32(request.number_of_coins - len(coins)),
order=CoinRecordOrder.amount,
coin_id_filter=HashFilter(
[c.name() for c in interface.side_effects.selected_coins],
mode=uint8(FilterMode.exclude.value),
),
reverse=request.largest_first,
)
).records
)

async with action_scope.use() as interface:
Expand Down

0 comments on commit 8518654

Please sign in to comment.