From 8644d89ad76af4e9c2496d84285fd2b11c34fda9 Mon Sep 17 00:00:00 2001 From: tersec Date: Wed, 18 Sep 2024 11:57:05 +0000 Subject: [PATCH] consolidate RestPublishedSignedBeaconBlock and RestPublishedSignedBlockContents parsing fork handling --- .../eth2_apis/eth2_rest_serialization.nim | 112 +++++------------- 1 file changed, 31 insertions(+), 81 deletions(-) diff --git a/beacon_chain/spec/eth2_apis/eth2_rest_serialization.nim b/beacon_chain/spec/eth2_apis/eth2_rest_serialization.nim index fa3a7d7db9..949a7af185 100644 --- a/beacon_chain/spec/eth2_apis/eth2_rest_serialization.nim +++ b/beacon_chain/spec/eth2_apis/eth2_rest_serialization.nim @@ -2884,10 +2884,10 @@ proc decodeBody*( template getBlck(blckType: untyped): untyped = try: - let blck = RestJson.decode(body.data, blckType, - requireAllFields = true, - allowUnknownFields = true) - RestPublishedSignedBeaconBlock(ForkedSignedBeaconBlock.init(blck)) + RestPublishedSignedBeaconBlock(ForkedSignedBeaconBlock.init( + RestJson.decode(body.data, blckType, + requireAllFields = true, + allowUnknownFields = true))) except SerializationError as exc: debug "Failed to decode JSON data", err = exc.formatMsg(""), @@ -2898,16 +2898,9 @@ proc decodeBody*( return err(RestErrorMessage.init(Http400, UnexpectedDecodeError, [version, $exc.msg])) - let data = - case consensusFork - of ConsensusFork.Phase0: getBlck(phase0.SignedBeaconBlock) - of ConsensusFork.Altair: getBlck(altair.SignedBeaconBlock) - of ConsensusFork.Bellatrix: getBlck(bellatrix.SignedBeaconBlock) - of ConsensusFork.Capella: getBlck(capella.SignedBeaconBlock) - of ConsensusFork.Deneb: getBlck(deneb.SignedBeaconBlock) - of ConsensusFork.Electra: getBlck(electra.SignedBeaconBlock) + withConsensusFork(consensusFork): + ok(getBlck(consensusFork.SignedBeaconBlock)) - ok(data) elif body.contentType == OctetStreamMediaType: let consensusFork = ConsensusFork.decodeString(version).valueOr: return err(RestErrorMessage.init(Http400, UnableDecodeVersionError, @@ -2992,76 +2985,33 @@ proc decodeBody*( let consensusFork = ConsensusFork.decodeString(version).valueOr: return err(RestErrorMessage.init(Http400, UnableDecodeVersionError, [version, $error])) + + template getBlck(blckType: untyped): untyped = + try: + var res = RestJson.decode(body.data, blckType, + requireAllFields = true, + allowUnknownFields = true) + when compiles(res.signed_block.messsage): + {.error: "Deneb and later forks handled in case statement".} + else: + RestPublishedSignedBlockContents.init( + res.message, hash_tree_root(res.message), res.signature) + except SerializationError as exc: + debug "Failed to decode JSON data", + err = exc.formatMsg(""), + data = string.fromBytes(body.data) + return err(RestErrorMessage.init(Http400, UnableDecodeError, + [version, exc.formatMsg("")])) + except CatchableError as exc: + return err(RestErrorMessage.init(Http400, UnexpectedDecodeError, + [version, $exc.msg])) + let data = case consensusFork - of ConsensusFork.Phase0: - try: - var res = RestJson.decode(body.data, phase0.SignedBeaconBlock, - requireAllFields = true, - allowUnknownFields = true) - res.root = hash_tree_root(res.message) - RestPublishedSignedBlockContents( - kind: ConsensusFork.Phase0, phase0Data: res) - except SerializationError as exc: - debug "Failed to decode JSON data", - err = exc.formatMsg(""), - data = string.fromBytes(body.data) - return err(RestErrorMessage.init(Http400, UnableDecodeError, - [version, exc.formatMsg("")])) - except CatchableError as exc: - return err(RestErrorMessage.init(Http400, UnexpectedDecodeError, - [version, $exc.msg])) - of ConsensusFork.Altair: - try: - var res = RestJson.decode(body.data, altair.SignedBeaconBlock, - requireAllFields = true, - allowUnknownFields = true) - res.root = hash_tree_root(res.message) - RestPublishedSignedBlockContents( - kind: ConsensusFork.Altair, altairData: res) - except SerializationError as exc: - debug "Failed to decode JSON data", - err = exc.formatMsg(""), - data = string.fromBytes(body.data) - return err(RestErrorMessage.init(Http400, UnableDecodeError, - [version, exc.formatMsg("")])) - except CatchableError as exc: - return err(RestErrorMessage.init(Http400, UnexpectedDecodeError, - [version, $exc.msg])) - of ConsensusFork.Bellatrix: - try: - var res = RestJson.decode(body.data, bellatrix.SignedBeaconBlock, - requireAllFields = true, - allowUnknownFields = true) - res.root = hash_tree_root(res.message) - RestPublishedSignedBlockContents( - kind: ConsensusFork.Bellatrix, bellatrixData: res) - except SerializationError as exc: - debug "Failed to decode JSON data", - err = exc.formatMsg(""), - data = string.fromBytes(body.data) - return err(RestErrorMessage.init(Http400, UnableDecodeError, - [version, exc.formatMsg("")])) - except CatchableError as exc: - return err(RestErrorMessage.init(Http400, UnexpectedDecodeError, - [version, $exc.msg])) - of ConsensusFork.Capella: - try: - var res = RestJson.decode(body.data, capella.SignedBeaconBlock, - requireAllFields = true, - allowUnknownFields = true) - res.root = hash_tree_root(res.message) - RestPublishedSignedBlockContents( - kind: ConsensusFork.Capella, capellaData: res) - except SerializationError as exc: - debug "Failed to decode JSON data", - err = exc.formatMsg(""), - data = string.fromBytes(body.data) - return err(RestErrorMessage.init(Http400, UnableDecodeError, - [version, exc.formatMsg("")])) - except CatchableError as exc: - return err(RestErrorMessage.init(Http400, UnexpectedDecodeError, - [version, $exc.msg])) + of ConsensusFork.Phase0: getBlck(phase0.SignedBeaconBlock) + of ConsensusFork.Altair: getBlck(altair.SignedBeaconBlock) + of ConsensusFork.Bellatrix: getBlck(bellatrix.SignedBeaconBlock) + of ConsensusFork.Capella: getBlck(capella.SignedBeaconBlock) of ConsensusFork.Deneb: try: var res = RestJson.decode(body.data, DenebSignedBlockContents,