Coverage for tests/daemon/model/test_txs.py: 100%
254 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
1from __future__ import annotations
3import logging
5import pytest
6from monero import (
7 MoneroBlock,
8 MoneroKeyImage,
9 MoneroOutput,
10 MoneroSubmitTxResult,
11 MoneroTx,
12 MoneroTxPoolStats,
13 TxHeightComparator,
14)
16from utils import AssertUtils, BaseTestClass
18logger: logging.Logger = logging.getLogger("TestMoneroDaemonModelTxs")
21@pytest.mark.unit
22class TestMoneroDaemonModelTxs(BaseTestClass):
23 """Daemon txs model unit tests."""
25 def test_tx_deserialize(self) -> None:
26 tx: MoneroTx = MoneroTx()
27 tx.hash = "a" * 64
28 tx.is_miner_tx = False
29 tx.payment_id = "b" * 16
30 tx.fee = 7500000
31 tx.relay = True
32 tx.is_relayed = True
33 tx.is_confirmed = True
34 tx.in_tx_pool = False
35 tx.is_locked = False
36 tx.num_confirmations = 10
37 tx.unlock_time = 0
38 tx.last_relayed_timestamp = 1700000000
39 tx.received_timestamp = 1700000000
40 tx.is_double_spend_seen = False
41 tx.key = "c" * 64
42 tx.full_hex = "deadbeef"
43 tx.pruned_hex = "deadbeef"
44 tx.prunable_hex = "deadbeef"
45 tx.prunable_hash = "d" * 64
46 tx.size = 1500
47 tx.weight = 1500
48 tx.metadata = "meta"
49 tx.is_kept_by_block = False
50 tx.is_failed = False
51 tx.last_failed_hash = "e" * 64
52 tx.max_used_block_hash = "f" * 64
53 # mixin, rctSignatures, rctSigPrunable and signatures are still left
54 # unimplemented in from_property_tree() (see below)
55 AssertUtils.assert_serialization_integrity(tx)
57 @pytest.mark.parametrize("json_fragment", [
58 '{"mixin":5}',
59 '{"rctSignatures":"x"}',
60 '{"rctSigPrunable":"x"}',
61 '{"signatures":["x"]}',
62 ])
63 def test_tx_unimplemented_fields(self, json_fragment: str) -> None:
64 with pytest.raises(Exception, match="not implemented"):
65 MoneroTx.deserialize(json_fragment)
67 def test_tx_version_common_tx_sets_last_failed_and_max_used_block_height_deserialize(self) -> None:
68 tx: MoneroTx = MoneroTx()
69 tx.version = 2
70 tx.common_tx_sets = "sets"
71 tx.last_failed_height = 100
72 tx.max_used_block_height = 200
73 AssertUtils.assert_serialization_integrity(tx)
75 def test_tx_ring_size_deserialize(self) -> None:
76 tx: MoneroTx = MoneroTx()
77 tx.ring_size = 16
78 AssertUtils.assert_serialization_integrity(tx)
80 def test_tx_extra_deserialize(self) -> None:
81 tx: MoneroTx = MoneroTx()
82 tx.extra = [1, 2, 3, 255]
83 AssertUtils.assert_serialization_integrity(tx)
85 def test_tx_inputs_outputs_and_output_indices_deserialize(self) -> None:
86 tx: MoneroTx = MoneroTx()
87 tx.output_indices = [100, 101]
88 vin: MoneroOutput = MoneroOutput()
89 vin.amount = 1
90 vin.key_image = MoneroKeyImage()
91 vin.key_image.hex = "a" * 64
92 tx.inputs = [vin]
93 vout: MoneroOutput = MoneroOutput()
94 vout.amount = 2
95 vout.index = 0
96 tx.outputs = [vout]
97 AssertUtils.assert_serialization_integrity(tx)
99 def test_submit_tx_result_deserialize(self) -> None:
100 result: MoneroSubmitTxResult = MoneroSubmitTxResult()
101 result.credits = 1
102 result.top_block_hash = "a" * 64
103 result.is_relayed = True
104 result.is_double_spend = False
105 result.is_fee_too_low = False
106 result.has_invalid_input = False
107 result.has_invalid_output = False
108 result.has_too_few_outputs = False
109 result.is_mixin_too_low = False
110 result.is_overspend = False
111 result.reason = "ok"
112 result.is_too_big = False
113 result.sanity_check_failed = False
114 result.is_tx_extra_too_big = False
115 result.is_nonzero_unlock_time = False
116 # is_good is serialized but never read back by from_property_tree()
117 AssertUtils.assert_serialization_integrity(result)
119 def test_submit_tx_result_is_good_deserialize(self) -> None:
120 result: MoneroSubmitTxResult = MoneroSubmitTxResult()
121 result.is_good = True
122 json_str: str = result.serialize()
123 logger.debug(f"Serialized submit tx result: {json_str}")
124 assert "isGood" in json_str
125 restored: MoneroSubmitTxResult = MoneroSubmitTxResult.deserialize(json_str)
126 logger.debug(f"Deserialized submit tx result re-serialized: {restored.serialize()}")
127 assert restored.is_good == result.is_good
129 def test_tx_pool_stats_deserialize(self) -> None:
130 stats: MoneroTxPoolStats = MoneroTxPoolStats()
131 stats.num_txs = 5
132 stats.num_not_relayed = 1
133 stats.num_failing = 0
134 stats.num_double_spends = 0
135 stats.num10m = 2
136 stats.fee_total = 1000
137 stats.bytes_max = 2000
138 stats.bytes_med = 1500
139 stats.bytes_min = 1000
140 stats.bytes_total = 5000
141 stats.histo98pc = 1800
142 stats.oldest_timestamp = 1700000000
143 # histo is serialized as an object but from_property_tree() has a TODO
144 # and never reads it back into the map
145 AssertUtils.assert_serialization_integrity(stats)
147 def test_tx_pool_stats_histo_deserialize(self) -> None:
148 stats: MoneroTxPoolStats = MoneroTxPoolStats()
149 stats.histo = {100: 1, 200: 2}
150 json_str: str = stats.serialize()
151 logger.debug(f"Serialized tx pool stats: {json_str}")
152 assert "histo" in json_str
153 restored: MoneroTxPoolStats = MoneroTxPoolStats.deserialize(json_str)
154 logger.debug(f"Deserialized tx pool stats re-serialized: {restored.serialize()}")
155 assert dict(restored.histo) == dict(stats.histo)
157 def test_tx_copy(self) -> None:
158 tx: MoneroTx = MoneroTx()
159 tx.hash = "a" * 64
160 tx.is_confirmed = True
161 tx.fee = 7500000
163 copy: MoneroTx = tx.copy()
164 assert copy is not tx
165 assert copy.serialize() == tx.serialize()
167 def test_tx_copy_deep_copies_inputs(self) -> None:
168 tx: MoneroTx = MoneroTx()
169 tx.hash = "a" * 64
171 key_image: MoneroKeyImage = MoneroKeyImage()
172 key_image.hex = "1" * 64
173 tx_input: MoneroOutput = MoneroOutput()
174 tx_input.tx = tx
175 tx_input.key_image = key_image
176 tx_input.amount = 1234
177 tx.inputs = [tx_input]
179 copy: MoneroTx = tx.copy()
180 assert len(copy.inputs) == 1
181 assert copy.inputs[0] is not tx_input
182 assert copy.inputs[0].key_image.hex == "1" * 64 # type: ignore
183 assert copy.inputs[0].amount == 1234
184 assert copy.inputs[0].tx is copy
186 def test_tx_merge(self) -> None:
187 a: MoneroTx = MoneroTx()
188 a.hash = "a" * 64
189 a.is_confirmed = True # required: merge() dereferences is_confirmed directly
190 a.fee = 7500000
192 b: MoneroTx = a.copy()
193 b.num_confirmations = 5 # a.num_confirmations is unset -> merge fills the gap
194 a.merge(b)
195 assert a.num_confirmations == 5
197 def test_tx_merge_adopts_other_block(self) -> None:
198 block: MoneroBlock = MoneroBlock()
199 block.height = 200
201 other: MoneroTx = MoneroTx()
202 other.hash = "a" * 64
203 other.is_confirmed = True
204 other.block = block
205 block.txs = [other]
207 tx: MoneroTx = MoneroTx()
208 tx.hash = "a" * 64
209 tx.is_confirmed = True
210 # tx.block is unset -> merge adopts other's block and repoints the block at tx
211 tx.merge(other)
212 assert tx.block is block
213 assert block.txs[0] is tx
215 def test_tx_merge_is_confirmed_can_become_true(self) -> None:
216 a: MoneroTx = MoneroTx()
217 a.hash = "a" * 64
218 a.is_confirmed = False
219 b: MoneroTx = a.copy()
220 b.is_confirmed = True
221 a.merge(b)
222 assert a.is_confirmed is True
224 def test_tx_merge_is_double_spend_seen_can_become_true(self) -> None:
225 a: MoneroTx = MoneroTx()
226 a.hash = "a" * 64
227 a.is_confirmed = True
228 a.is_double_spend_seen = False
229 b: MoneroTx = a.copy()
230 b.is_double_spend_seen = True
231 a.merge(b)
232 assert a.is_double_spend_seen is True
234 def test_tx_merge_in_tx_pool_can_become_true(self) -> None:
235 a: MoneroTx = MoneroTx()
236 a.hash = "a" * 64
237 a.is_confirmed = False
238 a.in_tx_pool = False
239 b: MoneroTx = a.copy()
240 b.in_tx_pool = True
241 a.merge(b)
242 assert a.in_tx_pool is True
244 def test_tx_merge_is_locked_can_become_false(self) -> None:
245 a: MoneroTx = MoneroTx()
246 a.hash = "a" * 64
247 a.is_confirmed = True
248 a.is_locked = False # self: already unlocked
249 b: MoneroTx = a.copy()
250 b.is_locked = True # other: still locked
251 a.merge(b)
252 assert a.is_locked is False
254 def test_tx_merge_extra_and_output_indices(self) -> None:
255 a: MoneroTx = MoneroTx()
256 a.hash = "a" * 64
257 a.is_confirmed = True # required: merge() dereferences is_confirmed directly
258 b: MoneroTx = a.copy()
259 b.extra = [1, 2, 3, 255]
260 b.output_indices = [100, 101]
261 a.merge(b) # a.extra/output_indices are unset -> merge should adopt b's
262 assert a.extra == [1, 2, 3, 255]
263 assert a.output_indices == [100, 101]
265 def test_tx_merge_inputs(self) -> None:
266 def make_input(tx: MoneroTx, key_image_hex: str) -> MoneroOutput:
267 key_image: MoneroKeyImage = MoneroKeyImage()
268 key_image.hex = key_image_hex
269 tx_input: MoneroOutput = MoneroOutput()
270 tx_input.tx = tx
271 tx_input.key_image = key_image
272 return tx_input
274 a: MoneroTx = MoneroTx()
275 a.hash = "a" * 64
276 a.is_confirmed = True # required: merge() dereferences is_confirmed directly
277 a.inputs = [make_input(a, "1" * 64)]
279 b: MoneroTx = MoneroTx()
280 b.hash = "a" * 64
281 b.is_confirmed = True
282 shared_input: MoneroOutput = make_input(b, "1" * 64) # same key image -> merged into a's input
283 shared_input.amount = 5000
284 b.inputs = [shared_input, make_input(b, "2" * 64)] # new key image -> appended
286 a.merge(b)
287 assert len(a.inputs) == 2
288 assert a.inputs[0].key_image.hex == "1" * 64 # type: ignore
289 assert a.inputs[0].amount == 5000 # filled from b's matching input
290 assert a.inputs[1].key_image.hex == "2" * 64 # type: ignore
291 assert a.inputs[1].tx is a # appended input is repointed at a
293 def test_tx_lt_height_comparator(self) -> None:
294 tx_a: MoneroTx = MoneroTx()
295 tx_a.block = MoneroBlock()
296 tx_a.block.height = 100
298 tx_b: MoneroTx = MoneroTx()
299 tx_b.block = MoneroBlock()
300 tx_b.block.height = 200
302 assert tx_a < tx_b
303 assert not (tx_b < tx_a)
304 assert TxHeightComparator.compare(tx_a, tx_b)
305 assert not TxHeightComparator.compare(tx_b, tx_a)
307 txs: list[MoneroTx] = [tx_b, tx_a]
308 txs.sort()
309 assert txs[0] is tx_a
310 assert txs[1] is tx_b
312 # unconfirmed (no block) transactions sort after confirmed ones
313 tx_unconfirmed: MoneroTx = MoneroTx()
314 assert tx_a < tx_unconfirmed
315 assert not (tx_unconfirmed < tx_a)