Coverage for tests/ledger/test_block_utils.py: 100%

40 statements  

« prev     ^ index     » next       coverage.py v7.5.3, created at 2025-07-10 13:43 +0000

1# The MIT License (MIT) 

2# 

3# Copyright (c) 2021 RSK Labs Ltd 

4# 

5# Permission is hereby granted, free of charge, to any person obtaining a copy of 

6# this software and associated documentation files (the "Software"), to deal in 

7# the Software without restriction, including without limitation the rights to 

8# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 

9# of the Software, and to permit persons to whom the Software is furnished to do 

10# so, subject to the following conditions: 

11# 

12# The above copyright notice and this permission notice shall be included in all 

13# copies or substantial portions of the Software. 

14# 

15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 

16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 

17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 

18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 

19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 

20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 

21# SOFTWARE. 

22 

23from unittest import TestCase 

24from unittest.mock import Mock, call 

25from parameterized import parameterized 

26import rlp 

27import ledger.block_utils as bu 

28 

29 

30class TestBlockUtils(TestCase): 

31 def test_rlp_first_element_list_payload_length_ok(self): 

32 elements = [ 

33 b"hello", 

34 b"abcd", 

35 b"10"*10000, 

36 b"", 

37 b"a", 

38 [b"another", b"", b"list", b"1"*9999], 

39 ] 

40 expected_payload_length = sum(map(lambda e: len(rlp.encode(e)), elements)) 

41 self.assertEqual( 

42 expected_payload_length, 

43 bu.rlp_first_element_list_payload_length(rlp.encode(elements)), 

44 ) 

45 

46 def test_rlp_first_element_list_payload_length_ok_emptylist(self): 

47 self.assertEqual(0, bu.rlp_first_element_list_payload_length(rlp.encode([]))) 

48 

49 def test_rlp_first_element_list_payload_length_notalist(self): 

50 with self.assertRaises(ValueError): 

51 bu.rlp_first_element_list_payload_length(rlp.encode(b"abcd")) 

52 

53 @parameterized.expand([ 

54 ("17 elements", 17, 1), 

55 ("18 elements", 18, 1), 

56 ("19 elements", 19, 3), 

57 ("20 elements", 20, 3), 

58 ]) 

59 def test_rlp_mm_payload_size_ok(self, _, num_fields, num_fields_to_exclude): 

60 bu.rlp_first_element_list_payload_length = Mock(return_value="what-i-wanted") 

61 block = self._makeblock(num_fields) 

62 

63 self.assertEqual("what-i-wanted", bu.rlp_mm_payload_size(rlp.encode(block).hex())) 

64 self.assertEqual( 

65 [call(rlp.encode(block[:-num_fields_to_exclude]))], 

66 bu.rlp_first_element_list_payload_length.call_args_list, 

67 ) 

68 

69 def test_rlp_mm_payload_size_wrong_list_size(self): 

70 with self.assertRaises(ValueError): 

71 bu.rlp_mm_payload_size(rlp.encode([b"abcd"]*15).hex()) 

72 

73 with self.assertRaises(ValueError): 

74 bu.rlp_mm_payload_size(rlp.encode([b"abcd"]*21).hex()) 

75 

76 def test_rlp_mm_payload_size_wrong_datatype(self): 

77 with self.assertRaises(ValueError): 

78 bu.rlp_mm_payload_size("notahex") 

79 

80 with self.assertRaises(ValueError): 

81 bu.rlp_mm_payload_size(b"abcd") 

82 

83 @parameterized.expand([ 

84 ("17 elements, remove all", 17, False, 16, False), 

85 ("18 elements, remove all", 18, False, 17, False), 

86 ("19 elements, remove all", 19, False, 16, False), 

87 ("20 elements, remove all", 20, False, 17, False), 

88 ("17 elements, remove all, hex", 17, False, 16, True), 

89 ("18 elements, remove all, hex", 18, False, 17, True), 

90 ("19 elements, remove all, hex", 19, False, 16, True), 

91 ("20 elements, remove all, hex", 20, False, 17, True), 

92 ("17 elements, leave BTC block", 17, True, 17, False), 

93 ("18 elements, leave BTC block", 18, True, 18, False), 

94 ("19 elements, leave BTC block", 19, True, 17, False), 

95 ("20 elements, leave BTC block", 20, True, 18, False), 

96 ("17 elements, leave BTC block, hex", 17, True, 17, True), 

97 ("18 elements, leave BTC block, hex", 18, True, 18, True), 

98 ("19 elements, leave BTC block, hex", 19, True, 17, True), 

99 ("20 elements, leave BTC block, hex", 20, True, 18, True), 

100 ]) 

101 def test_remove_mm_fields_if_present_ok(self, _, num_fields, leave_btcblock, 

102 expected_fields, hex_result): 

103 block = self._makeblock(num_fields) 

104 

105 result_bytes = bu.remove_mm_fields_if_present(rlp.encode(block).hex(), 

106 leave_btcblock=leave_btcblock, 

107 hex=hex_result) 

108 

109 if hex_result: 

110 result_bytes = bytes.fromhex(result_bytes) 

111 

112 self.assertEqual(self._makeblock(expected_fields), rlp.decode(result_bytes)) 

113 

114 def _makeblock(self, num_fields): 

115 return list(map(lambda e: bytes([e])*e, range(num_fields)))