2020-10-27 09:00:57 +00:00
|
|
|
# beacon_chain
|
2021-01-19 17:44:03 +00:00
|
|
|
# Copyright (c) 2018-2021 Status Research & Development GmbH
|
2020-10-27 09:00:57 +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.
|
|
|
|
|
2021-03-26 06:52:01 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2020-10-27 09:00:57 +00:00
|
|
|
import
|
|
|
|
# Standard library
|
2021-06-23 14:43:18 +00:00
|
|
|
std/tables,
|
2020-10-27 09:00:57 +00:00
|
|
|
|
|
|
|
# Nimble packages
|
2021-06-23 14:43:18 +00:00
|
|
|
stew/objects,
|
2021-03-26 14:11:06 +00:00
|
|
|
json_rpc/servers/httpserver,
|
2020-10-27 09:00:57 +00:00
|
|
|
chronicles,
|
|
|
|
|
|
|
|
# Local modules
|
2021-08-12 13:08:20 +00:00
|
|
|
../spec/[forks, helpers, network, signatures],
|
2021-07-15 19:01:07 +00:00
|
|
|
../spec/datatypes/phase0,
|
2021-08-03 15:17:11 +00:00
|
|
|
../spec/eth2_apis/rpc_types,
|
2021-08-18 18:57:58 +00:00
|
|
|
../consensus_object_pools/[blockchain_dag, spec_cache, attestation_pool],
|
2021-10-19 14:09:26 +00:00
|
|
|
../beacon_node,
|
2021-03-05 13:12:00 +00:00
|
|
|
../validators/validator_duties,
|
|
|
|
../networking/eth2_network,
|
2020-10-27 09:00:57 +00:00
|
|
|
./rpc_utils
|
|
|
|
|
|
|
|
logScope: topics = "valapi"
|
|
|
|
|
|
|
|
type
|
|
|
|
RpcServer* = RpcHttpServer
|
|
|
|
|
2021-03-26 06:52:01 +00:00
|
|
|
proc installValidatorApiHandlers*(rpcServer: RpcServer, node: BeaconNode) {.
|
2021-08-27 09:00:06 +00:00
|
|
|
raises: [Defect, CatchableError].} =
|
2020-10-27 09:00:57 +00:00
|
|
|
rpcServer.rpc("get_v1_validator_block") do (
|
2021-07-15 19:01:07 +00:00
|
|
|
slot: Slot, graffiti: GraffitiBytes, randao_reveal: ValidatorSig) -> phase0.BeaconBlock:
|
2020-10-27 09:00:57 +00:00
|
|
|
debug "get_v1_validator_block", slot = slot
|
|
|
|
let head = node.doChecksAndGetCurrentHead(slot)
|
2021-06-01 11:13:40 +00:00
|
|
|
let proposer = node.dag.getProposer(head, slot)
|
2020-10-27 09:00:57 +00:00
|
|
|
if proposer.isNone():
|
2021-08-27 09:00:06 +00:00
|
|
|
raise newException(CatchableError,
|
|
|
|
"could not retrieve block for slot: " & $slot)
|
2021-05-20 10:44:13 +00:00
|
|
|
let message = await makeBeaconBlockForHeadAndSlot(
|
2021-06-01 11:13:40 +00:00
|
|
|
node, randao_reveal, proposer.get(), graffiti, head, slot)
|
2021-08-27 09:00:06 +00:00
|
|
|
if message.isErr():
|
|
|
|
raise newException(CatchableError,
|
|
|
|
"could not retrieve block for slot: " & $slot)
|
2021-08-29 14:50:21 +00:00
|
|
|
let blck = message.get()
|
|
|
|
case blck.kind
|
|
|
|
of BeaconBlockFork.Phase0:
|
2021-10-18 16:37:27 +00:00
|
|
|
return blck.phase0Data
|
2021-10-13 10:20:18 +00:00
|
|
|
else:
|
|
|
|
raiseNoAltairSupport()
|
2020-10-27 09:00:57 +00:00
|
|
|
|
2021-07-15 19:01:07 +00:00
|
|
|
rpcServer.rpc("post_v1_validator_block") do (body: phase0.SignedBeaconBlock) -> bool:
|
2020-10-27 09:00:57 +00:00
|
|
|
debug "post_v1_validator_block",
|
|
|
|
slot = body.message.slot,
|
|
|
|
prop_idx = body.message.proposer_index
|
|
|
|
let head = node.doChecksAndGetCurrentHead(body.message.slot)
|
|
|
|
|
|
|
|
if head.slot >= body.message.slot:
|
|
|
|
raise newException(CatchableError,
|
|
|
|
"Proposal is for a past slot: " & $body.message.slot)
|
2021-08-29 14:50:21 +00:00
|
|
|
if head == await proposeSignedBlock(
|
|
|
|
node, head, AttachedValidator(), ForkedSignedBeaconBlock.init(body)):
|
2020-10-27 09:00:57 +00:00
|
|
|
raise newException(CatchableError, "Could not propose block")
|
|
|
|
return true
|
|
|
|
|
2020-12-07 12:51:14 +00:00
|
|
|
rpcServer.rpc("get_v1_validator_attestation_data") do (
|
2020-10-27 09:00:57 +00:00
|
|
|
slot: Slot, committee_index: CommitteeIndex) -> AttestationData:
|
2020-12-07 12:51:14 +00:00
|
|
|
debug "get_v1_validator_attestation_data", slot = slot
|
2020-10-27 09:00:57 +00:00
|
|
|
let
|
|
|
|
head = node.doChecksAndGetCurrentHead(slot)
|
2021-06-01 11:13:40 +00:00
|
|
|
epochRef = node.dag.getEpochRef(head, slot.epoch)
|
2020-11-04 21:52:47 +00:00
|
|
|
return makeAttestationData(epochRef, head.atSlot(slot), committee_index)
|
2020-10-27 09:00:57 +00:00
|
|
|
|
|
|
|
rpcServer.rpc("get_v1_validator_aggregate_attestation") do (
|
|
|
|
slot: Slot, attestation_data_root: Eth2Digest)-> Attestation:
|
|
|
|
debug "get_v1_validator_aggregate_attestation"
|
|
|
|
let res = node.attestationPool[].getAggregatedAttestation(slot, attestation_data_root)
|
|
|
|
if res.isSome:
|
|
|
|
return res.get
|
|
|
|
raise newException(CatchableError, "Could not retrieve an aggregated attestation")
|
|
|
|
|
|
|
|
rpcServer.rpc("post_v1_validator_aggregate_and_proofs") do (
|
|
|
|
payload: SignedAggregateAndProof) -> bool:
|
|
|
|
debug "post_v1_validator_aggregate_and_proofs"
|
2021-10-20 09:16:48 +00:00
|
|
|
return (await node.sendAggregateAndProof(payload)).isOk()
|
2020-10-27 09:00:57 +00:00
|
|
|
|
|
|
|
rpcServer.rpc("get_v1_validator_duties_attester") do (
|
2021-08-03 15:17:11 +00:00
|
|
|
epoch: Epoch, public_keys: seq[ValidatorPubKey]) -> seq[RpcAttesterDuties]:
|
2020-10-27 09:00:57 +00:00
|
|
|
debug "get_v1_validator_duties_attester", epoch = epoch
|
|
|
|
let
|
|
|
|
head = node.doChecksAndGetCurrentHead(epoch)
|
2021-06-01 11:13:40 +00:00
|
|
|
epochRef = node.dag.getEpochRef(head, epoch)
|
2020-10-27 09:00:57 +00:00
|
|
|
committees_per_slot = get_committee_count_per_slot(epochRef)
|
|
|
|
for i in 0 ..< SLOTS_PER_EPOCH:
|
|
|
|
let slot = compute_start_slot_at_epoch(epoch) + i
|
|
|
|
for committee_index in 0'u64..<committees_per_slot:
|
|
|
|
let committee = get_beacon_committee(
|
|
|
|
epochRef, slot, committee_index.CommitteeIndex)
|
|
|
|
for index_in_committee, validatorIdx in committee:
|
2021-06-10 07:37:02 +00:00
|
|
|
let curr_val_pubkey = epochRef.validatorKey(validatorIdx)
|
|
|
|
if curr_val_pubkey.isSome():
|
|
|
|
if public_keys.findIt(it == curr_val_pubkey.get().toPubKey()) != -1:
|
|
|
|
result.add((public_key: curr_val_pubkey.get().toPubKey(),
|
2020-10-27 09:00:57 +00:00
|
|
|
validator_index: validatorIdx,
|
|
|
|
committee_index: committee_index.CommitteeIndex,
|
|
|
|
committee_length: committee.lenu64,
|
|
|
|
validator_committee_index: index_in_committee.uint64,
|
|
|
|
slot: slot))
|
|
|
|
|
|
|
|
rpcServer.rpc("get_v1_validator_duties_proposer") do (
|
2021-08-03 15:17:11 +00:00
|
|
|
epoch: Epoch) -> seq[RpcValidatorDuties]:
|
2020-10-27 09:00:57 +00:00
|
|
|
debug "get_v1_validator_duties_proposer", epoch = epoch
|
|
|
|
let
|
|
|
|
head = node.doChecksAndGetCurrentHead(epoch)
|
2021-06-01 11:13:40 +00:00
|
|
|
epochRef = node.dag.getEpochRef(head, epoch)
|
|
|
|
for i, bp in epochRef.beacon_proposers:
|
|
|
|
if bp.isSome():
|
2021-08-27 09:00:06 +00:00
|
|
|
result.add((public_key: epochRef.validatorKey(bp.get).get().toPubKey,
|
2021-06-01 11:13:40 +00:00
|
|
|
validator_index: bp.get(),
|
2020-10-27 09:00:57 +00:00
|
|
|
slot: compute_start_slot_at_epoch(epoch) + i))
|
|
|
|
|
|
|
|
rpcServer.rpc("post_v1_validator_beacon_committee_subscriptions") do (
|
|
|
|
committee_index: CommitteeIndex, slot: Slot, aggregator: bool,
|
|
|
|
validator_pubkey: ValidatorPubKey, slot_signature: ValidatorSig) -> bool:
|
2020-12-22 09:05:36 +00:00
|
|
|
debug "post_v1_validator_beacon_committee_subscriptions",
|
|
|
|
committee_index, slot
|
2021-04-18 08:24:59 +00:00
|
|
|
if committee_index.uint64 >= MAX_COMMITTEES_PER_SLOT.uint64:
|
2020-12-22 09:05:36 +00:00
|
|
|
raise newException(CatchableError,
|
|
|
|
"Invalid committee index")
|
|
|
|
|
|
|
|
if node.syncManager.inProgress:
|
|
|
|
raise newException(CatchableError,
|
|
|
|
"Beacon node is currently syncing and not serving request on that endpoint")
|
|
|
|
|
|
|
|
let wallSlot = node.beaconClock.now.slotOrZero
|
|
|
|
if wallSlot > slot + 1:
|
|
|
|
raise newException(CatchableError,
|
|
|
|
"Past slot requested")
|
|
|
|
|
|
|
|
let epoch = slot.epoch
|
|
|
|
if epoch >= wallSlot.epoch and epoch - wallSlot.epoch > 1:
|
|
|
|
raise newException(CatchableError,
|
|
|
|
"Slot requested not in current or next wall-slot epoch")
|
|
|
|
|
|
|
|
if not verify_slot_signature(
|
2021-06-11 17:51:46 +00:00
|
|
|
getStateField(node.dag.headState.data, fork),
|
|
|
|
getStateField(node.dag.headState.data, genesis_validators_root),
|
2020-12-22 09:05:36 +00:00
|
|
|
slot, validator_pubkey, slot_signature):
|
|
|
|
raise newException(CatchableError,
|
|
|
|
"Invalid slot signature")
|
|
|
|
|
2020-12-23 12:59:04 +00:00
|
|
|
let
|
|
|
|
head = node.doChecksAndGetCurrentHead(epoch)
|
2021-06-01 11:13:40 +00:00
|
|
|
epochRef = node.dag.getEpochRef(head, epoch)
|
2021-10-20 09:16:48 +00:00
|
|
|
subnet_id = compute_subnet_for_attestation(
|
2021-05-10 07:13:36 +00:00
|
|
|
get_committee_count_per_slot(epochRef), slot, committee_index)
|
2021-01-19 17:44:03 +00:00
|
|
|
|
2021-10-18 09:11:44 +00:00
|
|
|
# The validator index here is invalid, but since JSON-RPC is on its way
|
|
|
|
# to deprecation, this is fine
|
|
|
|
node.registerDuty(
|
2021-10-20 09:16:48 +00:00
|
|
|
slot, subnet_id, 0.ValidatorIndex,
|
2021-10-18 09:11:44 +00:00
|
|
|
is_aggregator(epochRef, slot, committee_index, slot_signature))
|