コード例 #1
0
def padded_varint_serializations_test() -> None:
    for _ in range(500):
        num: int = randint(0, 2**16)

        var_int: bytes = to_var_int(num)
        length: int = len(var_int)

        rpc_var_int: bytes = to_rpc_var_int(num)
        rpc_length: int = len(rpc_var_int)

        before: int = randint(0, 32)
        for i in range(before):
            var_int = bytes([randint(0, 255)]) + var_int
            rpc_var_int = bytes([randint(0, 255)]) + rpc_var_int
        for i in range(randint(0, 32)):
            var_int += bytes([randint(0, 255)])
            rpc_var_int += bytes([randint(0, 255)])

        res: Tuple[int, int] = from_var_int(var_int, before)
        assert res[0] == num
        assert res[1] == before + length

        rpc_res: Tuple[int, int] = from_rpc_var_int(rpc_var_int, before)
        assert res[0] == num
        assert res[1] == before + length
コード例 #2
0
def rpc_binary_serialize(data: Any) -> bytes:
    """Serialize an object according to epee for the binary RPC calls."""

    if data is None:
        return bytes()

    result: bytes = bytes.fromhex("011101010101020101") + to_rpc_var_int(len(data))
    for field in data:
        result += bytes([len(field)]) + field.encode("utf-8")
        result += rpc_binary_serialize_internal(data[field])
    return result
コード例 #3
0
def rpc_binary_serialize_internal(data: Any, code: int = -1) -> bytes:
    """Serialize data according to epee for the binary RPC calls."""

    result: bytes = bytes()
    if code == -1:
        code = data[0]
        result = bytes([code])
        data = data[1]

    if code in binary_lookup:
        result += binary_lookup[code].pack(data)
    elif code == 10:
        result += to_rpc_var_int(len(data)) + data
    elif code == 12:
        result += to_rpc_var_int(len(data))
        for field in data:
            result += bytes([len(field)]) + field.encode("utf-8")
            result += rpc_binary_serialize_internal(data[field])
    elif code & 0x80 == 0x80:
        result += to_rpc_var_int(len(data))
        for elem in data:
            result += rpc_binary_serialize_internal(elem, code & 0b1111)
    return result
コード例 #4
0
def varint_serializations_test() -> None:
    for _ in range(500):
        num: int = randint(0, 2**16)
        assert from_var_int(to_var_int(num), 0)[0] == num
        assert from_rpc_var_int(to_rpc_var_int(num), 0)[0] == num