Example #1
0
 def test_read_corrupt_string(self):
     stream = bytes(
         [0x05, 0x00, 0x00, 0x00,
          ord('h'),
          ord('e'),
          ord('l'),
          ord('l')])
     reader = StreamReader(stream)
     self.assertRaises(RuntimeError, reader.read_string)
     reader.close()
Example #2
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)
Example #3
0
 def test_read_bytes(self):
     stream = bytes([
         0x03, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x00, 0x05, 0x00, 0x00,
         0x00, 0x01, 0x02, 0x03, 0x04, 0x05
     ])
     reader = StreamReader(stream)
     expected1 = bytes([0x01, 0x02, 0x03])
     self.assertEqual(expected1, reader.read_bytes())
     expected2 = bytes([0x01, 0x02, 0x03, 0x04, 0x05])
     self.assertEqual(expected2, reader.read_bytes())
     reader.close()
Example #4
0
 def test_read_string(self):
     stream = bytes([
         0x05, 0x00, 0x00, 0x00,
         ord('h'),
         ord('e'),
         ord('l'),
         ord('l'),
         ord('o'), 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
         ord('w'),
         ord('o'),
         ord('r'),
         ord('l'),
         ord('d'),
         ord('!')
     ])
     reader = StreamReader(stream)
     expected1 = 'hello'
     self.assertEqual(expected1, reader.read_string())
     expected2 = 'world!'
     self.assertEqual(expected2, reader.read_string())
     reader.close()
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
Example #6
0
 def test_read_corrupt_bytes(self):
     stream = bytes([0x03, 0x00, 0x00, 0x00, 0x01, 0x02])
     reader = StreamReader(stream)
     self.assertRaises(RuntimeError, reader.read_bytes)
     reader.close()
Example #7
0
 def test_read_uint8(self):
     stream = bytes([0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
     reader = StreamReader(stream)
     expected = 255
     self.assertEqual(expected, reader.read_uint8())
     reader.close()
Example #8
0
 def test_read_corrupt_uint64(self):
     stream = bytes([0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
     reader = StreamReader(stream)
     self.assertRaises(RuntimeError, reader.read_uint64)
     reader.close()
Example #9
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()
Example #10
0
 def test_read_corrupt_uint8(self):
     stream = bytes()
     reader = StreamReader(stream)
     self.assertRaises(RuntimeError, reader.read_uint8)
     reader.close()
Example #11
0
def decode_get_transaction_status_response(buf: bytes) -> GetTransactionStatusResponse:
    get_transaction_status_response_msg = StreamReader(buf)
    if get_transaction_status_response_msg.get_size() == 0:
        raise RuntimeError(f'response is corrupt and cannot be decoded')

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

    # read transaction receipt message
    transaction_receipt_msg = StreamReader(get_transaction_status_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(get_transaction_status_response_msg.read_uint16())

    # read block height
    block_height = get_transaction_status_response_msg.read_uint64()

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

    return GetTransactionStatusResponse(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)