Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanBratanov committed Jun 28, 2023
1 parent a07c481 commit e6e8917
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

public class AttestationUtilPhase0 extends AttestationUtil {

/** Attestations with a slot > current_slot + the value of this constant would be ignored */
private static final UInt64 MAX_FUTURE_SLOT_ALLOWANCE = UInt64.valueOf(3);

public AttestationUtilPhase0(
Expand Down Expand Up @@ -77,7 +76,7 @@ protected boolean isCurrentTimeBeforeMinimumAttestationBroadcastTime(

protected boolean isFromFarFuture(
final Attestation attestation, final UInt64 genesisTime, final UInt64 currentTimeMillis) {
final UInt64 attestationSlotTimeMillis =
final UInt64 attestationForkChoiceEligibleTimeMillis =
secondsToMillis(
genesisTime.plus(
attestation
Expand All @@ -87,7 +86,7 @@ protected boolean isFromFarFuture(
final UInt64 discardAttestationsAfterMillis =
currentTimeMillis.plus(
secondsToMillis(MAX_FUTURE_SLOT_ALLOWANCE.times(specConfig.getSecondsPerSlot())));
return attestationSlotTimeMillis.isGreaterThan(discardAttestationsAfterMillis);
return attestationForkChoiceEligibleTimeMillis.isGreaterThan(discardAttestationsAfterMillis);
}

private boolean isCurrentTimeAfterAttestationPropagationSlotRange(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,21 @@
* network. (We define the following for convenience -- aggregate_and_proof =
* signed_aggregate_and_proof.message and aggregate = aggregate_and_proof.aggregate)
*
* <p>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).
* <ul>
* <li>Phase 0
* <p>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).
* <li>Deneb
* <p>aggregate.data.slot is equal to or earlier than the current_slot (with a
* MAXIMUM_GOSSIP_CLOCK_DISPARITY allowance) -- i.e. aggregate.data.slot <= current_slot (a
* client MAY queue future aggregates for processing at the appropriate slot).
* <p>the epoch of aggregate.data.slot is either the current or previous epoch (with a
* MAXIMUM_GOSSIP_CLOCK_DISPARITY allowance) -- i.e.
* compute_epoch_at_slot(aggregate.data.slot) in (get_previous_epoch(state),
* get_current_epoch(state))
* </ul>
*
* <p>The aggregate attestation defined by hash_tree_root(aggregate) has not already been seen (via
* aggregate gossip, within a block, or through the creation of an equivalent aggregate locally).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,21 @@
*
* <p>The attestation's committee index (attestation.data.index) is for the correct subnet.
*
* <p>attestation.data.slot is within the last ATTESTATION_PROPAGATION_SLOT_RANGE slots (within 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).
* <ul>
* <li>Phase 0
* <p>attestation.data.slot is within the last ATTESTATION_PROPAGATION_SLOT_RANGE slots
* (within 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).
* <li>Deneb
* <p>attestation.data.slot is equal to or earlier than the current_slot (with a
* MAXIMUM_GOSSIP_CLOCK_DISPARITY allowance) -- i.e. attestation.data.slot <= current_slot (a
* client MAY queue future attestation for processing at the appropriate slot).
* <p>the epoch of attestation.data.slot is either the current or previous epoch (with a
* MAXIMUM_GOSSIP_CLOCK_DISPARITY allowance) -- i.e.
* compute_epoch_at_slot(attestation.data.slot) in (get_previous_epoch(state),
* get_current_epoch(state))
* </ul>
*
* <p>The attestation is unaggregated -- that is, it has exactly one participating validator
* (len([bit for bit in attestation.aggregation_bits if bit == 0b1]) == 1).
Expand Down Expand Up @@ -158,7 +169,7 @@ public void shouldRejectAttestationWithIncorrectAggregateBitsSize() {
}

@Test
public void shouldRejectAttestationFromBeforeAttestationPropagationSlotRange() {
public void shouldIgnoreAttestationFromBeforeAttestationPropagationSlotRange() {
final Attestation attestation =
attestationGenerator.validAttestation(storageSystem.getChainHead());

Expand All @@ -172,6 +183,39 @@ public void shouldRejectAttestationFromBeforeAttestationPropagationSlotRange() {
assertThat(validate(attestation).code()).isEqualTo(IGNORE);
}

@Test
public void postDeneb_shouldIgnoreAttestationAfterCurrentSlot() {
final Spec spec = TestSpecFactory.createMinimalDeneb();

final AttestationValidator validator =
new AttestationValidator(spec, recentChainData, signatureVerifier, new StubMetricsSystem());

// attestation will be fork choice eligible in 12 slots, so more than MAX_FUTURE_SLOT_ALLOWANCE
final Attestation attestation =
attestationGenerator.validAttestation(storageSystem.getChainHead(), UInt64.valueOf(11));

chainUpdater.setCurrentSlot(ZERO);

assertThat(validate(validator, attestation).code()).isEqualTo(IGNORE);
}

@Test
public void postDeneb_shouldIgnoreAttestationNotInCurrentOrPreviousEpoch() {
final Spec spec = TestSpecFactory.createMinimalDeneb();

final AttestationValidator validator =
new AttestationValidator(spec, recentChainData, signatureVerifier, new StubMetricsSystem());

// attestation in epoch 0
final Attestation attestation =
attestationGenerator.validAttestation(storageSystem.getChainHead());

// current epoch is 3
chainUpdater.setCurrentSlot(UInt64.valueOf(25));

assertThat(validate(validator, attestation).code()).isEqualTo(IGNORE);
}

@Test
public void shouldAcceptAttestationWithinClockDisparityOfEarliestPropagationSlot() {
final Attestation attestation =
Expand All @@ -197,6 +241,22 @@ public void shouldDeferAttestationFromAfterThePropagationSlotRange() {
assertThat(validate(attestation).code()).isEqualTo(SAVE_FOR_FUTURE);
}

@Test
public void postDeneb_shouldDeferAttestationAfterCurrentSlotButNotTooFarInTheFuture() {
final Spec spec = TestSpecFactory.createMinimalDeneb();

final AttestationValidator validator =
new AttestationValidator(spec, recentChainData, signatureVerifier, new StubMetricsSystem());

// attestation will be fork choice eligible in 3 slots, so within MAX_FUTURE_SLOT_ALLOWANCE
final Attestation attestation =
attestationGenerator.validAttestation(storageSystem.getChainHead(), UInt64.valueOf(2));

chainUpdater.setCurrentSlot(ZERO);

assertThat(validate(validator, attestation).code()).isEqualTo(SAVE_FOR_FUTURE);
}

@Test
public void shouldAcceptAttestationWithinClockDisparityOfLatestPropagationSlot() {
final Attestation attestation =
Expand Down Expand Up @@ -406,6 +466,11 @@ private Predicate<? super CompletableFuture<InternalValidationResult>> rejected(
}

private InternalValidationResult validate(final Attestation attestation) {
return validate(validator, attestation);
}

private InternalValidationResult validate(
final AttestationValidator validator, final Attestation attestation) {
final BeaconState state = safeJoin(recentChainData.getBestState().orElseThrow());

return validator
Expand Down

0 comments on commit e6e8917

Please sign in to comment.