From 2d519ac7153eda830e070dbe23e7b1796bd218e3 Mon Sep 17 00:00:00 2001 From: Jacek Sieka Date: Sat, 9 Mar 2024 11:38:24 +0100 Subject: [PATCH] remove some unused rest serialization code (#6056) --- .../eth2_apis/eth2_rest_serialization.nim | 220 +++++++----------- 1 file changed, 81 insertions(+), 139 deletions(-) diff --git a/beacon_chain/spec/eth2_apis/eth2_rest_serialization.nim b/beacon_chain/spec/eth2_apis/eth2_rest_serialization.nim index 5373e10ac..d98d34caf 100644 --- a/beacon_chain/spec/eth2_apis/eth2_rest_serialization.nim +++ b/beacon_chain/spec/eth2_apis/eth2_rest_serialization.nim @@ -478,8 +478,6 @@ proc prepareJsonResponse*(t: typedesc[RestApiResponse], d: auto): seq[byte] = writer.writeField("data", d) writer.endRecord() stream.getOutput(seq[byte]) - except SerializationError: - default except IOError: default res @@ -499,8 +497,6 @@ proc prepareJsonStringResponse*[T: SomeForkedLightClientObject]( writer.writeField("data", forkyObject) writer.endRecord() stream.getOutput(string) - except SerializationError: - default except IOError: default res @@ -514,8 +510,6 @@ proc prepareJsonStringResponse*(t: typedesc[RestApiResponse], d: auto): string = var writer = JsonWriter[RestJson].init(stream) writer.writeValue(d) stream.getOutput(string) - except SerializationError: - default except IOError: default res @@ -536,8 +530,6 @@ proc jsonResponseWRoot*(t: typedesc[RestApiResponse], data: auto, writer.writeField("data", data) writer.endRecord() stream.getOutput(seq[byte]) - except SerializationError: - default except IOError: default RestApiResponse.response(res, Http200, "application/json") @@ -553,8 +545,6 @@ proc jsonResponse*(t: typedesc[RestApiResponse], data: auto): RestApiResponse = writer.writeField("data", data) writer.endRecord() stream.getOutput(seq[byte]) - except SerializationError: - default except IOError: default RestApiResponse.response(res, Http200, "application/json") @@ -580,8 +570,6 @@ proc jsonResponseBlock*(t: typedesc[RestApiResponse], writer.writeField("data", data) writer.endRecord() stream.getOutput(seq[byte]) - except SerializationError: - default except IOError: default RestApiResponse.response(res, Http200, "application/json", headers = headers) @@ -607,8 +595,6 @@ proc jsonResponseBlock*(t: typedesc[RestApiResponse], writer.writeField("data", forkyBlck) writer.endRecord() stream.getOutput(seq[byte]) - except SerializationError: - default except IOError: default RestApiResponse.response(res, Http200, "application/json", headers = headers) @@ -632,8 +618,6 @@ proc jsonResponseState*(t: typedesc[RestApiResponse], writer.writeField("data", forkyState.data) writer.endRecord() stream.getOutput(seq[byte]) - except SerializationError: - default except IOError: default RestApiResponse.response(res, Http200, "application/json", headers = headers) @@ -652,8 +636,6 @@ proc jsonResponseWOpt*(t: typedesc[RestApiResponse], data: auto, writer.writeField("data", data) writer.endRecord() stream.getOutput(seq[byte]) - except SerializationError: - default except IOError: default RestApiResponse.response(res, Http200, "application/json") @@ -674,8 +656,6 @@ proc jsonResponseFinalized*(t: typedesc[RestApiResponse], data: auto, writer.writeField("data", data) writer.endRecord() stream.getOutput(seq[byte]) - except SerializationError: - default except IOError: default RestApiResponse.response(res, Http200, "application/json") @@ -695,8 +675,6 @@ proc jsonResponseWVersion*(t: typedesc[RestApiResponse], data: auto, writer.writeField("data", data) writer.endRecord() stream.getOutput(seq[byte]) - except SerializationError: - default except IOError: default RestApiResponse.response(res, Http200, "application/json", headers = headers) @@ -718,8 +696,6 @@ proc jsonResponseVersioned*[T: SomeForkedLightClientObject]( writer.writeField("data", forkyObject) writer.endRecord() stream.getOutput(seq[byte]) - except SerializationError: - default except IOError: default RestApiResponse.response(res, Http200, "application/json") @@ -734,8 +710,6 @@ proc jsonResponsePlain*(t: typedesc[RestApiResponse], var writer = JsonWriter[RestJson].init(stream) writer.writeValue(data) stream.getOutput(seq[byte]) - except SerializationError: - default except IOError: default RestApiResponse.response(res, Http200, "application/json") @@ -750,8 +724,6 @@ proc jsonResponsePlain*(t: typedesc[RestApiResponse], var writer = JsonWriter[RestJson].init(stream) writer.writeValue(data) stream.getOutput(seq[byte]) - except SerializationError: - default except IOError: default RestApiResponse.response(res, Http200, "application/json", headers = headers) @@ -769,8 +741,6 @@ proc jsonResponseWMeta*(t: typedesc[RestApiResponse], writer.writeField("meta", meta) writer.endRecord() stream.getOutput(seq[byte]) - except SerializationError: - default except IOError: default RestApiResponse.response(res, Http200, "application/json") @@ -788,8 +758,6 @@ proc jsonMsgResponse*(t: typedesc[RestApiResponse], writer.writeField("message", msg) writer.endRecord() stream.getOutput(seq[byte]) - except SerializationError: - default except IOError: default RestApiResponse.response(data, Http200, "application/json") @@ -807,8 +775,6 @@ proc jsonError*(t: typedesc[RestApiResponse], status: HttpCode = Http200, writer.writeField("message", msg) writer.endRecord() stream.getOutput(string) - except SerializationError: - default except IOError: default RestApiResponse.error(status, data, "application/json") @@ -828,8 +794,6 @@ proc jsonError*(t: typedesc[RestApiResponse], status: HttpCode = Http200, writer.writeField("stacktraces", [stacktrace]) writer.endRecord() stream.getOutput(string) - except SerializationError: - default except IOError: default RestApiResponse.error(status, data, "application/json") @@ -849,8 +813,6 @@ proc jsonError*(t: typedesc[RestApiResponse], status: HttpCode = Http200, writer.writeField("stacktraces", stacktraces) writer.endRecord() stream.getOutput(string) - except SerializationError: - default except IOError: default RestApiResponse.error(status, data, "application/json") @@ -870,8 +832,6 @@ proc jsonError*(t: typedesc[RestApiResponse], writer.writeField("stacktraces", rmsg.stacktraces) writer.endRecord() stream.getOutput(string) - except SerializationError: - default except IOError: default RestApiResponse.error(rmsg.code.toHttpCode().get(), data, "application/json") @@ -891,8 +851,6 @@ proc jsonErrorList*(t: typedesc[RestApiResponse], writer.writeField("failures", failures) writer.endRecord() stream.getOutput(string) - except SerializationError: - default except IOError: default RestApiResponse.error(status, data, "application/json") @@ -915,8 +873,6 @@ proc sszResponseVersioned*[T: SomeForkedLightClientObject]( writer.writeValue forkyUpdate cursor.finalWrite (stream.pos - initPos).uint64.toBytesLE() stream.getOutput(seq[byte]) - except SerializationError: - default except IOError: default RestApiResponse.response(res, Http200, "application/octet-stream") @@ -938,8 +894,6 @@ proc sszResponse*(t: typedesc[RestApiResponse], data: auto, var writer = SszWriter.init(stream) writer.writeValue(data) stream.getOutput(seq[byte]) - except SerializationError: - default except IOError: default RestApiResponse.response(res, Http200, "application/octet-stream", @@ -955,8 +909,6 @@ proc sszResponse*(t: typedesc[RestApiResponse], data: auto, var writer = SszWriter.init(stream) writer.writeValue(data) stream.getOutput(seq[byte]) - except SerializationError: - default except IOError: default RestApiResponse.response(res, Http200, "application/octet-stream", @@ -1483,68 +1435,62 @@ proc readValue*[BlockType: ProduceBlockResponseV2]( of ConsensusFork.Phase0: let res = try: - Opt.some(RestJson.decode(string(data.get()), - phase0.BeaconBlock, - requireAllFields = true, - allowUnknownFields = true)) + RestJson.decode(string(data.get()), + phase0.BeaconBlock, + requireAllFields = true, + allowUnknownFields = true) except SerializationError: - Opt.none(phase0.BeaconBlock) - if res.isNone(): - reader.raiseUnexpectedValue("Incorrect phase0 block format") + reader.raiseUnexpectedValue("Incorrect phase0 block format") value = ProduceBlockResponseV2(kind: ConsensusFork.Phase0, - phase0Data: res.get()) + phase0Data: res) of ConsensusFork.Altair: let res = try: - Opt.some(RestJson.decode(string(data.get()), - altair.BeaconBlock, - requireAllFields = true, - allowUnknownFields = true)) + RestJson.decode(string(data.get()), + altair.BeaconBlock, + requireAllFields = true, + allowUnknownFields = true) except SerializationError: - Opt.none(altair.BeaconBlock) - if res.isNone(): - reader.raiseUnexpectedValue("Incorrect altair block format") + reader.raiseUnexpectedValue("Incorrect altair block format") + value = ProduceBlockResponseV2(kind: ConsensusFork.Altair, - altairData: res.get()) + altairData: res) of ConsensusFork.Bellatrix: let res = try: - Opt.some(RestJson.decode(string(data.get()), - bellatrix.BeaconBlock, - requireAllFields = true, - allowUnknownFields = true)) + RestJson.decode(string(data.get()), + bellatrix.BeaconBlock, + requireAllFields = true, + allowUnknownFields = true) except SerializationError: - Opt.none(bellatrix.BeaconBlock) - if res.isNone(): - reader.raiseUnexpectedValue("Incorrect bellatrix block format") + reader.raiseUnexpectedValue("Incorrect bellatrix block format") + value = ProduceBlockResponseV2(kind: ConsensusFork.Bellatrix, - bellatrixData: res.get()) + bellatrixData: res) of ConsensusFork.Capella: let res = try: - Opt.some(RestJson.decode(string(data.get()), - capella.BeaconBlock, - requireAllFields = true, - allowUnknownFields = true)) + RestJson.decode(string(data.get()), + capella.BeaconBlock, + requireAllFields = true, + allowUnknownFields = true) except SerializationError: - Opt.none(capella.BeaconBlock) - if res.isNone(): - reader.raiseUnexpectedValue("Incorrect capella block format") + reader.raiseUnexpectedValue("Incorrect capella block format") + value = ProduceBlockResponseV2(kind: ConsensusFork.Capella, - capellaData: res.get()) + capellaData: res) of ConsensusFork.Deneb: let res = try: - Opt.some(RestJson.decode(string(data.get()), - deneb.BlockContents, - requireAllFields = true, - allowUnknownFields = true)) + RestJson.decode(string(data.get()), + deneb.BlockContents, + requireAllFields = true, + allowUnknownFields = true) except SerializationError: - Opt.none(deneb.BlockContents) - if res.isNone(): - reader.raiseUnexpectedValue("Incorrect deneb block format") + reader.raiseUnexpectedValue("Incorrect deneb block format") + value = ProduceBlockResponseV2(kind: ConsensusFork.Deneb, - denebData: res.get()) + denebData: res) proc readValue*[BlockType: ForkedBlindedBeaconBlock]( reader: var JsonReader[RestJson], @@ -1571,6 +1517,7 @@ proc readValue*[BlockType: ForkedBlindedBeaconBlock]( except SerializationError as exc: reader.raiseUnexpectedValue("Incorrect phase0 block format, [" & exc.formatMsg("BlindedBlock") & "]") + value = ForkedBlindedBeaconBlock(kind: ConsensusFork.Phase0, phase0Data: res) of ConsensusFork.Altair: @@ -1625,20 +1572,20 @@ proc readValue*[BlockType: Web3SignerForkedBeaconBlock]( prepareForkedBlockReading(BlockType, reader, version, data, blinded, payloadValue, blockValue) - let res = - try: - Opt.some(RestJson.decode(string(data.get()), - BeaconBlockHeader, - requireAllFields = true, - allowUnknownFields = true)) - except SerializationError: - Opt.none(BeaconBlockHeader) - if res.isNone(): - reader.raiseUnexpectedValue("Incorrect block header format") if version.get() <= ConsensusFork.Altair: reader.raiseUnexpectedValue( "Web3Signer implementation supports Bellatrix and newer") - value = Web3SignerForkedBeaconBlock(kind: version.get(), data: res.get()) + + let res = + try: + RestJson.decode(string(data.get()), + BeaconBlockHeader, + requireAllFields = true, + allowUnknownFields = true) + except SerializationError: + reader.raiseUnexpectedValue("Incorrect block header format") + + value = Web3SignerForkedBeaconBlock(kind: version.get(), data: res) proc writeValue*[BlockType: Web3SignerForkedBeaconBlock]( writer: var JsonWriter[RestJson], value: BlockType) {.raises: [IOError].} = @@ -2275,63 +2222,58 @@ proc readValue*(reader: var JsonReader[RestJson], of ConsensusFork.Phase0: let res = try: - Opt.some(RestJson.decode(string(data.get()), - phase0.SignedBeaconBlock, - requireAllFields = true, - allowUnknownFields = true)) + RestJson.decode(string(data.get()), + phase0.SignedBeaconBlock, + requireAllFields = true, + allowUnknownFields = true) except SerializationError: - Opt.none(phase0.SignedBeaconBlock) - if res.isNone(): - reader.raiseUnexpectedValue("Incorrect phase0 block format") - value = ForkedSignedBeaconBlock.init(res.get()) + reader.raiseUnexpectedValue("Incorrect phase0 block format") + + value = ForkedSignedBeaconBlock.init(res) of ConsensusFork.Altair: let res = try: - Opt.some(RestJson.decode(string(data.get()), - altair.SignedBeaconBlock, - requireAllFields = true, - allowUnknownFields = true)) + RestJson.decode(string(data.get()), + altair.SignedBeaconBlock, + requireAllFields = true, + allowUnknownFields = true) except SerializationError: - Opt.none(altair.SignedBeaconBlock) - if res.isNone(): - reader.raiseUnexpectedValue("Incorrect altair block format") - value = ForkedSignedBeaconBlock.init(res.get()) + reader.raiseUnexpectedValue("Incorrect altair block format") + + value = ForkedSignedBeaconBlock.init(res) of ConsensusFork.Bellatrix: let res = try: - Opt.some(RestJson.decode(string(data.get()), - bellatrix.SignedBeaconBlock, - requireAllFields = true, - allowUnknownFields = true)) + RestJson.decode(string(data.get()), + bellatrix.SignedBeaconBlock, + requireAllFields = true, + allowUnknownFields = true) except SerializationError: - Opt.none(bellatrix.SignedBeaconBlock) - if res.isNone(): - reader.raiseUnexpectedValue("Incorrect bellatrix block format") - value = ForkedSignedBeaconBlock.init(res.get()) + reader.raiseUnexpectedValue("Incorrect bellatrix block format") + + value = ForkedSignedBeaconBlock.init(res) of ConsensusFork.Capella: let res = try: - Opt.some(RestJson.decode(string(data.get()), - capella.SignedBeaconBlock, - requireAllFields = true, - allowUnknownFields = true)) + RestJson.decode(string(data.get()), + capella.SignedBeaconBlock, + requireAllFields = true, + allowUnknownFields = true) except SerializationError: - Opt.none(capella.SignedBeaconBlock) - if res.isNone(): - reader.raiseUnexpectedValue("Incorrect capella block format") - value = ForkedSignedBeaconBlock.init(res.get()) + reader.raiseUnexpectedValue("Incorrect capella block format") + + value = ForkedSignedBeaconBlock.init(res) of ConsensusFork.Deneb: let res = try: - Opt.some(RestJson.decode(string(data.get()), - deneb.SignedBeaconBlock, - requireAllFields = true, - allowUnknownFields = true)) + RestJson.decode(string(data.get()), + deneb.SignedBeaconBlock, + requireAllFields = true, + allowUnknownFields = true) except SerializationError: - Opt.none(deneb.SignedBeaconBlock) - if res.isNone(): - reader.raiseUnexpectedValue("Incorrect deneb block format") - value = ForkedSignedBeaconBlock.init(res.get()) + reader.raiseUnexpectedValue("Incorrect deneb block format") + + value = ForkedSignedBeaconBlock.init(res) withBlck(value): forkyBlck.root = hash_tree_root(forkyBlck.message)