Fix POST to /eth/v2/beacon/blocks unable to verify correct block signature. (#6261)

This commit is contained in:
Eugene Kabanov 2024-05-06 07:15:17 +03:00 committed by GitHub
parent d0cea28a79
commit a6f68d3edc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 1 additions and 131 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

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