Coverage for tests/daemon/model/test_outputs.py: 100%

121 statements  

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

1from __future__ import annotations 

2 

3import logging 

4 

5import pytest 

6from monero import ( 

7 MoneroKeyImage, 

8 MoneroOutput, 

9 MoneroOutputDistributionEntry, 

10 MoneroOutputHistogramEntry, 

11 MoneroTx, 

12) 

13 

14from utils import AssertUtils, BaseTestClass 

15 

16logger: logging.Logger = logging.getLogger("TestMoneroDaemonModelOutputs") 

17 

18 

19@pytest.mark.unit 

20class TestMoneroDaemonModelOutputs(BaseTestClass): 

21 """Daemon outputs model unit tests.""" 

22 

23 def test_key_image_deserialize(self) -> None: 

24 key_image: MoneroKeyImage = MoneroKeyImage() 

25 key_image.hex = "a" * 64 

26 key_image.signature = "b" * 128 

27 AssertUtils.assert_serialization_integrity(key_image) 

28 

29 def test_key_image_deserialize_key_images(self) -> None: 

30 json_str: str = ( 

31 '{"keyImages": [' 

32 '{"hex": "' + "a" * 64 + '", "signature": "' + "b" * 128 + '"}, ' 

33 '{"hex": "' + "c" * 64 + '"}' 

34 ']}' 

35 ) 

36 key_images: list[MoneroKeyImage] = MoneroKeyImage.deserialize_key_images(json_str) 

37 assert len(key_images) == 2 

38 assert key_images[0].hex == "a" * 64 

39 assert key_images[0].signature == "b" * 128 

40 assert key_images[1].hex == "c" * 64 

41 assert key_images[1].signature is None 

42 

43 # a missing or empty keyImages array yields no key images 

44 assert MoneroKeyImage.deserialize_key_images("{}") == [] 

45 assert MoneroKeyImage.deserialize_key_images('{"keyImages": []}') == [] 

46 

47 # an empty string is not valid json 

48 with pytest.raises(Exception): 

49 MoneroKeyImage.deserialize_key_images("") 

50 

51 def test_output_deserialize(self) -> None: 

52 output: MoneroOutput = MoneroOutput() 

53 output.amount = 1000000 

54 output.index = 5 

55 key_image: MoneroKeyImage = MoneroKeyImage() 

56 key_image.hex = "a" * 64 

57 key_image.signature = "b" * 128 

58 output.key_image = key_image 

59 AssertUtils.assert_serialization_integrity(output) 

60 

61 def test_output_ring_output_indices_and_stealth_public_key_deserialize(self) -> None: 

62 output: MoneroOutput = MoneroOutput() 

63 output.amount = 1000000 

64 output.index = 5 

65 output.ring_output_indices = [10, 20, 30] 

66 output.stealth_public_key = "a" * 64 

67 output.mask = "b" * 64 

68 AssertUtils.assert_serialization_integrity(output) 

69 

70 def test_output_distribution_entry_deserialize(self) -> None: 

71 entry: MoneroOutputDistributionEntry = MoneroOutputDistributionEntry() 

72 entry.amount = 0 

73 entry.base = 100 

74 entry.distribution = [1, 2, 3, 4] 

75 entry.start_height = 0 

76 AssertUtils.assert_serialization_integrity(entry) 

77 

78 def test_output_histogram_entry_deserialize(self) -> None: 

79 entry: MoneroOutputHistogramEntry = MoneroOutputHistogramEntry() 

80 entry.amount = 0 

81 entry.num_instances = 10 

82 entry.unlocked_instances = 8 

83 entry.recent_instances = 2 

84 AssertUtils.assert_serialization_integrity(entry) 

85 

86 def test_key_image_copy(self) -> None: 

87 key_image: MoneroKeyImage = MoneroKeyImage() 

88 key_image.hex = "a" * 64 

89 key_image.signature = "b" * 128 

90 

91 copy: MoneroKeyImage = key_image.copy() 

92 assert copy is not key_image 

93 assert copy.serialize() == key_image.serialize() 

94 

95 def test_key_image_merge(self) -> None: 

96 a: MoneroKeyImage = MoneroKeyImage() 

97 a.hex = "a" * 64 

98 b: MoneroKeyImage = a.copy() 

99 b.signature = "b" * 128 # a.signature is unset -> merge fills the gap 

100 a.merge(b) 

101 assert a.signature == "b" * 128 

102 

103 def test_output_copy(self) -> None: 

104 output: MoneroOutput = MoneroOutput() 

105 output.amount = 1000000 

106 output.index = 5 

107 key_image: MoneroKeyImage = MoneroKeyImage() 

108 key_image.hex = "a" * 64 

109 output.key_image = key_image 

110 

111 copy: MoneroOutput = output.copy() 

112 assert copy is not output 

113 assert copy.key_image is not output.key_image # key_image is deep copied 

114 assert copy.serialize() == output.serialize() 

115 

116 def test_output_merge(self) -> None: 

117 a: MoneroOutput = MoneroOutput() 

118 a.amount = 1000000 

119 a.index = 5 

120 b: MoneroOutput = a.copy() # preserves the (unset) tx reference, so merge won't recurse into tx merge 

121 b.key_image = MoneroKeyImage() 

122 b.key_image.hex = "a" * 64 

123 a.merge(b) # a.key_image is unset -> merge adopts b's key_image 

124 assert a.key_image is not None 

125 assert a.key_image.hex == "a" * 64 

126 

127 def test_output_merge_recurses_into_tx_merge(self) -> None: 

128 tx_a: MoneroTx = MoneroTx() 

129 tx_a.hash = "a" * 64 

130 tx_a.is_confirmed = True 

131 

132 tx_b: MoneroTx = MoneroTx() 

133 tx_b.hash = "a" * 64 

134 tx_b.is_confirmed = True 

135 tx_b.num_confirmations = 9 # only tx merge reconciles this 

136 

137 out_a: MoneroOutput = MoneroOutput() 

138 out_a.tx = tx_a 

139 out_a.stealth_public_key = "s" * 64 

140 tx_a.outputs = [out_a] 

141 

142 out_b: MoneroOutput = MoneroOutput() 

143 out_b.tx = tx_b 

144 out_b.stealth_public_key = "s" * 64 

145 tx_b.outputs = [out_b] 

146 

147 # outputs on different txs -> merge delegates to tx merge (which comes back to merging outputs) 

148 out_a.merge(out_b) 

149 assert tx_a.num_confirmations == 9 

150 

151 def test_output_merge_ring_output_indices_and_stealth_public_key(self) -> None: 

152 a: MoneroOutput = MoneroOutput() 

153 a.amount = 1000000 

154 a.index = 5 

155 b: MoneroOutput = a.copy() # preserves the (unset) tx reference, so merge won't recurse into tx merge 

156 b.ring_output_indices = [10, 20, 30] 

157 b.stealth_public_key = "a" * 64 

158 b.mask = "b" * 64 

159 a.merge(b) # a's fields are unset -> merge should adopt b's 

160 assert a.ring_output_indices == [10, 20, 30] 

161 assert a.stealth_public_key == "a" * 64 

162 assert a.mask == "b" * 64