Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 26 additions & 21 deletions chia/_tests/cmds/cmd_test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
from chia.wallet.util.wallet_types import WalletType
from chia.wallet.wallet_request_types import (
GetSyncStatusResponse,
GetTransaction,
GetTransactionResponse,
GetWallets,
GetWalletsResponse,
NFTCalculateRoyalties,
Expand Down Expand Up @@ -115,27 +117,30 @@ async def get_wallets(self, request: GetWallets) -> GetWalletsResponse:
raise ValueError(f"Invalid fingerprint: {self.fingerprint}")
return GetWalletsResponse([WalletInfoResponse(id=uint32(1), name="", type=uint8(w_type.value), data="")])

async def get_transaction(self, transaction_id: bytes32) -> TransactionRecord:
self.add_to_log("get_transaction", (transaction_id,))
return TransactionRecord(
confirmed_at_height=uint32(1),
created_at_time=uint64(1234),
to_puzzle_hash=bytes32([1] * 32),
to_address=encode_puzzle_hash(bytes32([1] * 32), "xch"),
amount=uint64(12345678),
fee_amount=uint64(1234567),
confirmed=False,
sent=uint32(0),
spend_bundle=WalletSpendBundle([], G2Element()),
additions=[Coin(bytes32([1] * 32), bytes32([2] * 32), uint64(12345678))],
removals=[Coin(bytes32([2] * 32), bytes32([4] * 32), uint64(12345678))],
wallet_id=uint32(1),
sent_to=[("aaaaa", uint8(1), None)],
trade_id=None,
type=uint32(TransactionType.OUTGOING_TX.value),
name=bytes32([2] * 32),
memos={bytes32([3] * 32): [bytes([4] * 32)]},
valid_times=ConditionValidTimes(),
async def get_transaction(self, request: GetTransaction) -> GetTransactionResponse:
self.add_to_log("get_transaction", (request,))
return GetTransactionResponse(
TransactionRecord(
confirmed_at_height=uint32(1),
created_at_time=uint64(1234),
to_puzzle_hash=bytes32([1] * 32),
to_address=encode_puzzle_hash(bytes32([1] * 32), "xch"),
amount=uint64(12345678),
fee_amount=uint64(1234567),
confirmed=False,
sent=uint32(0),
spend_bundle=WalletSpendBundle([], G2Element()),
additions=[Coin(bytes32([1] * 32), bytes32([2] * 32), uint64(12345678))],
removals=[Coin(bytes32([2] * 32), bytes32([4] * 32), uint64(12345678))],
wallet_id=uint32(1),
sent_to=[("aaaaa", uint8(1), None)],
trade_id=None,
type=uint32(TransactionType.OUTGOING_TX.value),
name=bytes32([2] * 32),
memos={bytes32([3] * 32): [bytes([4] * 32)]},
valid_times=ConditionValidTimes(),
),
bytes32([2] * 32),
)

async def get_cat_name(self, wallet_id: int) -> str:
Expand Down
43 changes: 18 additions & 25 deletions chia/_tests/cmds/wallet/test_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from chia.wallet.trading.trade_status import TradeStatus
from chia.wallet.transaction_record import TransactionRecord
from chia.wallet.transaction_sorting import SortKey
from chia.wallet.util.query_filter import HashFilter, TransactionTypeFilter
from chia.wallet.util.query_filter import HashFilter
from chia.wallet.util.transaction_type import TransactionType
from chia.wallet.util.tx_config import DEFAULT_TX_CONFIG, TXConfig
from chia.wallet.util.wallet_types import WalletType
Expand All @@ -47,6 +47,9 @@
CreateOfferForIDsResponse,
FungibleAsset,
GetHeightInfoResponse,
GetTransaction,
GetTransactions,
GetTransactionsResponse,
GetWalletBalance,
GetWalletBalanceResponse,
GetWallets,
Expand All @@ -57,6 +60,7 @@
RoyaltyAsset,
SendTransactionResponse,
TakeOfferResponse,
TransactionRecordWithMetadata,
WalletInfoResponse,
)
from chia.wallet.wallet_spend_bundle import WalletSpendBundle
Expand Down Expand Up @@ -100,9 +104,9 @@ def test_get_transaction(capsys: object, get_test_cli_clients: tuple[TestRpcClie
"get_wallets": [(GetWallets(type=None, include_data=True),)] * 3,
"get_cat_name": [(1,)],
"get_transaction": [
(bytes32.from_hexstr(bytes32_hexstr),),
(bytes32.from_hexstr(bytes32_hexstr),),
(bytes32.from_hexstr(bytes32_hexstr),),
(GetTransaction(bytes32.from_hexstr(bytes32_hexstr)),),
(GetTransaction(bytes32.from_hexstr(bytes32_hexstr)),),
(GetTransaction(bytes32.from_hexstr(bytes32_hexstr)),),
],
}
test_rpc_clients.wallet_rpc_client.check_log(expected_calls)
Expand All @@ -113,24 +117,13 @@ def test_get_transactions(capsys: object, get_test_cli_clients: tuple[TestRpcCli

# set RPC Client
class GetTransactionsWalletRpcClient(TestWalletRpcClient):
async def get_transactions(
self,
wallet_id: int,
start: int,
end: int,
sort_key: Optional[SortKey] = None,
reverse: bool = False,
to_address: Optional[str] = None,
type_filter: Optional[TransactionTypeFilter] = None,
confirmed: Optional[bool] = None,
) -> list[TransactionRecord]:
self.add_to_log(
"get_transactions", (wallet_id, start, end, sort_key, reverse, to_address, type_filter, confirmed)
)
async def get_transactions(self, request: GetTransactions) -> GetTransactionsResponse:
self.add_to_log("get_transactions", (request,))
l_tx_rec = []
for i in range(start, end):
t_type = TransactionType.INCOMING_CLAWBACK_SEND if i == end - 1 else TransactionType.INCOMING_TX
tx_rec = TransactionRecord(
assert request.start is not None and request.end is not None
for i in range(request.start, request.end):
t_type = TransactionType.INCOMING_CLAWBACK_SEND if i == request.end - 1 else TransactionType.INCOMING_TX
tx_rec = TransactionRecordWithMetadata(
confirmed_at_height=uint32(1 + i),
created_at_time=uint64(1234 + i),
to_puzzle_hash=bytes32([1 + i] * 32),
Expand All @@ -152,7 +145,7 @@ async def get_transactions(
)
l_tx_rec.append(tx_rec)

return l_tx_rec
return GetTransactionsResponse(l_tx_rec, request.wallet_id)

async def get_coin_records(self, request: GetCoinRecords) -> dict[str, Any]:
self.add_to_log("get_coin_records", (request,))
Expand Down Expand Up @@ -201,8 +194,8 @@ async def get_coin_records(self, request: GetCoinRecords) -> dict[str, Any]:
expected_calls: logType = {
"get_wallets": [(GetWallets(type=None, include_data=True),)] * 2,
"get_transactions": [
(1, 2, 4, SortKey.RELEVANCE, True, None, None, None),
(1, 2, 4, SortKey.RELEVANCE, True, None, None, None),
(GetTransactions(uint32(1), uint16(2), uint16(4), SortKey.RELEVANCE.name, True, None, None, None),),
(GetTransactions(uint32(1), uint16(2), uint16(4), SortKey.RELEVANCE.name, True, None, None, None),),
],
"get_coin_records": [
(GetCoinRecords(coin_id_filter=HashFilter.include([expected_coin_id])),),
Expand Down Expand Up @@ -490,7 +483,7 @@ async def cat_spend(
test_condition_valid_times,
)
],
"get_transaction": [(get_bytes32(2),), (get_bytes32(2),)],
"get_transaction": [(GetTransaction(get_bytes32(2)),), (GetTransaction(get_bytes32(2)),)],
}
test_rpc_clients.wallet_rpc_client.check_log(expected_calls)

Expand Down
3 changes: 2 additions & 1 deletion chia/_tests/pools/test_pool_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
from chia.wallet.util.wallet_types import WalletType
from chia.wallet.wallet_node import WalletNode
from chia.wallet.wallet_request_types import (
GetTransactions,
GetWalletBalance,
GetWallets,
PWAbsorbRewards,
Expand Down Expand Up @@ -605,7 +606,7 @@ async def test_absorb_self(
PWAbsorbRewards(wallet_id=uint32(2), fee=uint64(fee), push=True), DEFAULT_TX_CONFIG
)

tx1 = await client.get_transactions(1)
tx1 = (await client.get_transactions(GetTransactions(uint32(1)))).transactions
assert (250_000_000_000 + fee) in [tx.amount for tx in tx1]

@pytest.mark.anyio
Expand Down
61 changes: 41 additions & 20 deletions chia/_tests/wallet/rpc/test_wallet_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@
GetPrivateKey,
GetSyncStatusResponse,
GetTimestampForHeight,
GetTransaction,
GetTransactions,
GetWalletBalance,
GetWalletBalances,
GetWallets,
Expand Down Expand Up @@ -345,7 +347,7 @@ async def assert_get_balance(rpc_client: WalletRpcClient, wallet_node: WalletNod


async def tx_in_mempool(client: WalletRpcClient, transaction_id: bytes32) -> bool:
tx = await client.get_transaction(transaction_id)
tx = (await client.get_transaction(GetTransaction(transaction_id))).transaction
return tx.is_in_mempool()


Expand Down Expand Up @@ -433,7 +435,7 @@ async def test_send_transaction(wallet_rpc_environment: WalletRpcTestEnvironment
await farm_transaction(full_node_api, wallet_node, spend_bundle)

# Checks that the memo can be retrieved
tx_confirmed = await client.get_transaction(transaction_id)
tx_confirmed = (await client.get_transaction(GetTransaction(transaction_id))).transaction
assert tx_confirmed.confirmed
assert len(tx_confirmed.memos) == 1
assert [b"this is a basic tx"] in tx_confirmed.memos.values()
Expand Down Expand Up @@ -479,7 +481,7 @@ async def test_push_transactions(wallet_rpc_environment: WalletRpcTestEnvironmen
await farm_transaction(full_node_api, wallet_node, spend_bundle)

for tx in resp_client.transactions:
assert (await client.get_transaction(transaction_id=tx.name)).confirmed
assert (await client.get_transaction(GetTransaction(transaction_id=tx.name))).transaction.confirmed

# Just testing NOT failure here really (parsing)
await client.push_tx(PushTX(spend_bundle))
Expand Down Expand Up @@ -973,7 +975,7 @@ async def test_send_transaction_multi(wallet_rpc_environment: WalletRpcTestEnvir
await time_out_assert(20, get_confirmed_balance, generated_funds - amount_outputs - amount_fee, client, 1)

# Checks that the memo can be retrieved
tx_confirmed = await client.get_transaction(send_tx_res.name)
tx_confirmed = (await client.get_transaction(GetTransaction(send_tx_res.name))).transaction
assert tx_confirmed.confirmed
memos = tx_confirmed.memos
assert len(memos) == len(outputs)
Expand All @@ -996,18 +998,20 @@ async def test_get_transactions(wallet_rpc_environment: WalletRpcTestEnvironment

await generate_funds(full_node_api, env.wallet_1, 5)

all_transactions = await client.get_transactions(1)
all_transactions = (await client.get_transactions(GetTransactions(uint32(1)))).transactions
assert len(all_transactions) >= 10
# Test transaction pagination
some_transactions = await client.get_transactions(1, 0, 5)
some_transactions_2 = await client.get_transactions(1, 5, 10)
some_transactions = (await client.get_transactions(GetTransactions(uint32(1), uint16(0), uint16(5)))).transactions
some_transactions_2 = (
await client.get_transactions(GetTransactions(uint32(1), uint16(5), uint16(10)))
).transactions
assert some_transactions == all_transactions[0:5]
assert some_transactions_2 == all_transactions[5:10]

# Testing sorts
# Test the default sort (CONFIRMED_AT_HEIGHT)
assert all_transactions == sorted(all_transactions, key=attrgetter("confirmed_at_height"))
all_transactions = await client.get_transactions(1, reverse=True)
all_transactions = (await client.get_transactions(GetTransactions(uint32(1), reverse=True))).transactions
assert all_transactions == sorted(all_transactions, key=attrgetter("confirmed_at_height"), reverse=True)

# Test RELEVANCE
Expand All @@ -1018,13 +1022,20 @@ async def test_get_transactions(wallet_rpc_environment: WalletRpcTestEnvironment
1, uint64(1), encode_puzzle_hash(puzhash, "txch"), DEFAULT_TX_CONFIG
) # Create a pending tx

all_transactions = await client.get_transactions(1, sort_key=SortKey.RELEVANCE)
with pytest.raises(ValueError, match="There is no known sort foo"):
await client.get_transactions(GetTransactions(uint32(1), sort_key="foo"))

all_transactions = (
await client.get_transactions(GetTransactions(uint32(1), sort_key=SortKey.RELEVANCE.name))
).transactions
sorted_transactions = sorted(all_transactions, key=attrgetter("created_at_time"), reverse=True)
sorted_transactions = sorted(sorted_transactions, key=attrgetter("confirmed_at_height"), reverse=True)
sorted_transactions = sorted(sorted_transactions, key=attrgetter("confirmed"))
assert all_transactions == sorted_transactions

all_transactions = await client.get_transactions(1, sort_key=SortKey.RELEVANCE, reverse=True)
all_transactions = (
await client.get_transactions(GetTransactions(uint32(1), sort_key=SortKey.RELEVANCE.name, reverse=True))
).transactions
sorted_transactions = sorted(all_transactions, key=attrgetter("created_at_time"))
sorted_transactions = sorted(sorted_transactions, key=attrgetter("confirmed_at_height"))
sorted_transactions = sorted(sorted_transactions, key=attrgetter("confirmed"), reverse=True)
Expand All @@ -1036,31 +1047,41 @@ async def test_get_transactions(wallet_rpc_environment: WalletRpcTestEnvironment
await full_node_api.wait_for_wallet_synced(wallet_node=wallet_node, timeout=20)
await client.send_transaction(1, uint64(1), encode_puzzle_hash(ph_by_addr, "txch"), DEFAULT_TX_CONFIG)
await full_node_api.wait_for_wallet_synced(wallet_node=wallet_node, timeout=20)
tx_for_address = await client.get_transactions(1, to_address=encode_puzzle_hash(ph_by_addr, "txch"))
tx_for_address = (
await client.get_transactions(GetTransactions(uint32(1), to_address=encode_puzzle_hash(ph_by_addr, "txch")))
).transactions
assert len(tx_for_address) == 1
assert tx_for_address[0].to_puzzle_hash == ph_by_addr

# Test type filter
all_transactions = await client.get_transactions(
1, type_filter=TransactionTypeFilter.include([TransactionType.COINBASE_REWARD])
)
all_transactions = (
await client.get_transactions(
GetTransactions(uint32(1), type_filter=TransactionTypeFilter.include([TransactionType.COINBASE_REWARD]))
)
).transactions
assert len(all_transactions) == 5
assert all(transaction.type == TransactionType.COINBASE_REWARD.value for transaction in all_transactions)
# Test confirmed filter
all_transactions = await client.get_transactions(1, confirmed=True)
all_transactions = (await client.get_transactions(GetTransactions(uint32(1), confirmed=True))).transactions
assert len(all_transactions) == 10
assert all(transaction.confirmed for transaction in all_transactions)
all_transactions = await client.get_transactions(1, confirmed=False)
all_transactions = (await client.get_transactions(GetTransactions(uint32(1), confirmed=False))).transactions
assert len(all_transactions) == 2
assert all(not transaction.confirmed for transaction in all_transactions)

# Test bypass broken txs
await wallet.wallet_state_manager.tx_store.add_transaction_record(
dataclasses.replace(all_transactions[0], type=uint32(TransactionType.INCOMING_CLAWBACK_SEND))
)
all_transactions = await client.get_transactions(
1, type_filter=TransactionTypeFilter.include([TransactionType.INCOMING_CLAWBACK_SEND]), confirmed=False
)
all_transactions = (
await client.get_transactions(
GetTransactions(
uint32(1),
type_filter=TransactionTypeFilter.include([TransactionType.INCOMING_CLAWBACK_SEND]),
confirmed=False,
)
)
).transactions
assert len(all_transactions) == 1


Expand All @@ -1073,7 +1094,7 @@ async def test_get_transaction_count(wallet_rpc_environment: WalletRpcTestEnviro

await generate_funds(full_node_api, env.wallet_1)

all_transactions = await client.get_transactions(1)
all_transactions = (await client.get_transactions(GetTransactions(uint32(1)))).transactions
assert len(all_transactions) > 0
transaction_count = await client.get_transaction_count(1)
assert transaction_count == len(all_transactions)
Expand Down
2 changes: 1 addition & 1 deletion chia/_tests/wallet/test_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ async def test_wallet_clawback_clawback(self, wallet_environments: WalletTestFra
assert len(txs["transactions"]) == 1
assert not txs["transactions"][0]["confirmed"]
assert txs["transactions"][0]["metadata"]["recipient_puzzle_hash"][2:] == normal_puzhash.hex()
assert txs["transactions"][0]["metadata"]["coin_id"] == merkle_coin.name().hex()
assert txs["transactions"][0]["metadata"]["coin_id"] == "0x" + merkle_coin.name().hex()
with pytest.raises(ValueError):
await api_0.spend_clawback_coins({})

Expand Down
21 changes: 13 additions & 8 deletions chia/_tests/wallet/vc_wallet/test_vc_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import pytest
from chia_rs import G2Element
from chia_rs.sized_bytes import bytes32
from chia_rs.sized_ints import uint8, uint16, uint64
from chia_rs.sized_ints import uint8, uint16, uint32, uint64
from typing_extensions import Literal

from chia._tests.environments.wallet import WalletEnvironment, WalletStateTransition, WalletTestFramework
Expand All @@ -31,6 +31,7 @@
from chia.wallet.wallet import Wallet
from chia.wallet.wallet_node import WalletNode
from chia.wallet.wallet_request_types import (
GetTransactions,
GetWallets,
VCAddProofs,
VCGet,
Expand Down Expand Up @@ -454,13 +455,17 @@ 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 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"),
0,
1,
reverse=True,
type_filter=TransactionTypeFilter.include([TransactionType.INCOMING_CRCAT_PENDING]),
)
pending_tx = (
await client_1.get_transactions(
GetTransactions(
uint32(env_1.dealias_wallet_id("crcat")),
uint16(0),
uint16(1),
reverse=True,
type_filter=TransactionTypeFilter.include([TransactionType.INCOMING_CRCAT_PENDING]),
)
)
).transactions
assert len(pending_tx) == 1

# Send the VC to wallet_1 to use for the CR-CATs
Expand Down
Loading
Loading