add some electra support to REST JSON decoding; rm unused code (#6549)

This commit is contained in:
tersec 2024-09-15 13:33:13 +00:00 committed by GitHub
parent bd47f62f07
commit 72b5c2bd6e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 46 additions and 85 deletions

View File

@ -15,15 +15,11 @@ import results, stew/[assign2, base10, byteutils, endians2], presto/common,
stint, chronicles stint, chronicles
import ".."/[eth2_ssz_serialization, forks, keystore], import ".."/[eth2_ssz_serialization, forks, keystore],
".."/../consensus_object_pools/block_pools_types, ".."/../consensus_object_pools/block_pools_types,
".."/datatypes/[phase0, altair, bellatrix],
".."/mev/[bellatrix_mev, capella_mev], ".."/mev/[bellatrix_mev, capella_mev],
".."/../validators/slashing_protection_common, ".."/../validators/slashing_protection_common,
"."/[rest_types, rest_keymanager_types] "."/[rest_types, rest_keymanager_types]
import nimcrypto/utils as ncrutils import nimcrypto/utils as ncrutils
from ".."/datatypes/capella import SignedBeaconBlock
from ".."/datatypes/deneb import BeaconState
export export
eth2_ssz_serialization, results, peerid, common, serialization, chronicles, eth2_ssz_serialization, results, peerid, common, serialization, chronicles,
json_serialization, net, sets, rest_types, slashing_protection_common, 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 ## 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 ## 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 const
DecimalSet = {'0' .. '9'} DecimalSet = {'0' .. '9'}
@ -1516,20 +1512,8 @@ proc readValue*[BlockType: ForkedBlindedBeaconBlock](
exc.formatMsg("BlindedBlock") & "]") exc.formatMsg("BlindedBlock") & "]")
value = ForkedBlindedBeaconBlock(kind: ConsensusFork.Altair, value = ForkedBlindedBeaconBlock(kind: ConsensusFork.Altair,
altairData: res) altairData: res)
of ConsensusFork.Bellatrix: of ConsensusFork.Bellatrix .. ConsensusFork.Capella:
reader.raiseUnexpectedValue("Bellatrix blinded block format unsupported") reader.raiseUnexpectedValue("pre-Deneb blinded block formats 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.Deneb: of ConsensusFork.Deneb:
let res = let res =
try: try:
@ -1592,34 +1576,6 @@ proc writeValue*[BlockType: Web3SignerForkedBeaconBlock](
writer.writeField("block_header", value.data) writer.writeField("block_header", value.data)
writer.endRecord() 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 ## RestPublishedBeaconBlockBody
proc readValue*(reader: var JsonReader[RestJson], proc readValue*(reader: var JsonReader[RestJson],
value: var RestPublishedBeaconBlockBody) {. value: var RestPublishedBeaconBlockBody) {.
@ -2161,6 +2117,7 @@ proc readValue*(reader: var JsonReader[RestJson],
reader.raiseUnexpectedField("Multiple version fields found", reader.raiseUnexpectedField("Multiple version fields found",
"ForkedSignedBeaconBlock") "ForkedSignedBeaconBlock")
let vres = reader.readValue(string) let vres = reader.readValue(string)
static: doAssert ConsensusFork.high == ConsensusFork.Electra
case vres case vres
of "phase0": of "phase0":
version = Opt.some(ConsensusFork.Phase0) version = Opt.some(ConsensusFork.Phase0)
@ -2172,6 +2129,8 @@ proc readValue*(reader: var JsonReader[RestJson],
version = Opt.some(ConsensusFork.Capella) version = Opt.some(ConsensusFork.Capella)
of "deneb": of "deneb":
version = Opt.some(ConsensusFork.Deneb) version = Opt.some(ConsensusFork.Deneb)
of "electra":
version = Opt.some(ConsensusFork.Electra)
else: else:
reader.raiseUnexpectedValue("Incorrect version field value") reader.raiseUnexpectedValue("Incorrect version field value")
of "data": of "data":
@ -3908,10 +3867,10 @@ proc decodeBytes*[T: DecodeTypes](
else: else:
err("Content-Type not supported") err("Content-Type not supported")
proc encodeString*(value: string): RestResult[string] = func encodeString*(value: string): RestResult[string] =
ok(value) ok(value)
proc encodeString*( func encodeString*(
value: value:
uint64 | uint64 |
SyncCommitteePeriod | SyncCommitteePeriod |
@ -3921,26 +3880,26 @@ proc encodeString*(
SyncSubcommitteeIndex): RestResult[string] = SyncSubcommitteeIndex): RestResult[string] =
ok(Base10.toString(uint64(value))) ok(Base10.toString(uint64(value)))
proc encodeString*(value: ValidatorSig): RestResult[string] = func encodeString*(value: ValidatorSig): RestResult[string] =
ok(hexOriginal(toRaw(value))) ok(hexOriginal(toRaw(value)))
proc encodeString*(value: GraffitiBytes): RestResult[string] = func encodeString*(value: GraffitiBytes): RestResult[string] =
ok(hexOriginal(distinctBase(value))) ok(hexOriginal(distinctBase(value)))
proc encodeString*(value: Eth2Digest): RestResult[string] = func encodeString*(value: Eth2Digest): RestResult[string] =
ok(hexOriginal(value.data)) ok(hexOriginal(value.data))
proc encodeString*(value: ValidatorIdent): RestResult[string] = func encodeString*(value: ValidatorIdent): RestResult[string] =
case value.kind case value.kind
of ValidatorQueryKind.Index: of ValidatorQueryKind.Index:
ok(Base10.toString(uint64(value.index))) ok(Base10.toString(uint64(value.index)))
of ValidatorQueryKind.Key: of ValidatorQueryKind.Key:
ok(hexOriginal(toRaw(value.key))) ok(hexOriginal(toRaw(value.key)))
proc encodeString*(value: ValidatorPubKey): RestResult[string] = func encodeString*(value: ValidatorPubKey): RestResult[string] =
ok(hexOriginal(toRaw(value))) ok(hexOriginal(toRaw(value)))
proc encodeString*(value: StateIdent): RestResult[string] = func encodeString*(value: StateIdent): RestResult[string] =
case value.kind case value.kind
of StateQueryKind.Slot: of StateQueryKind.Slot:
ok(Base10.toString(uint64(value.slot))) ok(Base10.toString(uint64(value.slot)))
@ -3957,7 +3916,7 @@ proc encodeString*(value: StateIdent): RestResult[string] =
of StateIdentType.Justified: of StateIdentType.Justified:
ok("justified") ok("justified")
proc encodeString*(value: BroadcastValidationType): RestResult[string] = func encodeString*(value: BroadcastValidationType): RestResult[string] =
case value case value
of BroadcastValidationType.Gossip: of BroadcastValidationType.Gossip:
ok("gossip") ok("gossip")
@ -3966,7 +3925,7 @@ proc encodeString*(value: BroadcastValidationType): RestResult[string] =
of BroadcastValidationType.ConsensusAndEquivocation: of BroadcastValidationType.ConsensusAndEquivocation:
ok("consensus_and_equivocation") ok("consensus_and_equivocation")
proc encodeString*(value: BlockIdent): RestResult[string] = func encodeString*(value: BlockIdent): RestResult[string] =
case value.kind case value.kind
of BlockQueryKind.Slot: of BlockQueryKind.Slot:
ok(Base10.toString(uint64(value.slot))) ok(Base10.toString(uint64(value.slot)))
@ -3981,7 +3940,7 @@ proc encodeString*(value: BlockIdent): RestResult[string] =
of BlockIdentType.Finalized: of BlockIdentType.Finalized:
ok("finalized") ok("finalized")
proc decodeString*(t: typedesc[PeerStateKind], func decodeString*(t: typedesc[PeerStateKind],
value: string): Result[PeerStateKind, cstring] = value: string): Result[PeerStateKind, cstring] =
case value case value
of "disconnected": of "disconnected":
@ -3993,9 +3952,9 @@ proc decodeString*(t: typedesc[PeerStateKind],
of "disconnecting": of "disconnecting":
ok(PeerStateKind.Disconnecting) ok(PeerStateKind.Disconnecting)
else: 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 case value
of PeerStateKind.Disconnected: of PeerStateKind.Disconnected:
ok("disconnected") ok("disconnected")
@ -4006,7 +3965,7 @@ proc encodeString*(value: PeerStateKind): Result[string, cstring] =
of PeerStateKind.Disconnecting: of PeerStateKind.Disconnecting:
ok("disconnecting") ok("disconnecting")
proc decodeString*(t: typedesc[PeerDirectKind], func decodeString*(t: typedesc[PeerDirectKind],
value: string): Result[PeerDirectKind, cstring] = value: string): Result[PeerDirectKind, cstring] =
case value case value
of "inbound": of "inbound":
@ -4014,19 +3973,19 @@ proc decodeString*(t: typedesc[PeerDirectKind],
of "outbound": of "outbound":
ok(PeerDirectKind.Outbound) ok(PeerDirectKind.Outbound)
else: 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 case value
of PeerDirectKind.Inbound: of PeerDirectKind.Inbound:
ok("inbound") ok("inbound")
of PeerDirectKind.Outbound: of PeerDirectKind.Outbound:
ok("outbound") ok("outbound")
proc encodeString*(peerid: PeerId): Result[string, cstring] = func encodeString*(peerid: PeerId): Result[string, cstring] =
ok($peerid) ok($peerid)
proc decodeString*(t: typedesc[EventTopic], func decodeString*(t: typedesc[EventTopic],
value: string): Result[EventTopic, cstring] = value: string): Result[EventTopic, cstring] =
case value case value
of "head": of "head":
@ -4058,7 +4017,7 @@ proc decodeString*(t: typedesc[EventTopic],
else: else:
err("Incorrect event's topic value") 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 var res: string
if EventTopic.Head in value: if EventTopic.Head in value:
res.add("head,") res.add("head,")
@ -4091,7 +4050,7 @@ proc encodeString*(value: set[EventTopic]): Result[string, cstring] =
res.setLen(len(res) - 1) res.setLen(len(res) - 1)
ok(res) ok(res)
proc toList*(value: set[ValidatorFilterKind]): seq[string] = func toList*(value: set[ValidatorFilterKind]): seq[string] =
const const
pendingSet = {ValidatorFilterKind.PendingInitialized, pendingSet = {ValidatorFilterKind.PendingInitialized,
ValidatorFilterKind.PendingQueued} ValidatorFilterKind.PendingQueued}
@ -4130,7 +4089,7 @@ proc toList*(value: set[ValidatorFilterKind]): seq[string] =
processSingle(ValidatorFilterKind.WithdrawalDone, "withdrawal_done") processSingle(ValidatorFilterKind.WithdrawalDone, "withdrawal_done")
res res
proc decodeString*(t: typedesc[ValidatorSig], func decodeString*(t: typedesc[ValidatorSig],
value: string): Result[ValidatorSig, cstring] = value: string): Result[ValidatorSig, cstring] =
if len(value) != ValidatorSigSize + 2: if len(value) != ValidatorSigSize + 2:
return err("Incorrect validator signature value length") return err("Incorrect validator signature value length")
@ -4138,7 +4097,7 @@ proc decodeString*(t: typedesc[ValidatorSig],
return err("Incorrect validator signature encoding") return err("Incorrect validator signature encoding")
ValidatorSig.fromHex(value) ValidatorSig.fromHex(value)
proc decodeString*(t: typedesc[ValidatorPubKey], func decodeString*(t: typedesc[ValidatorPubKey],
value: string): Result[ValidatorPubKey, cstring] = value: string): Result[ValidatorPubKey, cstring] =
if len(value) != ValidatorKeySize + 2: if len(value) != ValidatorKeySize + 2:
return err("Incorrect validator's key value length") return err("Incorrect validator's key value length")
@ -4147,35 +4106,35 @@ proc decodeString*(t: typedesc[ValidatorPubKey],
else: else:
ValidatorPubKey.fromHex(value) ValidatorPubKey.fromHex(value)
proc decodeString*(t: typedesc[GraffitiBytes], func decodeString*(t: typedesc[GraffitiBytes],
value: string): Result[GraffitiBytes, cstring] = value: string): Result[GraffitiBytes, cstring] =
try: try:
ok(GraffitiBytes.init(value)) ok(GraffitiBytes.init(value))
except ValueError: except ValueError:
err("Unable to decode graffiti value") err("Unable to decode graffiti value")
proc decodeString*(t: typedesc[string], func decodeString*(t: typedesc[string],
value: string): Result[string, cstring] = value: string): Result[string, cstring] =
ok(value) 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) let res = ? Base10.decode(uint64, value)
ok(Slot(res)) 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) let res = ? Base10.decode(uint64, value)
ok(Epoch(res)) ok(Epoch(res))
proc decodeString*(t: typedesc[SyncCommitteePeriod], func decodeString*(t: typedesc[SyncCommitteePeriod],
value: string): Result[SyncCommitteePeriod, cstring] = value: string): Result[SyncCommitteePeriod, cstring] =
let res = ? Base10.decode(uint64, value) let res = ? Base10.decode(uint64, value)
ok(SyncCommitteePeriod(res)) ok(SyncCommitteePeriod(res))
proc decodeString*(t: typedesc[uint64], func decodeString*(t: typedesc[uint64],
value: string): Result[uint64, cstring] = value: string): Result[uint64, cstring] =
Base10.decode(uint64, value) Base10.decode(uint64, value)
proc decodeString*(t: typedesc[StateIdent], func decodeString*(t: typedesc[StateIdent],
value: string): Result[StateIdent, cstring] = value: string): Result[StateIdent, cstring] =
if len(value) > 2: if len(value) > 2:
if (value[0] == '0') and (value[1] == 'x'): if (value[0] == '0') and (value[1] == 'x'):
@ -4207,7 +4166,7 @@ proc decodeString*(t: typedesc[StateIdent],
let res = ? Base10.decode(uint64, value) let res = ? Base10.decode(uint64, value)
ok(StateIdent(kind: StateQueryKind.Slot, slot: Slot(res))) ok(StateIdent(kind: StateQueryKind.Slot, slot: Slot(res)))
proc decodeString*(t: typedesc[BlockIdent], func decodeString*(t: typedesc[BlockIdent],
value: string): Result[BlockIdent, cstring] = value: string): Result[BlockIdent, cstring] =
if len(value) > 2: if len(value) > 2:
if (value[0] == '0') and (value[1] == 'x'): if (value[0] == '0') and (value[1] == 'x'):
@ -4236,7 +4195,7 @@ proc decodeString*(t: typedesc[BlockIdent],
let res = ? Base10.decode(uint64, value) let res = ? Base10.decode(uint64, value)
ok(BlockIdent(kind: BlockQueryKind.Slot, slot: Slot(res))) ok(BlockIdent(kind: BlockQueryKind.Slot, slot: Slot(res)))
proc decodeString*(t: typedesc[BroadcastValidationType], func decodeString*(t: typedesc[BroadcastValidationType],
value: string): Result[BroadcastValidationType, cstring] = value: string): Result[BroadcastValidationType, cstring] =
case value case value
of "gossip": of "gossip":
@ -4248,7 +4207,7 @@ proc decodeString*(t: typedesc[BroadcastValidationType],
else: else:
err("Incorrect broadcast validation type value") err("Incorrect broadcast validation type value")
proc decodeString*(t: typedesc[ValidatorIdent], func decodeString*(t: typedesc[ValidatorIdent],
value: string): Result[ValidatorIdent, cstring] = value: string): Result[ValidatorIdent, cstring] =
if len(value) > 2: if len(value) > 2:
if (value[0] == '0') and (value[1] == 'x'): if (value[0] == '0') and (value[1] == 'x'):
@ -4269,21 +4228,21 @@ proc decodeString*(t: typedesc[ValidatorIdent],
ok(ValidatorIdent(kind: ValidatorQueryKind.Index, ok(ValidatorIdent(kind: ValidatorQueryKind.Index,
index: RestValidatorIndex(res))) index: RestValidatorIndex(res)))
proc decodeString*(t: typedesc[PeerId], func decodeString*(t: typedesc[PeerId],
value: string): Result[PeerId, cstring] = value: string): Result[PeerId, cstring] =
PeerId.init(value) PeerId.init(value)
proc decodeString*(t: typedesc[CommitteeIndex], func decodeString*(t: typedesc[CommitteeIndex],
value: string): Result[CommitteeIndex, cstring] = value: string): Result[CommitteeIndex, cstring] =
let res = ? Base10.decode(uint64, value) let res = ? Base10.decode(uint64, value)
CommitteeIndex.init(res) CommitteeIndex.init(res)
proc decodeString*(t: typedesc[SyncSubcommitteeIndex], func decodeString*(t: typedesc[SyncSubcommitteeIndex],
value: string): Result[SyncSubcommitteeIndex, cstring] = value: string): Result[SyncSubcommitteeIndex, cstring] =
let res = ? Base10.decode(uint64, value) let res = ? Base10.decode(uint64, value)
SyncSubcommitteeIndex.init(res) SyncSubcommitteeIndex.init(res)
proc decodeString*(t: typedesc[Eth2Digest], func decodeString*(t: typedesc[Eth2Digest],
value: string): Result[Eth2Digest, cstring] = value: string): Result[Eth2Digest, cstring] =
if len(value) != RootHashSize + 2: if len(value) != RootHashSize + 2:
return err("Incorrect root value length") return err("Incorrect root value length")
@ -4291,7 +4250,7 @@ proc decodeString*(t: typedesc[Eth2Digest],
return err("Incorrect root value encoding") return err("Incorrect root value encoding")
parseRoot(value) parseRoot(value)
proc decodeString*(t: typedesc[ValidatorFilter], func decodeString*(t: typedesc[ValidatorFilter],
value: string): Result[ValidatorFilter, cstring] = value: string): Result[ValidatorFilter, cstring] =
case value case value
of "pending_initialized": of "pending_initialized":
@ -4336,14 +4295,16 @@ proc decodeString*(t: typedesc[ValidatorFilter],
else: else:
err("Incorrect validator state identifier value") err("Incorrect validator state identifier value")
proc decodeString*(t: typedesc[ConsensusFork], func decodeString*(t: typedesc[ConsensusFork],
value: string): Result[ConsensusFork, cstring] = value: string): Result[ConsensusFork, cstring] =
static: doAssert ConsensusFork.high == ConsensusFork.Electra
case toLowerAscii(value) case toLowerAscii(value)
of "phase0": ok(ConsensusFork.Phase0) of "phase0": ok(ConsensusFork.Phase0)
of "altair": ok(ConsensusFork.Altair) of "altair": ok(ConsensusFork.Altair)
of "bellatrix": ok(ConsensusFork.Bellatrix) of "bellatrix": ok(ConsensusFork.Bellatrix)
of "capella": ok(ConsensusFork.Capella) of "capella": ok(ConsensusFork.Capella)
of "deneb": ok(ConsensusFork.Deneb) of "deneb": ok(ConsensusFork.Deneb)
of "electra": ok(ConsensusFork.Electra)
else: err("Unsupported or invalid beacon block fork version") else: err("Unsupported or invalid beacon block fork version")
proc decodeString*(t: typedesc[EventBeaconBlockObject], proc decodeString*(t: typedesc[EventBeaconBlockObject],