Skip to content

Commit 78dd638

Browse files
committed
Split up bcc tests into multiple files
1 parent f616298 commit 78dd638

File tree

4 files changed

+192
-170
lines changed

4 files changed

+192
-170
lines changed

tests/trinity/core/p2p-proto/bcc/__init__.py

Whitespace-only changes.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
from cancel_token import CancelToken
2+
3+
from eth.db.atomic import AtomicDB
4+
from eth.beacon.db.chain import BeaconChainDB
5+
from eth.beacon.types.blocks import BaseBeaconBlock
6+
from eth.constants import (
7+
ZERO_HASH32,
8+
)
9+
10+
from trinity.protocol.bcc.context import BeaconContext
11+
from trinity.protocol.bcc.peer import (
12+
BCCPeerFactory,
13+
)
14+
15+
from p2p import ecies
16+
from p2p.tools.paragon.helpers import (
17+
get_directly_linked_peers_without_handshake as _get_directly_linked_peers_without_handshake,
18+
get_directly_linked_peers as _get_directly_linked_peers,
19+
)
20+
21+
22+
def get_fresh_chain_db():
23+
db = AtomicDB()
24+
genesis_block = BaseBeaconBlock(
25+
slot=0,
26+
randao_reveal=ZERO_HASH32,
27+
candidate_pow_receipt_root=ZERO_HASH32,
28+
ancestor_hashes=[ZERO_HASH32] * 32,
29+
state_root=ZERO_HASH32, # note: not the actual genesis state root
30+
attestations=[],
31+
specials=[],
32+
proposer_signature=None,
33+
)
34+
35+
chain_db = BeaconChainDB(db)
36+
chain_db.persist_block(genesis_block)
37+
return chain_db
38+
39+
40+
async def _setup_alice_and_bob_factories(alice_chain_db=None, bob_chain_db=None):
41+
cancel_token = CancelToken('trinity.get_directly_linked_peers_without_handshake')
42+
43+
#
44+
# Alice
45+
#
46+
if alice_chain_db is None:
47+
alice_chain_db = get_fresh_chain_db()
48+
49+
alice_context = BeaconContext(
50+
chain_db=alice_chain_db,
51+
network_id=1,
52+
)
53+
54+
alice_factory = BCCPeerFactory(
55+
privkey=ecies.generate_privkey(),
56+
context=alice_context,
57+
token=cancel_token,
58+
)
59+
60+
#
61+
# Bob
62+
#
63+
if bob_chain_db is None:
64+
bob_chain_db = get_fresh_chain_db()
65+
66+
bob_context = BeaconContext(
67+
chain_db=bob_chain_db,
68+
network_id=1,
69+
)
70+
71+
bob_factory = BCCPeerFactory(
72+
privkey=ecies.generate_privkey(),
73+
context=bob_context,
74+
token=cancel_token,
75+
)
76+
77+
return alice_factory, bob_factory
78+
79+
80+
async def get_directly_linked_peers_without_handshake(alice_chain_db=None, bob_chain_db=None):
81+
alice_factory, bob_factory = await _setup_alice_and_bob_factories(alice_chain_db, bob_chain_db)
82+
83+
return await _get_directly_linked_peers_without_handshake(
84+
alice_factory=alice_factory,
85+
bob_factory=bob_factory,
86+
)
87+
88+
89+
async def get_directly_linked_peers(request, event_loop, alice_chain_db=None, bob_chain_db=None):
90+
alice_factory, bob_factory = await _setup_alice_and_bob_factories(
91+
alice_chain_db,
92+
bob_chain_db,
93+
)
94+
95+
return await _get_directly_linked_peers(
96+
request,
97+
event_loop,
98+
alice_factory=alice_factory,
99+
bob_factory=bob_factory,
100+
)

tests/trinity/core/p2p-proto/test_bcc.py renamed to tests/trinity/core/p2p-proto/bcc/test_commands.py

Lines changed: 6 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -1,193 +1,29 @@
11
import pytest
22

3-
import asyncio
4-
5-
from cancel_token import CancelToken
6-
73
from eth.beacon.types.attestation_records import AttestationRecord
84
from eth.beacon.types.attestation_data import AttestationData
9-
from eth.db.atomic import AtomicDB
10-
from eth.beacon.db.chain import BeaconChainDB
115
from eth.beacon.types.blocks import BaseBeaconBlock
126

137
from eth.constants import (
148
ZERO_HASH32,
159
)
1610

1711
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,
12+
from p2p.peer import (
13+
MsgBuffer,
2414
)
25-
from trinity.protocol.bcc.proto import BCCProtocol
15+
2616
from trinity.protocol.bcc.commands import (
27-
Status,
28-
GetBeaconBlocks,
2917
BeaconBlocks,
18+
GetBeaconBlocks,
3019
AttestationRecords,
3120
)
3221

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,
22+
from .helpers import (
23+
get_directly_linked_peers,
3624
)
3725

3826

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-
19127
@pytest.mark.asyncio
19228
async def test_send_no_blocks(request, event_loop):
19329
alice, bob = await get_directly_linked_peers(request, event_loop)
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import pytest
2+
3+
import asyncio
4+
5+
from p2p.exceptions import HandshakeFailure
6+
7+
from trinity.protocol.bcc.proto import BCCProtocol
8+
from trinity.protocol.bcc.commands import (
9+
Status,
10+
)
11+
12+
from .helpers import (
13+
get_directly_linked_peers,
14+
get_directly_linked_peers_without_handshake,
15+
)
16+
17+
18+
@pytest.mark.asyncio
19+
async def test_directly_linked_peers_without_handshake():
20+
alice, bob = await get_directly_linked_peers_without_handshake()
21+
assert alice.sub_proto is None
22+
assert bob.sub_proto is None
23+
24+
25+
@pytest.mark.asyncio
26+
async def test_directly_linked_peers(request, event_loop):
27+
alice, bob = await get_directly_linked_peers(request, event_loop)
28+
assert isinstance(alice.sub_proto, BCCProtocol)
29+
assert isinstance(bob.sub_proto, BCCProtocol)
30+
31+
assert alice.head_hash == bob.context.chain_db.get_canonical_head().hash
32+
assert bob.head_hash == alice.context.chain_db.get_canonical_head().hash
33+
34+
35+
@pytest.mark.asyncio
36+
async def test_unidirectional_handshake(request, event_loop):
37+
alice, bob = await get_directly_linked_peers_without_handshake()
38+
alice_chain_db = alice.context.chain_db
39+
alice_genesis_hash = alice_chain_db.get_canonical_block_by_slot(0).hash
40+
alice_head_hash = alice_chain_db.get_canonical_head().hash
41+
42+
await asyncio.gather(alice.do_p2p_handshake(), bob.do_p2p_handshake())
43+
44+
await alice.send_sub_proto_handshake()
45+
cmd, msg = await bob.read_msg()
46+
47+
assert isinstance(cmd, Status)
48+
49+
assert msg["protocol_version"] == BCCProtocol.version
50+
assert msg["network_id"] == alice.context.network_id
51+
assert msg["genesis_hash"] == alice_head_hash
52+
assert msg["best_hash"] == alice_genesis_hash
53+
54+
await bob.process_sub_proto_handshake(cmd, msg)
55+
56+
assert bob.head_hash == alice_head_hash
57+
assert alice.head_hash is None
58+
59+
# stop cleanly
60+
asyncio.ensure_future(alice.run())
61+
asyncio.ensure_future(bob.run())
62+
await asyncio.gather(
63+
alice.cancel(),
64+
bob.cancel(),
65+
)
66+
67+
68+
@pytest.mark.asyncio
69+
async def test_handshake_wrong_network_id(request, event_loop):
70+
alice, bob = await get_directly_linked_peers_without_handshake()
71+
alice.context.network_id += 1
72+
await asyncio.gather(alice.do_p2p_handshake(), bob.do_p2p_handshake())
73+
74+
await alice.send_sub_proto_handshake()
75+
cmd, msg = await bob.read_msg()
76+
77+
with pytest.raises(HandshakeFailure):
78+
await bob.process_sub_proto_handshake(cmd, msg)
79+
80+
# stop cleanly
81+
asyncio.ensure_future(alice.run())
82+
asyncio.ensure_future(bob.run())
83+
await asyncio.gather(
84+
alice.cancel(),
85+
bob.cancel(),
86+
)

0 commit comments

Comments
 (0)