Coverage for tests/utils/tx_wallet_utils.py: 91%
147 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-22 19:49 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-22 19:49 +0000
1import logging
3from abc import ABC
4from typing import Optional
7from monero import (
8 MoneroTxWallet, MoneroUtils, MoneroBlock,
9 MoneroTxSet, MoneroTxQuery,
10 MoneroNetworkType, MoneroCheckTx,
11 MoneroCheckReserve
12)
14from .assert_utils import AssertUtils
15from .gen_utils import GenUtils
16from .context import TxContext
17from .transfer_utils import TransferUtils
19from .tx_wallet_tester import TxWalletTester
20from .txs_structure_tester import TxsStructureTester
23logger: logging.Logger = logging.getLogger("TxWalletUtils")
26class TxWalletUtils(ABC):
28 MAX_FEE: int = 7500000*10000
29 """Max tx fee."""
31 @classmethod
32 def test_tx_wallet(cls, tx: Optional[MoneroTxWallet], context: Optional[TxContext] = None) -> None:
33 """Test monero tx wallet.
35 :param MoneroTxWallet | None tx: wallet transaction to test.
36 :param TxContext | None context: test context (default `None`).
37 """
38 assert tx is not None
39 tester: TxWalletTester = TxWalletTester(tx, context)
40 tester.run()
42 # TODO test deep copy
43 #if ctx.is_copy is not True:
44 # cls.test_tx_wallet_copy(tx, ctx)
46 @classmethod
47 def test_txs_wallet(cls, txs: list[MoneroTxWallet], context: Optional[TxContext]) -> None:
48 """Test a list of transactions.
50 :param list[MoneroTxWallet] txs: transactions to test.
51 :param TxContext | None context: test context.
52 """
53 for tx in txs:
54 cls.test_tx_wallet(tx, context)
56 @classmethod
57 def test_described_tx_set(cls, described_tx_set: MoneroTxSet, network_type: MoneroNetworkType) -> None:
58 """Test described tx set.
60 :param MoneroTxSet described_tx_set: described tx set to test.
61 :param MoneroNetworkType network_type: tx set network type.
62 """
63 assert len(described_tx_set.txs) > 0, "Described tx set has no txs to test"
64 assert described_tx_set.signed_tx_hex is None, "Expected no signed tx hex to be defined in described tx set"
65 assert described_tx_set.unsigned_tx_hex is None, "Expected no unsigned tx hex to be defined in described tx set"
67 # test each transaction
68 # TODO use common tx wallet test?
69 assert described_tx_set.multisig_tx_hex is None
70 for parsed_tx in described_tx_set.txs:
71 # TODO monero-cpp full wallet is not assigning tx set to parsed txs
72 #assert parsed_tx.tx_set is not None
73 #assert parsed_tx.tx_set == described_tx_set, f"{parsed_tx.tx_set.serialize()} != {described_tx_set.serialize()}"
74 GenUtils.test_unsigned_big_integer(parsed_tx.input_sum, True)
75 GenUtils.test_unsigned_big_integer(parsed_tx.output_sum, True)
76 GenUtils.test_unsigned_big_integer(parsed_tx.fee)
77 GenUtils.test_unsigned_big_integer(parsed_tx.change_amount)
78 if parsed_tx.change_amount == 0:
79 assert parsed_tx.change_address is None
80 else:
81 assert parsed_tx.change_address is not None
82 MoneroUtils.validate_address(parsed_tx.change_address, network_type)
83 assert parsed_tx.ring_size is not None
84 assert parsed_tx.ring_size > 1
85 assert parsed_tx.unlock_time is not None
86 assert parsed_tx.unlock_time >= 0
87 assert parsed_tx.num_dummy_outputs is not None
88 assert parsed_tx.num_dummy_outputs >= 0
89 assert parsed_tx.extra_hex is not None
90 assert len(parsed_tx.extra_hex) > 0
91 assert parsed_tx.payment_id is None or len(parsed_tx.payment_id) > 0
92 assert parsed_tx.is_outgoing is True
93 assert parsed_tx.outgoing_transfer is not None
94 assert len(parsed_tx.outgoing_transfer.destinations) > 0
95 assert parsed_tx.is_incoming is None
96 for destination in parsed_tx.outgoing_transfer.destinations:
97 TransferUtils.test_destination(destination)
99 @classmethod
100 def test_get_txs_structure(cls, txs: list[MoneroTxWallet], query: Optional[MoneroTxQuery], regtest: bool) -> None:
101 """
102 Tests the integrity of the full structure in the given txs from the block down
103 to transfers / destinations.
105 :param list[MoneroTxWallet] txs: list of txs to get structure from.
106 :param MoneroTxQuery | None query: filter txs by query, if set.
107 :param bool regtest: indicates if running test on regtest network.
108 """
109 tester: TxsStructureTester = TxsStructureTester(txs, query, regtest)
110 tester.run()
112 @classmethod
113 def test_common_tx_sets(cls, txs: list[MoneroTxWallet], has_signed: bool, has_unsigned: bool, has_multisig: bool) -> None:
114 """Test common tx set in txs.
116 :param list[MoneroTxWallet] txs: txs to test common sets.
117 :param bool has_signed: expects signed tx hex to be defined in tx set.
118 :param bool has_unsigned: expects unsigned tx hex to be defined in tx set.
119 :param bool has_multisig: expects multisig tx hex to be defined in tx set.
120 """
121 assert len(txs) > 0
122 # assert that all sets are same reference
123 tx_set: Optional[MoneroTxSet] = None
124 for i, tx in enumerate(txs):
125 assert isinstance(tx, MoneroTxWallet)
126 if i == 0:
127 tx_set = tx.tx_set
128 else:
129 assert tx.tx_set == tx_set
131 # test expected set
132 assert tx_set is not None
134 if has_signed:
135 # check signed tx hex
136 assert tx_set.signed_tx_hex is not None
137 assert len(tx_set.signed_tx_hex) > 0
139 if has_unsigned:
140 # check unsigned tx hex
141 assert tx_set.unsigned_tx_hex is not None
142 assert len(tx_set.unsigned_tx_hex) > 0
144 if has_multisig:
145 # check multisign tx hex
146 assert tx_set.multisig_tx_hex is not None
147 assert len(tx_set.multisig_tx_hex) > 0
149 @classmethod
150 def test_spend_tx(cls, spend_tx: Optional[MoneroTxWallet]) -> None:
151 """Test spend transaction.
153 :param MoneroTxWallet | None spend_tx: Spend transaction to test.
154 """
155 # validate spend tx
156 assert spend_tx is not None
157 assert len(spend_tx.inputs) > 0
158 # validate tx inputs
159 for input_wallet in spend_tx.inputs:
160 assert input_wallet.key_image is not None
161 assert input_wallet.key_image.hex is not None
162 assert len(input_wallet.key_image.hex) > 0
164 @classmethod
165 def test_check_tx(cls, tx: Optional[MoneroTxWallet], check: MoneroCheckTx) -> None:
166 """Test tx with check result.
168 :param MoneroTxWallet | None tx: transaction to test.
169 :param MoneroCheckTx check: transaction check result to test.
170 """
171 assert tx is not None
172 assert check.is_good is not None
173 if check.is_good is True:
174 assert check.num_confirmations is not None
175 assert check.num_confirmations >= 0
176 assert check.in_tx_pool is not None
177 GenUtils.test_unsigned_big_integer(check.received_amount)
178 if check.in_tx_pool is True:
179 assert check.num_confirmations == 0
180 else:
181 # TODO (monero-wall-rpc) this fails (confirmations is 0) for (at least one) transaction
182 # that has 1 confirmation on test_check_tx_key()
183 assert check.num_confirmations > 0
184 else:
185 assert check.in_tx_pool is None
186 assert check.num_confirmations is None
187 assert check.received_amount is None
189 @classmethod
190 def test_check_reserve(cls, check: MoneroCheckReserve) -> None:
191 """Test wallet check reserve.
193 :param MoneroCheckReserve check: reserve check to test.
194 """
195 assert check.is_good is not None
196 if check.is_good is True:
197 assert check.total_amount is not None
198 GenUtils.test_unsigned_big_integer(check.total_amount)
199 assert check.total_amount >= 0
201 assert check.unconfirmed_spent_amount is not None
202 GenUtils.test_unsigned_big_integer(check.unconfirmed_spent_amount)
203 assert check.unconfirmed_spent_amount >= 0
204 else:
205 assert check.total_amount is None
206 assert check.unconfirmed_spent_amount is None
208 @classmethod
209 def set_block_copy(cls, copy: MoneroTxWallet, tx: MoneroTxWallet) -> None:
210 """
211 Replace tx block reference with copy.
213 :param MoneroTxWallet copy: copy transaction reference.
214 :param MoneroTxWallet tx: original transaction reference.
215 """
216 # skip unconfirmed tx
217 if copy.is_confirmed is not True:
218 return
220 # copy block
221 assert tx.block is not None
222 block: MoneroBlock = tx.block.copy()
224 # set copy tx in block copy
225 block.txs = [copy]
227 # set block copy reference to tx copy
228 copy.block = block
230 @classmethod
231 def txs_mergeable(cls, tx1: MoneroTxWallet, tx2: MoneroTxWallet) -> bool:
232 """Check if txs are mergeable.
234 :param MoneroTxWallet tx1: first tx to check merge with.
235 :param MoneroTxWallet tx2: second tx to check merge with.
236 :returns bool: `True` if txs are mergeable, `False` otherwise.
237 """
238 try:
239 # copy txs
240 copy1: MoneroTxWallet = tx1.copy()
241 copy2: MoneroTxWallet = tx2.copy()
242 # set block copies
243 cls.set_block_copy(copy1, tx1)
244 cls.set_block_copy(copy2, tx2)
245 # test merge
246 copy1.merge(copy2)
247 return True
248 except Exception as e:
249 # merge failed
250 logger.warning(f"Txs are not mergeable: {e}")
251 return False
253 @classmethod
254 def assert_list_txs_equals(cls, txs1: list[MoneroTxWallet], txs2: list[MoneroTxWallet], check_order: bool = False) -> None:
255 """Checks txs lists equality.
257 :param list[MoneroTxWallet] txs1: first list to check equality.
258 :param list[MoneroTxWallet] txs2: second list to check equality.
259 :param bool check_order: check also order (default `False`).
260 """
261 # assert lists have same size
262 assert len(txs1) == len(txs2), "Txs lists count doesn't equal"
264 if check_order:
265 # check lists have same objects and order
266 AssertUtils.assert_list_equals(txs1, txs2, "Txs lists doesn't equal")
267 return
269 # collect tx hashes
270 tx_hashes1: list[str] = []
271 tx_hashes2: list[str] = []
273 for i, tx1 in enumerate(txs1):
274 tx2: MoneroTxWallet = txs2[i]
275 assert tx1.hash is not None
276 assert tx2.hash is not None
277 tx_hashes1.append(tx1.hash)
278 tx_hashes2.append(tx2.hash)
280 # assert that lists have same hashes
281 for tx_hash1 in tx_hashes1:
282 assert tx_hash1 in tx_hashes2