implement the getStateRandao Beacon API (#4799)

This commit is contained in:
tersec 2023-04-11 15:27:48 +00:00 committed by GitHub
parent 0fbf911722
commit eed34e740a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 110 additions and 0 deletions

View File

@ -711,6 +711,53 @@ proc installBeaconApiHandlers*(router: var RestRouter, node: BeaconNode) =
return RestApiResponse.jsonError(Http404, StateNotFoundError) return RestApiResponse.jsonError(Http404, StateNotFoundError)
# https://ethereum.github.io/beacon-APIs/?urls.primaryName=dev#/Beacon/getStateRandao
# https://github.com/ethereum/beacon-APIs/blob/b3c4defa238aaa74bf22aa602aa1b24b68a4c78e/apis/beacon/states/randao.yaml
router.api(MethodGet,
"/eth/v1/beacon/states/{state_id}/randao") do (
state_id: StateIdent, epoch: Option[Epoch]) -> RestApiResponse:
let
sid = state_id.valueOr:
return RestApiResponse.jsonError(Http400, InvalidStateIdValueError,
$error)
bslot = node.getBlockSlotId(sid).valueOr:
if sid.kind == StateQueryKind.Root:
# TODO (cheatfate): Its impossible to retrieve state by `state_root`
# in current version of database.
return RestApiResponse.jsonError(Http500, NoImplementationError)
return RestApiResponse.jsonError(Http404, StateNotFoundError,
$error)
let qepoch =
if epoch.isSome():
let repoch = epoch.get()
if repoch.isErr():
return RestApiResponse.jsonError(Http400, InvalidEpochValueError,
$repoch.error())
let res = repoch.get()
if res > MaxEpoch:
return RestApiResponse.jsonError(Http400, EpochOverflowValueError)
if res < node.dag.cfg.ALTAIR_FORK_EPOCH:
return RestApiResponse.jsonError(Http400,
EpochFromTheIncorrectForkError)
if res > bslot.slot.epoch() + 1:
return RestApiResponse.jsonError(Http400,
EpochFromFutureError)
res
else:
# If ``epoch`` not present then the RANDAO mix for the epoch of
# the state will be obtained.
bslot.slot.epoch()
node.withStateForBlockSlotId(bslot):
withState(state):
return RestApiResponse.jsonResponseWOpt(
RestEpochRandao(randao: get_randao_mix(forkyState.data, qepoch)),
node.getStateOptimistic(state)
)
return RestApiResponse.jsonError(Http404, StateNotFoundError)
# https://ethereum.github.io/beacon-APIs/#/Beacon/getBlockHeaders # https://ethereum.github.io/beacon-APIs/#/Beacon/getBlockHeaders
router.api(MethodGet, "/eth/v1/beacon/headers") do ( router.api(MethodGet, "/eth/v1/beacon/headers") do (
slot: Option[Slot], parent_root: Option[Eth2Digest]) -> RestApiResponse: slot: Option[Slot], parent_root: Option[Eth2Digest]) -> RestApiResponse:

View File

@ -495,6 +495,9 @@ type
validators*: seq[ValidatorIndex] validators*: seq[ValidatorIndex]
validator_aggregates*: seq[seq[ValidatorIndex]] validator_aggregates*: seq[seq[ValidatorIndex]]
RestEpochRandao* = object
randao*: Eth2Digest
DataEnclosedObject*[T] = object DataEnclosedObject*[T] = object
data*: T data*: T

View File

@ -1561,6 +1561,66 @@
"body": [{"operator": "jstructcmps", "start": ["data"], "value": [{"index": "", "slot": "", "validators": [""]}]}] "body": [{"operator": "jstructcmps", "start": ["data"], "value": [{"index": "", "slot": "", "validators": [""]}]}]
} }
}, },
{
"topics": ["beacon", "states_randao_mix"],
"request": {
"url": "/eth/v1/beacon/states/head/randao",
"headers": {"Accept": "application/json"}
},
"response": {
"status": {"operator": "equals", "value": "200"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmps", "start": ["data"], "value": {"randao": ""}}]
}
},
{
"topics": ["beacon", "states_randao_mix"],
"request": {
"url": "/eth/v1/beacon/states/head/randao?epoch=0",
"headers": {"Accept": "application/json"}
},
"response": {
"status": {"operator": "equals", "value": "200"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmps", "start": ["data"], "value": {"randao": ""}}]
}
},
{
"topics": ["beacon", "states_randao_mix"],
"request": {
"url": "/eth/v1/beacon/states/head/randao?epoch=576460752303423488",
"headers": {"Accept": "application/json"}
},
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
"topics": ["beacon", "states_randao_mix"],
"request": {
"url": "/eth/v1/beacon/states/head/randao?epoch=18446744073709551615",
"headers": {"Accept": "application/json"}
},
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{
"topics": ["beacon", "states_randao_mix"],
"request": {
"url": "/eth/v1/beacon/states/head/randao?epoch=18446744073709551616",
"headers": {"Accept": "application/json"}
},
"response": {
"status": {"operator": "equals", "value": "400"},
"headers": [{"key": "Content-Type", "value": "application/json", "operator": "equals"}],
"body": [{"operator": "jstructcmpns", "value": {"code": 400, "message": ""}}]
}
},
{ {
"topics": ["beacon", "beacon_headers"], "topics": ["beacon", "beacon_headers"],
"request": { "request": {