def test_binary_fixed_lenght_of_zero(): sedes = Binary.fixed_length(0) assert sedes.serialize(b'') == b'' with pytest.raises(SerializationError): sedes.serialize(b'a') with pytest.raises(SerializationError): sedes.serialize(b'arst')
""" util to benchmark known usecase """ import random import rlp_cython as rlp from rlp_cython.sedes import big_endian_int, BigEndianInt, Binary, binary, CountableList from rlp_cython.sedes.serializable import Serializable import time address = Binary.fixed_length(20, allow_empty=True) int20 = BigEndianInt(20) int32 = BigEndianInt(32) int256 = BigEndianInt(256) hash32 = Binary.fixed_length(32) trie_root = Binary.fixed_length(32, allow_empty=True) def zpad(x, l): return b'\x00' * max(0, l - len(x)) + x class Transaction(Serializable): fields = [ ('nonce', big_endian_int), ('gasprice', big_endian_int), ('startgas', big_endian_int), ('to', address), ('value', big_endian_int), ('data', binary), ('v', big_endian_int), ('r', big_endian_int),
from rlp_cython.sedes import ( BigEndianInt, Binary, ) from hvm.constants import ( COLLATION_SIZE, ) address = Binary.fixed_length(20, allow_empty=True) collation_body = Binary.fixed_length(COLLATION_SIZE) hash32 = Binary.fixed_length(32) int32 = BigEndianInt(32) int256 = BigEndianInt(256) trie_root = Binary.fixed_length(32, allow_empty=True)
def test_binary_min_length_serialization(value, expected): sedes = Binary(min_length=3) assert sedes.serialize(value) == expected
def test_binary_variable_length_serialization_wrong_length(value): sedes = Binary(2, 4) with pytest.raises(SerializationError): sedes.serialize(value)
def test_binary_variable_length_serialization(value, expected): sedes = Binary(2, 4) assert sedes.serialize(value) == expected
def test_binary_fixed_length_serialization_with_wrong_length(value): sedes = Binary.fixed_length(5) with pytest.raises(SerializationError): sedes.serialize(value)
def test_binary_fixed_length_serialization(value, expected): sedes = Binary.fixed_length(5) assert sedes.serialize(value) == expected
def test_binary_unserializable_values(value): sedes = Binary() with pytest.raises(SerializationError): sedes.serialize(value)
def test_simple_binary_serialization(value, expected): sedes = Binary() assert sedes.serialize(value) == expected
def test_binary_min_and_max_length_with_allow_empty_wrong_length(value): sedes = Binary(min_length=3, max_length=5, allow_empty=True) with pytest.raises(SerializationError): sedes.serialize(value)
def test_binary_min_and_max_length_with_allow_empty(value, expected): sedes = Binary(min_length=3, max_length=5, allow_empty=True) assert sedes.serialize(value) == expected
def test_binary_max_length_serialization_wrong_length(value): sedes = Binary(max_length=3) with pytest.raises(SerializationError): sedes.serialize(value)