From bd0689f048c111a7015ba2189663e27fd90167c8 Mon Sep 17 00:00:00 2001 From: Etan Kissling Date: Sun, 26 May 2024 14:59:49 +0200 Subject: [PATCH] move `Transaction`, `ExecutionAddress` and `BloomLogs` to `base` Preparation for EIP-7495 SSZ `StableContainer` which can only contain immutable types in their fields. --- beacon_chain/spec/datatypes/base.nim | 33 +++++++++++++++++++++++ beacon_chain/spec/datatypes/bellatrix.nim | 32 ---------------------- 2 files changed, 33 insertions(+), 32 deletions(-) diff --git a/beacon_chain/spec/datatypes/base.nim b/beacon_chain/spec/datatypes/base.nim index 8ac1fdb62..faf407cba 100644 --- a/beacon_chain/spec/datatypes/base.nim +++ b/beacon_chain/spec/datatypes/base.nim @@ -373,6 +373,15 @@ type sync_committee_bits*: BitArray[SYNC_COMMITTEE_SIZE] sync_committee_signature*: TrustedSig + # https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.2/specs/bellatrix/beacon-chain.md#custom-types + Transaction* = List[byte, Limit MAX_BYTES_PER_TRANSACTION] + + ExecutionAddress* = object + data*: array[20, byte] # TODO there's a network_metadata type, but the import hierarchy's inconvenient + + BloomLogs* = object + data*: array[BYTES_PER_LOGS_BLOOM, byte] + # https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.6/specs/phase0/beacon-chain.md#beaconblockheader BeaconBlockHeader* = object slot*: Slot @@ -837,6 +846,30 @@ func hasSupermajoritySyncParticipation*( func hasSupermajoritySyncParticipation*(v: SomeSyncAggregate): bool = hasSupermajoritySyncParticipation(v.num_active_participants.uint64) +func fromHex*( + T: typedesc[BloomLogs], s: string): T {.raises: [ValueError].} = + hexToByteArray(s, result.data) + +func fromHex*( + T: typedesc[ExecutionAddress], s: string): T {.raises: [ValueError].} = + hexToByteArray(s, result.data) + +proc writeValue*( + writer: var JsonWriter, value: ExecutionAddress) {.raises: [IOError].} = + writer.writeValue to0xHex(value.data) + +proc readValue*( + reader: var JsonReader, + value: var ExecutionAddress) {.raises: [IOError, SerializationError].} = + try: + hexToByteArray(reader.readValue(string), value.data) + except ValueError: + reader.raiseUnexpectedValue( + "ExecutionAddress value should be a valid hex string") + +func `$`*(v: ExecutionAddress): string = + v.data.toHex() + chronicles.formatIt AttestationData: it.shortLog chronicles.formatIt Checkpoint: it.shortLog diff --git a/beacon_chain/spec/datatypes/bellatrix.nim b/beacon_chain/spec/datatypes/bellatrix.nim index cc71e59c4..301db8cea 100644 --- a/beacon_chain/spec/datatypes/bellatrix.nim +++ b/beacon_chain/spec/datatypes/bellatrix.nim @@ -35,15 +35,6 @@ const NEWPAYLOAD_TIMEOUT* = 8.seconds type - # https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.2/specs/bellatrix/beacon-chain.md#custom-types - Transaction* = List[byte, Limit MAX_BYTES_PER_TRANSACTION] - - ExecutionAddress* = object - data*: array[20, byte] # TODO there's a network_metadata type, but the import hierarchy's inconvenient - - BloomLogs* = object - data*: array[BYTES_PER_LOGS_BLOOM, byte] - # https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.2/specs/bellatrix/beacon-chain.md#executionpayload ExecutionPayload* = object # Execution block header fields @@ -361,29 +352,6 @@ type func initHashedBeaconState*(s: BeaconState): HashedBeaconState = HashedBeaconState(data: s) -func fromHex*(T: typedesc[BloomLogs], s: string): T {. - raises: [ValueError].} = - hexToByteArray(s, result.data) - -func fromHex*(T: typedesc[ExecutionAddress], s: string): T {. - raises: [ValueError].} = - hexToByteArray(s, result.data) - -proc writeValue*( - writer: var JsonWriter, value: ExecutionAddress) {.raises: [IOError].} = - writer.writeValue to0xHex(value.data) - -proc readValue*(reader: var JsonReader, value: var ExecutionAddress) {. - raises: [IOError, SerializationError].} = - try: - hexToByteArray(reader.readValue(string), value.data) - except ValueError: - raiseUnexpectedValue(reader, - "ExecutionAddress value should be a valid hex string") - -func `$`*(v: ExecutionAddress): string = - v.data.toHex() - func shortLog*(v: SomeBeaconBlock): auto = ( slot: shortLog(v.slot),