Debian Codegraph @

tests/functional_tests/get_output_distribution.py

raw symbols SCIP
1 #!/usr/bin/env python3
2
3 # Copyright (c) 2019-2022, The Monero Project
4 #
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without modification, are
8 # permitted provided that the following conditions are met:
9 #
10 # 1. Redistributions of source code must retain the above copyright notice, this list of
11 # conditions and the following disclaimer.
12 #
13 # 2. Redistributions in binary form must reproduce the above copyright notice, this list
14 # of conditions and the following disclaimer in the documentation and/or other
15 # materials provided with the distribution.
16 #
17 # 3. Neither the name of the copyright holder nor the names of its contributors may be
18 # used to endorse or promote products derived from this software without specific
19 # prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
22 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
24 # THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
29 # THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 """Test get_output_distribution RPC
32 """
33
34 from __future__ import print_function
35 from framework.daemon import Daemon
36 from framework.wallet import Wallet
37
38 class GetOutputDistributionTest():
39 def run_test(self):
40 self.reset()
41 self.create()
42 self.test_get_output_distribution()
43
44 def reset(self):
45 print('Resetting blockchain')
46 daemon = Daemon()
47 res = daemon.get_height()
48 daemon.pop_blocks(res.height - 1)
49 daemon.flush_txpool()
50
51 def create(self):
52 self.wallet = Wallet()
53 # close the wallet if any, will throw if none is loaded
54 try: self.wallet.close_wallet()
55 except: pass
56 res = self.wallet.restore_deterministic_wallet(seed = 'velvet lymph giddy number token physics poetry unquoted nibs useful sabotage limits benches lifestyle eden nitrogen anvil fewest avoid batch vials washing fences goat unquoted')
57
58 def test_get_output_distribution(self):
59 print("Test get_output_distribution")
60
61 daemon = Daemon()
62
63 res = daemon.get_output_distribution([0], 0, 0)
64 assert len(res.distributions) == 1
65 d = res.distributions[0]
66 assert d.amount == 0
67 assert d.base == 0
68 assert d.binary == False
69 assert len(d.distribution) == 1
70 assert d.distribution[0] == 0
71
72 res = daemon.generateblocks('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', 1)
73
74 res = daemon.get_output_distribution([0], 0, 0)
75 assert len(res.distributions) == 1
76 d = res.distributions[0]
77 assert d.amount == 0
78 assert d.base == 0
79 assert d.binary == False
80 assert len(d.distribution) == 2
81 assert d.distribution[0] == 0
82 assert d.distribution[1] == 1
83
84 res = daemon.pop_blocks(1)
85
86 res = daemon.get_output_distribution([0], 0, 0)
87 assert len(res.distributions) == 1
88 d = res.distributions[0]
89 assert d.amount == 0
90 assert d.base == 0
91 assert d.binary == False
92 assert len(d.distribution) == 1
93 assert d.distribution[0] == 0
94
95 res = daemon.generateblocks('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', 3)
96
97 res = daemon.get_output_distribution([0], 0, 0, cumulative = True)
98 assert len(res.distributions) == 1
99 d = res.distributions[0]
100 assert d.amount == 0
101 assert d.base == 0
102 assert d.binary == False
103 assert len(d.distribution) == 4
104 assert d.distribution[0] == 0
105 assert d.distribution[1] == 1
106 assert d.distribution[2] == 2
107 assert d.distribution[3] == 3
108
109 # extend
110 res = daemon.generateblocks('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', 80)
111
112 res = daemon.get_output_distribution([0], 0, 0, cumulative = True)
113 assert len(res.distributions) == 1
114 d = res.distributions[0]
115 assert d.amount == 0
116 assert d.base == 0
117 assert d.binary == False
118 assert len(d.distribution) == 84
119 for h in range(len(d.distribution)):
120 assert d.distribution[h] == h
121
122 # pop and replace, this will do through the "trim and extend" path
123 res = daemon.pop_blocks(2)
124 self.wallet.refresh()
125 dst = {'address': '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', 'amount': 1000000000000}
126 self.wallet.transfer([dst])
127 res = daemon.generateblocks('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', 1)
128 for step in range(3): # the second will be cached, the third will also be cached, but we get it in non-cumulative mode
129 res = daemon.get_output_distribution([0], 0, 0, cumulative = step < 3)
130 assert len(res.distributions) == 1
131 d = res.distributions[0]
132 assert d.amount == 0
133 assert d.base == 0
134 assert d.binary == False
135 assert len(d.distribution) == 83
136 for h in range(len(d.distribution)):
137 assert d.distribution[h] == (h if step < 3 else 1) + (2 if h == len(d.distribution) - 1 else 0)
138
139 # start at 0, end earlier
140 res = daemon.get_output_distribution([0], 0, 40, cumulative = True)
141 assert len(res.distributions) == 1
142 d = res.distributions[0]
143 assert d.amount == 0
144 assert d.base == 0
145 assert d.binary == False
146 assert len(d.distribution) == 41
147 for h in range(len(d.distribution)):
148 assert d.distribution[h] == h
149
150 # start after 0, end earlier
151 res = daemon.get_output_distribution([0], 10, 20, cumulative = True)
152 assert len(res.distributions) == 1
153 d = res.distributions[0]
154 assert d.amount == 0
155 assert d.base == 9
156 assert d.binary == False
157 assert len(d.distribution) == 11
158 for h in range(len(d.distribution)):
159 assert d.distribution[h] == 10 + h
160
161 # straddling up
162 res = daemon.get_output_distribution([0], 15, 25, cumulative = True)
163 assert len(res.distributions) == 1
164 d = res.distributions[0]
165 assert d.amount == 0
166 assert d.base == 14
167 assert d.binary == False
168 assert len(d.distribution) == 11
169 for h in range(len(d.distribution)):
170 assert d.distribution[h] == 15 + h
171
172 # straddling down
173 res = daemon.get_output_distribution([0], 8, 18, cumulative = True)
174 assert len(res.distributions) == 1
175 d = res.distributions[0]
176 assert d.amount == 0
177 assert d.base == 7
178 assert d.binary == False
179 assert len(d.distribution) == 11
180 for h in range(len(d.distribution)):
181 assert d.distribution[h] == 8 + h
182
183 # encompassing
184 res = daemon.get_output_distribution([0], 5, 20, cumulative = True)
185 assert len(res.distributions) == 1
186 d = res.distributions[0]
187 assert d.amount == 0
188 assert d.base == 4
189 assert d.binary == False
190 assert len(d.distribution) == 16
191 for h in range(len(d.distribution)):
192 assert d.distribution[h] == 5 + h
193
194 # single
195 res = daemon.get_output_distribution([0], 2, 2, cumulative = True)
196 assert len(res.distributions) == 1
197 d = res.distributions[0]
198 assert d.amount == 0
199 assert d.base == 1
200 assert d.binary == False
201 assert len(d.distribution) == 1
202 assert d.distribution[0] == 2
203
204 # a non existent amount
205 res = daemon.get_output_distribution([1], 0, 0)
206 assert len(res.distributions) == 1
207 d = res.distributions[0]
208 assert d.amount == 1
209 assert d.base == 0
210 assert d.binary == False
211 assert len(d.distribution) == 83
212 for h in range(len(d.distribution)):
213 assert d.distribution[h] == 0
214
215
216 class Guard:
217 def __enter__(self):
218 for i in range(4):
219 Wallet(idx = i).auto_refresh(False)
220 def __exit__(self, exc_type, exc_value, traceback):
221 for i in range(4):
222 Wallet(idx = i).auto_refresh(True)
223
224 if __name__ == '__main__':
225 with Guard() as guard:
226 GetOutputDistributionTest().run_test()