Coverage for tests/utils/wallet_txs_utils.py: 97%

60 statements  

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

1from abc import ABC 

2from typing import Optional 

3from random import shuffle 

4 

5from monero import ( 

6 MoneroWallet, MoneroTxWallet, MoneroTxQuery, 

7 MoneroTxConfig 

8) 

9 

10from .assert_utils import AssertUtils 

11from .context import TxContext 

12from .tx_wallet_utils import TxWalletUtils 

13 

14 

15class WalletTxsUtils(ABC): 

16 

17 @classmethod 

18 def get_and_test_txs( 

19 cls, 

20 wallet: MoneroWallet, 

21 query: Optional[MoneroTxQuery], 

22 ctx: Optional[TxContext], 

23 is_expected: Optional[bool], 

24 regtest: bool 

25 ) -> list[MoneroTxWallet]: 

26 """Get and test txs from wallet. 

27 

28 :param MoneroWallet wallet: wallet to get txs from. 

29 :param MoneroTxQuery | None query: filter wallet txs by query if defined. 

30 :param TxContext | None ctx: transaction context. 

31 :param bool | None is_expected: expects empty/non-empty txs. 

32 :param bool regtest: indicates if running test on regtest network. 

33 :returns list[MoneroTxWallet]: the fetched, tested txs. 

34 """ 

35 copy: Optional[MoneroTxQuery] = query.copy() if query is not None else None 

36 txs: list[MoneroTxWallet] = wallet.get_txs(query) if query is not None else wallet.get_txs() 

37 assert txs is not None 

38 

39 if is_expected is False: 

40 assert len(txs) == 0 

41 

42 if is_expected is True: 

43 assert len(txs) > 0 

44 

45 TxWalletUtils.test_txs_wallet(txs, ctx) 

46 TxWalletUtils.test_get_txs_structure(txs, query, regtest) 

47 

48 if query is not None: 

49 AssertUtils.assert_equals(copy, query) 

50 

51 return txs 

52 

53 @classmethod 

54 def get_random_transactions( 

55 cls, 

56 wallet: MoneroWallet, 

57 query: Optional[MoneroTxQuery] = None, 

58 min_txs: Optional[int] = None, 

59 max_txs: Optional[int] = None 

60 ) -> list[MoneroTxWallet]: 

61 """Get random transaction from wallet. 

62 

63 :param MoneroWallet wallet: wallet to get random txs from. 

64 :param MoneroTxQuery | None query: filter txs by query (default `None`). 

65 :param int | None min_txs: minimum number of txs to get (default `None`). 

66 :param int | None max_txs: maximum number of txs to get (default `None`). 

67 :returns list[MoneroTxWallet]: the fetched random txs. 

68 """ 

69 txs: list[MoneroTxWallet] = wallet.get_txs(query if query is not None else MoneroTxQuery()) 

70 

71 if min_txs is not None: 

72 assert len(txs) >= min_txs, f"{len(txs)}/{min_txs} transactions found with the query" 

73 

74 shuffle(txs) 

75 

76 if max_txs is None: 

77 return txs 

78 

79 result: list[MoneroTxWallet] = [] 

80 

81 for i, tx in enumerate(txs): 

82 result.append(tx) 

83 if i >= max_txs - 1: 

84 break 

85 

86 return result 

87 

88 @classmethod 

89 def get_unrelayed_tx(cls, wallet: MoneroWallet, account_idx: int) -> MoneroTxWallet: 

90 """Get unrelayed tx from wallet account. 

91 

92 :param MoneroWallet wallet: wallet to get unrelayed tx from. 

93 :param int account_idx: wallet account index to get unrelayed tx from. 

94 :returns MoneroTxWallet: unrealyed wallet tx. 

95 """ 

96 # TODO monero-project 

97 assert account_idx > 0, "Txs sent from/to same account are not properly synced from the pool" 

98 config: MoneroTxConfig = MoneroTxConfig() 

99 config.account_index = account_idx 

100 config.address = wallet.get_primary_address() 

101 config.amount = TxWalletUtils.MAX_FEE 

102 

103 tx: MoneroTxWallet = wallet.create_tx(config) 

104 assert (tx.full_hex is None or tx.full_hex == "") is False 

105 assert tx.relay is False, f"Expected tx.relay to be False, got {tx.relay}" 

106 return tx 

107 

108 @classmethod 

109 def test_scan_txs(cls, wallet: MoneroWallet, scan_wallet: MoneroWallet) -> None: 

110 """Test wallet transaction scan. 

111 

112 :param MoneroWallet wallet: original wallet to test. 

113 :param MoneroWallet scan_wallet: scan wallet to test. 

114 """ 

115 # get a few tx hashes 

116 tx_hashes: list[str] = [] 

117 txs: list[MoneroTxWallet] = wallet.get_txs() 

118 assert len(txs) > 2, "Not enough txs to scan" 

119 for i in range(1, 3): 

120 tx_hash: str | None = txs[i].hash 

121 assert tx_hash is not None 

122 tx_hashes.append(tx_hash) 

123 

124 # start wallet without scanning 

125 # TODO create wallet without daemon connection (offline does not reconnect, default connects to localhost, 

126 # offline then online causes confirmed txs to disappear) 

127 scan_wallet.stop_syncing() 

128 assert scan_wallet.is_connected_to_daemon() 

129 

130 # scan txs 

131 scan_wallet.scan_txs(tx_hashes) 

132 

133 # TODO scanning txs causes merge problems reconciling 0 fee, is_miner_tx with test txs 

134 

135 # close wallet 

136 scan_wallet.close(False)