Skip to content

Commit

Permalink
add electra tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdi-aouadi committed Nov 26, 2024
1 parent 2eba37a commit 9dc69bf
Showing 1 changed file with 50 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,20 @@
import static tech.pegasys.teku.validator.coordinator.performance.DefaultPerformanceTracker.ATTESTATION_INCLUSION_RANGE;

import java.util.List;
import org.assertj.core.api.Assumptions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestTemplate;
import tech.pegasys.infrastructure.logging.LogCaptor;
import tech.pegasys.teku.bls.BLSKeyGenerator;
import tech.pegasys.teku.bls.BLSKeyPair;
import tech.pegasys.teku.bls.BLSTestUtil;
import tech.pegasys.teku.infrastructure.async.SafeFuture;
import tech.pegasys.teku.infrastructure.logging.StatusLogger;
import tech.pegasys.teku.infrastructure.metrics.SettableGauge;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.TestSpecFactory;
import tech.pegasys.teku.spec.SpecMilestone;
import tech.pegasys.teku.spec.TestSpecContext;
import tech.pegasys.teku.spec.TestSpecInvocationContextProvider;
import tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState;
import tech.pegasys.teku.spec.datastructures.operations.Attestation;
import tech.pegasys.teku.spec.generator.AttestationGenerator;
Expand All @@ -47,36 +49,47 @@
import tech.pegasys.teku.validator.api.ValidatorPerformanceTrackingMode;
import tech.pegasys.teku.validator.coordinator.ActiveValidatorTracker;

@TestSpecContext(milestone = {SpecMilestone.PHASE0, SpecMilestone.ELECTRA})
public class DefaultPerformanceTrackerTest {

private static final List<BLSKeyPair> VALIDATOR_KEYS = BLSKeyGenerator.generateKeyPairs(64);
private final Spec spec = TestSpecFactory.createMinimalPhase0();
protected StorageSystem storageSystem = InMemoryStorageSystemBuilder.buildDefault(spec);
protected ChainBuilder chainBuilder = ChainBuilder.create(spec, VALIDATOR_KEYS);
protected ChainUpdater chainUpdater =
new ChainUpdater(storageSystem.recentChainData(), chainBuilder);

private final DataStructureUtil dataStructureUtil = new DataStructureUtil(spec);
private final StatusLogger log = mock(StatusLogger.class);
private final ActiveValidatorTracker validatorTracker = mock(ActiveValidatorTracker.class);
private final SyncCommitteePerformanceTracker syncCommitteePerformanceTracker =
mock(SyncCommitteePerformanceTracker.class);
private final ValidatorPerformanceMetrics validatorPerformanceMetrics =
mock(ValidatorPerformanceMetrics.class);

private final DefaultPerformanceTracker performanceTracker =
new DefaultPerformanceTracker(
storageSystem.combinedChainDataClient(),
log,
validatorPerformanceMetrics,
ValidatorPerformanceTrackingMode.ALL,
validatorTracker,
syncCommitteePerformanceTracker,
spec,
mock(SettableGauge.class));
private SpecMilestone specMilestone;
private Spec spec;
private StorageSystem storageSystem;
private ChainBuilder chainBuilder;
private ChainUpdater chainUpdater;
private DataStructureUtil dataStructureUtil;
private DefaultPerformanceTracker performanceTracker;

@BeforeEach
void beforeEach() {
void beforeEach(final TestSpecInvocationContextProvider.SpecContext specContext) {
specMilestone = specContext.getSpecMilestone();
spec = specContext.getSpec();
dataStructureUtil = specContext.getDataStructureUtil();

storageSystem = InMemoryStorageSystemBuilder.buildDefault(spec);
chainBuilder = ChainBuilder.create(spec, VALIDATOR_KEYS);

chainUpdater = new ChainUpdater(storageSystem.recentChainData(), chainBuilder);

performanceTracker =
new DefaultPerformanceTracker(
storageSystem.combinedChainDataClient(),
log,
validatorPerformanceMetrics,
ValidatorPerformanceTrackingMode.ALL,
validatorTracker,
syncCommitteePerformanceTracker,
spec,
mock(SettableGauge.class));

when(validatorTracker.getNumberOfValidatorsForEpoch(any())).thenReturn(0);
when(syncCommitteePerformanceTracker.calculatePerformance(any()))
.thenReturn(
Expand All @@ -85,7 +98,7 @@ void beforeEach() {
performanceTracker.start(UInt64.ZERO);
}

@Test
@TestTemplate
void shouldDisplayPerfectBlockInclusion() {
chainUpdater.updateBestBlock(chainUpdater.advanceChainUntil(10));
performanceTracker.reportBlockProductionAttempt(spec.computeEpochAtSlot(UInt64.valueOf(1)));
Expand All @@ -97,7 +110,7 @@ void shouldDisplayPerfectBlockInclusion() {
verify(log).performance(expectedBlockPerformance.toString());
}

@Test
@TestTemplate
void shouldDisplayBlockInclusionWhenProducedBlockIsChainHead() {
final UInt64 lastSlot = spec.computeStartSlotAtEpoch(UInt64.ONE);
final SignedBlockAndState bestBlock = chainUpdater.advanceChainUntil(2);
Expand All @@ -109,7 +122,7 @@ void shouldDisplayBlockInclusionWhenProducedBlockIsChainHead() {
verify(log).performance(expectedBlockPerformance.toString());
}

@Test
@TestTemplate
void shouldDisplayOneMissedBlock() {
chainUpdater.updateBestBlock(chainUpdater.advanceChainUntil(10));
performanceTracker.reportBlockProductionAttempt(spec.computeEpochAtSlot(UInt64.valueOf(1)));
Expand All @@ -123,7 +136,7 @@ void shouldDisplayOneMissedBlock() {
verify(log).performance(expectedBlockPerformance.toString());
}

@Test
@TestTemplate
void shouldDisplayPerfectAttestationInclusion() {
chainUpdater.updateBestBlock(chainUpdater.advanceChainUntil(1));

Expand All @@ -146,7 +159,7 @@ void shouldDisplayPerfectAttestationInclusion() {
verify(log).performance(expectedAttestationPerformance.toString());
}

@Test
@TestTemplate
void shouldDisplayInclusionDistanceOfMax2Min1() {
chainUpdater.updateBestBlock(chainUpdater.advanceChainUntil(1));

Expand Down Expand Up @@ -175,7 +188,7 @@ void shouldDisplayInclusionDistanceOfMax2Min1() {
verify(log).performance(expectedAttestationPerformance.toString());
}

@Test
@TestTemplate
void shouldDisplayIncorrectTargetRoot() {
chainUpdater.updateBestBlock(chainUpdater.advanceChainUntil(1));

Expand Down Expand Up @@ -214,8 +227,9 @@ void shouldDisplayIncorrectTargetRoot() {
verify(log).performance(expectedAttestationPerformance.toString());
}

@Test
@TestTemplate
void shouldDisplayIncorrectHeadBlockRoot() {
Assumptions.assumeThat(specMilestone).isEqualTo(SpecMilestone.ELECTRA);
chainUpdater.updateBestBlock(chainUpdater.advanceChainUntil(1));

ChainBuilder chainBuilderFork = chainBuilder.fork();
Expand Down Expand Up @@ -253,27 +267,21 @@ void shouldDisplayIncorrectHeadBlockRoot() {
verify(log).performance(expectedAttestationPerformance.toString());
}

@Test
@TestTemplate
void shouldClearOldSentObjects() {
chainUpdater.updateBestBlock(chainUpdater.advanceChainUntil(10));
performanceTracker.reportBlockProductionAttempt(spec.computeEpochAtSlot(UInt64.valueOf(1)));
performanceTracker.reportBlockProductionAttempt(spec.computeEpochAtSlot(UInt64.valueOf(2)));
performanceTracker.saveProducedBlock(chainUpdater.chainBuilder.getBlockAtSlot(1));
performanceTracker.saveProducedBlock(chainUpdater.chainBuilder.getBlockAtSlot(2));
performanceTracker.saveProducedAttestation(
spec.getGenesisSchemaDefinitions()
.getAttestationSchema()
.create(
dataStructureUtil.randomBitlist(),
dataStructureUtil.randomAttestationData(UInt64.ONE),
BLSTestUtil.randomSignature(0)));
performanceTracker.saveProducedAttestation(dataStructureUtil.randomAttestation(UInt64.ONE));
performanceTracker.onSlot(spec.computeStartSlotAtEpoch(UInt64.valueOf(2)));
assertThat(performanceTracker.producedAttestationsByEpoch).isEmpty();
assertThat(performanceTracker.producedBlocksByEpoch).isEmpty();
assertThat(performanceTracker.blockProductionAttemptsByEpoch).isEmpty();
}

@Test
@TestTemplate
void shouldNotCountDuplicateAttestationsIncludedOnChain() {
chainUpdater.updateBestBlock(chainUpdater.advanceChainUntil(1));

Expand Down Expand Up @@ -301,7 +309,7 @@ void shouldNotCountDuplicateAttestationsIncludedOnChain() {
verify(log).performance(expectedAttestationPerformance.toString());
}

@Test
@TestTemplate
void shouldNotSkipValidationForAttestationsWithSameDataButDifferentBitlists() {
chainUpdater.updateBestBlock(chainUpdater.advanceChainUntil(1));

Expand Down Expand Up @@ -335,7 +343,7 @@ void shouldNotSkipValidationForAttestationsWithSameDataButDifferentBitlists() {
verify(log).performance(expectedAttestationPerformance.toString());
}

@Test
@TestTemplate
void shouldReportExpectedAttestationOnlyForTheGivenEpoch() {
when(validatorTracker.getNumberOfValidatorsForEpoch(UInt64.valueOf(2))).thenReturn(2);
when(validatorTracker.getNumberOfValidatorsForEpoch(UInt64.valueOf(3))).thenReturn(1);
Expand All @@ -347,14 +355,14 @@ void shouldReportExpectedAttestationOnlyForTheGivenEpoch() {
verify(log).performance(expectedAttestationPerformance.toString());
}

@Test
@TestTemplate
void shouldNotReportAttestationPerformanceIfNoValidatorsInEpoch() {
when(validatorTracker.getNumberOfValidatorsForEpoch(UInt64.valueOf(2))).thenReturn(0);
performanceTracker.onSlot(spec.computeStartSlotAtEpoch(ATTESTATION_INCLUSION_RANGE.plus(2)));
verify(log, never()).performance(anyString());
}

@Test
@TestTemplate
void shouldReportSyncCommitteePerformance() {
final UInt64 epoch = UInt64.valueOf(2);
final SyncCommitteePerformance performance = new SyncCommitteePerformance(epoch, 10, 9, 8, 7);
Expand All @@ -366,7 +374,7 @@ void shouldReportSyncCommitteePerformance() {
verify(validatorPerformanceMetrics).updateSyncCommitteePerformance(performance);
}

@Test
@TestTemplate
void shouldHandleErrorsWhenReportTasksFail() {
chainUpdater.updateBestBlock(chainUpdater.advanceChainUntil(1));
final Attestation attestation = createAttestationForParentBlockOnSlot(1);
Expand Down

0 comments on commit 9dc69bf

Please sign in to comment.