diff --git a/beacon_chain/rpc/rest_beacon_api.nim b/beacon_chain/rpc/rest_beacon_api.nim index 60444a14e..341445c70 100644 --- a/beacon_chain/rpc/rest_beacon_api.nim +++ b/beacon_chain/rpc/rest_beacon_api.nim @@ -939,7 +939,7 @@ proc installBeaconApiHandlers*(router: var RestRouter, node: BeaconNode) = if contentBody.isNone(): return RestApiResponse.jsonError(Http400, EmptyRequestBodyError) contentBody.get() - restBlock = decodeBodyJsonOrSsz( + restBlock = decodeBody( RestPublishedSignedBlockContents, body, version).valueOr: return RestApiResponse.jsonError(error) diff --git a/beacon_chain/spec/eth2_apis/eth2_rest_serialization.nim b/beacon_chain/spec/eth2_apis/eth2_rest_serialization.nim index f7c58eae5..9cbd3a8c7 100644 --- a/beacon_chain/spec/eth2_apis/eth2_rest_serialization.nim +++ b/beacon_chain/spec/eth2_apis/eth2_rest_serialization.nim @@ -3592,10 +3592,10 @@ proc decodeBody*( [version, $body.contentType])) proc decodeBody*( - t: typedesc[RestPublishedSignedBlockContents], - body: ContentBody, - version: string - ): Result[RestPublishedSignedBlockContents, RestErrorMessage] = + t: typedesc[RestPublishedSignedBlockContents], + body: ContentBody, + version: string +): Result[RestPublishedSignedBlockContents, RestErrorMessage] = if body.contentType == ApplicationJsonMediaType: let data = try: @@ -3711,136 +3711,6 @@ proc decodeBody*[T](t: typedesc[T], return err("Unexpected deserialization error") ok(data) -proc decodeBodyJsonOrSsz*( - t: typedesc[RestPublishedSignedBlockContents], - body: ContentBody, - version: string - ): Result[RestPublishedSignedBlockContents, RestErrorMessage] = - if body.contentType == OctetStreamMediaType: - decodeBody(RestPublishedSignedBlockContents, body, version) - elif body.contentType == ApplicationJsonMediaType: - let consensusFork = ConsensusFork.decodeString(version).valueOr: - return err(RestErrorMessage.init(Http400, UnableDecodeVersionError, - [version, $error])) - case consensusFork - of ConsensusFork.Phase0: - let blck = - try: - RestJson.decode(body.data, phase0.SignedBeaconBlock, - requireAllFields = true, - allowUnknownFields = true) - 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])) - ok(RestPublishedSignedBlockContents( - kind: ConsensusFork.Phase0, phase0Data: blck)) - of ConsensusFork.Altair: - let blck = - try: - RestJson.decode(body.data, altair.SignedBeaconBlock, - requireAllFields = true, - allowUnknownFields = true) - 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])) - ok(RestPublishedSignedBlockContents( - kind: ConsensusFork.Altair, altairData: blck)) - of ConsensusFork.Bellatrix: - let blck = - try: - RestJson.decode(body.data, bellatrix.SignedBeaconBlock, - requireAllFields = true, - allowUnknownFields = true) - 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])) - ok(RestPublishedSignedBlockContents( - kind: ConsensusFork.Bellatrix, bellatrixData: blck)) - of ConsensusFork.Capella: - let blck = - try: - RestJson.decode(body.data, capella.SignedBeaconBlock, - requireAllFields = true, - allowUnknownFields = true) - 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])) - ok(RestPublishedSignedBlockContents( - kind: ConsensusFork.Capella, capellaData: blck)) - of ConsensusFork.Deneb: - let blckContents = - try: - RestJson.decode(body.data, DenebSignedBlockContents, - requireAllFields = true, - allowUnknownFields = true) - 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])) - ok(RestPublishedSignedBlockContents( - kind: ConsensusFork.Deneb, denebData: blckContents)) - of ConsensusFork.Electra: - let blckContents = - try: - RestJson.decode(body.data, ElectraSignedBlockContents, - requireAllFields = true, - allowUnknownFields = true) - 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])) - ok(RestPublishedSignedBlockContents( - kind: ConsensusFork.Electra, electraData: blckContents)) - else: - err(RestErrorMessage.init(Http415, "Invalid content type", - [version, $body.contentType])) - proc decodeBodyJsonOrSsz*[T](t: typedesc[T], body: ContentBody): Result[T, RestErrorMessage] = if body.contentType == ApplicationJsonMediaType: diff --git a/beacon_chain/validator_client/api.nim b/beacon_chain/validator_client/api.nim index 036745501..106919431 100644 --- a/beacon_chain/validator_client/api.nim +++ b/beacon_chain/validator_client/api.nim @@ -2382,17 +2382,23 @@ proc publishBlockV2*( {BeaconNodeRole.BlockProposalPublish}): case data.kind of ConsensusFork.Phase0: - publishBlockV2(it, data.phase0Data) + publishBlockV2(it, data.phase0Data, Opt.some(validation), + Opt.some(contentType)) of ConsensusFork.Altair: - publishBlockV2(it, data.altairData) + publishBlockV2(it, data.altairData, Opt.some(validation), + Opt.some(contentType)) of ConsensusFork.Bellatrix: - publishBlockV2(it, data.bellatrixData) + publishBlockV2(it, data.bellatrixData, Opt.some(validation), + Opt.some(contentType)) of ConsensusFork.Capella: - publishBlockV2(it, data.capellaData) + publishBlockV2(it, data.capellaData, Opt.some(validation), + Opt.some(contentType)) of ConsensusFork.Deneb: - publishBlockV2(it, data.denebData) + publishBlockV2(it, data.denebData, Opt.some(validation), + Opt.some(contentType)) of ConsensusFork.Electra: - publishBlockV2(it, data.electraData) + publishBlockV2(it, data.electraData, Opt.some(validation), + Opt.some(contentType)) do: if apiResponse.isErr(): handleCommunicationError() @@ -2433,17 +2439,23 @@ proc publishBlockV2*( {BeaconNodeRole.BlockProposalPublish}): case data.kind of ConsensusFork.Phase0: - publishBlock(it, data.phase0Data) + publishBlockV2(it, data.phase0Data, Opt.some(validation), + Opt.some(contentType)) of ConsensusFork.Altair: - publishBlock(it, data.altairData) + publishBlockV2(it, data.altairData, Opt.some(validation), + Opt.some(contentType)) of ConsensusFork.Bellatrix: - publishBlock(it, data.bellatrixData) + publishBlockV2(it, data.bellatrixData, Opt.some(validation), + Opt.some(contentType)) of ConsensusFork.Capella: - publishBlock(it, data.capellaData) + publishBlockV2(it, data.capellaData, Opt.some(validation), + Opt.some(contentType)) of ConsensusFork.Deneb: - publishBlock(it, data.denebData) + publishBlockV2(it, data.denebData, Opt.some(validation), + Opt.some(contentType)) of ConsensusFork.Electra: - publishBlock(it, data.electraData) + publishBlockV2(it, data.electraData, Opt.some(validation), + Opt.some(contentType)) do: if apiResponse.isErr():