-
-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
126 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import {toHexString} from "@chainsafe/ssz"; | ||
import {computeEpochAtSlot} from "@lodestar/state-transition"; | ||
import {phase0, Slot} from "@lodestar/types"; | ||
import {sleep} from "@lodestar/utils"; | ||
import {AttestationService, AttestationServiceArgs} from "./attestation.js"; | ||
import {groupAttDutiesByCommitteeIndex} from "./utils.js"; | ||
|
||
export class DistributedAttestationService extends AttestationService { | ||
constructor(...args: AttestationServiceArgs) { | ||
super(...args); | ||
} | ||
|
||
protected runAttestationTasks = async (slot: Slot, signal: AbortSignal): Promise<void> => { | ||
// Fetch info first so a potential delay is absorbed by the sleep() below | ||
const duties = this.dutiesService.getDutiesAtSlot(slot); | ||
if (duties.length === 0) { | ||
return; | ||
} | ||
|
||
// A validator should create and broadcast the attestation to the associated attestation subnet when either | ||
// (a) the validator has received a valid block from the expected block proposer for the assigned slot or | ||
// (b) one-third of the slot has transpired (SECONDS_PER_SLOT / 3 seconds after the start of slot) -- whichever comes first. | ||
await Promise.race([sleep(this.clock.msToSlot(slot + 1 / 3), signal), this.emitter.waitForBlockSlot(slot)]); | ||
this.metrics?.attesterStepCallProduceAttestation.observe(this.clock.secFromSlot(slot + 1 / 3)); | ||
|
||
const dutiesByCommitteeIndex = groupAttDutiesByCommitteeIndex(duties); | ||
|
||
// Step 1. Sign `Attestation` for each validator | ||
const signedAttestations: phase0.Attestation[] = []; | ||
|
||
await Promise.all( | ||
Array.from(dutiesByCommitteeIndex.entries()).map(async ([index, duties]) => { | ||
const attestationData = await this.produceAttestation(slot, index); | ||
const headRootHex = toHexString(attestationData.beaconBlockRoot); | ||
const currentEpoch = computeEpochAtSlot(slot); | ||
|
||
duties.map(async ({duty}) => { | ||
const signedAttestation = await this.signAttestation(duty, attestationData, currentEpoch, slot, headRootHex); | ||
if (signedAttestation) signedAttestations.push(signedAttestation); | ||
}); | ||
}) | ||
); | ||
|
||
// Step 2. Publish all `Attestations` in one go | ||
await this.publishAttestations(slot, signedAttestations); | ||
|
||
// Step 3. after all attestations are submitted, make an aggregate. | ||
// First, wait until the `aggregation_production_instant` (2/3rds of the way though the slot) | ||
await sleep(this.clock.msToSlot(slot + 2 / 3), signal); | ||
this.metrics?.attesterStepCallProduceAggregate.observe(this.clock.secFromSlot(slot + 2 / 3)); | ||
|
||
// Then download, sign and publish a `SignedAggregateAndProof` for each | ||
// validator that is elected to aggregate for this `slot` and | ||
// `committeeIndex`. | ||
await Promise.all( | ||
Array.from(dutiesByCommitteeIndex.entries()).map(([index, duties]) => { | ||
// const attestation = signedAttestations.find((a) => a.data.index === index); | ||
const attestationData: phase0.AttestationData = {...signedAttestations[0].data, index}; | ||
return this.produceAndPublishAggregates(attestationData, duties); | ||
}) | ||
); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters