Esempio n. 1
0
def decode_call_method_response(buf: bytes) -> CallMethodResponse:
    call_method_response_msg = StreamReader(buf)
    if call_method_response_msg.get_size() == 0:
        raise RuntimeError(f'response is corrupt and cannot be decoded')

    # decode request status
    request_status = request_status_decode(
        call_method_response_msg.read_uint16())

    # decode method arguments
    output_arguments = method_arguments_opaque_decode(
        call_method_response_msg.read_bytes())

    # decode execution result
    execution_result = execution_result_decode(
        call_method_response_msg.read_uint16())

    # read block height
    block_height = call_method_response_msg.read_uint64()

    # decode block timestamp
    block_timestamp = unix_nano_to_date(call_method_response_msg.read_uint64())

    return CallMethodResponse(request_status=request_status,
                              execution_result=execution_result,
                              output_arguments=output_arguments,
                              block_height=block_height,
                              block_timestamp=block_timestamp)
def method_arguments_opaque_decode(buf: bytes) -> List[MethodArgument]:
    args = list()
    method_args_reader = StreamReader(buf)

    while not method_args_reader.done_reading():
        arg = StreamReader(method_args_reader.read_bytes())
        name = arg.read_bytes()  # not using the arg name
        arg_type = arg.read_uint16()

        if arg_type == 0:
            val = arg.read_uint32()
            args.append(Uint32Arg(val))
        elif arg_type == 1:
            val = arg.read_uint64()
            args.append(Uint64Arg(val))
        elif arg_type == 2:
            val = arg.read_string()
            args.append(StringArg(val))
        elif arg_type == 3:
            val = arg.read_bytes()
            args.append(BytesArg(val))
        else:
            raise ValueError(
                f'received MethodArgument {name} has an unknown type: {arg_type}'
            )

    return args
Esempio n. 3
0
def decode_send_transaction_response(buf: bytes) -> SendTransactionResponse:
    send_transaction_response_msg = StreamReader(buf)
    if send_transaction_response_msg.get_size() == 0:
        raise RuntimeError(f'response is corrupt and cannot be decoded')

    # decode request status
    request_status = request_status_decode(
        send_transaction_response_msg.read_uint16())

    # read transaction receipt message
    transaction_receipt_msg = StreamReader(
        send_transaction_response_msg.read_bytes())

    # read tx hash
    tx_hash = transaction_receipt_msg.read_bytes()

    # decode execution result
    execution_result = execution_result_decode(
        transaction_receipt_msg.read_uint16())

    # decode method arguments
    output_arguments = method_arguments_opaque_decode(
        transaction_receipt_msg.read_bytes())

    # decode transaction status
    transaction_status = transaction_status_decode(
        send_transaction_response_msg.read_uint16())

    # read block height
    block_height = send_transaction_response_msg.read_uint64()

    # decode block timestamp
    block_timestamp = unix_nano_to_date(
        send_transaction_response_msg.read_uint64())

    return SendTransactionResponse(request_status=request_status,
                                   tx_hash=tx_hash,
                                   execution_result=execution_result,
                                   output_arguments=output_arguments,
                                   transaction_status=transaction_status,
                                   block_height=block_height,
                                   block_timestamp=block_timestamp)
Esempio n. 4
0
 def test_read_uint64(self):
     stream = bytes([0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
     reader = StreamReader(stream)
     expected = 18446744073709551615
     self.assertEqual(expected, reader.read_uint64())
     reader.close()