2022-06-09 08:50:36 +00:00
|
|
|
# beacon_chain
|
2024-01-06 14:26:56 +00:00
|
|
|
# Copyright (c) 2022-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-05-10 10:03:40 +00:00
|
|
|
import
|
2022-06-09 08:50:36 +00:00
|
|
|
std/sets,
|
2022-07-29 08:36:20 +00:00
|
|
|
metrics, chronicles,
|
2022-05-10 10:03:40 +00:00
|
|
|
../spec/datatypes/[phase0, altair, bellatrix],
|
2022-08-19 10:30:07 +00:00
|
|
|
../spec/eth2_apis/rest_types,
|
|
|
|
../validators/activity_metrics,
|
2023-11-08 12:03:51 +00:00
|
|
|
"."/[common, api, selection_proofs]
|
2022-05-10 10:03:40 +00:00
|
|
|
|
2022-07-14 21:11:25 +00:00
|
|
|
const
|
|
|
|
ServiceName = "sync_committee_service"
|
|
|
|
|
|
|
|
logScope: service = ServiceName
|
2022-07-13 14:43:57 +00:00
|
|
|
|
2022-05-10 10:03:40 +00:00
|
|
|
type
|
|
|
|
ContributionItem* = object
|
|
|
|
aggregator_index: uint64
|
|
|
|
selection_proof: ValidatorSig
|
|
|
|
validator: AttachedValidator
|
|
|
|
subcommitteeIdx: SyncSubcommitteeIndex
|
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
proc serveSyncCommitteeMessage*(
|
|
|
|
service: SyncCommitteeServiceRef,
|
|
|
|
slot: Slot,
|
|
|
|
beaconBlockRoot: Eth2Digest,
|
|
|
|
duty: SyncCommitteeDuty
|
|
|
|
): Future[bool] {.async: (raises: [CancelledError]).} =
|
2022-05-10 10:03:40 +00:00
|
|
|
let
|
|
|
|
vc = service.client
|
2024-03-22 02:37:44 +00:00
|
|
|
startTime = Moment.now()
|
2022-05-10 10:03:40 +00:00
|
|
|
fork = vc.forkAtEpoch(slot.epoch)
|
|
|
|
genesisValidatorsRoot = vc.beaconGenesis.genesis_validators_root
|
2022-11-24 07:46:35 +00:00
|
|
|
vindex = duty.validator_index
|
2023-02-20 11:28:56 +00:00
|
|
|
validator = vc.getValidatorForDuties(
|
|
|
|
duty.pubkey, slot, slashingSafe = true).valueOr: return false
|
2024-03-22 02:37:44 +00:00
|
|
|
|
|
|
|
logScope:
|
|
|
|
validator = validatorLog(validator)
|
|
|
|
block_root = shortLog(beaconBlockRoot)
|
|
|
|
slot = slot
|
|
|
|
|
|
|
|
let
|
2022-05-10 10:03:40 +00:00
|
|
|
message =
|
|
|
|
block:
|
2022-06-29 16:53:59 +00:00
|
|
|
let res = await getSyncCommitteeMessage(validator, fork,
|
|
|
|
genesisValidatorsRoot,
|
|
|
|
slot, beaconBlockRoot)
|
2022-05-10 10:03:40 +00:00
|
|
|
if res.isErr():
|
2024-03-22 02:37:44 +00:00
|
|
|
warn "Unable to sign committee message using remote signer"
|
2022-05-10 10:03:40 +00:00
|
|
|
return
|
|
|
|
res.get()
|
|
|
|
|
2024-03-22 02:37:44 +00:00
|
|
|
logScope:
|
|
|
|
message = shortLog(message)
|
|
|
|
|
|
|
|
debug "Sending sync committee message",
|
2022-05-10 10:03:40 +00:00
|
|
|
delay = vc.getDelay(message.slot.sync_committee_message_deadline())
|
|
|
|
|
|
|
|
let res =
|
|
|
|
try:
|
2022-09-29 07:57:14 +00:00
|
|
|
await vc.submitPoolSyncCommitteeSignature(message, 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 sync committee message",
|
|
|
|
reason = exc.getFailureReason()
|
2022-05-10 10:03:40 +00:00
|
|
|
return false
|
2022-07-13 14:43:57 +00:00
|
|
|
except CancelledError:
|
|
|
|
debug "Publish sync committee message request was interrupted"
|
|
|
|
return false
|
2022-05-10 10:03:40 +00:00
|
|
|
|
2024-03-22 02:37:44 +00:00
|
|
|
let
|
|
|
|
delay = vc.getDelay(message.slot.sync_committee_message_deadline())
|
|
|
|
dur = Moment.now() - startTime
|
|
|
|
|
2022-05-10 10:03:40 +00:00
|
|
|
if res:
|
2022-07-29 08:36:20 +00:00
|
|
|
beacon_sync_committee_messages_sent.inc()
|
|
|
|
beacon_sync_committee_message_sent_delay.observe(delay.toFloatSeconds())
|
2022-05-10 10:03:40 +00:00
|
|
|
notice "Sync committee message published",
|
2024-03-22 02:37:44 +00:00
|
|
|
validator_index = vindex, delay = delay, duration = dur
|
2022-05-10 10:03:40 +00:00
|
|
|
else:
|
|
|
|
warn "Sync committee message was not accepted by beacon node",
|
2024-03-22 02:37:44 +00:00
|
|
|
validator_index = vindex, delay = delay, duration = dur
|
|
|
|
res
|
2022-05-10 10:03:40 +00:00
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
proc produceAndPublishSyncCommitteeMessages(
|
|
|
|
service: SyncCommitteeServiceRef,
|
|
|
|
slot: Slot,
|
|
|
|
beaconBlockRoot: Eth2Digest,
|
|
|
|
duties: seq[SyncCommitteeDuty]
|
|
|
|
): Future[void] {.async: (raises: [CancelledError]).} =
|
2024-03-22 02:37:44 +00:00
|
|
|
let
|
|
|
|
vc = service.client
|
|
|
|
startTime = Moment.now()
|
2022-05-10 10:03:40 +00:00
|
|
|
|
|
|
|
let pendingSyncCommitteeMessages =
|
|
|
|
block:
|
|
|
|
var res: seq[Future[bool]]
|
|
|
|
for duty in duties:
|
2022-11-24 07:46:35 +00:00
|
|
|
debug "Serving sync message duty", duty, epoch = slot.epoch()
|
2022-05-10 10:03:40 +00:00
|
|
|
res.add(service.serveSyncCommitteeMessage(slot,
|
|
|
|
beaconBlockRoot,
|
|
|
|
duty))
|
|
|
|
res
|
|
|
|
|
|
|
|
let statistics =
|
|
|
|
block:
|
|
|
|
var errored, succeed, failed = 0
|
|
|
|
try:
|
|
|
|
await allFutures(pendingSyncCommitteeMessages)
|
|
|
|
except CancelledError as exc:
|
2023-09-24 07:28:09 +00:00
|
|
|
let pending = pendingSyncCommitteeMessages
|
|
|
|
.filterIt(not(it.finished())).mapIt(it.cancelAndWait())
|
|
|
|
await noCancel allFutures(pending)
|
2022-05-10 10:03:40 +00:00
|
|
|
raise exc
|
|
|
|
|
|
|
|
for future in pendingSyncCommitteeMessages:
|
2023-06-01 08:04:30 +00:00
|
|
|
if future.completed():
|
2024-10-16 17:20:39 +00:00
|
|
|
if future.value:
|
2022-05-10 10:03:40 +00:00
|
|
|
inc(succeed)
|
|
|
|
else:
|
|
|
|
inc(failed)
|
|
|
|
else:
|
|
|
|
inc(errored)
|
|
|
|
(succeed, errored, failed)
|
|
|
|
|
2024-03-22 02:37:44 +00:00
|
|
|
let
|
|
|
|
delay = vc.getDelay(slot.attestation_deadline())
|
|
|
|
dur = Moment.now() - startTime
|
|
|
|
|
2022-07-13 14:43:57 +00:00
|
|
|
debug "Sync committee message statistics",
|
|
|
|
total = len(pendingSyncCommitteeMessages),
|
2022-05-10 10:03:40 +00:00
|
|
|
succeed = statistics[0], failed_to_deliver = statistics[1],
|
2024-03-22 02:37:44 +00:00
|
|
|
not_accepted = statistics[2], delay = delay, duration = dur,
|
|
|
|
slot = slot, duties_count = len(duties)
|
2022-05-10 10:03:40 +00:00
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
proc serveContributionAndProof*(
|
|
|
|
service: SyncCommitteeServiceRef,
|
|
|
|
proof: ContributionAndProof,
|
|
|
|
validator: AttachedValidator
|
|
|
|
): Future[bool] {.async: (raises: [CancelledError]).} =
|
2024-03-22 02:37:44 +00:00
|
|
|
## Signs ConributionAndProof object and sends it to BN.
|
2022-05-10 10:03:40 +00:00
|
|
|
let
|
|
|
|
vc = service.client
|
2024-03-22 02:37:44 +00:00
|
|
|
startTime = Moment.now()
|
2022-05-10 10:03:40 +00:00
|
|
|
slot = proof.contribution.slot
|
|
|
|
genesisRoot = vc.beaconGenesis.genesis_validators_root
|
|
|
|
fork = vc.forkAtEpoch(slot.epoch)
|
|
|
|
|
2024-03-22 02:37:44 +00:00
|
|
|
logScope:
|
|
|
|
validator = validatorLog(validator)
|
|
|
|
contribution = shortLog(proof.contribution)
|
|
|
|
|
2022-05-10 10:03:40 +00:00
|
|
|
let signature =
|
|
|
|
block:
|
2024-03-22 02:37:44 +00:00
|
|
|
let res =
|
|
|
|
try:
|
|
|
|
await validator.getContributionAndProofSignature(
|
|
|
|
fork, genesisRoot, proof)
|
|
|
|
except CancelledError:
|
|
|
|
debug "Sync contribution signing process was interrupted"
|
|
|
|
return false
|
|
|
|
|
2022-05-10 10:03:40 +00:00
|
|
|
if res.isErr():
|
2023-03-03 20:20:01 +00:00
|
|
|
warn "Unable to sign sync committee contribution using remote signer",
|
2024-03-22 02:37:44 +00:00
|
|
|
reason = res.error()
|
2022-05-10 10:03:40 +00:00
|
|
|
return false
|
|
|
|
res.get()
|
2024-03-22 02:37:44 +00:00
|
|
|
|
2022-05-10 10:03:40 +00:00
|
|
|
debug "Sending sync contribution",
|
|
|
|
delay = vc.getDelay(slot.sync_contribution_deadline())
|
|
|
|
|
|
|
|
let restSignedProof = RestSignedContributionAndProof.init(
|
2022-06-29 16:53:59 +00:00
|
|
|
proof, signature)
|
2022-05-10 10:03:40 +00:00
|
|
|
|
|
|
|
let res =
|
|
|
|
try:
|
2022-09-29 07:57:14 +00:00
|
|
|
await vc.publishContributionAndProofs(@[restSignedProof],
|
|
|
|
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 sync contribution",
|
|
|
|
reason = exc.getFailureReason()
|
2022-05-10 10:03:40 +00:00
|
|
|
false
|
2022-07-13 14:43:57 +00:00
|
|
|
except CancelledError:
|
2024-03-22 02:37:44 +00:00
|
|
|
debug "Publication process of sync contribution was interrupted"
|
2022-07-13 14:43:57 +00:00
|
|
|
return false
|
2022-05-10 10:03:40 +00:00
|
|
|
|
2024-03-22 02:37:44 +00:00
|
|
|
let dur = Moment.now() - startTime
|
2022-05-10 10:03:40 +00:00
|
|
|
if res:
|
2022-07-29 08:36:20 +00:00
|
|
|
beacon_sync_committee_contributions_sent.inc()
|
2024-03-22 02:37:44 +00:00
|
|
|
notice "Sync contribution published", duration = dur
|
2022-05-10 10:03:40 +00:00
|
|
|
else:
|
2024-03-22 02:37:44 +00:00
|
|
|
warn "Sync contribution was not accepted by beacon node", duration = dur
|
|
|
|
res
|
2022-05-10 10:03:40 +00:00
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
proc produceAndPublishContributions(
|
|
|
|
service: SyncCommitteeServiceRef,
|
|
|
|
slot: Slot,
|
|
|
|
beaconBlockRoot: Eth2Digest,
|
|
|
|
duties: seq[SyncCommitteeDuty]
|
|
|
|
) {.async: (raises: [CancelledError]).} =
|
2024-03-22 02:37:44 +00:00
|
|
|
let
|
|
|
|
vc = service.client
|
|
|
|
startTime = Moment.now()
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
slot = slot
|
|
|
|
block_root = shortLog(beaconBlockRoot)
|
2022-11-24 07:46:35 +00:00
|
|
|
|
2023-08-15 17:00:27 +00:00
|
|
|
var (contributions, pendingFutures, contributionsMap) =
|
|
|
|
block:
|
|
|
|
var
|
|
|
|
resItems: seq[ContributionItem]
|
|
|
|
resFutures: seq[FutureBase]
|
|
|
|
resMap: array[SYNC_COMMITTEE_SUBNET_COUNT,
|
|
|
|
Future[SyncCommitteeContribution]]
|
|
|
|
for duty in duties:
|
|
|
|
let validator = vc.getValidatorForDuties(duty.pubkey, slot).valueOr:
|
|
|
|
continue
|
|
|
|
if validator.index.isNone():
|
|
|
|
continue
|
|
|
|
for inindex in duty.validator_sync_committee_indices:
|
|
|
|
let
|
|
|
|
subCommitteeIdx = getSubcommitteeIndex(inindex)
|
|
|
|
signature =
|
|
|
|
vc.getSyncCommitteeSelectionProof(duty.pubkey,
|
|
|
|
slot, inindex).valueOr:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if is_sync_committee_aggregator(signature):
|
|
|
|
resItems.add(ContributionItem(
|
|
|
|
aggregator_index: uint64(validator.index.get()),
|
|
|
|
selection_proof: signature,
|
|
|
|
validator: validator,
|
|
|
|
subcommitteeIdx: subCommitteeIdx
|
|
|
|
))
|
|
|
|
if isNil(resMap[subCommitteeIdx]):
|
|
|
|
let future =
|
|
|
|
vc.produceSyncCommitteeContribution(
|
|
|
|
slot, subCommitteeIdx, beaconBlockRoot, ApiStrategyKind.Best)
|
|
|
|
resMap[int(subCommitteeIdx)] = future
|
|
|
|
resFutures.add(FutureBase(future))
|
|
|
|
(resItems, resFutures, resMap)
|
|
|
|
|
|
|
|
if len(contributions) > 0:
|
2022-11-24 07:46:35 +00:00
|
|
|
let
|
2023-08-15 17:00:27 +00:00
|
|
|
pendingAggregates =
|
|
|
|
block:
|
|
|
|
var res: seq[Future[bool]]
|
|
|
|
while len(pendingFutures) > 0:
|
2022-05-10 10:03:40 +00:00
|
|
|
try:
|
2023-08-15 17:00:27 +00:00
|
|
|
discard await race(pendingFutures)
|
2024-10-16 17:20:39 +00:00
|
|
|
except ValueError:
|
|
|
|
raiseAssert "Number of pendingFutures should not be zero"
|
2023-08-15 17:00:27 +00:00
|
|
|
except CancelledError as exc:
|
2023-09-24 07:28:09 +00:00
|
|
|
let pending = pendingFutures
|
|
|
|
.filterIt(not(it.finished())).mapIt(it.cancelAndWait())
|
|
|
|
await noCancel allFutures(pending)
|
2023-08-15 17:00:27 +00:00
|
|
|
raise exc
|
|
|
|
|
|
|
|
var completed: seq[int]
|
|
|
|
for contrib in contributions:
|
|
|
|
let future = contributionsMap[contrib.subcommitteeIdx]
|
|
|
|
doAssert(not(isNil(future)))
|
|
|
|
let index = pendingFutures.find(FutureBase(future))
|
|
|
|
if future.finished() and (index >= 0):
|
|
|
|
if index notin completed: completed.add(index)
|
|
|
|
let aggContribution =
|
|
|
|
try:
|
2024-10-16 17:20:39 +00:00
|
|
|
let tfut =
|
|
|
|
cast[Future[SyncCommitteeContribution].
|
|
|
|
Raising([CancelledError, ValidatorApiError])](future)
|
|
|
|
Opt.some(tfut.read())
|
|
|
|
except FuturePendingError:
|
|
|
|
raiseAssert "Future should be finished"
|
2023-08-15 17:00:27 +00:00
|
|
|
except ValidatorApiError as exc:
|
|
|
|
warn "Unable to get sync message contribution data",
|
|
|
|
reason = exc.getFailureReason()
|
|
|
|
Opt.none(SyncCommitteeContribution)
|
|
|
|
except CancelledError as exc:
|
|
|
|
debug "Request for sync message contribution was " &
|
|
|
|
"interrupted"
|
|
|
|
raise exc
|
|
|
|
|
|
|
|
if aggContribution.isSome():
|
|
|
|
let proof = ContributionAndProof(
|
|
|
|
aggregator_index: contrib.aggregator_index,
|
|
|
|
contribution: aggContribution.get(),
|
|
|
|
selection_proof: contrib.selection_proof
|
|
|
|
)
|
|
|
|
res.add(
|
|
|
|
service.serveContributionAndProof(proof, contrib.validator))
|
|
|
|
|
|
|
|
pendingFutures =
|
|
|
|
block:
|
2024-10-16 17:20:39 +00:00
|
|
|
var tres: seq[FutureBase]
|
2023-08-15 17:00:27 +00:00
|
|
|
for index, value in pendingFutures.pairs():
|
2024-10-16 17:20:39 +00:00
|
|
|
if index notin completed: tres.add(value)
|
|
|
|
tres
|
2023-08-15 17:00:27 +00:00
|
|
|
res
|
|
|
|
statistics =
|
|
|
|
block:
|
|
|
|
var errored, succeed, failed = 0
|
|
|
|
try:
|
|
|
|
await allFutures(pendingAggregates)
|
2023-09-24 07:28:09 +00:00
|
|
|
except CancelledError as exc:
|
|
|
|
let pending = pendingAggregates
|
|
|
|
.filterIt(not(it.finished())).mapIt(it.cancelAndWait())
|
|
|
|
await noCancel allFutures(pending)
|
|
|
|
raise exc
|
2023-08-15 17:00:27 +00:00
|
|
|
|
|
|
|
for future in pendingAggregates:
|
|
|
|
if future.completed():
|
2024-10-16 17:20:39 +00:00
|
|
|
if future.value:
|
2023-08-15 17:00:27 +00:00
|
|
|
inc(succeed)
|
|
|
|
else:
|
|
|
|
inc(failed)
|
2022-05-10 10:03:40 +00:00
|
|
|
else:
|
2023-08-15 17:00:27 +00:00
|
|
|
inc(errored)
|
|
|
|
(succeed, errored, failed)
|
2022-05-10 10:03:40 +00:00
|
|
|
|
2024-03-22 02:37:44 +00:00
|
|
|
let
|
|
|
|
delay = vc.getDelay(slot.aggregate_deadline())
|
|
|
|
dur = Moment.now() - startTime
|
|
|
|
|
2022-07-13 14:43:57 +00:00
|
|
|
debug "Sync message contribution statistics",
|
2023-08-15 17:00:27 +00:00
|
|
|
total = len(contributions),
|
|
|
|
succeed = statistics[0],
|
|
|
|
failed_to_create = len(pendingAggregates) - len(contributions),
|
|
|
|
failed_to_deliver = statistics[1],
|
|
|
|
not_accepted = statistics[2],
|
2024-03-22 02:37:44 +00:00
|
|
|
delay = delay, duration = dur
|
2022-05-10 10:03:40 +00:00
|
|
|
|
|
|
|
else:
|
2024-03-22 02:37:44 +00:00
|
|
|
debug "No contribution and proofs scheduled for the slot"
|
2022-05-10 10:03:40 +00:00
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
proc publishSyncMessagesAndContributions(
|
|
|
|
service: SyncCommitteeServiceRef,
|
|
|
|
slot: Slot,
|
|
|
|
duties: seq[SyncCommitteeDuty]
|
|
|
|
) {.async: (raises: [CancelledError]).} =
|
2022-11-29 10:52:21 +00:00
|
|
|
let vc = service.client
|
2022-05-10 10:03:40 +00:00
|
|
|
|
2023-06-08 08:44:32 +00:00
|
|
|
await vc.waitForBlock(slot, syncCommitteeMessageSlotOffset)
|
2022-05-10 10:03:40 +00:00
|
|
|
|
2024-03-22 02:37:44 +00:00
|
|
|
logScope:
|
|
|
|
slot = slot
|
|
|
|
|
2022-05-10 10:03:40 +00:00
|
|
|
block:
|
|
|
|
let delay = vc.getDelay(slot.sync_committee_message_deadline())
|
2024-03-22 02:37:44 +00:00
|
|
|
debug "Producing sync committee messages", delay = delay,
|
2022-05-10 10:03:40 +00:00
|
|
|
duties_count = len(duties)
|
2022-07-13 14:43:57 +00:00
|
|
|
|
2022-05-10 10:03:40 +00:00
|
|
|
let beaconBlockRoot =
|
|
|
|
block:
|
|
|
|
try:
|
2023-11-14 11:13:26 +00:00
|
|
|
let res = await vc.getHeadBlockRoot(ApiStrategyKind.Best)
|
2023-02-15 14:09:31 +00:00
|
|
|
if res.execution_optimistic.isNone():
|
|
|
|
## The `execution_optimistic` is missing from the response, we assume
|
|
|
|
## that the BN is unaware optimistic sync, so we consider the BN
|
|
|
|
## to be synchronized with the network.
|
|
|
|
## TODO (cheatfate): This should be removed when VC will be able to
|
|
|
|
## handle getSpec() API call with fork constants.
|
|
|
|
res.data.root
|
|
|
|
else:
|
|
|
|
if res.execution_optimistic.get():
|
2024-03-22 02:37:44 +00:00
|
|
|
notice "Execution client not in sync"
|
2023-02-15 14:09:31 +00:00
|
|
|
return
|
|
|
|
res.data.root
|
2022-07-13 14:43:57 +00:00
|
|
|
except ValidatorApiError as exc:
|
2023-03-03 20:20:01 +00:00
|
|
|
warn "Unable to retrieve head block's root to sign", reason = exc.msg,
|
|
|
|
reason = exc.getFailureReason()
|
2022-07-13 14:43:57 +00:00
|
|
|
return
|
|
|
|
except CancelledError:
|
|
|
|
debug "Block root request was interrupted"
|
|
|
|
return
|
2022-05-10 10:03:40 +00:00
|
|
|
|
|
|
|
try:
|
2024-03-22 02:37:44 +00:00
|
|
|
await service.produceAndPublishSyncCommitteeMessages(
|
|
|
|
slot, beaconBlockRoot, duties)
|
2023-02-23 00:11:00 +00:00
|
|
|
except ValidatorApiError as exc:
|
2024-03-22 02:37:44 +00:00
|
|
|
warn "Unable to proceed sync committee messages",
|
2023-03-03 20:20:01 +00:00
|
|
|
duties_count = len(duties), reason = exc.getFailureReason()
|
2022-05-10 10:03:40 +00:00
|
|
|
return
|
2022-07-13 14:43:57 +00:00
|
|
|
except CancelledError:
|
2024-03-22 02:37:44 +00:00
|
|
|
debug "Sync committee messages production was interrupted"
|
2022-07-13 14:43:57 +00:00
|
|
|
return
|
2022-05-10 10:03:40 +00:00
|
|
|
|
2024-03-22 02:37:44 +00:00
|
|
|
let currentTime = vc.beaconClock.now()
|
|
|
|
if slot.sync_contribution_deadline() > currentTime:
|
|
|
|
let waitDur =
|
|
|
|
nanoseconds((slot.sync_contribution_deadline() - currentTime).nanoseconds)
|
|
|
|
# Sleeping until `sync_contribution_deadline`.
|
|
|
|
debug "Waiting for sync contribution deadline", wait_time = waitDur
|
|
|
|
await sleepAsync(waitDur)
|
2022-05-10 10:03:40 +00:00
|
|
|
|
|
|
|
block:
|
|
|
|
let delay = vc.getDelay(slot.sync_contribution_deadline())
|
|
|
|
debug "Producing contribution and proofs", delay = delay
|
2024-03-22 02:37:44 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
await service.produceAndPublishContributions(slot, beaconBlockRoot, duties)
|
|
|
|
except CancelledError:
|
|
|
|
debug "Sync committee contributions production was interrupted"
|
|
|
|
return
|
2022-05-10 10:03:40 +00:00
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
proc processSyncCommitteeTasks(
|
|
|
|
service: SyncCommitteeServiceRef,
|
|
|
|
slot: Slot
|
|
|
|
) {.async: (raises: [CancelledError]).} =
|
2022-10-21 14:53:30 +00:00
|
|
|
let
|
|
|
|
vc = service.client
|
|
|
|
duties = vc.getSyncCommitteeDutiesForSlot(slot + 1)
|
2023-04-17 21:31:54 +00:00
|
|
|
timeout = vc.beaconClock.durationToNextSlot()
|
2022-05-10 10:03:40 +00:00
|
|
|
|
2024-03-22 02:37:44 +00:00
|
|
|
logScope:
|
|
|
|
slot = slot
|
|
|
|
|
2023-04-17 21:31:54 +00:00
|
|
|
try:
|
|
|
|
await service.publishSyncMessagesAndContributions(slot,
|
|
|
|
duties).wait(timeout)
|
|
|
|
except AsyncTimeoutError:
|
|
|
|
warn "Unable to publish sync committee messages and contributions in time",
|
2024-03-22 02:37:44 +00:00
|
|
|
timeout = timeout
|
2023-04-17 21:31:54 +00:00
|
|
|
except CancelledError as exc:
|
|
|
|
debug "Sync committee publish task has been interrupted"
|
|
|
|
raise exc
|
2022-05-10 10:03:40 +00:00
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
proc mainLoop(service: SyncCommitteeServiceRef) {.async: (raises: []).} =
|
2022-05-10 10:03:40 +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 "Sync committee processing 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")
|
2022-11-24 22:19:58 +00:00
|
|
|
|
2023-04-17 21:31:54 +00:00
|
|
|
var currentSlot: Opt[Slot]
|
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 =
|
|
|
|
try:
|
2023-04-17 21:31:54 +00:00
|
|
|
let
|
|
|
|
# We use zero offset here, because we do waiting in
|
2023-06-08 08:44:32 +00:00
|
|
|
# waitForBlock(syncCommitteeMessageSlotOffset).
|
2023-04-17 21:31:54 +00:00
|
|
|
slot = await vc.checkedWaitForNextSlot(currentSlot, ZeroTimeDiff,
|
|
|
|
false)
|
|
|
|
if slot.isNone():
|
|
|
|
debug "System time adjusted backwards significantly, exiting"
|
|
|
|
true
|
|
|
|
else:
|
|
|
|
currentSlot = slot
|
|
|
|
await service.processSyncCommitteeTasks(currentSlot.get())
|
|
|
|
false
|
2022-07-13 14:43:57 +00:00
|
|
|
except CancelledError:
|
|
|
|
debug "Service interrupted"
|
|
|
|
true
|
|
|
|
|
|
|
|
if breakLoop:
|
|
|
|
break
|
2022-05-10 10:03:40 +00:00
|
|
|
|
2024-10-16 17:20:39 +00:00
|
|
|
proc init*(
|
|
|
|
t: typedesc[SyncCommitteeServiceRef],
|
|
|
|
vc: ValidatorClientRef
|
|
|
|
): Future[SyncCommitteeServiceRef] {.async: (raises: []).} =
|
2022-07-14 21:11:25 +00:00
|
|
|
logScope: service = ServiceName
|
2022-11-03 19:23:33 +00:00
|
|
|
let res = SyncCommitteeServiceRef(name: ServiceName, client: vc,
|
|
|
|
state: ServiceState.Initialized)
|
2022-07-14 21:11:25 +00:00
|
|
|
debug "Initializing service"
|
2024-03-22 02:37:44 +00:00
|
|
|
res
|
2022-05-10 10:03:40 +00:00
|
|
|
|
|
|
|
proc start*(service: SyncCommitteeServiceRef) =
|
|
|
|
service.lifeFut = mainLoop(service)
|