Debian Codegraph @

tests/functional_tests/p2p.py

raw symbols SCIP
1 #!/usr/bin/env python3
2
3 # Copyright (c) 2018-2022, The Monero Project
4
5 #
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without modification, are
9 # permitted provided that the following conditions are met:
10 #
11 # 1. Redistributions of source code must retain the above copyright notice, this list of
12 # conditions and the following disclaimer.
13 #
14 # 2. Redistributions in binary form must reproduce the above copyright notice, this list
15 # of conditions and the following disclaimer in the documentation and/or other
16 # materials provided with the distribution.
17 #
18 # 3. Neither the name of the copyright holder nor the names of its contributors may be
19 # used to endorse or promote products derived from this software without specific
20 # prior written permission.
21 #
22 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
25 # THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30 # THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32 from __future__ import print_function
33 import time
34
35 """Test daemon P2P
36 """
37
38 from framework.daemon import Daemon
39 from framework.wallet import Wallet
40
41 class P2PTest():
42 def run_test(self):
43 self.reset()
44 self.create()
45 self.mine(80)
46 self.test_p2p_reorg()
47 txid = self.test_p2p_tx_propagation()
48 self.test_p2p_block_propagation_shared(txid)
49 txid = self.test_p2p_tx_propagation()
50 self.test_p2p_block_propagation_new(txid)
51
52 def reset(self):
53 print('Resetting blockchain')
54 daemon = Daemon()
55 res = daemon.get_height()
56 daemon.pop_blocks(res.height - 1)
57 daemon.flush_txpool()
58
59 def create(self):
60 print('Creating wallet')
61 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'
62 self.wallet = Wallet(idx = 4)
63 # close the wallet if any, will throw if none is loaded
64 try: self.wallet.close_wallet()
65 except: pass
66 res = self.wallet.restore_deterministic_wallet(seed = seed)
67
68 def mine(self, blocks):
69 assert blocks >= 1
70
71 print("Generating", blocks, 'blocks')
72
73 daemon = Daemon(idx = 2)
74
75 # generate blocks
76 res_generateblocks = daemon.generateblocks('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', blocks)
77
78 def test_p2p_reorg(self):
79 print('Testing P2P reorg')
80 daemon2 = Daemon(idx = 2)
81 daemon3 = Daemon(idx = 3)
82
83 # give sync some time
84 time.sleep(1)
85
86 res = daemon2.get_info()
87 height = res.height
88 assert height > 0
89 top_block_hash = res.top_block_hash
90 assert len(top_block_hash) == 64
91
92 res = daemon3.get_info()
93 assert res.height == height
94 assert res.top_block_hash == top_block_hash
95
96 # disconnect daemons and mine separately on both
97 daemon2.out_peers(0)
98 daemon3.out_peers(0)
99
100 res = daemon2.generateblocks('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', 2)
101 res = daemon3.generateblocks('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', 3)
102
103 res = daemon2.get_info()
104 assert res.height == height + 2
105 daemon2_top_block_hash = res.top_block_hash
106 assert daemon2_top_block_hash != top_block_hash
107 res = daemon3.get_info()
108 assert res.height == height + 3
109 daemon3_top_block_hash = res.top_block_hash
110 assert daemon3_top_block_hash != top_block_hash
111 assert daemon3_top_block_hash != daemon2_top_block_hash
112
113 # reconnect, daemon2 will now switch to daemon3's chain
114 daemon2.out_peers(8)
115 daemon3.out_peers(8)
116 time.sleep(10)
117 res = daemon2.get_info()
118 assert res.height == height + 3
119 assert res.top_block_hash == daemon3_top_block_hash
120
121 # disconect, mine on daemon2 again more than daemon3
122 daemon2.out_peers(0)
123 daemon3.out_peers(0)
124
125 res = daemon2.generateblocks('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', 3)
126 res = daemon3.generateblocks('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', 2)
127
128 res = daemon2.get_info()
129 assert res.height == height + 6
130 daemon2_top_block_hash = res.top_block_hash
131 assert daemon2_top_block_hash != top_block_hash
132 res = daemon3.get_info()
133 assert res.height == height + 5
134 daemon3_top_block_hash = res.top_block_hash
135 assert daemon3_top_block_hash != top_block_hash
136 assert daemon3_top_block_hash != daemon2_top_block_hash
137
138 # reconnect, daemon3 will now switch to daemon2's chain
139 daemon2.out_peers(8)
140 daemon3.out_peers(8)
141 time.sleep(5)
142 res = daemon3.get_info()
143 assert res.height == height + 6
144 assert res.top_block_hash == daemon2_top_block_hash
145
146 # disconnect and mine a lot on daemon3
147 daemon2.out_peers(0)
148 daemon3.out_peers(0)
149 res = daemon3.generateblocks('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', 500)
150
151 # reconnect and wait for sync
152 daemon2.out_peers(8)
153 daemon3.out_peers(8)
154 loops = 100
155 while True:
156 res2 = daemon2.get_info()
157 res3 = daemon3.get_info()
158 if res2.top_block_hash == res3.top_block_hash:
159 break
160 time.sleep(10)
161 loops -= 1
162 assert loops >= 0
163
164 def test_p2p_tx_propagation(self):
165 print('Testing P2P tx propagation')
166 daemons = (Daemon(idx=2), Daemon(idx=3))
167
168 for daemon in daemons:
169 res = daemon.get_transaction_pool_hashes()
170 assert len(res.get('tx_hashes', [])) == 0
171
172 self.wallet.refresh()
173 res = self.wallet.get_balance()
174
175 dst = {'address': '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', 'amount': 1000000000000}
176 res = self.wallet.transfer([dst])
177 assert len(res.tx_hash) == 32*2
178 txid = res.tx_hash
179
180 # Due to Dandelion++, the network propagates transactions with a
181 # random delay, so poll for the transaction with a timeout. The delay
182 # should almost never exceed a maximum of 13s (~1/billion samples).
183 # Set the timeout slightly higher than the maximum delay to account
184 # for transmission and processing time.
185 timeout = 13.5
186 pending_daemons = set(daemons)
187 expected_hashes = [txid]
188 wait_cutoff = time.monotonic() + timeout
189 while True:
190 done = []
191 for daemon in pending_daemons:
192 res = daemon.get_transaction_pool_hashes()
193 hashes = res.get('tx_hashes')
194 if hashes:
195 assert hashes == expected_hashes
196 done.append(daemon)
197 pending_daemons.difference_update(done)
198 if len(pending_daemons) == 0:
199 break
200 max_delay = wait_cutoff - time.monotonic()
201 if max_delay <= 0:
202 break
203 time.sleep(min(.2, max_delay))
204 npending = len(pending_daemons)
205 assert npending == 0, '%d daemons pending' % npending
206
207 return txid
208
209 def test_p2p_block_propagation_shared(self, mempool_txid):
210 print('Testing P2P block propagation with shared TX')
211 daemon2 = Daemon(idx = 2)
212 daemon3 = Daemon(idx = 3)
213
214 # check precondition: txid in daemon2's and daemon3's mempool
215 res = daemon2.get_transaction_pool_hashes()
216 assert mempool_txid in res.get('tx_hashes', [])
217
218 res = daemon3.get_transaction_pool_hashes()
219 assert mempool_txid in res.get('tx_hashes', [])
220
221 # mine block on daemon2
222 res = daemon2.generateblocks('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', 1)
223 block_height = res.height
224
225 # wait until both are synced, or 5 seconds, whichever is first.
226 # the timeout time should be very, very low here since block propagation, unlike tx propagation, is designed
227 # to be as fast as possible.
228 # and since both daemons already have the tx in their mempools, a notification of a new fluffy block, which is
229 # pushed out immediately upon being mined, should result in an instant addition of that block to the chain,
230 # without any round trips.
231 # this test should only fail if you're running it on a potato where PoW verification + check_tx_inputs() takes
232 # more than 5 second.
233 deadline = time.monotonic() + 5
234 result = None
235 while result is None:
236 res2 = daemon2.get_info()
237 res3 = daemon3.get_info()
238 if res2.top_block_hash == res3.top_block_hash:
239 result = True
240 elif time.monotonic() > deadline:
241 result = False
242 else:
243 time.sleep(.25)
244 assert result, "Shared tx block propagation timed out"
245
246 # check the tx is moved to both daemons's blockchains at top block
247 for daemon in [daemon2, daemon3]:
248 res = daemon.get_transaction_pool_hashes()
249 assert not 'tx_hashes' in res or len(res.tx_hashes) == 0
250
251 res = daemon.get_transactions([mempool_txid])
252 assert len(res.get('txs', [])) == 1
253 tx_details = res.txs[0]
254 assert ('in_pool' not in tx_details) or (not tx_details.in_pool)
255 assert tx_details.block_height == block_height
256
257 def test_p2p_block_propagation_new(self, mempool_txid):
258 # there's a big problem with this testcase in that there's not yet a way to prevent daemon's from syncing
259 # mempools only, but still allow block propagation. so there's a good chance that the transaction will be synced
260 # between daemons between when daemon2's mempool is flushed and when daemon3 mines a new block. in this
261 # scenario, this testcase basically just degenerates into test_p2p_block_propagation_shared(). however, if this
262 # one ever fails but test_p2p_block_propagation_shared() passes, then we might have actually caught a problem
263 # with block propagation when one of the daemons is missing a tx(s)
264
265 print('Testing P2P block propagation with (possibly) new TX')
266 daemon2 = Daemon(idx = 2)
267 daemon3 = Daemon(idx = 3)
268
269 # check precondition: txid in daemon3's mempool
270 res = daemon3.get_transaction_pool_hashes()
271 assert mempool_txid in res.get('tx_hashes', [])
272
273 # flush daemon2 mempool
274 daemon2.flush_txpool()
275
276 # mine block on daemon3
277 res = daemon3.generateblocks('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', 1)
278 block_height = res.height
279
280 # wait until both are synced, or 5 seconds, whichever is first.
281 # the timeout time should be very, very low here since block propagation, unlike tx propagation, is designed
282 # to be as fast as possible. however, it might have to be raised if the daemon actually does make a round trip
283 # to request a missing tx in a fluffy block
284 deadline = time.monotonic() + 5
285 result = None
286 while result is None:
287 res2 = daemon2.get_info()
288 res3 = daemon3.get_info()
289 if res2.top_block_hash == res3.top_block_hash:
290 result = True
291 elif time.monotonic() > deadline:
292 result = False
293 else:
294 time.sleep(.25)
295 assert result, "New tx block propagation timed out"
296
297 # check the tx is moved to both daemons's blockchains at top block
298 for daemon in [daemon2, daemon3]:
299 res = daemon.get_transaction_pool_hashes()
300 assert not 'tx_hashes' in res or len(res.tx_hashes) == 0
301
302 res = daemon.get_transactions([mempool_txid])
303 assert len(res.get('txs', [])) == 1
304 tx_details = res.txs[0]
305 assert ('in_pool' not in tx_details) or (not tx_details.in_pool)
306 assert tx_details.block_height == block_height
307
308
309 if __name__ == '__main__':
310 P2PTest().run_test()