rest: fix ssz preference string (#3357)

This commit is contained in:
Jacek Sieka 2022-02-04 14:26:27 +01:00 committed by GitHub
parent e0fb5d95a6
commit 6f10e651ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 8 deletions

View File

@ -115,7 +115,7 @@ proc publishBlock*(body: altair.SignedBeaconBlock): RestPlainResponse {.
proc getBlockPlain*(block_id: BlockIdent): RestPlainResponse {. proc getBlockPlain*(block_id: BlockIdent): RestPlainResponse {.
rest, endpoint: "/eth/v1/beacon/blocks/{block_id}", rest, endpoint: "/eth/v1/beacon/blocks/{block_id}",
accept: "application/octet-stream,application-json;q=0.9", accept: preferSSZ,
meth: MethodGet.} meth: MethodGet.}
## https://ethereum.github.io/beacon-APIs/#/Beacon/getBlock ## https://ethereum.github.io/beacon-APIs/#/Beacon/getBlock
@ -138,7 +138,9 @@ proc raiseUnknownStatusError(resp: RestPlainResponse)
raise newException(RestError, msg) raise newException(RestError, msg)
proc getBlock*(client: RestClientRef, block_id: BlockIdent, proc getBlock*(client: RestClientRef, block_id: BlockIdent,
restAccept = ""): Future[ForkedSignedBeaconBlock] {.async.} = restAccept = preferSSZ): Future[ForkedSignedBeaconBlock] {.async.} =
# TODO restAccept should be "" by default, but for some reason that doesn't
# work
let resp = let resp =
if len(restAccept) > 0: if len(restAccept) > 0:
await client.getBlockPlain(block_id, restAcceptType = restAccept) await client.getBlockPlain(block_id, restAcceptType = restAccept)
@ -176,16 +178,18 @@ proc getBlock*(client: RestClientRef, block_id: BlockIdent,
proc getBlockV2Plain*(block_id: BlockIdent): RestPlainResponse {. proc getBlockV2Plain*(block_id: BlockIdent): RestPlainResponse {.
rest, endpoint: "/eth/v2/beacon/blocks/{block_id}", rest, endpoint: "/eth/v2/beacon/blocks/{block_id}",
accept: "application/octet-stream,application-json;q=0.9", accept: preferSSZ,
meth: MethodGet.} meth: MethodGet.}
## https://ethereum.github.io/beacon-APIs/#/Beacon/getBlockV2 ## https://ethereum.github.io/beacon-APIs/#/Beacon/getBlockV2
proc getBlockV2*(client: RestClientRef, block_id: BlockIdent, proc getBlockV2*(client: RestClientRef, block_id: BlockIdent,
cfg: RuntimeConfig, cfg: RuntimeConfig,
restAccept = ""): Future[Option[ForkedSignedBeaconBlock]] {. restAccept = preferSSZ): Future[Option[ForkedSignedBeaconBlock]] {.
async.} = async.} =
# Return the asked-for block, or None in case 404 is returned from the server. # Return the asked-for block, or None in case 404 is returned from the server.
# Raises on other errors # Raises on other errors
# TODO restAccept should be "" by default, but for some reason that doesn't
# work
let resp = let resp =
if len(restAccept) > 0: if len(restAccept) > 0:
await client.getBlockV2Plain(block_id, restAcceptType = restAccept) await client.getBlockV2Plain(block_id, restAcceptType = restAccept)

View File

@ -15,12 +15,14 @@ export chronos, client, rest_types, eth2_rest_serialization
proc getStatePlain*(state_id: StateIdent): RestPlainResponse {. proc getStatePlain*(state_id: StateIdent): RestPlainResponse {.
rest, endpoint: "/eth/v1/debug/beacon/states/{state_id}", rest, endpoint: "/eth/v1/debug/beacon/states/{state_id}",
accept: "application/octet-stream,application-json;q=0.9", accept: preferSSZ,
meth: MethodGet.} meth: MethodGet.}
## https://ethereum.github.io/beacon-APIs/#/Beacon/getState ## https://ethereum.github.io/beacon-APIs/#/Beacon/getState
proc getState*(client: RestClientRef, state_id: StateIdent, proc getState*(client: RestClientRef, state_id: StateIdent,
restAccept = ""): Future[phase0.BeaconState] {.async.} = restAccept = preferSSZ): Future[phase0.BeaconState] {.async.} =
# TODO restAccept should be "" by default, but for some reason that doesn't
# work
let resp = let resp =
if len(restAccept) > 0: if len(restAccept) > 0:
await client.getStatePlain(state_id, restAcceptType = restAccept) await client.getStatePlain(state_id, restAcceptType = restAccept)
@ -73,15 +75,17 @@ proc getDebugChainHeads*(): RestResponse[GetDebugChainHeadsResponse] {.
proc getStateV2Plain*(state_id: StateIdent): RestPlainResponse {. proc getStateV2Plain*(state_id: StateIdent): RestPlainResponse {.
rest, endpoint: "/eth/v2/debug/beacon/states/{state_id}", rest, endpoint: "/eth/v2/debug/beacon/states/{state_id}",
accept: "application/octet-stream,application-json;q=0.9", accept: preferSSZ,
meth: MethodGet.} meth: MethodGet.}
## https://ethereum.github.io/beacon-APIs/#/Debug/getStateV2 ## https://ethereum.github.io/beacon-APIs/#/Debug/getStateV2
proc getStateV2*(client: RestClientRef, state_id: StateIdent, proc getStateV2*(client: RestClientRef, state_id: StateIdent,
cfg: RuntimeConfig, cfg: RuntimeConfig,
restAccept = ""): Future[ref ForkedHashedBeaconState] {.async.} = restAccept = preferSSZ): Future[ref ForkedHashedBeaconState] {.async.} =
# nil is returned if the state is not found due to a 404 - `ref` is needed # nil is returned if the state is not found due to a 404 - `ref` is needed
# to manage stack usage # to manage stack usage
# TODO restAccept should be "" by default, but for some reason that doesn't
# work
let resp = let resp =
if len(restAccept) > 0: if len(restAccept) > 0:
await client.getStateV2Plain(state_id, restAcceptType = restAccept) await client.getStateV2Plain(state_id, restAcceptType = restAccept)

View File

@ -26,6 +26,9 @@ const
# https://github.com/ethereum/eth2.0-APIs/blob/master/apis/beacon/states/validators.yaml#L17 # https://github.com/ethereum/eth2.0-APIs/blob/master/apis/beacon/states/validators.yaml#L17
MaximumValidatorIds* = 16384 MaximumValidatorIds* = 16384
const
preferSSZ* = "application/octet-stream,application/json;q=0.9"
type type
EventTopic* {.pure.} = enum EventTopic* {.pure.} = enum
Head, Block, Attestation, VoluntaryExit, FinalizedCheckpoint, ChainReorg, Head, Block, Attestation, VoluntaryExit, FinalizedCheckpoint, ChainReorg,