Skip to content

Commit

Permalink
Add some use of action scope in wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
Quexington committed May 10, 2024
1 parent 86b5664 commit c661e53
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 86 deletions.
8 changes: 3 additions & 5 deletions chia/_tests/pools/test_pool_rpc.py
Expand Up @@ -605,8 +605,8 @@ async def farming_to_pool() -> bool:
assert ret["fee_transaction"] is None
else:
assert ret["fee_transaction"].fee_amount == fee
assert absorb_txs[0].fee_amount == fee
assert absorb_txs[1].fee_amount == fee
for tx in absorb_txs:
assert tx.fee_amount == fee
await full_node_api.process_transaction_records(records=absorb_txs)
main_expected_confirmed_balance -= fee
main_expected_confirmed_balance += block_count * 1_750_000_000_000
Expand Down Expand Up @@ -820,9 +820,7 @@ async def status_is_farming_to_pool() -> bool:
leave_pool_tx: Dict[str, Any] = await client.pw_self_pool(wallet_id, uint64(fee))
assert leave_pool_tx["transaction"].wallet_id == wallet_id
assert leave_pool_tx["transaction"].amount == 1
await full_node_api.wait_transaction_records_entered_mempool(
records=[TransactionRecord.from_json_dict_convenience(tx) for tx in leave_pool_tx["transactions"]]
)
await full_node_api.wait_transaction_records_entered_mempool(records=leave_pool_tx["transactions"])

await full_node_api.farm_blocks_to_puzzlehash(count=1, farm_to=our_ph, guarantee_transaction_blocks=True)

Expand Down
6 changes: 3 additions & 3 deletions chia/_tests/wallet/vc_wallet/test_vc_wallet.py
Expand Up @@ -340,15 +340,15 @@ async def test_vc_lifecycle(wallet_environments: WalletTestFramework) -> None:
assert await wallet_node_0.wallet_state_manager.get_wallet_for_asset_id(cr_cat_wallet_0.get_asset_id()) is not None
wallet_1_ph = await wallet_1.get_new_puzzlehash()
wallet_1_addr = encode_puzzle_hash(wallet_1_ph, "txch")
tx = await client_0.cat_spend(
await client_0.cat_spend(
cr_cat_wallet_0.id(),
wallet_environments.tx_config,
uint64(90),
wallet_1_addr,
uint64(2000000000),
memos=["hey"],
)
[tx] = await wallet_node_0.wallet_state_manager.add_pending_transactions([tx])
txs = await wallet_0.wallet_state_manager.tx_store.get_all_unconfirmed()
await wallet_environments.process_pending_states(
[
WalletStateTransition(
Expand Down Expand Up @@ -409,7 +409,7 @@ async def test_vc_lifecycle(wallet_environments: WalletTestFramework) -> None:
]
)
assert await wallet_node_1.wallet_state_manager.wallets[env_1.dealias_wallet_id("crcat")].match_hinted_coin(
next(c for c in tx.additions if c.amount == 90), wallet_1_ph
next(c for tx in txs for c in tx.additions if c.amount == 90), wallet_1_ph
)
pending_tx = await client_1.get_transactions(
env_1.dealias_wallet_id("crcat"),
Expand Down
46 changes: 21 additions & 25 deletions chia/wallet/cat_wallet/cat_wallet.py
Expand Up @@ -616,9 +616,6 @@ async def create_tandem_xch_tx(
assert message is not None
announcement = AssertCoinAnnouncement(asserted_id=origin_id, asserted_msg=message)

async with action_scope.use() as interface:
interface.side_effects.transactions.append(chia_tx)

return chia_tx, announcement

async def generate_unsigned_spendbundle(
Expand Down Expand Up @@ -804,27 +801,26 @@ async def generate_signed_transaction(
else:
other_tx_removals = set()
other_tx_additions = set()
tx_list = [
TransactionRecord(
confirmed_at_height=uint32(0),
created_at_time=uint64(int(time.time())),
to_puzzle_hash=puzzle_hashes[0],
amount=uint64(payment_sum),
fee_amount=fee,
confirmed=False,
sent=uint32(0),
spend_bundle=spend_bundle,
additions=list(set(spend_bundle.additions()) - other_tx_additions),
removals=list(set(spend_bundle.removals()) - other_tx_removals),
wallet_id=self.id(),
sent_to=[],
trade_id=None,
type=uint32(TransactionType.OUTGOING_TX.value),
name=spend_bundle.name(),
memos=list(compute_memos(spend_bundle).items()),
valid_times=parse_timelock_info(extra_conditions),
)
]
tx = TransactionRecord(
confirmed_at_height=uint32(0),
created_at_time=uint64(int(time.time())),
to_puzzle_hash=puzzle_hashes[0],
amount=uint64(payment_sum),
fee_amount=fee,
confirmed=False,
sent=uint32(0),
spend_bundle=spend_bundle,
additions=list(set(spend_bundle.additions()) - other_tx_additions),
removals=list(set(spend_bundle.removals()) - other_tx_removals),
wallet_id=self.id(),
sent_to=[],
trade_id=None,
type=uint32(TransactionType.OUTGOING_TX.value),
name=spend_bundle.name(),
memos=list(compute_memos(spend_bundle).items()),
valid_times=parse_timelock_info(extra_conditions),
)
tx_list = [tx]

if chia_tx is not None:
tx_list.append(
Expand All @@ -850,7 +846,7 @@ async def generate_signed_transaction(
)

async with action_scope.use() as interface:
interface.side_effects.transactions.extend(tx_list)
interface.side_effects.transactions.append(tx)

return tx_list

Expand Down
84 changes: 32 additions & 52 deletions chia/wallet/vc_wallet/cr_cat_wallet.py
@@ -1,6 +1,5 @@
from __future__ import annotations

import dataclasses
import logging
import time
import traceback
Expand Down Expand Up @@ -596,26 +595,7 @@ async def _generate_unsigned_spendbundle(
else:
vc_txs = []

return (
SpendBundle(
[
*coin_spends,
*(spend for tx in vc_txs if tx.spend_bundle is not None for spend in tx.spend_bundle.coin_spends),
*(
(
spend
for bundle in [chia_tx.spend_bundle]
if bundle is not None
for spend in bundle.coin_spends
)
if chia_tx is not None
else []
),
],
G2Element(),
),
[*vc_txs, *([chia_tx] if chia_tx is not None else [])],
)
return (SpendBundle(coin_spends, G2Element()), [*vc_txs, *([chia_tx] if chia_tx is not None else [])])

async def generate_signed_transaction(
self,
Expand Down Expand Up @@ -692,7 +672,10 @@ async def generate_signed_transaction(
for i, payment in enumerate(payments)
]

return [*tx_list, *(dataclasses.replace(tx, spend_bundle=None) for tx in other_txs)]
async with action_scope.use() as interface:
interface.side_effects.transactions.extend(tx_list)

return [*tx_list, *other_txs]

async def claim_pending_approval_balance(
self,
Expand Down Expand Up @@ -785,7 +768,6 @@ async def claim_pending_approval_balance(
)
if chia_tx.spend_bundle is None:
raise RuntimeError("Did not get spendbundle for fee transaction") # pragma: no cover
claim_bundle = SpendBundle.aggregate([claim_bundle, chia_tx.spend_bundle])
else:
chia_tx = None

Expand All @@ -801,38 +783,36 @@ async def claim_pending_approval_balance(
*(CreatePuzzleAnnouncement(crcat.expected_announcement()) for crcat, _ in crcats_and_puzhashes),
),
)
claim_bundle = SpendBundle.aggregate(
[claim_bundle, *(tx.spend_bundle for tx in vc_txs if tx.spend_bundle is not None)]
)

other_txs: List[TransactionRecord] = [
*(dataclasses.replace(tx, spend_bundle=None) for tx in vc_txs),
*((dataclasses.replace(chia_tx, spend_bundle=None),) if chia_tx is not None else []),
]
other_txs: List[TransactionRecord] = vc_txs
if chia_tx is not None:
other_txs.append(chia_tx)
other_additions: Set[Coin] = {rem for tx in other_txs for rem in tx.additions}
other_removals: Set[Coin] = {rem for tx in other_txs for rem in tx.removals}
return [
TransactionRecord(
confirmed_at_height=uint32(0),
created_at_time=uint64(int(time.time())),
to_puzzle_hash=await self.wallet_state_manager.main_wallet.get_puzzle_hash(False),
amount=uint64(sum(c.amount for c in coins)),
fee_amount=fee,
confirmed=False,
sent=uint32(0),
spend_bundle=claim_bundle,
additions=list(set(claim_bundle.additions()) - other_additions),
removals=list(set(claim_bundle.removals()) - other_removals),
wallet_id=self.id(),
sent_to=[],
trade_id=None,
type=uint32(TransactionType.INCOMING_TX.value),
name=claim_bundle.name(),
memos=list(compute_memos(claim_bundle).items()),
valid_times=parse_timelock_info(extra_conditions),
),
*other_txs,
]
tx = TransactionRecord(
confirmed_at_height=uint32(0),
created_at_time=uint64(int(time.time())),
to_puzzle_hash=await self.wallet_state_manager.main_wallet.get_puzzle_hash(False),
amount=uint64(sum(c.amount for c in coins)),
fee_amount=fee,
confirmed=False,
sent=uint32(0),
spend_bundle=claim_bundle,
additions=list(set(claim_bundle.additions()) - other_additions),
removals=list(set(claim_bundle.removals()) - other_removals),
wallet_id=self.id(),
sent_to=[],
trade_id=None,
type=uint32(TransactionType.INCOMING_TX.value),
name=claim_bundle.name(),
memos=list(compute_memos(claim_bundle).items()),
valid_times=parse_timelock_info(extra_conditions),
)

async with action_scope.use() as interface:
interface.side_effects.transactions.append(tx)

return [tx, *other_txs]

async def match_puzzle_info(self, puzzle_driver: PuzzleInfo) -> bool:
if (
Expand Down
7 changes: 6 additions & 1 deletion chia/wallet/vc_wallet/vc_wallet.py
Expand Up @@ -411,7 +411,12 @@ async def revoke_vc(
)
assert did_tx.spend_bundle is not None
final_bundle: SpendBundle = SpendBundle.aggregate([SpendBundle([vc_spend], G2Element()), did_tx.spend_bundle])
did_tx = dataclasses.replace(did_tx, spend_bundle=final_bundle, name=final_bundle.name())
async with action_scope.use() as interface:
# This should not be looked to for best practice. Ideally, the method to generate the transaction above
# takes a parameter to add in extra spends. That's currently out of scope, so I'm placing this hack in rn.
relevant_index = interface.side_effects.transactions.index(did_tx)
did_tx = dataclasses.replace(did_tx, spend_bundle=final_bundle, name=final_bundle.name())
interface.side_effects.transactions[relevant_index] = did_tx
if fee > 0:
chia_tx: TransactionRecord = await self.wallet_state_manager.main_wallet.create_tandem_xch_tx(
fee, tx_config, action_scope, extra_conditions=(vc_announcement,)
Expand Down

0 comments on commit c661e53

Please sign in to comment.