Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SSZ encoding support to REST API endpoint /eth/v1/validator/register_validator #6943

Merged
merged 4 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion beacon_chain/rpc/rest_constants.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# beacon_chain
# Copyright (c) 2021-2024 Status Research & Development GmbH
# Copyright (c) 2021-2025 Status Research & Development GmbH
# Licensed and distributed under either of
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
Expand Down Expand Up @@ -271,3 +271,4 @@ const
"Unable to load state for parent block, database corrupt?"
RewardOverflowError* =
"Reward value overflow"
InvalidContentTypeError* = "Invalid content type"
22 changes: 9 additions & 13 deletions beacon_chain/rpc/rest_validator_api.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2018-2024 Status Research & Development GmbH
# Copyright (c) 2018-2025 Status Research & Development GmbH
# Licensed and distributed under either of
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
Expand Down Expand Up @@ -1215,25 +1215,21 @@ proc installValidatorApiHandlers*(router: var RestRouter, node: BeaconNode) =
router.api2(MethodPost,
"/eth/v1/validator/register_validator") do (
contentBody: Option[ContentBody]) -> RestApiResponse:
if contentBody.isNone():
return RestApiResponse.jsonError(Http400, EmptyRequestBodyError)
let
body =
block:
if contentBody.isNone():
return RestApiResponse.jsonError(Http400, EmptyRequestBodyError)
let dres = decodeBody(seq[SignedValidatorRegistrationV1], contentBody.get())
if dres.isErr():
return RestApiResponse.jsonError(Http400,
InvalidPrepareBeaconProposerError)
dres.get()
body = decodeBodyJsonOrSsz(seq[SignedValidatorRegistrationV1],
contentBody.get()).valueOr:
return RestApiResponse.jsonError(error)

for signedValidatorRegistration in body:
for registration in body:
# Don't validate beyond syntactically, because
# "requests containing currently inactive or unknown validator pubkeys
# will be accepted, as they may become active at a later epoch". Along
# these lines, even if it's adding a validator the BN already has as a
# local validator, the keymanager API might remove that from the BN.
node.externalBuilderRegistrations[signedValidatorRegistration.message.pubkey] =
signedValidatorRegistration
node.externalBuilderRegistrations[registration.message.pubkey] =
registration

RestApiResponse.response(Http200)

Expand Down
42 changes: 39 additions & 3 deletions beacon_chain/spec/eth2_apis/eth2_rest_serialization.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3036,7 +3036,7 @@ proc decodeBody*(
[version, $exc.msg]))
ok(RestPublishedSignedBeaconBlock(ForkedSignedBeaconBlock.init(blck)))
else:
err(RestErrorMessage.init(Http415, "Invalid content type",
err(RestErrorMessage.init(Http415, InvalidContentTypeError,
[version, $body.contentType]))

proc decodeBody*(
Expand Down Expand Up @@ -3232,9 +3232,45 @@ proc decodeBody*(
ok(RestPublishedSignedBlockContents(
kind: ConsensusFork.Fulu, fuluData: blckContents))
else:
err(RestErrorMessage.init(Http415, "Invalid content type",
err(RestErrorMessage.init(Http415, InvalidContentTypeError,
[version, $body.contentType]))

proc decodeBodyJsonOrSsz*(
t: typedesc[seq[SignedValidatorRegistrationV1]],
body: ContentBody
): Result[seq[SignedValidatorRegistrationV1], RestErrorMessage] =
if body.contentType == ApplicationJsonMediaType:
let data =
try:
RestJson.decode(
body.data,
seq[SignedValidatorRegistrationV1],
requireAllFields = true,
allowUnknownFields = true)
except SerializationError as exc:
debug "Failed to deserialize REST JSON data",
err = exc.formatMsg("<data>")
return err(
RestErrorMessage.init(Http400, UnableDecodeError,
[exc.formatMsg("<data>")]))
ok(data)
elif body.contentType == OctetStreamMediaType:
let data =
try:
SSZ.decode(
body.data,
List[SignedValidatorRegistrationV1, Limit VALIDATOR_REGISTRY_LIMIT])
except SerializationError as exc:
debug "Failed to deserialize REST SSZ data",
err = exc.formatMsg("<data>")
return err(
RestErrorMessage.init(Http400, UnableDecodeError,
[exc.formatMsg("<data>")]))
ok(data.toSeq)
else:
err(RestErrorMessage.init(Http415, InvalidContentTypeError,
[$body.contentType]))

proc decodeBody*[T](t: typedesc[T],
body: ContentBody): Result[T, cstring] =
if body.contentType != ApplicationJsonMediaType:
Expand Down Expand Up @@ -3285,7 +3321,7 @@ proc decodeBodyJsonOrSsz*[T](t: typedesc[T],
RestErrorMessage.init(Http400, UnexpectedDecodeError, [$exc.msg]))
ok(blck)
else:
err(RestErrorMessage.init(Http415, "Invalid content type",
err(RestErrorMessage.init(Http415, InvalidContentTypeError,
[$body.contentType]))

proc encodeBytes*[T: EncodeTypes](value: T,
Expand Down
25 changes: 25 additions & 0 deletions ncli/resttest-rules.json
Original file line number Diff line number Diff line change
Expand Up @@ -5000,6 +5000,31 @@
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}]
}
},
{
"topics": ["validator", "register_validators"],
"request": {
"url": "/eth/v1/validator/register_validator",
"method": "POST",
"headers": {"Accept": "application/json"},
"body": {"content-type": "application/octet-stream", "data": "64000000000000000000000000000000000000000000000000000000b5b5b76700000000a81da27d35ce91ddd3129042e47d0bf91a0ff60ed415ed25cda46b42c8d9cb91eb727966e5054828417bda9a1e5e8ba79043ce8b1542c118846537dc33ec2bfe8fa63858d8104b94b7673dd1390f05ac8ff949ba709cfde534857ddfbf757faa0af4a9e1e3743add7c51ca36223a580d2fa25b4dc46623e57f8d75fc647e6966c4828ee90c8eaf4f1babaa4b2f40d82bc8000000000000000000000000000000000000000000000000000000b5b5b76700000000886979da71af92933ebb6055a8c4a43a0c5e4ab645f9272ab6d441a5c2645c3278893439890eeda72f5580f05ae0898b8ba37e74091eb05ab1f66bf02f03ab7bfe0ee8f471ff8de8e10ef71164cef57a2ea694cde397d678a2ca18994cb2e154170e2a71a40fca2505c1e375c2ec05f9bc0a4f987968822177fe8d6326890362e0e55d18d0cf30b171ae44f969dbdf33"}
},
"response": {
"status": {"operator": "equals", "value": "200"}
}
},
{
"topics": ["validator", "register_validators"],
"request": {
"url": "/eth/v1/validator/register_validator",
"method": "POST",
"headers": {"Accept": "application/json"},
"body": {"content-type": "application/octet-stream", "data": "64000000000000000000000000000000000000000000000000000000b5b5b76700000000a81da27d35ce91ddd3129042e47d0bf91a0ff60ed415ed25cda46b42c8d9cb91eb727966e5054828417bda9a1e5e8ba79043ce8b1542c118846537dc33ec2bfe8fa63858d8104b94b7673dd1390f05ac8ff949ba709cfde534857ddfbf757faa0af4a9e1e3743add7c51ca36223a580d2fa25b4dc46623e57f8d75fc647e6966c4828ee90c8eaf4f1babaa4b2f40d82bc8000000000000000000000000000000000000000000000000000000b5b5b76700000000886979da71af92933ebb6055a8c4a43a0c5e4ab645f9272ab6d441a5c2645c3278893439890eeda72f5580f05ae0898b8ba37e74091eb05ab1f66bf02f03ab7bfe0ee8f471ff8de8e10ef71164cef57a2ea694cde397d678a2ca18994cb2e154170e2a71a40fca2505c1e375c2ec05f9bc0a4f987968822177fe8d6326890362e0e55d18d0cf30b171ae44f969dbdf"}
},
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}]
}
},
{
"topics": ["key_management", "list_keys"],
"request": {
Expand Down
14 changes: 11 additions & 3 deletions ncli/resttest.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# beacon_chain
# Copyright (c) 2021-2024 Status Research & Development GmbH
# Copyright (c) 2021-2025 Status Research & Development GmbH
# Licensed and distributed under either of
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
Expand All @@ -10,7 +10,7 @@
import
std/[strutils, os, options, uri, json, tables],
results,
stew/[io2, base10],
stew/[io2, base10, byteutils],
confutils, chronicles, httputils,
chronos, chronos/streams/[asyncstream, tlsstream]

Expand Down Expand Up @@ -455,7 +455,15 @@ proc prepareRequest(uri: Uri,
return err("Field `body.data` must be present")
if bdata.kind != JString:
return err("Field `body.data` should be string")
(btype.str, bdata.str)
if toLowerAscii(btype.str) == "application/octet-stream":
let data =
try:
string.fromBytes(hexToSeqByte(bdata.str))
except ValueError:
return err("Field `body.data` should be valid hexadecimal string")
(btype.str, data)
else:
(btype.str, bdata.str)

var res = meth & " " & uri.path & requestUri & " HTTP/1.1\r\n"
res.add("Content-Length: " & Base10.toString(uint64(len(requestBodyData))) &
Expand Down
Loading