Skip to content

Commit

Permalink
changes as per feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanBratanov committed Jul 7, 2023
1 parent 0d3d281 commit 071b2d3
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ static SpecConfigBuilder builder() {
// Config: Time parameters
int getSecondsPerSlot();

default int getMillisPerSlot() {
return getSecondsPerSlot() * 1000;
}

int getSecondsPerEth1Block();

int getMinValidatorWithdrawabilityDelay();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,19 @@ public AttestationData(UInt64 slot, AttestationData data) {
}

public UInt64 getEarliestSlotForForkChoice(final Spec spec) {
// Attestations can't be processed by fork choice until their slot is in the past and until we
// are in the same epoch as their target.
return getSlot().plus(UInt64.ONE).max(getTarget().getEpochStartSlot(spec));
final UInt64 targetEpochStartSlot = getTarget().getEpochStartSlot(spec);
return getEarliestSlotForForkChoice(targetEpochStartSlot);
}

public UInt64 getEarliestSlotForForkChoice(final MiscHelpers miscHelpers) {
return getSlot()
.plus(UInt64.ONE)
.max(miscHelpers.computeStartSlotAtEpoch(getTarget().getEpoch()));
final UInt64 targetEpochStartSlot = miscHelpers.computeStartSlotAtEpoch(getTarget().getEpoch());
return getEarliestSlotForForkChoice(targetEpochStartSlot);
}

public UInt64 getEarliestSlotForForkChoice(final UInt64 targetEpochStartSlot) {
// Attestations can't be processed by fork choice until their slot is in the past and until we
// are in the same epoch as their target.
return getSlot().plus(1).max(targetEpochStartSlot);
}

public UInt64 getSlot() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,19 +122,18 @@ public UInt64 computeStartSlotAtEpoch(final UInt64 epoch) {
return epoch.times(specConfig.getSlotsPerEpoch());
}

public UInt64 computeStartTimeAtEpoch(final UInt64 genesisTime, final UInt64 epoch) {
final UInt64 epochStartSlot = computeStartSlotAtEpoch(epoch);
return computeStartTimeAtSlot(genesisTime, epochStartSlot);
}

public UInt64 computeStartTimeAtSlot(final UInt64 genesisTime, final UInt64 slot) {
return genesisTime.plus(slot.times(specConfig.getSecondsPerSlot()));
public UInt64 computeEndSlotAtEpoch(final UInt64 epoch) {
return computeStartSlotAtEpoch(epoch.plus(1)).minusMinZero(1);
}

public UInt64 computeSlotAtTime(final UInt64 genesisTime, final UInt64 currentTime) {
return currentTime.minusMinZero(genesisTime).dividedBy(specConfig.getSecondsPerSlot());
}

public UInt64 computeTimeAtSlot(final UInt64 genesisTime, final UInt64 slot) {
return genesisTime.plus(slot.times(specConfig.getSecondsPerSlot()));
}

public UInt64 computeTimeAtSlot(final BeaconState state, final UInt64 slot) {
UInt64 slotsSinceGenesis = slot.minus(SpecConfig.GENESIS_SLOT);
return state.getGenesisTime().plus(slotsSinceGenesis.times(specConfig.getSecondsPerSlot()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ public Optional<SlotInclusionGossipValidationResult> performSlotInclusionGossipV
final Attestation attestation, final UInt64 genesisTime, final UInt64 currentTimeMillis) {
final UInt64 attestationSlot = attestation.getData().getSlot();
final UInt64 attestationSlotTimeMillis =
secondsToMillis(miscHelpers.computeStartTimeAtSlot(genesisTime, attestationSlot));
if (isAttestationSlotAfterCurrentTime(attestationSlotTimeMillis, currentTimeMillis)
&& isFromFarFuture(attestation, genesisTime, currentTimeMillis)) {
secondsToMillis(miscHelpers.computeTimeAtSlot(genesisTime, attestationSlot));
if (isFromFarFuture(attestation, genesisTime, currentTimeMillis)
&& isAttestationSlotAfterCurrentTime(attestationSlotTimeMillis, currentTimeMillis)) {
return Optional.of(SlotInclusionGossipValidationResult.IGNORE);
}
if (isCurrentTimeBeforeMinimumAttestationBroadcastTime(
Expand All @@ -77,23 +77,20 @@ private boolean isAttestationSlotInCurrentOrPreviousEpoch(
miscHelpers.computeSlotAtTime(genesisTime, millisToSeconds(currentTimeMillis));
final UInt64 currentEpoch = miscHelpers.computeEpochAtSlot(currentSlot);
final UInt64 previousEpoch = currentEpoch.minusMinZero(1);
final UInt64 previousEpochStartTimeMillisWithDisparity =
secondsToMillis(miscHelpers.computeStartTimeAtEpoch(genesisTime, previousEpoch))
.minusMinZero(specConfig.getMaximumGossipClockDisparity());
// current epoch end time is the start of the new epoch
final UInt64 currentEpochEndTimeMillisWithDisparity =
secondsToMillis(miscHelpers.computeStartTimeAtEpoch(genesisTime, currentEpoch.increment()))
.plus(specConfig.getMaximumGossipClockDisparity());
final UInt64 slotDisparity = calculateMaximumGossipClockDisparityInSlots();
// min and max slot for the given attestation slot based on previous and current epoch with
// MAXIMUM_GOSSIP_CLOCK_DISPARITY
final UInt64 minSlot =
miscHelpers.computeSlotAtTime(
genesisTime, millisToSeconds(previousEpochStartTimeMillisWithDisparity));
final UInt64 maxSlot =
miscHelpers.computeSlotAtTime(
genesisTime, millisToSeconds(currentEpochEndTimeMillisWithDisparity));
miscHelpers.computeStartSlotAtEpoch(previousEpoch).minusMinZero(slotDisparity);
final UInt64 maxSlot = miscHelpers.computeEndSlotAtEpoch(currentEpoch).plus(slotDisparity);

return attestationSlot.isGreaterThanOrEqualTo(minSlot)
&& attestationSlot.isLessThanOrEqualTo(maxSlot);
}

private UInt64 calculateMaximumGossipClockDisparityInSlots() {
return UInt64.valueOf(
specConfig.getMaximumGossipClockDisparity() / specConfig.getMillisPerSlot())
.plus(1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ public AttestationWorthinessChecker createAttestationWorthinessChecker(final Bea
}

/**
* [IGNORE] aggregate.data.slot is within the last ATTESTATION_PROPAGATION_SLOT_RANGE slots (with
* a MAXIMUM_GOSSIP_CLOCK_DISPARITY allowance) -- i.e. aggregate.data.slot +
* ATTESTATION_PROPAGATION_SLOT_RANGE >= current_slot >= aggregate.data.slot (a client MAY queue
* future aggregates for processing at the appropriate slot).
* [IGNORE] attestation.data.slot is within the last ATTESTATION_PROPAGATION_SLOT_RANGE slots
* (with a MAXIMUM_GOSSIP_CLOCK_DISPARITY allowance) -- i.e. attestation.data.slot +
* ATTESTATION_PROPAGATION_SLOT_RANGE >= current_slot >= attestation.data.slot (a client MAY queue
* future attestations for processing at the appropriate slot).
*/
@Override
public Optional<SlotInclusionGossipValidationResult> performSlotInclusionGossipValidation(
Expand Down

0 comments on commit 071b2d3

Please sign in to comment.