-
-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix attestation compatibility issue in distributed validator cluster
- Loading branch information
Showing
5 changed files
with
127 additions
and
31 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,65 @@ | ||
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); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
dapplion
Contributor
|
||
|
||
// 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(async ([index, duties]) => { | ||
const attestation = signedAttestations.find((a) => a.data.index === index); | ||
if (attestation) { | ||
await this.produceAndPublishAggregates(attestation.data, duties); | ||
} else { | ||
this.logger.error("Error finding attestation to produce aggregate", {slot, index}); | ||
} | ||
}) | ||
); | ||
}; | ||
} |
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
@dapplion one difference between my implementation and the one you proposed in this commit is that here we only call
submitPoolAttestations
once for all committees and not for each committee.However, Lighthouse also calls
submitPoolAttestations
per committee and it is probably fine to do this either way.