Fixing bug.

This commit is contained in:
cheatfate 2024-05-03 16:28:40 +03:00
parent 86af562deb
commit 78c8c8e431
No known key found for this signature in database
GPG Key ID: 46ADD633A7201F95
3 changed files with 29 additions and 147 deletions

View File

@ -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)

View File

@ -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>"),
data = string.fromBytes(body.data)
return err(
RestErrorMessage.init(Http400, UnableDecodeError,
[version, exc.formatMsg("<data>")]))
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>"),
data = string.fromBytes(body.data)
return err(
RestErrorMessage.init(Http400, UnableDecodeError,
[version, exc.formatMsg("<data>")]))
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>"),
data = string.fromBytes(body.data)
return err(
RestErrorMessage.init(Http400, UnableDecodeError,
[version, exc.formatMsg("<data>")]))
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>"),
data = string.fromBytes(body.data)
return err(
RestErrorMessage.init(Http400, UnableDecodeError,
[version, exc.formatMsg("<data>")]))
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>"),
data = string.fromBytes(body.data)
return err(
RestErrorMessage.init(Http400, UnableDecodeError,
[version, exc.formatMsg("<data>")]))
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>"),
data = string.fromBytes(body.data)
return err(
RestErrorMessage.init(Http400, UnableDecodeError,
[version, exc.formatMsg("<data>")]))
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:

View File

@ -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():