From 765176ec8ca1dda29a7534d44afb962eb3502fd7 Mon Sep 17 00:00:00 2001 From: Carl Beekhuizen Date: Fri, 27 Dec 2019 09:37:26 +0100 Subject: [PATCH 1/2] PySpec SSZ Bytes instantiated from hex --- scripts/build_spec.py | 2 -- specs/core/0_beacon-chain.md | 14 ++++++-------- specs/core/1_custody-game.md | 2 +- specs/core/1_shard-data-chains.md | 4 ++-- test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py | 7 ++++++- 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/scripts/build_spec.py b/scripts/build_spec.py index 67c9a547c..1a719cb29 100644 --- a/scripts/build_spec.py +++ b/scripts/build_spec.py @@ -163,8 +163,6 @@ def objects_to_spec(functions: Dict[str, str], del functions[k] functions_spec = '\n\n'.join(functions.values()) for k in list(constants.keys()): - if k.startswith('DOMAIN_'): - constants[k] = f"DomainType(({constants[k]}).to_bytes(length=4, byteorder='little'))" if k == "BLS12_381_Q": constants[k] += " # noqa: E501" constants_spec = '\n'.join(map(lambda x: '%s = %s' % (x, constants[x]), constants)) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index 9c0b9fe66..0c9f49b74 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -199,7 +199,7 @@ The following values are (non-configurable) constants used throughout the specif | - | - | | `GENESIS_SLOT` | `Slot(0)` | | `GENESIS_EPOCH` | `Epoch(0)` | -| `BLS_WITHDRAWAL_PREFIX` | `Bytes1(b'\x00')` | +| `BLS_WITHDRAWAL_PREFIX` | `Bytes1('0x00')` | ### Time parameters @@ -249,15 +249,13 @@ The following values are (non-configurable) constants used throughout the specif ### Domain types -The following types are defined, mapping into `DomainType` (little endian): - | Name | Value | | - | - | -| `DOMAIN_BEACON_PROPOSER` | `0` | -| `DOMAIN_BEACON_ATTESTER` | `1` | -| `DOMAIN_RANDAO` | `2` | -| `DOMAIN_DEPOSIT` | `3` | -| `DOMAIN_VOLUNTARY_EXIT` | `4` | +| `DOMAIN_BEACON_PROPOSER` | `Bytes4('0x00000000')` | +| `DOMAIN_BEACON_ATTESTER` | `Bytes4('0x01000000')` | +| `DOMAIN_RANDAO` | `Bytes4('0x02000000')` | +| `DOMAIN_DEPOSIT` | `Bytes4('0x03000000')` | +| `DOMAIN_VOLUNTARY_EXIT` | `Bytes4('0x04000000')` | ## Containers diff --git a/specs/core/1_custody-game.md b/specs/core/1_custody-game.md index 017e2a22a..829c68ac1 100644 --- a/specs/core/1_custody-game.md +++ b/specs/core/1_custody-game.md @@ -131,7 +131,7 @@ The following types are defined, mapping into `DomainType` (little endian): | Name | Value | | - | - | -| `DOMAIN_CUSTODY_BIT_CHALLENGE` | `6` | +| `DOMAIN_CUSTODY_BIT_CHALLENGE` | `Bytes4('0x06000000')` | ### TODO PLACEHOLDER diff --git a/specs/core/1_shard-data-chains.md b/specs/core/1_shard-data-chains.md index d5964fe7d..408c9b2a8 100644 --- a/specs/core/1_shard-data-chains.md +++ b/specs/core/1_shard-data-chains.md @@ -101,8 +101,8 @@ This document describes the shard transition function (data layer only) and the | Name | Value | | - | - | -| `DOMAIN_SHARD_PROPOSER` | `128` | -| `DOMAIN_SHARD_ATTESTER` | `129` | +| `DOMAIN_SHARD_PROPOSER` | `Bytes4('0x80000000')` | +| `DOMAIN_SHARD_ATTESTER` | `Bytes4('0x81000000')` | ## Containers diff --git a/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py b/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py index 4bda43360..4ddff1b5e 100644 --- a/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py +++ b/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py @@ -451,10 +451,15 @@ class BaseBytes(bytes, Elements, metaclass=BytesType): @classmethod def extract_args(cls, *args): x = args - if len(x) == 1 and isinstance(x[0], (GeneratorType, bytes)): + if len(x) == 1 and isinstance(x[0], (GeneratorType, bytes, str)): x = x[0] if isinstance(x, bytes): # Includes BytesLike return x + if isinstance(x, str): + if x[:2] == '0x': + return bytes.fromhex(x[2:]) + else: + return bytes.fromhex(x) else: return bytes(x) # E.g. GeneratorType put into bytes. From e70863ca936a63b792f690d951eb757576f872cf Mon Sep 17 00:00:00 2001 From: Carl Beekhuizen Date: Mon, 30 Dec 2019 06:52:00 +0100 Subject: [PATCH 2/2] Bytes4 -> DomainType --- specs/core/0_beacon-chain.md | 10 +++++----- specs/core/1_custody-game.md | 2 +- specs/core/1_shard-data-chains.md | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/specs/core/0_beacon-chain.md b/specs/core/0_beacon-chain.md index 0c9f49b74..647d1c9bd 100644 --- a/specs/core/0_beacon-chain.md +++ b/specs/core/0_beacon-chain.md @@ -251,11 +251,11 @@ The following values are (non-configurable) constants used throughout the specif | Name | Value | | - | - | -| `DOMAIN_BEACON_PROPOSER` | `Bytes4('0x00000000')` | -| `DOMAIN_BEACON_ATTESTER` | `Bytes4('0x01000000')` | -| `DOMAIN_RANDAO` | `Bytes4('0x02000000')` | -| `DOMAIN_DEPOSIT` | `Bytes4('0x03000000')` | -| `DOMAIN_VOLUNTARY_EXIT` | `Bytes4('0x04000000')` | +| `DOMAIN_BEACON_PROPOSER` | `DomainType('0x00000000')` | +| `DOMAIN_BEACON_ATTESTER` | `DomainType('0x01000000')` | +| `DOMAIN_RANDAO` | `DomainType('0x02000000')` | +| `DOMAIN_DEPOSIT` | `DomainType('0x03000000')` | +| `DOMAIN_VOLUNTARY_EXIT` | `DomainType('0x04000000')` | ## Containers diff --git a/specs/core/1_custody-game.md b/specs/core/1_custody-game.md index 829c68ac1..b1f61de2f 100644 --- a/specs/core/1_custody-game.md +++ b/specs/core/1_custody-game.md @@ -131,7 +131,7 @@ The following types are defined, mapping into `DomainType` (little endian): | Name | Value | | - | - | -| `DOMAIN_CUSTODY_BIT_CHALLENGE` | `Bytes4('0x06000000')` | +| `DOMAIN_CUSTODY_BIT_CHALLENGE` | `DomainType('0x06000000')` | ### TODO PLACEHOLDER diff --git a/specs/core/1_shard-data-chains.md b/specs/core/1_shard-data-chains.md index 408c9b2a8..93570dbee 100644 --- a/specs/core/1_shard-data-chains.md +++ b/specs/core/1_shard-data-chains.md @@ -101,8 +101,8 @@ This document describes the shard transition function (data layer only) and the | Name | Value | | - | - | -| `DOMAIN_SHARD_PROPOSER` | `Bytes4('0x80000000')` | -| `DOMAIN_SHARD_ATTESTER` | `Bytes4('0x81000000')` | +| `DOMAIN_SHARD_PROPOSER` | `DomainType('0x80000000')` | +| `DOMAIN_SHARD_ATTESTER` | `DomainType('0x81000000')` | ## Containers