Skip to content

Commit

Permalink
Support more content-types when specifying the Graffiti string
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilIvanichkovv authored and zah committed Jan 6, 2022
1 parent 0e2b4e3 commit 2a12d1c
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 10 deletions.
2 changes: 1 addition & 1 deletion beacon_chain/rpc/rest_constants.nim
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const
"Missing `randao_reveal` value"
InvalidRandaoRevealValue* =
"Invalid randao reveal value"
InvalidGraffitiBytesValye* =
InvalidGraffitiBytesValue* =
"Invalid graffiti bytes value"
InvalidEpochValueError* =
"Invalid epoch value"
Expand Down
29 changes: 25 additions & 4 deletions beacon_chain/rpc/rest_nimbus_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,33 @@ proc installNimbusApiHandlers*(router: var RestRouter, node: BeaconNode) =
return RestApiResponse.jsonResponse((peers: res))

router.api(MethodPost, "/nimbus/v1/graffiti") do (
value: Option[GraffitiBytes]) -> RestApiResponse:
if value.isSome() and value.get().isOk():
node.graffitiBytes = value.get().get()
contentBody: Option[ContentBody]) -> RestApiResponse:
if contentBody.isNone:
return RestApiResponse.jsonError(Http400, EmptyRequestBodyError)

template setGraffitiAux(node: BeaconNode, graffitiStr: string): RestApiResponse =
node.graffitiBytes = try:
GraffitiBytes.init(graffitiStr)
except CatchableError as err:
return RestApiResponse.jsonError(Http400, InvalidGraffitiBytesValue,
err.msg)
RestApiResponse.jsonResponse((result: true))

case contentBody.get.contentType
of "application/json":
let graffitiBytes = decodeBody(GraffitiBytes, contentBody.get)
if graffitiBytes.isErr:
return RestApiResponse.jsonError(Http400, InvalidGraffitiBytesValue,
$graffitiBytes.error)
node.graffitiBytes = graffitiBytes.get
return RestApiResponse.jsonResponse((result: true))
of "text/plain":
return node.setGraffitiAux contentBody.get.strData
of "application/x-www-form-urlencoded":
return node.setGraffitiAux decodeUrl(contentBody.get.strData)
else:
return RestApiResponse.jsonError(Http400, InvalidGraffitiBytesValye)
return RestApiResponse.jsonError(Http400, "Unsupported content type: " &
$contentBody.get.contentType)

router.api(MethodGet, "/nimbus/v1/graffiti") do (
) -> RestApiResponse:
Expand Down
6 changes: 5 additions & 1 deletion beacon_chain/rpc/rest_utils.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import std/[options, macros],
presto,
stew/byteutils, presto,
nimcrypto/utils as ncrutils,
../spec/[forks],
../spec/eth2_apis/[rest_types, eth2_rest_serialization],
Expand Down Expand Up @@ -191,6 +191,10 @@ template withStateForBlockSlot*(nodeParam: BeaconNode,
withStateVars(stateToAdvance[]):
body

template strData*(body: ContentBody): string =
bind fromBytes
string.fromBytes(body.data)

proc toValidatorIndex*(value: RestValidatorIndex): Result[ValidatorIndex,
ValidatorIndexError] =
when sizeof(ValidatorIndex) == 4:
Expand Down
4 changes: 2 additions & 2 deletions beacon_chain/rpc/rest_validator_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ proc installValidatorApiHandlers*(router: var RestRouter, node: BeaconNode) =
let res = graffiti.get()
if res.isErr():
return RestApiResponse.jsonError(Http400,
InvalidGraffitiBytesValye,
InvalidGraffitiBytesValue,
$res.error())
res.get()
let qhead =
Expand Down Expand Up @@ -371,7 +371,7 @@ proc installValidatorApiHandlers*(router: var RestRouter, node: BeaconNode) =
let res = graffiti.get()
if res.isErr():
return RestApiResponse.jsonError(Http400,
InvalidGraffitiBytesValye,
InvalidGraffitiBytesValue,
$res.error())
res.get()
let qhead =
Expand Down
2 changes: 1 addition & 1 deletion beacon_chain/version.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const
versionMinor* = 5
versionBuild* = 5

versionBlob* = "stateofus" # Single word - ends up in the default graffitti
versionBlob* = "stateofus" # Single word - ends up in the default graffiti

gitRevision* = strip(staticExec("git rev-parse --short HEAD"))[0..5]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Environment="WEB3_URL=wss://provider/"
#
# To completely override the start command (to add custom parameters such as
# graffitti), override the `ExecStart` value instead:
# graffiti), override the `ExecStart` value instead:
#
# [Service]
# ExecStart=/usr/bin/nimbus_beacon_node --network=${NETWORK} \
Expand Down

0 comments on commit 2a12d1c

Please sign in to comment.