2022-06-09 08:50:36 +00:00
|
|
|
# Copyright (c) 2018-2022 Status Research & Development GmbH
|
2021-03-23 22:50:18 +00:00
|
|
|
# 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.
|
2022-06-09 08:50:36 +00:00
|
|
|
|
|
|
|
import std/[typetraits, sets]
|
2022-01-08 20:06:34 +00:00
|
|
|
import stew/[results, base10], chronicles
|
2022-01-05 14:49:10 +00:00
|
|
|
import ".."/[beacon_chain_db, beacon_node],
|
2021-09-23 22:13:25 +00:00
|
|
|
".."/networking/eth2_network,
|
|
|
|
".."/consensus_object_pools/[blockchain_dag, spec_cache,
|
|
|
|
attestation_pool, sync_committee_msg_pool],
|
|
|
|
".."/validators/validator_duties,
|
2021-11-07 21:03:23 +00:00
|
|
|
".."/spec/[beaconstate, forks, network],
|
2022-01-05 14:49:10 +00:00
|
|
|
".."/spec/datatypes/[phase0, altair],
|
|
|
|
"."/[rest_utils, state_ttl_cache]
|
2021-03-23 22:50:18 +00:00
|
|
|
|
2021-10-27 12:01:11 +00:00
|
|
|
export rest_utils
|
|
|
|
|
2021-03-23 22:50:18 +00:00
|
|
|
logScope: topics = "rest_validatorapi"
|
|
|
|
|
|
|
|
proc installValidatorApiHandlers*(router: var RestRouter, node: BeaconNode) =
|
2021-08-23 10:41:48 +00:00
|
|
|
# https://ethereum.github.io/beacon-APIs/#/Validator/getAttesterDuties
|
2022-01-06 07:38:40 +00:00
|
|
|
router.api(MethodPost, "/eth/v1/validator/duties/attester/{epoch}") do (
|
2021-03-23 22:50:18 +00:00
|
|
|
epoch: Epoch, contentBody: Option[ContentBody]) -> RestApiResponse:
|
|
|
|
let indexList =
|
|
|
|
block:
|
|
|
|
if contentBody.isNone():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, EmptyRequestBodyError)
|
2021-04-06 08:00:26 +00:00
|
|
|
let dres = decodeBody(seq[RestValidatorIndex], contentBody.get())
|
2021-03-23 22:50:18 +00:00
|
|
|
if dres.isErr():
|
2021-04-08 14:34:05 +00:00
|
|
|
return RestApiResponse.jsonError(Http400,
|
|
|
|
InvalidValidatorIndexValueError,
|
|
|
|
$dres.error())
|
2021-10-18 08:54:20 +00:00
|
|
|
var res: HashSet[ValidatorIndex]
|
2021-04-06 08:00:26 +00:00
|
|
|
let items = dres.get()
|
|
|
|
for item in items:
|
|
|
|
let vres = item.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-06 08:00:26 +00:00
|
|
|
of ValidatorIndexError.UnsupportedValue:
|
|
|
|
return RestApiResponse.jsonError(Http500,
|
2021-04-08 10:49:28 +00:00
|
|
|
UnsupportedValidatorIndexValueError)
|
2021-10-18 08:54:20 +00:00
|
|
|
res.incl(vres.get())
|
2021-04-06 08:00:26 +00:00
|
|
|
if len(res) == 0:
|
2021-04-08 14:34:05 +00:00
|
|
|
return RestApiResponse.jsonError(Http400,
|
|
|
|
EmptyValidatorIndexArrayError)
|
2021-04-06 08:00:26 +00:00
|
|
|
res
|
2021-03-23 22:50:18 +00:00
|
|
|
let qepoch =
|
|
|
|
block:
|
|
|
|
if epoch.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, InvalidEpochValueError,
|
2021-03-23 22:50:18 +00:00
|
|
|
$epoch.error())
|
2022-01-08 20:06:34 +00:00
|
|
|
let
|
|
|
|
res = epoch.get()
|
|
|
|
wallTime = node.beaconClock.now() + MAXIMUM_GOSSIP_CLOCK_DISPARITY
|
|
|
|
wallEpoch = wallTime.slotOrZero().epoch
|
|
|
|
if res > wallEpoch + 1:
|
|
|
|
return RestApiResponse.jsonError(Http400, InvalidEpochValueError,
|
|
|
|
"Cannot request duties past next epoch")
|
2021-03-23 22:50:18 +00:00
|
|
|
res
|
|
|
|
let qhead =
|
|
|
|
block:
|
2022-03-23 11:42:16 +00:00
|
|
|
let res = node.getSyncedHead(qepoch)
|
2021-03-23 22:50:18 +00:00
|
|
|
if res.isErr():
|
2021-04-27 20:46:24 +00:00
|
|
|
return RestApiResponse.jsonError(Http503, BeaconNodeInSyncError)
|
2021-03-23 22:50:18 +00:00
|
|
|
res.get()
|
2022-08-18 18:07:01 +00:00
|
|
|
let shufflingRef = node.dag.getShufflingRef(qhead, qepoch, true).valueOr:
|
2022-03-15 08:24:55 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, PrunedStateError)
|
|
|
|
|
2021-03-23 22:50:18 +00:00
|
|
|
let duties =
|
|
|
|
block:
|
2021-05-20 17:56:12 +00:00
|
|
|
var res: seq[RestAttesterDuty]
|
2022-01-05 18:38:04 +00:00
|
|
|
|
2022-01-08 23:28:49 +00:00
|
|
|
let
|
2022-08-18 18:07:01 +00:00
|
|
|
committees_per_slot = get_committee_count_per_slot(shufflingRef)
|
2022-01-08 23:28:49 +00:00
|
|
|
for committee_index in get_committee_indices(committees_per_slot):
|
2022-01-11 10:01:54 +00:00
|
|
|
for slot in qepoch.slots():
|
2022-08-18 18:07:01 +00:00
|
|
|
let
|
|
|
|
committee =
|
|
|
|
get_beacon_committee(shufflingRef, slot, committee_index)
|
2022-01-08 23:28:49 +00:00
|
|
|
for index_in_committee, validator_index in committee:
|
2021-06-10 07:37:02 +00:00
|
|
|
if validator_index in indexList:
|
2022-08-18 18:07:01 +00:00
|
|
|
let validator_key = node.dag.validatorKey(validator_index)
|
2021-06-10 07:37:02 +00:00
|
|
|
if validator_key.isSome():
|
2021-03-23 22:50:18 +00:00
|
|
|
res.add(
|
2021-05-20 17:56:12 +00:00
|
|
|
RestAttesterDuty(
|
2021-06-10 07:37:02 +00:00
|
|
|
pubkey: validator_key.get().toPubKey(),
|
2021-03-23 22:50:18 +00:00
|
|
|
validator_index: validator_index,
|
2022-01-08 23:28:49 +00:00
|
|
|
committee_index: committee_index,
|
|
|
|
committee_length: lenu64(committee),
|
2021-03-23 22:50:18 +00:00
|
|
|
committees_at_slot: committees_per_slot,
|
2022-01-08 23:28:49 +00:00
|
|
|
validator_committee_index: uint64(index_in_committee),
|
2021-03-23 22:50:18 +00:00
|
|
|
slot: slot
|
|
|
|
)
|
|
|
|
)
|
|
|
|
res
|
2022-06-20 05:53:39 +00:00
|
|
|
|
2022-07-04 20:35:33 +00:00
|
|
|
# getSyncedHead() implies non-optimistic node.
|
2022-06-20 05:53:39 +00:00
|
|
|
let optimistic =
|
2022-06-28 10:21:16 +00:00
|
|
|
if node.currentSlot().epoch() >= node.dag.cfg.BELLATRIX_FORK_EPOCH:
|
|
|
|
some(false)
|
|
|
|
else:
|
|
|
|
none[bool]()
|
2022-06-20 05:53:39 +00:00
|
|
|
|
2022-03-15 08:24:55 +00:00
|
|
|
return RestApiResponse.jsonResponseWRoot(
|
2022-08-18 18:07:01 +00:00
|
|
|
duties, shufflingRef.attester_dependent_root, optimistic)
|
2021-03-23 22:50:18 +00:00
|
|
|
|
2021-08-23 10:41:48 +00:00
|
|
|
# https://ethereum.github.io/beacon-APIs/#/Validator/getProposerDuties
|
2022-01-06 07:38:40 +00:00
|
|
|
router.api(MethodGet, "/eth/v1/validator/duties/proposer/{epoch}") do (
|
2021-03-23 22:50:18 +00:00
|
|
|
epoch: Epoch) -> RestApiResponse:
|
|
|
|
let qepoch =
|
|
|
|
block:
|
|
|
|
if epoch.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, InvalidEpochValueError,
|
2021-03-23 22:50:18 +00:00
|
|
|
$epoch.error())
|
2022-01-08 20:06:34 +00:00
|
|
|
let
|
|
|
|
res = epoch.get()
|
|
|
|
wallTime = node.beaconClock.now() + MAXIMUM_GOSSIP_CLOCK_DISPARITY
|
|
|
|
wallEpoch = wallTime.slotOrZero().epoch
|
|
|
|
if res > wallEpoch + 1:
|
|
|
|
return RestApiResponse.jsonError(Http400, InvalidEpochValueError,
|
|
|
|
"Cannot request duties past next epoch")
|
2021-03-23 22:50:18 +00:00
|
|
|
res
|
|
|
|
let qhead =
|
|
|
|
block:
|
2022-03-23 11:42:16 +00:00
|
|
|
let res = node.getSyncedHead(qepoch)
|
2021-03-23 22:50:18 +00:00
|
|
|
if res.isErr():
|
2021-04-27 20:46:24 +00:00
|
|
|
return RestApiResponse.jsonError(Http503, BeaconNodeInSyncError)
|
2021-03-23 22:50:18 +00:00
|
|
|
res.get()
|
2022-03-15 08:24:55 +00:00
|
|
|
let epochRef = node.dag.getEpochRef(qhead, qepoch, true).valueOr:
|
2022-09-27 16:56:08 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, PrunedStateError, $error)
|
2022-03-15 08:24:55 +00:00
|
|
|
|
2021-03-23 22:50:18 +00:00
|
|
|
let duties =
|
|
|
|
block:
|
2021-05-20 17:56:12 +00:00
|
|
|
var res: seq[RestProposerDuty]
|
2021-06-01 11:13:40 +00:00
|
|
|
for i, bp in epochRef.beacon_proposers:
|
2021-06-10 07:37:02 +00:00
|
|
|
if i == 0 and qepoch == 0:
|
|
|
|
# Fix for https://github.com/status-im/nimbus-eth2/issues/2488
|
|
|
|
# Slot(0) at Epoch(0) do not have a proposer.
|
|
|
|
continue
|
|
|
|
|
2021-06-01 11:13:40 +00:00
|
|
|
if bp.isSome():
|
2021-03-23 22:50:18 +00:00
|
|
|
res.add(
|
2021-05-20 17:56:12 +00:00
|
|
|
RestProposerDuty(
|
2022-08-18 18:07:01 +00:00
|
|
|
pubkey: node.dag.validatorKey(bp.get()).get().toPubKey(),
|
2021-06-01 11:13:40 +00:00
|
|
|
validator_index: bp.get(),
|
2022-01-11 10:01:54 +00:00
|
|
|
slot: qepoch.start_slot() + i
|
2021-03-23 22:50:18 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
res
|
2022-06-20 05:53:39 +00:00
|
|
|
|
2022-07-04 20:35:33 +00:00
|
|
|
# getSyncedHead() implies non-optimistic node.
|
2022-06-20 05:53:39 +00:00
|
|
|
let optimistic =
|
2022-06-28 10:21:16 +00:00
|
|
|
if node.currentSlot().epoch() >= node.dag.cfg.BELLATRIX_FORK_EPOCH:
|
|
|
|
some(false)
|
|
|
|
else:
|
|
|
|
none[bool]()
|
2022-06-20 05:53:39 +00:00
|
|
|
|
2022-03-15 08:24:55 +00:00
|
|
|
return RestApiResponse.jsonResponseWRoot(
|
2022-06-20 05:53:39 +00:00
|
|
|
duties, epochRef.proposer_dependent_root, optimistic)
|
2021-03-23 22:50:18 +00:00
|
|
|
|
2022-01-06 07:38:40 +00:00
|
|
|
router.api(MethodPost, "/eth/v1/validator/duties/sync/{epoch}") do (
|
2021-10-14 10:38:38 +00:00
|
|
|
epoch: Epoch, contentBody: Option[ContentBody]) -> RestApiResponse:
|
|
|
|
let indexList =
|
|
|
|
block:
|
|
|
|
if contentBody.isNone():
|
|
|
|
return RestApiResponse.jsonError(Http400, EmptyRequestBodyError)
|
|
|
|
let dres = decodeBody(seq[RestValidatorIndex], contentBody.get())
|
|
|
|
if dres.isErr():
|
|
|
|
return RestApiResponse.jsonError(Http400,
|
|
|
|
InvalidValidatorIndexValueError,
|
|
|
|
$dres.error())
|
|
|
|
var res: seq[ValidatorIndex]
|
|
|
|
let items = dres.get()
|
|
|
|
for item in items:
|
|
|
|
let vres = item.toValidatorIndex()
|
|
|
|
if vres.isErr():
|
|
|
|
case vres.error()
|
|
|
|
of ValidatorIndexError.TooHighValue:
|
|
|
|
return RestApiResponse.jsonError(Http400,
|
|
|
|
TooHighValidatorIndexValueError)
|
|
|
|
of ValidatorIndexError.UnsupportedValue:
|
|
|
|
return RestApiResponse.jsonError(Http500,
|
|
|
|
UnsupportedValidatorIndexValueError)
|
|
|
|
res.add(vres.get())
|
|
|
|
if len(res) == 0:
|
|
|
|
return RestApiResponse.jsonError(Http400,
|
|
|
|
EmptyValidatorIndexArrayError)
|
|
|
|
res
|
|
|
|
let qepoch =
|
|
|
|
block:
|
|
|
|
if epoch.isErr():
|
|
|
|
return RestApiResponse.jsonError(Http400, InvalidEpochValueError,
|
|
|
|
$epoch.error())
|
|
|
|
let res = epoch.get()
|
|
|
|
if res > MaxEpoch:
|
|
|
|
return RestApiResponse.jsonError(Http400, EpochOverflowValueError)
|
|
|
|
|
|
|
|
res
|
|
|
|
|
2021-11-30 01:14:31 +00:00
|
|
|
# We use a local proc in order to:
|
|
|
|
# * avoid code duplication
|
|
|
|
# * reduce code bloat from `withState`
|
|
|
|
proc produceResponse(requestedValidatorIndices: openArray[ValidatorIndex],
|
|
|
|
syncCommittee: openArray[ValidatorPubKey],
|
|
|
|
stateValidators: seq[Validator]
|
|
|
|
): seq[RestSyncCommitteeDuty] {.nimcall.} =
|
|
|
|
result = newSeqOfCap[RestSyncCommitteeDuty](len(requestedValidatorIndices))
|
|
|
|
for requestedValidatorIdx in requestedValidatorIndices:
|
|
|
|
if requestedValidatorIdx.uint64 >= stateValidators.lenu64:
|
|
|
|
# If the requested validator index was not valid within this old
|
|
|
|
# state, it's not possible that it will sit on the sync committee.
|
|
|
|
# Since this API must omit results for validators that don't have
|
|
|
|
# duties, we can simply ingnore this requested index.
|
|
|
|
# (we won't bother to validate it agains a more recent state).
|
|
|
|
continue
|
|
|
|
|
|
|
|
let requestedValidatorPubkey =
|
|
|
|
stateValidators[requestedValidatorIdx].pubkey
|
|
|
|
|
|
|
|
var indicesInSyncCommittee = newSeq[IndexInSyncCommittee]()
|
|
|
|
for idx, syncCommitteeMemberPubkey in syncCommittee:
|
|
|
|
if syncCommitteeMemberPubkey == requestedValidatorPubkey:
|
|
|
|
indicesInSyncCommittee.add(IndexInSyncCommittee idx)
|
|
|
|
|
|
|
|
if indicesInSyncCommittee.len > 0:
|
|
|
|
result.add RestSyncCommitteeDuty(
|
|
|
|
pubkey: requestedValidatorPubkey,
|
|
|
|
validator_index: requestedValidatorIdx,
|
|
|
|
validator_sync_committee_indices: indicesInSyncCommittee)
|
|
|
|
|
|
|
|
template emptyResponse: auto =
|
|
|
|
newSeq[RestSyncCommitteeDuty]()
|
|
|
|
|
|
|
|
# We check the head state first in order to avoid costly replays
|
|
|
|
# if possible:
|
|
|
|
let
|
|
|
|
qSyncPeriod = sync_committee_period(qepoch)
|
|
|
|
headEpoch = node.dag.head.slot.epoch
|
|
|
|
headSyncPeriod = sync_committee_period(headEpoch)
|
|
|
|
|
|
|
|
if qSyncPeriod == headSyncPeriod:
|
2022-06-20 05:53:39 +00:00
|
|
|
let optimistic = node.getStateOptimistic(node.dag.headState)
|
2022-03-16 07:20:40 +00:00
|
|
|
let res = withState(node.dag.headState):
|
2021-11-30 01:14:31 +00:00
|
|
|
when stateFork >= BeaconStateFork.Altair:
|
|
|
|
produceResponse(indexList,
|
2022-09-13 11:53:12 +00:00
|
|
|
forkyState.data.current_sync_committee.pubkeys.data,
|
|
|
|
forkyState.data.validators.asSeq)
|
2021-11-30 01:14:31 +00:00
|
|
|
else:
|
|
|
|
emptyResponse()
|
2022-06-20 05:53:39 +00:00
|
|
|
return RestApiResponse.jsonResponseWOpt(res, optimistic)
|
2021-11-30 01:14:31 +00:00
|
|
|
elif qSyncPeriod == (headSyncPeriod + 1):
|
2022-06-20 05:53:39 +00:00
|
|
|
let optimistic = node.getStateOptimistic(node.dag.headState)
|
2022-03-16 07:20:40 +00:00
|
|
|
let res = withState(node.dag.headState):
|
2021-11-30 01:14:31 +00:00
|
|
|
when stateFork >= BeaconStateFork.Altair:
|
|
|
|
produceResponse(indexList,
|
2022-09-13 11:53:12 +00:00
|
|
|
forkyState.data.next_sync_committee.pubkeys.data,
|
|
|
|
forkyState.data.validators.asSeq)
|
2021-11-30 01:14:31 +00:00
|
|
|
else:
|
|
|
|
emptyResponse()
|
2022-06-20 05:53:39 +00:00
|
|
|
return RestApiResponse.jsonResponseWOpt(res, optimistic)
|
2021-11-30 01:14:31 +00:00
|
|
|
elif qSyncPeriod > headSyncPeriod:
|
|
|
|
# The requested epoch may still be too far in the future.
|
|
|
|
if not(node.isSynced(node.dag.head)):
|
|
|
|
return RestApiResponse.jsonError(Http503, BeaconNodeInSyncError)
|
|
|
|
else:
|
|
|
|
return RestApiResponse.jsonError(Http400, EpochFromFutureError)
|
|
|
|
else:
|
|
|
|
# The slot at the start of the sync committee period is likely to have a
|
|
|
|
# state snapshot in the database, so we can restore the state relatively
|
|
|
|
# cheaply:
|
|
|
|
let earliestSlotInQSyncPeriod =
|
|
|
|
Slot(qSyncPeriod * SLOTS_PER_SYNC_COMMITTEE_PERIOD)
|
|
|
|
|
|
|
|
# TODO
|
|
|
|
# The DAG can offer a short-cut for getting just the information we need
|
|
|
|
# in order to compute the sync committee for the epoch. See the following
|
|
|
|
# discussion for more details:
|
|
|
|
# https://github.com/status-im/nimbus-eth2/pull/3133#pullrequestreview-817184693
|
Prune `BlockRef` on finalization (#3513)
Up til now, the block dag has been using `BlockRef`, a structure adapted
for a full DAG, to represent all of chain history. This is a correct and
simple design, but does not exploit the linearity of the chain once
parts of it finalize.
By pruning the in-memory `BlockRef` structure at finalization, we save,
at the time of writing, a cool ~250mb (or 25%:ish) chunk of memory
landing us at a steady state of ~750mb normal memory usage for a
validating node.
Above all though, we prevent memory usage from growing proportionally
with the length of the chain, something that would not be sustainable
over time - instead, the steady state memory usage is roughly
determined by the validator set size which grows much more slowly. With
these changes, the core should remain sustainable memory-wise post-merge
all the way to withdrawals (when the validator set is expected to grow).
In-memory indices are still used for the "hot" unfinalized portion of
the chain - this ensure that consensus performance remains unchanged.
What changes is that for historical access, we use a db-based linear
slot index which is cache-and-disk-friendly, keeping the cost for
accessing historical data at a similar level as before, achieving the
savings at no percievable cost to functionality or performance.
A nice collateral benefit is the almost-instant startup since we no
longer load any large indicies at dag init.
The cost of this functionality instead can be found in the complexity of
having to deal with two ways of traversing the chain - by `BlockRef` and
by slot.
* use `BlockId` instead of `BlockRef` where finalized / historical data
may be required
* simplify clearance pre-advancement
* remove dag.finalizedBlocks (~50:ish mb)
* remove `getBlockAtSlot` - use `getBlockIdAtSlot` instead
* `parent` and `atSlot` for `BlockId` now require a `ChainDAGRef`
instance, unlike `BlockRef` traversal
* prune `BlockRef` parents on finality (~200:ish mb)
* speed up ChainDAG init by not loading finalized history index
* mess up light client server error handling - this need revisiting :)
2022-03-17 17:42:56 +00:00
|
|
|
let bsi = node.dag.getBlockIdAtSlot(earliestSlotInQSyncPeriod).valueOr:
|
2022-01-05 18:38:04 +00:00
|
|
|
return RestApiResponse.jsonError(Http404, StateNotFoundError)
|
|
|
|
|
Prune `BlockRef` on finalization (#3513)
Up til now, the block dag has been using `BlockRef`, a structure adapted
for a full DAG, to represent all of chain history. This is a correct and
simple design, but does not exploit the linearity of the chain once
parts of it finalize.
By pruning the in-memory `BlockRef` structure at finalization, we save,
at the time of writing, a cool ~250mb (or 25%:ish) chunk of memory
landing us at a steady state of ~750mb normal memory usage for a
validating node.
Above all though, we prevent memory usage from growing proportionally
with the length of the chain, something that would not be sustainable
over time - instead, the steady state memory usage is roughly
determined by the validator set size which grows much more slowly. With
these changes, the core should remain sustainable memory-wise post-merge
all the way to withdrawals (when the validator set is expected to grow).
In-memory indices are still used for the "hot" unfinalized portion of
the chain - this ensure that consensus performance remains unchanged.
What changes is that for historical access, we use a db-based linear
slot index which is cache-and-disk-friendly, keeping the cost for
accessing historical data at a similar level as before, achieving the
savings at no percievable cost to functionality or performance.
A nice collateral benefit is the almost-instant startup since we no
longer load any large indicies at dag init.
The cost of this functionality instead can be found in the complexity of
having to deal with two ways of traversing the chain - by `BlockRef` and
by slot.
* use `BlockId` instead of `BlockRef` where finalized / historical data
may be required
* simplify clearance pre-advancement
* remove dag.finalizedBlocks (~50:ish mb)
* remove `getBlockAtSlot` - use `getBlockIdAtSlot` instead
* `parent` and `atSlot` for `BlockId` now require a `ChainDAGRef`
instance, unlike `BlockRef` traversal
* prune `BlockRef` parents on finality (~200:ish mb)
* speed up ChainDAG init by not loading finalized history index
* mess up light client server error handling - this need revisiting :)
2022-03-17 17:42:56 +00:00
|
|
|
node.withStateForBlockSlotId(bsi):
|
2022-06-20 05:53:39 +00:00
|
|
|
let optimistic = node.getStateOptimistic(state)
|
2022-03-16 07:20:40 +00:00
|
|
|
let res = withState(state):
|
2021-11-30 01:14:31 +00:00
|
|
|
when stateFork >= BeaconStateFork.Altair:
|
|
|
|
produceResponse(indexList,
|
2022-09-13 11:53:12 +00:00
|
|
|
forkyState.data.current_sync_committee.pubkeys.data,
|
|
|
|
forkyState.data.validators.asSeq)
|
2021-11-30 01:14:31 +00:00
|
|
|
else:
|
|
|
|
emptyResponse()
|
2022-06-20 05:53:39 +00:00
|
|
|
return RestApiResponse.jsonResponseWOpt(res, optimistic)
|
2021-10-14 10:38:38 +00:00
|
|
|
|
|
|
|
return RestApiResponse.jsonError(Http404, StateNotFoundError)
|
|
|
|
|
2021-08-23 10:41:48 +00:00
|
|
|
# https://ethereum.github.io/beacon-APIs/#/Validator/produceBlock
|
2022-01-06 07:38:40 +00:00
|
|
|
router.api(MethodGet, "/eth/v1/validator/blocks/{slot}") do (
|
2021-03-23 22:50:18 +00:00
|
|
|
slot: Slot, randao_reveal: Option[ValidatorSig],
|
|
|
|
graffiti: Option[GraffitiBytes]) -> RestApiResponse:
|
|
|
|
let message =
|
|
|
|
block:
|
2022-03-23 11:42:16 +00:00
|
|
|
let qslot = block:
|
|
|
|
if slot.isErr():
|
|
|
|
return RestApiResponse.jsonError(Http400, InvalidSlotValueError,
|
|
|
|
$slot.error())
|
|
|
|
let res = slot.get()
|
|
|
|
|
|
|
|
if res <= node.dag.finalizedHead.slot:
|
|
|
|
return RestApiResponse.jsonError(Http400, InvalidSlotValueError,
|
|
|
|
"Slot already finalized")
|
|
|
|
let
|
|
|
|
wallTime = node.beaconClock.now() + MAXIMUM_GOSSIP_CLOCK_DISPARITY
|
|
|
|
if res > wallTime.slotOrZero:
|
|
|
|
return RestApiResponse.jsonError(Http400, InvalidSlotValueError,
|
|
|
|
"Slot cannot be in the future")
|
|
|
|
|
|
|
|
if node.dag.cfg.blockForkAtEpoch(res.epoch) != BeaconBlockFork.Phase0:
|
|
|
|
return RestApiResponse.jsonError(Http400,
|
|
|
|
"Use v2 for Altair+ slots")
|
|
|
|
|
|
|
|
res
|
2021-03-23 22:50:18 +00:00
|
|
|
let qrandao =
|
|
|
|
if randao_reveal.isNone():
|
2021-04-08 14:34:05 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, MissingRandaoRevealValue)
|
2021-03-23 22:50:18 +00:00
|
|
|
else:
|
|
|
|
let res = randao_reveal.get()
|
|
|
|
if res.isErr():
|
|
|
|
return RestApiResponse.jsonError(Http400,
|
2021-04-08 14:34:05 +00:00
|
|
|
InvalidRandaoRevealValue,
|
2021-03-23 22:50:18 +00:00
|
|
|
$res.error())
|
|
|
|
res.get()
|
|
|
|
let qgraffiti =
|
|
|
|
if graffiti.isNone():
|
|
|
|
defaultGraffitiBytes()
|
|
|
|
else:
|
|
|
|
let res = graffiti.get()
|
|
|
|
if res.isErr():
|
|
|
|
return RestApiResponse.jsonError(Http400,
|
2022-01-05 15:42:29 +00:00
|
|
|
InvalidGraffitiBytesValue,
|
2021-03-23 22:50:18 +00:00
|
|
|
$res.error())
|
|
|
|
res.get()
|
|
|
|
let qhead =
|
|
|
|
block:
|
2022-03-23 11:42:16 +00:00
|
|
|
let res = node.getSyncedHead(qslot)
|
2021-03-23 22:50:18 +00:00
|
|
|
if res.isErr():
|
2022-03-23 11:42:16 +00:00
|
|
|
return RestApiResponse.jsonError(Http503, BeaconNodeInSyncError,
|
|
|
|
$res.error())
|
2021-03-23 22:50:18 +00:00
|
|
|
res.get()
|
2021-06-01 11:13:40 +00:00
|
|
|
let proposer = node.dag.getProposer(qhead, qslot)
|
2021-03-23 22:50:18 +00:00
|
|
|
if proposer.isNone():
|
2021-04-08 14:34:05 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, ProposerNotFoundError)
|
2021-05-20 10:44:13 +00:00
|
|
|
let res = await makeBeaconBlockForHeadAndSlot(
|
2021-06-01 11:13:40 +00:00
|
|
|
node, qrandao, proposer.get(), qgraffiti, qhead, qslot)
|
2021-08-27 09:00:06 +00:00
|
|
|
if res.isErr():
|
2021-08-29 14:50:21 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, res.error())
|
2021-03-23 22:50:18 +00:00
|
|
|
res.get()
|
2021-08-09 06:08:18 +00:00
|
|
|
return
|
2021-08-29 14:50:21 +00:00
|
|
|
case message.kind
|
|
|
|
of BeaconBlockFork.Phase0:
|
2021-10-18 16:37:27 +00:00
|
|
|
RestApiResponse.jsonResponse(message.phase0Data)
|
2021-08-09 06:08:18 +00:00
|
|
|
else:
|
2021-08-29 14:50:21 +00:00
|
|
|
RestApiResponse.jsonError(Http400,
|
2022-03-23 11:42:16 +00:00
|
|
|
"Use v2 for Altair+ slots")
|
2021-04-08 14:34:05 +00:00
|
|
|
|
2021-08-23 10:41:48 +00:00
|
|
|
# https://ethereum.github.io/beacon-APIs/#/Validator/produceBlockV2
|
2022-01-06 07:38:40 +00:00
|
|
|
router.api(MethodGet, "/eth/v2/validator/blocks/{slot}") do (
|
2021-08-09 06:08:18 +00:00
|
|
|
slot: Slot, randao_reveal: Option[ValidatorSig],
|
2022-09-21 07:38:08 +00:00
|
|
|
graffiti: Option[GraffitiBytes], skip_randao_verification: Option[string]) -> RestApiResponse:
|
2021-08-09 06:08:18 +00:00
|
|
|
let message =
|
|
|
|
block:
|
2022-03-23 11:42:16 +00:00
|
|
|
let qslot = block:
|
|
|
|
if slot.isErr():
|
|
|
|
return RestApiResponse.jsonError(Http400, InvalidSlotValueError,
|
|
|
|
$slot.error())
|
|
|
|
let res = slot.get()
|
|
|
|
|
|
|
|
if res <= node.dag.finalizedHead.slot:
|
|
|
|
return RestApiResponse.jsonError(Http400, InvalidSlotValueError,
|
|
|
|
"Slot already finalized")
|
|
|
|
let
|
|
|
|
wallTime = node.beaconClock.now() + MAXIMUM_GOSSIP_CLOCK_DISPARITY
|
|
|
|
if res > wallTime.slotOrZero:
|
|
|
|
return RestApiResponse.jsonError(Http400, InvalidSlotValueError,
|
|
|
|
"Slot cannot be in the future")
|
|
|
|
res
|
2022-09-21 07:38:08 +00:00
|
|
|
let qskip_randao_verification =
|
|
|
|
if skip_randao_verification.isNone():
|
|
|
|
false
|
|
|
|
else:
|
|
|
|
let res = skip_randao_verification.get()
|
|
|
|
if res.isErr() or res.get() != "":
|
|
|
|
return RestApiResponse.jsonError(Http400,
|
|
|
|
InvalidSkipRandaoVerificationValue)
|
|
|
|
true
|
2021-08-09 06:08:18 +00:00
|
|
|
let qrandao =
|
|
|
|
if randao_reveal.isNone():
|
|
|
|
return RestApiResponse.jsonError(Http400, MissingRandaoRevealValue)
|
|
|
|
else:
|
|
|
|
let res = randao_reveal.get()
|
|
|
|
if res.isErr():
|
|
|
|
return RestApiResponse.jsonError(Http400,
|
|
|
|
InvalidRandaoRevealValue,
|
|
|
|
$res.error())
|
|
|
|
res.get()
|
|
|
|
let qgraffiti =
|
|
|
|
if graffiti.isNone():
|
|
|
|
defaultGraffitiBytes()
|
|
|
|
else:
|
|
|
|
let res = graffiti.get()
|
|
|
|
if res.isErr():
|
|
|
|
return RestApiResponse.jsonError(Http400,
|
2022-01-05 15:42:29 +00:00
|
|
|
InvalidGraffitiBytesValue,
|
2021-08-09 06:08:18 +00:00
|
|
|
$res.error())
|
|
|
|
res.get()
|
|
|
|
let qhead =
|
|
|
|
block:
|
2022-03-23 11:42:16 +00:00
|
|
|
let res = node.getSyncedHead(qslot)
|
2021-08-09 06:08:18 +00:00
|
|
|
if res.isErr():
|
2022-03-23 11:42:16 +00:00
|
|
|
return RestApiResponse.jsonError(Http503, BeaconNodeInSyncError,
|
|
|
|
$res.error())
|
2021-08-09 06:08:18 +00:00
|
|
|
res.get()
|
|
|
|
let proposer = node.dag.getProposer(qhead, qslot)
|
|
|
|
if proposer.isNone():
|
|
|
|
return RestApiResponse.jsonError(Http400, ProposerNotFoundError)
|
|
|
|
let res = await makeBeaconBlockForHeadAndSlot(
|
2022-09-21 07:38:08 +00:00
|
|
|
node, qrandao, proposer.get(), qgraffiti, qhead, qslot, qskip_randao_verification)
|
2021-08-27 09:00:06 +00:00
|
|
|
if res.isErr():
|
2021-08-29 14:50:21 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, res.error())
|
2021-08-09 06:08:18 +00:00
|
|
|
res.get()
|
2021-08-29 14:50:21 +00:00
|
|
|
return RestApiResponse.jsonResponsePlain(message)
|
2021-03-23 22:50:18 +00:00
|
|
|
|
2021-08-23 10:41:48 +00:00
|
|
|
# https://ethereum.github.io/beacon-APIs/#/Validator/produceAttestationData
|
2022-01-06 07:38:40 +00:00
|
|
|
router.api(MethodGet, "/eth/v1/validator/attestation_data") do (
|
2021-03-23 22:50:18 +00:00
|
|
|
slot: Option[Slot],
|
|
|
|
committee_index: Option[CommitteeIndex]) -> RestApiResponse:
|
|
|
|
let adata =
|
|
|
|
block:
|
|
|
|
let qslot =
|
|
|
|
block:
|
|
|
|
if slot.isNone():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, MissingSlotValueError)
|
2021-03-23 22:50:18 +00:00
|
|
|
let res = slot.get()
|
|
|
|
if res.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, InvalidSlotValueError,
|
2021-03-23 22:50:18 +00:00
|
|
|
$res.error())
|
|
|
|
res.get()
|
2022-03-23 11:42:16 +00:00
|
|
|
if qslot <= node.dag.finalizedHead.slot:
|
|
|
|
return RestApiResponse.jsonError(Http400, InvalidSlotValueError,
|
|
|
|
"Slot already finalized")
|
|
|
|
let
|
|
|
|
wallTime = node.beaconClock.now()
|
|
|
|
if qslot > (wallTime + MAXIMUM_GOSSIP_CLOCK_DISPARITY).slotOrZero:
|
|
|
|
return RestApiResponse.jsonError(
|
|
|
|
Http400, InvalidSlotValueError, "Slot cannot be in the future")
|
|
|
|
if qslot + SLOTS_PER_EPOCH <
|
|
|
|
(wallTime - MAXIMUM_GOSSIP_CLOCK_DISPARITY).slotOrZero:
|
|
|
|
return RestApiResponse.jsonError(
|
|
|
|
Http400, InvalidSlotValueError,
|
|
|
|
"Slot cannot be more than an epoch in the past")
|
|
|
|
|
2021-03-23 22:50:18 +00:00
|
|
|
let qindex =
|
|
|
|
block:
|
|
|
|
if committee_index.isNone():
|
|
|
|
return RestApiResponse.jsonError(Http400,
|
2021-04-08 10:49:28 +00:00
|
|
|
MissingCommitteeIndexValueError)
|
2021-03-23 22:50:18 +00:00
|
|
|
let res = committee_index.get()
|
|
|
|
if res.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400,
|
|
|
|
InvalidCommitteeIndexValueError,
|
2021-03-23 22:50:18 +00:00
|
|
|
$res.error())
|
|
|
|
res.get()
|
|
|
|
let qhead =
|
|
|
|
block:
|
2022-03-23 11:42:16 +00:00
|
|
|
let res = node.getSyncedHead(qslot)
|
2021-03-23 22:50:18 +00:00
|
|
|
if res.isErr():
|
2021-04-27 20:46:24 +00:00
|
|
|
return RestApiResponse.jsonError(Http503, BeaconNodeInSyncError)
|
2021-03-23 22:50:18 +00:00
|
|
|
res.get()
|
2022-09-27 16:56:08 +00:00
|
|
|
let epochRef = node.dag.getEpochRef(qhead, qslot.epoch, true).valueOr:
|
|
|
|
return RestApiResponse.jsonError(Http400, PrunedStateError, $error)
|
2021-03-23 22:50:18 +00:00
|
|
|
makeAttestationData(epochRef, qhead.atSlot(qslot), qindex)
|
|
|
|
return RestApiResponse.jsonResponse(adata)
|
|
|
|
|
2021-08-23 10:41:48 +00:00
|
|
|
# https://ethereum.github.io/beacon-APIs/#/Validator/getAggregatedAttestation
|
2022-01-06 07:38:40 +00:00
|
|
|
router.api(MethodGet, "/eth/v1/validator/aggregate_attestation") do (
|
2021-03-23 22:50:18 +00:00
|
|
|
attestation_data_root: Option[Eth2Digest],
|
|
|
|
slot: Option[Slot]) -> RestApiResponse:
|
|
|
|
let attestation =
|
|
|
|
block:
|
|
|
|
let qslot =
|
|
|
|
block:
|
|
|
|
if slot.isNone():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, MissingSlotValueError)
|
2021-03-23 22:50:18 +00:00
|
|
|
let res = slot.get()
|
|
|
|
if res.isErr():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, InvalidSlotValueError,
|
2021-03-23 22:50:18 +00:00
|
|
|
$res.error())
|
|
|
|
res.get()
|
|
|
|
let qroot =
|
|
|
|
block:
|
|
|
|
if attestation_data_root.isNone():
|
2021-04-08 14:34:05 +00:00
|
|
|
return RestApiResponse.jsonError(Http400,
|
|
|
|
MissingAttestationDataRootValueError)
|
2021-03-23 22:50:18 +00:00
|
|
|
let res = attestation_data_root.get()
|
|
|
|
if res.isErr():
|
2021-04-08 14:34:05 +00:00
|
|
|
return RestApiResponse.jsonError(Http400,
|
|
|
|
InvalidAttestationDataRootValueError, $res.error())
|
2021-03-23 22:50:18 +00:00
|
|
|
res.get()
|
2022-03-29 07:15:42 +00:00
|
|
|
let res =
|
|
|
|
node.attestationPool[].getAggregatedAttestation(qslot, qroot)
|
2021-03-23 22:50:18 +00:00
|
|
|
if res.isNone():
|
2021-04-08 14:34:05 +00:00
|
|
|
return RestApiResponse.jsonError(Http400,
|
|
|
|
UnableToGetAggregatedAttestationError)
|
2021-03-23 22:50:18 +00:00
|
|
|
res.get()
|
|
|
|
return RestApiResponse.jsonResponse(attestation)
|
|
|
|
|
2021-08-23 10:41:48 +00:00
|
|
|
# https://ethereum.github.io/beacon-APIs/#/Validator/publishAggregateAndProofs
|
2022-01-06 07:38:40 +00:00
|
|
|
router.api(MethodPost, "/eth/v1/validator/aggregate_and_proofs") do (
|
2021-03-23 22:50:18 +00:00
|
|
|
contentBody: Option[ContentBody]) -> RestApiResponse:
|
2021-05-20 18:43:42 +00:00
|
|
|
let proofs =
|
2021-03-23 22:50:18 +00:00
|
|
|
block:
|
|
|
|
if contentBody.isNone():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, EmptyRequestBodyError)
|
2021-05-20 18:43:42 +00:00
|
|
|
let dres = decodeBody(seq[SignedAggregateAndProof], contentBody.get())
|
2021-03-23 22:50:18 +00:00
|
|
|
if dres.isErr():
|
2021-04-08 14:34:05 +00:00
|
|
|
return RestApiResponse.jsonError(Http400,
|
|
|
|
InvalidAggregateAndProofObjectError,
|
|
|
|
$dres.error())
|
2021-03-23 22:50:18 +00:00
|
|
|
dres.get()
|
2021-08-23 10:41:48 +00:00
|
|
|
# Since our validation logic supports batch processing, we will submit all
|
|
|
|
# aggregated attestations for validation.
|
|
|
|
var pending =
|
|
|
|
block:
|
|
|
|
var res: seq[Future[SendResult]]
|
|
|
|
for proof in proofs:
|
2022-07-06 16:11:44 +00:00
|
|
|
res.add(node.router.routeSignedAggregateAndProof(proof))
|
2021-08-23 10:41:48 +00:00
|
|
|
res
|
|
|
|
await allFutures(pending)
|
|
|
|
for future in pending:
|
|
|
|
if future.done():
|
|
|
|
let res = future.read()
|
|
|
|
if res.isErr():
|
|
|
|
return RestApiResponse.jsonError(Http400,
|
|
|
|
AggregateAndProofValidationError,
|
|
|
|
$res.error())
|
|
|
|
else:
|
|
|
|
return RestApiResponse.jsonError(Http500,
|
|
|
|
"Unexpected server failure, while sending aggregate and proof")
|
2021-07-13 11:15:07 +00:00
|
|
|
return RestApiResponse.jsonMsgResponse(AggregateAndProofValidationSuccess)
|
2021-03-23 22:50:18 +00:00
|
|
|
|
2021-08-23 10:41:48 +00:00
|
|
|
# https://ethereum.github.io/beacon-APIs/#/Validator/prepareBeaconCommitteeSubnet
|
2021-03-23 22:50:18 +00:00
|
|
|
router.api(MethodPost,
|
2022-01-06 07:38:40 +00:00
|
|
|
"/eth/v1/validator/beacon_committee_subscriptions") do (
|
2021-03-23 22:50:18 +00:00
|
|
|
contentBody: Option[ContentBody]) -> RestApiResponse:
|
|
|
|
let requests =
|
|
|
|
block:
|
|
|
|
if contentBody.isNone():
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, EmptyRequestBodyError)
|
2021-05-20 17:56:12 +00:00
|
|
|
let dres = decodeBody(seq[RestCommitteeSubscription],
|
2021-03-23 22:50:18 +00:00
|
|
|
contentBody.get())
|
|
|
|
if dres.isErr():
|
2021-04-08 14:34:05 +00:00
|
|
|
return RestApiResponse.jsonError(Http400,
|
|
|
|
InvalidSubscriptionRequestValueError,
|
|
|
|
$dres.error())
|
2021-03-23 22:50:18 +00:00
|
|
|
dres.get()
|
2021-06-01 11:13:40 +00:00
|
|
|
if not(node.isSynced(node.dag.head)):
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http503, BeaconNodeInSyncError)
|
2021-03-23 22:50:18 +00:00
|
|
|
|
2021-12-03 15:04:58 +00:00
|
|
|
let
|
|
|
|
wallSlot = node.beaconClock.now.slotOrZero
|
|
|
|
wallEpoch = wallSlot.epoch
|
|
|
|
head = node.dag.head
|
|
|
|
|
2022-08-18 18:07:01 +00:00
|
|
|
var currentEpoch, nextEpoch: Opt[ShufflingRef]
|
|
|
|
template getAndCacheShufflingRef(shufflingRefVar: var Opt[ShufflingRef],
|
|
|
|
epoch: Epoch): ShufflingRef =
|
|
|
|
if shufflingRefVar.isNone:
|
|
|
|
shufflingRefVar = block:
|
|
|
|
let tmp = node.dag.getShufflingRef(head, epoch, true).valueOr:
|
2022-01-05 18:38:04 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, PrunedStateError)
|
2022-08-18 18:07:01 +00:00
|
|
|
Opt.some tmp
|
2022-01-05 18:38:04 +00:00
|
|
|
|
2022-08-18 18:07:01 +00:00
|
|
|
shufflingRefVar.get
|
2021-12-03 15:04:58 +00:00
|
|
|
|
2021-03-23 22:50:18 +00:00
|
|
|
for request in requests:
|
2021-04-18 08:24:59 +00:00
|
|
|
if uint64(request.committee_index) >= uint64(MAX_COMMITTEES_PER_SLOT):
|
2021-04-08 10:49:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400,
|
|
|
|
InvalidCommitteeIndexValueError)
|
2021-11-12 22:29:28 +00:00
|
|
|
if uint64(request.validator_index) >=
|
2022-03-16 07:20:40 +00:00
|
|
|
lenu64(getStateField(node.dag.headState, validators)):
|
2021-11-12 22:29:28 +00:00
|
|
|
return RestApiResponse.jsonError(Http400,
|
2021-12-03 15:04:58 +00:00
|
|
|
InvalidValidatorIndexValueError)
|
2021-03-23 22:50:18 +00:00
|
|
|
if wallSlot > request.slot + 1:
|
2021-04-08 14:34:05 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, SlotFromThePastError)
|
2021-12-03 15:04:58 +00:00
|
|
|
|
2022-08-18 18:07:01 +00:00
|
|
|
let
|
|
|
|
epoch = request.slot.epoch
|
|
|
|
shufflingRef =
|
|
|
|
if epoch == wallEpoch:
|
|
|
|
currentEpoch.getAndCacheShufflingRef(wallEpoch)
|
|
|
|
elif epoch == wallEpoch + 1:
|
|
|
|
nextEpoch.getAndCacheShufflingRef(wallEpoch + 1)
|
|
|
|
else:
|
|
|
|
return RestApiResponse.jsonError(Http400,
|
|
|
|
SlotNotInNextWallSlotEpochError)
|
2021-12-03 15:04:58 +00:00
|
|
|
|
2021-10-20 09:16:48 +00:00
|
|
|
let subnet_id = compute_subnet_for_attestation(
|
2022-08-18 18:07:01 +00:00
|
|
|
get_committee_count_per_slot(shufflingRef), request.slot,
|
2021-03-23 22:50:18 +00:00
|
|
|
request.committee_index)
|
2021-10-18 09:11:44 +00:00
|
|
|
|
2022-09-07 18:34:52 +00:00
|
|
|
node.consensusManager[].actionTracker.registerDuty(
|
2021-10-20 09:16:48 +00:00
|
|
|
request.slot, subnet_id, request.validator_index,
|
2021-10-18 09:11:44 +00:00
|
|
|
request.is_aggregator)
|
|
|
|
|
2022-03-29 07:15:42 +00:00
|
|
|
let validator_pubkey =
|
2022-05-30 13:30:42 +00:00
|
|
|
getStateField(node.dag.headState, validators).item(
|
|
|
|
request.validator_index).pubkey
|
2021-12-20 19:20:31 +00:00
|
|
|
|
|
|
|
node.validatorMonitor[].addAutoMonitor(
|
|
|
|
validator_pubkey, ValidatorIndex(request.validator_index))
|
|
|
|
|
2021-07-13 11:15:07 +00:00
|
|
|
return RestApiResponse.jsonMsgResponse(BeaconCommitteeSubscriptionSuccess)
|
2021-04-13 10:19:31 +00:00
|
|
|
|
2021-09-23 22:13:25 +00:00
|
|
|
# https://ethereum.github.io/beacon-APIs/#/Validator/prepareSyncCommitteeSubnets
|
|
|
|
router.api(MethodPost,
|
2022-01-06 07:38:40 +00:00
|
|
|
"/eth/v1/validator/sync_committee_subscriptions") do (
|
2021-09-23 22:13:25 +00:00
|
|
|
contentBody: Option[ContentBody]) -> RestApiResponse:
|
|
|
|
let subscriptions =
|
|
|
|
block:
|
|
|
|
if contentBody.isNone():
|
|
|
|
return RestApiResponse.jsonError(Http400, EmptyRequestBodyError)
|
|
|
|
let dres = decodeBody(seq[RestSyncCommitteeSubscription],
|
|
|
|
contentBody.get())
|
|
|
|
if dres.isErr():
|
|
|
|
return RestApiResponse.jsonError(Http400,
|
|
|
|
InvalidSyncCommitteeSubscriptionRequestError)
|
|
|
|
let subs = dres.get()
|
|
|
|
for item in subs:
|
|
|
|
if item.until_epoch > MaxEpoch:
|
|
|
|
return RestApiResponse.jsonError(Http400, EpochOverflowValueError)
|
|
|
|
if item.until_epoch < node.dag.cfg.ALTAIR_FORK_EPOCH:
|
|
|
|
return RestApiResponse.jsonError(Http400,
|
|
|
|
EpochFromTheIncorrectForkError)
|
|
|
|
if uint64(item.validator_index) >=
|
2022-03-16 07:20:40 +00:00
|
|
|
lenu64(getStateField(node.dag.headState, validators)):
|
2021-09-23 22:13:25 +00:00
|
|
|
return RestApiResponse.jsonError(Http400,
|
|
|
|
InvalidValidatorIndexValueError)
|
2022-03-29 07:15:42 +00:00
|
|
|
let validator_pubkey =
|
2022-05-30 13:30:42 +00:00
|
|
|
getStateField(node.dag.headState, validators).item(
|
|
|
|
item.validator_index).pubkey
|
2021-12-20 19:20:31 +00:00
|
|
|
|
2022-03-29 07:15:42 +00:00
|
|
|
node.syncCommitteeMsgPool
|
|
|
|
.syncCommitteeSubscriptions[validator_pubkey] = item.until_epoch
|
2022-01-24 20:40:59 +00:00
|
|
|
|
2021-12-20 19:20:31 +00:00
|
|
|
node.validatorMonitor[].addAutoMonitor(
|
|
|
|
validator_pubkey, ValidatorIndex(item.validator_index))
|
|
|
|
|
2021-09-23 22:13:25 +00:00
|
|
|
subs
|
|
|
|
|
|
|
|
return RestApiResponse.jsonMsgResponse(SyncCommitteeSubscriptionSuccess)
|
|
|
|
|
|
|
|
# https://ethereum.github.io/beacon-APIs/#/Validator/produceSyncCommitteeContribution
|
|
|
|
router.api(MethodGet,
|
2022-01-06 07:38:40 +00:00
|
|
|
"/eth/v1/validator/sync_committee_contribution") do (
|
2022-01-17 15:44:28 +00:00
|
|
|
slot: Option[Slot], subcommittee_index: Option[SyncSubCommitteeIndex],
|
2021-09-23 22:13:25 +00:00
|
|
|
beacon_block_root: Option[Eth2Digest]) -> RestApiResponse:
|
2022-03-23 11:42:16 +00:00
|
|
|
let qslot = block:
|
2021-09-23 22:13:25 +00:00
|
|
|
if slot.isNone():
|
|
|
|
return RestApiResponse.jsonError(Http400, MissingSlotValueError)
|
2022-03-23 11:42:16 +00:00
|
|
|
|
|
|
|
let res = slot.get()
|
|
|
|
if res.isErr():
|
|
|
|
return RestApiResponse.jsonError(Http400, InvalidSlotValueError,
|
|
|
|
$res.error())
|
|
|
|
let rslot = res.get()
|
|
|
|
if epoch(rslot) < node.dag.cfg.ALTAIR_FORK_EPOCH:
|
|
|
|
return RestApiResponse.jsonError(Http400,
|
|
|
|
SlotFromTheIncorrectForkError)
|
|
|
|
rslot
|
|
|
|
if qslot <= node.dag.finalizedHead.slot:
|
|
|
|
return RestApiResponse.jsonError(Http400, InvalidSlotValueError,
|
|
|
|
"Slot already finalized")
|
2021-09-23 22:13:25 +00:00
|
|
|
let qindex =
|
|
|
|
if subcommittee_index.isNone():
|
|
|
|
return RestApiResponse.jsonError(Http400,
|
|
|
|
MissingSubCommitteeIndexValueError)
|
|
|
|
else:
|
2022-01-17 15:44:28 +00:00
|
|
|
let res = subcommittee_index.get()
|
2021-09-23 22:13:25 +00:00
|
|
|
if res.isErr():
|
|
|
|
return RestApiResponse.jsonError(Http400,
|
2022-01-08 23:28:49 +00:00
|
|
|
InvalidSubCommitteeIndexValueError,
|
|
|
|
$res.error())
|
|
|
|
res.get()
|
2021-09-23 22:13:25 +00:00
|
|
|
let qroot =
|
|
|
|
if beacon_block_root.isNone():
|
|
|
|
return RestApiResponse.jsonError(Http400,
|
|
|
|
MissingBeaconBlockRootValueError)
|
|
|
|
else:
|
|
|
|
let res = beacon_block_root.get()
|
|
|
|
if res.isErr():
|
|
|
|
return RestApiResponse.jsonError(Http400,
|
|
|
|
InvalidBeaconBlockRootValueError,
|
|
|
|
$res.error())
|
|
|
|
res.get()
|
|
|
|
|
|
|
|
# Check if node is fully synced.
|
2022-03-23 11:42:16 +00:00
|
|
|
let sres = node.getSyncedHead(qslot)
|
2021-09-23 22:13:25 +00:00
|
|
|
if sres.isErr():
|
|
|
|
return RestApiResponse.jsonError(Http503, BeaconNodeInSyncError)
|
|
|
|
|
|
|
|
var contribution = SyncCommitteeContribution()
|
|
|
|
let res = node.syncCommitteeMsgPool[].produceContribution(
|
2021-09-28 18:02:01 +00:00
|
|
|
qslot, qroot, qindex, contribution)
|
2021-09-23 22:13:25 +00:00
|
|
|
if not(res):
|
|
|
|
return RestApiResponse.jsonError(Http400, ProduceContributionError)
|
|
|
|
return RestApiResponse.jsonResponse(contribution)
|
|
|
|
|
|
|
|
# https://ethereum.github.io/beacon-APIs/#/Validator/publishContributionAndProofs
|
|
|
|
router.api(MethodPost,
|
2022-01-06 07:38:40 +00:00
|
|
|
"/eth/v1/validator/contribution_and_proofs") do (
|
2021-09-23 22:13:25 +00:00
|
|
|
contentBody: Option[ContentBody]) -> RestApiResponse:
|
|
|
|
let proofs =
|
|
|
|
block:
|
|
|
|
if contentBody.isNone():
|
|
|
|
return RestApiResponse.jsonError(Http400, EmptyRequestBodyError)
|
|
|
|
let dres = decodeBody(seq[SignedContributionAndProof],
|
|
|
|
contentBody.get())
|
|
|
|
if dres.isErr():
|
|
|
|
return RestApiResponse.jsonError(Http400,
|
2022-07-25 20:12:53 +00:00
|
|
|
InvalidContributionAndProofMessageError)
|
2021-09-23 22:13:25 +00:00
|
|
|
dres.get()
|
|
|
|
|
|
|
|
let pending =
|
|
|
|
block:
|
|
|
|
var res: seq[Future[SendResult]]
|
|
|
|
for proof in proofs:
|
2022-07-06 16:11:44 +00:00
|
|
|
res.add(node.router.routeSignedContributionAndProof(proof, true))
|
2021-09-23 22:13:25 +00:00
|
|
|
res
|
|
|
|
|
|
|
|
let failures =
|
|
|
|
block:
|
2022-09-29 20:55:18 +00:00
|
|
|
var res: seq[RestIndexedErrorMessageItem]
|
2021-09-23 22:13:25 +00:00
|
|
|
await allFutures(pending)
|
2022-05-10 10:03:40 +00:00
|
|
|
for index, future in pending:
|
2021-09-23 22:13:25 +00:00
|
|
|
if future.done():
|
|
|
|
let fres = future.read()
|
|
|
|
if fres.isErr():
|
2022-09-29 20:55:18 +00:00
|
|
|
let failure = RestIndexedErrorMessageItem(index: index,
|
|
|
|
message: $fres.error())
|
2021-09-23 22:13:25 +00:00
|
|
|
res.add(failure)
|
|
|
|
elif future.failed() or future.cancelled():
|
|
|
|
# This is unexpected failure, so we log the error message.
|
|
|
|
let exc = future.readError()
|
2022-09-29 20:55:18 +00:00
|
|
|
let failure = RestIndexedErrorMessageItem(index: index,
|
|
|
|
message: $exc.msg)
|
2021-09-23 22:13:25 +00:00
|
|
|
res.add(failure)
|
|
|
|
res
|
|
|
|
|
|
|
|
if len(failures) > 0:
|
|
|
|
return RestApiResponse.jsonErrorList(Http400,
|
|
|
|
ContributionAndProofValidationError,
|
|
|
|
failures)
|
|
|
|
else:
|
|
|
|
return RestApiResponse.jsonMsgResponse(
|
|
|
|
ContributionAndProofValidationSuccess
|
|
|
|
)
|
|
|
|
|
2022-07-25 20:12:53 +00:00
|
|
|
# https://ethereum.github.io/beacon-APIs/#/ValidatorRequiredApi/prepareBeaconProposer
|
|
|
|
router.api(MethodPost,
|
|
|
|
"/eth/v1/validator/prepare_beacon_proposer") do (
|
|
|
|
contentBody: Option[ContentBody]) -> RestApiResponse:
|
|
|
|
let
|
2022-08-09 09:53:02 +00:00
|
|
|
body =
|
2022-07-25 20:12:53 +00:00
|
|
|
block:
|
|
|
|
if contentBody.isNone():
|
|
|
|
return RestApiResponse.jsonError(Http400, EmptyRequestBodyError)
|
2022-08-09 09:53:02 +00:00
|
|
|
let dres = decodeBody(seq[PrepareBeaconProposer], contentBody.get())
|
2022-07-25 20:12:53 +00:00
|
|
|
if dres.isErr():
|
|
|
|
return RestApiResponse.jsonError(Http400,
|
|
|
|
InvalidPrepareBeaconProposerError)
|
|
|
|
dres.get()
|
|
|
|
currentEpoch = node.beaconClock.now.slotOrZero.epoch
|
|
|
|
|
2022-08-09 09:53:02 +00:00
|
|
|
for proposerData in body:
|
2022-08-23 16:19:52 +00:00
|
|
|
node.dynamicFeeRecipientsStore[].addMapping(
|
2022-08-09 09:53:02 +00:00
|
|
|
proposerData.validator_index,
|
|
|
|
proposerData.fee_recipient,
|
|
|
|
currentEpoch)
|
2022-07-25 20:12:53 +00:00
|
|
|
|
|
|
|
return RestApiResponse.response("", Http200, "text/plain")
|
|
|
|
|
2022-09-13 11:52:26 +00:00
|
|
|
# https://ethereum.github.io/beacon-APIs/#/Validator/registerValidator
|
|
|
|
# https://github.com/ethereum/beacon-APIs/blob/v2.3.0/apis/validator/register_validator.yaml
|
|
|
|
router.api(MethodPost,
|
|
|
|
"/eth/v1/validator/register_validator") do (
|
|
|
|
contentBody: Option[ContentBody]) -> RestApiResponse:
|
|
|
|
let
|
|
|
|
body =
|
|
|
|
block:
|
|
|
|
if contentBody.isNone():
|
|
|
|
return RestApiResponse.jsonError(Http400, EmptyRequestBodyError)
|
|
|
|
let dres = decodeBody(seq[SignedValidatorRegistrationV1], contentBody.get())
|
|
|
|
if dres.isErr():
|
|
|
|
return RestApiResponse.jsonError(Http400,
|
|
|
|
InvalidPrepareBeaconProposerError)
|
|
|
|
dres.get()
|
|
|
|
|
|
|
|
for signedValidatorRegistration in body:
|
|
|
|
# Don't validate beyond syntactically, because
|
|
|
|
# "requests containing currently inactive or unknown validator pubkeys
|
|
|
|
# will be accepted, as they may become active at a later epoch". Along
|
|
|
|
# these lines, even if it's adding a validator the BN already has as a
|
|
|
|
# local validator, the keymanager API might remove that from the BN.
|
|
|
|
node.externalBuilderRegistrations[signedValidatorRegistration.message.pubkey] =
|
|
|
|
signedValidatorRegistration
|
|
|
|
|
|
|
|
return RestApiResponse.response("", Http200, "text/plain")
|