use Bool as base name, make Bit an alias

This commit is contained in:
protolambda 2019-06-24 23:56:26 +02:00
parent c73417b4ca
commit 5989e5cd23
No known key found for this signature in database
GPG Key ID: EC89FDBB2B4C7623
10 changed files with 43 additions and 29 deletions

View File

@ -25,7 +25,7 @@ from eth2spec.utils.ssz.ssz_impl import (
signing_root, signing_root,
) )
from eth2spec.utils.ssz.ssz_typing import ( from eth2spec.utils.ssz.ssz_typing import (
Bit, Container, List, Vector, Bytes, uint64, Bit, Bool, Container, List, Vector, Bytes, uint64,
Bytes4, Bytes32, Bytes48, Bytes96, Bytes4, Bytes32, Bytes48, Bytes96,
) )
from eth2spec.utils.bls import ( from eth2spec.utils.bls import (
@ -53,7 +53,7 @@ from eth2spec.utils.ssz.ssz_impl import (
is_empty, is_empty,
) )
from eth2spec.utils.ssz.ssz_typing import ( from eth2spec.utils.ssz.ssz_typing import (
Bit, Container, List, Vector, Bytes, uint64, Bit, Bool, Container, List, Vector, Bytes, uint64,
Bytes4, Bytes32, Bytes48, Bytes96, Bytes4, Bytes32, Bytes48, Bytes96,
) )
from eth2spec.utils.bls import ( from eth2spec.utils.bls import (
@ -179,7 +179,7 @@ def combine_constants(old_constants: Dict[str, str], new_constants: Dict[str, st
ignored_dependencies = [ 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', 'Bytes4', 'Bytes32', 'Bytes48', 'Bytes96',
'uint8', 'uint16', 'uint32', 'uint64', 'uint128', 'uint256', 'uint8', 'uint16', 'uint32', 'uint64', 'uint128', 'uint256',
'bytes' # to be removed after updating spec doc 'bytes' # to be removed after updating spec doc

View File

@ -314,7 +314,7 @@ class Validator(Container):
pubkey: BLSPubkey pubkey: BLSPubkey
withdrawal_credentials: Hash # Commitment to pubkey for withdrawals and transfers withdrawal_credentials: Hash # Commitment to pubkey for withdrawals and transfers
effective_balance: Gwei # Balance at stake effective_balance: Gwei # Balance at stake
slashed: Bit slashed: Bool
# Status epochs # Status epochs
activation_eligibility_epoch: Epoch # When criteria for activation were met activation_eligibility_epoch: Epoch # When criteria for activation were met
activation_epoch: Epoch activation_epoch: Epoch
@ -354,7 +354,7 @@ class AttestationData(Container):
```python ```python
class AttestationDataAndCustodyBit(Container): class AttestationDataAndCustodyBit(Container):
data: AttestationData 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` #### `IndexedAttestation`

View File

@ -1,13 +1,13 @@
from typing import Any from typing import Any
from eth2spec.utils.ssz.ssz_impl import hash_tree_root from eth2spec.utils.ssz.ssz_impl import hash_tree_root
from eth2spec.utils.ssz.ssz_typing import ( from eth2spec.utils.ssz.ssz_typing import (
SSZType, SSZValue, uint, Container, Bytes, List, Bit, SSZType, SSZValue, uint, Container, Bytes, List, Bool,
Vector, BytesN Vector, BytesN
) )
def decode(data: Any, typ: SSZType) -> SSZValue: def decode(data: Any, typ: SSZType) -> SSZValue:
if issubclass(typ, (uint, Bit)): if issubclass(typ, (uint, Bool)):
return typ(data) return typ(data)
elif issubclass(typ, (List, Vector)): elif issubclass(typ, (List, Vector)):
return typ(decode(element, typ.elem_type) for element in data) return typ(decode(element, typ.elem_type) for element in data)

View File

@ -1,6 +1,6 @@
from eth2spec.utils.ssz.ssz_impl import hash_tree_root from eth2spec.utils.ssz.ssz_impl import hash_tree_root
from eth2spec.utils.ssz.ssz_typing import ( 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: if value.type().byte_len > 8:
return str(int(value)) return str(int(value))
return int(value) return int(value)
elif isinstance(value, Bit): elif isinstance(value, Bool):
return value == 1 return value == 1
elif isinstance(value, list): # normal python lists, ssz-List, Vector elif isinstance(value, list): # normal python lists, ssz-List, Vector
return [encode(element, include_hash_tree_roots) for element in value] return [encode(element, include_hash_tree_roots) for element in value]

View File

@ -2,7 +2,7 @@ from random import Random
from enum import Enum from enum import Enum
from eth2spec.utils.ssz.ssz_typing import ( 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 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: def get_random_basic_value(rng: Random, typ: BasicType) -> BasicValue:
if issubclass(typ, Bit): if issubclass(typ, Bool):
return typ(rng.choice((True, False))) return typ(rng.choice((True, False)))
elif issubclass(typ, uint): elif issubclass(typ, uint):
assert typ.byte_len in UINT_BYTE_SIZES 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: def get_min_basic_value(typ: BasicType) -> BasicValue:
if issubclass(typ, Bit): if issubclass(typ, Bool):
return typ(False) return typ(False)
elif issubclass(typ, uint): elif issubclass(typ, uint):
assert typ.byte_len in UINT_BYTE_SIZES 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: def get_max_basic_value(typ: BasicType) -> BasicValue:
if issubclass(typ, Bit): if issubclass(typ, Bool):
return typ(True) return typ(True)
elif issubclass(typ, uint): elif issubclass(typ, uint):
assert typ.byte_len in UINT_BYTE_SIZES assert typ.byte_len in UINT_BYTE_SIZES

View File

@ -19,7 +19,7 @@ def translate_typ(typ) -> ssz.BaseSedes:
return ssz.Vector(translate_typ(typ.elem_type), typ.length) return ssz.Vector(translate_typ(typ.elem_type), typ.length)
elif issubclass(typ, spec_ssz.List): elif issubclass(typ, spec_ssz.List):
return ssz.List(translate_typ(typ.elem_type)) return ssz.List(translate_typ(typ.elem_type))
elif issubclass(typ, spec_ssz.Bit): elif issubclass(typ, spec_ssz.Bool):
return ssz.boolean return ssz.boolean
elif issubclass(typ, spec_ssz.uint): elif issubclass(typ, spec_ssz.uint):
if typ.byte_len == 1: if typ.byte_len == 1:
@ -64,7 +64,7 @@ def translate_value(value, typ):
raise TypeError("invalid uint size") raise TypeError("invalid uint size")
elif issubclass(typ, spec_ssz.List): elif issubclass(typ, spec_ssz.List):
return [translate_value(elem, typ.elem_type) for elem in value] 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 return value
elif issubclass(typ, spec_ssz.Vector): elif issubclass(typ, spec_ssz.Vector):
return typ(*(translate_value(elem, typ.elem_type) for elem in value)) return typ(*(translate_value(elem, typ.elem_type) for elem in value))

View File

@ -1,7 +1,7 @@
from ..merkle_minimal import merkleize_chunks from ..merkle_minimal import merkleize_chunks
from ..hash_function import hash from ..hash_function import hash
from .ssz_typing import ( 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 # SSZ Serialization
@ -13,7 +13,7 @@ BYTES_PER_LENGTH_OFFSET = 4
def serialize_basic(value: SSZValue): def serialize_basic(value: SSZValue):
if isinstance(value, uint): if isinstance(value, uint):
return value.to_bytes(value.type().byte_len, 'little') return value.to_bytes(value.type().byte_len, 'little')
elif isinstance(value, Bit): elif isinstance(value, Bool):
if value: if value:
return b'\x01' return b'\x01'
else: else:
@ -25,7 +25,7 @@ def serialize_basic(value: SSZValue):
def deserialize_basic(value, typ: BasicType): def deserialize_basic(value, typ: BasicType):
if issubclass(typ, uint): if issubclass(typ, uint):
return typ(int.from_bytes(value, 'little')) return typ(int.from_bytes(value, 'little'))
elif issubclass(typ, Bit): elif issubclass(typ, Bool):
assert value in (b'\x00', b'\x01') assert value in (b'\x00', b'\x01')
return typ(value == b'\x01') return typ(value == b'\x01')
else: else:

View File

@ -31,7 +31,7 @@ class BasicValue(int, SSZValue, metaclass=BasicType):
pass pass
class Bit(BasicValue): # can't subclass bool. class Bool(BasicValue): # can't subclass bool.
byte_len = 1 byte_len = 1
def __new__(cls, value, *args, **kwargs): def __new__(cls, value, *args, **kwargs):
@ -47,6 +47,11 @@ class Bit(BasicValue): # can't subclass bool.
return self > 0 return self > 0
# Alias for Bool
class Bit(Bool):
pass
class uint(BasicValue, metaclass=BasicType): class uint(BasicValue, metaclass=BasicType):
def __new__(cls, value, *args, **kwargs): def __new__(cls, value, *args, **kwargs):

View File

@ -1,6 +1,6 @@
from .ssz_impl import serialize, hash_tree_root from .ssz_impl import serialize, hash_tree_root
from .ssz_typing import ( from .ssz_typing import (
Bit, Container, List, Vector, Bytes, BytesN, Bit, Bool, Container, List, Vector, Bytes, BytesN,
uint8, uint16, uint32, uint64, byte 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 sig_test_data[k] = v
test_data = [ test_data = [
("bool F", Bit(False), "00"), ("bit F", Bit(False), "00"),
("bool T", Bit(True), "01"), ("bit T", Bit(True), "01"),
("bool F", Bool(False), "00"),
("bool T", Bool(True), "01"),
("uint8 00", uint8(0x00), "00"), ("uint8 00", uint8(0x00), "00"),
("uint8 01", uint8(0x01), "01"), ("uint8 01", uint8(0x01), "01"),
("uint8 ab", uint8(0xab), "ab"), ("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 0000", uint16(0x0000), "0000"),
("uint16 abcd", uint16(0xabcd), "cdab"), ("uint16 abcd", uint16(0xabcd), "cdab"),
("uint32 00000000", uint32(0x00000000), "00000000"), ("uint32 00000000", uint32(0x00000000), "00000000"),

View File

@ -1,7 +1,7 @@
from .ssz_typing import ( from .ssz_typing import (
SSZValue, SSZType, BasicValue, BasicType, Series, ElementsType, SSZValue, SSZType, BasicValue, BasicType, Series, ElementsType,
Elements, Bit, Container, List, Vector, Bytes, BytesN, Elements, Bit, Bool, Container, List, Vector, Bytes, BytesN,
uint, uint8, uint16, uint32, uint64, uint128, uint256, byte, uint, uint8, uint16, uint32, uint64, uint128, uint256,
Bytes32, Bytes48 Bytes32, Bytes48
) )
@ -22,8 +22,8 @@ def test_subclasses():
assert issubclass(u, SSZValue) assert issubclass(u, SSZValue)
assert isinstance(u, SSZType) assert isinstance(u, SSZType)
assert isinstance(u, BasicType) assert isinstance(u, BasicType)
assert issubclass(Bit, BasicValue) assert issubclass(Bool, BasicValue)
assert isinstance(Bit, BasicType) assert isinstance(Bool, BasicType)
for c in [Container, List, Vector, Bytes, BytesN]: for c in [Container, List, Vector, Bytes, BytesN]:
assert issubclass(c, Series) assert issubclass(c, Series)
@ -38,21 +38,25 @@ def test_subclasses():
def test_basic_instances(): 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) v = u(123)
assert isinstance(v, uint) assert isinstance(v, uint)
assert isinstance(v, int) assert isinstance(v, int)
assert isinstance(v, BasicValue) assert isinstance(v, BasicValue)
assert isinstance(v, SSZValue) assert isinstance(v, SSZValue)
assert isinstance(Bit(True), BasicValue) assert isinstance(Bool(True), BasicValue)
assert isinstance(Bit(False), BasicValue) assert isinstance(Bool(False), BasicValue)
assert isinstance(Bit(True), Bool)
assert isinstance(Bit(False), Bool)
def test_basic_value_bounds(): def test_basic_value_bounds():
max = { max = {
Bool: 2 ** 1,
Bit: 2 ** 1, Bit: 2 ** 1,
uint8: 2 ** (8 * 1), uint8: 2 ** (8 * 1),
byte: 2 ** (8 * 1),
uint16: 2 ** (8 * 2), uint16: 2 ** (8 * 2),
uint32: 2 ** (8 * 4), uint32: 2 ** (8 * 4),
uint64: 2 ** (8 * 8), uint64: 2 ** (8 * 8),