2022-06-09 08:50:36 +00:00
|
|
|
# beacon_chain
|
|
|
|
# Copyright (c) 2021-2022 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.
|
|
|
|
|
2021-08-12 13:08:20 +00:00
|
|
|
import
|
2022-08-19 10:30:07 +00:00
|
|
|
std/[tables, os, sets, sequtils],
|
|
|
|
stew/[base10, results, byteutils],
|
|
|
|
bearssl/rand, chronos, presto, presto/client as presto_client,
|
|
|
|
chronicles, confutils, json_serialization/std/[options, net],
|
|
|
|
metrics, metrics/chronos_httpserver,
|
|
|
|
".."/spec/datatypes/[phase0, altair],
|
|
|
|
".."/spec/[eth2_merkleization, helpers, signatures, validator],
|
|
|
|
".."/spec/eth2_apis/[eth2_rest_serialization, rest_beacon_client],
|
|
|
|
".."/validators/[keystore_management, validator_pool, slashing_protection],
|
|
|
|
".."/[conf, beacon_clock, version, nimbus_binary_common]
|
|
|
|
|
|
|
|
export
|
|
|
|
os, sets, sequtils, chronos, presto, chronicles, confutils,
|
|
|
|
nimbus_binary_common, version, conf, options, tables, results, base10,
|
|
|
|
byteutils, presto_client, eth2_rest_serialization, rest_beacon_client,
|
|
|
|
phase0, altair, helpers, signatures, validator, eth2_merkleization,
|
|
|
|
beacon_clock, keystore_management, slashing_protection, validator_pool
|
2021-07-13 11:15:07 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
SYNC_TOLERANCE* = 4'u64
|
|
|
|
SLOT_LOOKAHEAD* = 1.seconds
|
|
|
|
HISTORICAL_DUTIES_EPOCHS* = 2'u64
|
|
|
|
TIME_DELAY_FROM_SLOT* = 79.milliseconds
|
|
|
|
SUBSCRIPTION_BUFFER_SLOTS* = 2'u64
|
|
|
|
|
2022-07-29 08:36:20 +00:00
|
|
|
DelayBuckets* = [-Inf, -4.0, -2.0, -1.0, -0.5, -0.1, -0.05,
|
|
|
|
0.05, 0.1, 0.5, 1.0, 2.0, 4.0, 8.0, Inf]
|
|
|
|
|
2021-07-13 11:15:07 +00:00
|
|
|
type
|
|
|
|
ServiceState* {.pure.} = enum
|
|
|
|
Initialized, Running, Error, Closing, Closed
|
|
|
|
|
|
|
|
BlockServiceEventRef* = ref object of RootObj
|
|
|
|
slot*: Slot
|
|
|
|
proposers*: seq[ValidatorPubKey]
|
|
|
|
|
|
|
|
ClientServiceRef* = ref object of RootObj
|
2022-07-13 14:43:57 +00:00
|
|
|
name*: string
|
2021-07-13 11:15:07 +00:00
|
|
|
state*: ServiceState
|
|
|
|
lifeFut*: Future[void]
|
|
|
|
client*: ValidatorClientRef
|
|
|
|
|
|
|
|
DutiesServiceRef* = ref object of ClientServiceRef
|
|
|
|
|
|
|
|
FallbackServiceRef* = ref object of ClientServiceRef
|
2022-07-13 14:43:57 +00:00
|
|
|
onlineEvent*: AsyncEvent
|
2021-07-13 11:15:07 +00:00
|
|
|
|
|
|
|
ForkServiceRef* = ref object of ClientServiceRef
|
|
|
|
|
|
|
|
AttestationServiceRef* = ref object of ClientServiceRef
|
|
|
|
|
|
|
|
BlockServiceRef* = ref object of ClientServiceRef
|
|
|
|
|
2022-05-10 10:03:40 +00:00
|
|
|
SyncCommitteeServiceRef* = ref object of ClientServiceRef
|
|
|
|
|
2022-07-21 16:54:07 +00:00
|
|
|
DoppelgangerServiceRef* = ref object of ClientServiceRef
|
|
|
|
enabled*: bool
|
|
|
|
|
2021-07-13 11:15:07 +00:00
|
|
|
DutyAndProof* = object
|
|
|
|
epoch*: Epoch
|
|
|
|
dependentRoot*: Eth2Digest
|
|
|
|
data*: RestAttesterDuty
|
|
|
|
slotSig*: Option[ValidatorSig]
|
|
|
|
|
2022-05-10 10:03:40 +00:00
|
|
|
SyncCommitteeDuty* = object
|
|
|
|
pubkey*: ValidatorPubKey
|
|
|
|
validator_index*: ValidatorIndex
|
|
|
|
validator_sync_committee_index*: IndexInSyncCommittee
|
|
|
|
|
|
|
|
SyncDutyAndProof* = object
|
|
|
|
epoch*: Epoch
|
|
|
|
data*: SyncCommitteeDuty
|
|
|
|
slotSig*: Option[ValidatorSig]
|
|
|
|
|
|
|
|
SyncCommitteeSubscriptionInfo* = object
|
|
|
|
validator_index*: ValidatorIndex
|
|
|
|
validator_sync_committee_indices*: seq[IndexInSyncCommittee]
|
|
|
|
|
2021-07-13 11:15:07 +00:00
|
|
|
ProposerTask* = object
|
|
|
|
duty*: RestProposerDuty
|
|
|
|
future*: Future[void]
|
|
|
|
|
|
|
|
ProposedData* = object
|
|
|
|
epoch*: Epoch
|
|
|
|
dependentRoot*: Eth2Digest
|
|
|
|
duties*: seq[ProposerTask]
|
|
|
|
|
2022-08-24 12:54:15 +00:00
|
|
|
BeaconNodeRole* {.pure.} = enum
|
|
|
|
Duty,
|
|
|
|
AttestationData, AttestationPublish,
|
|
|
|
AggrAttestationData, AggrAttestationPublish,
|
|
|
|
BlockProposalData, BlockProposalPublish,
|
|
|
|
SyncCommitteeData, SyncContributionPublish,
|
|
|
|
SyncSignaturePublish
|
|
|
|
|
2021-07-13 11:15:07 +00:00
|
|
|
BeaconNodeServer* = object
|
|
|
|
client*: RestClientRef
|
|
|
|
endpoint*: string
|
2021-10-20 09:58:38 +00:00
|
|
|
config*: Option[RestSpecVC]
|
2021-07-13 11:15:07 +00:00
|
|
|
ident*: Option[string]
|
2021-08-03 15:17:11 +00:00
|
|
|
genesis*: Option[RestGenesis]
|
2021-07-13 11:15:07 +00:00
|
|
|
syncInfo*: Option[RestSyncInfo]
|
|
|
|
status*: RestBeaconNodeStatus
|
2022-08-24 12:54:15 +00:00
|
|
|
roles*: set[BeaconNodeRole]
|
2022-08-19 12:45:20 +00:00
|
|
|
logIdent*: string
|
|
|
|
index*: int
|
2021-07-13 11:15:07 +00:00
|
|
|
|
|
|
|
EpochDuties* = object
|
|
|
|
duties*: Table[Epoch, DutyAndProof]
|
|
|
|
|
2022-05-10 10:03:40 +00:00
|
|
|
EpochSyncDuties* = object
|
|
|
|
duties*: Table[Epoch, SyncDutyAndProof]
|
|
|
|
|
2021-07-13 11:15:07 +00:00
|
|
|
RestBeaconNodeStatus* {.pure.} = enum
|
|
|
|
Uninitalized, Offline, Incompatible, NotSynced, Online
|
|
|
|
|
|
|
|
BeaconNodeServerRef* = ref BeaconNodeServer
|
|
|
|
|
|
|
|
AttesterMap* = Table[ValidatorPubKey, EpochDuties]
|
2022-05-10 10:03:40 +00:00
|
|
|
SyncCommitteeDutiesMap* = Table[ValidatorPubKey, EpochSyncDuties]
|
2021-07-13 11:15:07 +00:00
|
|
|
ProposerMap* = Table[Epoch, ProposedData]
|
|
|
|
|
2022-07-21 16:54:07 +00:00
|
|
|
DoppelgangerStatus* {.pure.} = enum
|
|
|
|
None, Checking, Passed
|
|
|
|
|
|
|
|
DoppelgangerAttempt* {.pure.} = enum
|
|
|
|
None, Failure, SuccessTrue, SuccessFalse
|
|
|
|
|
|
|
|
DoppelgangerState* = object
|
|
|
|
startEpoch*: Epoch
|
|
|
|
epochsCount*: uint64
|
|
|
|
lastAttempt*: DoppelgangerAttempt
|
|
|
|
status*: DoppelgangerStatus
|
|
|
|
|
|
|
|
DoppelgangerDetection* = object
|
|
|
|
startSlot*: Slot
|
|
|
|
validators*: Table[ValidatorIndex, DoppelgangerState]
|
|
|
|
|
2021-07-13 11:15:07 +00:00
|
|
|
ValidatorClient* = object
|
|
|
|
config*: ValidatorClientConf
|
2022-07-29 08:36:20 +00:00
|
|
|
metricsServer*: Option[MetricsHttpServerRef]
|
2021-07-13 11:15:07 +00:00
|
|
|
graffitiBytes*: GraffitiBytes
|
|
|
|
beaconNodes*: seq[BeaconNodeServerRef]
|
|
|
|
fallbackService*: FallbackServiceRef
|
|
|
|
forkService*: ForkServiceRef
|
|
|
|
dutiesService*: DutiesServiceRef
|
|
|
|
attestationService*: AttestationServiceRef
|
|
|
|
blockService*: BlockServiceRef
|
2022-05-10 10:03:40 +00:00
|
|
|
syncCommitteeService*: SyncCommitteeServiceRef
|
2022-07-21 16:54:07 +00:00
|
|
|
doppelgangerService*: DoppelgangerServiceRef
|
2022-07-13 14:43:57 +00:00
|
|
|
runSlotLoopFut*: Future[void]
|
|
|
|
sigintHandleFut*: Future[void]
|
|
|
|
sigtermHandleFut*: Future[void]
|
2022-08-19 10:30:07 +00:00
|
|
|
keymanagerHost*: ref KeymanagerHost
|
|
|
|
keymanagerServer*: RestServerRef
|
2021-07-13 11:15:07 +00:00
|
|
|
beaconClock*: BeaconClock
|
2022-07-21 16:54:07 +00:00
|
|
|
doppelgangerDetection*: DoppelgangerDetection
|
2022-08-19 10:30:07 +00:00
|
|
|
attachedValidators*: ref ValidatorPool
|
2022-02-16 11:31:23 +00:00
|
|
|
forks*: seq[Fork]
|
|
|
|
forksAvailable*: AsyncEvent
|
2022-07-14 21:11:25 +00:00
|
|
|
nodesAvailable*: AsyncEvent
|
|
|
|
gracefulExit*: AsyncEvent
|
2021-07-13 11:15:07 +00:00
|
|
|
attesters*: AttesterMap
|
|
|
|
proposers*: ProposerMap
|
2022-05-10 10:03:40 +00:00
|
|
|
syncCommitteeDuties*: SyncCommitteeDutiesMap
|
2021-08-03 15:17:11 +00:00
|
|
|
beaconGenesis*: RestGenesis
|
2021-07-13 11:15:07 +00:00
|
|
|
proposerTasks*: Table[Slot, seq[ProposerTask]]
|
2022-08-19 10:30:07 +00:00
|
|
|
rng*: ref HmacDrbgContext
|
2021-07-13 11:15:07 +00:00
|
|
|
|
|
|
|
ValidatorClientRef* = ref ValidatorClient
|
|
|
|
|
|
|
|
ValidatorClientError* = object of CatchableError
|
|
|
|
ValidatorApiError* = object of ValidatorClientError
|
|
|
|
|
|
|
|
const
|
|
|
|
DefaultDutyAndProof* = DutyAndProof(epoch: Epoch(0xFFFF_FFFF_FFFF_FFFF'u64))
|
2022-07-13 14:43:57 +00:00
|
|
|
DefaultSyncDutyAndProof* =
|
|
|
|
SyncDutyAndProof(epoch: Epoch(0xFFFF_FFFF_FFFF_FFFF'u64))
|
2021-07-13 11:15:07 +00:00
|
|
|
SlotDuration* = int64(SECONDS_PER_SLOT).seconds
|
2022-01-05 13:41:39 +00:00
|
|
|
OneThirdDuration* = int64(SECONDS_PER_SLOT).seconds div INTERVALS_PER_SLOT
|
2022-08-24 12:54:15 +00:00
|
|
|
AllBeaconNodeRoles* = {
|
|
|
|
BeaconNodeRole.Duty,
|
|
|
|
BeaconNodeRole.AttestationData,
|
|
|
|
BeaconNodeRole.AttestationPublish,
|
|
|
|
BeaconNodeRole.AggrAttestationData,
|
|
|
|
BeaconNodeRole.AggrAttestationPublish,
|
|
|
|
BeaconNodeRole.BlockProposalData,
|
|
|
|
BeaconNodeRole.BlockProposalPublish,
|
|
|
|
BeaconNodeRole.SyncCommitteeData,
|
|
|
|
BeaconNodeRole.SyncContributionPublish,
|
|
|
|
BeaconNodeRole.SyncSignaturePublish
|
|
|
|
}
|
|
|
|
|
|
|
|
proc `$`*(roles: set[BeaconNodeRole]): string =
|
|
|
|
if card(roles) > 0:
|
|
|
|
if roles != AllBeaconNodeRoles:
|
|
|
|
var res: seq[string]
|
|
|
|
if BeaconNodeRole.Duty in roles:
|
|
|
|
res.add("DuD")
|
|
|
|
if BeaconNodeRole.AttestationData in roles:
|
|
|
|
res.add("AtD")
|
|
|
|
if BeaconNodeRole.AttestationPublish in roles:
|
|
|
|
res.add("AtP")
|
|
|
|
if BeaconNodeRole.AggrAttestationData in roles:
|
|
|
|
res.add("AaD")
|
|
|
|
if BeaconNodeRole.AggrAttestationPublish in roles:
|
|
|
|
res.add("AaP")
|
|
|
|
if BeaconNodeRole.BlockProposalData in roles:
|
|
|
|
res.add("BpD")
|
|
|
|
if BeaconNodeRole.BlockProposalPublish in roles:
|
|
|
|
res.add("BpP")
|
|
|
|
if BeaconNodeRole.SyncCommitteeData in roles:
|
|
|
|
res.add("ScD")
|
|
|
|
if BeaconNodeRole.SyncContributionPublish in roles:
|
|
|
|
res.add("ScP")
|
|
|
|
if BeaconNodeRole.SyncSignaturePublish in roles:
|
|
|
|
res.add("SsP")
|
|
|
|
res.join(",")
|
|
|
|
else:
|
|
|
|
"{all}"
|
|
|
|
else:
|
|
|
|
"{}"
|
2021-07-13 11:15:07 +00:00
|
|
|
|
|
|
|
proc `$`*(bn: BeaconNodeServerRef): string =
|
|
|
|
if bn.ident.isSome():
|
2022-08-19 12:45:20 +00:00
|
|
|
bn.logIdent & "[" & bn.ident.get() & "]"
|
2021-07-13 11:15:07 +00:00
|
|
|
else:
|
2022-08-19 12:45:20 +00:00
|
|
|
bn.logIdent
|
2021-07-13 11:15:07 +00:00
|
|
|
|
|
|
|
chronicles.formatIt BeaconNodeServerRef:
|
|
|
|
$it
|
|
|
|
|
|
|
|
chronicles.expandIt(RestAttesterDuty):
|
|
|
|
pubkey = shortLog(it.pubkey)
|
|
|
|
slot = it.slot
|
|
|
|
validator_index = it.validator_index
|
|
|
|
committee_index = it.committee_index
|
|
|
|
committee_length = it.committee_length
|
|
|
|
committees_at_slot = it.committees_at_slot
|
|
|
|
validator_committee_index = it.validator_committee_index
|
|
|
|
|
|
|
|
proc stop*(csr: ClientServiceRef) {.async.} =
|
2022-07-14 21:11:25 +00:00
|
|
|
debug "Stopping service", service = csr.name
|
2021-07-13 11:15:07 +00:00
|
|
|
if csr.state == ServiceState.Running:
|
|
|
|
csr.state = ServiceState.Closing
|
|
|
|
if not(csr.lifeFut.finished()):
|
|
|
|
await csr.lifeFut.cancelAndWait()
|
|
|
|
csr.state = ServiceState.Closed
|
2022-07-14 21:11:25 +00:00
|
|
|
debug "Service stopped", service = csr.name
|
2021-07-13 11:15:07 +00:00
|
|
|
|
|
|
|
proc isDefault*(dap: DutyAndProof): bool =
|
|
|
|
dap.epoch == Epoch(0xFFFF_FFFF_FFFF_FFFF'u64)
|
|
|
|
|
2022-05-10 10:03:40 +00:00
|
|
|
proc isDefault*(sdap: SyncDutyAndProof): bool =
|
|
|
|
sdap.epoch == Epoch(0xFFFF_FFFF_FFFF_FFFF'u64)
|
|
|
|
|
2021-07-13 11:15:07 +00:00
|
|
|
proc isDefault*(prd: ProposedData): bool =
|
|
|
|
prd.epoch == Epoch(0xFFFF_FFFF_FFFF_FFFF'u64)
|
|
|
|
|
2022-08-19 12:45:20 +00:00
|
|
|
proc init*(t: typedesc[BeaconNodeServerRef], client: RestClientRef,
|
2022-08-24 12:54:15 +00:00
|
|
|
endpoint: string, index: int,
|
|
|
|
roles = AllBeaconNodeRoles): BeaconNodeServerRef =
|
2022-08-19 12:45:20 +00:00
|
|
|
BeaconNodeServerRef(
|
|
|
|
client: client,
|
|
|
|
endpoint: endpoint,
|
|
|
|
index: index,
|
2022-08-24 12:54:15 +00:00
|
|
|
roles: roles,
|
2022-08-19 12:45:20 +00:00
|
|
|
logIdent: client.address.hostname & ":" &
|
|
|
|
Base10.toString(client.address.port) &
|
2022-08-24 12:54:15 +00:00
|
|
|
"#" & Base10.toString(uint64(index))
|
2022-08-19 12:45:20 +00:00
|
|
|
)
|
|
|
|
|
2021-07-13 11:15:07 +00:00
|
|
|
proc init*(t: typedesc[DutyAndProof], epoch: Epoch, dependentRoot: Eth2Digest,
|
|
|
|
duty: RestAttesterDuty,
|
|
|
|
slotSig: Option[ValidatorSig]): DutyAndProof =
|
|
|
|
DutyAndProof(epoch: epoch, dependentRoot: dependentRoot, data: duty,
|
|
|
|
slotSig: slotSig)
|
|
|
|
|
2022-05-10 10:03:40 +00:00
|
|
|
proc init*(t: typedesc[SyncDutyAndProof], epoch: Epoch,
|
|
|
|
duty: SyncCommitteeDuty,
|
|
|
|
slotSig: Option[ValidatorSig]): SyncDutyAndProof =
|
|
|
|
SyncDutyAndProof(epoch: epoch, data: duty, slotSig: slotSig)
|
|
|
|
|
2021-07-13 11:15:07 +00:00
|
|
|
proc init*(t: typedesc[ProposedData], epoch: Epoch, dependentRoot: Eth2Digest,
|
2022-04-08 16:22:49 +00:00
|
|
|
data: openArray[ProposerTask]): ProposedData =
|
2021-07-13 11:15:07 +00:00
|
|
|
ProposedData(epoch: epoch, dependentRoot: dependentRoot, duties: @data)
|
|
|
|
|
|
|
|
proc getCurrentSlot*(vc: ValidatorClientRef): Option[Slot] =
|
|
|
|
let
|
|
|
|
wallTime = vc.beaconClock.now()
|
|
|
|
wallSlot = wallTime.toSlot()
|
|
|
|
|
|
|
|
if not(wallSlot.afterGenesis):
|
2022-01-11 10:01:54 +00:00
|
|
|
let checkGenesisTime = vc.beaconClock.fromNow(start_beacon_time(Slot(0)))
|
2021-07-13 11:15:07 +00:00
|
|
|
warn "Jump in time detected, something wrong with wallclock",
|
|
|
|
wall_time = wallTime, genesisIn = checkGenesisTime.offset
|
|
|
|
none[Slot]()
|
|
|
|
else:
|
|
|
|
some(wallSlot.slot)
|
|
|
|
|
|
|
|
proc getAttesterDutiesForSlot*(vc: ValidatorClientRef,
|
2021-07-15 08:17:32 +00:00
|
|
|
slot: Slot): seq[DutyAndProof] =
|
2022-05-10 10:03:40 +00:00
|
|
|
## Returns all `DutyAndProof` for the given `slot`.
|
2021-07-15 08:17:32 +00:00
|
|
|
var res: seq[DutyAndProof]
|
2021-07-13 11:15:07 +00:00
|
|
|
let epoch = slot.epoch()
|
2022-05-10 10:03:40 +00:00
|
|
|
for key, item in vc.attesters:
|
2021-07-13 11:15:07 +00:00
|
|
|
let duty = item.duties.getOrDefault(epoch, DefaultDutyAndProof)
|
|
|
|
if not(duty.isDefault()):
|
|
|
|
if duty.data.slot == slot:
|
2021-07-15 08:17:32 +00:00
|
|
|
res.add(duty)
|
2021-07-13 11:15:07 +00:00
|
|
|
res
|
|
|
|
|
2022-05-10 10:03:40 +00:00
|
|
|
proc getSyncCommitteeDutiesForSlot*(vc: ValidatorClientRef,
|
|
|
|
slot: Slot): seq[SyncDutyAndProof] =
|
|
|
|
## Returns all `SyncDutyAndProof` for the given `slot`.
|
|
|
|
var res: seq[SyncDutyAndProof]
|
|
|
|
let epoch = slot.epoch()
|
|
|
|
for key, item in mpairs(vc.syncCommitteeDuties):
|
|
|
|
item.duties.withValue(epoch, duty):
|
|
|
|
res.add(duty[])
|
|
|
|
res
|
|
|
|
|
|
|
|
proc removeOldSyncPeriodDuties*(vc: ValidatorClientRef,
|
|
|
|
slot: Slot) =
|
|
|
|
if slot.is_sync_committee_period:
|
|
|
|
let epoch = slot.epoch()
|
|
|
|
var prunedDuties = SyncCommitteeDutiesMap()
|
|
|
|
for key, item in vc.syncCommitteeDuties:
|
|
|
|
var curPeriodDuties = EpochSyncDuties()
|
|
|
|
for epochKey, epochDuty in item.duties:
|
|
|
|
if epochKey >= epoch:
|
|
|
|
curPeriodDuties.duties[epochKey] = epochDuty
|
|
|
|
prunedDuties[key] = curPeriodDuties
|
|
|
|
vc.syncCommitteeDuties = prunedDuties
|
|
|
|
|
2021-07-13 11:15:07 +00:00
|
|
|
proc getDurationToNextAttestation*(vc: ValidatorClientRef,
|
|
|
|
slot: Slot): string =
|
2022-01-11 10:01:54 +00:00
|
|
|
var minSlot = FAR_FUTURE_SLOT
|
2021-07-13 11:15:07 +00:00
|
|
|
let currentEpoch = slot.epoch()
|
|
|
|
for epoch in [currentEpoch, currentEpoch + 1'u64]:
|
2022-05-10 10:03:40 +00:00
|
|
|
for key, item in vc.attesters:
|
2021-07-13 11:15:07 +00:00
|
|
|
let duty = item.duties.getOrDefault(epoch, DefaultDutyAndProof)
|
|
|
|
if not(duty.isDefault()):
|
2022-01-11 10:01:54 +00:00
|
|
|
let dutySlotTime = duty.data.slot
|
2022-07-13 14:43:57 +00:00
|
|
|
if (duty.data.slot < minSlot) and (duty.data.slot >= slot):
|
2022-01-11 10:01:54 +00:00
|
|
|
minSlot = duty.data.slot
|
|
|
|
if minSlot != FAR_FUTURE_SLOT:
|
2021-07-13 11:15:07 +00:00
|
|
|
break
|
2022-01-11 10:01:54 +00:00
|
|
|
|
|
|
|
if minSlot == FAR_FUTURE_SLOT:
|
2021-07-13 11:15:07 +00:00
|
|
|
"<unknown>"
|
|
|
|
else:
|
2022-01-11 10:01:54 +00:00
|
|
|
$(minSlot.attestation_deadline() - slot.start_beacon_time())
|
2021-07-13 11:15:07 +00:00
|
|
|
|
|
|
|
proc getDurationToNextBlock*(vc: ValidatorClientRef, slot: Slot): string =
|
2022-01-11 10:01:54 +00:00
|
|
|
var minSlot = FAR_FUTURE_SLOT
|
2021-07-13 11:15:07 +00:00
|
|
|
let currentEpoch = slot.epoch()
|
|
|
|
for epoch in [currentEpoch, currentEpoch + 1'u64]:
|
|
|
|
let data = vc.proposers.getOrDefault(epoch)
|
|
|
|
if not(data.isDefault()):
|
|
|
|
for item in data.duties:
|
2022-08-19 10:30:07 +00:00
|
|
|
if item.duty.pubkey in vc.attachedValidators[]:
|
2022-07-13 14:43:57 +00:00
|
|
|
if (item.duty.slot < minSlot) and (item.duty.slot >= slot):
|
2022-01-11 10:01:54 +00:00
|
|
|
minSlot = item.duty.slot
|
|
|
|
if minSlot != FAR_FUTURE_SLOT:
|
2021-07-13 11:15:07 +00:00
|
|
|
break
|
2022-01-11 10:01:54 +00:00
|
|
|
if minSlot == FAR_FUTURE_SLOT:
|
2021-07-13 11:15:07 +00:00
|
|
|
"<unknown>"
|
|
|
|
else:
|
2022-01-11 10:01:54 +00:00
|
|
|
$(minSlot.block_deadline() - slot.start_beacon_time())
|
2021-07-13 11:15:07 +00:00
|
|
|
|
|
|
|
iterator attesterDutiesForEpoch*(vc: ValidatorClientRef,
|
|
|
|
epoch: Epoch): DutyAndProof =
|
2022-05-10 10:03:40 +00:00
|
|
|
for key, item in vc.attesters:
|
2021-07-13 11:15:07 +00:00
|
|
|
let epochDuties = item.duties.getOrDefault(epoch)
|
|
|
|
if not(isDefault(epochDuties)):
|
|
|
|
yield epochDuties
|
|
|
|
|
2022-05-10 10:03:40 +00:00
|
|
|
proc syncMembersSubscriptionInfoForEpoch*(
|
|
|
|
vc: ValidatorClientRef,
|
|
|
|
epoch: Epoch): seq[SyncCommitteeSubscriptionInfo] =
|
|
|
|
var res: seq[SyncCommitteeSubscriptionInfo]
|
|
|
|
for key, item in mpairs(vc.syncCommitteeDuties):
|
|
|
|
var cur: SyncCommitteeSubscriptionInfo
|
|
|
|
var initialized = false
|
|
|
|
|
|
|
|
item.duties.withValue(epoch, epochDuties):
|
|
|
|
if not initialized:
|
|
|
|
cur.validator_index = epochDuties.data.validator_index
|
|
|
|
initialized = true
|
|
|
|
cur.validator_sync_committee_indices.add(
|
|
|
|
epochDuties.data.validator_sync_committee_index)
|
|
|
|
|
|
|
|
if initialized:
|
|
|
|
res.add cur
|
|
|
|
|
|
|
|
res
|
|
|
|
|
2022-01-11 10:01:54 +00:00
|
|
|
proc getDelay*(vc: ValidatorClientRef, deadline: BeaconTime): TimeDiff =
|
|
|
|
vc.beaconClock.now() - deadline
|
2021-07-19 14:31:02 +00:00
|
|
|
|
|
|
|
proc getValidator*(vc: ValidatorClientRef,
|
2022-04-08 16:22:49 +00:00
|
|
|
key: ValidatorPubKey): Option[AttachedValidator] =
|
2022-08-19 10:30:07 +00:00
|
|
|
let validator = vc.attachedValidators[].getValidator(key)
|
2021-07-19 14:31:02 +00:00
|
|
|
if isNil(validator):
|
|
|
|
warn "Validator not in pool anymore", validator = shortLog(validator)
|
|
|
|
none[AttachedValidator]()
|
|
|
|
else:
|
|
|
|
if validator.index.isNone():
|
|
|
|
warn "Validator index is missing", validator = shortLog(validator)
|
|
|
|
none[AttachedValidator]()
|
|
|
|
else:
|
|
|
|
some(validator)
|
2022-02-16 11:31:23 +00:00
|
|
|
|
|
|
|
proc forkAtEpoch*(vc: ValidatorClientRef, epoch: Epoch): Fork =
|
|
|
|
# If schedule is present, it MUST not be empty.
|
|
|
|
doAssert(len(vc.forks) > 0)
|
|
|
|
var res: Fork
|
|
|
|
for item in vc.forks:
|
|
|
|
if item.epoch <= epoch:
|
|
|
|
res = item
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
res
|
2022-05-10 10:03:40 +00:00
|
|
|
|
2022-07-13 14:43:57 +00:00
|
|
|
proc getSubcommitteeIndex*(index: IndexInSyncCommittee): SyncSubcommitteeIndex =
|
|
|
|
SyncSubcommitteeIndex(uint16(index) div SYNC_SUBCOMMITTEE_SIZE)
|
2022-07-21 16:54:07 +00:00
|
|
|
|
|
|
|
proc currentSlot*(vc: ValidatorClientRef): Slot =
|
|
|
|
vc.beaconClock.now().slotOrZero()
|
|
|
|
|
|
|
|
proc addDoppelganger*(vc: ValidatorClientRef, validator: AttachedValidator) =
|
|
|
|
logScope:
|
|
|
|
validator = shortLog(validator)
|
|
|
|
|
|
|
|
if vc.config.doppelgangerDetection:
|
|
|
|
let
|
|
|
|
vindex = validator.index.get()
|
|
|
|
startEpoch = vc.currentSlot().epoch()
|
|
|
|
state =
|
|
|
|
if (startEpoch == GENESIS_EPOCH) and
|
|
|
|
(validator.startSlot == GENESIS_SLOT):
|
|
|
|
DoppelgangerState(startEpoch: startEpoch, epochsCount: 0'u64,
|
|
|
|
lastAttempt: DoppelgangerAttempt.None,
|
|
|
|
status: DoppelgangerStatus.Passed)
|
|
|
|
else:
|
|
|
|
DoppelgangerState(startEpoch: startEpoch, epochsCount: 0'u64,
|
|
|
|
lastAttempt: DoppelgangerAttempt.None,
|
|
|
|
status: DoppelgangerStatus.Checking)
|
|
|
|
res = vc.doppelgangerDetection.validators.hasKeyOrPut(vindex, state)
|
|
|
|
|
|
|
|
if res:
|
|
|
|
warn "Validator is already in doppelganger table",
|
|
|
|
validator_index = vindex, start_epoch = startEpoch,
|
|
|
|
start_slot = validator.startSlot
|
|
|
|
else:
|
|
|
|
if state.status == DoppelgangerStatus.Checking:
|
|
|
|
info "Doppelganger protection activated", validator_index = vindex,
|
|
|
|
start_epoch = startEpoch, start_slot = validator.startSlot
|
|
|
|
else:
|
|
|
|
info "Doppelganger protection skipped", validator_index = vindex,
|
|
|
|
start_epoch = startEpoch, start_slot = validator.startSlot
|
|
|
|
|
|
|
|
proc removeDoppelganger*(vc: ValidatorClientRef, index: ValidatorIndex) =
|
|
|
|
if vc.config.doppelgangerDetection:
|
|
|
|
var state: DoppelgangerState
|
|
|
|
# We do not care about race condition, when validator is not yet added to
|
|
|
|
# the doppelganger's table, but it should be removed.
|
|
|
|
discard vc.doppelgangerDetection.validators.pop(index, state)
|
|
|
|
|
|
|
|
proc addValidator*(vc: ValidatorClientRef, keystore: KeystoreData) =
|
2022-09-17 05:30:07 +00:00
|
|
|
let
|
|
|
|
slot = vc.currentSlot()
|
|
|
|
feeRecipient = vc.config.validatorsDir.getSuggestedFeeRecipient(
|
|
|
|
keystore.pubkey, vc.config.defaultFeeRecipient).valueOr(
|
|
|
|
vc.config.defaultFeeRecipient)
|
2022-07-21 16:54:07 +00:00
|
|
|
case keystore.kind
|
|
|
|
of KeystoreKind.Local:
|
2022-08-19 21:51:30 +00:00
|
|
|
vc.attachedValidators[].addLocalValidator(keystore, Opt.none ValidatorIndex,
|
2022-09-17 05:30:07 +00:00
|
|
|
feeRecipient, slot)
|
2022-07-21 16:54:07 +00:00
|
|
|
of KeystoreKind.Remote:
|
|
|
|
let
|
|
|
|
httpFlags =
|
|
|
|
block:
|
|
|
|
var res: set[HttpClientFlag]
|
|
|
|
if RemoteKeystoreFlag.IgnoreSSLVerification in keystore.flags:
|
|
|
|
res.incl({HttpClientFlag.NoVerifyHost,
|
|
|
|
HttpClientFlag.NoVerifyServerName})
|
|
|
|
res
|
|
|
|
prestoFlags = {RestClientFlag.CommaSeparatedArray}
|
|
|
|
clients =
|
|
|
|
block:
|
|
|
|
var res: seq[(RestClientRef, RemoteSignerInfo)]
|
|
|
|
for remote in keystore.remotes:
|
|
|
|
let client = RestClientRef.new($remote.url, prestoFlags,
|
|
|
|
httpFlags)
|
|
|
|
if client.isErr():
|
|
|
|
warn "Unable to resolve distributed signer address",
|
|
|
|
remote_url = $remote.url, validator = $remote.pubkey
|
|
|
|
else:
|
|
|
|
res.add((client.get(), remote))
|
|
|
|
res
|
|
|
|
if len(clients) > 0:
|
2022-08-19 10:30:07 +00:00
|
|
|
vc.attachedValidators[].addRemoteValidator(keystore, clients,
|
2022-09-17 05:30:07 +00:00
|
|
|
Opt.none ValidatorIndex,
|
|
|
|
feeRecipient, slot)
|
2022-07-21 16:54:07 +00:00
|
|
|
else:
|
|
|
|
warn "Unable to initialize remote validator",
|
|
|
|
validator = $keystore.pubkey
|
|
|
|
|
|
|
|
proc removeValidator*(vc: ValidatorClientRef,
|
|
|
|
pubkey: ValidatorPubKey) {.async.} =
|
2022-08-19 10:30:07 +00:00
|
|
|
let validator = vc.attachedValidators[].getValidator(pubkey)
|
2022-07-21 16:54:07 +00:00
|
|
|
if not(isNil(validator)):
|
|
|
|
if vc.config.doppelgangerDetection:
|
|
|
|
if validator.index.isSome():
|
|
|
|
vc.removeDoppelganger(validator.index.get())
|
|
|
|
case validator.kind
|
|
|
|
of ValidatorKind.Local:
|
|
|
|
discard
|
|
|
|
of ValidatorKind.Remote:
|
|
|
|
# We must close all the REST clients running for the remote validator.
|
|
|
|
let pending =
|
|
|
|
block:
|
|
|
|
var res: seq[Future[void]]
|
|
|
|
for item in validator.clients:
|
|
|
|
res.add(item[0].closeWait())
|
|
|
|
res
|
|
|
|
await allFutures(pending)
|
|
|
|
# Remove validator from ValidatorPool.
|
2022-08-19 10:30:07 +00:00
|
|
|
vc.attachedValidators[].removeValidator(pubkey)
|
2022-07-21 16:54:07 +00:00
|
|
|
|
|
|
|
proc doppelgangerCheck*(vc: ValidatorClientRef,
|
|
|
|
validator: AttachedValidator): bool =
|
|
|
|
if vc.config.doppelgangerDetection:
|
|
|
|
if validator.index.isNone():
|
|
|
|
return false
|
|
|
|
if validator.startSlot > GENESIS_SLOT:
|
|
|
|
let
|
|
|
|
vindex = validator.index.get()
|
|
|
|
default = DoppelgangerState(status: DoppelgangerStatus.None)
|
|
|
|
state = vc.doppelgangerDetection.validators.getOrDefault(vindex,
|
|
|
|
default)
|
|
|
|
state.status == DoppelgangerStatus.Passed
|
|
|
|
else:
|
|
|
|
true
|
|
|
|
else:
|
|
|
|
true
|