2022-07-29 08:36:20 +00:00
|
|
|
# beacon_chain
|
2024-01-06 06:18:28 +00:00
|
|
|
# Copyright (c) 2021-2024 Status Research & Development GmbH
|
2022-07-29 08:36:20 +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.
|
|
|
|
|
2024-02-26 08:48:07 +00:00
|
|
|
{.push raises: [].}
|
|
|
|
|
2021-07-13 11:15:07 +00:00
|
|
|
import std/[sets, sequtils]
|
2023-11-08 12:03:51 +00:00
|
|
|
import chronicles, metrics
|
2024-01-06 06:18:28 +00:00
|
|
|
import "."/[common, api, block_service, selection_proofs]
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2022-07-14 21:11:25 +00:00
|
|
|
const
|
|
|
|
ServiceName = "duties_service"
|
2022-11-17 11:09:20 +00:00
|
|
|
SUBSCRIPTION_LOOKAHEAD_EPOCHS* = 4'u64
|
2024-10-08 03:37:00 +00:00
|
|
|
AGGREGATION_PRE_COMPUTE_SLOTS* = 1'u64
|
|
|
|
# We do pre-computation for current and next slot only. Pre-computation
|
|
|
|
# is good for low number of validators, but with big count of validators
|
|
|
|
# number of remote signature requests could overload remote signature
|
|
|
|
# service.
|
2022-07-14 21:11:25 +00:00
|
|
|
|
|
|
|
logScope: service = ServiceName
|
2021-07-13 11:15:07 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
DutiesServiceLoop* = enum
|
2022-10-21 14:53:30 +00:00
|
|
|
AttesterLoop, ProposerLoop, IndicesLoop, SyncCommitteeLoop,
|
2023-11-06 14:40:44 +00:00
|
|
|
ProposerPreparationLoop, ValidatorRegisterLoop, DynamicValidatorsLoop,
|
|
|
|
SlashPruningLoop
|
2021-07-13 11:15:07 +00:00
|
|
|
|
|
|
|
chronicles.formatIt(DutiesServiceLoop):
|
|
|
|
case it
|
|
|
|
of AttesterLoop: "attester_loop"
|
|
|
|
of ProposerLoop: "proposer_loop"
|
|
|
|
of IndicesLoop: "index_loop"
|
2022-05-10 10:03:40 +00:00
|
|
|
of SyncCommitteeLoop: "sync_committee_loop"
|
2022-10-21 14:53:30 +00:00
|
|
|
of ProposerPreparationLoop: "proposer_prepare_loop"
|
2022-10-29 09:00:51 +00:00
|
|
|
of ValidatorRegisterLoop: "validator_register_loop"
|
2023-09-04 19:14:58 +00:00
|
|
|
of DynamicValidatorsLoop: "dynamic_validators_loop"
|
2023-11-06 14:40:44 +00:00
|
|
|
of SlashPruningLoop: "slashing_pruning_loop"
|
2021-07-13 11:15:07 +00:00
|
|
|
|
|
|
|
proc checkDuty(duty: RestAttesterDuty): bool =
|
|
|
|
(duty.committee_length <= MAX_VALIDATORS_PER_COMMITTEE) and
|
2022-06-29 16:53:59 +00:00
|
|
|
(uint64(duty.committee_index) < MAX_COMMITTEES_PER_SLOT) and
|
|
|
|
(uint64(duty.validator_committee_index) < duty.committee_length) and
|
2021-07-13 11:15:07 +00:00
|
|
|
(uint64(duty.validator_index) <= VALIDATOR_REGISTRY_LIMIT)
|
|
|
|
|
2022-05-10 10:03:40 +00:00
|
|
|
proc checkSyncDuty(duty: RestSyncCommitteeDuty): bool =
|
|
|
|
uint64(duty.validator_index) <= VALIDATOR_REGISTRY_LIMIT
|
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
proc pollForValidatorIndices*(
|
|
|
|
service: DutiesServiceRef
|
|
|
|
) {.async: (raises: [CancelledError]).} =
|
2023-04-17 21:31:54 +00:00
|
|
|
let vc = service.client
|
|
|
|
|
2021-07-13 11:15:07 +00:00
|
|
|
let validatorIdents =
|
|
|
|
block:
|
|
|
|
var res: seq[ValidatorIdent]
|
2022-08-19 10:30:07 +00:00
|
|
|
for validator in vc.attachedValidators[].items():
|
2022-12-09 16:05:55 +00:00
|
|
|
if validator.needsUpdate():
|
2021-07-13 11:15:07 +00:00
|
|
|
res.add(ValidatorIdent.init(validator.pubkey))
|
|
|
|
res
|
|
|
|
|
2023-06-20 16:41:20 +00:00
|
|
|
let start = Moment.now()
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2023-06-20 16:41:20 +00:00
|
|
|
var validators: seq[RestValidator]
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2023-06-20 16:41:20 +00:00
|
|
|
for idents in chunks(validatorIdents, ClientMaximumValidatorIds):
|
2021-07-13 11:15:07 +00:00
|
|
|
let res =
|
|
|
|
try:
|
2022-09-29 07:57:14 +00:00
|
|
|
await vc.getValidators(idents, ApiStrategyKind.First)
|
2023-02-23 00:11:00 +00:00
|
|
|
except ValidatorApiError as exc:
|
2023-03-03 20:20:01 +00:00
|
|
|
warn "Unable to get head state's validator information",
|
2023-02-23 00:11:00 +00:00
|
|
|
reason = exc.getFailureReason()
|
2021-07-19 14:31:02 +00:00
|
|
|
return
|
2022-07-14 21:11:25 +00:00
|
|
|
except CancelledError as exc:
|
|
|
|
debug "Validator's indices processing was interrupted"
|
|
|
|
raise exc
|
2024-10-16 17:20:39 +00:00
|
|
|
|
2021-07-13 11:15:07 +00:00
|
|
|
for item in res:
|
|
|
|
validators.add(item)
|
|
|
|
|
2022-10-14 12:19:17 +00:00
|
|
|
var
|
|
|
|
missing: seq[string]
|
|
|
|
updated: seq[string]
|
|
|
|
list: seq[AttachedValidator]
|
|
|
|
|
2021-07-13 11:15:07 +00:00
|
|
|
for item in validators:
|
2023-02-20 11:28:56 +00:00
|
|
|
let validator = vc.attachedValidators[].getValidator(item.validator.pubkey)
|
|
|
|
if validator.isNone():
|
2022-10-14 12:19:17 +00:00
|
|
|
missing.add(validatorLog(item.validator.pubkey, item.index))
|
2021-07-13 11:15:07 +00:00
|
|
|
else:
|
2024-06-11 19:38:16 +00:00
|
|
|
vc.attachedValidators[].updateValidator(validator.get(),
|
|
|
|
Opt.some ValidatorAndIndex(index: item.index,
|
|
|
|
validator: item.validator))
|
2022-10-14 12:19:17 +00:00
|
|
|
updated.add(validatorLog(item.validator.pubkey, item.index))
|
2023-02-20 11:28:56 +00:00
|
|
|
list.add(validator.get())
|
2022-10-14 12:19:17 +00:00
|
|
|
|
|
|
|
if len(updated) > 0:
|
2023-02-07 14:53:36 +00:00
|
|
|
info "Validator indices updated",
|
|
|
|
pending = len(validatorIdents) - len(updated),
|
|
|
|
missing = len(missing),
|
2023-06-20 16:41:20 +00:00
|
|
|
updated = len(updated),
|
|
|
|
elapsed_time = (Moment.now() - start)
|
2022-10-14 12:19:17 +00:00
|
|
|
trace "Validator indices update dump", missing_validators = missing,
|
|
|
|
updated_validators = updated
|
2022-10-21 14:53:30 +00:00
|
|
|
vc.indicesAvailable.fire()
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
proc pollForAttesterDuties*(
|
|
|
|
service: DutiesServiceRef,
|
|
|
|
epoch: Epoch
|
|
|
|
): Future[int] {.async: (raises: [CancelledError]).} =
|
2023-08-15 17:00:27 +00:00
|
|
|
var currentRoot: Opt[Eth2Digest]
|
2021-07-13 11:15:07 +00:00
|
|
|
let
|
2023-08-15 17:00:27 +00:00
|
|
|
vc = service.client
|
|
|
|
indices = toSeq(vc.attachedValidators[].indices())
|
|
|
|
relevantDuties =
|
2024-10-09 01:00:06 +00:00
|
|
|
if len(indices) > 0:
|
2023-08-15 17:00:27 +00:00
|
|
|
var duties: seq[RestAttesterDuty]
|
|
|
|
block mainLoop:
|
|
|
|
while true:
|
|
|
|
block innerLoop:
|
|
|
|
for chunk in indices.chunks(DutiesMaximumValidatorIds):
|
|
|
|
let res =
|
|
|
|
try:
|
|
|
|
await vc.getAttesterDuties(epoch, chunk,
|
|
|
|
ApiStrategyKind.First)
|
|
|
|
except ValidatorApiError as exc:
|
|
|
|
warn "Unable to get attester duties", epoch = epoch,
|
|
|
|
reason = exc.getFailureReason()
|
|
|
|
return 0
|
|
|
|
except CancelledError as exc:
|
|
|
|
debug "Attester duties processing was interrupted"
|
|
|
|
raise exc
|
2024-10-16 17:20:39 +00:00
|
|
|
|
2023-08-15 17:00:27 +00:00
|
|
|
if currentRoot.isNone():
|
|
|
|
# First request
|
|
|
|
currentRoot = Opt.some(res.dependent_root)
|
|
|
|
else:
|
|
|
|
if currentRoot.get() != res.dependent_root:
|
|
|
|
# `dependent_root` must be equal for all requests/response,
|
|
|
|
# if it got changed it means that some reorg was happened in
|
|
|
|
# beacon node and we should re-request all queries again.
|
|
|
|
duties.setLen(0)
|
|
|
|
currentRoot = Opt.none(Eth2Digest)
|
|
|
|
break innerLoop
|
|
|
|
|
|
|
|
for duty in res.data:
|
|
|
|
if checkDuty(duty) and
|
|
|
|
(duty.pubkey in vc.attachedValidators[]):
|
|
|
|
duties.add(duty)
|
|
|
|
break mainLoop
|
|
|
|
duties
|
2024-10-09 01:00:06 +00:00
|
|
|
else:
|
|
|
|
default(seq[RestAttesterDuty])
|
2023-08-15 17:00:27 +00:00
|
|
|
|
|
|
|
template checkReorg(a, b: untyped): bool =
|
|
|
|
not(a.dependentRoot == b.get())
|
2021-07-13 11:15:07 +00:00
|
|
|
|
|
|
|
let addOrReplaceItems =
|
|
|
|
block:
|
|
|
|
var alreadyWarned = false
|
|
|
|
var res: seq[tuple[epoch: Epoch, duty: RestAttesterDuty]]
|
|
|
|
for duty in relevantDuties:
|
2023-08-15 17:00:27 +00:00
|
|
|
var dutyFound = false
|
|
|
|
vc.attesters.withValue(duty.pubkey, map):
|
|
|
|
map[].duties.withValue(epoch, epochDuty):
|
|
|
|
dutyFound = true
|
|
|
|
if checkReorg(epochDuty[], currentRoot):
|
|
|
|
if not(alreadyWarned):
|
|
|
|
info "Attester duties re-organization",
|
|
|
|
prior_dependent_root = epochDuty[].dependentRoot,
|
|
|
|
dependent_root = currentRoot.get()
|
|
|
|
alreadyWarned = true
|
|
|
|
res.add((epoch, duty))
|
|
|
|
if not(dutyFound):
|
2021-07-13 11:15:07 +00:00
|
|
|
info "Received new attester duty", duty, epoch = epoch,
|
2023-08-15 17:00:27 +00:00
|
|
|
dependent_root = currentRoot.get()
|
2021-07-13 11:15:07 +00:00
|
|
|
res.add((epoch, duty))
|
|
|
|
res
|
|
|
|
|
2023-08-15 17:00:27 +00:00
|
|
|
for item in addOrReplaceItems:
|
|
|
|
let dap = DutyAndProof.init(item.epoch, currentRoot.get(), item.duty,
|
|
|
|
Opt.none(ValidatorSig))
|
|
|
|
vc.attesters.mgetOrPut(dap.data.pubkey,
|
|
|
|
default(EpochDuties)).duties[dap.epoch] = dap
|
2021-07-13 11:15:07 +00:00
|
|
|
return len(addOrReplaceItems)
|
|
|
|
|
2023-04-17 21:31:54 +00:00
|
|
|
proc pruneSyncCommitteeDuties*(service: DutiesServiceRef, slot: Slot) =
|
|
|
|
let vc = service.client
|
2022-10-21 14:53:30 +00:00
|
|
|
if slot.is_sync_committee_period():
|
|
|
|
var newSyncCommitteeDuties: SyncCommitteeDutiesMap
|
2023-08-15 17:00:27 +00:00
|
|
|
let period = slot.sync_committee_period()
|
2022-10-21 14:53:30 +00:00
|
|
|
for key, item in vc.syncCommitteeDuties:
|
2023-08-15 17:00:27 +00:00
|
|
|
var currentPeriodDuties = SyncPeriodDuties()
|
|
|
|
for periodKey, periodDuty in item.duties:
|
|
|
|
if periodKey >= period:
|
|
|
|
currentPeriodDuties.duties[periodKey] = periodDuty
|
2022-10-21 14:53:30 +00:00
|
|
|
newSyncCommitteeDuties[key] = currentPeriodDuties
|
|
|
|
vc.syncCommitteeDuties = newSyncCommitteeDuties
|
|
|
|
|
2023-08-15 17:00:27 +00:00
|
|
|
proc pruneSyncCommitteeSelectionProofs*(service: DutiesServiceRef, slot: Slot) =
|
2022-05-10 10:03:40 +00:00
|
|
|
let
|
2023-08-15 17:00:27 +00:00
|
|
|
vc = service.client
|
|
|
|
slotEpoch = slot.epoch()
|
|
|
|
var res: seq[Epoch]
|
|
|
|
for epoch in vc.syncCommitteeProofs.keys():
|
|
|
|
if epoch < slotEpoch: res.add(epoch)
|
|
|
|
for epoch in res:
|
|
|
|
vc.syncCommitteeProofs.del(epoch)
|
|
|
|
|
|
|
|
proc pollForSyncCommitteeDuties*(
|
2024-10-16 17:20:39 +00:00
|
|
|
service: DutiesServiceRef,
|
|
|
|
period: SyncCommitteePeriod
|
|
|
|
): Future[int] {.async: (raises: [CancelledError]).} =
|
2023-08-15 17:00:27 +00:00
|
|
|
let
|
|
|
|
vc = service.client
|
|
|
|
indices = toSeq(vc.attachedValidators[].indices())
|
2024-10-16 17:20:39 +00:00
|
|
|
epoch = max(period.start_epoch(), vc.getAltairEpoch())
|
2022-05-10 10:03:40 +00:00
|
|
|
relevantDuties =
|
|
|
|
block:
|
2023-08-15 17:00:27 +00:00
|
|
|
var duties: seq[RestSyncCommitteeDuty]
|
|
|
|
# We use `DutiesMaximumValidatorIds` here because validator ids are sent
|
|
|
|
# in HTTP request body and NOT in HTTP request headers.
|
|
|
|
for chunk in indices.chunks(DutiesMaximumValidatorIds):
|
|
|
|
let res =
|
|
|
|
try:
|
|
|
|
await vc.getSyncCommitteeDuties(epoch, chunk,
|
|
|
|
ApiStrategyKind.First)
|
|
|
|
except ValidatorApiError as exc:
|
|
|
|
warn "Unable to get sync committee duties",
|
|
|
|
period = period, epoch = epoch,
|
|
|
|
reason = exc.getFailureReason()
|
|
|
|
return 0
|
|
|
|
except CancelledError as exc:
|
|
|
|
debug "Sync committee duties processing was interrupted",
|
|
|
|
period = period, epoch = epoch
|
|
|
|
raise exc
|
|
|
|
|
|
|
|
for duty in res.data:
|
|
|
|
if checkSyncDuty(duty) and (duty.pubkey in vc.attachedValidators[]):
|
|
|
|
duties.add(duty)
|
|
|
|
|
|
|
|
duties
|
|
|
|
|
|
|
|
template checkReorg(a, b: untyped): bool =
|
|
|
|
not(compareUnsorted(a.validator_sync_committee_indices,
|
|
|
|
b.validator_sync_committee_indices))
|
2022-05-10 10:03:40 +00:00
|
|
|
|
|
|
|
let addOrReplaceItems =
|
|
|
|
block:
|
2023-08-15 17:00:27 +00:00
|
|
|
var
|
|
|
|
alreadyWarned = false
|
|
|
|
res: seq[tuple[period: SyncCommitteePeriod,
|
|
|
|
duty: RestSyncCommitteeDuty]]
|
2022-05-10 10:03:40 +00:00
|
|
|
for duty in relevantDuties:
|
2022-11-24 07:46:35 +00:00
|
|
|
var dutyFound = false
|
|
|
|
vc.syncCommitteeDuties.withValue(duty.pubkey, map):
|
2023-08-15 17:00:27 +00:00
|
|
|
map[].duties.withValue(period, periodDuty):
|
|
|
|
dutyFound = true
|
|
|
|
if checkReorg(periodDuty[], duty):
|
|
|
|
if not(alreadyWarned):
|
|
|
|
info "Sync committee duties re-organization"
|
|
|
|
alreadyWarned = true
|
|
|
|
res.add((period, duty))
|
|
|
|
if not(dutyFound):
|
|
|
|
res.add((period, duty))
|
|
|
|
info "Received new sync committee duty", duty, period
|
2022-11-24 07:46:35 +00:00
|
|
|
res
|
2022-05-10 10:03:40 +00:00
|
|
|
|
2023-08-15 17:00:27 +00:00
|
|
|
for item in addOrReplaceItems:
|
2024-03-22 02:37:44 +00:00
|
|
|
vc.syncCommitteeDuties.mgetOrPut(
|
|
|
|
item.duty.pubkey, default(SyncPeriodDuties)).duties[item.period] =
|
|
|
|
item.duty
|
2023-08-15 17:00:27 +00:00
|
|
|
len(addOrReplaceItems)
|
2022-05-10 10:03:40 +00:00
|
|
|
|
2023-04-17 21:31:54 +00:00
|
|
|
proc pruneAttesterDuties(service: DutiesServiceRef, epoch: Epoch) =
|
|
|
|
let vc = service.client
|
2021-07-13 11:15:07 +00:00
|
|
|
var attesters: AttesterMap
|
2022-05-10 10:03:40 +00:00
|
|
|
for key, item in vc.attesters:
|
2021-07-13 11:15:07 +00:00
|
|
|
var v = EpochDuties()
|
2022-05-10 10:03:40 +00:00
|
|
|
for epochKey, epochDuty in item.duties:
|
2021-07-13 11:15:07 +00:00
|
|
|
if (epochKey + HISTORICAL_DUTIES_EPOCHS) >= epoch:
|
|
|
|
v.duties[epochKey] = epochDuty
|
|
|
|
else:
|
2024-03-22 02:37:44 +00:00
|
|
|
debug "Attester duties for the epoch has been pruned",
|
|
|
|
pubkey = shortLog(key), epoch = epochKey, loop = AttesterLoop
|
2021-07-13 11:15:07 +00:00
|
|
|
attesters[key] = v
|
|
|
|
vc.attesters = attesters
|
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
proc pollForAttesterDuties*(
|
|
|
|
service: DutiesServiceRef
|
|
|
|
) {.async: (raises: [CancelledError]).} =
|
2021-07-13 11:15:07 +00:00
|
|
|
## Query the beacon node for attestation duties for all known validators.
|
|
|
|
##
|
|
|
|
## This function will perform (in the following order):
|
|
|
|
##
|
|
|
|
## 1. Poll for current-epoch duties and update the local `attesters` map.
|
|
|
|
## 2. Poll for next-epoch duties and update the local `attesters` map.
|
|
|
|
## 3. Push out any attestation subnet subscriptions to the BN.
|
2023-04-17 21:31:54 +00:00
|
|
|
let vc = service.client
|
|
|
|
let
|
|
|
|
currentSlot = vc.getCurrentSlot().get(Slot(0))
|
|
|
|
currentEpoch = currentSlot.epoch()
|
|
|
|
nextEpoch = currentEpoch + 1'u64
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2023-04-17 21:31:54 +00:00
|
|
|
if vc.attachedValidators[].count() != 0:
|
|
|
|
var counts: array[2, tuple[epoch: Epoch, count: int]]
|
|
|
|
counts[0] = (currentEpoch,
|
|
|
|
await service.pollForAttesterDuties(currentEpoch))
|
|
|
|
counts[1] = (nextEpoch,
|
|
|
|
await service.pollForAttesterDuties(nextEpoch))
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2023-04-17 21:31:54 +00:00
|
|
|
if (counts[0].count == 0) and (counts[1].count == 0):
|
|
|
|
debug "No new attester's duties received", slot = currentSlot
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2023-08-15 17:00:27 +00:00
|
|
|
block:
|
|
|
|
let
|
|
|
|
moment = Moment.now()
|
2023-11-08 12:03:51 +00:00
|
|
|
sigres =
|
|
|
|
await vc.fillAttestationSelectionProofs(currentSlot,
|
2024-10-08 03:37:00 +00:00
|
|
|
currentSlot + AGGREGATION_PRE_COMPUTE_SLOTS)
|
2023-11-08 12:03:51 +00:00
|
|
|
|
|
|
|
if vc.config.distributedEnabled:
|
|
|
|
debug "Attestation selection proofs have been received",
|
|
|
|
signatures_requested = sigres.signaturesRequested,
|
|
|
|
signatures_received = sigres.signaturesReceived,
|
2023-12-15 01:00:14 +00:00
|
|
|
selections_requested = sigres.selectionsRequested,
|
|
|
|
selections_received = sigres.selectionsReceived,
|
|
|
|
selections_processed = sigres.selectionsProcessed,
|
2023-11-08 12:03:51 +00:00
|
|
|
total_elapsed_time = (Moment.now() - moment)
|
|
|
|
else:
|
|
|
|
debug "Attestation selection proofs have been received",
|
|
|
|
signatures_requested = sigres.signaturesRequested,
|
|
|
|
signatures_received = sigres.signaturesReceived,
|
|
|
|
total_elapsed_time = (Moment.now() - moment)
|
2023-08-15 17:00:27 +00:00
|
|
|
|
2023-04-17 21:31:54 +00:00
|
|
|
let subscriptions =
|
|
|
|
block:
|
|
|
|
var res: seq[RestCommitteeSubscription]
|
|
|
|
for item in counts:
|
|
|
|
if item.count > 0:
|
|
|
|
for duty in vc.attesterDutiesForEpoch(item.epoch):
|
|
|
|
if currentSlot + SUBSCRIPTION_BUFFER_SLOTS < duty.data.slot:
|
|
|
|
let isAggregator =
|
|
|
|
if duty.slotSig.isSome():
|
|
|
|
is_aggregator(duty.data.committee_length,
|
|
|
|
duty.slotSig.get())
|
|
|
|
else:
|
|
|
|
false
|
|
|
|
let sub = RestCommitteeSubscription(
|
|
|
|
validator_index: duty.data.validator_index,
|
|
|
|
committee_index: duty.data.committee_index,
|
|
|
|
committees_at_slot: duty.data.committees_at_slot,
|
|
|
|
slot: duty.data.slot,
|
|
|
|
is_aggregator: isAggregator
|
|
|
|
)
|
|
|
|
res.add(sub)
|
|
|
|
res
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2023-04-17 21:31:54 +00:00
|
|
|
if len(subscriptions) > 0:
|
2024-10-16 17:20:39 +00:00
|
|
|
let res =
|
|
|
|
try:
|
|
|
|
await vc.prepareBeaconCommitteeSubnet(subscriptions)
|
|
|
|
except ValidatorApiError as exc:
|
|
|
|
warn "Failed to subscribe validators to beacon committee subnets",
|
|
|
|
slot = currentSlot, epoch = currentEpoch,
|
|
|
|
subscriptions_count = len(subscriptions),
|
|
|
|
reason = exc.msg
|
|
|
|
0
|
2023-04-17 21:31:54 +00:00
|
|
|
if res == 0:
|
|
|
|
warn "Failed to subscribe validators to beacon committee subnets",
|
|
|
|
slot = currentSlot, epoch = currentEpoch,
|
|
|
|
subscriptions_count = len(subscriptions)
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2023-04-17 21:31:54 +00:00
|
|
|
service.pruneAttesterDuties(currentEpoch)
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
proc pollForSyncCommitteeDuties*(
|
|
|
|
service: DutiesServiceRef
|
|
|
|
) {.async: (raises: [CancelledError]).} =
|
2023-04-17 21:31:54 +00:00
|
|
|
let
|
2024-10-16 17:20:39 +00:00
|
|
|
vc = service.client
|
2023-04-17 21:31:54 +00:00
|
|
|
currentSlot = vc.getCurrentSlot().get(Slot(0))
|
2023-08-15 17:00:27 +00:00
|
|
|
currentEpoch = currentSlot.epoch()
|
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
if not vc.isPastAltairFork(currentEpoch):
|
|
|
|
notice "Sync committee duties will not be queried before ALTAIR fork",
|
|
|
|
epoch = currentEpoch
|
|
|
|
# We are not going to poll for sync committee duties until `ALTAIR` epoch.
|
2023-08-15 17:00:27 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
let
|
|
|
|
currentPeriod = currentEpoch.sync_committee_period()
|
|
|
|
nextPeriod = currentPeriod + 1
|
2022-05-10 10:03:40 +00:00
|
|
|
|
2023-04-17 21:31:54 +00:00
|
|
|
if vc.attachedValidators[].count() != 0:
|
2023-08-15 17:00:27 +00:00
|
|
|
var counts: array[2, tuple[period: SyncCommitteePeriod, count: int]]
|
|
|
|
counts[0] = (currentPeriod,
|
|
|
|
await service.pollForSyncCommitteeDuties(currentPeriod))
|
2023-09-13 06:58:28 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
numDelayEpochs = 4 # Chosen empirically
|
|
|
|
numLookaheadEpochs =
|
|
|
|
max(EPOCHS_PER_SYNC_COMMITTEE_PERIOD, numDelayEpochs) -
|
|
|
|
numDelayEpochs + 1
|
|
|
|
if (currentEpoch + numLookaheadEpochs) >= nextPeriod.start_epoch:
|
|
|
|
counts[1] = (nextPeriod,
|
|
|
|
await service.pollForSyncCommitteeDuties(nextPeriod))
|
|
|
|
else:
|
|
|
|
# Skip fetching `nextPeriod` until sync committees are likely known,
|
|
|
|
# as determined by `numDelayEpochs` from sync committee period start.
|
|
|
|
counts[1] = (nextPeriod, 0)
|
2023-08-15 17:00:27 +00:00
|
|
|
|
|
|
|
if (counts[0].count == 0) and (counts[1].count == 0):
|
|
|
|
debug "No new sync committee duties received", slot = currentSlot
|
|
|
|
|
|
|
|
block:
|
|
|
|
let
|
|
|
|
moment = Moment.now()
|
2023-11-08 12:03:51 +00:00
|
|
|
sigres =
|
|
|
|
await vc.fillSyncCommitteeSelectionProofs(currentSlot,
|
2024-10-08 03:37:00 +00:00
|
|
|
currentSlot + AGGREGATION_PRE_COMPUTE_SLOTS)
|
2023-11-08 12:03:51 +00:00
|
|
|
|
|
|
|
if vc.config.distributedEnabled:
|
|
|
|
debug "Sync committee selection proofs have been received",
|
|
|
|
signatures_requested = sigres.signaturesRequested,
|
|
|
|
signatures_received = sigres.signaturesReceived,
|
2023-12-15 01:00:14 +00:00
|
|
|
selections_requested = sigres.selectionsRequested,
|
|
|
|
selections_received = sigres.selectionsReceived,
|
|
|
|
selections_processed = sigres.selectionsProcessed,
|
2023-11-08 12:03:51 +00:00
|
|
|
total_elapsed_time = (Moment.now() - moment)
|
|
|
|
else:
|
|
|
|
debug "Sync committee selection proofs have been received",
|
|
|
|
signatures_requested = sigres.signaturesRequested,
|
|
|
|
signatures_received = sigres.signaturesReceived,
|
|
|
|
total_elapsed_time = (Moment.now() - moment)
|
2023-08-15 17:00:27 +00:00
|
|
|
|
2023-04-17 21:31:54 +00:00
|
|
|
let
|
2023-08-15 17:00:27 +00:00
|
|
|
periods =
|
2023-04-17 21:31:54 +00:00
|
|
|
block:
|
2023-08-15 17:00:27 +00:00
|
|
|
var res: seq[tuple[slot: Slot, period: SyncCommitteePeriod]]
|
|
|
|
if service.syncSubscriptionEpoch.get(FAR_FUTURE_EPOCH) !=
|
|
|
|
currentEpoch:
|
|
|
|
res.add((currentSlot, currentPeriod))
|
2023-04-17 21:31:54 +00:00
|
|
|
let
|
2023-08-15 17:00:27 +00:00
|
|
|
lookaheadSlot = currentSlot +
|
|
|
|
SUBSCRIPTION_LOOKAHEAD_EPOCHS * SLOTS_PER_EPOCH
|
|
|
|
lookaheadPeriod = lookaheadSlot.sync_committee_period()
|
|
|
|
if lookaheadPeriod > currentPeriod:
|
|
|
|
res.add((lookaheadSlot, lookaheadPeriod))
|
2022-05-10 10:03:40 +00:00
|
|
|
res
|
2023-08-15 17:00:27 +00:00
|
|
|
subscriptions =
|
2023-04-17 21:31:54 +00:00
|
|
|
block:
|
2023-08-15 17:00:27 +00:00
|
|
|
var res: seq[RestSyncCommitteeSubscription]
|
|
|
|
for item in periods:
|
|
|
|
let
|
|
|
|
untilEpoch = start_epoch(item.period + 1)
|
|
|
|
subscriptionsInfo =
|
|
|
|
vc.syncMembersSubscriptionInfoForPeriod(item.period)
|
|
|
|
for info in subscriptionsInfo:
|
2023-04-17 21:31:54 +00:00
|
|
|
let sub = RestSyncCommitteeSubscription(
|
2023-08-15 17:00:27 +00:00
|
|
|
validator_index: info.validator_index,
|
2023-04-17 21:31:54 +00:00
|
|
|
sync_committee_indices:
|
2023-08-15 17:00:27 +00:00
|
|
|
info.validator_sync_committee_indices,
|
2023-04-17 21:31:54 +00:00
|
|
|
until_epoch: untilEpoch
|
|
|
|
)
|
|
|
|
res.add(sub)
|
2023-08-15 17:00:27 +00:00
|
|
|
res
|
2023-04-17 21:31:54 +00:00
|
|
|
if len(subscriptions) > 0:
|
2024-10-16 17:20:39 +00:00
|
|
|
let (res, reason) =
|
|
|
|
try:
|
|
|
|
(await vc.prepareSyncCommitteeSubnets(subscriptions), "")
|
|
|
|
except ValidatorApiError as exc:
|
|
|
|
(0, $exc.msg)
|
|
|
|
|
2023-04-17 21:31:54 +00:00
|
|
|
if res == 0:
|
|
|
|
warn "Failed to subscribe validators to sync committee subnets",
|
2023-08-15 17:00:27 +00:00
|
|
|
slot = currentSlot, epoch = currentPeriod, period = currentPeriod,
|
2024-10-16 17:20:39 +00:00
|
|
|
periods = periods, subscriptions_count = len(subscriptions),
|
|
|
|
reason = reason
|
2023-08-15 17:00:27 +00:00
|
|
|
else:
|
|
|
|
service.syncSubscriptionEpoch = Opt.some(currentEpoch)
|
2023-04-17 21:31:54 +00:00
|
|
|
|
2023-08-15 17:00:27 +00:00
|
|
|
service.pruneSyncCommitteeDuties(currentSlot)
|
|
|
|
service.pruneSyncCommitteeSelectionProofs(currentSlot)
|
2022-05-10 10:03:40 +00:00
|
|
|
|
2023-04-17 21:31:54 +00:00
|
|
|
proc pruneBeaconProposers(service: DutiesServiceRef, epoch: Epoch) =
|
|
|
|
let vc = service.client
|
2022-10-21 14:53:30 +00:00
|
|
|
|
2021-07-13 11:15:07 +00:00
|
|
|
var proposers: ProposerMap
|
2022-05-10 10:03:40 +00:00
|
|
|
for epochKey, data in vc.proposers:
|
2021-07-13 11:15:07 +00:00
|
|
|
if (epochKey + HISTORICAL_DUTIES_EPOCHS) >= epoch:
|
|
|
|
proposers[epochKey] = data
|
|
|
|
else:
|
|
|
|
debug "Proposer duty has been pruned", epoch = epochKey,
|
|
|
|
loop = ProposerLoop
|
|
|
|
vc.proposers = proposers
|
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
proc pollForBeaconProposers*(
|
|
|
|
service: DutiesServiceRef
|
|
|
|
) {.async: (raises: [CancelledError]).} =
|
2023-04-17 21:31:54 +00:00
|
|
|
let vc = service.client
|
|
|
|
let
|
|
|
|
currentSlot = vc.getCurrentSlot().get(Slot(0))
|
|
|
|
currentEpoch = currentSlot.epoch()
|
|
|
|
|
|
|
|
if vc.attachedValidators[].count() != 0:
|
|
|
|
try:
|
|
|
|
let res = await vc.getProposerDuties(currentEpoch,
|
|
|
|
ApiStrategyKind.First)
|
|
|
|
let
|
|
|
|
dependentRoot = res.dependent_root
|
|
|
|
duties = res.data
|
|
|
|
relevantDuties = duties.filterIt(it.pubkey in vc.attachedValidators[])
|
|
|
|
|
|
|
|
if len(relevantDuties) > 0:
|
|
|
|
vc.addOrReplaceProposers(currentEpoch, dependentRoot, relevantDuties)
|
|
|
|
else:
|
|
|
|
debug "No relevant proposer duties received", slot = currentSlot,
|
|
|
|
duties_count = len(duties)
|
|
|
|
except ValidatorApiError as exc:
|
|
|
|
notice "Unable to get proposer duties", slot = currentSlot,
|
|
|
|
epoch = currentEpoch, reason = exc.getFailureReason()
|
|
|
|
except CancelledError as exc:
|
|
|
|
debug "Proposer duties processing was interrupted"
|
|
|
|
raise exc
|
|
|
|
|
2024-10-09 01:00:06 +00:00
|
|
|
service.pruneBeaconProposers(currentEpoch)
|
|
|
|
vc.pruneBlocksSeen(currentEpoch)
|
2023-04-17 21:31:54 +00:00
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
proc prepareBeaconProposers*(
|
|
|
|
service: DutiesServiceRef
|
|
|
|
) {.async: (raises: [CancelledError]).} =
|
2023-04-17 21:31:54 +00:00
|
|
|
let vc = service.client
|
|
|
|
let
|
|
|
|
currentSlot = vc.getCurrentSlot().get(Slot(0))
|
|
|
|
currentEpoch = currentSlot.epoch()
|
|
|
|
proposers = vc.prepareProposersList(currentEpoch)
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2023-04-17 21:31:54 +00:00
|
|
|
if len(proposers) > 0:
|
|
|
|
let count =
|
2021-07-13 11:15:07 +00:00
|
|
|
try:
|
2023-04-17 21:31:54 +00:00
|
|
|
await prepareBeaconProposer(vc, proposers)
|
2023-02-23 00:11:00 +00:00
|
|
|
except ValidatorApiError as exc:
|
2023-04-17 21:31:54 +00:00
|
|
|
warn "Unable to prepare beacon proposers", slot = currentSlot,
|
|
|
|
epoch = currentEpoch, err_name = exc.name,
|
|
|
|
err_msg = exc.msg, reason = exc.getFailureReason()
|
|
|
|
0
|
2022-07-14 21:11:25 +00:00
|
|
|
except CancelledError as exc:
|
2023-04-17 21:31:54 +00:00
|
|
|
debug "Beacon proposer preparation processing was interrupted"
|
2022-07-14 21:11:25 +00:00
|
|
|
raise exc
|
2024-10-16 17:20:39 +00:00
|
|
|
|
2023-04-17 21:31:54 +00:00
|
|
|
debug "Beacon proposers prepared",
|
|
|
|
validators_count = vc.attachedValidators[].count(),
|
|
|
|
proposers_count = len(proposers),
|
|
|
|
prepared_count = count
|
2022-10-21 14:53:30 +00:00
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
proc registerValidators*(
|
|
|
|
service: DutiesServiceRef
|
|
|
|
) {.async: (raises: [CancelledError]).} =
|
2022-10-29 09:00:51 +00:00
|
|
|
let vc = service.client
|
2023-04-17 21:31:54 +00:00
|
|
|
let
|
|
|
|
currentSlot = vc.getCurrentSlot().get(Slot(0))
|
|
|
|
genesisFork = vc.forks[0]
|
|
|
|
registrations =
|
|
|
|
try:
|
|
|
|
await vc.prepareRegistrationList(getTime(), genesisFork)
|
|
|
|
except CancelledError as exc:
|
|
|
|
debug "Validator registration preparation was interrupted",
|
|
|
|
slot = currentSlot, fork = genesisFork
|
|
|
|
raise exc
|
2022-10-29 09:00:51 +00:00
|
|
|
|
2023-04-17 21:31:54 +00:00
|
|
|
count =
|
2022-10-29 09:00:51 +00:00
|
|
|
if len(registrations) > 0:
|
|
|
|
try:
|
|
|
|
await registerValidator(vc, registrations)
|
|
|
|
except ValidatorApiError as exc:
|
|
|
|
warn "Unable to register validators", slot = currentSlot,
|
|
|
|
fork = genesisFork, err_name = exc.name,
|
2023-02-23 00:11:00 +00:00
|
|
|
err_msg = exc.msg, reason = exc.getFailureReason()
|
2022-10-29 09:00:51 +00:00
|
|
|
0
|
|
|
|
except CancelledError as exc:
|
|
|
|
debug "Validator registration was interrupted", slot = currentSlot,
|
|
|
|
fork = genesisFork
|
|
|
|
raise exc
|
|
|
|
else:
|
|
|
|
0
|
|
|
|
|
2023-04-17 21:31:54 +00:00
|
|
|
if count > 0:
|
|
|
|
debug "Validators registered", slot = currentSlot,
|
|
|
|
beacon_nodes_count = count, registrations = len(registrations),
|
|
|
|
validators_count = vc.attachedValidators[].count()
|
2022-10-29 09:00:51 +00:00
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
proc attesterDutiesLoop(
|
|
|
|
service: DutiesServiceRef
|
|
|
|
) {.async: (raises: [CancelledError]).} =
|
2021-07-13 11:15:07 +00:00
|
|
|
let vc = service.client
|
2023-04-17 21:31:54 +00:00
|
|
|
debug "Attester duties loop is waiting for initialization"
|
|
|
|
await allFutures(
|
|
|
|
vc.preGenesisEvent.wait(),
|
|
|
|
vc.indicesAvailable.wait(),
|
|
|
|
vc.forksAvailable.wait()
|
|
|
|
)
|
2022-02-16 11:31:23 +00:00
|
|
|
doAssert(len(vc.forks) > 0, "Fork schedule must not be empty at this point")
|
2021-07-13 11:15:07 +00:00
|
|
|
while true:
|
2023-08-15 17:00:27 +00:00
|
|
|
await service.waitForNextSlot()
|
|
|
|
# Cleaning up previous attestation duties task.
|
|
|
|
if not(isNil(service.pollingAttesterDutiesTask)) and
|
|
|
|
not(service.pollingAttesterDutiesTask.finished()):
|
|
|
|
await cancelAndWait(service.pollingAttesterDutiesTask)
|
|
|
|
# Spawning new attestation duties task.
|
|
|
|
service.pollingAttesterDutiesTask = service.pollForAttesterDuties()
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
proc proposerDutiesLoop(
|
|
|
|
service: DutiesServiceRef
|
|
|
|
) {.async: (raises: [CancelledError]).} =
|
2021-07-13 11:15:07 +00:00
|
|
|
let vc = service.client
|
2023-04-17 21:31:54 +00:00
|
|
|
debug "Proposer duties loop is waiting for initialization"
|
|
|
|
await allFutures(
|
|
|
|
vc.preGenesisEvent.wait(),
|
|
|
|
vc.indicesAvailable.wait(),
|
|
|
|
vc.forksAvailable.wait()
|
|
|
|
)
|
2022-02-16 11:31:23 +00:00
|
|
|
doAssert(len(vc.forks) > 0, "Fork schedule must not be empty at this point")
|
2021-07-13 11:15:07 +00:00
|
|
|
while true:
|
2023-04-17 21:31:54 +00:00
|
|
|
await service.pollForBeaconProposers()
|
2023-08-15 17:00:27 +00:00
|
|
|
await service.waitForNextSlot()
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
proc validatorIndexLoop(
|
|
|
|
service: DutiesServiceRef
|
|
|
|
) {.async: (raises: [CancelledError]).} =
|
2021-07-13 11:15:07 +00:00
|
|
|
let vc = service.client
|
2023-04-17 21:31:54 +00:00
|
|
|
debug "Validator indices loop is waiting for initialization"
|
|
|
|
await vc.preGenesisEvent.wait()
|
2021-07-13 11:15:07 +00:00
|
|
|
while true:
|
2023-04-17 21:31:54 +00:00
|
|
|
await service.pollForValidatorIndices()
|
2023-08-15 17:00:27 +00:00
|
|
|
await service.waitForNextSlot()
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
proc dynamicValidatorsLoop*(
|
|
|
|
service: DutiesServiceRef,
|
|
|
|
web3signerUrl: Web3SignerUrl,
|
|
|
|
intervalInSeconds: int
|
|
|
|
) {.async: (raises: [CancelledError]).} =
|
2023-09-04 19:14:58 +00:00
|
|
|
let vc = service.client
|
2023-09-06 19:04:10 +00:00
|
|
|
doAssert(intervalInSeconds > 0)
|
2023-09-04 19:14:58 +00:00
|
|
|
|
|
|
|
proc addValidatorProc(data: KeystoreData) =
|
|
|
|
vc.addValidator(data)
|
|
|
|
|
|
|
|
var
|
2023-09-06 19:04:10 +00:00
|
|
|
timeout = seconds(intervalInSeconds)
|
2023-09-04 19:14:58 +00:00
|
|
|
exitLoop = false
|
|
|
|
|
|
|
|
while not(exitLoop):
|
|
|
|
exitLoop =
|
|
|
|
try:
|
|
|
|
await sleepAsync(timeout)
|
|
|
|
timeout =
|
|
|
|
block:
|
2023-09-06 19:04:10 +00:00
|
|
|
let res = await queryValidatorsSource(web3signerUrl)
|
2023-09-04 19:14:58 +00:00
|
|
|
if res.isOk():
|
|
|
|
let keystores = res.get()
|
2023-09-06 19:04:10 +00:00
|
|
|
debug "Web3Signer has been polled for validators",
|
2023-09-04 19:14:58 +00:00
|
|
|
keystores_found = len(keystores),
|
2023-10-13 12:42:00 +00:00
|
|
|
web3signer_url = web3signerUrl.url
|
2023-09-06 19:04:10 +00:00
|
|
|
vc.attachedValidators.updateDynamicValidators(web3signerUrl,
|
|
|
|
keystores,
|
2023-09-04 19:14:58 +00:00
|
|
|
addValidatorProc)
|
2023-09-06 19:04:10 +00:00
|
|
|
seconds(intervalInSeconds)
|
2023-09-04 19:14:58 +00:00
|
|
|
else:
|
|
|
|
seconds(5)
|
|
|
|
false
|
|
|
|
except CancelledError:
|
|
|
|
true
|
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
proc proposerPreparationsLoop(
|
|
|
|
service: DutiesServiceRef
|
|
|
|
) {.async: (raises: [CancelledError]).} =
|
2022-10-21 14:53:30 +00:00
|
|
|
let vc = service.client
|
2023-04-17 21:31:54 +00:00
|
|
|
debug "Beacon proposer preparation loop is waiting for initialization"
|
|
|
|
await allFutures(
|
|
|
|
vc.preGenesisEvent.wait(),
|
|
|
|
vc.indicesAvailable.wait()
|
|
|
|
)
|
2022-10-21 14:53:30 +00:00
|
|
|
while true:
|
|
|
|
await service.prepareBeaconProposers()
|
2023-08-15 17:00:27 +00:00
|
|
|
await service.waitForNextSlot()
|
2022-10-21 14:53:30 +00:00
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
proc validatorRegisterLoop(
|
|
|
|
service: DutiesServiceRef
|
|
|
|
) {.async: (raises: [CancelledError]).} =
|
2022-10-29 09:00:51 +00:00
|
|
|
let vc = service.client
|
|
|
|
doAssert(vc.config.payloadBuilderEnable)
|
|
|
|
debug "Validator registration loop is waiting for initialization"
|
2023-04-17 21:31:54 +00:00
|
|
|
await allFutures(
|
|
|
|
vc.preGenesisEvent.wait(),
|
|
|
|
vc.indicesAvailable.wait(),
|
|
|
|
vc.forksAvailable.wait()
|
|
|
|
)
|
|
|
|
doAssert(len(vc.forks) > 0, "Fork schedule must not be empty at this point")
|
2022-10-29 09:00:51 +00:00
|
|
|
while true:
|
|
|
|
await service.registerValidators()
|
2023-08-15 17:00:27 +00:00
|
|
|
await service.waitForNextSlot()
|
2022-10-29 09:00:51 +00:00
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
proc syncCommitteeDutiesLoop(
|
|
|
|
service: DutiesServiceRef
|
|
|
|
) {.async: (raises: [CancelledError]).} =
|
2022-05-10 10:03:40 +00:00
|
|
|
let vc = service.client
|
2023-04-17 21:31:54 +00:00
|
|
|
debug "Sync committee duties loop is waiting for initialization"
|
|
|
|
await allFutures(
|
|
|
|
vc.preGenesisEvent.wait(),
|
|
|
|
vc.indicesAvailable.wait(),
|
|
|
|
vc.forksAvailable.wait()
|
|
|
|
)
|
2022-06-23 03:59:37 +00:00
|
|
|
doAssert(len(vc.forks) > 0, "Fork schedule must not be empty at this point")
|
2022-05-10 10:03:40 +00:00
|
|
|
while true:
|
2023-08-15 17:00:27 +00:00
|
|
|
await service.waitForNextSlot()
|
|
|
|
if not(isNil(service.pollingSyncDutiesTask)) and
|
|
|
|
not(service.pollingSyncDutiesTask.finished()):
|
|
|
|
await cancelAndWait(service.pollingSyncDutiesTask)
|
|
|
|
# Spawning new attestation duties task.
|
|
|
|
service.pollingSyncDutiesTask = service.pollForSyncCommitteeDuties()
|
2022-05-10 10:03:40 +00:00
|
|
|
|
2023-11-06 14:40:44 +00:00
|
|
|
proc getNextEpochMiddleSlot(vc: ValidatorClientRef): Slot =
|
|
|
|
let
|
|
|
|
middleSlot = Slot(SLOTS_PER_EPOCH div 2)
|
|
|
|
currentSlot = vc.beaconClock.now().slotOrZero()
|
|
|
|
slotInEpoch = currentSlot.since_epoch_start()
|
|
|
|
|
|
|
|
if slotInEpoch >= middleSlot:
|
|
|
|
(currentSlot.epoch + 1'u64).start_slot() + uint64(middleSlot)
|
|
|
|
else:
|
|
|
|
currentSlot + (uint64(middleSlot) - uint64(slotInEpoch))
|
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
proc pruneSlashingDatabase(
|
|
|
|
service: DutiesServiceRef
|
|
|
|
) {.async: (raises: [CancelledError]).} =
|
2023-11-06 14:40:44 +00:00
|
|
|
let
|
|
|
|
vc = service.client
|
|
|
|
currentSlot = vc.beaconClock.now().slotOrZero()
|
|
|
|
startTime = Moment.now()
|
|
|
|
blockHeader =
|
|
|
|
try:
|
|
|
|
await vc.getFinalizedBlockHeader()
|
|
|
|
except CancelledError as exc:
|
|
|
|
debug "Finalized block header request was interrupted",
|
|
|
|
slot = currentSlot
|
|
|
|
raise exc
|
|
|
|
checkpointTime = Moment.now()
|
|
|
|
if blockHeader.isSome():
|
|
|
|
let epoch = blockHeader.get().data.header.message.slot.epoch
|
|
|
|
vc.finalizedEpoch = Opt.some(epoch)
|
|
|
|
if service.lastSlashingEpoch.get(FAR_FUTURE_EPOCH) != epoch:
|
|
|
|
vc.attachedValidators[]
|
|
|
|
.slashingProtection
|
|
|
|
.pruneAfterFinalization(epoch)
|
|
|
|
service.lastSlashingEpoch = Opt.some(epoch)
|
|
|
|
let finishTime = Moment.now()
|
|
|
|
debug "Slashing database has been pruned", slot = currentSlot,
|
|
|
|
epoch = currentSlot.epoch(),
|
|
|
|
finalized_epoch = epoch,
|
|
|
|
elapsed_time = (finishTime - startTime),
|
|
|
|
pruning_time = (finishTime - checkpointTime)
|
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
proc slashingDatabasePruningLoop(
|
|
|
|
service: DutiesServiceRef
|
|
|
|
) {.async: (raises: [CancelledError]).} =
|
2023-11-06 14:40:44 +00:00
|
|
|
let vc = service.client
|
|
|
|
debug "Slashing database pruning loop is waiting for initialization"
|
|
|
|
await allFutures(
|
|
|
|
vc.preGenesisEvent.wait(),
|
|
|
|
vc.indicesAvailable.wait(),
|
|
|
|
vc.forksAvailable.wait()
|
|
|
|
)
|
|
|
|
doAssert(len(vc.forks) > 0, "Fork schedule must not be empty at this point")
|
|
|
|
while true:
|
|
|
|
let slot = await vc.checkedWaitForSlot(vc.getNextEpochMiddleSlot(),
|
|
|
|
aggregateSlotOffset, false)
|
|
|
|
if slot.isNone():
|
|
|
|
continue
|
|
|
|
|
|
|
|
if not(isNil(service.pruneSlashingDatabaseTask)) and
|
|
|
|
not(service.pruneSlashingDatabaseTask.finished()):
|
|
|
|
await cancelAndWait(service.pruneSlashingDatabaseTask)
|
|
|
|
service.pruneSlashingDatabaseTask = service.pruneSlashingDatabase()
|
|
|
|
|
2021-07-13 11:15:07 +00:00
|
|
|
template checkAndRestart(serviceLoop: DutiesServiceLoop,
|
|
|
|
future: Future[void], body: untyped): untyped =
|
|
|
|
if future.finished():
|
|
|
|
if future.failed():
|
2024-10-16 17:20:39 +00:00
|
|
|
let error = future.error
|
2021-07-13 11:15:07 +00:00
|
|
|
debug "The loop ended unexpectedly with an error",
|
|
|
|
error_name = error.name, error_msg = error.msg, loop = serviceLoop
|
|
|
|
elif future.cancelled():
|
2022-07-13 14:43:57 +00:00
|
|
|
debug "The loop was interrupted", loop = serviceLoop
|
2021-07-13 11:15:07 +00:00
|
|
|
else:
|
|
|
|
debug "The loop is finished unexpectedly without an error",
|
|
|
|
loop = serviceLoop
|
|
|
|
future = body
|
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
proc mainLoop(service: DutiesServiceRef) {.async: (raises: []).} =
|
2022-10-29 09:00:51 +00:00
|
|
|
let vc = service.client
|
|
|
|
|
2021-07-13 11:15:07 +00:00
|
|
|
service.state = ServiceState.Running
|
2022-07-13 14:43:57 +00:00
|
|
|
debug "Service started"
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2022-07-13 14:43:57 +00:00
|
|
|
var
|
2022-07-14 21:11:25 +00:00
|
|
|
attestFut = service.attesterDutiesLoop()
|
|
|
|
proposeFut = service.proposerDutiesLoop()
|
|
|
|
indicesFut = service.validatorIndexLoop()
|
2022-10-21 14:53:30 +00:00
|
|
|
syncFut = service.syncCommitteeDutiesLoop()
|
|
|
|
prepareFut = service.proposerPreparationsLoop()
|
2022-10-29 09:00:51 +00:00
|
|
|
registerFut =
|
|
|
|
if vc.config.payloadBuilderEnable:
|
|
|
|
service.validatorRegisterLoop()
|
|
|
|
else:
|
|
|
|
nil
|
2023-09-06 19:04:10 +00:00
|
|
|
dynamicFuts =
|
|
|
|
if vc.config.web3signerUpdateInterval > 0:
|
2023-10-13 12:42:00 +00:00
|
|
|
mapIt(vc.config.web3SignerUrls,
|
2023-09-06 19:04:10 +00:00
|
|
|
service.dynamicValidatorsLoop(it, vc.config.web3signerUpdateInterval))
|
2023-09-04 19:14:58 +00:00
|
|
|
else:
|
|
|
|
debug "Dynamic validators update loop disabled"
|
2023-09-06 19:04:10 +00:00
|
|
|
@[]
|
2023-11-06 14:40:44 +00:00
|
|
|
slashPruningFut = service.slashingDatabasePruningLoop()
|
2023-10-13 12:42:00 +00:00
|
|
|
web3SignerUrls = vc.config.web3SignerUrls
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2022-07-13 14:43:57 +00:00
|
|
|
while true:
|
|
|
|
# This loop could look much more nicer/better, when
|
|
|
|
# https://github.com/nim-lang/Nim/issues/19911 will be fixed, so it could
|
|
|
|
# become safe to combine loops, breaks and exception handlers.
|
|
|
|
let breakLoop =
|
2021-07-13 11:15:07 +00:00
|
|
|
try:
|
2023-04-17 21:31:54 +00:00
|
|
|
var futures = @[
|
|
|
|
FutureBase(attestFut),
|
|
|
|
FutureBase(proposeFut),
|
|
|
|
FutureBase(indicesFut),
|
|
|
|
FutureBase(syncFut),
|
2023-09-04 19:14:58 +00:00
|
|
|
FutureBase(prepareFut),
|
2023-11-06 14:40:44 +00:00
|
|
|
FutureBase(slashPruningFut)
|
2023-04-17 21:31:54 +00:00
|
|
|
]
|
2023-09-06 19:04:10 +00:00
|
|
|
for fut in dynamicFuts:
|
|
|
|
futures.add fut
|
2022-10-29 09:00:51 +00:00
|
|
|
if not(isNil(registerFut)): futures.add(FutureBase(registerFut))
|
2024-10-16 17:20:39 +00:00
|
|
|
try:
|
|
|
|
discard await race(futures)
|
|
|
|
except ValueError:
|
|
|
|
raiseAssert "Futures sequence will never be empty"
|
2022-07-14 21:11:25 +00:00
|
|
|
checkAndRestart(AttesterLoop, attestFut, service.attesterDutiesLoop())
|
|
|
|
checkAndRestart(ProposerLoop, proposeFut, service.proposerDutiesLoop())
|
|
|
|
checkAndRestart(IndicesLoop, indicesFut, service.validatorIndexLoop())
|
2022-10-21 14:53:30 +00:00
|
|
|
checkAndRestart(SyncCommitteeLoop, syncFut,
|
|
|
|
service.syncCommitteeDutiesLoop())
|
|
|
|
checkAndRestart(ProposerPreparationLoop, prepareFut,
|
|
|
|
service.proposerPreparationsLoop())
|
2022-10-29 09:00:51 +00:00
|
|
|
if not(isNil(registerFut)):
|
|
|
|
checkAndRestart(ValidatorRegisterLoop, registerFut,
|
|
|
|
service.validatorRegisterLoop())
|
2023-09-06 19:04:10 +00:00
|
|
|
for i in 0 ..< dynamicFuts.len:
|
|
|
|
checkAndRestart(DynamicValidatorsLoop, dynamicFuts[i],
|
|
|
|
service.dynamicValidatorsLoop(
|
2023-10-13 12:42:00 +00:00
|
|
|
web3SignerUrls[i],
|
2023-09-06 19:04:10 +00:00
|
|
|
vc.config.web3signerUpdateInterval))
|
2023-11-06 14:40:44 +00:00
|
|
|
checkAndRestart(SlashPruningLoop, slashPruningFut,
|
|
|
|
service.slashingDatabasePruningLoop())
|
2022-07-13 14:43:57 +00:00
|
|
|
false
|
2021-07-13 11:15:07 +00:00
|
|
|
except CancelledError:
|
2022-07-13 14:43:57 +00:00
|
|
|
debug "Service interrupted"
|
2022-07-14 21:11:25 +00:00
|
|
|
var pending: seq[Future[void]]
|
|
|
|
if not(attestFut.finished()):
|
|
|
|
pending.add(attestFut.cancelAndWait())
|
|
|
|
if not(proposeFut.finished()):
|
|
|
|
pending.add(proposeFut.cancelAndWait())
|
|
|
|
if not(indicesFut.finished()):
|
|
|
|
pending.add(indicesFut.cancelAndWait())
|
|
|
|
if not(syncFut.finished()):
|
|
|
|
pending.add(syncFut.cancelAndWait())
|
2022-10-21 14:53:30 +00:00
|
|
|
if not(prepareFut.finished()):
|
|
|
|
pending.add(prepareFut.cancelAndWait())
|
2022-10-29 09:00:51 +00:00
|
|
|
if not(isNil(registerFut)) and not(registerFut.finished()):
|
|
|
|
pending.add(registerFut.cancelAndWait())
|
2023-09-06 19:04:10 +00:00
|
|
|
for dynamicFut in dynamicFuts:
|
|
|
|
if not dynamicFut.finished():
|
|
|
|
pending.add(dynamicFut.cancelAndWait())
|
2023-08-15 17:00:27 +00:00
|
|
|
if not(isNil(service.pollingAttesterDutiesTask)) and
|
|
|
|
not(service.pollingAttesterDutiesTask.finished()):
|
|
|
|
pending.add(service.pollingAttesterDutiesTask.cancelAndWait())
|
|
|
|
if not(isNil(service.pollingSyncDutiesTask)) and
|
|
|
|
not(service.pollingSyncDutiesTask.finished()):
|
|
|
|
pending.add(service.pollingSyncDutiesTask.cancelAndWait())
|
2023-11-06 14:40:44 +00:00
|
|
|
if not(isNil(service.pruneSlashingDatabaseTask)) and
|
|
|
|
not(service.pruneSlashingDatabaseTask.finished()):
|
|
|
|
pending.add(service.pruneSlashingDatabaseTask.cancelAndWait())
|
2024-10-16 17:20:39 +00:00
|
|
|
await noCancel allFutures(pending)
|
2022-07-13 14:43:57 +00:00
|
|
|
true
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2022-07-13 14:43:57 +00:00
|
|
|
if breakLoop:
|
|
|
|
break
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
proc init*(
|
|
|
|
t: typedesc[DutiesServiceRef],
|
|
|
|
vc: ValidatorClientRef
|
|
|
|
): Future[DutiesServiceRef] {.async: (raises: []).} =
|
2022-07-14 21:11:25 +00:00
|
|
|
logScope: service = ServiceName
|
|
|
|
let res = DutiesServiceRef(name: ServiceName,
|
2022-07-13 14:43:57 +00:00
|
|
|
client: vc, state: ServiceState.Initialized)
|
2021-07-13 11:15:07 +00:00
|
|
|
debug "Initializing service"
|
2024-10-16 17:20:39 +00:00
|
|
|
res
|
2021-07-13 11:15:07 +00:00
|
|
|
|
|
|
|
proc start*(service: DutiesServiceRef) =
|
|
|
|
service.lifeFut = mainLoop(service)
|