2021-03-17 18:46:45 +00:00
|
|
|
# Copyright (c) 2018-2020 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.
|
|
|
|
import
|
2021-03-17 20:42:55 +00:00
|
|
|
std/[typetraits, sequtils, strutils, deques, sets, options],
|
2021-03-17 18:46:45 +00:00
|
|
|
stew/[results, base10],
|
|
|
|
chronicles,
|
|
|
|
nimcrypto/utils as ncrutils,
|
2021-03-17 20:42:55 +00:00
|
|
|
../beacon_node_common, ../networking/eth2_network,
|
|
|
|
../consensus_object_pools/[blockchain_dag, exit_pool],
|
2021-03-23 22:50:18 +00:00
|
|
|
../gossip_processing/gossip_validation,
|
|
|
|
../validators/validator_duties,
|
|
|
|
../spec/[crypto, digest, validator, datatypes, network],
|
2021-03-17 18:46:45 +00:00
|
|
|
../ssz/merkleization,
|
2021-03-23 22:50:18 +00:00
|
|
|
./eth2_json_rest_serialization, ./rest_utils
|
2021-03-17 18:46:45 +00:00
|
|
|
|
|
|
|
logScope: topics = "rest_beaconapi"
|
|
|
|
|
2021-03-29 10:59:39 +00:00
|
|
|
const
|
|
|
|
# https://github.com/ethereum/eth2.0-APIs/blob/master/apis/beacon/states/validator_balances.yaml#L17
|
|
|
|
# https://github.com/ethereum/eth2.0-APIs/blob/master/apis/beacon/states/validators.yaml#L17
|
|
|
|
MaximumValidatorIds = 30
|
|
|
|
|
2021-03-17 18:46:45 +00:00
|
|
|
type
|
2021-03-29 11:18:17 +00:00
|
|
|
RestValidatorTuple = tuple
|
2021-03-17 18:46:45 +00:00
|
|
|
index: ValidatorIndex
|
|
|
|
balance: string
|
|
|
|
status: string
|
|
|
|
validator: Validator
|
|
|
|
|
2021-03-29 11:18:17 +00:00
|
|
|
RestValidatorBalanceTuple = tuple
|
2021-03-17 18:46:45 +00:00
|
|
|
index: ValidatorIndex
|
|
|
|
balance: string
|
|
|
|
|
2021-03-29 11:18:17 +00:00
|
|
|
RestBeaconStatesCommitteesTuple* = tuple
|
2021-03-17 18:46:45 +00:00
|
|
|
index: CommitteeIndex
|
|
|
|
slot: Slot
|
|
|
|
validators: seq[ValidatorIndex]
|
|
|
|
|
2021-03-29 11:18:17 +00:00
|
|
|
RestAttestationsFailureTuple* = tuple
|
2021-03-23 22:50:18 +00:00
|
|
|
index: uint64
|
|
|
|
message: string
|
2021-03-17 18:46:45 +00:00
|
|
|
|
|
|
|
proc validateFilter(filters: seq[ValidatorFilter]): Result[ValidatorFilter,
|
|
|
|
cstring] =
|
|
|
|
var res: ValidatorFilter
|
|
|
|
for item in filters:
|
|
|
|
if res * item != {}:
|
|
|
|
return err("Validator status must be unique")
|
|
|
|
res.incl(item)
|
|
|
|
|
|
|
|
if res == {}:
|
|
|
|
res = {ValidatorFilterKind.PendingInitialized,
|
|
|
|
ValidatorFilterKind.PendingQueued,
|
|
|
|
ValidatorFilterKind.ActiveOngoing,
|
|
|
|
ValidatorFilterKind.ActiveExiting,
|
|
|
|
ValidatorFilterKind.ActiveSlashed,
|
|
|
|
ValidatorFilterKind.ExitedUnslashed,
|
|
|
|
ValidatorFilterKind.ExitedSlashed,
|
|
|
|
ValidatorFilterKind.WithdrawalPossible,
|
|
|
|
ValidatorFilterKind.WithdrawalDone}
|
|
|
|
ok(res)
|
|
|
|
|
|
|
|
proc getStatus(validator: Validator,
|
|
|
|
current_epoch: Epoch): Result[ValidatorFilterKind, cstring] =
|
|
|
|
if validator.activation_epoch > current_epoch:
|
|
|
|
# pending
|
|
|
|
if validator.activation_eligibility_epoch == FAR_FUTURE_EPOCH:
|
|
|
|
ok(ValidatorFilterKind.PendingInitialized)
|
|
|
|
else:
|
|
|
|
# validator.activation_eligibility_epoch < FAR_FUTURE_EPOCH:
|
|
|
|
ok(ValidatorFilterKind.PendingQueued)
|
|
|
|
elif (validator.activation_epoch <= current_epoch) and
|
|
|
|
(current_epoch < validator.exit_epoch):
|
|
|
|
# active
|
|
|
|
if validator.exit_epoch == FAR_FUTURE_EPOCH:
|
|
|
|
ok(ValidatorFilterKind.ActiveOngoing)
|
|
|
|
elif not validator.slashed:
|
|
|
|
# validator.exit_epoch < FAR_FUTURE_EPOCH
|
|
|
|
ok(ValidatorFilterKind.ActiveExiting)
|
|
|
|
else:
|
|
|
|
# validator.exit_epoch < FAR_FUTURE_EPOCH and validator.slashed:
|
|
|
|
ok(ValidatorFilterKind.ActiveSlashed)
|
|
|
|
elif (validator.exit_epoch <= current_epoch) and
|
|
|
|
(current_epoch < validator.withdrawable_epoch):
|
|
|
|
# exited
|
|
|
|
if not validator.slashed:
|
|
|
|
ok(ValidatorFilterKind.ExitedUnslashed)
|
|
|
|
else:
|
|
|
|
# validator.slashed
|
|
|
|
ok(ValidatorFilterKind.ExitedSlashed)
|
|
|
|
elif validator.withdrawable_epoch <= current_epoch:
|
|
|
|
# withdrawal
|
|
|
|
if validator.effective_balance != 0:
|
|
|
|
ok(ValidatorFilterKind.WithdrawalPossible)
|
|
|
|
else:
|
|
|
|
# validator.effective_balance == 0
|
|
|
|
ok(ValidatorFilterKind.WithdrawalDone)
|
|
|
|
else:
|
|
|
|
err("Invalid validator status")
|
|
|
|
|
|
|
|
proc toString*(kind: ValidatorFilterKind): string =
|
|
|
|
case kind
|
|
|
|
of ValidatorFilterKind.PendingInitialized:
|
|
|
|
"pending_initialized"
|
|
|
|
of ValidatorFilterKind.PendingQueued:
|
|
|
|
"pending_queued"
|
|
|
|
of ValidatorFilterKind.ActiveOngoing:
|
|
|
|
"active_ongoing"
|
|
|
|
of ValidatorFilterKind.ActiveExiting:
|
|
|
|
"active_exiting"
|
|
|
|
of ValidatorFilterKind.ActiveSlashed:
|
|
|
|
"active_slashed"
|
|
|
|
of ValidatorFilterKind.ExitedUnslashed:
|
|
|
|
"exited_unslashed"
|
|
|
|
of ValidatorFilterKind.ExitedSlashed:
|
|
|
|
"exited_slashed"
|
|
|
|
of ValidatorFilterKind.WithdrawalPossible:
|
|
|
|
"withdrawal_possible"
|
|
|
|
of ValidatorFilterKind.WithdrawalDone:
|
|
|
|
"withdrawal_done"
|
|
|
|
|
|
|
|
proc installBeaconApiHandlers*(router: var RestRouter, node: BeaconNode) =
|
|
|
|
# https://ethereum.github.io/eth2.0-APIs/#/Beacon/getGenesis
|
|
|
|
router.api(MethodGet, "/api/eth/v1/beacon/genesis") do () -> RestApiResponse:
|
|
|
|
return RestApiResponse.jsonResponse(
|
2021-03-23 22:50:18 +00:00
|
|
|
(
|
|
|
|
genesis_time: node.chainDag.headState.data.data.genesis_time,
|
2021-03-17 18:46:45 +00:00
|
|
|
genesis_validators_root:
|
|
|
|
node.chainDag.headState.data.data.genesis_validators_root,
|
|
|
|
genesis_fork_version: node.runtimePreset.GENESIS_FORK_VERSION
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# https://ethereum.github.io/eth2.0-APIs/#/Beacon/getStateRoot
|
|
|
|
router.api(MethodGet, "/api/eth/v1/beacon/states/{state_id}/root") do (
|
|
|
|
state_id: StateIdent) -> RestApiResponse:
|
2021-03-23 22:50:18 +00:00
|
|
|
let bslot =
|
|
|
|
block:
|
|
|
|
if state_id.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, InvalidStateIdValueError,
|
2021-03-23 22:50:18 +00:00
|
|
|
$state_id.error())
|
|
|
|
let bres = node.getBlockSlot(state_id.get())
|
|
|
|
if bres.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http404, StateNotFoundError,
|
2021-03-23 22:50:18 +00:00
|
|
|
$bres.error())
|
|
|
|
bres.get()
|
|
|
|
node.withStateForBlockSlot(bslot):
|
|
|
|
return RestApiResponse.jsonResponse((root: hashedState().root))
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http500, InternalServerError)
|
2021-03-17 18:46:45 +00:00
|
|
|
|
|
|
|
# https://ethereum.github.io/eth2.0-APIs/#/Beacon/getStateFork
|
|
|
|
router.api(MethodGet, "/api/eth/v1/beacon/states/{state_id}/fork") do (
|
|
|
|
state_id: StateIdent) -> RestApiResponse:
|
2021-03-23 22:50:18 +00:00
|
|
|
let bslot =
|
|
|
|
block:
|
|
|
|
if state_id.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, InvalidStateIdValueError,
|
2021-03-23 22:50:18 +00:00
|
|
|
$state_id.error())
|
|
|
|
let bres = node.getBlockSlot(state_id.get())
|
|
|
|
if bres.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http404, StateNotFoundError,
|
2021-03-23 22:50:18 +00:00
|
|
|
$bres.error())
|
|
|
|
bres.get()
|
|
|
|
node.withStateForBlockSlot(bslot):
|
2021-03-17 18:46:45 +00:00
|
|
|
return RestApiResponse.jsonResponse(
|
2021-03-23 22:50:18 +00:00
|
|
|
(
|
2021-03-17 18:46:45 +00:00
|
|
|
previous_version: state().fork.previous_version,
|
|
|
|
current_version: state().fork.current_version,
|
|
|
|
epoch: state().fork.epoch
|
|
|
|
)
|
|
|
|
)
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http500, InternalServerError)
|
2021-03-17 18:46:45 +00:00
|
|
|
|
|
|
|
# https://ethereum.github.io/eth2.0-APIs/#/Beacon/getStateFinalityCheckpoints
|
|
|
|
router.api(MethodGet,
|
|
|
|
"/api/eth/v1/beacon/states/{state_id}/finality_checkpoints") do (
|
|
|
|
state_id: StateIdent) -> RestApiResponse:
|
2021-03-23 22:50:18 +00:00
|
|
|
let bslot =
|
|
|
|
block:
|
|
|
|
if state_id.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, InvalidStateIdValueError,
|
2021-03-23 22:50:18 +00:00
|
|
|
$state_id.error())
|
|
|
|
let bres = node.getBlockSlot(state_id.get())
|
|
|
|
if bres.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http404, StateNotFoundError,
|
2021-03-23 22:50:18 +00:00
|
|
|
$bres.error())
|
|
|
|
bres.get()
|
|
|
|
node.withStateForBlockSlot(bslot):
|
2021-03-17 18:46:45 +00:00
|
|
|
return RestApiResponse.jsonResponse(
|
2021-03-23 22:50:18 +00:00
|
|
|
(
|
|
|
|
previous_justified: state().previous_justified_checkpoint,
|
|
|
|
current_justified: state().current_justified_checkpoint,
|
|
|
|
finalized: state().finalized_checkpoint
|
2021-03-17 18:46:45 +00:00
|
|
|
)
|
|
|
|
)
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http500, InternalServerError)
|
2021-03-17 18:46:45 +00:00
|
|
|
|
|
|
|
# https://ethereum.github.io/eth2.0-APIs/#/Beacon/getStateValidators
|
|
|
|
router.api(MethodGet, "/api/eth/v1/beacon/states/{state_id}/validators") do (
|
|
|
|
state_id: StateIdent, id: seq[ValidatorIdent],
|
|
|
|
status: seq[ValidatorFilter]) -> RestApiResponse:
|
2021-03-23 22:50:18 +00:00
|
|
|
let bslot =
|
|
|
|
block:
|
|
|
|
if state_id.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, InvalidStateIdValueError,
|
2021-03-23 22:50:18 +00:00
|
|
|
$state_id.error())
|
|
|
|
let bres = node.getBlockSlot(state_id.get())
|
|
|
|
if bres.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http404, StateNotFoundError,
|
2021-03-23 22:50:18 +00:00
|
|
|
$bres.error())
|
|
|
|
bres.get()
|
2021-03-17 18:46:45 +00:00
|
|
|
let validatorIds =
|
|
|
|
block:
|
|
|
|
if id.isErr():
|
|
|
|
return RestApiResponse.jsonError(Http400,
|
2021-04-08 10:49:28 +00:00
|
|
|
InvalidValidatorIdValueError)
|
2021-03-29 10:59:39 +00:00
|
|
|
let ires = id.get()
|
|
|
|
if len(ires) > MaximumValidatorIds:
|
|
|
|
return RestApiResponse.jsonError(Http400,
|
2021-04-08 10:49:28 +00:00
|
|
|
MaximumNumberOfValidatorIdsError)
|
2021-03-29 10:59:39 +00:00
|
|
|
ires
|
2021-03-17 18:46:45 +00:00
|
|
|
|
|
|
|
let validatorsMask =
|
|
|
|
block:
|
|
|
|
if status.isErr():
|
|
|
|
return RestApiResponse.jsonError(Http400,
|
2021-04-08 10:49:28 +00:00
|
|
|
InvalidValidatorStatusValueError)
|
2021-03-17 18:46:45 +00:00
|
|
|
let res = validateFilter(status.get())
|
|
|
|
if res.isErr():
|
|
|
|
return RestApiResponse.jsonError(Http400,
|
2021-04-08 10:49:28 +00:00
|
|
|
InvalidValidatorStatusValueError,
|
2021-03-17 18:46:45 +00:00
|
|
|
$res.error())
|
|
|
|
res.get()
|
|
|
|
|
|
|
|
let (keySet, indexSet) =
|
|
|
|
block:
|
|
|
|
var res1: HashSet[ValidatorPubKey]
|
|
|
|
var res2: HashSet[ValidatorIndex]
|
|
|
|
for item in validatorIds:
|
|
|
|
case item.kind
|
|
|
|
of ValidatorQueryKind.Key:
|
|
|
|
if item.key in res1:
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, UniqueValidatorKeyError)
|
2021-03-17 18:46:45 +00:00
|
|
|
res1.incl(item.key)
|
|
|
|
of ValidatorQueryKind.Index:
|
2021-04-03 00:21:44 +00:00
|
|
|
let vitem =
|
|
|
|
block:
|
|
|
|
let vres = item.index.toValidatorIndex()
|
|
|
|
if vres.isErr():
|
|
|
|
case vres.error()
|
|
|
|
of ValidatorIndexError.TooHighValue:
|
|
|
|
return RestApiResponse.jsonError(Http400,
|
2021-04-08 10:49:28 +00:00
|
|
|
TooHighValidatorIndexValueError)
|
2021-04-03 00:21:44 +00:00
|
|
|
of ValidatorIndexError.UnsupportedValue:
|
|
|
|
return RestApiResponse.jsonError(Http500,
|
2021-04-08 10:49:28 +00:00
|
|
|
UnsupportedValidatorIndexValueError)
|
2021-04-03 00:21:44 +00:00
|
|
|
vres.get()
|
|
|
|
|
|
|
|
if vitem in res2:
|
2021-03-17 18:46:45 +00:00
|
|
|
return RestApiResponse.jsonError(Http400,
|
2021-04-08 10:49:28 +00:00
|
|
|
UniqueValidatorIndexError)
|
2021-04-03 00:21:44 +00:00
|
|
|
res2.incl(vitem)
|
2021-03-17 18:46:45 +00:00
|
|
|
(res1, res2)
|
|
|
|
|
2021-03-23 22:50:18 +00:00
|
|
|
node.withStateForBlockSlot(bslot):
|
2021-03-17 18:46:45 +00:00
|
|
|
let current_epoch = get_current_epoch(node.chainDag.headState.data.data)
|
2021-03-29 11:18:17 +00:00
|
|
|
var res: seq[RestValidatorTuple]
|
2021-03-17 18:46:45 +00:00
|
|
|
for index, validator in state().validators.pairs():
|
2021-04-02 14:10:21 +00:00
|
|
|
let includeFlag =
|
2021-04-08 23:02:30 +00:00
|
|
|
(len(keySet) == 0) and (len(indexSet) == 0) or
|
|
|
|
(len(indexSet) > 0 and (ValidatorIndex(index) in indexSet)) or
|
|
|
|
(len(keySet) > 0 and (validator.pubkey in keySet))
|
2021-03-17 18:46:45 +00:00
|
|
|
let sres = validator.getStatus(current_epoch)
|
|
|
|
if sres.isOk():
|
|
|
|
let vstatus = sres.get()
|
2021-04-02 14:10:21 +00:00
|
|
|
let statusFlag = vstatus in validatorsMask
|
|
|
|
if includeFlag and statusFlag:
|
2021-03-17 18:46:45 +00:00
|
|
|
res.add((
|
|
|
|
index: ValidatorIndex(index),
|
|
|
|
balance: Base10.toString(state().balances[index]),
|
|
|
|
status: toString(vstatus),
|
|
|
|
validator: validator
|
|
|
|
))
|
2021-03-23 22:50:18 +00:00
|
|
|
return RestApiResponse.jsonResponse(res)
|
2021-03-17 18:46:45 +00:00
|
|
|
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http500, InternalServerError)
|
2021-03-17 18:46:45 +00:00
|
|
|
|
|
|
|
# https://ethereum.github.io/eth2.0-APIs/#/Beacon/getStateValidator
|
|
|
|
router.api(MethodGet,
|
|
|
|
"/api/eth/v1/beacon/states/{state_id}/validators/{validator_id}") do (
|
|
|
|
state_id: StateIdent, validator_id: ValidatorIdent) -> RestApiResponse:
|
2021-03-23 22:50:18 +00:00
|
|
|
let bslot =
|
|
|
|
block:
|
|
|
|
if state_id.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, InvalidStateIdValueError,
|
2021-03-23 22:50:18 +00:00
|
|
|
$state_id.error())
|
|
|
|
let bres = node.getBlockSlot(state_id.get())
|
|
|
|
if bres.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http404, StateNotFoundError,
|
2021-03-23 22:50:18 +00:00
|
|
|
$bres.error())
|
|
|
|
bres.get()
|
2021-03-17 18:46:45 +00:00
|
|
|
if validator_id.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, InvalidValidatorIdValueError,
|
|
|
|
$validator_id.error())
|
2021-03-23 22:50:18 +00:00
|
|
|
node.withStateForBlockSlot(bslot):
|
2021-03-17 18:46:45 +00:00
|
|
|
let current_epoch = get_current_epoch(node.chainDag.headState.data.data)
|
|
|
|
let vid = validator_id.get()
|
|
|
|
case vid.kind
|
|
|
|
of ValidatorQueryKind.Key:
|
|
|
|
for index, validator in state().validators.pairs():
|
|
|
|
if validator.pubkey == vid.key:
|
|
|
|
let sres = validator.getStatus(current_epoch)
|
|
|
|
if sres.isOk():
|
|
|
|
return RestApiResponse.jsonResponse(
|
2021-03-23 22:50:18 +00:00
|
|
|
(
|
2021-03-17 18:46:45 +00:00
|
|
|
index: ValidatorIndex(index),
|
|
|
|
balance: Base10.toString(state().balances[index]),
|
|
|
|
status: toString(sres.get()),
|
|
|
|
validator: validator
|
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
return RestApiResponse.jsonError(Http400,
|
2021-04-08 10:49:28 +00:00
|
|
|
ValidatorStatusNotFoundError)
|
|
|
|
return RestApiResponse.jsonError(Http404, ValidatorNotFoundError)
|
2021-03-17 18:46:45 +00:00
|
|
|
of ValidatorQueryKind.Index:
|
2021-04-03 00:21:44 +00:00
|
|
|
let vindex =
|
|
|
|
block:
|
|
|
|
let vres = vid.index.toValidatorIndex()
|
|
|
|
if vres.isErr():
|
|
|
|
case vres.error()
|
|
|
|
of ValidatorIndexError.TooHighValue:
|
|
|
|
return RestApiResponse.jsonError(Http400,
|
2021-04-08 10:49:28 +00:00
|
|
|
TooHighValidatorIndexValueError)
|
2021-04-03 00:21:44 +00:00
|
|
|
of ValidatorIndexError.UnsupportedValue:
|
|
|
|
return RestApiResponse.jsonError(Http500,
|
2021-04-08 10:49:28 +00:00
|
|
|
UnsupportedValidatorIndexValueError)
|
2021-04-03 00:21:44 +00:00
|
|
|
vres.get()
|
|
|
|
|
|
|
|
if uint64(vindex) >= uint64(len(state().validators)):
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http404, ValidatorNotFoundError)
|
2021-04-03 00:21:44 +00:00
|
|
|
let validator = state().validators[vindex]
|
2021-03-17 18:46:45 +00:00
|
|
|
let sres = validator.getStatus(current_epoch)
|
|
|
|
if sres.isOk():
|
|
|
|
return RestApiResponse.jsonResponse(
|
2021-03-23 22:50:18 +00:00
|
|
|
(
|
2021-04-03 00:21:44 +00:00
|
|
|
index: vindex,
|
|
|
|
balance: Base10.toString(state().balances[vindex]),
|
2021-03-17 18:46:45 +00:00
|
|
|
status: toString(sres.get()),
|
|
|
|
validator: validator
|
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
return RestApiResponse.jsonError(Http400,
|
2021-04-08 10:49:28 +00:00
|
|
|
ValidatorStatusNotFoundError)
|
|
|
|
return RestApiResponse.jsonError(Http500, InternalServerError)
|
2021-03-17 18:46:45 +00:00
|
|
|
|
|
|
|
# https://ethereum.github.io/eth2.0-APIs/#/Beacon/getStateValidatorBalances
|
|
|
|
router.api(MethodGet,
|
|
|
|
"/api/eth/v1/beacon/states/{state_id}/validator_balances") do (
|
|
|
|
state_id: StateIdent, id: seq[ValidatorIdent]) -> RestApiResponse:
|
2021-03-23 22:50:18 +00:00
|
|
|
let bslot =
|
|
|
|
block:
|
|
|
|
if state_id.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, InvalidStateIdValueError,
|
2021-03-23 22:50:18 +00:00
|
|
|
$state_id.error())
|
|
|
|
let bres = node.getBlockSlot(state_id.get())
|
|
|
|
if bres.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http404, StateNotFoundError,
|
2021-03-23 22:50:18 +00:00
|
|
|
$bres.error())
|
|
|
|
bres.get()
|
2021-03-17 18:46:45 +00:00
|
|
|
let validatorIds =
|
|
|
|
block:
|
|
|
|
if id.isErr():
|
|
|
|
return RestApiResponse.jsonError(Http400,
|
2021-04-08 10:49:28 +00:00
|
|
|
InvalidValidatorIdValueError)
|
2021-03-29 10:59:39 +00:00
|
|
|
let ires = id.get()
|
|
|
|
if len(ires) > MaximumValidatorIds:
|
|
|
|
return RestApiResponse.jsonError(Http400,
|
2021-04-08 10:49:28 +00:00
|
|
|
MaximumNumberOfValidatorIdsError)
|
2021-03-29 10:59:39 +00:00
|
|
|
ires
|
2021-03-17 18:46:45 +00:00
|
|
|
let (keySet, indexSet) =
|
|
|
|
block:
|
|
|
|
var res1: HashSet[ValidatorPubKey]
|
|
|
|
var res2: HashSet[ValidatorIndex]
|
|
|
|
for item in validatorIds:
|
|
|
|
case item.kind
|
|
|
|
of ValidatorQueryKind.Key:
|
|
|
|
if item.key in res1:
|
|
|
|
return RestApiResponse.jsonError(Http400,
|
2021-04-08 10:49:28 +00:00
|
|
|
UniqueValidatorKeyError)
|
2021-03-17 18:46:45 +00:00
|
|
|
res1.incl(item.key)
|
|
|
|
of ValidatorQueryKind.Index:
|
2021-04-03 00:21:44 +00:00
|
|
|
let vitem =
|
|
|
|
block:
|
|
|
|
let vres = item.index.toValidatorIndex()
|
|
|
|
if vres.isErr():
|
|
|
|
case vres.error()
|
|
|
|
of ValidatorIndexError.TooHighValue:
|
|
|
|
return RestApiResponse.jsonError(Http400,
|
2021-04-08 10:49:28 +00:00
|
|
|
TooHighValidatorIndexValueError)
|
2021-04-03 00:21:44 +00:00
|
|
|
of ValidatorIndexError.UnsupportedValue:
|
|
|
|
return RestApiResponse.jsonError(Http500,
|
2021-04-08 10:49:28 +00:00
|
|
|
UnsupportedValidatorIndexValueError)
|
2021-04-03 00:21:44 +00:00
|
|
|
vres.get()
|
|
|
|
if vitem in res2:
|
2021-03-17 18:46:45 +00:00
|
|
|
return RestApiResponse.jsonError(Http400,
|
2021-04-08 10:49:28 +00:00
|
|
|
UniqueValidatorIndexError)
|
2021-04-03 00:21:44 +00:00
|
|
|
res2.incl(vitem)
|
2021-03-17 18:46:45 +00:00
|
|
|
(res1, res2)
|
2021-03-23 22:50:18 +00:00
|
|
|
node.withStateForBlockSlot(bslot):
|
2021-03-17 18:46:45 +00:00
|
|
|
let current_epoch = get_current_epoch(node.chainDag.headState.data.data)
|
2021-03-29 11:18:17 +00:00
|
|
|
var res: seq[RestValidatorBalanceTuple]
|
2021-03-17 18:46:45 +00:00
|
|
|
for index, validator in state().validators.pairs():
|
2021-04-02 14:10:21 +00:00
|
|
|
let includeFlag =
|
2021-04-08 23:02:30 +00:00
|
|
|
(len(keySet) == 0) and (len(indexSet) == 0) or
|
|
|
|
(len(indexSet) > 0 and (ValidatorIndex(index) in indexSet)) or
|
|
|
|
(len(keySet) > 0 and (validator.pubkey in keySet))
|
2021-03-17 18:46:45 +00:00
|
|
|
let sres = validator.getStatus(current_epoch)
|
|
|
|
if sres.isOk():
|
|
|
|
let vstatus = sres.get()
|
2021-04-02 14:10:21 +00:00
|
|
|
if includeFlag:
|
2021-03-17 18:46:45 +00:00
|
|
|
res.add((
|
|
|
|
index: ValidatorIndex(index),
|
|
|
|
balance: Base10.toString(state().balances[index]),
|
|
|
|
))
|
2021-03-23 22:50:18 +00:00
|
|
|
return RestApiResponse.jsonResponse(res)
|
2021-03-17 18:46:45 +00:00
|
|
|
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http500, InternalServerError)
|
2021-03-17 18:46:45 +00:00
|
|
|
|
|
|
|
# https://ethereum.github.io/eth2.0-APIs/#/Beacon/getEpochCommittees
|
|
|
|
router.api(MethodGet,
|
|
|
|
"/api/eth/v1/beacon/states/{state_id}/committees") do (
|
|
|
|
state_id: StateIdent, epoch: Option[Epoch], index: Option[CommitteeIndex],
|
|
|
|
slot: Option[Slot]) -> RestApiResponse:
|
2021-03-23 22:50:18 +00:00
|
|
|
let bslot =
|
|
|
|
block:
|
|
|
|
if state_id.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, InvalidStateIdValueError,
|
2021-03-23 22:50:18 +00:00
|
|
|
$state_id.error())
|
|
|
|
let bres = node.getBlockSlot(state_id.get())
|
|
|
|
if bres.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http404, StateNotFoundError,
|
2021-03-23 22:50:18 +00:00
|
|
|
$bres.error())
|
|
|
|
bres.get()
|
2021-03-17 18:46:45 +00:00
|
|
|
let vepoch =
|
|
|
|
if epoch.isSome():
|
|
|
|
let repoch = epoch.get()
|
|
|
|
if repoch.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, InvalidEpochValueError,
|
2021-03-17 18:46:45 +00:00
|
|
|
$repoch.error())
|
2021-04-04 09:48:44 +00:00
|
|
|
let res = repoch.get()
|
|
|
|
if res > MaxEpoch:
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, EpochOverflowValueError)
|
2021-04-04 09:48:44 +00:00
|
|
|
some(res)
|
2021-03-17 18:46:45 +00:00
|
|
|
else:
|
|
|
|
none[Epoch]()
|
|
|
|
let vindex =
|
|
|
|
if index.isSome():
|
|
|
|
let rindex = index.get()
|
|
|
|
if rindex.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400,
|
|
|
|
InvalidCommitteeIndexValueError,
|
2021-03-17 18:46:45 +00:00
|
|
|
$rindex.error())
|
|
|
|
some(rindex.get())
|
|
|
|
else:
|
|
|
|
none[CommitteeIndex]()
|
|
|
|
let vslot =
|
|
|
|
if slot.isSome():
|
|
|
|
let rslot = slot.get()
|
|
|
|
if rslot.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, InvalidSlotValueError,
|
2021-03-17 18:46:45 +00:00
|
|
|
$rslot.error())
|
|
|
|
some(rslot.get())
|
|
|
|
else:
|
|
|
|
none[Slot]()
|
2021-03-23 22:50:18 +00:00
|
|
|
node.withStateForBlockSlot(bslot):
|
2021-03-17 18:46:45 +00:00
|
|
|
proc getCommittee(slot: Slot,
|
2021-03-29 11:18:17 +00:00
|
|
|
index: CommitteeIndex): RestBeaconStatesCommitteesTuple =
|
2021-03-17 18:46:45 +00:00
|
|
|
let validators = get_beacon_committee(state, slot, index,
|
|
|
|
cache).mapIt(it)
|
|
|
|
(index: index, slot: slot, validators: validators)
|
|
|
|
|
|
|
|
proc forSlot(slot: Slot, cindex: Option[CommitteeIndex],
|
2021-03-29 11:18:17 +00:00
|
|
|
res: var seq[RestBeaconStatesCommitteesTuple]) =
|
2021-03-17 18:46:45 +00:00
|
|
|
let committees_per_slot =
|
|
|
|
get_committee_count_per_slot(state, Epoch(slot), cache)
|
|
|
|
|
|
|
|
if cindex.isNone:
|
|
|
|
for committee_index in 0'u64 ..< committees_per_slot:
|
|
|
|
res.add(getCommittee(slot, CommitteeIndex(committee_index)))
|
|
|
|
else:
|
|
|
|
let idx = cindex.get()
|
|
|
|
if uint64(idx) < committees_per_slot:
|
|
|
|
res.add(getCommittee(slot, CommitteeIndex(idx)))
|
|
|
|
|
2021-03-29 11:18:17 +00:00
|
|
|
var res: seq[RestBeaconStatesCommitteesTuple]
|
2021-03-17 18:46:45 +00:00
|
|
|
let qepoch =
|
|
|
|
if vepoch.isNone:
|
|
|
|
compute_epoch_at_slot(state().slot)
|
|
|
|
else:
|
|
|
|
vepoch.get()
|
|
|
|
|
|
|
|
if vslot.isNone():
|
|
|
|
for i in 0 ..< SLOTS_PER_EPOCH:
|
|
|
|
forSlot(compute_start_slot_at_epoch(qepoch) + i, vindex, res)
|
|
|
|
else:
|
|
|
|
forSlot(vslot.get(), vindex, res)
|
|
|
|
|
2021-03-23 22:50:18 +00:00
|
|
|
return RestApiResponse.jsonResponse(res)
|
2021-03-17 18:46:45 +00:00
|
|
|
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http500, InternalServerError)
|
2021-03-17 18:46:45 +00:00
|
|
|
|
|
|
|
# https://ethereum.github.io/eth2.0-APIs/#/Beacon/getBlockHeaders
|
|
|
|
router.api(MethodGet, "/api/eth/v1/beacon/headers") do (
|
|
|
|
slot: Option[Slot], parent_root: Option[Eth2Digest]) -> RestApiResponse:
|
2021-04-03 01:19:16 +00:00
|
|
|
# TODO (cheatfate): This call is not complete, because structure
|
2021-03-29 10:59:39 +00:00
|
|
|
# of database do not allow to query blocks by `parent_root`.
|
2021-04-03 01:19:16 +00:00
|
|
|
let qslot =
|
|
|
|
if slot.isSome():
|
|
|
|
let rslot = slot.get()
|
|
|
|
if rslot.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, InvalidSlotValueError,
|
2021-04-03 01:19:16 +00:00
|
|
|
$rslot.error())
|
|
|
|
rslot.get()
|
|
|
|
else:
|
|
|
|
node.chainDag.head.slot
|
|
|
|
|
|
|
|
if parent_root.isSome():
|
|
|
|
let rroot = parent_root.get()
|
|
|
|
if rroot.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, InvalidParentRootValueError,
|
2021-04-03 01:19:16 +00:00
|
|
|
$rroot.error())
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http500, NoImplementationError)
|
2021-04-03 01:19:16 +00:00
|
|
|
|
|
|
|
let bdata =
|
|
|
|
block:
|
|
|
|
let head =
|
|
|
|
block:
|
|
|
|
let res = node.getCurrentHead(qslot)
|
|
|
|
if res.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http404, SlotNotFoundError,
|
2021-04-03 01:19:16 +00:00
|
|
|
$res.error())
|
|
|
|
res.get()
|
|
|
|
let blockSlot = head.atSlot(qslot)
|
|
|
|
if isNil(blockSlot.blck):
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http404, BlockNotFoundError)
|
2021-04-03 01:19:16 +00:00
|
|
|
node.chainDag.get(blockSlot.blck)
|
|
|
|
|
|
|
|
return RestApiResponse.jsonResponse(
|
|
|
|
(
|
|
|
|
root: bdata.data.root,
|
|
|
|
canonical: bdata.refs.isAncestorOf(node.chainDag.head),
|
|
|
|
header: (
|
|
|
|
message: (
|
|
|
|
slot: bdata.data.message.slot,
|
|
|
|
proposer_index: bdata.data.message.proposer_index,
|
|
|
|
parent_root: bdata.data.message.parent_root,
|
|
|
|
state_root: bdata.data.message.state_root,
|
|
|
|
body_root: bdata.data.message.body.hash_tree_root()
|
|
|
|
),
|
|
|
|
signature: bdata.data.signature
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2021-03-17 18:46:45 +00:00
|
|
|
|
|
|
|
# https://ethereum.github.io/eth2.0-APIs/#/Beacon/getBlockHeader
|
|
|
|
router.api(MethodGet, "/api/eth/v1/beacon/headers/{block_id}") do (
|
|
|
|
block_id: BlockIdent) -> RestApiResponse:
|
2021-03-23 22:50:18 +00:00
|
|
|
let bdata =
|
|
|
|
block:
|
|
|
|
if block_id.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, InvalidBlockIdValueError,
|
2021-03-23 22:50:18 +00:00
|
|
|
$block_id.error())
|
|
|
|
let res = node.getBlockDataFromBlockIdent(block_id.get())
|
|
|
|
if res.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http404, BlockNotFoundError)
|
2021-03-23 22:50:18 +00:00
|
|
|
res.get()
|
|
|
|
|
2021-03-17 18:46:45 +00:00
|
|
|
return RestApiResponse.jsonResponse(
|
2021-03-23 22:50:18 +00:00
|
|
|
(
|
|
|
|
root: bdata.data.root,
|
|
|
|
canonical: bdata.refs.isAncestorOf(node.chainDag.head),
|
2021-03-17 18:46:45 +00:00
|
|
|
header: (
|
|
|
|
message: (
|
2021-03-23 22:50:18 +00:00
|
|
|
slot: bdata.data.message.slot,
|
|
|
|
proposer_index: bdata.data.message.proposer_index,
|
|
|
|
parent_root: bdata.data.message.parent_root,
|
|
|
|
state_root: bdata.data.message.state_root,
|
|
|
|
body_root: bdata.data.message.body.hash_tree_root()
|
2021-03-17 18:46:45 +00:00
|
|
|
),
|
2021-03-23 22:50:18 +00:00
|
|
|
signature: bdata.data.signature
|
2021-03-17 18:46:45 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# https://ethereum.github.io/eth2.0-APIs/#/Beacon/publishBlock
|
|
|
|
router.api(MethodPost, "/api/eth/v1/beacon/blocks") do (
|
|
|
|
contentBody: Option[ContentBody]) -> RestApiResponse:
|
2021-03-23 22:50:18 +00:00
|
|
|
let blck =
|
|
|
|
block:
|
|
|
|
if contentBody.isNone():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, EmptyRequestBodyError)
|
2021-03-23 22:50:18 +00:00
|
|
|
let dres = decodeBody(SignedBeaconBlock, contentBody.get())
|
|
|
|
if dres.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, InvalidBlockObjectError,
|
|
|
|
$dres.error())
|
2021-03-23 22:50:18 +00:00
|
|
|
dres.get()
|
|
|
|
let head = node.chainDag.head
|
|
|
|
if not(node.isSynced(head)):
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http503, BeaconNodeInSyncError)
|
2021-03-23 22:50:18 +00:00
|
|
|
|
|
|
|
if head.slot >= blck.message.slot:
|
|
|
|
node.network.broadcast(getBeaconBlocksTopic(node.forkDigest), blck)
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http202, BlockValidationError)
|
2021-03-23 22:50:18 +00:00
|
|
|
else:
|
|
|
|
let res = proposeSignedBlock(node, head, AttachedValidator(), blck)
|
|
|
|
if res == head:
|
|
|
|
node.network.broadcast(getBeaconBlocksTopic(node.forkDigest), blck)
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http202, BlockValidationError)
|
2021-03-23 22:50:18 +00:00
|
|
|
else:
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http200, BlockValidationSuccess)
|
2021-03-17 18:46:45 +00:00
|
|
|
|
|
|
|
# https://ethereum.github.io/eth2.0-APIs/#/Beacon/getBlock
|
|
|
|
router.api(MethodGet, "/api/eth/v1/beacon/blocks/{block_id}") do (
|
|
|
|
block_id: BlockIdent) -> RestApiResponse:
|
2021-03-23 22:50:18 +00:00
|
|
|
let bdata =
|
|
|
|
block:
|
|
|
|
if block_id.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, InvalidBlockIdValueError,
|
2021-03-23 22:50:18 +00:00
|
|
|
$block_id.error())
|
|
|
|
let res = node.getBlockDataFromBlockIdent(block_id.get())
|
|
|
|
if res.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http404, BlockNotFoundError)
|
2021-03-23 22:50:18 +00:00
|
|
|
res.get()
|
|
|
|
return RestApiResponse.jsonResponse(bdata.data)
|
2021-03-17 18:46:45 +00:00
|
|
|
|
|
|
|
# https://ethereum.github.io/eth2.0-APIs/#/Beacon/getBlockRoot
|
|
|
|
router.api(MethodGet, "/api/eth/v1/beacon/blocks/{block_id}/root") do (
|
|
|
|
block_id: BlockIdent) -> RestApiResponse:
|
2021-03-23 22:50:18 +00:00
|
|
|
let bdata =
|
|
|
|
block:
|
|
|
|
if block_id.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, InvalidBlockIdValueError,
|
2021-03-23 22:50:18 +00:00
|
|
|
$block_id.error())
|
|
|
|
let res = node.getBlockDataFromBlockIdent(block_id.get())
|
|
|
|
if res.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http404, BlockNotFoundError)
|
2021-03-23 22:50:18 +00:00
|
|
|
res.get()
|
|
|
|
return RestApiResponse.jsonResponse((root: bdata.data.root))
|
2021-03-17 18:46:45 +00:00
|
|
|
|
|
|
|
# https://ethereum.github.io/eth2.0-APIs/#/Beacon/getBlockAttestations
|
|
|
|
router.api(MethodGet,
|
|
|
|
"/api/eth/v1/beacon/blocks/{block_id}/attestations") do (
|
|
|
|
block_id: BlockIdent) -> RestApiResponse:
|
2021-03-23 22:50:18 +00:00
|
|
|
let bdata =
|
|
|
|
block:
|
|
|
|
if block_id.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, InvalidBlockIdValueError,
|
2021-03-23 22:50:18 +00:00
|
|
|
$block_id.error())
|
|
|
|
let res = node.getBlockDataFromBlockIdent(block_id.get())
|
|
|
|
if res.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http404, BlockNotFoundError)
|
2021-03-23 22:50:18 +00:00
|
|
|
res.get()
|
2021-03-17 18:46:45 +00:00
|
|
|
return RestApiResponse.jsonResponse(
|
2021-03-23 22:50:18 +00:00
|
|
|
bdata.data.message.body.attestations.asSeq()
|
2021-03-17 18:46:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# https://ethereum.github.io/eth2.0-APIs/#/Beacon/getPoolAttestations
|
|
|
|
router.api(MethodGet, "/api/eth/v1/beacon/pool/attestations") do (
|
|
|
|
slot: Option[Slot],
|
|
|
|
committee_index: Option[CommitteeIndex]) -> RestApiResponse:
|
|
|
|
let vindex =
|
|
|
|
if committee_index.isSome():
|
|
|
|
let rindex = committee_index.get()
|
|
|
|
if rindex.isErr():
|
|
|
|
return RestApiResponse.jsonError(Http400,
|
2021-04-08 10:49:28 +00:00
|
|
|
InvalidCommitteeIndexValueError,
|
2021-03-17 18:46:45 +00:00
|
|
|
$rindex.error())
|
|
|
|
some(rindex.get())
|
|
|
|
else:
|
|
|
|
none[CommitteeIndex]()
|
|
|
|
let vslot =
|
|
|
|
if slot.isSome():
|
|
|
|
let rslot = slot.get()
|
|
|
|
if rslot.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, InvalidSlotValueError,
|
2021-03-17 18:46:45 +00:00
|
|
|
$rslot.error())
|
|
|
|
some(rslot.get())
|
|
|
|
else:
|
|
|
|
none[Slot]()
|
|
|
|
var res: seq[Attestation]
|
|
|
|
for item in node.attestationPool[].attestations(vslot, vindex):
|
|
|
|
res.add(item)
|
2021-03-23 22:50:18 +00:00
|
|
|
return RestApiResponse.jsonResponse(res)
|
2021-03-17 18:46:45 +00:00
|
|
|
|
|
|
|
# https://ethereum.github.io/eth2.0-APIs/#/Beacon/submitPoolAttestations
|
|
|
|
router.api(MethodPost, "/api/eth/v1/beacon/pool/attestations") do (
|
|
|
|
contentBody: Option[ContentBody]) -> RestApiResponse:
|
2021-03-23 22:50:18 +00:00
|
|
|
let attestations =
|
|
|
|
block:
|
|
|
|
if contentBody.isNone():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, EmptyRequestBodyError)
|
2021-03-23 22:50:18 +00:00
|
|
|
let dres = decodeBody(seq[Attestation], contentBody.get())
|
|
|
|
if dres.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400,
|
|
|
|
InvalidAttestationObjectError,
|
2021-03-23 22:50:18 +00:00
|
|
|
$dres.error())
|
|
|
|
dres.get()
|
|
|
|
|
2021-03-29 11:18:17 +00:00
|
|
|
var failures: seq[RestAttestationsFailureTuple]
|
2021-03-23 22:50:18 +00:00
|
|
|
for atindex, attestation in attestations.pairs():
|
|
|
|
let wallTime = node.processor.getWallTime()
|
2021-04-03 01:50:47 +00:00
|
|
|
let res = await node.attestationPool.validateAttestation(
|
|
|
|
node.processor.batchCrypto, attestation, wallTime,
|
|
|
|
attestation.data.index, true
|
2021-03-23 22:50:18 +00:00
|
|
|
)
|
|
|
|
if res.isErr():
|
|
|
|
failures.add((index: uint64(atindex), message: $res.error()))
|
|
|
|
else:
|
|
|
|
node.sendAttestation(attestation)
|
|
|
|
|
|
|
|
if len(failures) > 0:
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonErrorList(Http400, AttestationValidationError,
|
2021-03-23 22:50:18 +00:00
|
|
|
failures)
|
|
|
|
else:
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http200, AttestationValidationSuccess)
|
2021-03-17 18:46:45 +00:00
|
|
|
|
|
|
|
# https://ethereum.github.io/eth2.0-APIs/#/Beacon/getPoolAttesterSlashings
|
|
|
|
router.api(MethodGet, "/api/eth/v1/beacon/pool/attester_slashings") do (
|
|
|
|
) -> RestApiResponse:
|
|
|
|
var res: seq[AttesterSlashing]
|
|
|
|
if isNil(node.exitPool):
|
2021-03-23 22:50:18 +00:00
|
|
|
return RestApiResponse.jsonResponse(res)
|
2021-03-17 18:46:45 +00:00
|
|
|
let length = len(node.exitPool.attester_slashings)
|
|
|
|
res = newSeqOfCap[AttesterSlashing](length)
|
|
|
|
for item in node.exitPool.attester_slashings.items():
|
|
|
|
res.add(item)
|
2021-03-23 22:50:18 +00:00
|
|
|
return RestApiResponse.jsonResponse(res)
|
2021-03-17 18:46:45 +00:00
|
|
|
|
|
|
|
# https://ethereum.github.io/eth2.0-APIs/#/Beacon/submitPoolAttesterSlashings
|
|
|
|
router.api(MethodPost, "/api/eth/v1/beacon/pool/attester_slashings") do (
|
|
|
|
contentBody: Option[ContentBody]) -> RestApiResponse:
|
2021-03-23 22:50:18 +00:00
|
|
|
let slashing =
|
|
|
|
block:
|
|
|
|
if contentBody.isNone():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, EmptyRequestBodyError)
|
2021-03-23 22:50:18 +00:00
|
|
|
let dres = decodeBody(AttesterSlashing, contentBody.get())
|
|
|
|
if dres.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400,
|
|
|
|
InvalidAttesterSlashingObjectError,
|
|
|
|
$dres.error())
|
2021-03-23 22:50:18 +00:00
|
|
|
let res = dres.get()
|
|
|
|
let vres = node.exitPool[].validateAttesterSlashing(res)
|
|
|
|
if vres.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400,
|
|
|
|
AttesterSlashingValidationError,
|
|
|
|
$vres.error())
|
2021-03-23 22:50:18 +00:00
|
|
|
res
|
|
|
|
node.sendAttesterSlashing(slashing)
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http200, AttesterSlashingValidationSuccess)
|
2021-03-17 18:46:45 +00:00
|
|
|
|
|
|
|
# https://ethereum.github.io/eth2.0-APIs/#/Beacon/getPoolProposerSlashings
|
|
|
|
router.api(MethodGet, "/api/eth/v1/beacon/pool/proposer_slashings") do (
|
|
|
|
) -> RestApiResponse:
|
|
|
|
var res: seq[ProposerSlashing]
|
|
|
|
if isNil(node.exitPool):
|
2021-03-23 22:50:18 +00:00
|
|
|
return RestApiResponse.jsonResponse(res)
|
2021-03-17 18:46:45 +00:00
|
|
|
let length = len(node.exitPool.proposer_slashings)
|
|
|
|
res = newSeqOfCap[ProposerSlashing](length)
|
|
|
|
for item in node.exitPool.proposer_slashings.items():
|
|
|
|
res.add(item)
|
2021-03-23 22:50:18 +00:00
|
|
|
return RestApiResponse.jsonResponse(res)
|
2021-03-17 18:46:45 +00:00
|
|
|
|
|
|
|
# https://ethereum.github.io/eth2.0-APIs/#/Beacon/submitPoolProposerSlashings
|
|
|
|
router.api(MethodPost, "/api/eth/v1/beacon/pool/proposer_slashings") do (
|
|
|
|
contentBody: Option[ContentBody]) -> RestApiResponse:
|
2021-03-23 22:50:18 +00:00
|
|
|
let slashing =
|
|
|
|
block:
|
|
|
|
if contentBody.isNone():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, EmptyRequestBodyError)
|
2021-03-23 22:50:18 +00:00
|
|
|
let dres = decodeBody(ProposerSlashing, contentBody.get())
|
|
|
|
if dres.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400,
|
|
|
|
InvalidProposerSlashingObjectError,
|
|
|
|
$dres.error())
|
2021-03-23 22:50:18 +00:00
|
|
|
let res = dres.get()
|
|
|
|
let vres = node.exitPool[].validateProposerSlashing(res)
|
|
|
|
if vres.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400,
|
|
|
|
ProposerSlashingValidationError,
|
|
|
|
$vres.error())
|
2021-03-23 22:50:18 +00:00
|
|
|
res
|
|
|
|
node.sendProposerSlashing(slashing)
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http200, ProposerSlashingValidationSuccess)
|
2021-03-17 18:46:45 +00:00
|
|
|
|
|
|
|
# https://ethereum.github.io/eth2.0-APIs/#/Beacon/getPoolVoluntaryExits
|
|
|
|
router.api(MethodGet, "/api/eth/v1/beacon/pool/voluntary_exits") do (
|
|
|
|
) -> RestApiResponse:
|
|
|
|
var res: seq[SignedVoluntaryExit]
|
|
|
|
if isNil(node.exitPool):
|
2021-03-23 22:50:18 +00:00
|
|
|
return RestApiResponse.jsonResponse(res)
|
2021-03-17 18:46:45 +00:00
|
|
|
let length = len(node.exitPool.voluntary_exits)
|
|
|
|
res = newSeqOfCap[SignedVoluntaryExit](length)
|
|
|
|
for item in node.exitPool.voluntary_exits.items():
|
|
|
|
res.add(item)
|
2021-03-23 22:50:18 +00:00
|
|
|
return RestApiResponse.jsonResponse(res)
|
2021-03-17 18:46:45 +00:00
|
|
|
|
|
|
|
# https://ethereum.github.io/eth2.0-APIs/#/Beacon/submitPoolVoluntaryExit
|
|
|
|
router.api(MethodPost, "/api/eth/v1/beacon/pool/voluntary_exits") do (
|
|
|
|
contentBody: Option[ContentBody]) -> RestApiResponse:
|
2021-03-23 22:50:18 +00:00
|
|
|
let exit =
|
|
|
|
block:
|
|
|
|
if contentBody.isNone():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, EmptyRequestBodyError)
|
2021-03-23 22:50:18 +00:00
|
|
|
let dres = decodeBody(SignedVoluntaryExit, contentBody.get())
|
|
|
|
if dres.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400,
|
|
|
|
InvalidVoluntaryExitObjectError,
|
|
|
|
$dres.error())
|
2021-03-23 22:50:18 +00:00
|
|
|
let res = dres.get()
|
|
|
|
let vres = node.exitPool[].validateVoluntaryExit(res)
|
|
|
|
if vres.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400,
|
|
|
|
VoluntaryExitValidationError,
|
|
|
|
$vres.error())
|
2021-03-23 22:50:18 +00:00
|
|
|
res
|
|
|
|
node.sendVoluntaryExit(exit)
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http200, VoluntaryExitValidationSuccess)
|
2021-04-13 10:19:31 +00:00
|
|
|
|
|
|
|
router.redirect(
|
|
|
|
MethodGet,
|
|
|
|
"/eth/v1/beacon/genesis",
|
|
|
|
"/api/eth/v1/beacon/genesis"
|
|
|
|
)
|
|
|
|
router.redirect(
|
|
|
|
MethodGet,
|
|
|
|
"/eth/v1/beacon/states/{state_id}/root",
|
|
|
|
"/api/eth/v1/beacon/states/{state_id}/root"
|
|
|
|
)
|
|
|
|
router.redirect(
|
|
|
|
MethodGet,
|
|
|
|
"/eth/v1/beacon/states/{state_id}/fork",
|
|
|
|
"/api/eth/v1/beacon/states/{state_id}/fork"
|
|
|
|
)
|
|
|
|
router.redirect(
|
|
|
|
MethodGet,
|
|
|
|
"/eth/v1/beacon/states/{state_id}/finality_checkpoints",
|
|
|
|
"/api/eth/v1/beacon/states/{state_id}/finality_checkpoints"
|
|
|
|
)
|
|
|
|
router.redirect(
|
|
|
|
MethodGet,
|
|
|
|
"/eth/v1/beacon/states/{state_id}/validators",
|
|
|
|
"/api/eth/v1/beacon/states/{state_id}/validators"
|
|
|
|
)
|
|
|
|
router.redirect(
|
|
|
|
MethodGet,
|
|
|
|
"/eth/v1/beacon/states/{state_id}/validators/{validator_id}",
|
|
|
|
"/api/eth/v1/beacon/states/{state_id}/validators/{validator_id}"
|
|
|
|
)
|
|
|
|
router.redirect(
|
|
|
|
MethodGet,
|
|
|
|
"/eth/v1/beacon/states/{state_id}/validator_balances",
|
|
|
|
"/api/eth/v1/beacon/states/{state_id}/validator_balances"
|
|
|
|
)
|
|
|
|
router.redirect(
|
|
|
|
MethodGet,
|
|
|
|
"/eth/v1/beacon/states/{state_id}/committees",
|
|
|
|
"/api/eth/v1/beacon/states/{state_id}/committees"
|
|
|
|
)
|
|
|
|
router.redirect(
|
|
|
|
MethodGet,
|
|
|
|
"/eth/v1/beacon/headers",
|
|
|
|
"/api/eth/v1/beacon/headers"
|
|
|
|
)
|
|
|
|
router.redirect(
|
|
|
|
MethodGet,
|
|
|
|
"/eth/v1/beacon/headers/{block_id}",
|
|
|
|
"/api/eth/v1/beacon/headers/{block_id}"
|
|
|
|
)
|
|
|
|
router.redirect(
|
|
|
|
MethodPost,
|
|
|
|
"/eth/v1/beacon/blocks",
|
|
|
|
"/api/eth/v1/beacon/blocks"
|
|
|
|
)
|
|
|
|
router.redirect(
|
|
|
|
MethodGet,
|
|
|
|
"/eth/v1/beacon/blocks/{block_id}",
|
|
|
|
"/api/eth/v1/beacon/blocks/{block_id}"
|
|
|
|
)
|
|
|
|
router.redirect(
|
|
|
|
MethodGet,
|
|
|
|
"/eth/v1/beacon/blocks/{block_id}/root",
|
|
|
|
"/api/eth/v1/beacon/blocks/{block_id}/root"
|
|
|
|
)
|
|
|
|
router.redirect(
|
|
|
|
MethodGet,
|
|
|
|
"/eth/v1/beacon/blocks/{block_id}/attestations",
|
|
|
|
"/api/eth/v1/beacon/blocks/{block_id}/attestations"
|
|
|
|
)
|
|
|
|
router.redirect(
|
|
|
|
MethodGet,
|
|
|
|
"/eth/v1/beacon/pool/attestations",
|
|
|
|
"/api/eth/v1/beacon/pool/attestations"
|
|
|
|
)
|
|
|
|
router.redirect(
|
|
|
|
MethodPost,
|
|
|
|
"/eth/v1/beacon/pool/attester_slashings",
|
|
|
|
"/api/eth/v1/beacon/pool/attester_slashings"
|
|
|
|
)
|
|
|
|
router.redirect(
|
|
|
|
MethodGet,
|
|
|
|
"/eth/v1/beacon/pool/attester_slashings",
|
|
|
|
"/api/eth/v1/beacon/pool/attester_slashings"
|
|
|
|
)
|
|
|
|
router.redirect(
|
|
|
|
MethodPost,
|
|
|
|
"/eth/v1/beacon/pool/proposer_slashings",
|
|
|
|
"/api/eth/v1/beacon/pool/proposer_slashings"
|
|
|
|
)
|
|
|
|
router.redirect(
|
|
|
|
MethodGet,
|
|
|
|
"/eth/v1/beacon/pool/proposer_slashings",
|
|
|
|
"/api/eth/v1/beacon/pool/proposer_slashings"
|
|
|
|
)
|
|
|
|
router.redirect(
|
|
|
|
MethodPost,
|
|
|
|
"/eth/v1/beacon/pool/voluntary_exits",
|
|
|
|
"/api/eth/v1/beacon/pool/voluntary_exits"
|
|
|
|
)
|
|
|
|
router.redirect(
|
|
|
|
MethodGet,
|
|
|
|
"/eth/v1/beacon/pool/voluntary_exits",
|
|
|
|
"/api/eth/v1/beacon/pool/voluntary_exits"
|
|
|
|
)
|