From 5989e5cd2368a5e7b4913bcfa6ef5025019b0aa7 Mon Sep 17 00:00:00 2001 From: protolambda Date: Mon, 24 Jun 2019 23:56:26 +0200 Subject: [PATCH] use Bool as base name, make Bit an alias --- scripts/build_spec.py | 6 +++--- specs/core/0_beacon-chain.md | 4 ++-- test_libs/pyspec/eth2spec/debug/decode.py | 4 ++-- test_libs/pyspec/eth2spec/debug/encode.py | 4 ++-- .../pyspec/eth2spec/debug/random_value.py | 8 ++++---- test_libs/pyspec/eth2spec/fuzzing/decoder.py | 4 ++-- .../pyspec/eth2spec/utils/ssz/ssz_impl.py | 6 +++--- .../pyspec/eth2spec/utils/ssz/ssz_typing.py | 7 ++++++- .../pyspec/eth2spec/utils/ssz/test_ssz_impl.py | 11 ++++++++--- .../eth2spec/utils/ssz/test_ssz_typing.py | 18 +++++++++++------- 10 files changed, 43 insertions(+), 29 deletions(-) diff --git a/scripts/build_spec.py b/scripts/build_spec.py index 0b55dfa44..08793869f 100644 --- a/scripts/build_spec.py +++ b/scripts/build_spec.py @@ -25,7 +25,7 @@ from eth2spec.utils.ssz.ssz_impl import ( signing_root, ) from eth2spec.utils.ssz.ssz_typing import ( - Bit, Container, List, Vector, Bytes, uint64, + Bit, Bool, Container, List, Vector, Bytes, uint64, Bytes4, Bytes32, Bytes48, Bytes96, ) from eth2spec.utils.bls import ( @@ -53,7 +53,7 @@ from eth2spec.utils.ssz.ssz_impl import ( is_empty, ) from eth2spec.utils.ssz.ssz_typing import ( - Bit, Container, List, Vector, Bytes, uint64, + Bit, Bool, Container, List, Vector, Bytes, uint64, Bytes4, Bytes32, Bytes48, Bytes96, ) from eth2spec.utils.bls import ( @@ -179,7 +179,7 @@ def combine_constants(old_constants: Dict[str, str], new_constants: Dict[str, st ignored_dependencies = [ - 'Bit', 'Vector', 'List', 'Container', 'Hash', 'BLSPubkey', 'BLSSignature', 'Bytes', 'BytesN' + 'Bit', 'Bool', 'Vector', 'List', 'Container', 'Hash', 'BLSPubkey', 'BLSSignature', 'Bytes', 'BytesN' 'Bytes4', 'Bytes32', 'Bytes48', 'Bytes96', 'uint8', 'uint16', 'uint32', 'uint64', 'uint128', 'uint256', 'bytes' # to be removed after updating spec doc diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index 7d2459eb3..b3e3ba0ea 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -314,7 +314,7 @@ class Validator(Container): pubkey: BLSPubkey withdrawal_credentials: Hash # Commitment to pubkey for withdrawals and transfers effective_balance: Gwei # Balance at stake - slashed: Bit + slashed: Bool # Status epochs activation_eligibility_epoch: Epoch # When criteria for activation were met activation_epoch: Epoch @@ -354,7 +354,7 @@ class AttestationData(Container): ```python class AttestationDataAndCustodyBit(Container): data: AttestationData - custody_bit: Bit # Challengeable bit for the custody of crosslink data + custody_bit: Bit # Challengeable bit (SSZ-bool, 1 byte) for the custody of crosslink data ``` #### `IndexedAttestation` diff --git a/test_libs/pyspec/eth2spec/debug/decode.py b/test_libs/pyspec/eth2spec/debug/decode.py index 743479371..c0b53b0ef 100644 --- a/test_libs/pyspec/eth2spec/debug/decode.py +++ b/test_libs/pyspec/eth2spec/debug/decode.py @@ -1,13 +1,13 @@ from typing import Any from eth2spec.utils.ssz.ssz_impl import hash_tree_root from eth2spec.utils.ssz.ssz_typing import ( - SSZType, SSZValue, uint, Container, Bytes, List, Bit, + SSZType, SSZValue, uint, Container, Bytes, List, Bool, Vector, BytesN ) def decode(data: Any, typ: SSZType) -> SSZValue: - if issubclass(typ, (uint, Bit)): + if issubclass(typ, (uint, Bool)): return typ(data) elif issubclass(typ, (List, Vector)): return typ(decode(element, typ.elem_type) for element in data) diff --git a/test_libs/pyspec/eth2spec/debug/encode.py b/test_libs/pyspec/eth2spec/debug/encode.py index 0878a1f94..02814e441 100644 --- a/test_libs/pyspec/eth2spec/debug/encode.py +++ b/test_libs/pyspec/eth2spec/debug/encode.py @@ -1,6 +1,6 @@ from eth2spec.utils.ssz.ssz_impl import hash_tree_root from eth2spec.utils.ssz.ssz_typing import ( - SSZValue, uint, Container, Bit + SSZValue, uint, Container, Bool ) @@ -10,7 +10,7 @@ def encode(value: SSZValue, include_hash_tree_roots=False): if value.type().byte_len > 8: return str(int(value)) return int(value) - elif isinstance(value, Bit): + elif isinstance(value, Bool): return value == 1 elif isinstance(value, list): # normal python lists, ssz-List, Vector return [encode(element, include_hash_tree_roots) for element in value] diff --git a/test_libs/pyspec/eth2spec/debug/random_value.py b/test_libs/pyspec/eth2spec/debug/random_value.py index 8e13cd5e1..c6efb722b 100644 --- a/test_libs/pyspec/eth2spec/debug/random_value.py +++ b/test_libs/pyspec/eth2spec/debug/random_value.py @@ -2,7 +2,7 @@ from random import Random from enum import Enum from eth2spec.utils.ssz.ssz_typing import ( - SSZType, SSZValue, BasicValue, BasicType, uint, Container, Bytes, List, Bit, + SSZType, SSZValue, BasicValue, BasicType, uint, Container, Bytes, List, Bool, Vector, BytesN ) @@ -118,7 +118,7 @@ def get_random_bytes_list(rng: Random, length: int) -> bytes: def get_random_basic_value(rng: Random, typ: BasicType) -> BasicValue: - if issubclass(typ, Bit): + if issubclass(typ, Bool): return typ(rng.choice((True, False))) elif issubclass(typ, uint): assert typ.byte_len in UINT_BYTE_SIZES @@ -128,7 +128,7 @@ def get_random_basic_value(rng: Random, typ: BasicType) -> BasicValue: def get_min_basic_value(typ: BasicType) -> BasicValue: - if issubclass(typ, Bit): + if issubclass(typ, Bool): return typ(False) elif issubclass(typ, uint): assert typ.byte_len in UINT_BYTE_SIZES @@ -138,7 +138,7 @@ def get_min_basic_value(typ: BasicType) -> BasicValue: def get_max_basic_value(typ: BasicType) -> BasicValue: - if issubclass(typ, Bit): + if issubclass(typ, Bool): return typ(True) elif issubclass(typ, uint): assert typ.byte_len in UINT_BYTE_SIZES diff --git a/test_libs/pyspec/eth2spec/fuzzing/decoder.py b/test_libs/pyspec/eth2spec/fuzzing/decoder.py index a4ebb7e70..e533ca5c2 100644 --- a/test_libs/pyspec/eth2spec/fuzzing/decoder.py +++ b/test_libs/pyspec/eth2spec/fuzzing/decoder.py @@ -19,7 +19,7 @@ def translate_typ(typ) -> ssz.BaseSedes: return ssz.Vector(translate_typ(typ.elem_type), typ.length) elif issubclass(typ, spec_ssz.List): return ssz.List(translate_typ(typ.elem_type)) - elif issubclass(typ, spec_ssz.Bit): + elif issubclass(typ, spec_ssz.Bool): return ssz.boolean elif issubclass(typ, spec_ssz.uint): if typ.byte_len == 1: @@ -64,7 +64,7 @@ def translate_value(value, typ): raise TypeError("invalid uint size") elif issubclass(typ, spec_ssz.List): return [translate_value(elem, typ.elem_type) for elem in value] - elif issubclass(typ, spec_ssz.Bit): + elif issubclass(typ, spec_ssz.Bool): return value elif issubclass(typ, spec_ssz.Vector): return typ(*(translate_value(elem, typ.elem_type) for elem in value)) diff --git a/test_libs/pyspec/eth2spec/utils/ssz/ssz_impl.py b/test_libs/pyspec/eth2spec/utils/ssz/ssz_impl.py index e2a264955..b9c7b6d38 100644 --- a/test_libs/pyspec/eth2spec/utils/ssz/ssz_impl.py +++ b/test_libs/pyspec/eth2spec/utils/ssz/ssz_impl.py @@ -1,7 +1,7 @@ from ..merkle_minimal import merkleize_chunks from ..hash_function import hash from .ssz_typing import ( - SSZValue, SSZType, BasicValue, BasicType, Series, Elements, Bit, Container, List, Bytes, uint, + SSZValue, SSZType, BasicValue, BasicType, Series, Elements, Bool, Container, List, Bytes, uint, ) # SSZ Serialization @@ -13,7 +13,7 @@ BYTES_PER_LENGTH_OFFSET = 4 def serialize_basic(value: SSZValue): if isinstance(value, uint): return value.to_bytes(value.type().byte_len, 'little') - elif isinstance(value, Bit): + elif isinstance(value, Bool): if value: return b'\x01' else: @@ -25,7 +25,7 @@ def serialize_basic(value: SSZValue): def deserialize_basic(value, typ: BasicType): if issubclass(typ, uint): return typ(int.from_bytes(value, 'little')) - elif issubclass(typ, Bit): + elif issubclass(typ, Bool): assert value in (b'\x00', b'\x01') return typ(value == b'\x01') else: diff --git a/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py b/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py index 891480a5c..6079f2866 100644 --- a/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py +++ b/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py @@ -31,7 +31,7 @@ class BasicValue(int, SSZValue, metaclass=BasicType): pass -class Bit(BasicValue): # can't subclass bool. +class Bool(BasicValue): # can't subclass bool. byte_len = 1 def __new__(cls, value, *args, **kwargs): @@ -47,6 +47,11 @@ class Bit(BasicValue): # can't subclass bool. return self > 0 +# Alias for Bool +class Bit(Bool): + pass + + class uint(BasicValue, metaclass=BasicType): def __new__(cls, value, *args, **kwargs): diff --git a/test_libs/pyspec/eth2spec/utils/ssz/test_ssz_impl.py b/test_libs/pyspec/eth2spec/utils/ssz/test_ssz_impl.py index ae0849098..aa7aee64a 100644 --- a/test_libs/pyspec/eth2spec/utils/ssz/test_ssz_impl.py +++ b/test_libs/pyspec/eth2spec/utils/ssz/test_ssz_impl.py @@ -1,6 +1,6 @@ from .ssz_impl import serialize, hash_tree_root from .ssz_typing import ( - Bit, Container, List, Vector, Bytes, BytesN, + Bit, Bool, Container, List, Vector, Bytes, BytesN, uint8, uint16, uint32, uint64, byte ) @@ -47,11 +47,16 @@ for k, v in {0: 1, 32: 2, 64: 3, 95: 0xff}.items(): sig_test_data[k] = v test_data = [ - ("bool F", Bit(False), "00"), - ("bool T", Bit(True), "01"), + ("bit F", Bit(False), "00"), + ("bit T", Bit(True), "01"), + ("bool F", Bool(False), "00"), + ("bool T", Bool(True), "01"), ("uint8 00", uint8(0x00), "00"), ("uint8 01", uint8(0x01), "01"), ("uint8 ab", uint8(0xab), "ab"), + ("byte 00", byte(0x00), "00"), + ("byte 01", byte(0x01), "01"), + ("byte ab", byte(0xab), "ab"), ("uint16 0000", uint16(0x0000), "0000"), ("uint16 abcd", uint16(0xabcd), "cdab"), ("uint32 00000000", uint32(0x00000000), "00000000"), diff --git a/test_libs/pyspec/eth2spec/utils/ssz/test_ssz_typing.py b/test_libs/pyspec/eth2spec/utils/ssz/test_ssz_typing.py index bd86a5806..2af742360 100644 --- a/test_libs/pyspec/eth2spec/utils/ssz/test_ssz_typing.py +++ b/test_libs/pyspec/eth2spec/utils/ssz/test_ssz_typing.py @@ -1,7 +1,7 @@ from .ssz_typing import ( SSZValue, SSZType, BasicValue, BasicType, Series, ElementsType, - Elements, Bit, Container, List, Vector, Bytes, BytesN, - uint, uint8, uint16, uint32, uint64, uint128, uint256, + Elements, Bit, Bool, Container, List, Vector, Bytes, BytesN, + byte, uint, uint8, uint16, uint32, uint64, uint128, uint256, Bytes32, Bytes48 ) @@ -22,8 +22,8 @@ def test_subclasses(): assert issubclass(u, SSZValue) assert isinstance(u, SSZType) assert isinstance(u, BasicType) - assert issubclass(Bit, BasicValue) - assert isinstance(Bit, BasicType) + assert issubclass(Bool, BasicValue) + assert isinstance(Bool, BasicType) for c in [Container, List, Vector, Bytes, BytesN]: assert issubclass(c, Series) @@ -38,21 +38,25 @@ def test_subclasses(): def test_basic_instances(): - for u in [uint, uint8, uint16, uint32, uint64, uint128, uint256]: + for u in [uint, uint8, byte, uint16, uint32, uint64, uint128, uint256]: v = u(123) assert isinstance(v, uint) assert isinstance(v, int) assert isinstance(v, BasicValue) assert isinstance(v, SSZValue) - assert isinstance(Bit(True), BasicValue) - assert isinstance(Bit(False), BasicValue) + assert isinstance(Bool(True), BasicValue) + assert isinstance(Bool(False), BasicValue) + assert isinstance(Bit(True), Bool) + assert isinstance(Bit(False), Bool) def test_basic_value_bounds(): max = { + Bool: 2 ** 1, Bit: 2 ** 1, uint8: 2 ** (8 * 1), + byte: 2 ** (8 * 1), uint16: 2 ** (8 * 2), uint32: 2 ** (8 * 4), uint64: 2 ** (8 * 8),