Skip to content

Commit

Permalink
better calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanBratanov committed Jun 29, 2023
1 parent 23f2ffd commit ec62ffa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,13 @@ public UInt64 computeStartSlotAtEpoch(final UInt64 epoch) {
return epoch.times(specConfig.getSlotsPerEpoch());
}

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

public UInt64 computeEpochAtTime(final UInt64 genesisTime, final UInt64 time) {
final UInt64 slot = computeSlotAtTime(genesisTime, time);
return computeEpochAtSlot(slot);
public UInt64 computeStartTimeAtEpoch(final UInt64 genesisTime, final UInt64 epoch) {
final UInt64 epochStartSlot = computeStartSlotAtEpoch(epoch);
return computeTimeAtSlot(genesisTime, epochStartSlot);
}

public UInt64 computeTimeAtSlot(final BeaconState state, final UInt64 slot) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

package tech.pegasys.teku.spec.logic.versions.deneb.util;

import static tech.pegasys.teku.infrastructure.time.TimeUtilities.millisToSeconds;
import static tech.pegasys.teku.infrastructure.time.TimeUtilities.secondsToMillis;

import java.util.Optional;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
Expand Down Expand Up @@ -67,22 +67,24 @@ && isFromFarFuture(attestation, genesisTime, currentTimeMillis)) {

private boolean isAttestationSlotAfterCurrentTime(
final UInt64 attestationSlot, final UInt64 genesisTime, final UInt64 currentTimeMillis) {
final UInt64 currentTimeWithDisparity = calculateCurrentTimeWithDisparity(currentTimeMillis);
final UInt64 currentSlot = miscHelpers.computeSlotAtTime(genesisTime, currentTimeWithDisparity);
return attestationSlot.isGreaterThan(currentSlot);
final UInt64 attestationSlotTimeMillis =
secondsToMillis(miscHelpers.computeTimeAtSlot(genesisTime, attestationSlot));
return attestationSlotTimeMillis.isGreaterThan(
currentTimeMillis.plus(specConfig.getMaximumGossipClockDisparity()));
}

private boolean isAttestationSlotCurrentOrPreviousEpoch(
final UInt64 attestationSlot, final UInt64 genesisTime, final UInt64 currentTimeMillis) {
final UInt64 currentTimeWithDisparity = calculateCurrentTimeWithDisparity(currentTimeMillis);
final UInt64 currentEpoch =
miscHelpers.computeEpochAtTime(genesisTime, currentTimeWithDisparity);
final UInt64 attestationEpoch = miscHelpers.computeEpochAtSlot(attestationSlot);
return attestationEpoch.equals(currentEpoch)
|| attestationEpoch.equals(currentEpoch.minusMinZero(1));
}

private UInt64 calculateCurrentTimeWithDisparity(final UInt64 currentTimeMillis) {
return millisToSeconds(currentTimeMillis.plus(specConfig.getMaximumGossipClockDisparity()));
final UInt64 nextEpochStartTimeMillis =
secondsToMillis(miscHelpers.computeStartTimeAtEpoch(genesisTime, attestationEpoch.plus(1)));
final UInt64 previousEpochStartTimeMillis =
secondsToMillis(
miscHelpers.computeStartTimeAtEpoch(genesisTime, attestationEpoch.minusMinZero(1)));
return currentTimeMillis
.minusMinZero(specConfig.getMaximumGossipClockDisparity())
.isGreaterThanOrEqualTo(previousEpochStartTimeMillis)
&& currentTimeMillis.isLessThan(
nextEpochStartTimeMillis.plus(specConfig.getMaximumGossipClockDisparity()));
}
}

0 comments on commit ec62ffa

Please sign in to comment.