Ejemplo n.º 1
0
async def main():
    my_key_pair_nacl = nacl.signing.SigningKey.generate()

    conn = await connect(nodes[0].addr())
    handshake = create_handshake(my_key_pair_nacl, nodes[0].node_key.pk, 12345)
    handshake.Handshake.edge_info.nonce = 1505

    # First handshake attempt. Should fail with Genesis Mismatch
    sign_handshake(my_key_pair_nacl, handshake.Handshake)
    await conn.send(handshake)
    response = await conn.recv()
    assert response.enum == 'HandshakeFailure', response.enum
    assert response.HandshakeFailure[
        1].enum == 'ProtocolVersionMismatch', response.HandshakeFailure[1].enum
    pvm = response.HandshakeFailure[1].ProtocolVersionMismatch.version
    handshake.Handshake.version = pvm

    # Second handshake attempt. Should fail with Protocol Version Mismatch
    sign_handshake(my_key_pair_nacl, handshake.Handshake)
    await conn.send(handshake)
    response = await conn.recv()
    assert response.enum == 'HandshakeFailure', response.enum
    assert response.HandshakeFailure[
        1].enum == 'GenesisMismatch', response.HandshakeFailure[1].enum
    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

    # Third handshake attempt.
    sign_handshake(my_key_pair_nacl, handshake.Handshake)
    await conn.send(handshake)
    response = await conn.recv()

    # Connection should be closed by other peer because too large nonce on handshake.
    assert response is None
Ejemplo n.º 2
0
async def main():
    my_key_pair_nacl = nacl.signing.SigningKey.generate()

    conn = await connect(nodes[0].addr())

    # First handshake attempt. Should fail with Genesis Mismatch
    handshake = create_handshake(my_key_pair_nacl, nodes[0].node_key.pk, 12345)
    sign_handshake(my_key_pair_nacl, handshake.Handshake)

    await conn.send(handshake)
    response = await conn.recv()

    assert response.enum == 'HandshakeFailure', response.enum
    assert response.HandshakeFailure[
        1].enum == 'GenesisMismatch', response.HandshakeFailure[1].enum

    # Second handshake attempt. Should fail with Protocol Version Mismatch
    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(my_key_pair_nacl, handshake.Handshake)

    await conn.send(handshake)
    response = await conn.recv()

    assert response.enum == 'HandshakeFailure', response.enum
    assert response.HandshakeFailure[
        1].enum == 'ProtocolVersionMismatch', response.HandshakeFailure[1].enum

    # Third handshake attempt. Should succeed
    pvm = response.HandshakeFailure[1].ProtocolVersionMismatch
    handshake.Handshake.version = pvm
    sign_handshake(my_key_pair_nacl, handshake.Handshake)

    await conn.send(handshake)
    response = await conn.recv()

    assert response.enum == 'Handshake', response.enum
    assert response.Handshake.chain_info.genesis_id.chain_id == handshake.Handshake.chain_info.genesis_id.chain_id
    assert response.Handshake.chain_info.genesis_id.hash == handshake.Handshake.chain_info.genesis_id.hash
    assert response.Handshake.edge_info.nonce == 1
    assert response.Handshake.peer_id.keyType == 0
    assert response.Handshake.peer_id.data == base58.b58decode(
        nodes[0].node_key.pk[len(ED_PREFIX):])
    assert response.Handshake.target_peer_id.keyType == 0
    assert response.Handshake.target_peer_id.data == bytes(
        my_key_pair_nacl.verify_key)
    assert response.Handshake.version == handshake.Handshake.version
    assert response.Handshake.listen_port == nodes[0].addr()[1]