Debian Codegraph @

tests/functional_tests/txpool.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 from __future__ import print_function
32
33 """Test txpool
34 """
35
36 from framework.daemon import Daemon
37 from framework.wallet import Wallet
38 from framework.zmq import Zmq
39
40 class TransferTest():
41 def run_test(self):
42 self.reset()
43 self.create()
44 self.mine()
45 self.check_txpool()
46
47 def reset(self):
48 print('Resetting blockchain')
49 daemon = Daemon()
50 res = daemon.get_height()
51 daemon.pop_blocks(res.height - 1)
52 daemon.flush_txpool()
53
54 def create(self):
55 print('Creating wallet')
56 wallet = Wallet()
57 # close the wallet if any, will throw if none is loaded
58 try: wallet.close_wallet()
59 except: pass
60 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'
61 res = wallet.restore_deterministic_wallet(seed = seed)
62
63 def mine(self):
64 print("Mining some blocks")
65 daemon = Daemon()
66 wallet = Wallet()
67
68 daemon.generateblocks('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', 80)
69 wallet.refresh()
70
71 def create_txes(self, address, ntxes):
72 print('Creating ' + str(ntxes) + ' transactions')
73
74 daemon = Daemon()
75 wallet = Wallet()
76
77 dst = {'address': address, 'amount': 1000000000000}
78
79 txes = {}
80 for i in range(ntxes):
81 res = wallet.transfer([dst], get_tx_hex = True)
82 txes[res.tx_hash] = res
83
84 return txes
85
86 def check_empty_pool(self):
87 daemon = Daemon()
88
89 res = daemon.get_transaction_pool_hashes()
90 assert not 'tx_hashes' in res or len(res.tx_hashes) == 0
91 res = daemon.get_transaction_pool_stats()
92 assert res.pool_stats.bytes_total == 0
93 assert res.pool_stats.bytes_min == 0
94 assert res.pool_stats.bytes_max == 0
95 assert res.pool_stats.bytes_med == 0
96 assert res.pool_stats.fee_total == 0
97 assert res.pool_stats.oldest == 0
98 assert res.pool_stats.txs_total == 0
99 assert res.pool_stats.num_failing == 0
100 assert res.pool_stats.num_10m == 0
101 assert res.pool_stats.num_not_relayed == 0
102 assert res.pool_stats.histo_98pc == 0
103 assert not 'histo' in res.pool_stats or len(res.pool_stats.histo) == 0
104 assert res.pool_stats.num_double_spends == 0
105
106 def check_txpool(self):
107 daemon = Daemon()
108 wallet = Wallet()
109 zmq = Zmq()
110
111 zmq_topic = "json-minimal-txpool_add"
112 zmq.sub(zmq_topic)
113
114 res = daemon.get_info()
115 height = res.height
116 txpool_size = res.tx_pool_size
117
118 self.check_empty_pool()
119
120 txes = self.create_txes('46r4nYSevkfBUMhuykdK3gQ98XDqDTYW1hNLaXNvjpsJaSbNtdXh1sKMsdVgqkaihChAzEy29zEDPMR3NHQvGoZCLGwTerK', 5)
121
122 res = daemon.get_info()
123 assert res.tx_pool_size == txpool_size + 5
124 txpool_size = res.tx_pool_size
125
126 res = daemon.get_transaction_pool()
127 assert len(res.transactions) == txpool_size
128 total_bytes = 0
129 total_fee = 0
130 min_bytes = 99999999999999
131 max_bytes = 0
132 for txid in txes.keys():
133 x = [x for x in res.transactions if x.id_hash == txid]
134 assert len(x) == 1
135 x = x[0]
136 assert x.kept_by_block == False
137 assert x.last_failed_id_hash == '0'*64
138 assert x.double_spend_seen == False
139 assert x.weight >= x.blob_size
140
141 assert x.blob_size * 2 == len(txes[txid].tx_blob)
142 assert x.fee == txes[txid].fee
143 assert x.tx_blob == txes[txid].tx_blob
144
145 total_bytes += x.blob_size
146 total_fee += x.fee
147 min_bytes = min(min_bytes, x.blob_size)
148 max_bytes = max(max_bytes, x.blob_size)
149
150 print('Checking all txs received via zmq')
151 for i in range(len(txes.keys())):
152 zmq_event = zmq.recv(zmq_topic)
153 assert len(zmq_event) == 1
154
155 zmq_tx = zmq_event[0]
156
157 x = [x for x in res.transactions if x.id_hash == zmq_tx["id"]]
158 assert len(x) == 1
159
160 x = x[0]
161 assert x.blob_size == zmq_tx["blob_size"]
162 assert x.weight == zmq_tx["weight"]
163 assert x.fee == zmq_tx["fee"]
164
165 res = daemon.get_transaction_pool_hashes()
166 assert sorted(res.tx_hashes) == sorted(txes.keys())
167
168 res = daemon.get_transaction_pool_stats()
169 assert res.pool_stats.bytes_total == total_bytes
170 assert res.pool_stats.bytes_min == min_bytes
171 assert res.pool_stats.bytes_max == max_bytes
172 assert res.pool_stats.bytes_med >= min_bytes and res.pool_stats.bytes_med <= max_bytes
173 assert res.pool_stats.fee_total == total_fee
174 assert res.pool_stats.txs_total == len(txes)
175 assert res.pool_stats.num_failing == 0
176 assert res.pool_stats.num_10m == 0
177 assert res.pool_stats.num_not_relayed == 0
178 assert res.pool_stats.num_double_spends == 0
179
180 print('Flushing 2 transactions')
181 txes_keys = list(txes.keys())
182 daemon.flush_txpool([txes_keys[1], txes_keys[3]])
183 res = daemon.get_transaction_pool()
184 assert len(res.transactions) == txpool_size - 2
185 assert len([x for x in res.transactions if x.id_hash == txes_keys[1]]) == 0
186 assert len([x for x in res.transactions if x.id_hash == txes_keys[3]]) == 0
187
188 new_keys = list(txes.keys())
189 new_keys.remove(txes_keys[1])
190 new_keys.remove(txes_keys[3])
191 res = daemon.get_transaction_pool_hashes()
192 assert sorted(res.tx_hashes) == sorted(new_keys)
193
194 res = daemon.get_transaction_pool()
195 assert len(res.transactions) == len(new_keys)
196 total_bytes = 0
197 total_fee = 0
198 min_bytes = 99999999999999
199 max_bytes = 0
200 for txid in new_keys:
201 x = [x for x in res.transactions if x.id_hash == txid]
202 assert len(x) == 1
203 x = x[0]
204 assert x.kept_by_block == False
205 assert x.last_failed_id_hash == '0'*64
206 assert x.double_spend_seen == False
207 assert x.weight >= x.blob_size
208
209 assert x.blob_size * 2 == len(txes[txid].tx_blob)
210 assert x.fee == txes[txid].fee
211 assert x.tx_blob == txes[txid].tx_blob
212
213 total_bytes += x.blob_size
214 total_fee += x.fee
215 min_bytes = min(min_bytes, x.blob_size)
216 max_bytes = max(max_bytes, x.blob_size)
217
218 res = daemon.get_transaction_pool_stats()
219 assert res.pool_stats.bytes_total == total_bytes
220 assert res.pool_stats.bytes_min == min_bytes
221 assert res.pool_stats.bytes_max == max_bytes
222 assert res.pool_stats.bytes_med >= min_bytes and res.pool_stats.bytes_med <= max_bytes
223 assert res.pool_stats.fee_total == total_fee
224 assert res.pool_stats.txs_total == len(new_keys)
225 assert res.pool_stats.num_failing == 0
226 assert res.pool_stats.num_10m == 0
227 assert res.pool_stats.num_not_relayed == 0
228 assert res.pool_stats.num_double_spends == 0
229
230 print('Flushing unknown transactions')
231 unknown_txids = ['1'*64, '2'*64, '3'*64]
232 daemon.flush_txpool(unknown_txids)
233 res = daemon.get_transaction_pool()
234 assert len(res.transactions) == txpool_size - 2
235
236 print('Mining transactions')
237 daemon.generateblocks('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', 1)
238 res = daemon.get_transaction_pool()
239 assert not 'transactions' in res or len(res.transactions) == txpool_size - 5
240 res = daemon.get_transaction_pool_hashes()
241 assert not 'tx_hashes' in res or len(res.tx_hashes) == 0
242
243 self.check_empty_pool()
244
245 print('Popping block')
246 daemon.pop_blocks(1)
247 res = daemon.get_transaction_pool_hashes()
248 assert sorted(res.tx_hashes) == sorted(new_keys)
249 res = daemon.get_transaction_pool()
250 assert len(res.transactions) == txpool_size - 2
251 for txid in new_keys:
252 x = [x for x in res.transactions if x.id_hash == txid]
253 assert len(x) == 1
254 x = x[0]
255 assert x.kept_by_block == True
256 assert x.last_failed_id_hash == '0'*64
257 assert x.double_spend_seen == False
258 assert x.weight >= x.blob_size
259
260 assert x.blob_size * 2 == len(txes[txid].tx_blob)
261 assert x.fee == txes[txid].fee
262 assert x.tx_blob == txes[txid].tx_blob
263
264 print('Checking relaying txes')
265 res = daemon.get_transaction_pool_hashes()
266 assert len(res.tx_hashes) > 0
267 txid = res.tx_hashes[0]
268 daemon.relay_tx([txid])
269 res = daemon.get_transactions([txid])
270 assert len(res.txs) == 1
271 assert res.txs[0].tx_hash == txid
272 assert res.txs[0].in_pool
273 assert res.txs[0].relayed
274
275 daemon.flush_txpool()
276 self.check_empty_pool()
277
278
279 if __name__ == '__main__':
280 TransferTest().run_test()