Skip to content

Commit

Permalink
pytest: make run_handshake handle unsolicited Block messages (#4437)
Browse files Browse the repository at this point in the history
This addresses the following failure:

    Traceback (most recent call last):
      File "tests/spec/network/peers_request.py", line 44, in <module>
        asyncio.run(main())
      File "/usr/lib/python3.7/asyncio/runners.py", line 43, in run
        return loop.run_until_complete(main)
      File "/usr/lib/python3.7/asyncio/base_events.py", line 579, in run_until_complete
        return future.result()
      File "tests/spec/network/peers_request.py", line 35, in main
        await run_handshake(conn1, nodes[0].node_key.pk, key_pair_1, listen_port=12346)
      File "lib/peer.py", line 182, in run_handshake
        assert response.enum == 'Handshake', response.enum if response.enum != 'HandshakeFailure' else response.HandshakeFailure[1].enum
    AssertionError: Block

Fixes: #4432
  • Loading branch information
mina86 authored Jul 1, 2021
1 parent afac695 commit 60713c9
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions pytest/lib/peer.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,25 +161,29 @@ async def run_handshake(conn: Connection,
key_pair: SigningKey,
listen_port=12345):
handshake = create_handshake(key_pair, target_public_key, listen_port)
sign_handshake(key_pair, handshake.Handshake)

await conn.send(handshake)
response = await conn.recv()
async def send_handshake():
sign_handshake(key_pair, handshake.Handshake)
await conn.send(handshake)
# The peer might sent us an unsolicited message before replying to
# a successful handshake. This is because node is multi-threaded and
# peers are added to PeerManager before the reply is sent. Since we
# don’t care about those messages, ignore them and wait for some kind of
# Handshake reply.
return await conn.recv(lambda msg: msg.enum.startswith('Handshake'))

response = await send_handshake()

if response.enum == 'HandshakeFailure' and response.HandshakeFailure[1].enum == 'ProtocolVersionMismatch':
pvm = response.HandshakeFailure[1].ProtocolVersionMismatch.version
handshake.Handshake.version = pvm
sign_handshake(key_pair, handshake.Handshake)
await conn.send(handshake)
response = await conn.recv()
response = await send_handshake()

if response.enum == 'HandshakeFailure' and response.HandshakeFailure[1].enum == 'GenesisMismatch':
gm = response.HandshakeFailure[1].GenesisMismatch
handshake.Handshake.chain_info.genesis_id.chain_id = gm.chain_id
handshake.Handshake.chain_info.genesis_id.hash = gm.hash
sign_handshake(key_pair, handshake.Handshake)
await conn.send(handshake)
response = await conn.recv()
response = await send_handshake()

assert response.enum == 'Handshake', response.enum if response.enum != 'HandshakeFailure' else response.HandshakeFailure[1].enum

Expand Down

0 comments on commit 60713c9

Please sign in to comment.