def test_byte_vector_invalid_length(value, expected_length): byte_vector = ByteVector(expected_length) with pytest.raises(SerializationError): ssz.encode(value, byte_vector) properly_serialized_value = ssz.encode(value, ByteVector(len(value))) with pytest.raises(DeserializationError): ssz.decode(properly_serialized_value, byte_vector)
def test_byte_vector(value): byte_vector = ByteVector(len(value)) serialized_value = ssz.encode(value, byte_vector) assert serialized_value == ssz.encode( tuple(bytes([byte_value]) for byte_value in value), Vector(byte, len(value))) assert ssz.decode(serialized_value, byte_vector) == value
def prepare_byte_vector_benchmark(): size = 512 repetitions = 10000 byte_vector = ByteVector(size) data = tuple( bytes(random.getrandbits(8) for _ in range(size)) for _ in range(repetitions)) def benchmark(): for data_item in data: ssz.hash_tree_root(data_item, byte_vector) return benchmark
def test_vector_of_composite(bytes16_vector, result): sedes = Vector(ByteVector(16), len(bytes16_vector)) assert ssz.get_hash_tree_root(bytes16_vector, sedes) == result
import itertools import pytest import ssz from ssz.constants import CHUNK_SIZE, EMPTY_CHUNK from ssz.hash import hash_eth2 as h from ssz.sedes import Bitlist, Bitvector, ByteVector, Container, List, Vector, uint128 from ssz.utils import pad_zeros bytes16 = ByteVector(16) EMPTY_BYTES = b"\x00" * 16 A_BYTES = b"\xaa" * 16 B_BYTES = b"\xbb" * 16 C_BYTES = b"\xcc" * 16 D_BYTES = b"\xdd" * 16 E_BYTES = b"\xee" * 16 @pytest.mark.parametrize( ("serialized_uints128", "result"), ( ((A_BYTES,), A_BYTES + EMPTY_BYTES), ((A_BYTES, B_BYTES), A_BYTES + B_BYTES), ((A_BYTES, B_BYTES, C_BYTES), h(A_BYTES + B_BYTES + C_BYTES + EMPTY_BYTES)), ( (A_BYTES, B_BYTES, C_BYTES, D_BYTES, E_BYTES), h(h(A_BYTES + B_BYTES + C_BYTES + D_BYTES) + h(E_BYTES + 3 * EMPTY_BYTES)), ), ), )
) @pytest.mark.parametrize( ("sedes", "size"), ( (boolean, 1), (uint8, 1), (uint256, 32), (Vector(uint8, 0), 0), (Vector(uint8, 2), 2), (Container((uint8, Vector(uint8, 4))), 5), (Vector(List(uint8), 0), 0), (Vector(Container((uint8, Vector(uint8, 4))), 4), 20), (byte, 1), (ByteVector(0), 0), (bytes32, 32), (bytes48, 48), (bytes96, 96), ), ) def test_fixed_size(sedes, size): assert sedes.is_fixed_sized assert sedes.get_fixed_size() == size @pytest.mark.parametrize( "sedes", ( List(uint8), List(Vector(uint8, 2)),
# under the License. from typing import Sequence from ssz.hashable_container import HashableContainer, SignedHashableContainer from ssz.sedes import ( Bitlist, ByteVector, Bitvector, List, Vector, boolean, bytes32, bytes48, bytes96, uint64, ) import sys import argparse if __name__ == "__main__": # Binary read from stdin bs = sys.stdin.buffer.read() # Execute function/method to test hash = ByteVector(len(bs)).get_hash_tree_root(bs) # Write result to stdout in binary sys.stdout.buffer.write(hash)
@pytest.mark.parametrize("value", (b"\x00", b"\x00\x01\x02\x03")) def test_byte_vector(value): byte_vector = ByteVector(len(value)) serialized_value = ssz.encode(value, byte_vector) assert serialized_value == ssz.encode( tuple(bytes([byte_value]) for byte_value in value), Vector(byte, len(value))) assert ssz.decode(serialized_value, byte_vector) == value @pytest.mark.parametrize( ("value", "expected_length"), ((b"", 1), (b"\x00", 2), (b"\x00\x01\x02\x03", 32), (b"\xff" * 64, 32)), ) def test_byte_vector_invalid_length(value, expected_length): byte_vector = ByteVector(expected_length) with pytest.raises(SerializationError): ssz.encode(value, byte_vector) properly_serialized_value = value with pytest.raises(DeserializationError): ssz.decode(properly_serialized_value, byte_vector) @pytest.mark.parametrize(("sedes", "id"), ((byte, "Byte"), (ByteVector(64), "ByteVector64"))) def test_get_sedes_id(sedes, id): assert sedes.get_sedes_id() == id
def test_byte_vector(value): byte_sequence = tuple(bytes([byte_value]) for byte_value in value) expected_vector_root = ssz.hash_tree_root(byte_sequence, Vector(byte, len(value))) assert ssz.hash_tree_root(value, ByteVector(len(value))) == expected_vector_root
def bytevector_sedes_and_values_st(draw): size = draw(st.integers(1, 65)) return ByteVector(size), bytevector_value_st(size)