Пример #1
0
def test_bytes_n(data, length):
    sedes = BytesN(length)
    value = data.draw(st.binary(
        min_size=length,
        max_size=length,
    ))
    assert len(hash_tree_root(value, sedes)) == 32
Пример #2
0
def test_bytes_n_list_randomized(data, length, sequence_type):
    sedes = List(BytesN(length))
    items = data.draw(
        st.lists(
            st.binary(
                min_size=length,
                max_size=length,
            )
        )
    )
    value = sequence_type(items)
    assert len(hash_tree_root(value, sedes)) == 32
Пример #3
0
def test_round_trip():
    for num_bytes in range(1, 33):
        value = b'\x01' * num_bytes
        sedes_obj = BytesN(num_bytes)
        assert sedes_obj.deserialize(sedes_obj.serialize(value)) == value
Пример #4
0
def test_hash_deserialize_values():
    for num_bytes in range(1, 33):
        value = b'\x01' * num_bytes
        assert BytesN(num_bytes).deserialize(value) == value
Пример #5
0
def test_reject_object_with_negative_bytes(num_bytes):
    with pytest.raises(ValueError):
        BytesN(num_bytes)
Пример #6
0
)
def test_reject_object_with_negative_bytes(num_bytes):
    with pytest.raises(ValueError):
        BytesN(num_bytes)


def test_serialize_values():
    for num_bytes in range(1, 33):
        value = b'\x01' * num_bytes
        assert BytesN(num_bytes).serialize(value) == value


@pytest.mark.parametrize(
    'value,sedes',
    (
        (b'\x01' * 32, BytesN(16)),
        (b'\x01' * 32, BytesN(20)),
        (b'\x01' * 16, BytesN(20)),
        (b'\x01' * 16, bytes32),
        (b'\x01' * 16, bytes48),
        (b'\x01' * 16, bytes96),
        (b'\x01' * 32, BytesN(20)),
    ),
)
def test_serialize_bad_values(value, sedes):
    with pytest.raises(SerializationError):
        sedes.serialize(value)


def test_hash_deserialize_values():
    for num_bytes in range(1, 33):
Пример #7
0
import ssz
from ssz.sedes import (
    BytesN,
    uint64,
)

from eth2.beacon.typing import (
    Epoch, )

bytes4 = BytesN(4)


class Fork(ssz.Serializable):

    fields = [
        # Previous fork version
        ('previous_version', bytes4),
        # Current fork version
        ('current_version', bytes4),
        # Fork epoch number
        ('epoch', uint64)
    ]

    def __init__(self, previous_version: bytes, current_version: bytes,
                 epoch: Epoch) -> None:
        super().__init__(
            previous_version=previous_version,
            current_version=current_version,
            epoch=epoch,
        )