Skip to content

Commit

Permalink
add vault side effects to action scope
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffwalmsley committed Jul 29, 2024
1 parent 9e01679 commit b6c038c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
21 changes: 12 additions & 9 deletions chia/wallet/vault/vault_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,18 @@ async def _generate_unsigned_transaction(
delegated_solution = solution_for_conditions(conditions)

p2_singleton_spends: List[CoinSpend] = []
for coin in coins:
if not p2_singleton_spends:
p2_solution = Program.to(
[self.vault_info.inner_puzzle_hash, delegated_puzzle, delegated_solution, coin.name()]
)
else:
p2_solution = Program.to([self.vault_info.inner_puzzle_hash, 0, 0, coin.name()])

p2_singleton_spends.append(make_spend(coin, p2_singleton_puzzle, p2_solution))
async with action_scope.use() as interface:
for coin in coins:
if not p2_singleton_spends:
p2_solution = Program.to(
[self.vault_info.inner_puzzle_hash, delegated_puzzle, delegated_solution, coin.name()]
)
else:
p2_solution = Program.to([self.vault_info.inner_puzzle_hash, 0, 0, coin.name()])
interface.side_effects.solutions.append(p2_solution)
interface.side_effects.coin_ids.append(coin.name())

p2_singleton_spends.append(make_spend(coin, p2_singleton_puzzle, p2_solution))

next_puzzle_hash = (
self.vault_info.coin.puzzle_hash if tx_config.reuse_puzhash else (await self.get_new_vault_puzzlehash())
Expand Down
26 changes: 26 additions & 0 deletions chia/wallet/wallet_action_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from dataclasses import dataclass, field
from typing import TYPE_CHECKING, AsyncIterator, List, Optional, cast

from chia.types.blockchain_format.program import Program
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.types.spend_bundle import SpendBundle
from chia.util.action_scope import ActionScope
from chia.wallet.signer_protocol import SigningResponse
Expand All @@ -19,6 +21,8 @@ class WalletSideEffects:
transactions: List[TransactionRecord] = field(default_factory=list)
signing_responses: List[SigningResponse] = field(default_factory=list)
extra_spends: List[SpendBundle] = field(default_factory=list)
solutions: List[Program] = field(default_factory=list)
coin_ids: List[bytes32] = field(default_factory=list)

def __bytes__(self) -> bytes:
blob = b""
Expand All @@ -34,6 +38,13 @@ def __bytes__(self) -> bytes:
for sb in self.extra_spends:
sb_bytes = bytes(sb)
blob += len(sb_bytes).to_bytes(4, "big") + sb_bytes
blob += len(self.solutions).to_bytes(4, "big")
for sol in self.solutions:
sol_bytes = bytes(sol)
blob += len(sol_bytes).to_bytes(4, "big") + sol_bytes
blob += len(self.coin_ids).to_bytes(4, "big")
for coin_id in self.coin_ids:
blob += len(coin_id).to_bytes(4, "big") + bytes(coin_id)
return blob

@classmethod
Expand Down Expand Up @@ -61,6 +72,21 @@ def from_bytes(cls, blob: bytes) -> WalletSideEffects:
blob = blob[4:]
instance.extra_spends.append(SpendBundle.from_bytes(blob[:len_prefix]))
blob = blob[len_prefix:]
sol_len_prefix = int.from_bytes(blob[:4], "big")
blob = blob[4:]
for _ in range(0, sol_len_prefix):
len_prefix = int.from_bytes(blob[:4], "big")
blob = blob[4:]
instance.solutions.append(Program.from_bytes(blob[:len_prefix]))
blob = blob[len_prefix:]
coin_id_len_prefix = int.from_bytes(blob[:4], "big")
blob = blob[4:]
for _ in range(0, coin_id_len_prefix):
len_prefix = int.from_bytes(blob[:4], "big")
blob = blob[4:]
coin_id_bytes = blob[:len_prefix]
blob = blob[len_prefix:]
instance.coin_ids.append(bytes32(coin_id_bytes))

return instance

Expand Down

0 comments on commit b6c038c

Please sign in to comment.