From 98366c95601f26d0553834ca2857d8af4385525f Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Sat, 13 Apr 2019 09:28:00 +1000 Subject: [PATCH 01/19] bump ssz to `0.1.0a4` --- eth2/beacon/db/chain.py | 20 ++++++----- eth2/beacon/state_machines/base.py | 5 ++- eth2/beacon/types/attestations.py | 6 ++-- eth2/beacon/types/forks.py | 5 +-- .../types/pending_attestation_records.py | 6 ++-- eth2/beacon/types/slashable_attestations.py | 4 +-- eth2/beacon/types/states.py | 17 +++++----- setup.py | 2 +- tests/eth2/beacon/conftest.py | 33 ++++++++++++++----- tests/eth2/beacon/db/test_beacon_chaindb.py | 4 +-- .../beacon/types/test_crosslink_record.py | 2 ++ 11 files changed, 62 insertions(+), 42 deletions(-) diff --git a/eth2/beacon/db/chain.py b/eth2/beacon/db/chain.py index b4abf356a0..f183758327 100644 --- a/eth2/beacon/db/chain.py +++ b/eth2/beacon/db/chain.py @@ -129,7 +129,7 @@ def persist_block_chain( # Beacon State # @abstractmethod - def get_state_by_root(self, state_root: Hash32) -> BeaconState: + def get_state_by_root(self, state_root: Hash32, state_class: Type[BeaconState]) -> BeaconState: pass @abstractmethod @@ -209,7 +209,7 @@ def _get_canonical_block_root(db: BaseDB, slot: int) -> Hash32: "No canonical block for block slot #{0}".format(slot) ) else: - return ssz.decode(encoded_key, sedes=ssz.sedes.bytes_sedes) + return ssz.decode(encoded_key, sedes=ssz.sedes.byte_list) def get_canonical_block_by_slot(self, slot: int, @@ -546,7 +546,7 @@ def _add_block_slot_to_root_lookup(db: BaseDB, block: BaseBeaconBlock) -> None: ) db.set( block_slot_to_root_key, - ssz.encode(block.signed_root, sedes=ssz.sedes.bytes_sedes), + ssz.encode(block.signed_root, sedes=ssz.sedes.byte_list), ) @staticmethod @@ -566,11 +566,13 @@ def _add_block_root_to_slot_lookup(db: BaseDB, block: BaseBeaconBlock) -> None: # # Beacon State API # - def get_state_by_root(self, state_root: Hash32) -> BeaconState: - return self._get_state_by_root(self.db, state_root) + def get_state_by_root(self, state_root: Hash32, state_class: Type[BeaconState]) -> BeaconState: + return self._get_state_by_root(self.db, state_root, state_class) @staticmethod - def _get_state_by_root(db: BaseDB, state_root: Hash32) -> BeaconState: + def _get_state_by_root(db: BaseDB, + state_root: Hash32, + state_class: Type[BeaconState]) -> BeaconState: """ Return the requested beacon state as specified by state hash. @@ -581,7 +583,7 @@ def _get_state_by_root(db: BaseDB, state_root: Hash32) -> BeaconState: state_ssz = db[state_root] except KeyError: raise StateRootNotFound(f"No state with root {encode_hex(state_root)} found") - return _decode_state(state_ssz) + return _decode_state(state_ssz, state_class) def persist_state(self, state: BeaconState) -> None: @@ -625,6 +627,6 @@ def _decode_block(block_ssz: bytes, sedes: Type[BaseBeaconBlock]) -> BaseBeaconB @functools.lru_cache(128) -def _decode_state(state_ssz: bytes) -> BeaconState: +def _decode_state(state_ssz: bytes, state_class: Type[BeaconState]) -> BeaconState: # TODO: forkable BeaconState fields? - return ssz.decode(state_ssz, sedes=BeaconState) + return ssz.decode(state_ssz, sedes=state_class) diff --git a/eth2/beacon/state_machines/base.py b/eth2/beacon/state_machines/base.py index ee40307744..bd37350d07 100644 --- a/eth2/beacon/state_machines/base.py +++ b/eth2/beacon/state_machines/base.py @@ -94,7 +94,10 @@ def __init__(self, @property def state(self) -> BeaconState: if self._state is None: - self._state = self.chaindb.get_state_by_root(self.block.state_root) + self._state = self.chaindb.get_state_by_root( + self.block.state_root, + self.get_state_class() + ) return self._state @classmethod diff --git a/eth2/beacon/types/attestations.py b/eth2/beacon/types/attestations.py index 463d256854..836265fd41 100644 --- a/eth2/beacon/types/attestations.py +++ b/eth2/beacon/types/attestations.py @@ -1,6 +1,6 @@ import ssz from ssz.sedes import ( - bytes_sedes, + byte_list, bytes96, ) @@ -21,11 +21,11 @@ class Attestation(ssz.Serializable): fields = [ # Attester aggregation bitfield - ('aggregation_bitfield', bytes_sedes), + ('aggregation_bitfield', byte_list), # Attestation data ('data', AttestationData), # Proof of custody bitfield - ('custody_bitfield', bytes_sedes), + ('custody_bitfield', byte_list), # BLS aggregate signature ('aggregate_signature', bytes96), ] diff --git a/eth2/beacon/types/forks.py b/eth2/beacon/types/forks.py index c8489be920..ec8048f12c 100644 --- a/eth2/beacon/types/forks.py +++ b/eth2/beacon/types/forks.py @@ -1,6 +1,6 @@ import ssz from ssz.sedes import ( - BytesN, + bytes4, uint64, ) @@ -9,9 +9,6 @@ ) -bytes4 = BytesN(4) - - class Fork(ssz.Serializable): fields = [ diff --git a/eth2/beacon/types/pending_attestation_records.py b/eth2/beacon/types/pending_attestation_records.py index 3a7fae68b4..2e8ba8efbc 100644 --- a/eth2/beacon/types/pending_attestation_records.py +++ b/eth2/beacon/types/pending_attestation_records.py @@ -1,6 +1,6 @@ import ssz from ssz.sedes import ( - bytes_sedes, + byte_list, uint64, ) @@ -18,11 +18,11 @@ class PendingAttestationRecord(ssz.Serializable): fields = [ # Attester aggregation bitfield - ('aggregation_bitfield', bytes_sedes), + ('aggregation_bitfield', byte_list), # Attestation data ('data', AttestationData), # Custody bitfield - ('custody_bitfield', bytes_sedes), + ('custody_bitfield', byte_listbyte_list), # Inclusion slot ('inclusion_slot', uint64), ] diff --git a/eth2/beacon/types/slashable_attestations.py b/eth2/beacon/types/slashable_attestations.py index 4137f9742b..5db4abff7c 100644 --- a/eth2/beacon/types/slashable_attestations.py +++ b/eth2/beacon/types/slashable_attestations.py @@ -6,7 +6,7 @@ import ssz from ssz.sedes import ( List, - bytes_sedes, + byte_list, bytes96, uint64, ) @@ -36,7 +36,7 @@ class SlashableAttestation(ssz.Serializable): # Attestation data ('data', AttestationData), # Custody bitfield - ('custody_bitfield', bytes_sedes), + ('custody_bitfield', byte_list), # Aggregate signature ('aggregate_signature', bytes96), ] diff --git a/eth2/beacon/types/states.py b/eth2/beacon/types/states.py index 0c658eb73a..06beafbb91 100644 --- a/eth2/beacon/types/states.py +++ b/eth2/beacon/types/states.py @@ -13,6 +13,7 @@ import ssz from ssz.sedes import ( List, + Vector, bytes32, uint64, ) @@ -60,7 +61,7 @@ class BeaconState(ssz.Serializable): ('validator_registry_update_epoch', uint64), # Randomness and committees - ('latest_randao_mixes', List(bytes32)), + ('latest_randao_mixes', Vector(bytes32, 1)), ('previous_shuffling_start_shard', uint64), ('current_shuffling_start_shard', uint64), ('previous_shuffling_epoch', uint64), @@ -82,11 +83,11 @@ class BeaconState(ssz.Serializable): ('finalized_root', bytes32), # Recent state - ('latest_crosslinks', List(CrosslinkRecord)), - ('latest_block_roots', List(bytes32)), # Needed to process attestations, older to newer # noqa: E501 - ('latest_state_roots', List(bytes32)), - ('latest_active_index_roots', List(bytes32)), - ('latest_slashed_balances', List(uint64)), # Balances slashed at every withdrawal period # noqa: E501 + ('latest_crosslinks', Vector(CrosslinkRecord, 1)), + ('latest_block_roots', Vector(bytes32, 1)), # Needed to process attestations, older to newer # noqa: E501 + ('latest_state_roots', Vector(bytes32, 1)), + ('latest_active_index_roots', Vector(bytes32, 1)), + ('latest_slashed_balances', Vector(uint64, 1)), # Balances slashed at every withdrawal period # noqa: E501 ('latest_block_header', BeaconBlockHeader), ('historical_roots', List(bytes32)), # allow for a log-sized Merkle proof from any block to any historical block root" # noqa: E501 @@ -141,6 +142,7 @@ def __init__( raise ValueError( "The length of validator_registry and validator_balances should be the same." ) + super().__init__( # Misc slot=slot, @@ -221,8 +223,7 @@ def create_filled_state(cls, latest_randao_mixes_length: int, latest_slashed_exit_length: int, activated_genesis_validators: Sequence[ValidatorRecord]=(), - genesis_balances: Sequence[Gwei]=(), - genesis_block_class: Type[BaseBeaconBlock]) -> 'BeaconState': + genesis_balances: Sequence[Gwei]=()) -> 'BeaconState': return cls( # Misc slot=genesis_slot, diff --git a/setup.py b/setup.py index 596a6e5d78..b952961d99 100644 --- a/setup.py +++ b/setup.py @@ -84,7 +84,7 @@ "py-ecc>=1.6.0,<2.0.0", "rlp>=1.1.0,<2.0.0", "py-evm==0.2.0a42", - "ssz==0.1.0a2", + "ssz==0.1.0a4", ], 'libp2p': [ "base58>=1.0.3", diff --git a/tests/eth2/beacon/conftest.py b/tests/eth2/beacon/conftest.py index e0e7af4522..11ff3a65ae 100644 --- a/tests/eth2/beacon/conftest.py +++ b/tests/eth2/beacon/conftest.py @@ -54,6 +54,17 @@ DEFAULT_NUM_VALIDATORS = 40 +# SSZ +@pytest.fixture(scope="function", autouse=True) +def override_length(config): + BeaconState._meta.container_sedes.field_name_to_sedes["latest_randao_mixes"].length = config.LATEST_RANDAO_MIXES_LENGTH # noqa: E501 + BeaconState._meta.container_sedes.field_name_to_sedes["latest_crosslinks"].length = config.SHARD_COUNT # noqa: E501 + BeaconState._meta.container_sedes.field_name_to_sedes["latest_block_roots"].length = config.SLOTS_PER_HISTORICAL_ROOT # noqa: E501 + BeaconState._meta.container_sedes.field_name_to_sedes["latest_state_roots"].length = config.SLOTS_PER_HISTORICAL_ROOT # noqa: E501 + BeaconState._meta.container_sedes.field_name_to_sedes["latest_active_index_roots"].length = config.LATEST_ACTIVE_INDEX_ROOTS_LENGTH # noqa: E501 + BeaconState._meta.container_sedes.field_name_to_sedes["latest_slashed_balances"].length = config.LATEST_SLASHED_EXIT_LENGTH # noqa: E501 + + @pytest.fixture(scope="session") def privkeys(): """ @@ -154,11 +165,13 @@ def sample_genesis_block_class(): @pytest.fixture -def sample_beacon_state_params(genesis_slot, +def sample_beacon_state_params(config, + genesis_slot, genesis_epoch, sample_fork_params, sample_eth1_data_params, - sample_block_header_params): + sample_block_header_params, + sample_crosslink_record_params): return { 'slot': genesis_slot + 100, 'genesis_time': 0, @@ -166,7 +179,7 @@ def sample_beacon_state_params(genesis_slot, 'validator_registry': (), 'validator_balances': (), 'validator_registry_update_epoch': 0, - 'latest_randao_mixes': (), + 'latest_randao_mixes': (ZERO_HASH32,) * config.LATEST_RANDAO_MIXES_LENGTH, 'previous_shuffling_start_shard': 1, 'current_shuffling_start_shard': 2, 'previous_shuffling_epoch': genesis_epoch, @@ -182,11 +195,14 @@ def sample_beacon_state_params(genesis_slot, 'justification_bitfield': 0, 'finalized_epoch': 0, 'finalized_root': b'\x33' * 32, - 'latest_crosslinks': (), - 'latest_block_roots': (), - 'latest_state_roots': (), - 'latest_active_index_roots': (), - 'latest_slashed_balances': (), + 'latest_crosslinks': ( + (CrosslinkRecord(**sample_crosslink_record_params),) * + config.SHARD_COUNT + ), + 'latest_block_roots': (ZERO_HASH32,) * config.SLOTS_PER_HISTORICAL_ROOT, + 'latest_state_roots': (ZERO_HASH32,) * config.SLOTS_PER_HISTORICAL_ROOT, + 'latest_active_index_roots': (ZERO_HASH32,) * config.LATEST_ACTIVE_INDEX_ROOTS_LENGTH, + 'latest_slashed_balances': (0,) * config.LATEST_SLASHED_EXIT_LENGTH, 'latest_block_header': BeaconBlockHeader(**sample_block_header_params), 'historical_roots': (), 'latest_eth1_data': Eth1Data(**sample_eth1_data_params), @@ -367,7 +383,6 @@ def filled_beacon_state(genesis_epoch, latest_active_index_roots_length=latest_active_index_roots_length, latest_randao_mixes_length=latest_randao_mixes_length, latest_slashed_exit_length=latest_slashed_exit_length, - genesis_block_class=SerenityBeaconBlock, ) diff --git a/tests/eth2/beacon/db/test_beacon_chaindb.py b/tests/eth2/beacon/db/test_beacon_chaindb.py index a9b9eb809b..023cae1283 100644 --- a/tests/eth2/beacon/db/test_beacon_chaindb.py +++ b/tests/eth2/beacon/db/test_beacon_chaindb.py @@ -120,8 +120,8 @@ def test_chaindb_get_canonical_block_root(chaindb, block): def test_chaindb_state(chaindb, state): chaindb.persist_state(state) - - result_state = chaindb.get_state_by_root(state.root) + state_class = BeaconState + result_state = chaindb.get_state_by_root(state.root, state_class) assert result_state.root == state.root diff --git a/tests/eth2/beacon/types/test_crosslink_record.py b/tests/eth2/beacon/types/test_crosslink_record.py index 42bfb42d57..99f07d6c25 100644 --- a/tests/eth2/beacon/types/test_crosslink_record.py +++ b/tests/eth2/beacon/types/test_crosslink_record.py @@ -7,3 +7,5 @@ def test_defaults(sample_crosslink_record_params): crosslink = CrosslinkRecord(**sample_crosslink_record_params) assert crosslink.epoch == sample_crosslink_record_params['epoch'] assert crosslink.crosslink_data_root == sample_crosslink_record_params['crosslink_data_root'] + print(CrosslinkRecord.get_static_size()) + print('3 * CrosslinkRecord.get_static_size()', 3 * CrosslinkRecord.get_static_size()) From 47b8a150633b09d0a6826196650e376b7965559a Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Thu, 18 Apr 2019 09:12:06 +0800 Subject: [PATCH 02/19] Workaround: rename property `hash_tree_root` to `tree_root` for the SSZ integration issue. --- eth2/beacon/helpers.py | 2 +- .../state_machines/forks/serenity/epoch_processing.py | 2 +- eth2/beacon/types/blocks.py | 8 ++++---- eth2/beacon/types/historical_batch.py | 2 +- tests/eth2/beacon/state_machines/test_state_transition.py | 2 +- tests/eth2/beacon/test_helpers.py | 2 +- tests/eth2/beacon/types/test_block.py | 2 +- 7 files changed, 10 insertions(+), 10 deletions(-) diff --git a/eth2/beacon/helpers.py b/eth2/beacon/helpers.py index ac7e7e6616..2acc7494d8 100644 --- a/eth2/beacon/helpers.py +++ b/eth2/beacon/helpers.py @@ -58,7 +58,7 @@ def get_temporary_block_header(block: BeaconBlock) -> BeaconBlockHeader: slot=block.slot, previous_block_root=block.previous_block_root, state_root=ZERO_HASH32, - block_body_root=block.body.hash_tree_root, + block_body_root=block.body.tree_root, signature=EMPTY_SIGNATURE, ) diff --git a/eth2/beacon/state_machines/forks/serenity/epoch_processing.py b/eth2/beacon/state_machines/forks/serenity/epoch_processing.py index f0876c0deb..cb182b9806 100644 --- a/eth2/beacon/state_machines/forks/serenity/epoch_processing.py +++ b/eth2/beacon/state_machines/forks/serenity/epoch_processing.py @@ -1193,7 +1193,7 @@ def _update_historical_roots(state: BeaconState, state_roots=state.latest_state_roots, slots_per_historical_root=config.SLOTS_PER_HISTORICAL_ROOT, ) - updated_historical_roots += (historical_batch.hash_tree_root,) + updated_historical_roots += (historical_batch.tree_root,) return state.copy( historical_roots=updated_historical_roots diff --git a/eth2/beacon/types/blocks.py b/eth2/beacon/types/blocks.py index b891f94dd4..93926fcaff 100644 --- a/eth2/beacon/types/blocks.py +++ b/eth2/beacon/types/blocks.py @@ -86,7 +86,7 @@ def __repr__(self) -> str: _hash_tree_root = None @property - def hash_tree_root(self) -> Hash32: + def tree_root(self) -> Hash32: if self._hash_tree_root is None: self._hash_tree_root = ssz.hash_tree_root(self) return self._hash_tree_root @@ -178,7 +178,7 @@ def cast_block_body(cls, _hash_tree_root = None @property - def hash_tree_root(self) -> Hash32: + def tree_root(self) -> Hash32: if self._hash_tree_root is None: self._hash_tree_root = ssz.hash_tree_root(self) return self._hash_tree_root @@ -225,7 +225,7 @@ def __repr__(self) -> str: _hash_tree_root = None @property - def hash_tree_root(self) -> Hash32: + def tree_root(self) -> Hash32: # NOTE: this is used in the fork choice calculation if self._hash_tree_root is None: self._hash_tree_root = ssz.hash_tree_root(self) @@ -250,7 +250,7 @@ def header(self) -> BeaconBlockHeader: slot=self.slot, previous_block_root=self.previous_block_root, state_root=self.state_root, - block_body_root=self.body.hash_tree_root, + block_body_root=self.body.tree_root, signature=self.signature, ) diff --git a/eth2/beacon/types/historical_batch.py b/eth2/beacon/types/historical_batch.py index 12188cb48f..36523b8a1d 100644 --- a/eth2/beacon/types/historical_batch.py +++ b/eth2/beacon/types/historical_batch.py @@ -38,7 +38,7 @@ def __init__(self, _hash_tree_root = None @property - def hash_tree_root(self) -> Hash32: + def tree_root(self) -> Hash32: if self._hash_tree_root is None: self._hash_tree_root = ssz.hash_tree_root(self) return self._hash_tree_root diff --git a/tests/eth2/beacon/state_machines/test_state_transition.py b/tests/eth2/beacon/state_machines/test_state_transition.py index 794bcc194c..c82f29225a 100644 --- a/tests/eth2/beacon/state_machines/test_state_transition.py +++ b/tests/eth2/beacon/state_machines/test_state_transition.py @@ -104,6 +104,6 @@ def test_per_slot_transition(base_db, state_roots=state.latest_state_roots, slots_per_historical_root=config.SLOTS_PER_HISTORICAL_ROOT, ) - assert updated_state.historical_roots[-1] == historical_batch.hash_tree_root + assert updated_state.historical_roots[-1] == historical_batch.tree_root else: assert updated_state.historical_roots == state.historical_roots diff --git a/tests/eth2/beacon/test_helpers.py b/tests/eth2/beacon/test_helpers.py index 8e79b75117..5859eb73d4 100644 --- a/tests/eth2/beacon/test_helpers.py +++ b/tests/eth2/beacon/test_helpers.py @@ -54,7 +54,7 @@ def test_get_temporary_block_header(sample_block): assert header.slot == sample_block.slot assert header.previous_block_root == sample_block.previous_block_root assert header.state_root == ZERO_HASH32 - assert header.block_body_root == sample_block.body.hash_tree_root + assert header.block_body_root == sample_block.body.tree_root assert header.signature == sample_block.signature diff --git a/tests/eth2/beacon/types/test_block.py b/tests/eth2/beacon/types/test_block.py index 2e4427621a..289c9f293a 100644 --- a/tests/eth2/beacon/types/test_block.py +++ b/tests/eth2/beacon/types/test_block.py @@ -47,4 +47,4 @@ def test_block_root_and_block_header_root_equivalence(sample_block): block = sample_block header = block.header - assert block.hash_tree_root == header.hash_tree_root + assert block.tree_root == header.tree_root From 9efa9872f194b85358245d9d2e2f71bc92edf16a Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Thu, 18 Apr 2019 10:28:35 +0800 Subject: [PATCH 03/19] linter --- eth2/beacon/epoch_processing_helpers.py | 2 +- eth2/beacon/types/states.py | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/eth2/beacon/epoch_processing_helpers.py b/eth2/beacon/epoch_processing_helpers.py index 3773a9e5f3..8e55f623b1 100644 --- a/eth2/beacon/epoch_processing_helpers.py +++ b/eth2/beacon/epoch_processing_helpers.py @@ -1,4 +1,4 @@ -from typing import ( +from typing import ( # noqa: F401 Dict, Iterable, Sequence, diff --git a/eth2/beacon/types/states.py b/eth2/beacon/types/states.py index 06beafbb91..5e21059c71 100644 --- a/eth2/beacon/types/states.py +++ b/eth2/beacon/types/states.py @@ -1,6 +1,5 @@ from typing import ( Sequence, - Type, ) from eth_typing import ( @@ -38,7 +37,7 @@ ValidatorIndex, ) -from .blocks import BeaconBlockHeader, BaseBeaconBlock, BeaconBlock +from .blocks import BeaconBlockHeader, BeaconBlock from .eth1_data import Eth1Data from .eth1_data_vote import Eth1DataVote from .crosslink_records import CrosslinkRecord From 7a573adecda90c0beca49786e3396450ebc726b8 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Thu, 25 Apr 2019 18:24:55 +0800 Subject: [PATCH 04/19] bump to ssz==0.1.0a6 fix --- eth2/beacon/types/pending_attestation_records.py | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/eth2/beacon/types/pending_attestation_records.py b/eth2/beacon/types/pending_attestation_records.py index 2e8ba8efbc..d49bc31bc8 100644 --- a/eth2/beacon/types/pending_attestation_records.py +++ b/eth2/beacon/types/pending_attestation_records.py @@ -22,7 +22,7 @@ class PendingAttestationRecord(ssz.Serializable): # Attestation data ('data', AttestationData), # Custody bitfield - ('custody_bitfield', byte_listbyte_list), + ('custody_bitfield', byte_list), # Inclusion slot ('inclusion_slot', uint64), ] diff --git a/setup.py b/setup.py index b952961d99..1107e90c2a 100644 --- a/setup.py +++ b/setup.py @@ -84,7 +84,7 @@ "py-ecc>=1.6.0,<2.0.0", "rlp>=1.1.0,<2.0.0", "py-evm==0.2.0a42", - "ssz==0.1.0a4", + "ssz==0.1.0a6", ], 'libp2p': [ "base58>=1.0.3", From aa236895e6f04e7c93d1febfaa8b749899cd36eb Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Thu, 25 Apr 2019 19:07:50 +0800 Subject: [PATCH 05/19] wrap ssz hash tree root --- eth2/beacon/helpers.py | 2 +- .../forks/serenity/epoch_processing.py | 2 +- eth2/beacon/tools/builder/validator.py | 7 +++-- .../attestation_data_and_custody_bits.py | 16 ++--------- eth2/beacon/types/blocks.py | 27 +++++++------------ eth2/beacon/types/crosslink_records.py | 13 ++------- eth2/beacon/types/deposit_input.py | 7 +++-- eth2/beacon/types/historical_batch.py | 8 +++--- eth2/beacon/types/slashable_attestations.py | 13 ++------- eth2/beacon/types/states.py | 15 ++--------- eth2/beacon/types/transfers.py | 5 +--- .../state_machines/test_state_transition.py | 2 +- tests/eth2/beacon/test_helpers.py | 2 +- tests/eth2/beacon/types/test_block.py | 2 +- .../types/test_slashable_attestation.py | 2 +- tests/eth2/beacon/types/test_states.py | 5 ---- 16 files changed, 36 insertions(+), 92 deletions(-) diff --git a/eth2/beacon/helpers.py b/eth2/beacon/helpers.py index 2acc7494d8..e6fa61531a 100644 --- a/eth2/beacon/helpers.py +++ b/eth2/beacon/helpers.py @@ -58,7 +58,7 @@ def get_temporary_block_header(block: BeaconBlock) -> BeaconBlockHeader: slot=block.slot, previous_block_root=block.previous_block_root, state_root=ZERO_HASH32, - block_body_root=block.body.tree_root, + block_body_root=block.body.root, signature=EMPTY_SIGNATURE, ) diff --git a/eth2/beacon/state_machines/forks/serenity/epoch_processing.py b/eth2/beacon/state_machines/forks/serenity/epoch_processing.py index cb182b9806..d511ae2257 100644 --- a/eth2/beacon/state_machines/forks/serenity/epoch_processing.py +++ b/eth2/beacon/state_machines/forks/serenity/epoch_processing.py @@ -1193,7 +1193,7 @@ def _update_historical_roots(state: BeaconState, state_roots=state.latest_state_roots, slots_per_historical_root=config.SLOTS_PER_HISTORICAL_ROOT, ) - updated_historical_roots += (historical_batch.tree_root,) + updated_historical_roots += (historical_batch.root,) return state.copy( historical_roots=updated_historical_roots diff --git a/eth2/beacon/tools/builder/validator.py b/eth2/beacon/tools/builder/validator.py index f81259ad4e..277f9b2572 100644 --- a/eth2/beacon/tools/builder/validator.py +++ b/eth2/beacon/tools/builder/validator.py @@ -24,8 +24,8 @@ from eth.constants import ( ZERO_HASH32, ) - from py_ecc import bls +import ssz from eth2._utils.bitfield import ( get_empty_bitfield, @@ -35,6 +35,9 @@ CommitteeConfig, Eth2Config, ) +from eth2.beacon._utils.hash import ( + hash_eth2, +) from eth2.beacon.constants import ( ZERO_TIMESTAMP, ) @@ -157,7 +160,7 @@ def sign_proof_of_possession(deposit_input: DepositInput, SignatureDomain.DOMAIN_DEPOSIT, ) return bls.sign( - message_hash=deposit_input.root, + message_hash=hash_eth2(ssz.encode(deposit_input)), privkey=privkey, domain=domain, ) diff --git a/eth2/beacon/types/attestation_data_and_custody_bits.py b/eth2/beacon/types/attestation_data_and_custody_bits.py index eb107503de..2cae03c398 100644 --- a/eth2/beacon/types/attestation_data_and_custody_bits.py +++ b/eth2/beacon/types/attestation_data_and_custody_bits.py @@ -6,10 +6,6 @@ Hash32, ) -from eth2.beacon._utils.hash import ( - hash_eth2, -) - from .attestation_data import ( AttestationData, ) @@ -33,16 +29,8 @@ def __init__(self, custody_bit=custody_bit, ) - _hash = None - - @property - def hash(self) -> Hash32: - if self._hash is None: - self._hash = hash_eth2(ssz.encode(self)) - return self._hash + _root = None @property def root(self) -> Hash32: - # Alias of `hash`. - # Using flat hash, will likely use SSZ tree hash. - return self.hash + return super().root diff --git a/eth2/beacon/types/blocks.py b/eth2/beacon/types/blocks.py index 93926fcaff..f15a2a706d 100644 --- a/eth2/beacon/types/blocks.py +++ b/eth2/beacon/types/blocks.py @@ -83,13 +83,11 @@ def __repr__(self) -> str: encode_hex(self.signed_root)[2:10], ) - _hash_tree_root = None + _root = None @property - def tree_root(self) -> Hash32: - if self._hash_tree_root is None: - self._hash_tree_root = ssz.hash_tree_root(self) - return self._hash_tree_root + def root(self) -> Hash32: + return super().root _signed_root = None @@ -175,13 +173,11 @@ def cast_block_body(cls, transfers=body.transfers, ) - _hash_tree_root = None + _root = None @property - def tree_root(self) -> Hash32: - if self._hash_tree_root is None: - self._hash_tree_root = ssz.hash_tree_root(self) - return self._hash_tree_root + def root(self) -> Hash32: + return super().root class BaseBeaconBlock(ssz.Serializable, Configurable, ABC): @@ -222,14 +218,11 @@ def __repr__(self) -> str: encode_hex(self.signed_root)[2:10], ) - _hash_tree_root = None + _root = None @property - def tree_root(self) -> Hash32: - # NOTE: this is used in the fork choice calculation - if self._hash_tree_root is None: - self._hash_tree_root = ssz.hash_tree_root(self) - return self._hash_tree_root + def root(self) -> Hash32: + return super().root _signed_root = None @@ -250,7 +243,7 @@ def header(self) -> BeaconBlockHeader: slot=self.slot, previous_block_root=self.previous_block_root, state_root=self.state_root, - block_body_root=self.body.tree_root, + block_body_root=self.body.root, signature=self.signature, ) diff --git a/eth2/beacon/types/crosslink_records.py b/eth2/beacon/types/crosslink_records.py index b9d348bfdb..8a0fd688bf 100644 --- a/eth2/beacon/types/crosslink_records.py +++ b/eth2/beacon/types/crosslink_records.py @@ -8,7 +8,6 @@ bytes32, ) -from eth2.beacon._utils.hash import hash_eth2 from eth2.beacon.typing import Epoch @@ -30,16 +29,8 @@ def __init__(self, crosslink_data_root=crosslink_data_root, ) - _hash = None - - @property - def hash(self) -> Hash32: - if self._hash is None: - self._hash = hash_eth2(ssz.encode(self)) - return self._hash + _root = None @property def root(self) -> Hash32: - # Alias of `hash`. - # Using flat hash, will likely use SSZ tree hash. - return self.hash + return super().root diff --git a/eth2/beacon/types/deposit_input.py b/eth2/beacon/types/deposit_input.py index 7170b21324..64f4a084e0 100644 --- a/eth2/beacon/types/deposit_input.py +++ b/eth2/beacon/types/deposit_input.py @@ -36,13 +36,12 @@ def __init__(self, ) _root = None - _signed_root = None @property def root(self) -> Hash32: - if self._root is None: - self._root = hash_eth2(ssz.encode(self)) - return self._root + return super().root + + _signed_root = None @property def signed_root(self) -> Hash32: diff --git a/eth2/beacon/types/historical_batch.py b/eth2/beacon/types/historical_batch.py index 36523b8a1d..2534aa1fb9 100644 --- a/eth2/beacon/types/historical_batch.py +++ b/eth2/beacon/types/historical_batch.py @@ -35,10 +35,8 @@ def __init__(self, state_roots=state_roots, ) - _hash_tree_root = None + _root = None @property - def tree_root(self) -> Hash32: - if self._hash_tree_root is None: - self._hash_tree_root = ssz.hash_tree_root(self) - return self._hash_tree_root + def root(self) -> Hash32: + return super().root diff --git a/eth2/beacon/types/slashable_attestations.py b/eth2/beacon/types/slashable_attestations.py index 5db4abff7c..1acea07135 100644 --- a/eth2/beacon/types/slashable_attestations.py +++ b/eth2/beacon/types/slashable_attestations.py @@ -18,7 +18,6 @@ from eth2._utils.bitfield import ( has_voted, ) -from eth2.beacon._utils.hash import hash_eth2 from eth2.beacon.typing import ( ValidatorIndex, ) @@ -53,19 +52,11 @@ def __init__(self, aggregate_signature, ) - _hash = None - - @property - def hash(self) -> Hash32: - if self._hash is None: - self._hash = hash_eth2(ssz.encode(self.data)) - return self._hash + _root = None @property def root(self) -> Hash32: - # Alias of `hash`. - # Using flat hash, will likely use SSZ tree hash. - return self.hash + return super().root @property def are_validator_indices_ascending(self) -> bool: diff --git a/eth2/beacon/types/states.py b/eth2/beacon/types/states.py index 5e21059c71..868d8b35da 100644 --- a/eth2/beacon/types/states.py +++ b/eth2/beacon/types/states.py @@ -21,9 +21,6 @@ ZERO_HASH32, ) -from eth2.beacon._utils.hash import ( - hash_eth2, -) from eth2.beacon.helpers import ( slot_to_epoch, get_temporary_block_header, @@ -188,19 +185,11 @@ def __repr__(self) -> str: encode_hex(self.root)[2:10], ) - _hash = None - - @property - def hash(self) -> Hash32: - if self._hash is None: - self._hash = hash_eth2(ssz.encode(self)) - return self._hash + _root = None @property def root(self) -> Hash32: - # Alias of `hash`. - # Using flat hash, might change to SSZ tree hash. - return self.hash + return super().root @property def num_validators(self) -> int: diff --git a/eth2/beacon/types/transfers.py b/eth2/beacon/types/transfers.py index a7443834f6..e2c498d737 100644 --- a/eth2/beacon/types/transfers.py +++ b/eth2/beacon/types/transfers.py @@ -10,7 +10,6 @@ uint64 ) -from eth2.beacon._utils.hash import hash_eth2 from eth2.beacon.constants import EMPTY_SIGNATURE from eth2.beacon.typing import ( @@ -60,6 +59,4 @@ def __init__(self, @property def root(self) -> Hash32: - if self._root is None: - self._root = hash_eth2(ssz.encode(self)) - return self._root + return super().root diff --git a/tests/eth2/beacon/state_machines/test_state_transition.py b/tests/eth2/beacon/state_machines/test_state_transition.py index c82f29225a..795465b49f 100644 --- a/tests/eth2/beacon/state_machines/test_state_transition.py +++ b/tests/eth2/beacon/state_machines/test_state_transition.py @@ -104,6 +104,6 @@ def test_per_slot_transition(base_db, state_roots=state.latest_state_roots, slots_per_historical_root=config.SLOTS_PER_HISTORICAL_ROOT, ) - assert updated_state.historical_roots[-1] == historical_batch.tree_root + assert updated_state.historical_roots[-1] == historical_batch.root else: assert updated_state.historical_roots == state.historical_roots diff --git a/tests/eth2/beacon/test_helpers.py b/tests/eth2/beacon/test_helpers.py index 5859eb73d4..f7dc34c0ca 100644 --- a/tests/eth2/beacon/test_helpers.py +++ b/tests/eth2/beacon/test_helpers.py @@ -54,7 +54,7 @@ def test_get_temporary_block_header(sample_block): assert header.slot == sample_block.slot assert header.previous_block_root == sample_block.previous_block_root assert header.state_root == ZERO_HASH32 - assert header.block_body_root == sample_block.body.tree_root + assert header.block_body_root == sample_block.body.root assert header.signature == sample_block.signature diff --git a/tests/eth2/beacon/types/test_block.py b/tests/eth2/beacon/types/test_block.py index 289c9f293a..c9918e0283 100644 --- a/tests/eth2/beacon/types/test_block.py +++ b/tests/eth2/beacon/types/test_block.py @@ -47,4 +47,4 @@ def test_block_root_and_block_header_root_equivalence(sample_block): block = sample_block header = block.header - assert block.tree_root == header.tree_root + assert block.root == header.root diff --git a/tests/eth2/beacon/types/test_slashable_attestation.py b/tests/eth2/beacon/types/test_slashable_attestation.py index abbc41c86f..b089c3851c 100644 --- a/tests/eth2/beacon/types/test_slashable_attestation.py +++ b/tests/eth2/beacon/types/test_slashable_attestation.py @@ -31,7 +31,7 @@ def test_root(sample_slashable_attestation_params): # NOTE: see note in `test_hash`, this test will need to be updated # once ssz tree hash lands... - assert slashable_attestation.root == slashable_attestation.hash + assert slashable_attestation.root == slashable_attestation.root @pytest.mark.parametrize( diff --git a/tests/eth2/beacon/types/test_states.py b/tests/eth2/beacon/types/test_states.py index 9c624fce0c..bfef17fe21 100644 --- a/tests/eth2/beacon/types/test_states.py +++ b/tests/eth2/beacon/types/test_states.py @@ -73,11 +73,6 @@ def test_num_crosslink_records(expected, assert state.num_crosslinks == expected -def test_hash(sample_beacon_state_params): - state = BeaconState(**sample_beacon_state_params) - assert state.root == hash_eth2(ssz.encode(state)) - - @pytest.mark.parametrize( 'validator_index, new_pubkey, new_balance', [ From dccd35d1d1b2f791c77f26d5d88bbf33456cc624 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Fri, 26 Apr 2019 13:23:21 +0800 Subject: [PATCH 06/19] wrap ssz signing_root --- eth2/beacon/chains/base.py | 4 +- eth2/beacon/db/chain.py | 30 +++++++------- eth2/beacon/deposit_helpers.py | 4 +- .../forks/serenity/block_validation.py | 14 +++---- .../forks/serenity/slot_processing.py | 3 +- eth2/beacon/tools/builder/proposer.py | 3 +- eth2/beacon/tools/builder/validator.py | 12 ++---- eth2/beacon/types/blocks.py | 40 +++++++++---------- eth2/beacon/types/deposit_input.py | 23 +++++------ eth2/beacon/types/voluntary_exits.py | 15 +++---- tests/core/p2p-proto/bcc/helpers.py | 2 +- tests/core/p2p-proto/bcc/test_handshake.py | 2 +- tests/core/p2p-proto/bcc/test_requests.py | 6 +-- tests/core/p2p-proto/bcc/test_validators.py | 20 +++++----- tests/eth2/beacon/chains/test_beacon_chain.py | 6 +-- tests/eth2/beacon/conftest.py | 2 +- tests/eth2/beacon/db/test_beacon_chaindb.py | 30 +++++++------- tests/eth2/beacon/helpers.py | 2 +- .../forks/test_serenity_block_validation.py | 2 +- .../test_serenity_operation_processing.py | 4 +- tests/eth2/beacon/state_machines/test_demo.py | 2 +- tests/eth2/beacon/test_helpers.py | 6 +-- tests/eth2/beacon/types/test_states.py | 3 -- trinity/protocol/bcc/peer.py | 6 +-- trinity/protocol/bcc/servers.py | 2 +- trinity/protocol/bcc/validators.py | 6 +-- trinity/sync/beacon/chain.py | 4 +- 27 files changed, 117 insertions(+), 136 deletions(-) diff --git a/eth2/beacon/chains/base.py b/eth2/beacon/chains/base.py index d9b79f86fa..2d9fde7fe4 100644 --- a/eth2/beacon/chains/base.py +++ b/eth2/beacon/chains/base.py @@ -370,7 +370,7 @@ def import_block( "Attempt to import block #{}. Cannot import block {} before importing " "its parent block at {}".format( block.slot, - block.signed_root, + block.signing_root, block.previous_block_root, ) ) @@ -395,7 +395,7 @@ def import_block( self.logger.debug( 'IMPORTED_BLOCK: slot %s | signed root %s', imported_block.slot, - encode_hex(imported_block.signed_root), + encode_hex(imported_block.signing_root), ) return imported_block, new_canonical_blocks, old_canonical_blocks diff --git a/eth2/beacon/db/chain.py b/eth2/beacon/db/chain.py index f183758327..bffe20e059 100644 --- a/eth2/beacon/db/chain.py +++ b/eth2/beacon/db/chain.py @@ -375,7 +375,7 @@ def _set_block_scores_to_db( score = block.slot db.set( - SchemaV1.make_block_root_to_score_lookup_key(block.signed_root), + SchemaV1.make_block_root_to_score_lookup_key(block.signing_root), ssz.encode(score, sedes=ssz.sedes.uint64), ) return score @@ -395,7 +395,7 @@ def _persist_block_chain( return tuple(), tuple() try: - previous_canonical_head = cls._get_canonical_head(db, block_class).signed_root + previous_canonical_head = cls._get_canonical_head(db, block_class).signing_root head_score = cls._get_score(db, previous_canonical_head) except CanonicalHeadNotFound: no_canonical_head = True @@ -406,7 +406,7 @@ def _persist_block_chain( if not is_genesis and not cls._block_exists(db, first_block.previous_block_root): raise ParentNotFound( "Cannot persist block ({}) with unknown parent ({})".format( - encode_hex(first_block.signed_root), + encode_hex(first_block.signing_root), encode_hex(first_block.previous_block_root), ) ) @@ -416,14 +416,14 @@ def _persist_block_chain( # TODO: this should probably be done as part of the fork choice rule processing db.set( SchemaV1.make_finalized_head_root_lookup_key(), - first_block.signed_root, + first_block.signing_root, ) else: score = first_block.slot curr_block_head = first_block db.set( - curr_block_head.signed_root, + curr_block_head.signing_root, ssz.encode(curr_block_head), ) cls._add_block_root_to_slot_lookup(db, curr_block_head) @@ -432,28 +432,28 @@ def _persist_block_chain( orig_blocks_seq = concat([(first_block,), blocks_iterator]) for parent, child in sliding_window(2, orig_blocks_seq): - if parent.signed_root != child.previous_block_root: + if parent.signing_root != child.previous_block_root: raise ValidationError( "Non-contiguous chain. Expected {} to have {} as parent but was {}".format( - encode_hex(child.signed_root), - encode_hex(parent.signed_root), + encode_hex(child.signing_root), + encode_hex(parent.signing_root), encode_hex(child.previous_block_root), ) ) curr_block_head = child db.set( - curr_block_head.signed_root, + curr_block_head.signing_root, ssz.encode(curr_block_head), ) cls._add_block_root_to_slot_lookup(db, curr_block_head) score = cls._set_block_scores_to_db(db, curr_block_head) if no_canonical_head: - return cls._set_as_canonical_chain_head(db, curr_block_head.signed_root, block_class) + return cls._set_as_canonical_chain_head(db, curr_block_head.signing_root, block_class) if score > head_score: - return cls._set_as_canonical_chain_head(db, curr_block_head.signed_root, block_class) + return cls._set_as_canonical_chain_head(db, curr_block_head.signing_root, block_class) else: return tuple(), tuple() @@ -494,7 +494,7 @@ def _set_as_canonical_chain_head( for block in new_canonical_blocks: cls._add_block_slot_to_root_lookup(db, block) - db.set(SchemaV1.make_canonical_head_root_lookup_key(), block.signed_root) + db.set(SchemaV1.make_canonical_head_root_lookup_key(), block.signing_root) return new_canonical_blocks, tuple(old_canonical_blocks) @@ -523,7 +523,7 @@ def _find_new_ancestors( # This just means the block is not on the canonical chain. pass else: - if orig.signed_root == block.signed_root: + if orig.signing_root == block.signing_root: # Found the common ancestor, stop. break @@ -546,7 +546,7 @@ def _add_block_slot_to_root_lookup(db: BaseDB, block: BaseBeaconBlock) -> None: ) db.set( block_slot_to_root_key, - ssz.encode(block.signed_root, sedes=ssz.sedes.byte_list), + ssz.encode(block.signing_root, sedes=ssz.sedes.byte_list), ) @staticmethod @@ -556,7 +556,7 @@ def _add_block_root_to_slot_lookup(db: BaseDB, block: BaseBeaconBlock) -> None: block root. """ block_root_to_slot_key = SchemaV1.make_block_root_to_slot_lookup_key( - block.signed_root + block.signing_root ) db.set( block_root_to_slot_key, diff --git a/eth2/beacon/deposit_helpers.py b/eth2/beacon/deposit_helpers.py index 7381e72b4c..657bef572d 100644 --- a/eth2/beacon/deposit_helpers.py +++ b/eth2/beacon/deposit_helpers.py @@ -115,8 +115,8 @@ def process_deposit(state: BeaconState, # Verify the proof of possession proof_is_valid = bls.verify( pubkey=pubkey, - message_hash=deposit_input.signed_root, - signature=deposit_input.proof_of_possession, + message_hash=deposit_input.signing_root, + signature=deposit_input.signature, domain=get_domain( state.fork, state.current_epoch(slots_per_epoch), diff --git a/eth2/beacon/state_machines/forks/serenity/block_validation.py b/eth2/beacon/state_machines/forks/serenity/block_validation.py index f8e9691535..45f198b82d 100644 --- a/eth2/beacon/state_machines/forks/serenity/block_validation.py +++ b/eth2/beacon/state_machines/forks/serenity/block_validation.py @@ -87,7 +87,7 @@ def validate_block_slot(state: BeaconState, def validate_block_previous_root(state: BeaconState, block: BaseBeaconBlock) -> None: - expected_root = state.latest_block_header.signed_root + expected_root = state.latest_block_header.signing_root previous_root = block.previous_block_root if previous_root != expected_root: raise ValidationError( @@ -103,9 +103,7 @@ def validate_proposer_signature(state: BeaconState, block: BaseBeaconBlock, beacon_chain_shard_number: Shard, committee_config: CommitteeConfig) -> None: - - # TODO: Replace this with real signed_root - message_hash = block.signed_root + message_hash = block.signing_root # Get the public key of proposer beacon_proposer_index = get_beacon_proposer_index( @@ -200,7 +198,7 @@ def validate_block_header_signature(header: BeaconBlockHeader, slots_per_epoch: int) -> None: header_signature_is_valid = bls.verify( pubkey=pubkey, - message_hash=header.signed_root, # TODO: use signed_root + message_hash=header.signing_root, signature=header.signature, domain=get_domain( fork, @@ -211,7 +209,7 @@ def validate_block_header_signature(header: BeaconBlockHeader, if not header_signature_is_valid: raise ValidationError( "Header signature is invalid: " - f"proposer pubkey: {pubkey}, message_hash: {header.signed_root}, " + f"proposer pubkey: {pubkey}, message_hash: {header.signing_root}, " f"signature: {header.signature}" ) @@ -766,7 +764,7 @@ def validate_voluntary_exit_signature(state: BeaconState, domain = get_domain(state.fork, voluntary_exit.epoch, SignatureDomain.DOMAIN_VOLUNTARY_EXIT) is_valid_signature = bls.verify( pubkey=validator.pubkey, - message_hash=voluntary_exit.signed_root, + message_hash=voluntary_exit.signing_root, signature=voluntary_exit.signature, domain=domain, ) @@ -774,6 +772,6 @@ def validate_voluntary_exit_signature(state: BeaconState, if not is_valid_signature: raise ValidationError( f"Invalid VoluntaryExit signature, validator_index={voluntary_exit.validator_index}, " - f"pubkey={validator.pubkey}, message_hash={voluntary_exit.signed_root}," + f"pubkey={validator.pubkey}, message_hash={voluntary_exit.signing_root}," f"signature={voluntary_exit.signature}, domain={domain}" ) diff --git a/eth2/beacon/state_machines/forks/serenity/slot_processing.py b/eth2/beacon/state_machines/forks/serenity/slot_processing.py index ba7d5ec3c1..f1ac02c7c5 100644 --- a/eth2/beacon/state_machines/forks/serenity/slot_processing.py +++ b/eth2/beacon/state_machines/forks/serenity/slot_processing.py @@ -53,8 +53,7 @@ def process_cache_state(state: BeaconState, config: Eth2Config) -> BeaconState: state.latest_block_roots, state.slot, slots_per_historical_root, - # TODO make `signed_root` - state.latest_block_header.signed_root, + state.latest_block_header.signing_root, ) state = state.copy( diff --git a/eth2/beacon/tools/builder/proposer.py b/eth2/beacon/tools/builder/proposer.py index 471e2dc2bb..b48c7c1616 100644 --- a/eth2/beacon/tools/builder/proposer.py +++ b/eth2/beacon/tools/builder/proposer.py @@ -131,9 +131,8 @@ def create_block_on_state( state, block = state_machine.import_block(block, check_proposer_signature=False) # Sign - # TODO make sure we use the correct signed_root signature = sign_transaction( - message_hash=block.signed_root, + message_hash=block.signing_root, privkey=privkey, fork=state.fork, slot=slot, diff --git a/eth2/beacon/tools/builder/validator.py b/eth2/beacon/tools/builder/validator.py index 277f9b2572..1b13213687 100644 --- a/eth2/beacon/tools/builder/validator.py +++ b/eth2/beacon/tools/builder/validator.py @@ -25,7 +25,6 @@ ZERO_HASH32, ) from py_ecc import bls -import ssz from eth2._utils.bitfield import ( get_empty_bitfield, @@ -35,9 +34,6 @@ CommitteeConfig, Eth2Config, ) -from eth2.beacon._utils.hash import ( - hash_eth2, -) from eth2.beacon.constants import ( ZERO_TIMESTAMP, ) @@ -160,7 +156,7 @@ def sign_proof_of_possession(deposit_input: DepositInput, SignatureDomain.DOMAIN_DEPOSIT, ) return bls.sign( - message_hash=hash_eth2(ssz.encode(deposit_input)), + message_hash=deposit_input.signing_root, privkey=privkey, domain=domain, ) @@ -199,7 +195,7 @@ def create_block_header_with_signature( block_body_root=block_body_root, ) block_header_signature = sign_transaction( - message_hash=block_header.signed_root, + message_hash=block_header.signing_root, privkey=privkey, fork=state.fork, slot=block_header.slot, @@ -548,7 +544,7 @@ def create_mock_voluntary_exit(state: BeaconState, ) return voluntary_exit.copy( signature=sign_transaction( - message_hash=voluntary_exit.signed_root, + message_hash=voluntary_exit.signing_root, privkey=keymap[state.validator_registry[validator_index].pubkey], fork=state.fork, slot=get_epoch_start_slot(current_epoch, config.SLOTS_PER_EPOCH), @@ -576,7 +572,7 @@ def create_deposit_data(*, deposit_input=DepositInput( pubkey=pubkey, withdrawal_credentials=withdrawal_credentials, - proof_of_possession=sign_proof_of_possession( + signature=sign_proof_of_possession( deposit_input=DepositInput( pubkey=pubkey, withdrawal_credentials=withdrawal_credentials, diff --git a/eth2/beacon/types/blocks.py b/eth2/beacon/types/blocks.py index f15a2a706d..28a221187f 100644 --- a/eth2/beacon/types/blocks.py +++ b/eth2/beacon/types/blocks.py @@ -52,7 +52,7 @@ from eth2.beacon.db.chain import BaseBeaconChainDB # noqa: F401 -class BeaconBlockHeader(ssz.Serializable): +class BeaconBlockHeader(ssz.SignedSerializable): fields = [ ('slot', uint64), @@ -80,7 +80,7 @@ def __init__(self, def __repr__(self) -> str: return ''.format( self.slot, - encode_hex(self.signed_root)[2:10], + encode_hex(self.signing_root)[2:10], ) _root = None @@ -89,14 +89,13 @@ def __repr__(self) -> str: def root(self) -> Hash32: return super().root - _signed_root = None + _signing_root = None @property - def signed_root(self) -> Hash32: - # TODO Use SSZ built-in function - if self._signed_root is None: - self._signed_root = ssz.hash_tree_root(self.copy(signature=EMPTY_SIGNATURE)) - return Hash32(self._signed_root) + def signing_root(self) -> Hash32: + if self._signing_root is None: + self._signing_root = super().signing_root + return Hash32(self._signing_root) class BeaconBlockBody(ssz.Serializable): @@ -180,7 +179,7 @@ def root(self) -> Hash32: return super().root -class BaseBeaconBlock(ssz.Serializable, Configurable, ABC): +class BaseBeaconBlock(ssz.SignedSerializable, Configurable, ABC): fields = [ # # Header @@ -212,11 +211,11 @@ def __init__(self, signature=signature, ) - def __repr__(self) -> str: - return ''.format( - self.slot, - encode_hex(self.signed_root)[2:10], - ) + # def __repr__(self) -> str: + # return ''.format( + # self.slot, + # encode_hex(self.signing_root)[2:10], + # ) _root = None @@ -224,14 +223,13 @@ def __repr__(self) -> str: def root(self) -> Hash32: return super().root - _signed_root = None + _signing_root = None @property - def signed_root(self) -> Hash32: - # TODO Use SSZ built-in function - if self._signed_root is None: - self._signed_root = ssz.hash_tree_root(self.copy(signature=EMPTY_SIGNATURE)) - return Hash32(self._signed_root) + def signing_root(self) -> Hash32: + if self._signing_root is None: + self._signing_root = super().signing_root + return Hash32(self._signing_root) @property def num_attestations(self) -> int: @@ -309,7 +307,7 @@ def from_parent(cls, return cls( slot=slot, - previous_block_root=parent_block.signed_root, + previous_block_root=parent_block.signing_root, state_root=parent_block.state_root, body=cls.block_body_class.create_empty_body(), signature=EMPTY_SIGNATURE, diff --git a/eth2/beacon/types/deposit_input.py b/eth2/beacon/types/deposit_input.py index 64f4a084e0..75e44802f3 100644 --- a/eth2/beacon/types/deposit_input.py +++ b/eth2/beacon/types/deposit_input.py @@ -10,11 +10,10 @@ bytes96, ) -from eth2.beacon._utils.hash import hash_eth2 from eth2.beacon.constants import EMPTY_SIGNATURE -class DepositInput(ssz.Serializable): +class DepositInput(ssz.SignedSerializable): fields = [ # BLS pubkey @@ -22,17 +21,17 @@ class DepositInput(ssz.Serializable): # Withdrawal credentials ('withdrawal_credentials', bytes32), # BLS proof of possession (a BLS signature) - ('proof_of_possession', bytes96), + ('signature', bytes96), ] def __init__(self, pubkey: BLSPubkey, withdrawal_credentials: Hash32, - proof_of_possession: BLSSignature=EMPTY_SIGNATURE) -> None: + signature: BLSSignature=EMPTY_SIGNATURE) -> None: super().__init__( pubkey=pubkey, withdrawal_credentials=withdrawal_credentials, - proof_of_possession=proof_of_possession, + signature=signature, ) _root = None @@ -41,13 +40,11 @@ def __init__(self, def root(self) -> Hash32: return super().root - _signed_root = None + _signing_root = None @property - def signed_root(self) -> Hash32: - # TODO: Use SSZ built-in function - if self._signed_root is None: - self._signed_root = hash_eth2( - ssz.encode(self.copy(proof_of_possession=EMPTY_SIGNATURE)) - ) - return self._signed_root + def signing_root(self) -> Hash32: + # Use SSZ built-in function + if self._signing_root is None: + self._signing_root = super().signing_root + return self._signing_root diff --git a/eth2/beacon/types/voluntary_exits.py b/eth2/beacon/types/voluntary_exits.py index 64bca2c47a..349438fb3b 100644 --- a/eth2/beacon/types/voluntary_exits.py +++ b/eth2/beacon/types/voluntary_exits.py @@ -8,9 +8,6 @@ uint64, ) -from eth2.beacon._utils.hash import ( - hash_eth2, -) from eth2.beacon.typing import ( Epoch, ValidatorIndex, @@ -18,7 +15,7 @@ from eth2.beacon.constants import EMPTY_SIGNATURE -class VoluntaryExit(ssz.Serializable): +class VoluntaryExit(ssz.SignedSerializable): fields = [ # Minimum epoch for processing exit @@ -39,11 +36,11 @@ def __init__(self, signature, ) - _signed_root = None + _signing_root = None @property - def signed_root(self) -> Hash32: + def signing_root(self) -> Hash32: # Use SSZ built-in function - if self._signed_root is None: - self._signed_root = hash_eth2(ssz.encode(self.copy(signature=EMPTY_SIGNATURE))) - return self._signed_root + if self._signing_root is None: + self._signing_root = super().signing_root + return self._signing_root diff --git a/tests/core/p2p-proto/bcc/helpers.py b/tests/core/p2p-proto/bcc/helpers.py index a566f78d03..bd6089f980 100644 --- a/tests/core/p2p-proto/bcc/helpers.py +++ b/tests/core/p2p-proto/bcc/helpers.py @@ -76,7 +76,7 @@ def create_test_block(parent=None, **kwargs): } if parent is not None: - kwargs["previous_block_root"] = parent.signed_root + kwargs["previous_block_root"] = parent.signing_root kwargs["slot"] = parent.slot + 1 return BeaconBlock(**merge(defaults, kwargs)) diff --git a/tests/core/p2p-proto/bcc/test_handshake.py b/tests/core/p2p-proto/bcc/test_handshake.py index 900ea1ac26..0b5eb663cf 100644 --- a/tests/core/p2p-proto/bcc/test_handshake.py +++ b/tests/core/p2p-proto/bcc/test_handshake.py @@ -59,7 +59,7 @@ async def test_unidirectional_handshake(): SERENITY_CONFIG.GENESIS_SLOT, BeaconBlock, ) - alice_genesis_root = alice_genesis.signed_root + alice_genesis_root = alice_genesis.signing_root alice_head = await alice_chain_db.coro_get_canonical_head(BeaconBlock) alice_head_slot = alice_head.slot diff --git a/tests/core/p2p-proto/bcc/test_requests.py b/tests/core/p2p-proto/bcc/test_requests.py index bce02dcede..d6e9ee11df 100644 --- a/tests/core/p2p-proto/bcc/test_requests.py +++ b/tests/core/p2p-proto/bcc/test_requests.py @@ -59,7 +59,7 @@ async def test_get_single_block_by_slot(request, event_loop): chain_db = await get_chain_db((block,)) alice, response_buffer = await get_request_server_setup(request, event_loop, chain_db) - alice.sub_proto.send_get_blocks(block.signed_root, 1, request_id=5) + alice.sub_proto.send_get_blocks(block.signing_root, 1, request_id=5) response = await response_buffer.msg_queue.get() assert isinstance(response.command, BeaconBlocks) @@ -173,7 +173,7 @@ async def test_get_canonical_block_range_by_root(request, event_loop): alice, response_buffer = await get_request_server_setup(request, event_loop, chain_db) - alice.sub_proto.send_get_blocks(base_branch[1].signed_root, 4, request_id=5) + alice.sub_proto.send_get_blocks(base_branch[1].signing_root, 4, request_id=5) response = await response_buffer.msg_queue.get() assert isinstance(response.command, BeaconBlocks) @@ -223,7 +223,7 @@ async def test_get_non_canonical_branch(request, event_loop): alice, response_buffer = await get_request_server_setup(request, event_loop, chain_db) - alice.sub_proto.send_get_blocks(non_canonical_branch[1].signed_root, 3, request_id=5) + alice.sub_proto.send_get_blocks(non_canonical_branch[1].signing_root, 3, request_id=5) response = await response_buffer.msg_queue.get() assert isinstance(response.command, BeaconBlocks) diff --git a/tests/core/p2p-proto/bcc/test_validators.py b/tests/core/p2p-proto/bcc/test_validators.py index 757a9ea1e9..5d3cc865ea 100644 --- a/tests/core/p2p-proto/bcc/test_validators.py +++ b/tests/core/p2p-proto/bcc/test_validators.py @@ -26,10 +26,10 @@ (10, 100, TEST_BRANCH), - (TEST_BRANCH[0].signed_root, 100, ()), - (TEST_BRANCH[0].signed_root, 100, TEST_BRANCH[:1]), - (TEST_BRANCH[0].signed_root, 100, TEST_BRANCH[:2]), - (TEST_BRANCH[0].signed_root, 100, TEST_BRANCH), + (TEST_BRANCH[0].signing_root, 100, ()), + (TEST_BRANCH[0].signing_root, 100, TEST_BRANCH[:1]), + (TEST_BRANCH[0].signing_root, 100, TEST_BRANCH[:2]), + (TEST_BRANCH[0].signing_root, 100, TEST_BRANCH), ]) def test_valid_beacon_blocks_validation(block_slot_or_hash, max_blocks, result): validator = BeaconBlocksValidator(10, 100) @@ -46,12 +46,12 @@ def test_valid_beacon_blocks_validation(block_slot_or_hash, max_blocks, result): (11, 100, TEST_BRANCH), (9, 100, TEST_BRANCH), - (TEST_BRANCH[0].signed_root, 100, TEST_BRANCH[1:]), - (TEST_BRANCH[0].signed_root, 100, TEST_BRANCH[::-1]), - (TEST_BRANCH[0].signed_root, 100, (TEST_BRANCH[0], TEST_BRANCH[2])), - (TEST_BRANCH[0].signed_root, 100, (TEST_BRANCH[0], TEST_BRANCH[2], TEST_BRANCH[1])), - (TEST_BRANCH[0].signed_root, 100, (TEST_BRANCH[0],) + TEST_BRANCH[1::-1]), - (TEST_BRANCH[0].signed_root, 99, TEST_BRANCH), + (TEST_BRANCH[0].signing_root, 100, TEST_BRANCH[1:]), + (TEST_BRANCH[0].signing_root, 100, TEST_BRANCH[::-1]), + (TEST_BRANCH[0].signing_root, 100, (TEST_BRANCH[0], TEST_BRANCH[2])), + (TEST_BRANCH[0].signing_root, 100, (TEST_BRANCH[0], TEST_BRANCH[2], TEST_BRANCH[1])), + (TEST_BRANCH[0].signing_root, 100, (TEST_BRANCH[0],) + TEST_BRANCH[1::-1]), + (TEST_BRANCH[0].signing_root, 99, TEST_BRANCH), (b"\x00" * 32, 100, TEST_BRANCH), ]) def test_invalid_beacon_blocks_validation(block_slot_or_hash, max_blocks, result): diff --git a/tests/eth2/beacon/chains/test_beacon_chain.py b/tests/eth2/beacon/chains/test_beacon_chain.py index e41dac3b27..3943204abb 100644 --- a/tests/eth2/beacon/chains/test_beacon_chain.py +++ b/tests/eth2/beacon/chains/test_beacon_chain.py @@ -47,7 +47,7 @@ def test_canonical_chain(valid_chain, genesis_slot): block = genesis_block.copy( slot=genesis_block.slot + 1, - previous_block_root=genesis_block.signed_root, + previous_block_root=genesis_block.signing_root, ) valid_chain.chaindb.persist_block(block, block.__class__) @@ -58,7 +58,7 @@ def test_canonical_chain(valid_chain, genesis_slot): ) assert canonical_block_1 == block - result_block = valid_chain.get_block_by_root(block.signed_root) + result_block = valid_chain.get_block_by_root(block.signing_root) assert result_block == block @@ -98,7 +98,7 @@ def test_import_blocks(valid_chain, assert block == valid_chain.get_canonical_block_by_slot( block.slot ) - assert block.signed_root == valid_chain.get_canonical_block_root( + assert block.signing_root == valid_chain.get_canonical_block_root( block.slot ) blocks += (block,) diff --git a/tests/eth2/beacon/conftest.py b/tests/eth2/beacon/conftest.py index 11ff3a65ae..98fdd2bffc 100644 --- a/tests/eth2/beacon/conftest.py +++ b/tests/eth2/beacon/conftest.py @@ -240,7 +240,7 @@ def sample_deposit_input_params(): return { 'pubkey': 123, 'withdrawal_credentials': b'\11' * 32, - 'proof_of_possession': (0, 0), + 'signature': (0, 0), } diff --git a/tests/eth2/beacon/db/test_beacon_chaindb.py b/tests/eth2/beacon/db/test_beacon_chaindb.py index 023cae1283..7faa98825d 100644 --- a/tests/eth2/beacon/db/test_beacon_chaindb.py +++ b/tests/eth2/beacon/db/test_beacon_chaindb.py @@ -58,13 +58,13 @@ def test_chaindb_add_block_number_to_root_lookup(chaindb, block): def test_chaindb_persist_block_and_slot_to_root(chaindb, block): with pytest.raises(BlockNotFound): - chaindb.get_block_by_root(block.signed_root, block.__class__) - slot_to_root_key = SchemaV1.make_block_root_to_score_lookup_key(block.signed_root) + chaindb.get_block_by_root(block.signing_root, block.__class__) + slot_to_root_key = SchemaV1.make_block_root_to_score_lookup_key(block.signing_root) assert not chaindb.exists(slot_to_root_key) chaindb.persist_block(block, block.__class__) - assert chaindb.get_block_by_root(block.signed_root, block.__class__) == block + assert chaindb.get_block_by_root(block.signing_root, block.__class__) == block assert chaindb.exists(slot_to_root_key) @@ -76,7 +76,7 @@ def test_chaindb_persist_block_and_unknown_parent(chaindb, block, seed): def test_chaindb_persist_block_and_block_to_root(chaindb, block): - block_to_root_key = SchemaV1.make_block_root_to_score_lookup_key(block.signed_root) + block_to_root_key = SchemaV1.make_block_root_to_score_lookup_key(block.signing_root) assert not chaindb.exists(block_to_root_key) chaindb.persist_block(block, block.__class__) assert chaindb.exists(block_to_root_key) @@ -89,33 +89,33 @@ def test_chaindb_get_score(chaindb, sample_beacon_block_params): ) chaindb.persist_block(genesis, genesis.__class__) - genesis_score_key = SchemaV1.make_block_root_to_score_lookup_key(genesis.signed_root) + genesis_score_key = SchemaV1.make_block_root_to_score_lookup_key(genesis.signing_root) genesis_score = ssz.decode(chaindb.db.get(genesis_score_key), sedes=ssz.sedes.uint64) assert genesis_score == 0 - assert chaindb.get_score(genesis.signed_root) == 0 + assert chaindb.get_score(genesis.signing_root) == 0 block1 = BeaconBlock(**sample_beacon_block_params).copy( - previous_block_root=genesis.signed_root, + previous_block_root=genesis.signing_root, slot=1, ) chaindb.persist_block(block1, block1.__class__) - block1_score_key = SchemaV1.make_block_root_to_score_lookup_key(block1.signed_root) + block1_score_key = SchemaV1.make_block_root_to_score_lookup_key(block1.signing_root) block1_score = ssz.decode(chaindb.db.get(block1_score_key), sedes=ssz.sedes.uint64) assert block1_score == 1 - assert chaindb.get_score(block1.signed_root) == 1 + assert chaindb.get_score(block1.signing_root) == 1 def test_chaindb_get_block_by_root(chaindb, block): chaindb.persist_block(block, block.__class__) - result_block = chaindb.get_block_by_root(block.signed_root, block.__class__) + result_block = chaindb.get_block_by_root(block.signing_root, block.__class__) validate_ssz_equal(result_block, block) def test_chaindb_get_canonical_block_root(chaindb, block): chaindb.persist_block(block, block.__class__) block_root = chaindb.get_canonical_block_root(block.slot) - assert block_root == block.signed_root + assert block_root == block.signing_root def test_chaindb_state(chaindb, state): @@ -136,14 +136,14 @@ def test_chaindb_get_canonical_head(chaindb, block): chaindb.persist_block(block, block.__class__) canonical_head_root = chaindb.get_canonical_head_root() - assert canonical_head_root == block.signed_root + assert canonical_head_root == block.signing_root result_block = chaindb.get_canonical_head(block.__class__) assert result_block == block block_2 = block.copy( slot=block.slot + 1, - previous_block_root=block.signed_root, + previous_block_root=block.signing_root, ) chaindb.persist_block(block_2, block_2.__class__) result_block = chaindb.get_canonical_head(block.__class__) @@ -151,7 +151,7 @@ def test_chaindb_get_canonical_head(chaindb, block): block_3 = block.copy( slot=block_2.slot + 1, - previous_block_root=block_2.signed_root, + previous_block_root=block_2.signing_root, ) chaindb.persist_block(block_3, block_3.__class__) result_block = chaindb.get_canonical_head(block.__class__) @@ -161,5 +161,5 @@ def test_chaindb_get_canonical_head(chaindb, block): def test_get_slot_by_root(chaindb, block): chaindb.persist_block(block, block.__class__) block_slot = block.slot - result_slot = chaindb.get_slot_by_root(block.signed_root) + result_slot = chaindb.get_slot_by_root(block.signing_root) assert result_slot == block_slot diff --git a/tests/eth2/beacon/helpers.py b/tests/eth2/beacon/helpers.py index 96a09ce72e..38ff672089 100644 --- a/tests/eth2/beacon/helpers.py +++ b/tests/eth2/beacon/helpers.py @@ -38,6 +38,6 @@ def get_pseudo_chain(length, genesis_block): for slot in range(1, length * 3): block = genesis_block.copy( slot=slot, - previous_block_root=block.signed_root + previous_block_root=block.signing_root ) yield block diff --git a/tests/eth2/beacon/state_machines/forks/test_serenity_block_validation.py b/tests/eth2/beacon/state_machines/forks/test_serenity_block_validation.py index 20807abce3..7197316ac1 100644 --- a/tests/eth2/beacon/state_machines/forks/test_serenity_block_validation.py +++ b/tests/eth2/beacon/state_machines/forks/test_serenity_block_validation.py @@ -111,7 +111,7 @@ def test_validate_proposer_signature( proposed_block = block.copy( signature=bls.sign( - message_hash=header.signed_root, + message_hash=header.signing_root, privkey=proposer_privkey, domain=get_domain( Fork( diff --git a/tests/eth2/beacon/state_machines/forks/test_serenity_operation_processing.py b/tests/eth2/beacon/state_machines/forks/test_serenity_operation_processing.py index 4f08800986..5ea15cb9ca 100644 --- a/tests/eth2/beacon/state_machines/forks/test_serenity_operation_processing.py +++ b/tests/eth2/beacon/state_machines/forks/test_serenity_operation_processing.py @@ -56,7 +56,7 @@ def test_process_max_attestations(genesis_state, genesis_block, ), attestation_slot=attestation_slot, - beacon_block_root=genesis_block.signed_root, + beacon_block_root=genesis_block.signing_root, keymap=keymap, voted_attesters_ratio=1.0, ) @@ -272,7 +272,7 @@ def test_process_attestations(genesis_state, genesis_block, ), attestation_slot=attestation_slot, - beacon_block_root=genesis_block.signed_root, + beacon_block_root=genesis_block.signing_root, keymap=keymap, voted_attesters_ratio=1.0, ) diff --git a/tests/eth2/beacon/state_machines/test_demo.py b/tests/eth2/beacon/state_machines/test_demo.py index 581d7ff57c..1791ffaa86 100644 --- a/tests/eth2/beacon/state_machines/test_demo.py +++ b/tests/eth2/beacon/state_machines/test_demo.py @@ -99,7 +99,7 @@ def test_demo(base_db, block, ), attestation_slot=attestation_slot, - beacon_block_root=block.signed_root, + beacon_block_root=block.signing_root, keymap=keymap, voted_attesters_ratio=1.0, ) diff --git a/tests/eth2/beacon/test_helpers.py b/tests/eth2/beacon/test_helpers.py index f7dc34c0ca..bab8d1aa4b 100644 --- a/tests/eth2/beacon/test_helpers.py +++ b/tests/eth2/beacon/test_helpers.py @@ -68,7 +68,7 @@ def generate_mock_latest_historical_roots( chain_length = (current_slot // slots_per_epoch + 1) * slots_per_epoch blocks = get_pseudo_chain(chain_length, genesis_block) latest_block_roots = [ - block.signed_root + block.signing_root for block in blocks[:current_slot] ] + [ ZERO_HASH32 @@ -125,7 +125,7 @@ def test_get_block_root(sample_beacon_state_params, target_slot, slots_per_historical_root, ) - assert block_root == blocks[target_slot].signed_root + assert block_root == blocks[target_slot].signing_root else: with pytest.raises(ValidationError): get_block_root( @@ -180,7 +180,7 @@ def test_get_state_root(sample_beacon_state_params, target_slot, slots_per_historical_root, ) - assert state_root == blocks[target_slot].signed_root + assert state_root == blocks[target_slot].signing_root else: with pytest.raises(ValidationError): get_state_root( diff --git a/tests/eth2/beacon/types/test_states.py b/tests/eth2/beacon/types/test_states.py index bfef17fe21..b2f4ca634b 100644 --- a/tests/eth2/beacon/types/test_states.py +++ b/tests/eth2/beacon/types/test_states.py @@ -8,9 +8,6 @@ from eth2.beacon.types.crosslink_records import ( CrosslinkRecord, ) -from eth2.beacon._utils.hash import ( - hash_eth2, -) from tests.eth2.beacon.helpers import ( mock_validator_record, diff --git a/trinity/protocol/bcc/peer.py b/trinity/protocol/bcc/peer.py index 8785ba7da7..6a589f9672 100644 --- a/trinity/protocol/bcc/peer.py +++ b/trinity/protocol/bcc/peer.py @@ -59,7 +59,7 @@ async def send_sub_proto_handshake(self) -> None: BeaconBlock, ) head = await self.chain_db.coro_get_canonical_head(BeaconBlock) - self.sub_proto.send_handshake(genesis.signed_root, head.slot) + self.sub_proto.send_handshake(genesis.signing_root, head.slot) async def process_sub_proto_handshake(self, cmd: Command, msg: _DecodedMsgType) -> None: if not isinstance(cmd, Status): @@ -79,11 +79,11 @@ async def process_sub_proto_handshake(self, cmd: Command, msg: _DecodedMsgType) BeaconBlock, ) # TODO change message descriptor to 'genesis_root', accounting for the spec - if msg['genesis_hash'] != genesis_block.signed_root: + if msg['genesis_hash'] != genesis_block.signing_root: await self.disconnect(DisconnectReason.useless_peer) raise HandshakeFailure( f"{self} genesis ({encode_hex(msg['genesis_hash'])}) does not " - f"match ours ({encode_hex(genesis_block.signed_root)}), disconnecting" + f"match ours ({encode_hex(genesis_block.signing_root)}), disconnecting" ) self.head_slot = msg['head_slot'] diff --git a/trinity/protocol/bcc/servers.py b/trinity/protocol/bcc/servers.py index f50e639a06..5e0f04b08f 100644 --- a/trinity/protocol/bcc/servers.py +++ b/trinity/protocol/bcc/servers.py @@ -126,7 +126,7 @@ async def _get_blocks(self, # TODO: pass accurate `block_class: Type[BaseBeaconBlock]` under # per BeaconStateMachine fork block = await self.db.coro_get_canonical_block_by_slot(slot, BeaconBlock) - if block.previous_block_root == parent.signed_root: + if block.previous_block_root == parent.signing_root: yield block else: break diff --git a/trinity/protocol/bcc/validators.py b/trinity/protocol/bcc/validators.py index 63b08bacb9..4708503234 100644 --- a/trinity/protocol/bcc/validators.py +++ b/trinity/protocol/bcc/validators.py @@ -58,11 +58,11 @@ def _validate_first_block(self, blocks: Tuple[BaseBeaconBlock, ...]) -> None: f"returned block is from slot #{first_block.slot}" ) else: - if first_block.signed_root != self.block_slot_or_hash: + if first_block.signing_root != self.block_slot_or_hash: block_hash = cast(Hash32, self.block_slot_or_hash) raise ValidationError( f"Requested blocks starting with hash {encode_hex(block_hash)} " - f"but first returned block has hash {encode_hex(first_block.signed_root)}" + f"but first returned block has hash {encode_hex(first_block.signing_root)}" ) def _validate_number(self, blocks: Tuple[BaseBeaconBlock, ...]) -> None: @@ -80,7 +80,7 @@ def _validate_sequence(self, blocks: Tuple[BaseBeaconBlock, ...]) -> None: for parent, child in sliding_window(2, blocks): # check that the received blocks form a sequence of descendents connected by parent # hashes, starting with the oldest ancestor - if child.previous_block_root != parent.signed_root: + if child.previous_block_root != parent.signing_root: raise ValidationError( "Returned blocks are not a connected branch" ) diff --git a/trinity/sync/beacon/chain.py b/trinity/sync/beacon/chain.py index 00a4b9a7e4..79012f03fb 100644 --- a/trinity/sync/beacon/chain.py +++ b/trinity/sync/beacon/chain.py @@ -128,7 +128,7 @@ async def sync(self) -> None: except ValidationError: return else: - if batch[0].previous_block_root != last_block.signed_root: + if batch[0].previous_block_root != last_block.signing_root: self.logger.info(f"Received batch is not linked to previous one") break last_block = batch[-1] @@ -176,7 +176,7 @@ async def validate_first_batch(self, batch: Tuple[BaseBeaconBlock, ...]) -> None parent_slot, BeaconBlock, ) - if canonical_parent.signed_root != previous_block_root: + if canonical_parent.signing_root != previous_block_root: message = f"Peer has different block finalized at slot #{parent_slot}" self.logger.info(message) raise ValidationError(message) From ca2c2669159338b8be22ba61d575cebe97abc4a9 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Fri, 26 Apr 2019 13:39:32 +0800 Subject: [PATCH 07/19] Use tree_hash_root for `latest_active_index_roots` --- eth2/beacon/on_genesis.py | 13 ++++-------- .../forks/serenity/epoch_processing.py | 15 ++++---------- eth2/beacon/types/slashable_attestations.py | 1 - .../forks/test_serenity_epoch_processing.py | 20 ++++++++----------- 4 files changed, 16 insertions(+), 33 deletions(-) diff --git a/eth2/beacon/on_genesis.py b/eth2/beacon/on_genesis.py index 6ebcb7b777..d4361d8256 100644 --- a/eth2/beacon/on_genesis.py +++ b/eth2/beacon/on_genesis.py @@ -10,6 +10,7 @@ from eth.constants import ( ZERO_HASH32, ) +import ssz from eth2.beacon.deposit_helpers import ( process_deposit, @@ -29,7 +30,6 @@ from eth2.beacon.types.eth1_data import Eth1Data from eth2.beacon.types.forks import Fork from eth2.beacon.types.states import BeaconState -from eth2.beacon._utils.hash import hash_eth2 from eth2.beacon.typing import ( Epoch, Gwei, @@ -151,18 +151,13 @@ def get_genesis_beacon_state(*, activation_exit_delay=activation_exit_delay, ) - # TODO: chanege to hash_tree_root active_validator_indices = get_active_validator_indices( state.validator_registry, genesis_epoch, ) - genesis_active_index_root = hash_eth2( - b''.join( - [ - index.to_bytes(32, 'little') - for index in active_validator_indices - ] - ) + genesis_active_index_root = ssz.hash_tree_root( + active_validator_indices, + ssz.sedes.List(ssz.uint64), ) latest_active_index_roots = (genesis_active_index_root,) * latest_active_index_roots_length state = state.copy( diff --git a/eth2/beacon/state_machines/forks/serenity/epoch_processing.py b/eth2/beacon/state_machines/forks/serenity/epoch_processing.py index d511ae2257..cf8f823c3d 100644 --- a/eth2/beacon/state_machines/forks/serenity/epoch_processing.py +++ b/eth2/beacon/state_machines/forks/serenity/epoch_processing.py @@ -14,6 +14,7 @@ curry, first, ) +import ssz from eth2.beacon import helpers from eth2._utils.numeric import ( @@ -59,9 +60,6 @@ exit_validator, prepare_validator_for_withdrawal, ) -from eth2.beacon._utils.hash import ( - hash_eth2, -) from eth2.beacon.datastructures.inclusion_info import InclusionInfo from eth2.beacon.types.attestations import Attestation from eth2.beacon.types.pending_attestation_records import PendingAttestationRecord @@ -1041,18 +1039,13 @@ def _update_latest_active_index_roots(state: BeaconState, """ next_epoch = state.next_epoch(committee_config.SLOTS_PER_EPOCH) - # TODO: chanege to hash_tree_root active_validator_indices = get_active_validator_indices( state.validator_registry, Epoch(next_epoch + committee_config.ACTIVATION_EXIT_DELAY), ) - index_root = hash_eth2( - b''.join( - [ - index.to_bytes(32, 'little') - for index in active_validator_indices - ] - ) + index_root = ssz.hash_tree_root( + active_validator_indices, + ssz.sedes.List(ssz.uint64), ) latest_active_index_roots = update_tuple_item( diff --git a/eth2/beacon/types/slashable_attestations.py b/eth2/beacon/types/slashable_attestations.py index 1acea07135..0f95a254d0 100644 --- a/eth2/beacon/types/slashable_attestations.py +++ b/eth2/beacon/types/slashable_attestations.py @@ -83,7 +83,6 @@ def message_hashes(self) -> Tuple[Hash32, Hash32]: Build the message_hashes that validators are expected to sign for an ``AttesterSlashing`` operation. """ - # TODO: change to hash_tree_root when we have SSZ tree hashing return ( AttestationDataAndCustodyBit(data=self.data, custody_bit=False).root, AttestationDataAndCustodyBit(data=self.data, custody_bit=True).root, diff --git a/tests/eth2/beacon/state_machines/forks/test_serenity_epoch_processing.py b/tests/eth2/beacon/state_machines/forks/test_serenity_epoch_processing.py index 1a9d271863..e556428950 100644 --- a/tests/eth2/beacon/state_machines/forks/test_serenity_epoch_processing.py +++ b/tests/eth2/beacon/state_machines/forks/test_serenity_epoch_processing.py @@ -14,6 +14,7 @@ assoc, curry, ) +import ssz from eth2._utils.tuple import ( update_tuple_item, @@ -1569,18 +1570,13 @@ def test_update_latest_active_index_roots(genesis_state, result_state = _update_latest_active_index_roots(state, committee_config) - # TODO: chanege to hash_tree_root - index_root = hash_eth2( - b''.join( - [ - index.to_bytes(32, 'little') - for index in get_active_validator_indices( - state.validator_registry, - # TODO: change to `per-epoch` version - slot_to_epoch(state.slot, slots_per_epoch), - ) - ] - ) + index_root = ssz.hash_tree_root( + get_active_validator_indices( + state.validator_registry, + # TODO: change to `per-epoch` version + slot_to_epoch(state.slot, slots_per_epoch), + ), + ssz.sedes.List(ssz.uint64), ) target_epoch = state.next_epoch(slots_per_epoch) + activation_exit_delay From a1c45542da6ee34cf2ba83abc1da5d5b676bbac1 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Fri, 26 Apr 2019 13:58:02 +0800 Subject: [PATCH 08/19] PR feedback, thanks to Jannik --- tests/eth2/beacon/conftest.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/tests/eth2/beacon/conftest.py b/tests/eth2/beacon/conftest.py index 98fdd2bffc..51a01559f6 100644 --- a/tests/eth2/beacon/conftest.py +++ b/tests/eth2/beacon/conftest.py @@ -57,12 +57,16 @@ # SSZ @pytest.fixture(scope="function", autouse=True) def override_length(config): - BeaconState._meta.container_sedes.field_name_to_sedes["latest_randao_mixes"].length = config.LATEST_RANDAO_MIXES_LENGTH # noqa: E501 - BeaconState._meta.container_sedes.field_name_to_sedes["latest_crosslinks"].length = config.SHARD_COUNT # noqa: E501 - BeaconState._meta.container_sedes.field_name_to_sedes["latest_block_roots"].length = config.SLOTS_PER_HISTORICAL_ROOT # noqa: E501 - BeaconState._meta.container_sedes.field_name_to_sedes["latest_state_roots"].length = config.SLOTS_PER_HISTORICAL_ROOT # noqa: E501 - BeaconState._meta.container_sedes.field_name_to_sedes["latest_active_index_roots"].length = config.LATEST_ACTIVE_INDEX_ROOTS_LENGTH # noqa: E501 - BeaconState._meta.container_sedes.field_name_to_sedes["latest_slashed_balances"].length = config.LATEST_SLASHED_EXIT_LENGTH # noqa: E501 + vector_dict = { + "latest_randao_mixes": config.LATEST_RANDAO_MIXES_LENGTH, + "latest_crosslinks": config.SHARD_COUNT, + "latest_block_roots": config.SLOTS_PER_HISTORICAL_ROOT, + "latest_state_roots": config.SLOTS_PER_HISTORICAL_ROOT, + "latest_active_index_roots": config.LATEST_ACTIVE_INDEX_ROOTS_LENGTH, + "latest_slashed_balances": config.LATEST_SLASHED_EXIT_LENGTH, + } + for key, value in vector_dict.items(): + BeaconState._meta.container_sedes.field_name_to_sedes[key].length = value @pytest.fixture(scope="session") From 3a632e073e9ecedbedc14b1a31a5e89373e1480f Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Sat, 27 Apr 2019 22:24:20 +0800 Subject: [PATCH 09/19] Fix the SSZ objects dependency Fix the dependency between `BeaconBlockHeader`, `BeaconBlockBody` and `ProposerSlashing` by moving `BeaconBlockHeader` to `eth2/beacon/types/block_headers.py` --- eth2/beacon/tools/builder/validator.py | 8 ++- eth2/beacon/types/block_headers.py | 73 +++++++++++++++++++++++++ eth2/beacon/types/blocks.py | 66 +++++----------------- eth2/beacon/types/proposer_slashings.py | 2 +- 4 files changed, 94 insertions(+), 55 deletions(-) create mode 100644 eth2/beacon/types/block_headers.py diff --git a/eth2/beacon/tools/builder/validator.py b/eth2/beacon/tools/builder/validator.py index 1b13213687..43e95c726e 100644 --- a/eth2/beacon/tools/builder/validator.py +++ b/eth2/beacon/tools/builder/validator.py @@ -181,13 +181,17 @@ def sign_transaction(*, ) +SAMPLE_HASH_1 = Hash32(b'\x11' * 32) +SAMPLE_HASH_2 = Hash32(b'\x22' * 32) + + def create_block_header_with_signature( state: BeaconState, block_body_root: Hash32, privkey: int, slots_per_epoch: int, - previous_block_root: bytes=b'\x11' * 32, - state_root: bytes=b'\x22' * 32)-> BeaconBlockHeader: + previous_block_root: Hash32=SAMPLE_HASH_1, + state_root: Hash32=SAMPLE_HASH_2)-> BeaconBlockHeader: block_header = BeaconBlockHeader( slot=state.slot, previous_block_root=previous_block_root, diff --git a/eth2/beacon/types/block_headers.py b/eth2/beacon/types/block_headers.py new file mode 100644 index 0000000000..dc799ae092 --- /dev/null +++ b/eth2/beacon/types/block_headers.py @@ -0,0 +1,73 @@ +from typing import ( + TYPE_CHECKING, +) + +from eth_typing import ( + BLSSignature, + Hash32, +) +from eth_utils import ( + encode_hex, +) + +import ssz +from ssz.sedes import ( + bytes32, + bytes96, + uint64, +) + +from eth2.beacon.constants import EMPTY_SIGNATURE +from eth2.beacon.typing import ( + Slot, +) + + +if TYPE_CHECKING: + from eth2.beacon.db.chain import BaseBeaconChainDB # noqa: F401 + + +class BeaconBlockHeader(ssz.SignedSerializable): + + fields = [ + ('slot', uint64), + ('previous_block_root', bytes32), + ('state_root', bytes32), + ('block_body_root', bytes32), + ('signature', bytes96), + ] + + def __init__(self, + *, + slot: Slot, + previous_block_root: Hash32, + state_root: Hash32, + block_body_root: Hash32, + signature: BLSSignature=EMPTY_SIGNATURE): + super().__init__( + slot=slot, + previous_block_root=previous_block_root, + state_root=state_root, + block_body_root=block_body_root, + signature=signature, + ) + + def __repr__(self) -> str: + return ''.format( + self.slot, + encode_hex(self.signing_root)[2:10], + ) + + _root = None + + @property + def root(self) -> Hash32: + return super().root + + _signing_root = None + + @property + def signing_root(self) -> Hash32: + if self._signing_root is None: + self._signing_root = super().signing_root + return Hash32(self._signing_root) diff --git a/eth2/beacon/types/blocks.py b/eth2/beacon/types/blocks.py index 28a221187f..02ede64e28 100644 --- a/eth2/beacon/types/blocks.py +++ b/eth2/beacon/types/blocks.py @@ -16,9 +16,6 @@ BLSSignature, Hash32, ) -from eth_utils import ( - encode_hex, -) import ssz from ssz.sedes import ( @@ -42,68 +39,23 @@ from .attestations import Attestation from .attester_slashings import AttesterSlashing +from .block_headers import BeaconBlockHeader from .deposits import Deposit from .eth1_data import Eth1Data +from .proposer_slashings import ProposerSlashing from .transfers import Transfer from .voluntary_exits import VoluntaryExit if TYPE_CHECKING: - from .proposer_slashings import ProposerSlashing # noqa: F401 from eth2.beacon.db.chain import BaseBeaconChainDB # noqa: F401 -class BeaconBlockHeader(ssz.SignedSerializable): - - fields = [ - ('slot', uint64), - ('previous_block_root', bytes32), - ('state_root', bytes32), - ('block_body_root', bytes32), - ('signature', bytes96), - ] - - def __init__(self, - *, - slot: uint64, - previous_block_root: bytes32, - state_root: bytes32, - block_body_root: bytes32, - signature: BLSSignature=EMPTY_SIGNATURE): - super().__init__( - slot=slot, - previous_block_root=previous_block_root, - state_root=state_root, - block_body_root=block_body_root, - signature=signature, - ) - - def __repr__(self) -> str: - return ''.format( - self.slot, - encode_hex(self.signing_root)[2:10], - ) - - _root = None - - @property - def root(self) -> Hash32: - return super().root - - _signing_root = None - - @property - def signing_root(self) -> Hash32: - if self._signing_root is None: - self._signing_root = super().signing_root - return Hash32(self._signing_root) - - class BeaconBlockBody(ssz.Serializable): fields = [ ('randao_reveal', bytes96), ('eth1_data', Eth1Data), - ('proposer_slashings', List('ProposerSlashing')), + ('proposer_slashings', List(ProposerSlashing)), ('attester_slashings', List(AttesterSlashing)), ('attestations', List(Attestation)), ('deposits', List(Deposit)), @@ -115,7 +67,7 @@ def __init__(self, *, randao_reveal: bytes96, eth1_data: Eth1Data, - proposer_slashings: Sequence['ProposerSlashing'], + proposer_slashings: Sequence[ProposerSlashing], attester_slashings: Sequence[AttesterSlashing], attestations: Sequence[Attestation], deposits: Sequence[Deposit], @@ -323,3 +275,13 @@ def convert_block(cls, body=block.body, signature=block.signature, ) + + @classmethod + def from_header(cls, header: BeaconBlockHeader) -> 'BeaconBlock': + return cls( + slot=header.slot, + previous_block_root=header.previous_block_root, + state_root=header.state_root, + signature=header.signature, + body=BeaconBlockBody.create_empty_body(), + ) diff --git a/eth2/beacon/types/proposer_slashings.py b/eth2/beacon/types/proposer_slashings.py index 20cc81a2b3..4f26e9e3fc 100644 --- a/eth2/beacon/types/proposer_slashings.py +++ b/eth2/beacon/types/proposer_slashings.py @@ -3,7 +3,7 @@ uint64, ) -from .blocks import BeaconBlockHeader +from .block_headers import BeaconBlockHeader from eth2.beacon.typing import ( ValidatorIndex, ) From 742999b347285b30f99ea68ab54be0cd5e8bccac Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Sun, 28 Apr 2019 18:14:19 +0800 Subject: [PATCH 10/19] Use SSZ Vector` for `HistoricalBatch` fields --- .../forks/serenity/epoch_processing.py | 1 - eth2/beacon/types/historical_batch.py | 12 ++++-------- tests/eth2/beacon/conftest.py | 12 ++++++++++-- .../beacon/state_machines/test_state_transition.py | 1 - 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/eth2/beacon/state_machines/forks/serenity/epoch_processing.py b/eth2/beacon/state_machines/forks/serenity/epoch_processing.py index cf8f823c3d..499cfb0219 100644 --- a/eth2/beacon/state_machines/forks/serenity/epoch_processing.py +++ b/eth2/beacon/state_machines/forks/serenity/epoch_processing.py @@ -1184,7 +1184,6 @@ def _update_historical_roots(state: BeaconState, historical_batch = HistoricalBatch( block_roots=state.latest_block_roots, state_roots=state.latest_state_roots, - slots_per_historical_root=config.SLOTS_PER_HISTORICAL_ROOT, ) updated_historical_roots += (historical_batch.root,) diff --git a/eth2/beacon/types/historical_batch.py b/eth2/beacon/types/historical_batch.py index 2534aa1fb9..c81505454a 100644 --- a/eth2/beacon/types/historical_batch.py +++ b/eth2/beacon/types/historical_batch.py @@ -9,7 +9,7 @@ import ssz from ssz.sedes import ( bytes32, - List, + Vector, ) @@ -17,19 +17,15 @@ class HistoricalBatch(ssz.Serializable): fields = [ # Block roots - ('block_roots', List(bytes32)), + ('block_roots', Vector(bytes32, 1)), # State roots - ('state_roots', List(bytes32)), + ('state_roots', Vector(bytes32, 1)), ] def __init__(self, *, block_roots: Sequence[Hash32], - state_roots: Sequence[Hash32], - slots_per_historical_root: int) -> None: - assert len(block_roots) == slots_per_historical_root - assert len(state_roots) == slots_per_historical_root - + state_roots: Sequence[Hash32]) -> None: super().__init__( block_roots=block_roots, state_roots=state_roots, diff --git a/tests/eth2/beacon/conftest.py b/tests/eth2/beacon/conftest.py index 51a01559f6..5535fdaf19 100644 --- a/tests/eth2/beacon/conftest.py +++ b/tests/eth2/beacon/conftest.py @@ -24,6 +24,7 @@ from eth2.beacon.types.deposit_data import DepositData from eth2.beacon.types.deposit_input import DepositInput from eth2.beacon.types.eth1_data import Eth1Data +from eth2.beacon.types.historical_batch import HistoricalBatch from eth2.beacon.types.slashable_attestations import SlashableAttestation from eth2.beacon.types.states import BeaconState @@ -57,7 +58,7 @@ # SSZ @pytest.fixture(scope="function", autouse=True) def override_length(config): - vector_dict = { + state_vector_dict = { "latest_randao_mixes": config.LATEST_RANDAO_MIXES_LENGTH, "latest_crosslinks": config.SHARD_COUNT, "latest_block_roots": config.SLOTS_PER_HISTORICAL_ROOT, @@ -65,9 +66,16 @@ def override_length(config): "latest_active_index_roots": config.LATEST_ACTIVE_INDEX_ROOTS_LENGTH, "latest_slashed_balances": config.LATEST_SLASHED_EXIT_LENGTH, } - for key, value in vector_dict.items(): + for key, value in state_vector_dict.items(): BeaconState._meta.container_sedes.field_name_to_sedes[key].length = value + historical_batch_vector_dict = { + "block_roots": config.SLOTS_PER_HISTORICAL_ROOT, + "state_roots": config.SLOTS_PER_HISTORICAL_ROOT, + } + for key, value in historical_batch_vector_dict.items(): + HistoricalBatch._meta.container_sedes.field_name_to_sedes[key].length = value + @pytest.fixture(scope="session") def privkeys(): diff --git a/tests/eth2/beacon/state_machines/test_state_transition.py b/tests/eth2/beacon/state_machines/test_state_transition.py index 795465b49f..2533f1de4b 100644 --- a/tests/eth2/beacon/state_machines/test_state_transition.py +++ b/tests/eth2/beacon/state_machines/test_state_transition.py @@ -102,7 +102,6 @@ def test_per_slot_transition(base_db, historical_batch = HistoricalBatch( block_roots=state.latest_block_roots, state_roots=state.latest_state_roots, - slots_per_historical_root=config.SLOTS_PER_HISTORICAL_ROOT, ) assert updated_state.historical_roots[-1] == historical_batch.root else: From adf0512d6f4cae305ff86d60cfbe2e1d65e3ade7 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Mon, 29 Apr 2019 12:35:32 +0800 Subject: [PATCH 11/19] `Deposit.proof` should be Vector --- eth2/beacon/types/deposits.py | 4 ++-- tests/eth2/beacon/conftest.py | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/eth2/beacon/types/deposits.py b/eth2/beacon/types/deposits.py index 5fb85fbb53..63c0bd3717 100644 --- a/eth2/beacon/types/deposits.py +++ b/eth2/beacon/types/deposits.py @@ -7,7 +7,7 @@ ) import ssz from ssz.sedes import ( - List, + Vector, bytes32, uint64, ) @@ -24,7 +24,7 @@ class Deposit(ssz.Serializable): fields = [ # Merkle branch in the deposit tree - ('proof', List(bytes32)), + ('proof', Vector(bytes32, 1)), # Index in the deposit tree ('index', uint64), # Deposit data diff --git a/tests/eth2/beacon/conftest.py b/tests/eth2/beacon/conftest.py index 5535fdaf19..d343410850 100644 --- a/tests/eth2/beacon/conftest.py +++ b/tests/eth2/beacon/conftest.py @@ -21,6 +21,7 @@ ) from eth2.beacon.types.attestation_data import AttestationData from eth2.beacon.types.crosslink_records import CrosslinkRecord +from eth2.beacon.types.deposits import Deposit from eth2.beacon.types.deposit_data import DepositData from eth2.beacon.types.deposit_input import DepositInput from eth2.beacon.types.eth1_data import Eth1Data @@ -76,6 +77,10 @@ def override_length(config): for key, value in historical_batch_vector_dict.items(): HistoricalBatch._meta.container_sedes.field_name_to_sedes[key].length = value + Deposit._meta.container_sedes.field_name_to_sedes['proof'].length = ( + config.DEPOSIT_CONTRACT_TREE_DEPTH + ) + @pytest.fixture(scope="session") def privkeys(): From 268a8043e72b6e5ece5fcb698b8f502a838542a5 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Thu, 2 May 2019 15:48:44 +0800 Subject: [PATCH 12/19] bump ssz to `0.1.0a7` --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 1107e90c2a..b78f8cbbde 100644 --- a/setup.py +++ b/setup.py @@ -84,7 +84,7 @@ "py-ecc>=1.6.0,<2.0.0", "rlp>=1.1.0,<2.0.0", "py-evm==0.2.0a42", - "ssz==0.1.0a6", + "ssz==0.1.0a7", ], 'libp2p': [ "base58>=1.0.3", From 8f830cd109588a20f18fc6773eff45421c36053b Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Thu, 2 May 2019 16:55:12 +0800 Subject: [PATCH 13/19] Fix some sample params of vectors --- tests/eth2/beacon/conftest.py | 19 ++++++++++--------- .../forks/test_serenity_block_validation.py | 2 -- .../forks/test_serenity_epoch_processing.py | 6 +++--- tests/eth2/beacon/test_deposit_helpers.py | 1 + 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/tests/eth2/beacon/conftest.py b/tests/eth2/beacon/conftest.py index d343410850..a7f2c56711 100644 --- a/tests/eth2/beacon/conftest.py +++ b/tests/eth2/beacon/conftest.py @@ -13,7 +13,6 @@ CommitteeConfig, ) from eth2.beacon.constants import ( - EMPTY_SIGNATURE, FAR_FUTURE_EPOCH, ) from eth2.beacon.helpers import ( @@ -55,6 +54,8 @@ DEFAULT_RANDAO = b'\45' * 32 DEFAULT_NUM_VALIDATORS = 40 +SAMPLE_SIGNATURE = b'\56' * 96 + # SSZ @pytest.fixture(scope="function", autouse=True) @@ -124,7 +125,7 @@ def sample_attestation_params(sample_attestation_data_params): 'aggregation_bitfield': b'\12' * 16, 'data': AttestationData(**sample_attestation_data_params), 'custody_bitfield': b'\34' * 16, - 'aggregate_signature': [0, 0], + 'aggregate_signature': SAMPLE_SIGNATURE, } @@ -153,7 +154,7 @@ def sample_attestation_data_and_custody_bit_params(sample_attestation_data_param @pytest.fixture def sample_beacon_block_body_params(sample_eth1_data_params): return { - 'randao_reveal': EMPTY_SIGNATURE, + 'randao_reveal': SAMPLE_SIGNATURE, 'eth1_data': Eth1Data(**sample_eth1_data_params), 'proposer_slashings': (), 'attester_slashings': (), @@ -171,7 +172,7 @@ def sample_beacon_block_params(sample_beacon_block_body_params, 'slot': genesis_slot + 10, 'previous_block_root': ZERO_HASH32, 'state_root': b'\x55' * 32, - 'signature': EMPTY_SIGNATURE, + 'signature': SAMPLE_SIGNATURE, 'body': BeaconBlockBody(**sample_beacon_block_body_params) } @@ -255,9 +256,9 @@ def sample_crosslink_record_params(): @pytest.fixture def sample_deposit_input_params(): return { - 'pubkey': 123, + 'pubkey': b'\x67' * 48, 'withdrawal_credentials': b'\11' * 32, - 'signature': (0, 0), + 'signature': SAMPLE_SIGNATURE, } @@ -284,7 +285,7 @@ def sample_voluntary_exit_params(): return { 'epoch': 123, 'validator_index': 15, - 'signature': EMPTY_SIGNATURE, + 'signature': SAMPLE_SIGNATURE, } @@ -333,7 +334,7 @@ def sample_slashable_attestation_params(sample_attestation_data_params): 'validator_indices': (10, 11, 12, 15, 28), 'data': AttestationData(**sample_attestation_data_params), 'custody_bitfield': b'\00' * 4, - 'aggregate_signature': EMPTY_SIGNATURE, + 'aggregate_signature': SAMPLE_SIGNATURE, } @@ -362,7 +363,7 @@ def sample_attester_slashing_params(sample_slashable_attestation_params): @pytest.fixture def sample_validator_record_params(): return { - 'pubkey': 123, + 'pubkey': b'\x67' * 48, 'withdrawal_credentials': b'\x01' * 32, 'activation_epoch': FAR_FUTURE_EPOCH, 'exit_epoch': FAR_FUTURE_EPOCH, diff --git a/tests/eth2/beacon/state_machines/forks/test_serenity_block_validation.py b/tests/eth2/beacon/state_machines/forks/test_serenity_block_validation.py index 7197316ac1..dc921b4d12 100644 --- a/tests/eth2/beacon/state_machines/forks/test_serenity_block_validation.py +++ b/tests/eth2/beacon/state_machines/forks/test_serenity_block_validation.py @@ -79,10 +79,8 @@ def test_validate_block_slot(sample_beacon_state_params, ( (5, 2, 0, bls.privtopub(0), True, ), (5, 2, 0, bls.privtopub(0)[1:] + b'\x01', False), - (5, 2, 0, b'\x01\x23', False), (5, 2, 123, bls.privtopub(123), True), (5, 2, 123, bls.privtopub(123)[1:] + b'\x01', False), - (5, 2, 123, b'\x01\x23', False), ) ) def test_validate_proposer_signature( diff --git a/tests/eth2/beacon/state_machines/forks/test_serenity_epoch_processing.py b/tests/eth2/beacon/state_machines/forks/test_serenity_epoch_processing.py index e556428950..4ad8e7547c 100644 --- a/tests/eth2/beacon/state_machines/forks/test_serenity_epoch_processing.py +++ b/tests/eth2/beacon/state_machines/forks/test_serenity_epoch_processing.py @@ -461,7 +461,7 @@ def test_process_crosslinks( success_crosslink_in_previous_epoch, success_crosslink_in_current_epoch, sample_attestation_data_params, - sample_attestation_params): + sample_pending_attestation_record_params): shard = 1 previous_epoch_crosslink_data_root = hash_eth2(b'previous_epoch_crosslink_data_root') current_epoch_crosslink_data_root = hash_eth2(b'current_epoch_crosslink_data_root') @@ -504,7 +504,7 @@ def test_process_crosslinks( aggregation_bitfield, committee.index(v_index)) # Generate the attestation previous_epoch_attestations.append( - Attestation(**sample_attestation_params).copy( + PendingAttestationRecord(**sample_pending_attestation_record_params).copy( aggregation_bitfield=aggregation_bitfield, data=AttestationData(**sample_attestation_data_params).copy( slot=slot_in_previous_epoch, @@ -543,7 +543,7 @@ def test_process_crosslinks( aggregation_bitfield, committee.index(v_index)) # Generate the attestation current_epoch_attestations.append( - Attestation(**sample_attestation_params).copy( + PendingAttestationRecord(**sample_pending_attestation_record_params).copy( aggregation_bitfield=aggregation_bitfield, data=AttestationData(**sample_attestation_data_params).copy( slot=slot_in_current_epoch, diff --git a/tests/eth2/beacon/test_deposit_helpers.py b/tests/eth2/beacon/test_deposit_helpers.py index 1f0572884f..96eadac60f 100644 --- a/tests/eth2/beacon/test_deposit_helpers.py +++ b/tests/eth2/beacon/test_deposit_helpers.py @@ -96,6 +96,7 @@ def test_add_pending_validator(sample_beacon_state_params, validator, amount, ) + print(f'state.validator_registry[-1].root={state.validator_registry[-1].root}') assert state.validator_registry[-1] == validator From 1514dfceb91752155d5f9b513e9a16845b2650c1 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Thu, 2 May 2019 17:07:14 +0800 Subject: [PATCH 14/19] fix --- tests/eth2/beacon/test_helpers.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/eth2/beacon/test_helpers.py b/tests/eth2/beacon/test_helpers.py index bab8d1aa4b..c796192146 100644 --- a/tests/eth2/beacon/test_helpers.py +++ b/tests/eth2/beacon/test_helpers.py @@ -14,6 +14,7 @@ hash_eth2, ) from eth2.beacon.constants import ( + EMPTY_SIGNATURE, GWEI_PER_ETH, FAR_FUTURE_EPOCH, ) @@ -55,7 +56,7 @@ def test_get_temporary_block_header(sample_block): assert header.previous_block_root == sample_block.previous_block_root assert header.state_root == ZERO_HASH32 assert header.block_body_root == sample_block.body.root - assert header.signature == sample_block.signature + assert header.signature == EMPTY_SIGNATURE def generate_mock_latest_historical_roots( From 2d89c20bea3f2ce6e77f3275638c36c2755fa94b Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Thu, 2 May 2019 17:43:21 +0800 Subject: [PATCH 15/19] Fix plugin --- eth2/beacon/tools/misc/__init__.py | 0 eth2/beacon/tools/misc/ssz_vector.py | 31 +++++++++++++++++++++ tests/eth2/beacon/conftest.py | 27 +++--------------- tests/plugins/eth2/beacon/test_validator.py | 7 ++++- 4 files changed, 41 insertions(+), 24 deletions(-) create mode 100644 eth2/beacon/tools/misc/__init__.py create mode 100644 eth2/beacon/tools/misc/ssz_vector.py diff --git a/eth2/beacon/tools/misc/__init__.py b/eth2/beacon/tools/misc/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/eth2/beacon/tools/misc/ssz_vector.py b/eth2/beacon/tools/misc/ssz_vector.py new file mode 100644 index 0000000000..41270e8bfd --- /dev/null +++ b/eth2/beacon/tools/misc/ssz_vector.py @@ -0,0 +1,31 @@ +from eth2.configs import ( + Eth2Config, +) + +from eth2.beacon.types.deposits import Deposit +from eth2.beacon.types.historical_batch import HistoricalBatch +from eth2.beacon.types.states import BeaconState + + +def override_vector_length(config: Eth2Config) -> None: + state_vector_dict = { + "latest_randao_mixes": config.LATEST_RANDAO_MIXES_LENGTH, + "latest_crosslinks": config.SHARD_COUNT, + "latest_block_roots": config.SLOTS_PER_HISTORICAL_ROOT, + "latest_state_roots": config.SLOTS_PER_HISTORICAL_ROOT, + "latest_active_index_roots": config.LATEST_ACTIVE_INDEX_ROOTS_LENGTH, + "latest_slashed_balances": config.LATEST_SLASHED_EXIT_LENGTH, + } + for key, value in state_vector_dict.items(): + BeaconState._meta.container_sedes.field_name_to_sedes[key].length = value + + historical_batch_vector_dict = { + "block_roots": config.SLOTS_PER_HISTORICAL_ROOT, + "state_roots": config.SLOTS_PER_HISTORICAL_ROOT, + } + for key, value in historical_batch_vector_dict.items(): + HistoricalBatch._meta.container_sedes.field_name_to_sedes[key].length = value + + Deposit._meta.container_sedes.field_name_to_sedes['proof'].length = ( + config.DEPOSIT_CONTRACT_TREE_DEPTH + ) diff --git a/tests/eth2/beacon/conftest.py b/tests/eth2/beacon/conftest.py index a7f2c56711..e34fe0dc19 100644 --- a/tests/eth2/beacon/conftest.py +++ b/tests/eth2/beacon/conftest.py @@ -20,17 +20,18 @@ ) from eth2.beacon.types.attestation_data import AttestationData from eth2.beacon.types.crosslink_records import CrosslinkRecord -from eth2.beacon.types.deposits import Deposit from eth2.beacon.types.deposit_data import DepositData from eth2.beacon.types.deposit_input import DepositInput from eth2.beacon.types.eth1_data import Eth1Data -from eth2.beacon.types.historical_batch import HistoricalBatch from eth2.beacon.types.slashable_attestations import SlashableAttestation from eth2.beacon.types.states import BeaconState from eth2.beacon.on_genesis import ( get_genesis_block, ) +from eth2.beacon.tools.misc.ssz_vector import ( + override_vector_length, +) from eth2.beacon.types.blocks import ( BeaconBlockBody, BeaconBlockHeader, @@ -60,27 +61,7 @@ # SSZ @pytest.fixture(scope="function", autouse=True) def override_length(config): - state_vector_dict = { - "latest_randao_mixes": config.LATEST_RANDAO_MIXES_LENGTH, - "latest_crosslinks": config.SHARD_COUNT, - "latest_block_roots": config.SLOTS_PER_HISTORICAL_ROOT, - "latest_state_roots": config.SLOTS_PER_HISTORICAL_ROOT, - "latest_active_index_roots": config.LATEST_ACTIVE_INDEX_ROOTS_LENGTH, - "latest_slashed_balances": config.LATEST_SLASHED_EXIT_LENGTH, - } - for key, value in state_vector_dict.items(): - BeaconState._meta.container_sedes.field_name_to_sedes[key].length = value - - historical_batch_vector_dict = { - "block_roots": config.SLOTS_PER_HISTORICAL_ROOT, - "state_roots": config.SLOTS_PER_HISTORICAL_ROOT, - } - for key, value in historical_batch_vector_dict.items(): - HistoricalBatch._meta.container_sedes.field_name_to_sedes[key].length = value - - Deposit._meta.container_sedes.field_name_to_sedes['proof'].length = ( - config.DEPOSIT_CONTRACT_TREE_DEPTH - ) + override_vector_length(config) @pytest.fixture(scope="session") diff --git a/tests/plugins/eth2/beacon/test_validator.py b/tests/plugins/eth2/beacon/test_validator.py index 7afbed8969..f0d8e75ca8 100644 --- a/tests/plugins/eth2/beacon/test_validator.py +++ b/tests/plugins/eth2/beacon/test_validator.py @@ -38,6 +38,9 @@ from eth2.beacon.tools.builder.initializer import ( create_mock_genesis, ) +from eth2.beacon.tools.misc.ssz_vector import ( + override_vector_length, +) from eth2.beacon.tools.builder.proposer import ( _get_proposer_index, ) @@ -80,6 +83,8 @@ beacon_chain_config = BeaconChainConfig(chain_name='TestTestTest', genesis_data=genesis_data) chain_class = beacon_chain_config.beacon_chain_class +override_vector_length(XIAO_LONG_BAO_CONFIG) + class FakeProtocol: def __init__(self): @@ -187,7 +192,7 @@ async def test_validator_propose_block_succeeds(caplog, event_loop, event_bus): head_block=head, ) # test: ensure the proposed block is saved to the chaindb - assert v.chain.get_block_by_root(block.signed_root) == block + assert v.chain.get_block_by_root(block.signing_root) == block # test: ensure that the `canonical_head` changed after proposing new_head = v.chain.get_canonical_head() From 661f2a4d13a760648c3ce8a3670afb7815bd355f Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Thu, 2 May 2019 17:54:02 +0800 Subject: [PATCH 16/19] clean up --- eth2/beacon/db/chain.py | 1 - .../state_machines/forks/test_serenity_block_validation.py | 4 +--- .../state_machines/forks/test_serenity_epoch_processing.py | 1 - tests/eth2/beacon/test_deposit_helpers.py | 1 - tests/eth2/beacon/types/test_crosslink_record.py | 2 -- 5 files changed, 1 insertion(+), 8 deletions(-) diff --git a/eth2/beacon/db/chain.py b/eth2/beacon/db/chain.py index bffe20e059..da1bc18e39 100644 --- a/eth2/beacon/db/chain.py +++ b/eth2/beacon/db/chain.py @@ -628,5 +628,4 @@ def _decode_block(block_ssz: bytes, sedes: Type[BaseBeaconBlock]) -> BaseBeaconB @functools.lru_cache(128) def _decode_state(state_ssz: bytes, state_class: Type[BeaconState]) -> BeaconState: - # TODO: forkable BeaconState fields? return ssz.decode(state_ssz, sedes=state_class) diff --git a/tests/eth2/beacon/state_machines/forks/test_serenity_block_validation.py b/tests/eth2/beacon/state_machines/forks/test_serenity_block_validation.py index dc921b4d12..6d8f92c3a6 100644 --- a/tests/eth2/beacon/state_machines/forks/test_serenity_block_validation.py +++ b/tests/eth2/beacon/state_machines/forks/test_serenity_block_validation.py @@ -362,9 +362,7 @@ def _corrupt_signature(slots_per_epoch, params, fork): def _create_slashable_attestation_messages(params): - # TODO update when we move to `ssz` tree hash - votes = SlashableAttestation(**params) - return votes.message_hashes + return SlashableAttestation(**params).message_hashes @pytest.mark.parametrize( diff --git a/tests/eth2/beacon/state_machines/forks/test_serenity_epoch_processing.py b/tests/eth2/beacon/state_machines/forks/test_serenity_epoch_processing.py index 4ad8e7547c..1bf13e8ac6 100644 --- a/tests/eth2/beacon/state_machines/forks/test_serenity_epoch_processing.py +++ b/tests/eth2/beacon/state_machines/forks/test_serenity_epoch_processing.py @@ -1573,7 +1573,6 @@ def test_update_latest_active_index_roots(genesis_state, index_root = ssz.hash_tree_root( get_active_validator_indices( state.validator_registry, - # TODO: change to `per-epoch` version slot_to_epoch(state.slot, slots_per_epoch), ), ssz.sedes.List(ssz.uint64), diff --git a/tests/eth2/beacon/test_deposit_helpers.py b/tests/eth2/beacon/test_deposit_helpers.py index 96eadac60f..1f0572884f 100644 --- a/tests/eth2/beacon/test_deposit_helpers.py +++ b/tests/eth2/beacon/test_deposit_helpers.py @@ -96,7 +96,6 @@ def test_add_pending_validator(sample_beacon_state_params, validator, amount, ) - print(f'state.validator_registry[-1].root={state.validator_registry[-1].root}') assert state.validator_registry[-1] == validator diff --git a/tests/eth2/beacon/types/test_crosslink_record.py b/tests/eth2/beacon/types/test_crosslink_record.py index 99f07d6c25..42bfb42d57 100644 --- a/tests/eth2/beacon/types/test_crosslink_record.py +++ b/tests/eth2/beacon/types/test_crosslink_record.py @@ -7,5 +7,3 @@ def test_defaults(sample_crosslink_record_params): crosslink = CrosslinkRecord(**sample_crosslink_record_params) assert crosslink.epoch == sample_crosslink_record_params['epoch'] assert crosslink.crosslink_data_root == sample_crosslink_record_params['crosslink_data_root'] - print(CrosslinkRecord.get_static_size()) - print('3 * CrosslinkRecord.get_static_size()', 3 * CrosslinkRecord.get_static_size()) From fb104847b2f2ca40fea299139abbb6b495488d36 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Thu, 2 May 2019 18:50:05 +0800 Subject: [PATCH 17/19] PR feedback, add `__repr__` back, thanks to Chih-Cheng --- eth2/beacon/types/blocks.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/eth2/beacon/types/blocks.py b/eth2/beacon/types/blocks.py index 02ede64e28..46645a6c4d 100644 --- a/eth2/beacon/types/blocks.py +++ b/eth2/beacon/types/blocks.py @@ -16,7 +16,9 @@ BLSSignature, Hash32, ) - +from eth_utils import ( + encode_hex, +) import ssz from ssz.sedes import ( List, @@ -163,11 +165,11 @@ def __init__(self, signature=signature, ) - # def __repr__(self) -> str: - # return ''.format( - # self.slot, - # encode_hex(self.signing_root)[2:10], - # ) + def __repr__(self) -> str: + return ''.format( + self.slot, + encode_hex(self.signing_root)[2:10], + ) _root = None From 6d6f42c65f005ca819a0bc2c58469049d2910710 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Thu, 2 May 2019 19:46:47 +0800 Subject: [PATCH 18/19] PR feedback, thanks to Jannik --- eth2/beacon/tools/misc/ssz_vector.py | 2 +- .../attestation_data_and_custody_bits.py | 9 --------- eth2/beacon/types/block_headers.py | 14 ------------- eth2/beacon/types/blocks.py | 20 ------------------- eth2/beacon/types/crosslink_records.py | 6 ------ eth2/beacon/types/deposit_input.py | 15 -------------- eth2/beacon/types/historical_batch.py | 6 ------ eth2/beacon/types/slashable_attestations.py | 6 ------ eth2/beacon/types/states.py | 6 ------ eth2/beacon/types/transfers.py | 7 ------- eth2/beacon/types/voluntary_exits.py | 10 ---------- tests/eth2/beacon/conftest.py | 6 +++--- tests/plugins/eth2/beacon/test_validator.py | 4 ++-- 13 files changed, 6 insertions(+), 105 deletions(-) diff --git a/eth2/beacon/tools/misc/ssz_vector.py b/eth2/beacon/tools/misc/ssz_vector.py index 41270e8bfd..f90a8b47d4 100644 --- a/eth2/beacon/tools/misc/ssz_vector.py +++ b/eth2/beacon/tools/misc/ssz_vector.py @@ -7,7 +7,7 @@ from eth2.beacon.types.states import BeaconState -def override_vector_length(config: Eth2Config) -> None: +def override_vector_lengths(config: Eth2Config) -> None: state_vector_dict = { "latest_randao_mixes": config.LATEST_RANDAO_MIXES_LENGTH, "latest_crosslinks": config.SHARD_COUNT, diff --git a/eth2/beacon/types/attestation_data_and_custody_bits.py b/eth2/beacon/types/attestation_data_and_custody_bits.py index 2cae03c398..5cbfd9bfc8 100644 --- a/eth2/beacon/types/attestation_data_and_custody_bits.py +++ b/eth2/beacon/types/attestation_data_and_custody_bits.py @@ -2,9 +2,6 @@ from ssz.sedes import ( boolean, ) -from eth_typing import ( - Hash32, -) from .attestation_data import ( AttestationData, @@ -28,9 +25,3 @@ def __init__(self, data=data, custody_bit=custody_bit, ) - - _root = None - - @property - def root(self) -> Hash32: - return super().root diff --git a/eth2/beacon/types/block_headers.py b/eth2/beacon/types/block_headers.py index dc799ae092..1759d71b0a 100644 --- a/eth2/beacon/types/block_headers.py +++ b/eth2/beacon/types/block_headers.py @@ -57,17 +57,3 @@ def __repr__(self) -> str: self.slot, encode_hex(self.signing_root)[2:10], ) - - _root = None - - @property - def root(self) -> Hash32: - return super().root - - _signing_root = None - - @property - def signing_root(self) -> Hash32: - if self._signing_root is None: - self._signing_root = super().signing_root - return Hash32(self._signing_root) diff --git a/eth2/beacon/types/blocks.py b/eth2/beacon/types/blocks.py index 46645a6c4d..15482306bb 100644 --- a/eth2/beacon/types/blocks.py +++ b/eth2/beacon/types/blocks.py @@ -126,12 +126,6 @@ def cast_block_body(cls, transfers=body.transfers, ) - _root = None - - @property - def root(self) -> Hash32: - return super().root - class BaseBeaconBlock(ssz.SignedSerializable, Configurable, ABC): fields = [ @@ -171,20 +165,6 @@ def __repr__(self) -> str: encode_hex(self.signing_root)[2:10], ) - _root = None - - @property - def root(self) -> Hash32: - return super().root - - _signing_root = None - - @property - def signing_root(self) -> Hash32: - if self._signing_root is None: - self._signing_root = super().signing_root - return Hash32(self._signing_root) - @property def num_attestations(self) -> int: return len(self.body.attestations) diff --git a/eth2/beacon/types/crosslink_records.py b/eth2/beacon/types/crosslink_records.py index 8a0fd688bf..de4cbe862f 100644 --- a/eth2/beacon/types/crosslink_records.py +++ b/eth2/beacon/types/crosslink_records.py @@ -28,9 +28,3 @@ def __init__(self, epoch=epoch, crosslink_data_root=crosslink_data_root, ) - - _root = None - - @property - def root(self) -> Hash32: - return super().root diff --git a/eth2/beacon/types/deposit_input.py b/eth2/beacon/types/deposit_input.py index 75e44802f3..4f2acfc251 100644 --- a/eth2/beacon/types/deposit_input.py +++ b/eth2/beacon/types/deposit_input.py @@ -33,18 +33,3 @@ def __init__(self, withdrawal_credentials=withdrawal_credentials, signature=signature, ) - - _root = None - - @property - def root(self) -> Hash32: - return super().root - - _signing_root = None - - @property - def signing_root(self) -> Hash32: - # Use SSZ built-in function - if self._signing_root is None: - self._signing_root = super().signing_root - return self._signing_root diff --git a/eth2/beacon/types/historical_batch.py b/eth2/beacon/types/historical_batch.py index c81505454a..bd1bdc4d4d 100644 --- a/eth2/beacon/types/historical_batch.py +++ b/eth2/beacon/types/historical_batch.py @@ -30,9 +30,3 @@ def __init__(self, block_roots=block_roots, state_roots=state_roots, ) - - _root = None - - @property - def root(self) -> Hash32: - return super().root diff --git a/eth2/beacon/types/slashable_attestations.py b/eth2/beacon/types/slashable_attestations.py index 0f95a254d0..682342b457 100644 --- a/eth2/beacon/types/slashable_attestations.py +++ b/eth2/beacon/types/slashable_attestations.py @@ -52,12 +52,6 @@ def __init__(self, aggregate_signature, ) - _root = None - - @property - def root(self) -> Hash32: - return super().root - @property def are_validator_indices_ascending(self) -> bool: for i in range(len(self.validator_indices) - 1): diff --git a/eth2/beacon/types/states.py b/eth2/beacon/types/states.py index 868d8b35da..b0789afde3 100644 --- a/eth2/beacon/types/states.py +++ b/eth2/beacon/types/states.py @@ -185,12 +185,6 @@ def __repr__(self) -> str: encode_hex(self.root)[2:10], ) - _root = None - - @property - def root(self) -> Hash32: - return super().root - @property def num_validators(self) -> int: return len(self.validator_registry) diff --git a/eth2/beacon/types/transfers.py b/eth2/beacon/types/transfers.py index e2c498d737..22df88ff5e 100644 --- a/eth2/beacon/types/transfers.py +++ b/eth2/beacon/types/transfers.py @@ -1,7 +1,6 @@ from eth_typing import ( BLSPubkey, BLSSignature, - Hash32, ) import ssz from ssz.sedes import ( @@ -54,9 +53,3 @@ def __init__(self, pubkey=pubkey, signature=signature, ) - - _root = None - - @property - def root(self) -> Hash32: - return super().root diff --git a/eth2/beacon/types/voluntary_exits.py b/eth2/beacon/types/voluntary_exits.py index 349438fb3b..2056d74a2f 100644 --- a/eth2/beacon/types/voluntary_exits.py +++ b/eth2/beacon/types/voluntary_exits.py @@ -1,6 +1,5 @@ from eth_typing import ( BLSSignature, - Hash32, ) import ssz from ssz.sedes import ( @@ -35,12 +34,3 @@ def __init__(self, validator_index, signature, ) - - _signing_root = None - - @property - def signing_root(self) -> Hash32: - # Use SSZ built-in function - if self._signing_root is None: - self._signing_root = super().signing_root - return self._signing_root diff --git a/tests/eth2/beacon/conftest.py b/tests/eth2/beacon/conftest.py index e34fe0dc19..1b309ee416 100644 --- a/tests/eth2/beacon/conftest.py +++ b/tests/eth2/beacon/conftest.py @@ -30,7 +30,7 @@ get_genesis_block, ) from eth2.beacon.tools.misc.ssz_vector import ( - override_vector_length, + override_vector_lengths, ) from eth2.beacon.types.blocks import ( BeaconBlockBody, @@ -60,8 +60,8 @@ # SSZ @pytest.fixture(scope="function", autouse=True) -def override_length(config): - override_vector_length(config) +def override_lengths(config): + override_vector_lengths(config) @pytest.fixture(scope="session") diff --git a/tests/plugins/eth2/beacon/test_validator.py b/tests/plugins/eth2/beacon/test_validator.py index f0d8e75ca8..fdb74ba9f8 100644 --- a/tests/plugins/eth2/beacon/test_validator.py +++ b/tests/plugins/eth2/beacon/test_validator.py @@ -39,7 +39,7 @@ create_mock_genesis, ) from eth2.beacon.tools.misc.ssz_vector import ( - override_vector_length, + override_vector_lengths, ) from eth2.beacon.tools.builder.proposer import ( _get_proposer_index, @@ -83,7 +83,7 @@ beacon_chain_config = BeaconChainConfig(chain_name='TestTestTest', genesis_data=genesis_data) chain_class = beacon_chain_config.beacon_chain_class -override_vector_length(XIAO_LONG_BAO_CONFIG) +override_vector_lengths(XIAO_LONG_BAO_CONFIG) class FakeProtocol: From de7d33d31cdd6faa2c2c55151a30a9e10119255e Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Thu, 2 May 2019 19:55:32 +0800 Subject: [PATCH 19/19] Update block __repr__ --- eth2/beacon/types/block_headers.py | 7 ++++--- eth2/beacon/types/blocks.py | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/eth2/beacon/types/block_headers.py b/eth2/beacon/types/block_headers.py index 1759d71b0a..e35f1f591c 100644 --- a/eth2/beacon/types/block_headers.py +++ b/eth2/beacon/types/block_headers.py @@ -53,7 +53,8 @@ def __init__(self, ) def __repr__(self) -> str: - return ''.format( - self.slot, - encode_hex(self.signing_root)[2:10], + return ( + f'' ) diff --git a/eth2/beacon/types/blocks.py b/eth2/beacon/types/blocks.py index 15482306bb..5cf057dcdf 100644 --- a/eth2/beacon/types/blocks.py +++ b/eth2/beacon/types/blocks.py @@ -160,9 +160,10 @@ def __init__(self, ) def __repr__(self) -> str: - return ''.format( - self.slot, - encode_hex(self.signing_root)[2:10], + return ( + f'' ) @property