Skip to content

Commit be744ca

Browse files
authored
[LABS-298] Simplify push_transactions (#20279)
* Simplify `push_transactions` * Further simplify
1 parent 7b3a773 commit be744ca

File tree

2 files changed

+25
-44
lines changed

2 files changed

+25
-44
lines changed

chia/_tests/wallet/rpc/test_wallet_rpc.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,14 +386,24 @@ async def test_push_transactions(wallet_environments: WalletTestFramework) -> No
386386
)
387387
).signed_tx
388388

389+
with pytest.raises(ValueError, match="Cannot add conditions to a transaction if no new fee spend is being added"):
390+
await client.push_transactions(
391+
PushTransactions(transactions=[tx]),
392+
tx_config=wallet_environments.tx_config,
393+
extra_conditions=(Remark(rest=Program.to("foo")),),
394+
)
395+
389396
resp_client = await client.push_transactions(
390397
PushTransactions(transactions=[tx], fee=uint64(10)),
391398
wallet_environments.tx_config,
392399
)
400+
await full_node_api.wait_for_wallet_synced(wallet_node)
393401
resp = await client.fetch("push_transactions", {"transactions": [tx.to_json_dict()], "fee": 10})
394402
assert resp["success"]
403+
await full_node_api.wait_for_wallet_synced(wallet_node)
395404
resp = await client.fetch("push_transactions", {"transactions": [bytes(tx).hex()], "fee": 10})
396405
assert resp["success"]
406+
await full_node_api.wait_for_wallet_synced(wallet_node)
397407

398408
spend_bundle = WalletSpendBundle.aggregate(
399409
[tx.spend_bundle for tx in resp_client.transactions if tx.spend_bundle is not None]

chia/wallet/wallet_rpc_api.py

Lines changed: 15 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from chia.rpc.util import ALL_TRANSLATION_LAYERS, RpcEndpoint, marshal
2424
from chia.server.ws_connection import WSChiaConnection
2525
from chia.types.blockchain_format.coin import coin_as_list
26-
from chia.types.blockchain_format.program import INFINITE_COST, Program, run_with_cost
26+
from chia.types.blockchain_format.program import Program
2727
from chia.types.coin_record import CoinRecord
2828
from chia.types.signing_mode import CHIP_0002_SIGN_MESSAGE_PREFIX, SigningMode
2929
from chia.util.bech32m import decode_puzzle_hash, encode_puzzle_hash
@@ -39,14 +39,14 @@
3939
from chia.wallet.cat_wallet.cat_wallet import CATWallet
4040
from chia.wallet.conditions import (
4141
AssertCoinAnnouncement,
42+
AssertConcurrentSpend,
4243
AssertPuzzleAnnouncement,
4344
Condition,
4445
ConditionValidTimes,
4546
CreateCoin,
4647
CreateCoinAnnouncement,
4748
CreatePuzzleAnnouncement,
4849
conditions_from_json_dicts,
49-
parse_conditions_non_consensus,
5050
parse_timelock_info,
5151
)
5252
from chia.wallet.derive_keys import (
@@ -1007,50 +1007,21 @@ async def push_transactions(
10071007
) -> PushTransactionsResponse:
10081008
if not action_scope.config.push:
10091009
raise ValueError("Cannot push transactions if push is False")
1010+
tx_removals = [c for tx in request.transactions for c in tx.removals]
10101011
async with action_scope.use() as interface:
10111012
interface.side_effects.transactions.extend(request.transactions)
1012-
if request.fee != 0:
1013-
all_conditions_and_origins = [
1014-
(condition, cs.coin.name())
1015-
for tx in interface.side_effects.transactions
1016-
if tx.spend_bundle is not None
1017-
for cs in tx.spend_bundle.coin_spends
1018-
for condition in run_with_cost(cs.puzzle_reveal, INFINITE_COST, cs.solution)[1].as_iter()
1019-
]
1020-
create_coin_announcement = next(
1021-
condition
1022-
for condition in parse_conditions_non_consensus(
1023-
[con for con, coin in all_conditions_and_origins], abstractions=False
1024-
)
1025-
if isinstance(condition, CreateCoinAnnouncement)
1026-
)
1027-
announcement_origin = next(
1028-
coin
1029-
for condition, coin in all_conditions_and_origins
1030-
if condition == create_coin_announcement.to_program()
1031-
)
1032-
async with self.service.wallet_state_manager.new_action_scope(
1033-
dataclasses.replace(
1034-
action_scope.config.tx_config,
1035-
excluded_coin_ids=[
1036-
*action_scope.config.tx_config.excluded_coin_ids,
1037-
*(c.name() for tx in interface.side_effects.transactions for c in tx.removals),
1038-
],
1039-
),
1040-
push=False,
1041-
) as inner_action_scope:
1042-
await self.service.wallet_state_manager.main_wallet.create_tandem_xch_tx(
1043-
request.fee,
1044-
inner_action_scope,
1045-
extra_conditions=(
1046-
*extra_conditions,
1047-
CreateCoinAnnouncement(
1048-
create_coin_announcement.msg, announcement_origin
1049-
).corresponding_assertion(),
1050-
),
1051-
)
1052-
1053-
interface.side_effects.transactions.extend(inner_action_scope.side_effects.transactions)
1013+
interface.side_effects.selected_coins.extend(tx_removals)
1014+
if request.fee != 0:
1015+
await self.service.wallet_state_manager.main_wallet.create_tandem_xch_tx(
1016+
request.fee,
1017+
action_scope,
1018+
extra_conditions=(
1019+
*extra_conditions,
1020+
AssertConcurrentSpend(tx_removals[0].name()),
1021+
),
1022+
)
1023+
elif extra_conditions != tuple():
1024+
raise ValueError("Cannot add conditions to a transaction if no new fee spend is being added")
10541025

10551026
return PushTransactionsResponse([], []) # tx_endpoint takes care of this
10561027

0 commit comments

Comments
 (0)