2022-06-09 08:50:36 +00:00
|
|
|
# beacon_chain
|
2024-01-06 14:26:56 +00:00
|
|
|
# Copyright (c) 2021-2024 Status Research & Development GmbH
|
2022-06-09 08:50:36 +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-03-22 02:37:44 +00:00
|
|
|
{.push raises: [].}
|
|
|
|
|
2022-08-19 10:30:07 +00:00
|
|
|
import
|
|
|
|
std/sets,
|
|
|
|
chronicles,
|
2023-11-19 13:08:07 +00:00
|
|
|
../validators/[activity_metrics, validator_duties],
|
2023-06-08 08:44:32 +00:00
|
|
|
"."/[common, api]
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2022-07-14 21:11:25 +00:00
|
|
|
const
|
|
|
|
ServiceName = "attestation_service"
|
|
|
|
|
|
|
|
logScope: service = ServiceName
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2021-07-15 08:17:32 +00:00
|
|
|
type
|
|
|
|
AggregateItem* = object
|
|
|
|
aggregator_index: uint64
|
|
|
|
selection_proof: ValidatorSig
|
2024-10-16 17:20:39 +00:00
|
|
|
committee_index: CommitteeIndex
|
2021-07-15 08:17:32 +00:00
|
|
|
validator: AttachedValidator
|
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
func getAttesterDutiesByCommittee(
|
|
|
|
duties: openArray[DutyAndProof]
|
|
|
|
): Table[CommitteeIndex, seq[DutyAndProof]] =
|
|
|
|
var res: Table[CommitteeIndex, seq[DutyAndProof]]
|
|
|
|
for item in duties:
|
|
|
|
res.mgetOrPut(item.data.committee_index, default(seq[DutyAndProof])).
|
|
|
|
add(item)
|
|
|
|
res
|
|
|
|
|
2023-11-19 13:08:07 +00:00
|
|
|
proc serveAttestation(
|
2024-10-16 17:20:39 +00:00
|
|
|
service: AttestationServiceRef,
|
|
|
|
registered: RegisteredAttestation
|
|
|
|
): Future[bool] {.async: (raises: [CancelledError]).} =
|
2023-11-19 13:08:07 +00:00
|
|
|
let
|
|
|
|
vc = service.client
|
|
|
|
fork = vc.forkAtEpoch(registered.data.slot.epoch)
|
|
|
|
validator = registered.validator
|
2024-10-16 17:20:39 +00:00
|
|
|
attestationSlot = registered.data.slot
|
|
|
|
afterElectra = vc.isPastElectraFork(attestationSlot.epoch)
|
2022-07-21 16:54:07 +00:00
|
|
|
|
2023-04-17 21:31:54 +00:00
|
|
|
logScope:
|
|
|
|
validator = validatorLog(validator)
|
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
let signature =
|
|
|
|
try:
|
|
|
|
let res =
|
|
|
|
await validator.getAttestationSignature(
|
2023-11-19 13:08:07 +00:00
|
|
|
fork, vc.beaconGenesis.genesis_validators_root, registered.data)
|
2024-10-16 17:20:39 +00:00
|
|
|
if res.isErr():
|
|
|
|
warn "Unable to sign attestation", reason = res.error()
|
2021-11-30 01:20:21 +00:00
|
|
|
return false
|
2024-10-16 17:20:39 +00:00
|
|
|
res.get()
|
|
|
|
except CancelledError as exc:
|
|
|
|
debug "Attestation signature process was interrupted"
|
|
|
|
raise exc
|
2022-06-29 16:53:59 +00:00
|
|
|
|
2023-04-17 21:31:54 +00:00
|
|
|
logScope:
|
2024-10-16 17:20:39 +00:00
|
|
|
delay = vc.getDelay(attestationSlot.attestation_deadline())
|
2023-04-17 21:31:54 +00:00
|
|
|
|
|
|
|
debug "Sending attestation"
|
2021-08-23 10:41:48 +00:00
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
validator.doppelgangerActivity(attestationSlot.epoch)
|
2023-03-02 15:55:45 +00:00
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
template submitAttestation(atst: untyped): untyped =
|
|
|
|
logScope:
|
|
|
|
attestation = shortLog(atst)
|
2021-07-13 11:15:07 +00:00
|
|
|
try:
|
2024-10-16 17:20:39 +00:00
|
|
|
when atst is electra.Attestation:
|
|
|
|
await vc.submitPoolAttestationsV2(@[atst], ApiStrategyKind.First)
|
|
|
|
else:
|
|
|
|
await vc.submitPoolAttestations(@[atst], ApiStrategyKind.First)
|
2023-02-23 00:11:00 +00:00
|
|
|
except ValidatorApiError as exc:
|
2023-04-17 21:31:54 +00:00
|
|
|
warn "Unable to publish attestation", reason = exc.getFailureReason()
|
2021-07-19 14:31:02 +00:00
|
|
|
return false
|
2022-07-14 21:11:25 +00:00
|
|
|
except CancelledError as exc:
|
|
|
|
debug "Attestation publishing process was interrupted"
|
|
|
|
raise exc
|
2024-10-16 17:20:39 +00:00
|
|
|
|
|
|
|
let res =
|
|
|
|
if afterElectra:
|
|
|
|
let attestation = registered.toElectraAttestation(signature)
|
|
|
|
submitAttestation(attestation)
|
|
|
|
else:
|
|
|
|
let attestation = registered.toAttestation(signature)
|
|
|
|
submitAttestation(attestation)
|
2021-07-13 11:15:07 +00:00
|
|
|
|
|
|
|
if res:
|
2024-10-16 17:20:39 +00:00
|
|
|
let delay = vc.getDelay(attestationSlot.attestation_deadline())
|
2022-07-29 08:36:20 +00:00
|
|
|
beacon_attestations_sent.inc()
|
|
|
|
beacon_attestation_sent_delay.observe(delay.toFloatSeconds())
|
2023-04-17 21:31:54 +00:00
|
|
|
notice "Attestation published"
|
2021-07-13 11:15:07 +00:00
|
|
|
else:
|
2023-04-17 21:31:54 +00:00
|
|
|
warn "Attestation was not accepted by beacon node"
|
2021-07-15 08:17:32 +00:00
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
res
|
|
|
|
|
|
|
|
proc serveAggregateAndProof*(
|
|
|
|
service: AttestationServiceRef,
|
|
|
|
proof: phase0.AggregateAndProof,
|
|
|
|
validator: AttachedValidator
|
|
|
|
): Future[bool] {.async: (raises: [CancelledError]).} =
|
2021-07-15 08:17:32 +00:00
|
|
|
let
|
|
|
|
vc = service.client
|
|
|
|
genesisRoot = vc.beaconGenesis.genesis_validators_root
|
2022-02-16 11:31:23 +00:00
|
|
|
slot = proof.aggregate.data.slot
|
2022-07-21 16:54:07 +00:00
|
|
|
fork = vc.forkAtEpoch(slot.epoch)
|
|
|
|
|
2023-04-17 21:31:54 +00:00
|
|
|
logScope:
|
|
|
|
validator = validatorLog(validator)
|
|
|
|
attestation = shortLog(proof.aggregate)
|
|
|
|
|
|
|
|
debug "Signing aggregate", fork = fork
|
2021-07-15 08:17:32 +00:00
|
|
|
|
2021-11-30 01:20:21 +00:00
|
|
|
let signature =
|
2022-07-14 21:11:25 +00:00
|
|
|
try:
|
2023-04-17 21:31:54 +00:00
|
|
|
let res =
|
|
|
|
await validator.getAggregateAndProofSignature(fork, genesisRoot, proof)
|
2021-11-30 01:20:21 +00:00
|
|
|
if res.isErr():
|
2023-03-03 20:20:01 +00:00
|
|
|
warn "Unable to sign aggregate and proof using remote signer",
|
2023-04-17 21:31:54 +00:00
|
|
|
reason = res.error()
|
2021-11-30 01:20:21 +00:00
|
|
|
return false
|
|
|
|
res.get()
|
2022-07-14 21:11:25 +00:00
|
|
|
except CancelledError as exc:
|
|
|
|
debug "Aggregated attestation signing process was interrupted"
|
|
|
|
raise exc
|
|
|
|
|
2024-05-14 04:12:35 +00:00
|
|
|
let signedProof = phase0.SignedAggregateAndProof(
|
|
|
|
message: proof, signature: signature)
|
2023-04-17 21:31:54 +00:00
|
|
|
logScope:
|
|
|
|
delay = vc.getDelay(slot.aggregate_deadline())
|
2021-07-19 14:31:02 +00:00
|
|
|
|
2023-04-17 21:31:54 +00:00
|
|
|
debug "Sending aggregated attestation", fork = fork
|
2021-08-23 10:41:48 +00:00
|
|
|
|
2023-03-02 15:55:45 +00:00
|
|
|
validator.doppelgangerActivity(proof.aggregate.data.slot.epoch)
|
|
|
|
|
2021-07-19 14:31:02 +00:00
|
|
|
let res =
|
|
|
|
try:
|
2022-09-29 07:57:14 +00:00
|
|
|
await vc.publishAggregateAndProofs(@[signedProof], 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 publish aggregated attestation",
|
2023-04-17 21:31:54 +00:00
|
|
|
reason = exc.getFailureReason()
|
2021-07-19 14:31:02 +00:00
|
|
|
return false
|
2022-07-14 21:11:25 +00:00
|
|
|
except CancelledError as exc:
|
2022-07-13 14:43:57 +00:00
|
|
|
debug "Publish aggregate and proofs request was interrupted"
|
2022-07-14 21:11:25 +00:00
|
|
|
raise exc
|
2021-07-19 14:31:02 +00:00
|
|
|
|
|
|
|
if res:
|
2022-07-29 08:36:20 +00:00
|
|
|
beacon_aggregates_sent.inc()
|
2023-04-17 21:31:54 +00:00
|
|
|
notice "Aggregated attestation published"
|
2021-07-19 14:31:02 +00:00
|
|
|
else:
|
2023-04-17 21:31:54 +00:00
|
|
|
warn "Aggregated attestation was not accepted by beacon node"
|
2021-07-19 14:31:02 +00:00
|
|
|
return res
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
proc serveAggregateAndProofV2*(
|
|
|
|
service: AttestationServiceRef,
|
|
|
|
proof: ForkyAggregateAndProof,
|
|
|
|
validator: AttachedValidator
|
|
|
|
): Future[bool] {.async: (raises: [CancelledError]).} =
|
|
|
|
let
|
|
|
|
vc = service.client
|
|
|
|
genesisRoot = vc.beaconGenesis.genesis_validators_root
|
|
|
|
slot = proof.aggregate.data.slot
|
|
|
|
fork = vc.forkAtEpoch(slot.epoch)
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
validator = validatorLog(validator)
|
|
|
|
attestation = shortLog(proof.aggregate)
|
|
|
|
|
|
|
|
debug "Signing aggregate", fork = fork
|
|
|
|
|
|
|
|
let signature =
|
|
|
|
try:
|
|
|
|
let res =
|
|
|
|
await validator.getAggregateAndProofSignature(fork, genesisRoot, proof)
|
|
|
|
if res.isErr():
|
|
|
|
warn "Unable to sign aggregate and proof using remote signer",
|
|
|
|
reason = res.error()
|
|
|
|
return false
|
|
|
|
res.get()
|
|
|
|
except CancelledError as exc:
|
|
|
|
debug "Aggregated attestation signing process was interrupted"
|
|
|
|
raise exc
|
|
|
|
|
|
|
|
let signedProof =
|
|
|
|
when proof is phase0.AggregateAndProof:
|
|
|
|
phase0.SignedAggregateAndProof(
|
|
|
|
message: proof, signature: signature)
|
|
|
|
elif proof is electra.AggregateAndProof:
|
|
|
|
electra.SignedAggregateAndProof(
|
|
|
|
message: proof, signature: signature)
|
|
|
|
else:
|
|
|
|
static:
|
|
|
|
raiseAssert "Unsupported SignedAggregateAndProof"
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
delay = vc.getDelay(slot.aggregate_deadline())
|
|
|
|
|
|
|
|
debug "Sending aggregated attestation", fork = fork
|
|
|
|
|
|
|
|
validator.doppelgangerActivity(proof.aggregate.data.slot.epoch)
|
|
|
|
|
|
|
|
let res =
|
|
|
|
try:
|
|
|
|
await vc.publishAggregateAndProofsV2(@[signedProof],
|
|
|
|
ApiStrategyKind.First)
|
|
|
|
except ValidatorApiError as exc:
|
|
|
|
warn "Unable to publish aggregated attestation",
|
|
|
|
reason = exc.getFailureReason()
|
|
|
|
return false
|
|
|
|
except CancelledError as exc:
|
|
|
|
debug "Publish aggregate and proofs request was interrupted"
|
|
|
|
raise exc
|
|
|
|
|
|
|
|
if res:
|
|
|
|
beacon_aggregates_sent.inc()
|
|
|
|
notice "Aggregated attestation published"
|
|
|
|
else:
|
|
|
|
warn "Aggregated attestation was not accepted by beacon node"
|
|
|
|
res
|
|
|
|
|
|
|
|
proc produceAndPublishAttestations*(
|
|
|
|
service: AttestationServiceRef,
|
|
|
|
slot: Slot,
|
|
|
|
committee_index: CommitteeIndex,
|
|
|
|
duties: seq[DutyAndProof]
|
|
|
|
): Future[AttestationData] {.
|
|
|
|
async: (raises: [CancelledError, ValidatorApiError]).} =
|
2021-07-13 11:15:07 +00:00
|
|
|
doAssert(MAX_VALIDATORS_PER_COMMITTEE <= uint64(high(int)))
|
2023-11-19 13:08:07 +00:00
|
|
|
let
|
|
|
|
vc = service.client
|
|
|
|
fork = vc.forkAtEpoch(slot.epoch)
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2023-11-19 13:08:07 +00:00
|
|
|
let data = await vc.produceAttestationData(slot, committee_index,
|
|
|
|
ApiStrategyKind.Best)
|
|
|
|
|
|
|
|
let registeredRes = vc.attachedValidators[].slashingProtection.withContext:
|
|
|
|
var tmp: seq[RegisteredAttestation]
|
|
|
|
for duty in duties:
|
|
|
|
if (duty.data.slot != data.slot) or
|
|
|
|
(uint64(duty.data.committee_index) != data.index):
|
|
|
|
warn "Inconsistent validator duties during attestation signing",
|
2024-03-22 02:37:44 +00:00
|
|
|
pubkey = shortLog(duty.data.pubkey),
|
2023-11-19 13:08:07 +00:00
|
|
|
duty_slot = duty.data.slot,
|
|
|
|
duty_index = duty.data.committee_index,
|
|
|
|
attestation_slot = data.slot, attestation_index = data.index
|
|
|
|
continue
|
|
|
|
|
|
|
|
let validator = vc.getValidatorForDuties(
|
|
|
|
duty.data.pubkey, duty.data.slot).valueOr:
|
|
|
|
continue
|
|
|
|
|
|
|
|
doAssert(validator.index.isSome())
|
|
|
|
let validator_index = validator.index.get()
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
validator = validatorLog(validator)
|
|
|
|
|
|
|
|
# TODO: signing_root is recomputed in getAttestationSignature just after,
|
|
|
|
# but not for locally attached validators.
|
|
|
|
let
|
|
|
|
signingRoot = compute_attestation_signing_root(
|
|
|
|
fork, vc.beaconGenesis.genesis_validators_root, data)
|
|
|
|
registered = registerAttestationInContext(
|
|
|
|
validator_index, validator.pubkey, data.source.epoch,
|
|
|
|
data.target.epoch, signingRoot)
|
|
|
|
if registered.isErr():
|
|
|
|
warn "Slashing protection activated for attestation",
|
|
|
|
attestationData = shortLog(data),
|
|
|
|
signingRoot = shortLog(signingRoot),
|
|
|
|
badVoteDetails = $registered.error()
|
|
|
|
continue
|
|
|
|
|
|
|
|
tmp.add(RegisteredAttestation(
|
|
|
|
validator: validator,
|
2024-10-16 17:20:39 +00:00
|
|
|
committee_index: duty.data.committee_index,
|
2023-11-19 13:08:07 +00:00
|
|
|
index_in_committee: duty.data.validator_committee_index,
|
|
|
|
committee_len: int duty.data.committee_length,
|
|
|
|
data: data
|
|
|
|
))
|
|
|
|
tmp
|
|
|
|
|
|
|
|
if registeredRes.isErr():
|
|
|
|
warn "Could not update slashing database, skipping attestation duties",
|
|
|
|
error = registeredRes.error()
|
|
|
|
else:
|
|
|
|
let
|
|
|
|
pendingAttestations = registeredRes[].mapIt(service.serveAttestation(it))
|
|
|
|
statistics =
|
|
|
|
block:
|
|
|
|
var errored, succeed, failed = 0
|
|
|
|
try:
|
|
|
|
await allFutures(pendingAttestations)
|
|
|
|
except CancelledError as exc:
|
|
|
|
let pending = pendingAttestations
|
|
|
|
.filterIt(not(it.finished())).mapIt(it.cancelAndWait())
|
|
|
|
await noCancel allFutures(pending)
|
|
|
|
raise exc
|
|
|
|
|
|
|
|
for future in pendingAttestations:
|
|
|
|
if future.completed():
|
2024-10-16 17:20:39 +00:00
|
|
|
if future.value:
|
2023-11-19 13:08:07 +00:00
|
|
|
inc(succeed)
|
|
|
|
else:
|
|
|
|
inc(failed)
|
|
|
|
else:
|
|
|
|
inc(errored)
|
|
|
|
(succeed, errored, failed)
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2023-11-19 13:08:07 +00:00
|
|
|
let delay = vc.getDelay(slot.attestation_deadline())
|
|
|
|
debug "Attestation statistics", total = len(pendingAttestations),
|
|
|
|
succeed = statistics[0], failed_to_deliver = statistics[1],
|
|
|
|
not_accepted = statistics[2], delay = delay, slot = slot,
|
|
|
|
committee_index = committee_index, duties_count = len(duties)
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
data
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
proc produceAndPublishAggregates(
|
|
|
|
service: AttestationServiceRef,
|
|
|
|
adata: AttestationData,
|
|
|
|
duties: seq[DutyAndProof]
|
|
|
|
) {.async: (raises: [CancelledError]).} =
|
2021-07-13 11:15:07 +00:00
|
|
|
let
|
|
|
|
vc = service.client
|
|
|
|
slot = adata.slot
|
2022-01-08 23:28:49 +00:00
|
|
|
committeeIndex = adata.index
|
2021-07-13 11:15:07 +00:00
|
|
|
attestationRoot = adata.hash_tree_root()
|
|
|
|
|
2021-07-15 08:17:32 +00:00
|
|
|
let aggregateItems =
|
2021-07-13 11:15:07 +00:00
|
|
|
block:
|
2021-07-15 08:17:32 +00:00
|
|
|
var res: seq[AggregateItem]
|
2021-07-13 11:15:07 +00:00
|
|
|
for duty in duties:
|
2023-02-20 11:28:56 +00:00
|
|
|
let validator = vc.getValidatorForDuties(
|
2022-12-09 16:05:55 +00:00
|
|
|
duty.data.pubkey, slot).valueOr:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if (duty.data.slot != slot) or
|
|
|
|
(duty.data.committee_index != committeeIndex):
|
2023-03-03 20:20:01 +00:00
|
|
|
warn "Inconsistent validator duties during aggregate signing",
|
|
|
|
duty_slot = duty.data.slot, slot = slot,
|
|
|
|
duty_committee_index = duty.data.committee_index,
|
|
|
|
committee_index = committeeIndex
|
2022-12-09 16:05:55 +00:00
|
|
|
continue
|
|
|
|
if duty.slotSig.isSome():
|
|
|
|
let slotSignature = duty.slotSig.get()
|
|
|
|
if is_aggregator(duty.data.committee_length, slotSignature):
|
|
|
|
res.add(AggregateItem(
|
|
|
|
aggregator_index: uint64(duty.data.validator_index),
|
2024-10-16 17:20:39 +00:00
|
|
|
committee_index: CommitteeIndex(committeeIndex),
|
2022-12-09 16:05:55 +00:00
|
|
|
selection_proof: slotSignature,
|
|
|
|
validator: validator
|
|
|
|
))
|
2021-07-13 11:15:07 +00:00
|
|
|
res
|
|
|
|
|
2021-07-15 08:17:32 +00:00
|
|
|
if len(aggregateItems) > 0:
|
2024-10-16 17:20:39 +00:00
|
|
|
let aggregates =
|
|
|
|
block:
|
|
|
|
let aggAttestation =
|
|
|
|
try:
|
|
|
|
await vc.getAggregatedAttestation(slot, attestationRoot,
|
|
|
|
ApiStrategyKind.Best)
|
|
|
|
except ValidatorApiError as exc:
|
|
|
|
warn "Unable to get aggregated attestation data", slot = slot,
|
|
|
|
attestation_root = shortLog(attestationRoot),
|
|
|
|
reason = exc.getFailureReason()
|
|
|
|
return
|
|
|
|
except CancelledError as exc:
|
|
|
|
debug "Aggregated attestation request was interrupted"
|
|
|
|
raise exc
|
2021-07-15 08:17:32 +00:00
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
if isLowestScoreAggregatedAttestation(aggAttestation):
|
|
|
|
warn "Aggregated attestation with the root was not seen by the " &
|
|
|
|
"beacon node",
|
|
|
|
attestation_root = shortLog(attestationRoot)
|
|
|
|
return
|
2024-05-01 15:32:28 +00:00
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
var res: seq[Future[bool].Raising([CancelledError])]
|
2021-07-15 08:17:32 +00:00
|
|
|
for item in aggregateItems:
|
2024-05-14 04:12:35 +00:00
|
|
|
let proof = phase0.AggregateAndProof(
|
2021-07-15 08:17:32 +00:00
|
|
|
aggregator_index: item.aggregator_index,
|
|
|
|
aggregate: aggAttestation,
|
|
|
|
selection_proof: item.selection_proof
|
|
|
|
)
|
|
|
|
res.add(service.serveAggregateAndProof(proof, item.validator))
|
|
|
|
res
|
|
|
|
|
|
|
|
let statistics =
|
|
|
|
block:
|
|
|
|
var errored, succeed, failed = 0
|
|
|
|
try:
|
2024-10-16 17:20:39 +00:00
|
|
|
await allFutures(aggregates)
|
2022-07-14 21:11:25 +00:00
|
|
|
except CancelledError as exc:
|
2024-10-16 17:20:39 +00:00
|
|
|
let pending = aggregates
|
2023-09-24 07:28:09 +00:00
|
|
|
.filterIt(not(it.finished())).mapIt(it.cancelAndWait())
|
|
|
|
await noCancel allFutures(pending)
|
2022-07-14 21:11:25 +00:00
|
|
|
raise exc
|
2021-07-15 08:17:32 +00:00
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
for future in aggregates:
|
2023-06-01 08:04:30 +00:00
|
|
|
if future.completed():
|
2024-10-16 17:20:39 +00:00
|
|
|
if future.value:
|
2021-07-15 08:17:32 +00:00
|
|
|
inc(succeed)
|
|
|
|
else:
|
|
|
|
inc(failed)
|
|
|
|
else:
|
|
|
|
inc(errored)
|
|
|
|
(succeed, errored, failed)
|
|
|
|
|
2022-01-11 10:01:54 +00:00
|
|
|
let delay = vc.getDelay(slot.aggregate_deadline())
|
2024-10-16 17:20:39 +00:00
|
|
|
debug "Aggregated attestation statistics", total = len(aggregates),
|
2021-07-19 14:31:02 +00:00
|
|
|
succeed = statistics[0], failed_to_deliver = statistics[1],
|
|
|
|
not_accepted = statistics[2], delay = delay, slot = slot,
|
|
|
|
committee_index = committeeIndex
|
2021-07-15 08:17:32 +00:00
|
|
|
|
2021-07-13 11:15:07 +00:00
|
|
|
else:
|
2021-07-19 14:31:02 +00:00
|
|
|
debug "No aggregate and proofs scheduled for slot", slot = slot,
|
2021-07-15 08:17:32 +00:00
|
|
|
committee_index = committeeIndex
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
proc publishAttestationsAndAggregates(
|
|
|
|
service: AttestationServiceRef,
|
|
|
|
slot: Slot,
|
|
|
|
committee_index: CommitteeIndex,
|
|
|
|
duties: seq[DutyAndProof]
|
|
|
|
) {.async: (raises: [CancelledError]).} =
|
2021-07-13 11:15:07 +00:00
|
|
|
let vc = service.client
|
|
|
|
|
|
|
|
block:
|
2022-01-11 10:01:54 +00:00
|
|
|
let delay = vc.getDelay(slot.attestation_deadline())
|
2021-07-19 14:31:02 +00:00
|
|
|
debug "Producing attestations", delay = delay, slot = slot,
|
|
|
|
committee_index = committee_index,
|
|
|
|
duties_count = len(duties)
|
2021-07-13 11:15:07 +00:00
|
|
|
let ad =
|
|
|
|
try:
|
2021-07-19 14:31:02 +00:00
|
|
|
await service.produceAndPublishAttestations(slot, committee_index, duties)
|
2023-02-23 00:11:00 +00:00
|
|
|
except ValidatorApiError as exc:
|
2023-03-03 20:20:01 +00:00
|
|
|
warn "Unable to proceed attestations", slot = slot,
|
|
|
|
committee_index = committee_index, duties_count = len(duties),
|
|
|
|
reason = exc.getFailureReason()
|
2021-07-19 14:31:02 +00:00
|
|
|
return
|
2022-07-14 21:11:25 +00:00
|
|
|
except CancelledError as exc:
|
2022-07-13 14:43:57 +00:00
|
|
|
debug "Publish attestation request was interrupted"
|
2022-07-14 21:11:25 +00:00
|
|
|
raise exc
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2021-08-23 10:41:48 +00:00
|
|
|
let aggregateTime =
|
|
|
|
# chronos.Duration substraction could not return negative value, in such
|
|
|
|
# case it will return `ZeroDuration`.
|
2021-12-21 18:56:08 +00:00
|
|
|
vc.beaconClock.durationToNextSlot() - OneThirdDuration
|
2021-07-13 11:15:07 +00:00
|
|
|
if aggregateTime != ZeroDuration:
|
|
|
|
await sleepAsync(aggregateTime)
|
|
|
|
|
|
|
|
block:
|
2022-01-11 10:01:54 +00:00
|
|
|
let delay = vc.getDelay(slot.aggregate_deadline())
|
2021-07-19 14:31:02 +00:00
|
|
|
debug "Producing aggregate and proofs", delay = delay
|
2021-07-13 11:15:07 +00:00
|
|
|
await service.produceAndPublishAggregates(ad, duties)
|
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
proc produceAndPublishAttestationsV2*(
|
|
|
|
service: AttestationServiceRef,
|
|
|
|
slot: Slot,
|
|
|
|
duties: seq[DutyAndProof]
|
|
|
|
): Future[AttestationData] {.
|
|
|
|
async: (raises: [CancelledError, ValidatorApiError]).} =
|
|
|
|
doAssert(MAX_VALIDATORS_PER_COMMITTEE <= uint64(high(int)))
|
|
|
|
let
|
|
|
|
vc = service.client
|
|
|
|
fork = vc.forkAtEpoch(slot.epoch)
|
|
|
|
data = await vc.produceAttestationData(slot, CommitteeIndex(0),
|
|
|
|
ApiStrategyKind.Best)
|
|
|
|
registeredRes =
|
|
|
|
vc.attachedValidators[].slashingProtection.withContext:
|
|
|
|
var tmp: seq[RegisteredAttestation]
|
|
|
|
for duty in duties:
|
|
|
|
if (duty.data.slot != data.slot):
|
|
|
|
warn "Inconsistent validator duties during attestation signing",
|
|
|
|
pubkey = shortLog(duty.data.pubkey),
|
|
|
|
duty_slot = duty.data.slot,
|
|
|
|
duty_index = duty.data.committee_index,
|
|
|
|
attestation_slot = data.slot
|
|
|
|
continue
|
|
|
|
|
|
|
|
let validator =
|
|
|
|
vc.getValidatorForDuties(duty.data.pubkey, duty.data.slot).valueOr:
|
|
|
|
continue
|
|
|
|
|
|
|
|
doAssert(validator.index.isSome())
|
|
|
|
let validator_index = validator.index.get()
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
validator = validatorLog(validator)
|
|
|
|
|
|
|
|
# TODO: signing_root is recomputed in getAttestationSignature just
|
|
|
|
# after, but not for locally attached validators.
|
|
|
|
let
|
|
|
|
signingRoot =
|
|
|
|
compute_attestation_signing_root(
|
|
|
|
fork, vc.beaconGenesis.genesis_validators_root, data)
|
|
|
|
registered =
|
|
|
|
registerAttestationInContext(
|
|
|
|
validator_index, validator.pubkey, data.source.epoch,
|
|
|
|
data.target.epoch, signingRoot)
|
|
|
|
|
|
|
|
if registered.isErr():
|
|
|
|
warn "Slashing protection activated for attestation",
|
|
|
|
attestationData = shortLog(data),
|
|
|
|
signingRoot = shortLog(signingRoot),
|
|
|
|
badVoteDetails = $registered.error()
|
|
|
|
continue
|
|
|
|
|
|
|
|
tmp.add(RegisteredAttestation(
|
|
|
|
validator: validator,
|
|
|
|
committee_index: duty.data.committee_index,
|
|
|
|
index_in_committee: duty.data.validator_committee_index,
|
|
|
|
committee_len: int(duty.data.committee_length),
|
|
|
|
data: data
|
|
|
|
))
|
|
|
|
tmp
|
|
|
|
|
|
|
|
if registeredRes.isErr():
|
|
|
|
warn "Could not update slashing database, skipping attestation duties",
|
|
|
|
reason = registeredRes.error()
|
|
|
|
return
|
|
|
|
|
|
|
|
let
|
|
|
|
pendingAttestations = registeredRes[].mapIt(service.serveAttestation(it))
|
|
|
|
statistics =
|
|
|
|
block:
|
|
|
|
var errored, succeed, failed = 0
|
|
|
|
try:
|
|
|
|
await allFutures(pendingAttestations)
|
|
|
|
except CancelledError as exc:
|
|
|
|
let pending = pendingAttestations
|
|
|
|
.filterIt(not(it.finished())).mapIt(it.cancelAndWait())
|
|
|
|
await noCancel allFutures(pending)
|
|
|
|
raise exc
|
|
|
|
|
|
|
|
for future in pendingAttestations:
|
|
|
|
if future.completed():
|
|
|
|
if future.value:
|
|
|
|
inc(succeed)
|
|
|
|
else:
|
|
|
|
inc(failed)
|
|
|
|
else:
|
|
|
|
inc(errored)
|
|
|
|
(succeed, errored, failed)
|
|
|
|
|
|
|
|
delay = vc.getDelay(slot.attestation_deadline())
|
|
|
|
|
|
|
|
debug "Attestation statistics", total = len(pendingAttestations),
|
|
|
|
succeed = statistics[0], failed_to_deliver = statistics[1],
|
|
|
|
not_accepted = statistics[2], delay = delay, slot = slot,
|
|
|
|
duties_count = len(duties)
|
|
|
|
data
|
|
|
|
|
|
|
|
proc produceAndPublishAggregatesV2(
|
|
|
|
service: AttestationServiceRef,
|
|
|
|
adata: AttestationData,
|
|
|
|
duties: seq[DutyAndProof]
|
|
|
|
) {.async: (raises: [CancelledError]).} =
|
|
|
|
let
|
|
|
|
vc = service.client
|
|
|
|
slot = adata.slot
|
|
|
|
attestationRoot = adata.hash_tree_root()
|
|
|
|
|
|
|
|
let aggregateItems =
|
|
|
|
block:
|
|
|
|
var res: seq[AggregateItem]
|
|
|
|
for duty in duties:
|
|
|
|
let validator =
|
|
|
|
vc.getValidatorForDuties(duty.data.pubkey, slot).valueOr:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if duty.data.slot != slot:
|
|
|
|
warn "Inconsistent validator duties during aggregate signing",
|
|
|
|
duty_slot = duty.data.slot, slot = slot,
|
|
|
|
duty_committee_index = duty.data.committee_index
|
|
|
|
continue
|
|
|
|
if duty.slotSig.isSome():
|
|
|
|
let slotSignature = duty.slotSig.get()
|
|
|
|
if is_aggregator(duty.data.committee_length, slotSignature):
|
|
|
|
res.add(AggregateItem(
|
|
|
|
aggregator_index: uint64(duty.data.validator_index),
|
|
|
|
committee_index: duty.data.committee_index,
|
|
|
|
selection_proof: slotSignature,
|
|
|
|
validator: validator
|
|
|
|
))
|
|
|
|
res
|
|
|
|
|
|
|
|
if len(aggregateItems) == 0:
|
|
|
|
debug "No aggregate and proofs scheduled for slot", slot = slot
|
|
|
|
return
|
|
|
|
|
|
|
|
# All duties should be sorted by `committee_index`.
|
|
|
|
let committee_index = duties[0].data.committee_index
|
|
|
|
|
|
|
|
let aggregates =
|
2021-07-13 11:15:07 +00:00
|
|
|
block:
|
2024-10-16 17:20:39 +00:00
|
|
|
let attestation =
|
|
|
|
try:
|
|
|
|
await vc.getAggregatedAttestationV2(slot, attestationRoot,
|
|
|
|
committee_index,
|
|
|
|
ApiStrategyKind.Best)
|
|
|
|
except ValidatorApiError as exc:
|
|
|
|
warn "Unable to get aggregated attestation data", slot = slot,
|
|
|
|
attestation_root = shortLog(attestationRoot),
|
|
|
|
reason = exc.getFailureReason()
|
|
|
|
return
|
|
|
|
except CancelledError as exc:
|
|
|
|
debug "Aggregated attestation request was interrupted"
|
|
|
|
raise exc
|
|
|
|
|
|
|
|
if isLowestScoreAggregatedAttestation(attestation):
|
|
|
|
warn "Aggregated attestation with the root was not seen by the " &
|
|
|
|
"beacon node",
|
|
|
|
attestation_root = shortLog(attestationRoot)
|
|
|
|
return
|
|
|
|
|
|
|
|
var res: seq[Future[bool].Raising([CancelledError])]
|
|
|
|
for item in aggregateItems:
|
|
|
|
withAttestation(attestation):
|
|
|
|
when consensusFork > ConsensusFork.Deneb:
|
|
|
|
let proof =
|
|
|
|
electra.AggregateAndProof(
|
|
|
|
aggregator_index: item.aggregator_index,
|
|
|
|
aggregate: forkyAttestation,
|
|
|
|
selection_proof: item.selection_proof
|
|
|
|
)
|
|
|
|
res.add(service.serveAggregateAndProofV2(proof, item.validator))
|
|
|
|
else:
|
|
|
|
let proof =
|
|
|
|
phase0.AggregateAndProof(
|
|
|
|
aggregator_index: item.aggregator_index,
|
|
|
|
aggregate: forkyAttestation,
|
|
|
|
selection_proof: item.selection_proof
|
|
|
|
)
|
|
|
|
res.add(service.serveAggregateAndProofV2(proof, item.validator))
|
2021-07-13 11:15:07 +00:00
|
|
|
res
|
2022-10-14 12:19:17 +00:00
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
let statistics =
|
|
|
|
block:
|
|
|
|
var errored, succeed, failed = 0
|
|
|
|
try:
|
|
|
|
await allFutures(aggregates)
|
|
|
|
except CancelledError as exc:
|
|
|
|
let pending = aggregates
|
|
|
|
.filterIt(not(it.finished())).mapIt(it.cancelAndWait())
|
|
|
|
await noCancel allFutures(pending)
|
|
|
|
raise exc
|
|
|
|
|
|
|
|
for future in aggregates:
|
|
|
|
if future.completed():
|
|
|
|
if future.value:
|
|
|
|
inc(succeed)
|
|
|
|
else:
|
|
|
|
inc(failed)
|
|
|
|
else:
|
|
|
|
inc(errored)
|
|
|
|
(succeed, errored, failed)
|
|
|
|
|
|
|
|
let delay = vc.getDelay(slot.aggregate_deadline())
|
|
|
|
debug "Aggregated attestation statistics", total = len(aggregates),
|
|
|
|
succeed = statistics[0], failed_to_deliver = statistics[1],
|
|
|
|
not_accepted = statistics[2], delay = delay, slot = slot,
|
|
|
|
committee_index = committeeIndex
|
|
|
|
|
|
|
|
proc publishAttestationsAndAggregatesV2(
|
|
|
|
service: AttestationServiceRef,
|
|
|
|
slot: Slot,
|
|
|
|
duties: seq[DutyAndProof]
|
|
|
|
) {.async: (raises: [CancelledError]).} =
|
|
|
|
let
|
|
|
|
vc = service.client
|
|
|
|
|
|
|
|
block:
|
|
|
|
let delay = vc.getDelay(slot.attestation_deadline())
|
|
|
|
debug "Producing attestations", delay = delay, slot = slot,
|
|
|
|
duties_count = len(duties)
|
|
|
|
|
|
|
|
let ad =
|
|
|
|
try:
|
|
|
|
await service.produceAndPublishAttestationsV2(slot, duties)
|
|
|
|
except ValidatorApiError as exc:
|
|
|
|
warn "Unable to proceed attestations", slot = slot,
|
|
|
|
duties_count = len(duties), reason = exc.getFailureReason()
|
|
|
|
return
|
|
|
|
except CancelledError as exc:
|
|
|
|
debug "Publish attestation request was interrupted"
|
|
|
|
raise exc
|
|
|
|
|
|
|
|
let aggregateTime =
|
|
|
|
# chronos.Duration substraction could not return negative value, in such
|
|
|
|
# case it will return `ZeroDuration`.
|
|
|
|
vc.beaconClock.durationToNextSlot() - OneThirdDuration
|
|
|
|
if aggregateTime != ZeroDuration:
|
|
|
|
await sleepAsync(aggregateTime)
|
|
|
|
|
|
|
|
block:
|
|
|
|
let
|
|
|
|
delay = vc.getDelay(slot.aggregate_deadline())
|
|
|
|
dutiesByCommittee = getAttesterDutiesByCommittee(duties)
|
|
|
|
debug "Producing aggregate and proofs", delay = delay
|
|
|
|
var tasks: seq[Future[void].Raising([CancelledError])]
|
|
|
|
try:
|
|
|
|
for index, cduties in dutiesByCommittee:
|
|
|
|
tasks.add(service.produceAndPublishAggregatesV2(ad, cduties))
|
|
|
|
await allFutures(tasks)
|
|
|
|
except CancelledError as exc:
|
|
|
|
# Cancelling all the pending tasks.
|
|
|
|
let pending = tasks.filterIt(not(it.finished())).mapIt(it.cancelAndWait())
|
|
|
|
await noCancel allFutures(pending)
|
|
|
|
raise exc
|
|
|
|
|
|
|
|
proc spawnAttestationTasks(
|
|
|
|
service: AttestationServiceRef,
|
|
|
|
slot: Slot
|
|
|
|
) {.async: (raises: [CancelledError]).} =
|
|
|
|
let
|
|
|
|
vc = service.client
|
|
|
|
dutiesByCommittee =
|
|
|
|
getAttesterDutiesByCommittee(vc.getAttesterDutiesForSlot(slot))
|
|
|
|
|
2023-11-19 13:08:07 +00:00
|
|
|
# Waiting for blocks to be published before attesting.
|
|
|
|
await vc.waitForBlock(slot, attestationSlotOffset)
|
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
var tasks: seq[Future[void].Raising([CancelledError])]
|
2023-04-17 21:31:54 +00:00
|
|
|
try:
|
|
|
|
for index, duties in dutiesByCommittee:
|
|
|
|
tasks.add(service.publishAttestationsAndAggregates(slot, index, duties))
|
|
|
|
let timeout = vc.beaconClock.durationToNextSlot()
|
|
|
|
await allFutures(tasks).wait(timeout)
|
|
|
|
except AsyncTimeoutError:
|
|
|
|
# Cancelling all the pending tasks.
|
|
|
|
let pending = tasks.filterIt(not(it.finished())).mapIt(it.cancelAndWait())
|
|
|
|
await allFutures(pending)
|
|
|
|
except CancelledError as exc:
|
|
|
|
# Cancelling all the pending tasks.
|
|
|
|
let pending = tasks.filterIt(not(it.finished())).mapIt(it.cancelAndWait())
|
2023-09-24 07:28:09 +00:00
|
|
|
await noCancel allFutures(pending)
|
2023-04-17 21:31:54 +00:00
|
|
|
raise exc
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
proc spawnAttestationTasksV2(
|
|
|
|
service: AttestationServiceRef,
|
|
|
|
slot: Slot
|
|
|
|
) {.async: (raises: [CancelledError]).} =
|
|
|
|
let
|
|
|
|
vc = service.client
|
|
|
|
duties = vc.getAttesterDutiesForSlot(slot)
|
|
|
|
|
|
|
|
# Waiting for blocks to be published before attesting.
|
|
|
|
await vc.waitForBlock(slot, attestationSlotOffset)
|
|
|
|
|
|
|
|
try:
|
|
|
|
let timeout = vc.beaconClock.durationToNextSlot()
|
|
|
|
await service.publishAttestationsAndAggregatesV2(slot, duties).wait(timeout)
|
|
|
|
except AsyncTimeoutError:
|
|
|
|
discard
|
|
|
|
except CancelledError as exc:
|
|
|
|
# Cancelling all the pending tasks.
|
|
|
|
raise exc
|
|
|
|
|
|
|
|
proc mainLoop(service: AttestationServiceRef) {.async: (raises: []).} =
|
2021-07-13 11:15:07 +00:00
|
|
|
let vc = service.client
|
|
|
|
service.state = ServiceState.Running
|
2022-07-13 14:43:57 +00:00
|
|
|
debug "Service started"
|
|
|
|
|
2023-04-17 21:31:54 +00:00
|
|
|
debug "Attester loop is waiting for initialization"
|
|
|
|
try:
|
|
|
|
await allFutures(
|
|
|
|
vc.preGenesisEvent.wait(),
|
|
|
|
vc.genesisEvent.wait(),
|
|
|
|
vc.indicesAvailable.wait(),
|
|
|
|
vc.forksAvailable.wait()
|
|
|
|
)
|
|
|
|
except CancelledError:
|
|
|
|
debug "Service interrupted"
|
|
|
|
return
|
|
|
|
|
|
|
|
doAssert(len(vc.forks) > 0, "Fork schedule must not be empty at this point")
|
|
|
|
|
|
|
|
var currentSlot: Opt[Slot]
|
2022-07-13 14:43:57 +00:00
|
|
|
while true:
|
2024-10-16 17:20:39 +00:00
|
|
|
try:
|
|
|
|
let
|
|
|
|
# We use zero offset here, because we do waiting in
|
|
|
|
# waitForBlock(attestationSlotOffset).
|
|
|
|
slot = await vc.checkedWaitForNextSlot(currentSlot,
|
|
|
|
ZeroTimeDiff, false)
|
|
|
|
if slot.isNone():
|
|
|
|
debug "System time adjusted backwards significantly, exiting"
|
|
|
|
return
|
|
|
|
|
|
|
|
currentSlot = slot
|
|
|
|
if vc.isPastElectraFork(currentSlot.get().epoch()):
|
|
|
|
await service.spawnAttestationTasksV2(currentSlot.get())
|
|
|
|
else:
|
|
|
|
await service.spawnAttestationTasks(currentSlot.get())
|
|
|
|
except CancelledError:
|
|
|
|
debug "Service interrupted"
|
|
|
|
return
|
|
|
|
|
|
|
|
proc init*(
|
|
|
|
t: typedesc[AttestationServiceRef],
|
|
|
|
vc: ValidatorClientRef
|
|
|
|
): Future[AttestationServiceRef] {.async: (raises: []).} =
|
2022-07-14 21:11:25 +00:00
|
|
|
logScope: service = ServiceName
|
|
|
|
let res = AttestationServiceRef(name: ServiceName,
|
2022-07-13 14:43:57 +00:00
|
|
|
client: vc, state: ServiceState.Initialized)
|
2022-07-14 21:11:25 +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: AttestationServiceRef) =
|
|
|
|
service.lifeFut = mainLoop(service)
|