Add ssz_{de,}serialize aliases

This commit is contained in:
Justin Traglia 2024-10-10 13:41:02 -05:00
parent 83c04b8469
commit 6416a562ab
No known key found for this signature in database
GPG Key ID: F099C9CD25DF82A1
4 changed files with 18 additions and 10 deletions

View File

@ -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

View File

@ -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]
```

View File

@ -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)
```

View File

@ -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())