From 72b5c2bd6eb5070355577b9c397110d9f7627b09 Mon Sep 17 00:00:00 2001 From: tersec Date: Sun, 15 Sep 2024 13:33:13 +0000 Subject: [PATCH] add some electra support to REST JSON decoding; rm unused code (#6549) --- .../eth2_apis/eth2_rest_serialization.nim | 131 ++++++------------ 1 file changed, 46 insertions(+), 85 deletions(-) diff --git a/beacon_chain/spec/eth2_apis/eth2_rest_serialization.nim b/beacon_chain/spec/eth2_apis/eth2_rest_serialization.nim index 213a133f6..b0e49bc46 100644 --- a/beacon_chain/spec/eth2_apis/eth2_rest_serialization.nim +++ b/beacon_chain/spec/eth2_apis/eth2_rest_serialization.nim @@ -15,15 +15,11 @@ import results, stew/[assign2, base10, byteutils, endians2], presto/common, stint, chronicles import ".."/[eth2_ssz_serialization, forks, keystore], ".."/../consensus_object_pools/block_pools_types, - ".."/datatypes/[phase0, altair, bellatrix], ".."/mev/[bellatrix_mev, capella_mev], ".."/../validators/slashing_protection_common, "."/[rest_types, rest_keymanager_types] import nimcrypto/utils as ncrutils -from ".."/datatypes/capella import SignedBeaconBlock -from ".."/datatypes/deneb import BeaconState - export eth2_ssz_serialization, results, peerid, common, serialization, chronicles, json_serialization, net, sets, rest_types, slashing_protection_common, @@ -311,7 +307,7 @@ template writeValue*(w: JsonWriter[RestJson], value: tuple) = ## TODO nim-json-serializations should allow setting up this policy per format ## ## This also means that when new fields are introduced to the object definitions -## below, one must use the `Option[T]` type. +## below, one must use the `Opt[T]` type. const DecimalSet = {'0' .. '9'} @@ -1516,20 +1512,8 @@ proc readValue*[BlockType: ForkedBlindedBeaconBlock]( exc.formatMsg("BlindedBlock") & "]") value = ForkedBlindedBeaconBlock(kind: ConsensusFork.Altair, altairData: res) - of ConsensusFork.Bellatrix: - reader.raiseUnexpectedValue("Bellatrix blinded block format unsupported") - of ConsensusFork.Capella: - let res = - try: - RestJson.decode(string(data.get()), - capella_mev.BlindedBeaconBlock, - requireAllFields = true, - allowUnknownFields = true) - except SerializationError as exc: - reader.raiseUnexpectedValue("Incorrect capella block format, [" & - exc.formatMsg("BlindedBlock") & "]") - value = ForkedBlindedBeaconBlock(kind: ConsensusFork.Capella, - capellaData: res) + of ConsensusFork.Bellatrix .. ConsensusFork.Capella: + reader.raiseUnexpectedValue("pre-Deneb blinded block formats unsupported") of ConsensusFork.Deneb: let res = try: @@ -1592,34 +1576,6 @@ proc writeValue*[BlockType: Web3SignerForkedBeaconBlock]( writer.writeField("block_header", value.data) writer.endRecord() -proc writeValue*[BlockType: ForkedBeaconBlock]( - writer: var JsonWriter[RestJson], value: BlockType) {.raises: [IOError].} = - - template forkIdentifier(id: string): auto = - when BlockType is ForkedBeaconBlock: - id - else: - (static toUpperAscii id) - - writer.beginRecord() - case value.kind - of ConsensusFork.Phase0: - writer.writeField("version", forkIdentifier "phase0") - writer.writeField("data", value.phase0Data) - of ConsensusFork.Altair: - writer.writeField("version", forkIdentifier "altair") - writer.writeField("data", value.altairData) - of ConsensusFork.Bellatrix: - writer.writeField("version", forkIdentifier "bellatrix") - writer.writeField("data", value.bellatrixData) - of ConsensusFork.Capella: - writer.writeField("version", forkIdentifier "capella") - writer.writeField("data", value.capellaData) - of ConsensusFork.Deneb: - writer.writeField("version", forkIdentifier "deneb") - writer.writeField("data", value.denebData) - writer.endRecord() - ## RestPublishedBeaconBlockBody proc readValue*(reader: var JsonReader[RestJson], value: var RestPublishedBeaconBlockBody) {. @@ -2161,6 +2117,7 @@ proc readValue*(reader: var JsonReader[RestJson], reader.raiseUnexpectedField("Multiple version fields found", "ForkedSignedBeaconBlock") let vres = reader.readValue(string) + static: doAssert ConsensusFork.high == ConsensusFork.Electra case vres of "phase0": version = Opt.some(ConsensusFork.Phase0) @@ -2172,6 +2129,8 @@ proc readValue*(reader: var JsonReader[RestJson], version = Opt.some(ConsensusFork.Capella) of "deneb": version = Opt.some(ConsensusFork.Deneb) + of "electra": + version = Opt.some(ConsensusFork.Electra) else: reader.raiseUnexpectedValue("Incorrect version field value") of "data": @@ -3908,10 +3867,10 @@ proc decodeBytes*[T: DecodeTypes]( else: err("Content-Type not supported") -proc encodeString*(value: string): RestResult[string] = +func encodeString*(value: string): RestResult[string] = ok(value) -proc encodeString*( +func encodeString*( value: uint64 | SyncCommitteePeriod | @@ -3921,26 +3880,26 @@ proc encodeString*( SyncSubcommitteeIndex): RestResult[string] = ok(Base10.toString(uint64(value))) -proc encodeString*(value: ValidatorSig): RestResult[string] = +func encodeString*(value: ValidatorSig): RestResult[string] = ok(hexOriginal(toRaw(value))) -proc encodeString*(value: GraffitiBytes): RestResult[string] = +func encodeString*(value: GraffitiBytes): RestResult[string] = ok(hexOriginal(distinctBase(value))) -proc encodeString*(value: Eth2Digest): RestResult[string] = +func encodeString*(value: Eth2Digest): RestResult[string] = ok(hexOriginal(value.data)) -proc encodeString*(value: ValidatorIdent): RestResult[string] = +func encodeString*(value: ValidatorIdent): RestResult[string] = case value.kind of ValidatorQueryKind.Index: ok(Base10.toString(uint64(value.index))) of ValidatorQueryKind.Key: ok(hexOriginal(toRaw(value.key))) -proc encodeString*(value: ValidatorPubKey): RestResult[string] = +func encodeString*(value: ValidatorPubKey): RestResult[string] = ok(hexOriginal(toRaw(value))) -proc encodeString*(value: StateIdent): RestResult[string] = +func encodeString*(value: StateIdent): RestResult[string] = case value.kind of StateQueryKind.Slot: ok(Base10.toString(uint64(value.slot))) @@ -3957,7 +3916,7 @@ proc encodeString*(value: StateIdent): RestResult[string] = of StateIdentType.Justified: ok("justified") -proc encodeString*(value: BroadcastValidationType): RestResult[string] = +func encodeString*(value: BroadcastValidationType): RestResult[string] = case value of BroadcastValidationType.Gossip: ok("gossip") @@ -3966,7 +3925,7 @@ proc encodeString*(value: BroadcastValidationType): RestResult[string] = of BroadcastValidationType.ConsensusAndEquivocation: ok("consensus_and_equivocation") -proc encodeString*(value: BlockIdent): RestResult[string] = +func encodeString*(value: BlockIdent): RestResult[string] = case value.kind of BlockQueryKind.Slot: ok(Base10.toString(uint64(value.slot))) @@ -3981,7 +3940,7 @@ proc encodeString*(value: BlockIdent): RestResult[string] = of BlockIdentType.Finalized: ok("finalized") -proc decodeString*(t: typedesc[PeerStateKind], +func decodeString*(t: typedesc[PeerStateKind], value: string): Result[PeerStateKind, cstring] = case value of "disconnected": @@ -3993,9 +3952,9 @@ proc decodeString*(t: typedesc[PeerStateKind], of "disconnecting": ok(PeerStateKind.Disconnecting) else: - err("Incorrect peer's state value") + err("Incorrect peer state value") -proc encodeString*(value: PeerStateKind): Result[string, cstring] = +func encodeString*(value: PeerStateKind): Result[string, cstring] = case value of PeerStateKind.Disconnected: ok("disconnected") @@ -4006,7 +3965,7 @@ proc encodeString*(value: PeerStateKind): Result[string, cstring] = of PeerStateKind.Disconnecting: ok("disconnecting") -proc decodeString*(t: typedesc[PeerDirectKind], +func decodeString*(t: typedesc[PeerDirectKind], value: string): Result[PeerDirectKind, cstring] = case value of "inbound": @@ -4014,19 +3973,19 @@ proc decodeString*(t: typedesc[PeerDirectKind], of "outbound": ok(PeerDirectKind.Outbound) else: - err("Incorrect peer's direction value") + err("Incorrect peer direction value") -proc encodeString*(value: PeerDirectKind): Result[string, cstring] = +func encodeString*(value: PeerDirectKind): Result[string, cstring] = case value of PeerDirectKind.Inbound: ok("inbound") of PeerDirectKind.Outbound: ok("outbound") -proc encodeString*(peerid: PeerId): Result[string, cstring] = +func encodeString*(peerid: PeerId): Result[string, cstring] = ok($peerid) -proc decodeString*(t: typedesc[EventTopic], +func decodeString*(t: typedesc[EventTopic], value: string): Result[EventTopic, cstring] = case value of "head": @@ -4058,7 +4017,7 @@ proc decodeString*(t: typedesc[EventTopic], else: err("Incorrect event's topic value") -proc encodeString*(value: set[EventTopic]): Result[string, cstring] = +func encodeString*(value: set[EventTopic]): Result[string, cstring] = var res: string if EventTopic.Head in value: res.add("head,") @@ -4091,7 +4050,7 @@ proc encodeString*(value: set[EventTopic]): Result[string, cstring] = res.setLen(len(res) - 1) ok(res) -proc toList*(value: set[ValidatorFilterKind]): seq[string] = +func toList*(value: set[ValidatorFilterKind]): seq[string] = const pendingSet = {ValidatorFilterKind.PendingInitialized, ValidatorFilterKind.PendingQueued} @@ -4130,7 +4089,7 @@ proc toList*(value: set[ValidatorFilterKind]): seq[string] = processSingle(ValidatorFilterKind.WithdrawalDone, "withdrawal_done") res -proc decodeString*(t: typedesc[ValidatorSig], +func decodeString*(t: typedesc[ValidatorSig], value: string): Result[ValidatorSig, cstring] = if len(value) != ValidatorSigSize + 2: return err("Incorrect validator signature value length") @@ -4138,7 +4097,7 @@ proc decodeString*(t: typedesc[ValidatorSig], return err("Incorrect validator signature encoding") ValidatorSig.fromHex(value) -proc decodeString*(t: typedesc[ValidatorPubKey], +func decodeString*(t: typedesc[ValidatorPubKey], value: string): Result[ValidatorPubKey, cstring] = if len(value) != ValidatorKeySize + 2: return err("Incorrect validator's key value length") @@ -4147,35 +4106,35 @@ proc decodeString*(t: typedesc[ValidatorPubKey], else: ValidatorPubKey.fromHex(value) -proc decodeString*(t: typedesc[GraffitiBytes], +func decodeString*(t: typedesc[GraffitiBytes], value: string): Result[GraffitiBytes, cstring] = try: ok(GraffitiBytes.init(value)) except ValueError: err("Unable to decode graffiti value") -proc decodeString*(t: typedesc[string], +func decodeString*(t: typedesc[string], value: string): Result[string, cstring] = ok(value) -proc decodeString*(t: typedesc[Slot], value: string): Result[Slot, cstring] = +func decodeString*(t: typedesc[Slot], value: string): Result[Slot, cstring] = let res = ? Base10.decode(uint64, value) ok(Slot(res)) -proc decodeString*(t: typedesc[Epoch], value: string): Result[Epoch, cstring] = +func decodeString*(t: typedesc[Epoch], value: string): Result[Epoch, cstring] = let res = ? Base10.decode(uint64, value) ok(Epoch(res)) -proc decodeString*(t: typedesc[SyncCommitteePeriod], +func decodeString*(t: typedesc[SyncCommitteePeriod], value: string): Result[SyncCommitteePeriod, cstring] = let res = ? Base10.decode(uint64, value) ok(SyncCommitteePeriod(res)) -proc decodeString*(t: typedesc[uint64], +func decodeString*(t: typedesc[uint64], value: string): Result[uint64, cstring] = Base10.decode(uint64, value) -proc decodeString*(t: typedesc[StateIdent], +func decodeString*(t: typedesc[StateIdent], value: string): Result[StateIdent, cstring] = if len(value) > 2: if (value[0] == '0') and (value[1] == 'x'): @@ -4207,7 +4166,7 @@ proc decodeString*(t: typedesc[StateIdent], let res = ? Base10.decode(uint64, value) ok(StateIdent(kind: StateQueryKind.Slot, slot: Slot(res))) -proc decodeString*(t: typedesc[BlockIdent], +func decodeString*(t: typedesc[BlockIdent], value: string): Result[BlockIdent, cstring] = if len(value) > 2: if (value[0] == '0') and (value[1] == 'x'): @@ -4236,7 +4195,7 @@ proc decodeString*(t: typedesc[BlockIdent], let res = ? Base10.decode(uint64, value) ok(BlockIdent(kind: BlockQueryKind.Slot, slot: Slot(res))) -proc decodeString*(t: typedesc[BroadcastValidationType], +func decodeString*(t: typedesc[BroadcastValidationType], value: string): Result[BroadcastValidationType, cstring] = case value of "gossip": @@ -4248,7 +4207,7 @@ proc decodeString*(t: typedesc[BroadcastValidationType], else: err("Incorrect broadcast validation type value") -proc decodeString*(t: typedesc[ValidatorIdent], +func decodeString*(t: typedesc[ValidatorIdent], value: string): Result[ValidatorIdent, cstring] = if len(value) > 2: if (value[0] == '0') and (value[1] == 'x'): @@ -4269,21 +4228,21 @@ proc decodeString*(t: typedesc[ValidatorIdent], ok(ValidatorIdent(kind: ValidatorQueryKind.Index, index: RestValidatorIndex(res))) -proc decodeString*(t: typedesc[PeerId], +func decodeString*(t: typedesc[PeerId], value: string): Result[PeerId, cstring] = PeerId.init(value) -proc decodeString*(t: typedesc[CommitteeIndex], +func decodeString*(t: typedesc[CommitteeIndex], value: string): Result[CommitteeIndex, cstring] = let res = ? Base10.decode(uint64, value) CommitteeIndex.init(res) -proc decodeString*(t: typedesc[SyncSubcommitteeIndex], +func decodeString*(t: typedesc[SyncSubcommitteeIndex], value: string): Result[SyncSubcommitteeIndex, cstring] = let res = ? Base10.decode(uint64, value) SyncSubcommitteeIndex.init(res) -proc decodeString*(t: typedesc[Eth2Digest], +func decodeString*(t: typedesc[Eth2Digest], value: string): Result[Eth2Digest, cstring] = if len(value) != RootHashSize + 2: return err("Incorrect root value length") @@ -4291,7 +4250,7 @@ proc decodeString*(t: typedesc[Eth2Digest], return err("Incorrect root value encoding") parseRoot(value) -proc decodeString*(t: typedesc[ValidatorFilter], +func decodeString*(t: typedesc[ValidatorFilter], value: string): Result[ValidatorFilter, cstring] = case value of "pending_initialized": @@ -4336,14 +4295,16 @@ proc decodeString*(t: typedesc[ValidatorFilter], else: err("Incorrect validator state identifier value") -proc decodeString*(t: typedesc[ConsensusFork], +func decodeString*(t: typedesc[ConsensusFork], value: string): Result[ConsensusFork, cstring] = + static: doAssert ConsensusFork.high == ConsensusFork.Electra case toLowerAscii(value) of "phase0": ok(ConsensusFork.Phase0) of "altair": ok(ConsensusFork.Altair) of "bellatrix": ok(ConsensusFork.Bellatrix) of "capella": ok(ConsensusFork.Capella) of "deneb": ok(ConsensusFork.Deneb) + of "electra": ok(ConsensusFork.Electra) else: err("Unsupported or invalid beacon block fork version") proc decodeString*(t: typedesc[EventBeaconBlockObject],