2021-07-13 11:15:07 +00:00
|
|
|
import std/[sets, sequtils]
|
|
|
|
import chronicles
|
2021-08-24 19:49:51 +00:00
|
|
|
import "."/[common, api, block_service]
|
2021-07-13 11:15:07 +00:00
|
|
|
|
|
|
|
logScope: service = "attestation_service"
|
|
|
|
|
2021-07-15 08:17:32 +00:00
|
|
|
type
|
|
|
|
AggregateItem* = object
|
|
|
|
aggregator_index: uint64
|
|
|
|
selection_proof: ValidatorSig
|
|
|
|
validator: AttachedValidator
|
|
|
|
|
2021-07-13 11:15:07 +00:00
|
|
|
proc serveAttestation(service: AttestationServiceRef, adata: AttestationData,
|
2021-07-15 08:17:32 +00:00
|
|
|
duty: DutyAndProof): Future[bool] {.async.} =
|
2021-07-13 11:15:07 +00:00
|
|
|
let vc = service.client
|
2021-07-19 14:31:02 +00:00
|
|
|
let validator =
|
|
|
|
block:
|
|
|
|
let res = vc.getValidator(duty.data.pubkey)
|
|
|
|
if res.isNone():
|
|
|
|
return false
|
|
|
|
res.get()
|
2022-02-16 11:31:23 +00:00
|
|
|
let fork = vc.forkAtEpoch(adata.slot.epoch)
|
2021-07-13 11:15:07 +00:00
|
|
|
|
|
|
|
# TODO: signing_root is recomputed in signBlockProposal just after,
|
|
|
|
# but not for locally attached validators.
|
|
|
|
let signingRoot =
|
2021-12-09 12:56:54 +00:00
|
|
|
compute_attestation_signing_root(
|
|
|
|
fork, vc.beaconGenesis.genesis_validators_root, adata)
|
2021-08-23 10:41:48 +00:00
|
|
|
let attestationRoot = adata.hash_tree_root()
|
2021-07-13 11:15:07 +00:00
|
|
|
|
|
|
|
let vindex = validator.index.get()
|
|
|
|
let notSlashable = vc.attachedValidators.slashingProtection
|
2021-12-22 12:37:31 +00:00
|
|
|
.registerAttestation(vindex, validator.pubkey,
|
2021-07-13 11:15:07 +00:00
|
|
|
adata.source.epoch,
|
|
|
|
adata.target.epoch, signingRoot)
|
|
|
|
if notSlashable.isErr():
|
2021-07-19 14:31:02 +00:00
|
|
|
warn "Slashing protection activated for attestation",
|
|
|
|
slot = duty.data.slot,
|
|
|
|
validator = shortLog(validator),
|
|
|
|
validator_index = vindex, badVoteDetails = $notSlashable.error
|
2021-07-13 11:15:07 +00:00
|
|
|
return false
|
|
|
|
|
2021-11-30 01:20:21 +00:00
|
|
|
let attestation =
|
|
|
|
block:
|
|
|
|
let res = await validator.produceAndSignAttestation(adata,
|
|
|
|
int(duty.data.committee_length),
|
|
|
|
Natural(duty.data.validator_committee_index),
|
|
|
|
fork, vc.beaconGenesis.genesis_validators_root)
|
|
|
|
if res.isErr():
|
|
|
|
error "Unable to sign attestation", validator = shortLog(validator),
|
|
|
|
error_msg = res.error()
|
|
|
|
return false
|
|
|
|
res.get()
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2021-08-23 10:41:48 +00:00
|
|
|
debug "Sending attestation", attestation = shortLog(attestation),
|
|
|
|
validator = shortLog(validator), validator_index = vindex,
|
|
|
|
attestation_root = shortLog(attestationRoot),
|
2022-01-11 10:01:54 +00:00
|
|
|
delay = vc.getDelay(adata.slot.attestation_deadline())
|
2021-08-23 10:41:48 +00:00
|
|
|
|
2021-07-13 11:15:07 +00:00
|
|
|
let res =
|
|
|
|
try:
|
|
|
|
await vc.submitPoolAttestations(@[attestation])
|
2021-07-19 14:31:02 +00:00
|
|
|
except ValidatorApiError:
|
|
|
|
error "Unable to publish attestation",
|
|
|
|
attestation = shortLog(attestation),
|
|
|
|
validator = shortLog(validator),
|
|
|
|
validator_index = vindex
|
|
|
|
return false
|
|
|
|
except CatchableError as exc:
|
|
|
|
error "Unexpected error occured while publishing attestation",
|
|
|
|
attestation = shortLog(attestation),
|
|
|
|
validator = shortLog(validator),
|
|
|
|
validator_index = vindex,
|
|
|
|
err_name = exc.name, err_msg = exc.msg
|
|
|
|
return false
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2022-01-11 10:01:54 +00:00
|
|
|
let delay = vc.getDelay(adata.slot.attestation_deadline())
|
2021-07-13 11:15:07 +00:00
|
|
|
if res:
|
2021-07-19 14:31:02 +00:00
|
|
|
notice "Attestation published", attestation = shortLog(attestation),
|
|
|
|
validator = shortLog(validator),
|
|
|
|
validator_index = vindex,
|
|
|
|
delay = delay,
|
2021-08-23 10:41:48 +00:00
|
|
|
attestation_root = attestationRoot
|
2021-07-13 11:15:07 +00:00
|
|
|
else:
|
|
|
|
warn "Attestation was not accepted by beacon node",
|
2021-07-19 14:31:02 +00:00
|
|
|
attestation = shortLog(attestation),
|
|
|
|
validator = shortLog(validator),
|
2021-08-23 10:41:48 +00:00
|
|
|
validator_index = vindex, delay = delay
|
2021-07-19 14:31:02 +00:00
|
|
|
return res
|
2021-07-15 08:17:32 +00:00
|
|
|
|
|
|
|
proc serveAggregateAndProof*(service: AttestationServiceRef,
|
|
|
|
proof: AggregateAndProof,
|
|
|
|
validator: AttachedValidator): Future[bool] {.
|
|
|
|
async.} =
|
|
|
|
let
|
|
|
|
vc = service.client
|
|
|
|
genesisRoot = vc.beaconGenesis.genesis_validators_root
|
2022-02-16 11:31:23 +00:00
|
|
|
slot = proof.aggregate.data.slot
|
|
|
|
fork = vc.forkAtEpoch(slot.epoch)
|
|
|
|
|
|
|
|
debug "Signing aggregate", validator = shortLog(validator),
|
|
|
|
attestation = shortLog(proof.aggregate), fork = fork
|
2021-07-15 08:17:32 +00:00
|
|
|
|
2021-11-30 01:20:21 +00:00
|
|
|
let signature =
|
|
|
|
block:
|
|
|
|
let res = await signAggregateAndProof(validator, proof, fork,
|
|
|
|
genesisRoot)
|
|
|
|
if res.isErr():
|
|
|
|
error "Unable to sign aggregate and proof using remote signer",
|
|
|
|
validator = shortLog(validator),
|
2022-01-11 10:01:54 +00:00
|
|
|
attestation = shortLog(proof.aggregate),
|
2021-11-30 01:20:21 +00:00
|
|
|
error_msg = res.error()
|
|
|
|
return false
|
|
|
|
res.get()
|
2021-07-15 08:17:32 +00:00
|
|
|
let signedProof = SignedAggregateAndProof(message: proof,
|
|
|
|
signature: signature)
|
2021-07-19 14:31:02 +00:00
|
|
|
|
|
|
|
let vindex = validator.index.get()
|
2021-08-23 10:41:48 +00:00
|
|
|
|
2022-02-16 11:31:23 +00:00
|
|
|
debug "Sending aggregated attestation", fork = fork,
|
2021-08-23 10:41:48 +00:00
|
|
|
attestation = shortLog(signedProof.message.aggregate),
|
|
|
|
validator = shortLog(validator), validator_index = vindex,
|
2022-01-11 10:01:54 +00:00
|
|
|
delay = vc.getDelay(slot.aggregate_deadline())
|
2021-08-23 10:41:48 +00:00
|
|
|
|
2021-07-19 14:31:02 +00:00
|
|
|
let res =
|
|
|
|
try:
|
|
|
|
await vc.publishAggregateAndProofs(@[signedProof])
|
|
|
|
except ValidatorApiError:
|
|
|
|
error "Unable to publish aggregated attestation",
|
|
|
|
attestation = shortLog(signedProof.message.aggregate),
|
|
|
|
validator = shortLog(validator),
|
|
|
|
validator_index = vindex
|
|
|
|
return false
|
|
|
|
except CatchableError as exc:
|
|
|
|
error "Unexpected error occured while publishing aggregated attestation",
|
|
|
|
attestation = shortLog(signedProof.message.aggregate),
|
|
|
|
validator = shortLog(validator),
|
|
|
|
err_name = exc.name, err_msg = exc.msg
|
|
|
|
return false
|
|
|
|
|
|
|
|
if res:
|
|
|
|
notice "Aggregated attestation published",
|
|
|
|
attestation = shortLog(signedProof.message.aggregate),
|
|
|
|
validator = shortLog(validator),
|
2022-01-11 10:01:54 +00:00
|
|
|
validator_index = vindex
|
2021-07-19 14:31:02 +00:00
|
|
|
else:
|
|
|
|
warn "Aggregated attestation was not accepted by beacon node",
|
|
|
|
attestation = shortLog(signedProof.message.aggregate),
|
|
|
|
validator = shortLog(validator),
|
2022-01-11 10:01:54 +00:00
|
|
|
validator_index = vindex
|
2021-07-19 14:31:02 +00:00
|
|
|
return res
|
2021-07-13 11:15:07 +00:00
|
|
|
|
|
|
|
proc produceAndPublishAttestations*(service: AttestationServiceRef,
|
|
|
|
slot: Slot, committee_index: CommitteeIndex,
|
2021-07-15 08:17:32 +00:00
|
|
|
duties: seq[DutyAndProof]
|
2021-07-13 11:15:07 +00:00
|
|
|
): Future[AttestationData] {.
|
|
|
|
async.} =
|
|
|
|
doAssert(MAX_VALIDATORS_PER_COMMITTEE <= uint64(high(int)))
|
|
|
|
let vc = service.client
|
|
|
|
|
|
|
|
# This call could raise ValidatorApiError, but it is handled in
|
|
|
|
# publishAttestationsAndAggregates().
|
|
|
|
let ad = await vc.produceAttestationData(slot, committee_index)
|
|
|
|
|
|
|
|
let pendingAttestations =
|
|
|
|
block:
|
|
|
|
var res: seq[Future[bool]]
|
|
|
|
for duty in duties:
|
2021-07-15 08:17:32 +00:00
|
|
|
debug "Serving attestation duty", duty = duty.data, epoch = slot.epoch()
|
|
|
|
if (duty.data.slot != ad.slot) or
|
|
|
|
(uint64(duty.data.committee_index) != ad.index):
|
2021-07-13 11:15:07 +00:00
|
|
|
error "Inconsistent validator duties during attestation signing",
|
2021-07-19 14:31:02 +00:00
|
|
|
validator = shortLog(duty.data.pubkey),
|
|
|
|
duty_slot = duty.data.slot,
|
2021-07-15 08:17:32 +00:00
|
|
|
duty_index = duty.data.committee_index,
|
2021-07-13 11:15:07 +00:00
|
|
|
attestation_slot = ad.slot, attestation_index = ad.index
|
|
|
|
continue
|
|
|
|
res.add(service.serveAttestation(ad, duty))
|
|
|
|
res
|
|
|
|
|
|
|
|
let statistics =
|
|
|
|
block:
|
|
|
|
var errored, succeed, failed = 0
|
|
|
|
try:
|
|
|
|
await allFutures(pendingAttestations)
|
|
|
|
except CancelledError:
|
|
|
|
for fut in pendingAttestations:
|
|
|
|
if not(fut.finished()):
|
|
|
|
fut.cancel()
|
|
|
|
await allFutures(pendingAttestations)
|
|
|
|
|
|
|
|
for future in pendingAttestations:
|
|
|
|
if future.done():
|
|
|
|
if future.read():
|
|
|
|
inc(succeed)
|
|
|
|
else:
|
|
|
|
inc(failed)
|
|
|
|
else:
|
|
|
|
inc(errored)
|
|
|
|
(succeed, errored, failed)
|
|
|
|
|
2022-01-11 10:01:54 +00:00
|
|
|
let delay = vc.getDelay(slot.attestation_deadline())
|
2021-07-13 11:15:07 +00:00
|
|
|
debug "Attestation statistics", total = len(pendingAttestations),
|
|
|
|
succeed = statistics[0], failed_to_deliver = statistics[1],
|
|
|
|
not_accepted = statistics[2], delay = delay, slot = slot,
|
2021-11-05 15:39:47 +00:00
|
|
|
committee_index = committee_index, duties_count = len(duties)
|
2021-07-13 11:15:07 +00:00
|
|
|
|
|
|
|
return ad
|
|
|
|
|
|
|
|
proc produceAndPublishAggregates(service: AttestationServiceRef,
|
|
|
|
adata: AttestationData,
|
2021-07-15 08:17:32 +00:00
|
|
|
duties: seq[DutyAndProof]) {.async.} =
|
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:
|
2021-07-15 08:17:32 +00:00
|
|
|
let validator = vc.attachedValidators.getValidator(duty.data.pubkey)
|
|
|
|
if not(isNil(validator)):
|
|
|
|
if (duty.data.slot != slot) or
|
|
|
|
(duty.data.committee_index != committeeIndex):
|
|
|
|
error "Inconsistent validator duties during aggregate signing",
|
|
|
|
duty_slot = duty.data.slot, slot = slot,
|
|
|
|
duty_committee_index = duty.data.committee_index,
|
|
|
|
committee_index = committeeIndex
|
|
|
|
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),
|
|
|
|
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:
|
|
|
|
let aggAttestation =
|
2021-07-13 11:15:07 +00:00
|
|
|
try:
|
2021-07-15 08:17:32 +00:00
|
|
|
await vc.getAggregatedAttestation(slot, attestationRoot)
|
2021-07-13 11:15:07 +00:00
|
|
|
except ValidatorApiError:
|
2021-07-19 14:31:02 +00:00
|
|
|
error "Unable to get aggregated attestation data", slot = slot,
|
|
|
|
attestation_root = shortLog(attestationRoot)
|
|
|
|
return
|
|
|
|
except CatchableError as exc:
|
|
|
|
error "Unexpected error occured while getting aggregated attestation",
|
|
|
|
slot = slot, attestation_root = shortLog(attestationRoot),
|
|
|
|
err_name = exc.name, err_msg = exc.msg
|
2021-07-13 11:15:07 +00:00
|
|
|
return
|
2021-07-15 08:17:32 +00:00
|
|
|
|
|
|
|
let pendingAggregates =
|
|
|
|
block:
|
|
|
|
var res: seq[Future[bool]]
|
|
|
|
for item in aggregateItems:
|
|
|
|
let proof = AggregateAndProof(
|
|
|
|
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:
|
|
|
|
await allFutures(pendingAggregates)
|
|
|
|
except CancelledError:
|
|
|
|
for fut in pendingAggregates:
|
|
|
|
if not(fut.finished()):
|
|
|
|
fut.cancel()
|
|
|
|
await allFutures(pendingAggregates)
|
|
|
|
|
|
|
|
for future in pendingAggregates:
|
|
|
|
if future.done():
|
|
|
|
if future.read():
|
|
|
|
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())
|
2021-07-19 14:31:02 +00:00
|
|
|
debug "Aggregated attestation statistics", total = len(pendingAggregates),
|
|
|
|
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
|
|
|
|
|
|
|
proc publishAttestationsAndAggregates(service: AttestationServiceRef,
|
|
|
|
slot: Slot,
|
|
|
|
committee_index: CommitteeIndex,
|
2021-07-15 08:17:32 +00:00
|
|
|
duties: seq[DutyAndProof]) {.async.} =
|
2021-07-13 11:15:07 +00:00
|
|
|
let vc = service.client
|
|
|
|
# Waiting for blocks to be published before attesting.
|
|
|
|
let startTime = Moment.now()
|
2021-08-23 10:41:48 +00:00
|
|
|
try:
|
2021-11-25 12:20:36 +00:00
|
|
|
let timeout = attestationSlotOffset # 4.seconds in mainnet
|
2022-01-11 10:01:54 +00:00
|
|
|
await vc.waitForBlockPublished(slot).wait(nanoseconds(timeout.nanoseconds))
|
2021-08-23 10:41:48 +00:00
|
|
|
let dur = Moment.now() - startTime
|
|
|
|
debug "Block proposal awaited", slot = slot, duration = dur
|
|
|
|
except AsyncTimeoutError:
|
|
|
|
let dur = Moment.now() - startTime
|
|
|
|
debug "Block was not produced in time", slot = slot, duration = dur
|
2021-07-13 11:15:07 +00:00
|
|
|
|
|
|
|
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)
|
2021-07-13 11:15:07 +00:00
|
|
|
except ValidatorApiError:
|
2021-07-19 14:31:02 +00:00
|
|
|
error "Unable to proceed attestations", slot = slot,
|
|
|
|
committee_index = committee_index, duties_count = len(duties)
|
|
|
|
return
|
|
|
|
except CatchableError as exc:
|
|
|
|
error "Unexpected error while producing attestations", slot = slot,
|
|
|
|
committee_index = committee_index, duties_count = len(duties),
|
|
|
|
err_name = exc.name, err_msg = exc.msg
|
2021-07-13 11:15:07 +00:00
|
|
|
return
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
proc spawnAttestationTasks(service: AttestationServiceRef,
|
|
|
|
slot: Slot) =
|
|
|
|
let vc = service.client
|
|
|
|
let dutiesByCommittee =
|
|
|
|
block:
|
2021-07-15 08:17:32 +00:00
|
|
|
var res: Table[CommitteeIndex, seq[DutyAndProof]]
|
2021-07-13 11:15:07 +00:00
|
|
|
let attesters = vc.getAttesterDutiesForSlot(slot)
|
2021-07-15 08:17:32 +00:00
|
|
|
var default: seq[DutyAndProof]
|
2021-07-13 11:15:07 +00:00
|
|
|
for item in attesters:
|
2021-07-15 08:17:32 +00:00
|
|
|
res.mgetOrPut(item.data.committee_index, default).add(item)
|
2021-07-13 11:15:07 +00:00
|
|
|
res
|
|
|
|
for index, duties in dutiesByCommittee.pairs():
|
|
|
|
if len(duties) > 0:
|
|
|
|
asyncSpawn service.publishAttestationsAndAggregates(slot, index, duties)
|
|
|
|
|
|
|
|
proc mainLoop(service: AttestationServiceRef) {.async.} =
|
|
|
|
let vc = service.client
|
|
|
|
service.state = ServiceState.Running
|
|
|
|
try:
|
|
|
|
while true:
|
2022-01-11 10:01:54 +00:00
|
|
|
let sleepTime =
|
|
|
|
attestationSlotOffset + vc.beaconClock.durationToNextSlot()
|
|
|
|
|
2021-07-13 11:15:07 +00:00
|
|
|
let sres = vc.getCurrentSlot()
|
|
|
|
if sres.isSome():
|
|
|
|
let currentSlot = sres.get()
|
|
|
|
service.spawnAttestationTasks(currentSlot)
|
|
|
|
await sleepAsync(sleepTime)
|
|
|
|
except CatchableError as exc:
|
|
|
|
warn "Service crashed with unexpected error", err_name = exc.name,
|
|
|
|
err_msg = exc.msg
|
|
|
|
|
|
|
|
proc init*(t: typedesc[AttestationServiceRef],
|
|
|
|
vc: ValidatorClientRef): Future[AttestationServiceRef] {.async.} =
|
|
|
|
debug "Initializing service"
|
|
|
|
var res = AttestationServiceRef(client: vc, state: ServiceState.Initialized)
|
|
|
|
return res
|
|
|
|
|
|
|
|
proc start*(service: AttestationServiceRef) =
|
|
|
|
service.lifeFut = mainLoop(service)
|