Coverage for tests/utils/submit_then_relay_tx_tester.py: 93%

55 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-22 19:49 +0000

1 

2from time import sleep 

3 

4from monero import ( 

5 MoneroDaemonRpc, MoneroTx, 

6 MoneroSubmitTxResult 

7) 

8 

9from .daemon_utils import DaemonUtils 

10 

11 

12class SubmitThenRelayTxTester: 

13 """Test submitting then relaying txs.""" 

14 

15 daemon: MoneroDaemonRpc 

16 """Daemon rpc test instance.""" 

17 txs: list[MoneroTx] 

18 """Transactions to test.""" 

19 

20 def __init__(self, daemon: MoneroDaemonRpc, txs: list[MoneroTx]) -> None: 

21 """Initialize a submit-then-relay-tx tester. 

22 

23 :param MoneroDaemonRpc daemon: daemon rpc instance. 

24 :param list[MoneroTx] txs: txs to test. 

25 """ 

26 self.daemon = daemon 

27 self.txs = txs 

28 

29 def is_tx_in_pool(self, tx_hash: str) -> bool: 

30 """Check if tx is in pool. 

31 

32 :param str tx_hash: the transaction's hash to check. 

33 :returns bool: `True` if transaction was found in the pool. 

34 """ 

35 pool_txs: list[MoneroTx] = self.daemon.get_tx_pool() 

36 for a_tx in pool_txs: 

37 assert a_tx.hash is not None 

38 if a_tx.hash == tx_hash: 

39 assert a_tx.is_relayed is False 

40 return True 

41 

42 return False 

43 

44 def ensure_txs_relayed(self) -> None: 

45 """Ensure test transactions are relayed.""" 

46 # ensure txs are relayed 

47 pool_txs: list[MoneroTx] = self.daemon.get_tx_pool() 

48 for tx in self.txs: 

49 found: bool = False 

50 for a_tx in pool_txs: 

51 assert a_tx.hash is not None 

52 if a_tx.hash == tx.hash: 

53 assert a_tx.is_relayed is True 

54 found = True 

55 break 

56 

57 assert found, "Tx was not found after being submitted to the daemon's tx pool" 

58 

59 def get_and_test_tx_hashes(self) -> list[str]: 

60 """Submit and get tx hashes without relaying. 

61 

62 :returns list[str]: list of non-relayed tx hashes. 

63 """ 

64 tx_hashes: list[str] = [] 

65 for tx in self.txs: 

66 assert tx.hash is not None 

67 assert tx.full_hex is not None 

68 tx_hashes.append(tx.hash) 

69 result: MoneroSubmitTxResult = self.daemon.submit_tx_hex(tx.full_hex, True) 

70 DaemonUtils.test_submit_tx_result_good(result) 

71 assert result.is_relayed is False 

72 

73 # ensure tx is in pool 

74 assert self.is_tx_in_pool(tx.hash), "Tx was not found after being submitted to the daemon's tx pool" 

75 

76 # fetch tx by hash and ensure not relayed 

77 fetched_tx: MoneroTx | None = self.daemon.get_tx(tx.hash) 

78 assert fetched_tx is not None 

79 assert fetched_tx.is_relayed is False 

80 

81 return tx_hashes 

82 

83 def test(self) -> None: 

84 """Run test.""" 

85 # submit txs hex but don't relay 

86 tx_hashes: list[str] = self.get_and_test_tx_hashes() 

87 

88 # relay the txs 

89 try: 

90 if len(tx_hashes) == 1: 

91 self.daemon.relay_tx_by_hash(tx_hashes[0]) 

92 else: 

93 self.daemon.relay_txs_by_hash(tx_hashes) 

94 

95 except Exception: 

96 # flush txs when relay fails to prevent double spends in other tests 

97 self.daemon.flush_tx_pool(tx_hashes) 

98 raise 

99 

100 # wait for txs to be relayed 

101 # TODO (monero-project): all txs should be relayed: https://github.com/monero-project/monero/issues/8523 

102 

103 sleep(1) 

104 self.ensure_txs_relayed()