Skip to content

Commit

Permalink
More test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Quexington committed Jan 3, 2025
1 parent 36dc78a commit d54a1b8
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 71 deletions.
138 changes: 71 additions & 67 deletions chia/data_layer/data_layer_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -1031,79 +1031,83 @@ async def make_update_offer(
if dl_wallet is None:
raise ValueError("DL Wallet is not initialized")

offered_launchers: list[bytes32] = [k for k, v in offer_dict.items() if v < 0 and k is not None]
all_transactions: list[TransactionRecord] = []
for launcher in offered_launchers:
try:
this_solver: Solver = solver[launcher.hex()]
except KeyError:
this_solver = solver["0x" + launcher.hex()]
new_root: bytes32 = this_solver["new_root"]
new_ph: bytes32 = await wallet_state_manager.main_wallet.get_puzzle_hash(
new=not action_scope.config.tx_config.reuse_puzhash
)
async with wallet_state_manager.new_action_scope(
action_scope.config.tx_config, push=False
) as inner_action_scope:
await dl_wallet.generate_signed_transaction(
[uint64(1)],
[new_ph],
inner_action_scope,
launcher_id=launcher,
new_root_hash=new_root,
add_pending_singleton=False,
announce_new_state=True,
extra_conditions=extra_conditions,
)
extra_conditions = tuple()

async with inner_action_scope.use() as interface:
dl_spend: CoinSpend = next(
cs
for tx in interface.side_effects.transactions
for cs in tx.spend_bundle.coin_spends
if tx.spend_bundle is not None and match_dl_singleton(cs.puzzle_reveal)[0]
)
dl_solution: Program = dl_spend.solution.to_program()
old_graftroot: Program = dl_solution.at("rrffrf")
new_graftroot: Program = create_graftroot_offer_puz(
[bytes32(dep["launcher_id"]) for dep in this_solver["dependencies"]],
[list(v for v in dep["values_to_prove"]) for dep in this_solver["dependencies"]],
old_graftroot,
async with action_scope.use() as interface:
offered_launchers: list[bytes32] = [k for k, v in offer_dict.items() if v < 0 and k is not None]
all_transactions: list[TransactionRecord] = []
for launcher in offered_launchers:
try:
this_solver: Solver = solver[launcher.hex()]
except KeyError:
this_solver = solver["0x" + launcher.hex()]
new_root: bytes32 = this_solver["new_root"]
new_ph: bytes32 = await wallet_state_manager.main_wallet.get_puzzle_hash(
new=not action_scope.config.tx_config.reuse_puzhash
)

new_solution: Program = dl_solution.replace(rrffrf=new_graftroot, rrffrrf=Program.to([None] * 5))
new_spend: CoinSpend = dl_spend.replace(solution=SerializedProgram.from_program(new_solution))
async with inner_action_scope.use() as interface:
for i, tx in enumerate(interface.side_effects.transactions):
if tx.spend_bundle is not None and dl_spend in tx.spend_bundle.coin_spends:
break
else:
# No test coverage for this line because it should never be reached
raise RuntimeError("Internal logic error while constructing update offer") # pragma: no cover
new_bundle = WalletSpendBundle(
[
*(
cs
for cs in interface.side_effects.transactions[i].spend_bundle.coin_spends
if cs != dl_spend
),
new_spend,
],
G2Element(),
async with wallet_state_manager.new_action_scope(
action_scope.config.tx_config, push=False, fee=interface.side_effects.fee_left_to_pay
) as inner_action_scope:
await dl_wallet.generate_signed_transaction(
[uint64(1)],
[new_ph],
inner_action_scope,
launcher_id=launcher,
new_root_hash=new_root,
add_pending_singleton=False,
announce_new_state=True,
extra_conditions=extra_conditions,
)
interface.side_effects.transactions[i] = dataclasses.replace(
interface.side_effects.transactions[i], spend_bundle=new_bundle, name=new_bundle.name()
extra_conditions = tuple()

async with inner_action_scope.use() as interface:
dl_spend: CoinSpend = next(
cs
for tx in interface.side_effects.transactions
if tx.spend_bundle is not None
for cs in tx.spend_bundle.coin_spends
if match_dl_singleton(cs.puzzle_reveal)[0]
)
dl_solution: Program = dl_spend.solution.to_program()
old_graftroot: Program = dl_solution.at("rrffrf")
new_graftroot: Program = create_graftroot_offer_puz(
[bytes32(dep["launcher_id"]) for dep in this_solver["dependencies"]],
[list(v for v in dep["values_to_prove"]) for dep in this_solver["dependencies"]],
old_graftroot,
)

all_transactions.extend(inner_action_scope.side_effects.transactions)
new_solution: Program = dl_solution.replace(rrffrf=new_graftroot, rrffrrf=Program.to([None] * 5))
new_spend: CoinSpend = dl_spend.replace(solution=SerializedProgram.from_program(new_solution))
async with inner_action_scope.use() as interface:
for i, tx in enumerate(interface.side_effects.transactions):
if tx.spend_bundle is not None and dl_spend in tx.spend_bundle.coin_spends:
break
else:
# No test coverage for this line because it should never be reached
raise RuntimeError(
"Internal logic error while constructing update offer"
) # pragma: no cover
dl_tx = interface.side_effects.transactions[i].spend_bundle
assert dl_tx is not None
new_bundle = WalletSpendBundle(
[
*(cs for cs in dl_tx.coin_spends if cs != dl_spend),
new_spend,
],
G2Element(),
)
interface.side_effects.transactions[i] = dataclasses.replace(
interface.side_effects.transactions[i], spend_bundle=new_bundle, name=new_bundle.name()
)

# create some dummy requested payments
requested_payments = {
k: [NotarizedPayment(bytes32.zeros, uint64(v), [], bytes32.zeros)] for k, v in offer_dict.items() if v > 0
}
all_transactions.extend(inner_action_scope.side_effects.transactions)
interface.side_effects.fee_left_to_pay = inner_action_scope.side_effects.fee_left_to_pay

# create some dummy requested payments
requested_payments = {
k: [NotarizedPayment(bytes32.zeros, uint64(v), [], bytes32.zeros)]
for k, v in offer_dict.items()
if v > 0
}

async with action_scope.use() as interface:
interface.side_effects.transactions.extend(all_transactions)

return Offer(
Expand Down
10 changes: 6 additions & 4 deletions chia/wallet/nft_wallet/nft_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,11 @@ async def make_nft1_offer(

# First, sending all the coins to the OFFER_MOD
async with wallet_state_manager.new_action_scope(
action_scope.config.tx_config, push=False
action_scope.config.tx_config,
push=False,
fee=interface.side_effects.fee_left_to_pay
if wallet.type() == WalletType.STANDARD_WALLET
else fee_left_to_pay,
) as inner_action_scope:
if wallet.type() == WalletType.STANDARD_WALLET:
payments = royalty_payments[asset] if asset in royalty_payments else []
Expand All @@ -905,7 +909,6 @@ async def make_nft1_offer(
OFFER_MOD_HASH,
inner_action_scope,
primaries=[Payment(OFFER_MOD_HASH, uint64(payment_sum))] if payment_sum > 0 else [],
fee=interface.side_effects.fee_left_to_pay,
coins=offered_coins_by_asset[asset],
extra_conditions=(*extra_conditions, *announcements_to_assert),
)
Expand All @@ -915,7 +918,6 @@ async def make_nft1_offer(
[abs(amount)],
[OFFER_MOD_HASH],
inner_action_scope,
fee=fee_left_to_pay,
coins=offered_coins_by_asset[asset],
trade_prices_list=[
list(price)
Expand All @@ -930,7 +932,6 @@ async def make_nft1_offer(
[abs(amount), sum(p.amount for _, p in payments)],
[OFFER_MOD_HASH, OFFER_MOD_HASH],
inner_action_scope,
fee=fee_left_to_pay,
coins=offered_coins_by_asset[asset],
extra_conditions=(*extra_conditions, *announcements_to_assert),
)
Expand Down Expand Up @@ -1049,6 +1050,7 @@ async def make_nft1_offer(
offer = Offer(notarized_payments, aggregate_bundle, driver_dict)

interface.side_effects.transactions.extend(all_transactions)
interface.side_effects.fee_left_to_pay = uint64(0)

return offer

Expand Down
4 changes: 4 additions & 0 deletions chia/wallet/trade_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ async def cancel_pending_offers(
excluded_coin_ids=[],
),
push=False,
fee=interface.side_effects.fee_left_to_pay,
) as inner_action_scope:
await wallet.generate_signed_transaction(
uint64(sum(c.amount for c in selected_coins) - interface.side_effects.fee_left_to_pay),
Expand All @@ -345,6 +346,7 @@ async def cancel_pending_offers(
excluded_coin_ids=[],
),
push=False,
fee=interface.side_effects.fee_left_to_pay,
) as inner_action_scope:
await wallet.generate_signed_transaction(
[coin.amount],
Expand All @@ -354,6 +356,8 @@ async def cancel_pending_offers(
extra_conditions=(*extra_conditions, *announcement_conditions),
)

interface.side_effects.fee_left_to_pay = inner_action_scope.side_effects.fee_left_to_pay

cancellation_additions.extend(
[
add
Expand Down

0 comments on commit d54a1b8

Please sign in to comment.