Skip to content

Commit

Permalink
[CHIA-713] Add action_scope: WalletActionScope to all tx endpoints (#…
Browse files Browse the repository at this point in the history
…18127)

* Add the concept of 'action scopes'

* Add `WalletActionScope`

* Fix CATWallet pending_change calculation

* Add action_scope: WalletActionScope to all tx endpoints

* Add the concept of 'action scopes'

* pylint and test coverage

* add try/finally

* add try/except

* Undo giving a variable a name

* Fix CRCAT test

* Fix trade tests

* Fix cat test

* Fix miscellaneous test

* Fix miscellaneous test

* Fix miscellaneous test

* Add auto claim test coverage
  • Loading branch information
Quexington authored Jul 16, 2024
1 parent 0ef148e commit 4ffb6df
Show file tree
Hide file tree
Showing 45 changed files with 2,340 additions and 1,377 deletions.
96 changes: 56 additions & 40 deletions chia/_tests/core/full_node/test_full_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,13 @@ async def test_block_compression(self, setup_two_nodes_and_wallet, empty_blockch
await full_node_1.wait_for_wallet_synced(wallet_node=wallet_node_1, timeout=30)

# Send a transaction to mempool
[tr] = await wallet.generate_signed_transaction(
tx_size,
ph,
DEFAULT_TX_CONFIG,
)
async with wallet.wallet_state_manager.new_action_scope(push=False) as action_scope:
[tr] = await wallet.generate_signed_transaction(
tx_size,
ph,
DEFAULT_TX_CONFIG,
action_scope,
)
[tr] = await wallet.wallet_state_manager.add_pending_transactions([tr])
await time_out_assert(
10,
Expand Down Expand Up @@ -217,11 +219,13 @@ async def check_transaction_confirmed(transaction) -> bool:
assert len((await full_node_1.get_all_full_blocks())[-1].transactions_generator_ref_list) == 0

# Send another tx
[tr] = await wallet.generate_signed_transaction(
20000,
ph,
DEFAULT_TX_CONFIG,
)
async with wallet.wallet_state_manager.new_action_scope(push=False) as action_scope:
[tr] = await wallet.generate_signed_transaction(
20000,
ph,
DEFAULT_TX_CONFIG,
action_scope,
)
[tr] = await wallet.wallet_state_manager.add_pending_transactions([tr])
await time_out_assert(
10,
Expand Down Expand Up @@ -256,23 +260,27 @@ async def check_transaction_confirmed(transaction) -> bool:
await full_node_1.wait_for_wallet_synced(wallet_node=wallet_node_1, timeout=30)

# Send another 2 tx
[tr] = await wallet.generate_signed_transaction(
30000,
ph,
DEFAULT_TX_CONFIG,
)
async with wallet.wallet_state_manager.new_action_scope(push=False) as action_scope:
[tr] = await wallet.generate_signed_transaction(
30000,
ph,
DEFAULT_TX_CONFIG,
action_scope,
)
[tr] = await wallet.wallet_state_manager.add_pending_transactions([tr])
await time_out_assert(
10,
full_node_2.full_node.mempool_manager.get_spendbundle,
tr.spend_bundle,
tr.name,
)
[tr] = await wallet.generate_signed_transaction(
40000,
ph,
DEFAULT_TX_CONFIG,
)
async with wallet.wallet_state_manager.new_action_scope(push=False) as action_scope:
[tr] = await wallet.generate_signed_transaction(
40000,
ph,
DEFAULT_TX_CONFIG,
action_scope,
)
[tr] = await wallet.wallet_state_manager.add_pending_transactions([tr])
await time_out_assert(
10,
Expand All @@ -281,11 +289,13 @@ async def check_transaction_confirmed(transaction) -> bool:
tr.name,
)

[tr] = await wallet.generate_signed_transaction(
50000,
ph,
DEFAULT_TX_CONFIG,
)
async with wallet.wallet_state_manager.new_action_scope(push=False) as action_scope:
[tr] = await wallet.generate_signed_transaction(
50000,
ph,
DEFAULT_TX_CONFIG,
action_scope,
)
[tr] = await wallet.wallet_state_manager.add_pending_transactions([tr])
await time_out_assert(
10,
Expand All @@ -294,11 +304,13 @@ async def check_transaction_confirmed(transaction) -> bool:
tr.name,
)

[tr] = await wallet.generate_signed_transaction(
3000000000000,
ph,
DEFAULT_TX_CONFIG,
)
async with wallet.wallet_state_manager.new_action_scope(push=False) as action_scope:
[tr] = await wallet.generate_signed_transaction(
3000000000000,
ph,
DEFAULT_TX_CONFIG,
action_scope,
)
[tr] = await wallet.wallet_state_manager.add_pending_transactions([tr])
await time_out_assert(
10,
Expand All @@ -325,11 +337,13 @@ async def check_transaction_confirmed(transaction) -> bool:
assert num_blocks == 0

# Creates a standard_transaction and an anyone-can-spend tx
[tr] = await wallet.generate_signed_transaction(
30000,
Program.to(1).get_tree_hash(),
DEFAULT_TX_CONFIG,
)
async with wallet.wallet_state_manager.new_action_scope(push=False) as action_scope:
[tr] = await wallet.generate_signed_transaction(
30000,
Program.to(1).get_tree_hash(),
DEFAULT_TX_CONFIG,
action_scope,
)
extra_spend = SpendBundle(
[
make_spend(
Expand Down Expand Up @@ -371,11 +385,13 @@ async def check_transaction_confirmed(transaction) -> bool:
assert len(all_blocks[-1].transactions_generator_ref_list) == 0

# Make a standard transaction and an anyone-can-spend transaction
[tr] = await wallet.generate_signed_transaction(
30000,
Program.to(1).get_tree_hash(),
DEFAULT_TX_CONFIG,
)
async with wallet.wallet_state_manager.new_action_scope(push=False) as action_scope:
[tr] = await wallet.generate_signed_transaction(
30000,
Program.to(1).get_tree_hash(),
DEFAULT_TX_CONFIG,
action_scope,
)
extra_spend = SpendBundle(
[
make_spend(
Expand Down
14 changes: 8 additions & 6 deletions chia/_tests/core/full_node/test_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ async def peak_height(fna: FullNodeAPI):
await time_out_assert(20, peak_height, num_blocks, full_node_api_1)
await time_out_assert(20, peak_height, num_blocks, full_node_api_2)

[tx] = await wallet_0.wallet_state_manager.main_wallet.generate_signed_transaction(
10, ph1, DEFAULT_TX_CONFIG, 0
)
async with wallet_0.wallet_state_manager.new_action_scope(push=False) as action_scope:
[tx] = await wallet_0.wallet_state_manager.main_wallet.generate_signed_transaction(
10, ph1, DEFAULT_TX_CONFIG, action_scope, 0
)
[tx] = await wallet_0.wallet_state_manager.add_pending_transactions([tx])

await time_out_assert(
Expand Down Expand Up @@ -154,9 +155,10 @@ async def test_mempool_tx_sync(self, three_nodes_two_wallets, self_hostname, see
)
await time_out_assert(20, wallet_0.wallet_state_manager.main_wallet.get_confirmed_balance, funds)

[tx] = await wallet_0.wallet_state_manager.main_wallet.generate_signed_transaction(
10, bytes32.random(seeded_random), DEFAULT_TX_CONFIG, 0
)
async with wallet_0.wallet_state_manager.new_action_scope(push=False) as action_scope:
[tx] = await wallet_0.wallet_state_manager.main_wallet.generate_signed_transaction(
10, bytes32.random(seeded_random), DEFAULT_TX_CONFIG, action_scope, 0
)
[tx] = await wallet_0.wallet_state_manager.add_pending_transactions([tx])

await time_out_assert(
Expand Down
69 changes: 41 additions & 28 deletions chia/_tests/core/mempool/test_mempool_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -1622,9 +1622,10 @@ async def make_setup_and_coins(
for _ in range(2):
await farm_a_block(full_node_api, wallet_node, ph)
other_recipients = [Payment(puzzle_hash=p, amount=uint64(200), memos=[]) for p in phs[1:]]
[tx] = await wallet.generate_signed_transaction(
uint64(200), phs[0], DEFAULT_TX_CONFIG, primaries=other_recipients
)
async with wallet.wallet_state_manager.new_action_scope(push=False) as action_scope:
[tx] = await wallet.generate_signed_transaction(
uint64(200), phs[0], DEFAULT_TX_CONFIG, action_scope, primaries=other_recipients
)
[tx], _ = await wallet.wallet_state_manager.sign_transactions([tx])
assert tx.spend_bundle is not None
await send_to_mempool(full_node_api, tx.spend_bundle)
Expand All @@ -1640,9 +1641,16 @@ async def make_setup_and_coins(
wallet, coins, ph = await make_setup_and_coins(full_node_api, wallet_node)

# Make sure spending AB then BC would generate a conflict for the latter
[tx_a] = await wallet.generate_signed_transaction(uint64(30), ph, DEFAULT_TX_CONFIG, coins={coins[0].coin})
[tx_b] = await wallet.generate_signed_transaction(uint64(30), ph, DEFAULT_TX_CONFIG, coins={coins[1].coin})
[tx_c] = await wallet.generate_signed_transaction(uint64(30), ph, DEFAULT_TX_CONFIG, coins={coins[2].coin})
async with wallet.wallet_state_manager.new_action_scope(push=False) as action_scope:
[tx_a] = await wallet.generate_signed_transaction(
uint64(30), ph, DEFAULT_TX_CONFIG, action_scope, coins={coins[0].coin}
)
[tx_b] = await wallet.generate_signed_transaction(
uint64(30), ph, DEFAULT_TX_CONFIG, action_scope, coins={coins[1].coin}
)
[tx_c] = await wallet.generate_signed_transaction(
uint64(30), ph, DEFAULT_TX_CONFIG, action_scope, coins={coins[2].coin}
)
[tx_a, tx_b, tx_c], _ = await wallet.wallet_state_manager.sign_transactions([tx_a, tx_b, tx_c])
assert tx_a.spend_bundle is not None
assert tx_b.spend_bundle is not None
Expand All @@ -1657,9 +1665,10 @@ async def make_setup_and_coins(
# Make sure DE and EF would aggregate on E when E is eligible for deduplication

# Create a coin with the identity puzzle hash
[tx] = await wallet.generate_signed_transaction(
uint64(200), IDENTITY_PUZZLE_HASH, DEFAULT_TX_CONFIG, coins={coins[3].coin}
)
async with wallet.wallet_state_manager.new_action_scope(push=False) as action_scope:
[tx] = await wallet.generate_signed_transaction(
uint64(200), IDENTITY_PUZZLE_HASH, DEFAULT_TX_CONFIG, action_scope, coins={coins[3].coin}
)
[tx], _ = await wallet.wallet_state_manager.sign_transactions([tx])
assert tx.spend_bundle is not None
await send_to_mempool(full_node_api, tx.spend_bundle)
Expand All @@ -1682,22 +1691,25 @@ async def make_setup_and_coins(
message = b"Identical spend aggregation test"
e_announcement = AssertCoinAnnouncement(asserted_id=e_coin_id, asserted_msg=message)
# Create transactions D and F that consume an announcement created by E
[tx_d] = await wallet.generate_signed_transaction(
uint64(100),
ph,
DEFAULT_TX_CONFIG,
fee=uint64(0),
coins={coins[4].coin},
extra_conditions=(e_announcement,),
)
[tx_f] = await wallet.generate_signed_transaction(
uint64(150),
ph,
DEFAULT_TX_CONFIG,
fee=uint64(0),
coins={coins[5].coin},
extra_conditions=(e_announcement,),
)
async with wallet.wallet_state_manager.new_action_scope(push=False) as action_scope:
[tx_d] = await wallet.generate_signed_transaction(
uint64(100),
ph,
DEFAULT_TX_CONFIG,
action_scope,
fee=uint64(0),
coins={coins[4].coin},
extra_conditions=(e_announcement,),
)
[tx_f] = await wallet.generate_signed_transaction(
uint64(150),
ph,
DEFAULT_TX_CONFIG,
action_scope,
fee=uint64(0),
coins={coins[5].coin},
extra_conditions=(e_announcement,),
)
[tx_d, tx_f], _ = await wallet.wallet_state_manager.sign_transactions([tx_d, tx_f])
assert tx_d.spend_bundle is not None
assert tx_f.spend_bundle is not None
Expand All @@ -1724,9 +1736,10 @@ async def make_setup_and_coins(
sb_e2 = spend_bundle_from_conditions(conditions, e_coin)
g_coin = coins[6].coin
g_coin_id = g_coin.name()
[tx_g] = await wallet.generate_signed_transaction(
uint64(13), ph, DEFAULT_TX_CONFIG, coins={g_coin}, extra_conditions=(e_announcement,)
)
async with wallet.wallet_state_manager.new_action_scope(push=False) as action_scope:
[tx_g] = await wallet.generate_signed_transaction(
uint64(13), ph, DEFAULT_TX_CONFIG, action_scope, coins={g_coin}, extra_conditions=(e_announcement,)
)
[tx_g], _ = await wallet.wallet_state_manager.sign_transactions([tx_g])
assert tx_g.spend_bundle is not None
sb_e2g = SpendBundle.aggregate([sb_e2, tx_g.spend_bundle])
Expand Down
5 changes: 4 additions & 1 deletion chia/_tests/core/mempool/test_mempool_performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ async def test_mempool_update_performance(
await time_out_assert(30, wallet_balance_at_least, True, wallet_node, send_amount + fee_amount)

ph = await wallet.get_new_puzzlehash()
[big_transaction] = await wallet.generate_signed_transaction(send_amount, ph, DEFAULT_TX_CONFIG, fee_amount)
async with wallet.wallet_state_manager.new_action_scope(push=False) as action_scope:
[big_transaction] = await wallet.generate_signed_transaction(
send_amount, ph, DEFAULT_TX_CONFIG, action_scope, fee_amount
)
[big_transaction], _ = await wallet.wallet_state_manager.sign_transactions([big_transaction])
assert big_transaction.spend_bundle is not None
status, err = await full_node.add_transaction(
Expand Down
2 changes: 2 additions & 0 deletions chia/_tests/pools/test_pool_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,8 @@ async def status_is_farming_to_pool() -> bool:
assert pw_info.current.pool_url == "https://pool-a.org"
assert pw_info.current.relative_lock_height == 5

await full_node_api.wait_for_wallet_synced(wallet_node=wallet_node, timeout=20)

join_pool_tx: TransactionRecord = (
await client.pw_join_pool(
wallet_id,
Expand Down
52 changes: 29 additions & 23 deletions chia/_tests/simulation/test_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,14 @@ async def test_simulator_auto_farm_and_get_coins(

await time_out_assert(10, wallet.get_confirmed_balance, funds)
await time_out_assert(5, wallet.get_unconfirmed_balance, funds)
[tx] = await wallet.generate_signed_transaction(
uint64(10),
await wallet_node_2.wallet_state_manager.main_wallet.get_new_puzzlehash(),
DEFAULT_TX_CONFIG,
uint64(0),
)
async with wallet.wallet_state_manager.new_action_scope(push=False) as action_scope:
[tx] = await wallet.generate_signed_transaction(
uint64(10),
await wallet_node_2.wallet_state_manager.main_wallet.get_new_puzzlehash(),
DEFAULT_TX_CONFIG,
action_scope,
uint64(0),
)
[tx] = await wallet.wallet_state_manager.add_pending_transactions([tx])
# wait till out of mempool
await time_out_assert(10, full_node_api.full_node.mempool_manager.get_spendbundle, None, tx.name)
Expand Down Expand Up @@ -387,12 +389,14 @@ async def test_wait_transaction_records_entered_mempool(

# repeating just to try to expose any flakiness
for coin in coins:
[tx] = await wallet.generate_signed_transaction(
amount=uint64(tx_amount),
puzzle_hash=await wallet_node.wallet_state_manager.main_wallet.get_new_puzzlehash(),
tx_config=DEFAULT_TX_CONFIG,
coins={coin},
)
async with wallet.wallet_state_manager.new_action_scope(push=False) as action_scope:
[tx] = await wallet.generate_signed_transaction(
amount=uint64(tx_amount),
puzzle_hash=await wallet_node.wallet_state_manager.main_wallet.get_new_puzzlehash(),
tx_config=DEFAULT_TX_CONFIG,
action_scope=action_scope,
coins={coin},
)
[tx] = await wallet.wallet_state_manager.add_pending_transactions([tx])

await full_node_api.wait_transaction_records_entered_mempool(records=[tx])
Expand Down Expand Up @@ -432,17 +436,19 @@ async def test_process_transactions(
# repeating just to try to expose any flakiness
for repeat in range(repeats):
coins = [next(coins_iter) for _ in range(tx_per_repeat)]
transactions = [
(
await wallet.generate_signed_transaction(
amount=uint64(tx_amount),
puzzle_hash=await wallet_node.wallet_state_manager.main_wallet.get_new_puzzlehash(),
tx_config=DEFAULT_TX_CONFIG,
coins={coin},
)
)[0]
for coin in coins
]
async with wallet.wallet_state_manager.new_action_scope(push=False) as action_scope:
transactions = [
(
await wallet.generate_signed_transaction(
amount=uint64(tx_amount),
puzzle_hash=await wallet_node.wallet_state_manager.main_wallet.get_new_puzzlehash(),
tx_config=DEFAULT_TX_CONFIG,
action_scope=action_scope,
coins={coin},
)
)[0]
for coin in coins
]
for tx in transactions:
assert tx.spend_bundle is not None, "the above created transaction is missing the expected spend bundle"

Expand Down
Loading

0 comments on commit 4ffb6df

Please sign in to comment.