From 6416a562abb6085b87889e9c202cd3d2cb7d2b39 Mon Sep 17 00:00:00 2001 From: Justin Traglia Date: Thu, 10 Oct 2024 13:41:02 -0500 Subject: [PATCH] Add ssz_{de,}serialize aliases --- pysetup/spec_builders/electra.py | 2 +- specs/electra/beacon-chain.md | 6 +++--- specs/electra/validator.md | 8 ++++---- tests/core/pyspec/eth2spec/utils/ssz/ssz_impl.py | 12 ++++++++++-- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/pysetup/spec_builders/electra.py b/pysetup/spec_builders/electra.py index 23237dce9..6746d9aad 100644 --- a/pysetup/spec_builders/electra.py +++ b/pysetup/spec_builders/electra.py @@ -10,7 +10,7 @@ class ElectraSpecBuilder(BaseSpecBuilder): def imports(cls, preset_name: str): return f''' from eth2spec.deneb import {preset_name} as deneb -from eth2spec.utils.ssz.ssz_impl import serialize, deserialize +from eth2spec.utils.ssz.ssz_impl import ssz_serialize, ssz_deserialize ''' @classmethod diff --git a/specs/electra/beacon-chain.md b/specs/electra/beacon-chain.md index 8dca8efa4..aff2167cf 100644 --- a/specs/electra/beacon-chain.md +++ b/specs/electra/beacon-chain.md @@ -1146,9 +1146,9 @@ def process_withdrawals(state: BeaconState, payload: ExecutionPayload) -> None: ```python def get_execution_requests_list(execution_requests: ExecutionRequests) -> Sequence[bytes]: - deposit_bytes = serialize(execution_requests.deposits) - withdrawal_bytes = serialize(execution_requests.withdrawals) - consolidation_bytes = serialize(execution_requests.consolidations) + deposit_bytes = ssz_serialize(execution_requests.deposits) + withdrawal_bytes = ssz_serialize(execution_requests.withdrawals) + consolidation_bytes = ssz_serialize(execution_requests.consolidations) return [deposit_bytes, withdrawal_bytes, consolidation_bytes] ``` diff --git a/specs/electra/validator.md b/specs/electra/validator.md index 4622ff480..ae8d53c8f 100644 --- a/specs/electra/validator.md +++ b/specs/electra/validator.md @@ -195,10 +195,10 @@ in [EIP-7685](https://eips.ethereum.org/EIPS/eip-7685). The index of each elemen ```python def get_execution_requests(execution_requests: Sequence[bytes]) -> ExecutionRequests: - deposits = deserialize(List[DepositRequest, MAX_DEPOSIT_REQUESTS_PER_PAYLOAD], execution_requests[0]) - withdrawals = deserialize(List[WithdrawalRequest, MAX_WITHDRAWAL_REQUESTS_PER_PAYLOAD], execution_requests[1]) - consolidations = deserialize(List[ConsolidationRequest, MAX_CONSOLIDATION_REQUESTS_PER_PAYLOAD], - execution_requests[2]) + deposits = ssz_deserialize(List[DepositRequest, MAX_DEPOSIT_REQUESTS_PER_PAYLOAD], execution_requests[0]) + withdrawals = ssz_deserialize(List[WithdrawalRequest, MAX_WITHDRAWAL_REQUESTS_PER_PAYLOAD], execution_requests[1]) + consolidations = ssz_deserialize(List[ConsolidationRequest, MAX_CONSOLIDATION_REQUESTS_PER_PAYLOAD], + execution_requests[2]) return ExecutionRequests(deposits, withdrawals, consolidations) ``` diff --git a/tests/core/pyspec/eth2spec/utils/ssz/ssz_impl.py b/tests/core/pyspec/eth2spec/utils/ssz/ssz_impl.py index a76cf80f9..645cbff77 100644 --- a/tests/core/pyspec/eth2spec/utils/ssz/ssz_impl.py +++ b/tests/core/pyspec/eth2spec/utils/ssz/ssz_impl.py @@ -5,14 +5,22 @@ from remerkleable.core import Type, View from remerkleable.byte_arrays import Bytes32 -def serialize(obj: View) -> bytes: +def ssz_serialize(obj: View) -> bytes: return obj.encode_bytes() -def deserialize(typ: Type[View], data: bytes) -> View: +def serialize(obj: View) -> bytes: + return ssz_serialize(obj) + + +def ssz_deserialize(typ: Type[View], data: bytes) -> View: return typ.decode_bytes(data) +def deserialize(typ: Type[View], data: bytes) -> View: + return ssz_deserialize(typ, data) + + def hash_tree_root(obj: View) -> Bytes32: return Bytes32(obj.get_backing().merkle_root())