async def test_write_resp_failure():
    s = FakeNetStream()
    # Test: Raise `WriteMessageFailure` if `resp_code` is SUCCESS,
    #   but `msg` is not `ssz.Serializable`.
    with pytest.raises(WriteMessageFailure):
        await write_resp(s, "error msg", ResponseCode.SUCCESS)

    # Test: Raise `WriteMessageFailure` if `resp_code` is not SUCCESS,
    #   but `msg` is not `str`.
    with pytest.raises(WriteMessageFailure):
        await write_resp(s, Status.create(), ResponseCode.INVALID_REQUEST)
Exemple #2
0
 def status_with_wrong_checkpoint(chain):
     return Status.create(
         finalized_root=b"\x78"
         * 32  # finalized root different from another node.
     )
Exemple #3
0
 def status_with_wrong_fork_version(chain):
     return Status.create(
         head_fork_version=b"\x12\x34\x56\x78"  # version different from another node.
     )
            return b""
        # Force to blocking wait for first byte.
        buf.extend(await self._queue.get())
        while not self._queue.empty():
            if n != -1 and len(buf) >= n:
                break
            buf.extend(await self._queue.get())
        return bytes(buf)

    async def write(self, data: bytes) -> int:
        for i in data:
            await self._queue.put(i.to_bytes(1, "big"))
        return len(data)


@pytest.mark.parametrize("msg", (Status.create(), ))
@pytest.mark.asyncio
async def test_read_write_req_msg(msg):
    s = FakeNetStream()
    await write_req(s, msg)
    msg_read = await read_req(s, type(msg))
    assert msg_read == msg


@pytest.mark.parametrize("msg", (Status.create(), ))
@pytest.mark.asyncio
async def test_read_write_resp_msg(msg):
    s = FakeNetStream()
    resp_code = ResponseCode.SUCCESS
    await write_resp(s, msg, resp_code)
    msg_read = await read_resp(s, type(msg))
Exemple #5
0
    WriteMessageFailure,
)
from trinity.protocol.bcc_libp2p.messages import Status
from trinity.protocol.bcc_libp2p.utils import (
    get_blocks_from_canonical_chain_by_slot,
    get_blocks_from_fork_chain_by_root,
    peer_id_from_pubkey,
    read_req,
    read_resp,
    write_req,
    write_resp,
)
from trinity.tools.bcc_factories import BeaconBlockFactory

# Wrong type of `fork_version`, which should be `bytes4`.
invalid_ssz_msg = Status(head_fork_version="1")


def test_peer_id_from_pubkey():
    pubkey = datatypes.PublicKey(
        b"n\x85UD\xe9^\xbfo\x05\xd1z\xbd\xe5k\x87Y\xe9\xfa\xb3z:\xf8z\xc5\xd7K\xa6\x00\xbbc\xda4M\x10\x1cO\x88\tl\x82\x7f\xd7\xec6\xd8\xdc\xe2\x9c\xdcG\xa5\xea|\x9e\xc57\xf8G\xbe}\xfa\x10\xe9\x12"  # noqa: E501
    )
    peer_id_expected = ID.from_base58(
        "QmQiv6sR3qHqhUVgC5qUBVWi8YzM6HknYbu4oQKVAqPCGF")
    assert peer_id_from_pubkey(pubkey) == peer_id_expected


class FakeNetStream:
    _queue: "asyncio.Queue[bytes]"

    class FakeMplexConn(NamedTuple):