Coverage for tests/daemon/rpc/test_outputs.py: 100%
93 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 MoneroDaemonRpc,
8 MoneroKeyImageSpentStatus,
9 MoneroOutput,
10 MoneroOutputDistributionEntry,
11 MoneroOutputHistogramEntry,
12 MoneroTx,
13 MoneroWalletRpc,
14)
16from utils import DaemonUtils, OutputUtils, TestUtils as Utils, WalletTxsUtils
17from .base_test_monero_daemon_rpc import BaseTestMoneroDaemonRpc
19logger: logging.Logger = logging.getLogger("TestMoneroDaemonRpcOutputs")
22@pytest.mark.integration
23class TestMoneroDaemonRpcOutputs(BaseTestMoneroDaemonRpc):
24 """Rpc daemon outputs integration tests."""
26 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
27 def test_get_spent_status_of_key_images(self, daemon: MoneroDaemonRpc, wallet: MoneroWalletRpc) -> None:
28 Utils.WALLET_TX_TRACKER.wait_for_txs_to_clear_pool(wallet)
30 # submit txs to the pool to collect key images then flush them
31 txs: list[MoneroTx] = []
33 for i in range(1, 3):
34 tx: MoneroTx = WalletTxsUtils.get_unrelayed_tx(wallet, i)
35 assert tx.full_hex is not None
36 daemon.submit_tx_hex(tx.full_hex, True)
37 txs.append(tx)
39 key_images: list[str] = []
40 tx_hashes: list[str] = []
41 for tx in txs:
42 assert tx.hash is not None
43 tx_hashes.append(tx.hash)
45 for tx in daemon.get_txs(tx_hashes):
46 for tx_input in tx.inputs:
47 assert tx_input.key_image is not None
48 assert tx_input.key_image.hex is not None
49 key_images.append(tx_input.key_image.hex)
51 daemon.flush_tx_pool(tx_hashes)
53 # key images are not spent
54 DaemonUtils.test_spent_statuses(daemon, key_images, MoneroKeyImageSpentStatus.NOT_SPENT)
56 # submit txs to the pool but don't relay
57 for tx in txs:
58 assert tx.full_hex is not None
59 daemon.submit_tx_hex(tx.full_hex, True)
61 # key images are in the tx pool
62 DaemonUtils.test_spent_statuses(daemon, key_images, MoneroKeyImageSpentStatus.TX_POOL)
64 # collect key images of confirmed txs
65 key_images = []
66 txs = DaemonUtils.get_confirmed_txs(daemon, 10)
67 for tx in txs:
68 for tx_input in tx.inputs:
69 assert tx_input.key_image is not None
70 assert tx_input.key_image.hex is not None
71 key_images.append(tx_input.key_image.hex)
73 # key images are all spent
74 DaemonUtils.test_spent_statuses(daemon, key_images, MoneroKeyImageSpentStatus.CONFIRMED)
76 # flush this test's txs from pool
77 daemon.flush_tx_pool(tx_hashes)
79 # Can get an output histogram (binary)
80 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
81 def test_get_output_histogram_binary(self, daemon: MoneroDaemonRpc) -> None:
82 entries: list[MoneroOutputHistogramEntry] = daemon.get_output_histogram([], None, None, None, None)
83 assert len(entries) > 0
84 for entry in entries:
85 OutputUtils.test_output_histogram_entry(entry)
87 # Can get an output distribution
88 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
89 def test_get_output_distribution(self, daemon: MoneroDaemonRpc) -> None:
90 amounts: list[int] = [0]
91 entries: list[MoneroOutputDistributionEntry] = daemon.get_output_distribution(amounts)
92 for entry in entries:
93 OutputUtils.test_output_distribution_entry(entry)
95 # Can get the global output indices of a transaction
96 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
97 def test_get_output_indices(self, daemon: MoneroDaemonRpc) -> None:
98 tx_hashes: list[str] = DaemonUtils.get_confirmed_tx_hashes(daemon)
99 assert len(tx_hashes) > 0, "No confirmed txs found"
101 for tx_hash in tx_hashes:
102 indices: list[int] = daemon.get_output_indices(tx_hash)
103 assert len(indices) > 0
104 for index in indices:
105 assert index >= 0
107 with pytest.raises(Exception) as exc_info:
108 daemon.get_output_indices("")
109 assert str(exc_info.value) == "Must provide a transaction hash"
111 # Can get outputs given a list of output amounts and indices (binary)
112 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
113 def test_get_outputs(self, daemon: MoneroDaemonRpc) -> None:
114 tx_hashes: list[str] = DaemonUtils.get_confirmed_tx_hashes(daemon)
115 assert len(tx_hashes) > 0, "No confirmed txs found"
117 # identify each output by amount 0 (RingCT) and its global index
118 requested: list[MoneroOutput] = []
119 for tx_hash in tx_hashes:
120 for index in daemon.get_output_indices(tx_hash):
121 output: MoneroOutput = MoneroOutput()
122 output.amount = 0
123 output.index = index
124 requested.append(output)
126 num_requested: int = len(requested)
127 assert num_requested > 0
129 fetched: list[MoneroOutput] = daemon.get_outputs(requested)
130 num_fetched: int = len(fetched)
131 assert num_fetched == num_requested
133 for i, output in enumerate(fetched):
134 logger.debug(f"Testing fetched output: {output.serialize()}")
135 assert output.index == requested[i].index
136 assert output.stealth_public_key is not None and len(output.stealth_public_key) == 64
137 assert output.mask is not None and len(output.mask) == 64
138 assert output.tx is not None
139 assert output.tx.block is not None and output.tx.block.height is not None
141 with pytest.raises(Exception) as exc_info:
142 daemon.get_outputs([])
143 assert str(exc_info.value) == "Must provide outputs to fetch"