2021-08-03 15:17:11 +00:00
|
|
|
# Copyright (c) 2018-2021 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).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
|
|
|
import
|
2021-09-27 18:31:11 +00:00
|
|
|
chronos, presto/client, chronicles,
|
|
|
|
".."/[helpers, forks], ".."/datatypes/[phase0, altair, merge],
|
|
|
|
".."/eth2_ssz_serialization,
|
2021-08-03 15:17:11 +00:00
|
|
|
"."/[rest_types, eth2_rest_serialization]
|
|
|
|
|
2021-08-27 09:00:06 +00:00
|
|
|
export chronos, client, rest_types, eth2_rest_serialization
|
2021-08-03 15:17:11 +00:00
|
|
|
|
|
|
|
proc getGenesis*(): RestResponse[GetGenesisResponse] {.
|
|
|
|
rest, endpoint: "/eth/v1/beacon/genesis",
|
|
|
|
meth: MethodGet.}
|
2021-08-27 09:00:06 +00:00
|
|
|
## https://ethereum.github.io/beacon-APIs/#/Beacon/getGenesis
|
2021-08-03 15:17:11 +00:00
|
|
|
|
|
|
|
proc getStateRoot*(state_id: StateIdent): RestResponse[GetStateRootResponse] {.
|
|
|
|
rest, endpoint: "/eth/v1/beacon/states/{state_id}/root",
|
|
|
|
meth: MethodGet.}
|
2021-08-27 09:00:06 +00:00
|
|
|
## https://ethereum.github.io/beacon-APIs/#/Beacon/getStateRoot
|
2021-08-03 15:17:11 +00:00
|
|
|
|
|
|
|
proc getStateFork*(state_id: StateIdent): RestResponse[GetStateForkResponse] {.
|
|
|
|
rest, endpoint: "/eth/v1/beacon/states/{state_id}/fork",
|
|
|
|
meth: MethodGet.}
|
2021-08-27 09:00:06 +00:00
|
|
|
## https://ethereum.github.io/beacon-APIs/#/Beacon/getStateFork
|
2021-08-03 15:17:11 +00:00
|
|
|
|
|
|
|
proc getStateFinalityCheckpoints*(state_id: StateIdent
|
|
|
|
): RestResponse[GetStateFinalityCheckpointsResponse] {.
|
|
|
|
rest, endpoint: "/api/eth/v1/beacon/states/{state_id}/finality_checkpoints",
|
|
|
|
meth: MethodGet.}
|
|
|
|
|
|
|
|
proc getStateValidators*(state_id: StateIdent,
|
|
|
|
id: seq[ValidatorIdent]
|
|
|
|
): RestResponse[GetStateValidatorsResponse] {.
|
|
|
|
rest, endpoint: "/eth/v1/beacon/states/{state_id}/validators",
|
|
|
|
meth: MethodGet.}
|
2021-08-27 09:00:06 +00:00
|
|
|
## https://ethereum.github.io/beacon-APIs/#/Beacon/getStateValidators
|
2021-08-03 15:17:11 +00:00
|
|
|
|
|
|
|
proc getStateValidator*(state_id: StateIdent,
|
|
|
|
validator_id: ValidatorIdent
|
|
|
|
): RestResponse[GetStateValidatorResponse] {.
|
|
|
|
rest,
|
|
|
|
endpoint: "/eth/v1/beacon/states/{state_id}/validators/{validator_id}",
|
|
|
|
meth: MethodGet.}
|
2021-08-27 09:00:06 +00:00
|
|
|
## https://ethereum.github.io/beacon-APIs/#/Beacon/getStateValidator
|
2021-08-03 15:17:11 +00:00
|
|
|
|
|
|
|
proc getStateValidatorBalances*(state_id: StateIdent
|
|
|
|
): RestResponse[GetStateValidatorBalancesResponse] {.
|
|
|
|
rest, endpoint: "/eth/v1/beacon/states/{state_id}/validator_balances",
|
|
|
|
meth: MethodGet.}
|
2021-08-27 09:00:06 +00:00
|
|
|
## https://ethereum.github.io/beacon-APIs/#/Beacon/getStateValidators
|
2021-08-03 15:17:11 +00:00
|
|
|
|
|
|
|
proc getEpochCommittees*(state_id: StateIdent
|
|
|
|
): RestResponse[GetEpochCommitteesResponse] {.
|
|
|
|
rest, endpoint: "/eth/v1/beacon/states/{state_id}/committees",
|
|
|
|
meth: MethodGet.}
|
2021-08-27 09:00:06 +00:00
|
|
|
## https://ethereum.github.io/beacon-APIs/#/Beacon/getEpochCommittees
|
2021-08-03 15:17:11 +00:00
|
|
|
|
|
|
|
# TODO altair
|
|
|
|
# proc getEpochSyncCommittees*(state_id: StateIdent
|
|
|
|
# ): RestResponse[GetEpochSyncCommitteesResponse] {.
|
|
|
|
# rest, endpoint: "/eth/v1/beacon/states/{state_id}/sync_committees",
|
|
|
|
# meth: MethodGet.}
|
2021-08-27 09:00:06 +00:00
|
|
|
# ## https://ethereum.github.io/beacon-APIs/#/Beacon/getEpochSyncCommittees
|
2021-08-03 15:17:11 +00:00
|
|
|
|
|
|
|
proc getBlockHeaders*(slot: Option[Slot], parent_root: Option[Eth2Digest]
|
|
|
|
): RestResponse[GetBlockHeadersResponse] {.
|
|
|
|
rest, endpoint: "/api/eth/v1/beacon/headers",
|
|
|
|
meth: MethodGet.}
|
2021-08-27 09:00:06 +00:00
|
|
|
## https://ethereum.github.io/beacon-APIs/#/Beacon/getBlockHeaders
|
2021-08-03 15:17:11 +00:00
|
|
|
|
|
|
|
proc getBlockHeader*(block_id: BlockIdent): RestResponse[GetBlockHeaderResponse] {.
|
|
|
|
rest, endpoint: "/api/eth/v1/beacon/headers/{block_id}",
|
|
|
|
meth: MethodGet.}
|
2021-08-27 09:00:06 +00:00
|
|
|
## https://ethereum.github.io/beacon-APIs/#/Beacon/getBlockHeader
|
2021-08-03 15:17:11 +00:00
|
|
|
|
|
|
|
proc publishBlock*(body: phase0.SignedBeaconBlock): RestPlainResponse {.
|
|
|
|
rest, endpoint: "/eth/v1/beacon/blocks",
|
|
|
|
meth: MethodPost.}
|
2021-08-27 09:00:06 +00:00
|
|
|
## https://ethereum.github.io/beacon-APIs/#/Beacon/publishBlock
|
|
|
|
|
|
|
|
proc publishBlock*(body: altair.SignedBeaconBlock): RestPlainResponse {.
|
|
|
|
rest, endpoint: "/eth/v1/beacon/blocks",
|
|
|
|
meth: MethodPost.}
|
|
|
|
## https://ethereum.github.io/beacon-APIs/#/Beacon/publishBlock
|
2021-08-03 15:17:11 +00:00
|
|
|
|
2021-09-27 18:31:11 +00:00
|
|
|
proc getBlockPlain*(block_id: BlockIdent): RestPlainResponse {.
|
2021-08-03 15:17:11 +00:00
|
|
|
rest, endpoint: "/api/eth/v1/beacon/blocks/{block_id}",
|
2021-09-27 18:31:11 +00:00
|
|
|
accept: "application/octet-stream,application-json;q=0.9",
|
2021-08-03 15:17:11 +00:00
|
|
|
meth: MethodGet.}
|
|
|
|
|
2021-09-27 18:31:11 +00:00
|
|
|
proc getBlock*(client: RestClientRef, block_id: BlockIdent,
|
|
|
|
restAccept = ""): Future[ForkedSignedBeaconBlock] {.async.} =
|
|
|
|
let resp =
|
|
|
|
if len(restAccept) > 0:
|
|
|
|
await client.getBlockPlain(block_id, restAcceptType = restAccept)
|
|
|
|
else:
|
|
|
|
await client.getBlockPlain(block_id)
|
|
|
|
let data =
|
|
|
|
case resp.status
|
|
|
|
of 200:
|
|
|
|
case resp.contentType
|
|
|
|
of "application/json":
|
|
|
|
let blck =
|
|
|
|
block:
|
|
|
|
let res = decodeBytes(GetBlockResponse, resp.data,
|
|
|
|
resp.contentType)
|
|
|
|
if res.isErr():
|
|
|
|
raise newException(RestError, $res.error())
|
|
|
|
res.get()
|
|
|
|
ForkedSignedBeaconBlock.init(blck.data)
|
|
|
|
of "application/octet-stream":
|
|
|
|
let blck =
|
|
|
|
block:
|
|
|
|
let res = decodeBytes(GetPhase0BlockSszResponse, resp.data,
|
|
|
|
resp.contentType)
|
|
|
|
if res.isErr():
|
|
|
|
raise newException(RestError, $res.error())
|
|
|
|
res.get()
|
|
|
|
ForkedSignedBeaconBlock.init(blck)
|
|
|
|
else:
|
|
|
|
raise newException(RestError, "Unsupported content-type")
|
|
|
|
of 400, 404, 500:
|
|
|
|
let error =
|
|
|
|
block:
|
|
|
|
let res = decodeBytes(RestGenericError, resp.data, resp.contentType)
|
|
|
|
if res.isErr():
|
|
|
|
let msg = "Incorrect response error format (" & $resp.status &
|
|
|
|
") [" & $res.error() & "]"
|
|
|
|
raise newException(RestError, msg)
|
|
|
|
res.get()
|
|
|
|
let msg = "Error response (" & $resp.status & ") [" & error.message & "]"
|
|
|
|
raise newException(RestError, msg)
|
|
|
|
else:
|
|
|
|
let msg = "Unknown response status error (" & $resp.status & ")"
|
|
|
|
raise newException(RestError, msg)
|
|
|
|
return data
|
|
|
|
|
|
|
|
proc getBlockV2Plain*(block_id: BlockIdent): RestPlainResponse {.
|
2021-08-27 09:00:06 +00:00
|
|
|
rest, endpoint: "/api/eth/v2/beacon/blocks/{block_id}",
|
2021-09-27 18:31:11 +00:00
|
|
|
accept: "application/octet-stream,application-json;q=0.9",
|
2021-08-27 09:00:06 +00:00
|
|
|
meth: MethodGet.}
|
|
|
|
## https://ethereum.github.io/beacon-APIs/#/Beacon/getBlockV2
|
2021-08-03 15:17:11 +00:00
|
|
|
|
2021-09-27 18:31:11 +00:00
|
|
|
proc getBlockV2*(client: RestClientRef, block_id: BlockIdent,
|
|
|
|
forks: array[2, Fork],
|
|
|
|
restAccept = ""): Future[ForkedSignedBeaconBlock] {.
|
|
|
|
async.} =
|
|
|
|
let resp =
|
|
|
|
if len(restAccept) > 0:
|
|
|
|
await client.getBlockV2Plain(block_id, restAcceptType = restAccept)
|
|
|
|
else:
|
|
|
|
await client.getBlockV2Plain(block_id)
|
|
|
|
let data =
|
|
|
|
case resp.status
|
|
|
|
of 200:
|
|
|
|
case resp.contentType
|
|
|
|
of "application/json":
|
|
|
|
let blck =
|
|
|
|
block:
|
|
|
|
let res = decodeBytes(GetBlockV2Response, resp.data,
|
|
|
|
resp.contentType)
|
|
|
|
if res.isErr():
|
|
|
|
raise newException(RestError, $res.error())
|
|
|
|
res.get()
|
|
|
|
blck
|
|
|
|
of "application/octet-stream":
|
|
|
|
let header =
|
|
|
|
block:
|
|
|
|
let res = decodeBytes(GetBlockV2Header, resp.data, resp.contentType)
|
|
|
|
if res.isErr():
|
|
|
|
raise newException(RestError, $res.error())
|
|
|
|
res.get()
|
|
|
|
if header.slot.epoch() < forks[1].epoch:
|
|
|
|
let blck =
|
|
|
|
block:
|
|
|
|
let res = decodeBytes(GetPhase0BlockSszResponse, resp.data,
|
|
|
|
resp.contentType)
|
|
|
|
if res.isErr():
|
|
|
|
raise newException(RestError, $res.error())
|
|
|
|
res.get()
|
|
|
|
ForkedSignedBeaconBlock.init(blck)
|
|
|
|
else:
|
|
|
|
let blck =
|
|
|
|
block:
|
|
|
|
let res = decodeBytes(GetAltairBlockSszResponse, resp.data,
|
|
|
|
resp.contentType)
|
|
|
|
if res.isErr():
|
|
|
|
raise newException(RestError, $res.error())
|
|
|
|
res.get()
|
|
|
|
ForkedSignedBeaconBlock.init(blck)
|
|
|
|
else:
|
|
|
|
raise newException(RestError, "Unsupported content-type")
|
|
|
|
of 400, 404, 500:
|
|
|
|
let error =
|
|
|
|
block:
|
|
|
|
let res = decodeBytes(RestGenericError, resp.data, resp.contentType)
|
|
|
|
if res.isErr():
|
|
|
|
let msg = "Incorrect response error format (" & $resp.status &
|
|
|
|
") [" & $res.error() & "]"
|
|
|
|
raise newException(RestError, msg)
|
|
|
|
res.get()
|
|
|
|
let msg = "Error response (" & $resp.status & ") [" & error.message & "]"
|
|
|
|
raise newException(RestError, msg)
|
|
|
|
else:
|
|
|
|
let msg = "Unknown response status error (" & $resp.status & ")"
|
|
|
|
raise newException(RestError, msg)
|
|
|
|
return data
|
|
|
|
|
2021-08-03 15:17:11 +00:00
|
|
|
proc getBlockRoot*(block_id: BlockIdent): RestResponse[GetBlockRootResponse] {.
|
|
|
|
rest, endpoint: "/eth/v1/beacon/blocks/{block_id}/root",
|
|
|
|
meth: MethodGet.}
|
2021-08-27 09:00:06 +00:00
|
|
|
## https://ethereum.github.io/beacon-APIs/#/Beacon/getBlockRoot
|
2021-08-03 15:17:11 +00:00
|
|
|
|
|
|
|
proc getBlockAttestations*(block_id: BlockIdent
|
|
|
|
): RestResponse[GetBlockAttestationsResponse] {.
|
|
|
|
rest, endpoint: "/eth/v1/beacon/blocks/{block_id}/attestations",
|
|
|
|
meth: MethodGet.}
|
2021-08-27 09:00:06 +00:00
|
|
|
## https://ethereum.github.io/beacon-APIs/#/Beacon/getBlockAttestations
|
2021-08-03 15:17:11 +00:00
|
|
|
|
|
|
|
proc getPoolAttestations*(
|
|
|
|
slot: Option[Slot],
|
|
|
|
committee_index: Option[CommitteeIndex]
|
|
|
|
): RestResponse[GetPoolAttestationsResponse] {.
|
|
|
|
rest, endpoint: "/api/eth/v1/beacon/pool/attestations",
|
|
|
|
meth: MethodGet.}
|
2021-08-27 09:00:06 +00:00
|
|
|
## https://ethereum.github.io/beacon-APIs/#/Beacon/getPoolAttestations
|
2021-08-03 15:17:11 +00:00
|
|
|
|
|
|
|
proc submitPoolAttestations*(body: seq[Attestation]): RestPlainResponse {.
|
|
|
|
rest, endpoint: "/eth/v1/beacon/pool/attestations",
|
|
|
|
meth: MethodPost.}
|
2021-08-27 09:00:06 +00:00
|
|
|
## https://ethereum.github.io/beacon-APIs/#/Beacon/submitPoolAttestations
|
2021-08-03 15:17:11 +00:00
|
|
|
|
|
|
|
proc getPoolAttesterSlashings*(): RestResponse[GetPoolAttesterSlashingsResponse] {.
|
|
|
|
rest, endpoint: "/api/eth/v1/beacon/pool/attester_slashings",
|
|
|
|
meth: MethodGet.}
|
2021-08-27 09:00:06 +00:00
|
|
|
## https://ethereum.github.io/beacon-APIs/#/Beacon/getPoolAttesterSlashings
|
2021-08-03 15:17:11 +00:00
|
|
|
|
|
|
|
proc submitPoolAttesterSlashings*(body: AttesterSlashing): RestPlainResponse {.
|
|
|
|
rest, endpoint: "/api/eth/v1/beacon/pool/attester_slashings",
|
|
|
|
meth: MethodPost.}
|
2021-08-27 09:00:06 +00:00
|
|
|
## https://ethereum.github.io/beacon-APIs/#/Beacon/submitPoolAttesterSlashings
|
2021-08-03 15:17:11 +00:00
|
|
|
|
|
|
|
proc getPoolProposerSlashings*(): RestResponse[GetPoolProposerSlashingsResponse] {.
|
|
|
|
rest, endpoint: "/api/eth/v1/beacon/pool/proposer_slashings",
|
|
|
|
meth: MethodGet.}
|
2021-08-27 09:00:06 +00:00
|
|
|
## https://ethereum.github.io/beacon-APIs/#/Beacon/getPoolProposerSlashings
|
2021-08-03 15:17:11 +00:00
|
|
|
|
|
|
|
proc submitPoolProposerSlashings*(body: ProposerSlashing): RestPlainResponse {.
|
|
|
|
rest, endpoint: "/api/eth/v1/beacon/pool/proposer_slashings",
|
|
|
|
meth: MethodPost.}
|
2021-08-27 09:00:06 +00:00
|
|
|
## https://ethereum.github.io/beacon-APIs/#/Beacon/submitPoolProposerSlashings
|
2021-08-03 15:17:11 +00:00
|
|
|
|
|
|
|
# TODO Altair
|
|
|
|
# proc submitPoolSyncCommitteeSignatures*(body: seq[RestSyncCommitteeSignature]): RestPlainResponse {.
|
|
|
|
# rest, endpoint: "/eth/v1/beacon/pool/sync_committees",
|
|
|
|
# meth: MethodPost.}
|
2021-08-27 09:00:06 +00:00
|
|
|
# ## https://ethereum.github.io/beacon-APIs/#/Beacon/submitPoolSyncCommitteeSignatures
|
2021-08-03 15:17:11 +00:00
|
|
|
|
|
|
|
proc getPoolVoluntaryExits*(): RestResponse[GetPoolVoluntaryExitsResponse] {.
|
|
|
|
rest, endpoint: "/api/eth/v1/beacon/pool/voluntary_exits",
|
|
|
|
meth: MethodGet.}
|
2021-08-27 09:00:06 +00:00
|
|
|
## https://ethereum.github.io/beacon-APIs/#/Beacon/getPoolVoluntaryExits
|
2021-08-03 15:17:11 +00:00
|
|
|
|
|
|
|
proc submitPoolVoluntaryExit*(body: SignedVoluntaryExit): RestPlainResponse {.
|
|
|
|
rest, endpoint: "/api/eth/v1/beacon/pool/voluntary_exits",
|
|
|
|
meth: MethodPost.}
|
2021-08-27 09:00:06 +00:00
|
|
|
## https://ethereum.github.io/beacon-APIs/#/Beacon/submitPoolVoluntaryExit
|