Skip to content

Commit

Permalink
add test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanBratanov committed Jul 10, 2023
1 parent 79d2ba7 commit 47ebdd1
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,6 @@ public UInt64 computeStartSlotAtEpoch(final UInt64 epoch) {
return epoch.times(specConfig.getSlotsPerEpoch());
}

public UInt64 computeEndSlotAtEpoch(final UInt64 epoch) {
return computeStartSlotAtEpoch(epoch).plus(specConfig.getSlotsPerEpoch() - 1);
}

public UInt64 computeSlotAtTime(final UInt64 genesisTime, final UInt64 currentTime) {
return currentTime.minusMinZero(genesisTime).dividedBy(specConfig.getSecondsPerSlot());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import static tech.pegasys.teku.infrastructure.time.TimeUtilities.millisToSeconds;
import static tech.pegasys.teku.infrastructure.time.TimeUtilities.secondsToMillis;

import com.google.common.annotations.VisibleForTesting;
import java.util.Optional;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.config.SpecConfig;
Expand Down Expand Up @@ -69,7 +70,8 @@ private boolean isAttestationSlotAfterCurrentTime(
currentTimeMillis.plus(specConfig.getMaximumGossipClockDisparity()));
}

private boolean isAttestationSlotInCurrentOrPreviousEpoch(
@VisibleForTesting
boolean isAttestationSlotInCurrentOrPreviousEpoch(
final UInt64 attestationSlot, final UInt64 genesisTime, final UInt64 currentTimeMillis) {
final UInt64 currentSlot =
miscHelpers.computeSlotAtTime(genesisTime, millisToSeconds(currentTimeMillis));
Expand All @@ -80,7 +82,8 @@ private boolean isAttestationSlotInCurrentOrPreviousEpoch(
// MAXIMUM_GOSSIP_CLOCK_DISPARITY
final UInt64 minSlot =
miscHelpers.computeStartSlotAtEpoch(previousEpoch).minusMinZero(slotDisparity);
final UInt64 maxSlot = miscHelpers.computeEndSlotAtEpoch(currentEpoch).plus(slotDisparity);
final UInt64 maxSlot =
miscHelpers.computeStartSlotAtEpoch(currentEpoch.plus(1)).plus(slotDisparity);

return attestationSlot.isGreaterThanOrEqualTo(minSlot)
&& attestationSlot.isLessThanOrEqualTo(maxSlot);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static tech.pegasys.teku.infrastructure.unsigned.UInt64.ONE;
import static tech.pegasys.teku.infrastructure.unsigned.UInt64.ZERO;

import com.google.common.annotations.VisibleForTesting;
import java.util.Optional;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.config.SpecConfig;
Expand Down Expand Up @@ -82,16 +83,18 @@ protected boolean isFromFarFuture(
return attestationForkChoiceEligibleTimeMillis.isGreaterThan(discardAttestationsAfterMillis);
}

private boolean isCurrentTimeBeforeMinimumAttestationBroadcastTime(
@VisibleForTesting
boolean isCurrentTimeAfterAttestationPropagationSlotRange(
final UInt64 attestationSlot, final UInt64 genesisTime, final UInt64 currentTimeMillis) {
final UInt64 minimumBroadcastTimeMillis =
minimumBroadcastTimeMillis(attestationSlot, genesisTime);
return currentTimeMillis.isLessThan(minimumBroadcastTimeMillis);
return maximumBroadcastTimeMillis(attestationSlot, genesisTime).isLessThan(currentTimeMillis);
}

private boolean isCurrentTimeAfterAttestationPropagationSlotRange(
@VisibleForTesting
boolean isCurrentTimeBeforeMinimumAttestationBroadcastTime(
final UInt64 attestationSlot, final UInt64 genesisTime, final UInt64 currentTimeMillis) {
return maximumBroadcastTimeMillis(attestationSlot, genesisTime).isLessThan(currentTimeMillis);
final UInt64 minimumBroadcastTimeMillis =
minimumBroadcastTimeMillis(attestationSlot, genesisTime);
return currentTimeMillis.isLessThan(minimumBroadcastTimeMillis);
}

private UInt64 maximumBroadcastTimeMillis(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,39 @@ public void isSlotAtNthEpochBoundary_invalidNParameter_negative() {
.hasMessageContaining("Parameter n must be greater than 0");
}

@ParameterizedTest
@MethodSource("getComputesSlotAtTimeArguments")
public void computesSlotAtTime(final long currentTime, final UInt64 expectedSlot) {
final UInt64 actualSlot =
miscHelpers.computeSlotAtTime(UInt64.ZERO, UInt64.valueOf(currentTime));
assertThat(actualSlot).isEqualTo(expectedSlot);
}

@ParameterizedTest
@MethodSource("getComputesTimeAtSlotArguments")
public void computesTimeAtSlot(final UInt64 slot, final long expectedTime) {
final UInt64 actualTime = miscHelpers.computeTimeAtSlot(UInt64.ZERO, slot);
assertThat(actualTime).isEqualTo(UInt64.valueOf(expectedTime));
}

public static Stream<Arguments> getComputesSlotAtTimeArguments() {
// 6 seconds per slot
return Stream.of(
Arguments.of(6 * 10, UInt64.valueOf(10)),
Arguments.of(6 * 4, UInt64.valueOf(4)),
Arguments.of(0, UInt64.ZERO),
Arguments.of(60253, UInt64.valueOf(10042)));
}

public static Stream<Arguments> getComputesTimeAtSlotArguments() {
// 6 seconds per slot
return Stream.of(
Arguments.of(UInt64.valueOf(10), 6 * 10),
Arguments.of(UInt64.valueOf(4), 6 * 4),
Arguments.of(UInt64.ZERO, 0),
Arguments.of(UInt64.valueOf(10042), 60252));
}

public static Stream<Arguments> getNValues() {
return Stream.of(
Arguments.of(1), Arguments.of(2), Arguments.of(3), Arguments.of(4), Arguments.of(5));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright ConsenSys Software Inc., 2022
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

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

import static org.assertj.core.api.Assertions.assertThat;
import static tech.pegasys.teku.infrastructure.time.TimeUtilities.secondsToMillis;

import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.SpecMilestone;
import tech.pegasys.teku.spec.SpecVersion;
import tech.pegasys.teku.spec.TestSpecFactory;

public class AttestationUtilDenebTest {

private static final SpecVersion SPEC_VERSION =
TestSpecFactory.createMinimalDeneb().forMilestone(SpecMilestone.DENEB);

private static final int SECONDS_PER_SLOT = SPEC_VERSION.getConfig().getSecondsPerSlot();

private final AttestationUtilDeneb attestationUtilDeneb =
(AttestationUtilDeneb) SPEC_VERSION.getAttestationUtil();

@ParameterizedTest
@MethodSource("provideAttestationSlotInCurrentOrPreviousEpochArguments")
public void testAttestationSlotInCurrentOrPreviousEpoch(
final int attestationSlot, final UInt64 currentTime, final boolean expectedResult) {
final UInt64 currentTimeMillis = secondsToMillis(currentTime);
// set genesisTime as 0 for simplification
final UInt64 genesisTime = UInt64.ZERO;
final boolean actualResult =
attestationUtilDeneb.isAttestationSlotInCurrentOrPreviousEpoch(
UInt64.valueOf(attestationSlot), genesisTime, currentTimeMillis);
assertThat(actualResult).isEqualTo(expectedResult);
}

// minimal is 6 seconds per slot, 8 slots per epoch
private static Stream<Arguments> provideAttestationSlotInCurrentOrPreviousEpochArguments() {
return Stream.of(
Arguments.of(6, getTimeForSlot(10), true),
Arguments.of(6, getTimeForSlot(3), true),
Arguments.of(6, getTimeForSlot(26), false),
Arguments.of(10, getTimeForSlot(6), false),
// testing MAXIMUM_GOSSIP_CLOCK_DISPARITY
// current time is in epoch 3 and attestation slot is in the last slot of epoch 1
Arguments.of(7, getTimeForSlot(22), true),
// current time is in epoch 1 and attestation slot is in the first slot of epoch 2
Arguments.of(17, getTimeForSlot(10), true));
}

private static UInt64 getTimeForSlot(final int slot) {
return UInt64.valueOf(slot).times(SECONDS_PER_SLOT);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright ConsenSys Software Inc., 2022
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

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

import static org.assertj.core.api.Assertions.assertThat;
import static tech.pegasys.teku.infrastructure.time.TimeUtilities.secondsToMillis;

import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.SpecMilestone;
import tech.pegasys.teku.spec.SpecVersion;
import tech.pegasys.teku.spec.TestSpecFactory;

class AttestationUtilPhase0Test {

private static final SpecVersion SPEC_VERSION =
TestSpecFactory.createMinimalPhase0().forMilestone(SpecMilestone.PHASE0);

private static final int SECONDS_PER_SLOT = SPEC_VERSION.getConfig().getSecondsPerSlot();

private final AttestationUtilPhase0 attestationUtilPhase0 =
(AttestationUtilPhase0) SPEC_VERSION.getAttestationUtil();

@ParameterizedTest
@MethodSource("provideCurrentTimeAfterAttestationPropagationSlotRangeArguments")
public void testIsCurrentTimeAfterAttestationPropagationSlotRange(
final int attestationSlot, final UInt64 currentTimeMillis, final boolean expectedResult) {
// set genesisTime as 0 for simplification
final UInt64 genesisTime = UInt64.ZERO;
final boolean actualResult =
attestationUtilPhase0.isCurrentTimeAfterAttestationPropagationSlotRange(
UInt64.valueOf(attestationSlot), genesisTime, currentTimeMillis);
assertThat(actualResult).isEqualTo(expectedResult);
}

@ParameterizedTest
@MethodSource("provideCurrentTimeBeforeMinimumAttestationBroadcastTimeArguments")
public void testIsCurrentTimeBeforeMinimumAttestationBroadcastTime(
final int attestationSlot, final UInt64 currentTimeMillis, final boolean expectedResult) {
// set genesisTime as 0 for simplification
final UInt64 genesisTime = UInt64.ZERO;
final boolean actualResult =
attestationUtilPhase0.isCurrentTimeBeforeMinimumAttestationBroadcastTime(
UInt64.valueOf(attestationSlot), genesisTime, currentTimeMillis);
assertThat(actualResult).isEqualTo(expectedResult);
}

// ATTESTATION_PROPAGATION_SLOT_RANGE is 32
private static Stream<Arguments>
provideCurrentTimeAfterAttestationPropagationSlotRangeArguments() {
return Stream.of(
Arguments.of(0, getTimeForSlotInMillis(34), true),
Arguments.of(0, getTimeForSlotInMillis(32), false),
Arguments.of(12, getTimeForSlotInMillis(26), false),
Arguments.of(12, getTimeForSlotInMillis(46), true),
// testing MAXIMUM_GOSSIP_CLOCK_DISPARITY
Arguments.of(12, getTimeForSlotInMillis(45).plus(500), false),
Arguments.of(12, getTimeForSlotInMillis(45).plus(501), true));
}

private static Stream<Arguments>
provideCurrentTimeBeforeMinimumAttestationBroadcastTimeArguments() {
return Stream.of(
Arguments.of(0, UInt64.ZERO, false),
Arguments.of(8, getTimeForSlotInMillis(7), true),
Arguments.of(7, getTimeForSlotInMillis(8), false),
// testing MAXIMUM_GOSSIP_CLOCK_DISPARITY
Arguments.of(7, getTimeForSlotInMillis(7).minus(501), true),
Arguments.of(7, getTimeForSlotInMillis(7).minus(500), false));
}

private static UInt64 getTimeForSlotInMillis(final int slot) {
return secondsToMillis(UInt64.valueOf(slot).times(SECONDS_PER_SLOT));
}
}

0 comments on commit 47ebdd1

Please sign in to comment.