|
1 | 1 | import pytest |
2 | 2 |
|
3 | | -import asyncio |
4 | | - |
5 | | -from cancel_token import CancelToken |
6 | | - |
7 | 3 | from eth.beacon.types.attestation_records import AttestationRecord |
8 | 4 | from eth.beacon.types.attestation_data import AttestationData |
9 | | -from eth.db.atomic import AtomicDB |
10 | | -from eth.beacon.db.chain import BeaconChainDB |
11 | 5 | from eth.beacon.types.blocks import BaseBeaconBlock |
12 | 6 |
|
13 | 7 | from eth.constants import ( |
14 | 8 | ZERO_HASH32, |
15 | 9 | ) |
16 | 10 |
|
17 | | -from p2p import ecies |
18 | | -from p2p.exceptions import HandshakeFailure |
19 | | -from p2p.peer import MsgBuffer |
20 | | - |
21 | | -from trinity.protocol.bcc.context import BeaconContext |
22 | | -from trinity.protocol.bcc.peer import ( |
23 | | - BCCPeerFactory, |
| 11 | +from p2p.peer import ( |
| 12 | + MsgBuffer, |
24 | 13 | ) |
25 | | -from trinity.protocol.bcc.proto import BCCProtocol |
| 14 | + |
26 | 15 | from trinity.protocol.bcc.commands import ( |
27 | | - Status, |
28 | | - GetBeaconBlocks, |
29 | 16 | BeaconBlocks, |
| 17 | + GetBeaconBlocks, |
30 | 18 | AttestationRecords, |
31 | 19 | ) |
32 | 20 |
|
33 | | -from p2p.tools.paragon.helpers import ( |
34 | | - get_directly_linked_peers_without_handshake as _get_directly_linked_peers_without_handshake, |
35 | | - get_directly_linked_peers as _get_directly_linked_peers, |
| 21 | +from .helpers import ( |
| 22 | + get_directly_linked_peers, |
36 | 23 | ) |
37 | 24 |
|
38 | 25 |
|
39 | | -def get_fresh_chain_db(): |
40 | | - db = AtomicDB() |
41 | | - genesis_block = BaseBeaconBlock( |
42 | | - slot=0, |
43 | | - randao_reveal=ZERO_HASH32, |
44 | | - candidate_pow_receipt_root=ZERO_HASH32, |
45 | | - ancestor_hashes=[ZERO_HASH32] * 32, |
46 | | - state_root=ZERO_HASH32, # note: not the actual genesis state root |
47 | | - attestations=[], |
48 | | - specials=[], |
49 | | - proposer_signature=None, |
50 | | - ) |
51 | | - |
52 | | - chain_db = BeaconChainDB(db) |
53 | | - chain_db.persist_block(genesis_block) |
54 | | - return chain_db |
55 | | - |
56 | | - |
57 | | -async def _setup_alice_and_bob_factories(alice_chain_db=None, bob_chain_db=None): |
58 | | - cancel_token = CancelToken('trinity.get_directly_linked_peers_without_handshake') |
59 | | - |
60 | | - # |
61 | | - # Alice |
62 | | - # |
63 | | - if alice_chain_db is None: |
64 | | - alice_chain_db = get_fresh_chain_db() |
65 | | - |
66 | | - alice_context = BeaconContext( |
67 | | - chain_db=alice_chain_db, |
68 | | - network_id=1, |
69 | | - ) |
70 | | - |
71 | | - alice_factory = BCCPeerFactory( |
72 | | - privkey=ecies.generate_privkey(), |
73 | | - context=alice_context, |
74 | | - token=cancel_token, |
75 | | - ) |
76 | | - |
77 | | - # |
78 | | - # Bob |
79 | | - # |
80 | | - if bob_chain_db is None: |
81 | | - bob_chain_db = get_fresh_chain_db() |
82 | | - |
83 | | - bob_context = BeaconContext( |
84 | | - chain_db=bob_chain_db, |
85 | | - network_id=1, |
86 | | - ) |
87 | | - |
88 | | - bob_factory = BCCPeerFactory( |
89 | | - privkey=ecies.generate_privkey(), |
90 | | - context=bob_context, |
91 | | - token=cancel_token, |
92 | | - ) |
93 | | - |
94 | | - return alice_factory, bob_factory |
95 | | - |
96 | | - |
97 | | -async def get_directly_linked_peers_without_handshake(alice_chain_db=None, bob_chain_db=None): |
98 | | - alice_factory, bob_factory = await _setup_alice_and_bob_factories(alice_chain_db, bob_chain_db) |
99 | | - |
100 | | - return await _get_directly_linked_peers_without_handshake( |
101 | | - alice_factory=alice_factory, |
102 | | - bob_factory=bob_factory, |
103 | | - ) |
104 | | - |
105 | | - |
106 | | -async def get_directly_linked_peers(request, event_loop, alice_chain_db=None, bob_chain_db=None): |
107 | | - alice_factory, bob_factory = await _setup_alice_and_bob_factories( |
108 | | - alice_chain_db, |
109 | | - bob_chain_db, |
110 | | - ) |
111 | | - |
112 | | - return await _get_directly_linked_peers( |
113 | | - request, |
114 | | - event_loop, |
115 | | - alice_factory=alice_factory, |
116 | | - bob_factory=bob_factory, |
117 | | - ) |
118 | | - |
119 | | - |
120 | | -@pytest.mark.asyncio |
121 | | -async def test_directly_linked_peers_without_handshake(): |
122 | | - alice, bob = await get_directly_linked_peers_without_handshake() |
123 | | - assert alice.sub_proto is None |
124 | | - assert bob.sub_proto is None |
125 | | - |
126 | | - |
127 | | -@pytest.mark.asyncio |
128 | | -async def test_directly_linked_peers(request, event_loop): |
129 | | - alice, bob = await get_directly_linked_peers(request, event_loop) |
130 | | - assert isinstance(alice.sub_proto, BCCProtocol) |
131 | | - assert isinstance(bob.sub_proto, BCCProtocol) |
132 | | - |
133 | | - assert alice.head_hash == bob.context.chain_db.get_canonical_head().hash |
134 | | - assert bob.head_hash == alice.context.chain_db.get_canonical_head().hash |
135 | | - |
136 | | - |
137 | | -@pytest.mark.asyncio |
138 | | -async def test_unidirectional_handshake(request, event_loop): |
139 | | - alice, bob = await get_directly_linked_peers_without_handshake() |
140 | | - alice_chain_db = alice.context.chain_db |
141 | | - alice_genesis_hash = alice_chain_db.get_canonical_block_by_slot(0).hash |
142 | | - alice_head_hash = alice_chain_db.get_canonical_head().hash |
143 | | - |
144 | | - await asyncio.gather(alice.do_p2p_handshake(), bob.do_p2p_handshake()) |
145 | | - |
146 | | - await alice.send_sub_proto_handshake() |
147 | | - cmd, msg = await bob.read_msg() |
148 | | - |
149 | | - assert isinstance(cmd, Status) |
150 | | - |
151 | | - assert msg["protocol_version"] == BCCProtocol.version |
152 | | - assert msg["network_id"] == alice.context.network_id |
153 | | - assert msg["genesis_hash"] == alice_head_hash |
154 | | - assert msg["best_hash"] == alice_genesis_hash |
155 | | - |
156 | | - await bob.process_sub_proto_handshake(cmd, msg) |
157 | | - |
158 | | - assert bob.head_hash == alice_head_hash |
159 | | - assert alice.head_hash is None |
160 | | - |
161 | | - # stop cleanly |
162 | | - asyncio.ensure_future(alice.run()) |
163 | | - asyncio.ensure_future(bob.run()) |
164 | | - await asyncio.gather( |
165 | | - alice.cancel(), |
166 | | - bob.cancel(), |
167 | | - ) |
168 | | - |
169 | | - |
170 | | -@pytest.mark.asyncio |
171 | | -async def test_handshake_wrong_network_id(request, event_loop): |
172 | | - alice, bob = await get_directly_linked_peers_without_handshake() |
173 | | - alice.context.network_id += 1 |
174 | | - await asyncio.gather(alice.do_p2p_handshake(), bob.do_p2p_handshake()) |
175 | | - |
176 | | - await alice.send_sub_proto_handshake() |
177 | | - cmd, msg = await bob.read_msg() |
178 | | - |
179 | | - with pytest.raises(HandshakeFailure): |
180 | | - await bob.process_sub_proto_handshake(cmd, msg) |
181 | | - |
182 | | - # stop cleanly |
183 | | - asyncio.ensure_future(alice.run()) |
184 | | - asyncio.ensure_future(bob.run()) |
185 | | - await asyncio.gather( |
186 | | - alice.cancel(), |
187 | | - bob.cancel(), |
188 | | - ) |
189 | | - |
190 | | - |
191 | 26 | @pytest.mark.asyncio |
192 | 27 | async def test_send_no_blocks(request, event_loop): |
193 | 28 | alice, bob = await get_directly_linked_peers(request, event_loop) |
|
0 commit comments