Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdi-aouadi committed Aug 2, 2024
1 parent fdc8a57 commit b5a758b
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@
import tech.pegasys.teku.spec.datastructures.builder.SignedValidatorRegistration;
import tech.pegasys.teku.spec.datastructures.genesis.GenesisData;
import tech.pegasys.teku.spec.datastructures.metadata.BlockContainerAndMetaData;
import tech.pegasys.teku.spec.datastructures.metadata.ObjectAndMetaData;
import tech.pegasys.teku.spec.datastructures.operations.Attestation;
import tech.pegasys.teku.spec.datastructures.operations.AttestationData;
import tech.pegasys.teku.spec.datastructures.operations.SignedAggregateAndProof;
Expand Down Expand Up @@ -498,29 +497,19 @@ private AttestationData createAttestationData(
}

@Override
public SafeFuture<Optional<ObjectAndMetaData<Attestation>>> createAggregate(
public SafeFuture<Optional<Attestation>> createAggregate(
final UInt64 slot,
final Bytes32 attestationHashTreeRoot,
final Optional<UInt64> committeeIndex) {
if (isSyncActive()) {
return NodeSyncingException.failedFuture();
}

final Optional<Attestation> maybeAttestation =
return SafeFuture.completedFuture(
attestationPool
.createAggregateFor(attestationHashTreeRoot, committeeIndex)
.filter(attestation -> attestation.getData().getSlot().equals(slot))
.map(ValidatableAttestation::getAttestation);

return SafeFuture.completedFuture(
maybeAttestation.map(
attestation ->
new ObjectAndMetaData<>(
attestation,
spec.atSlot(attestation.getData().getSlot()).getMilestone(),
false,
false,
false)));
.map(ValidatableAttestation::getAttestation));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ public void createAttestationData_shouldUseCorrectSourceWhenEpochTransitionRequi
@Test
public void createAggregate_shouldFailWhenNodeIsSyncing() {
nodeIsSyncing();
final SafeFuture<Optional<ObjectAndMetaData<Attestation>>> result =
final SafeFuture<Optional<Attestation>> result =
validatorApiHandler.createAggregate(
ONE, dataStructureUtil.randomAttestationData().hashTreeRoot(), Optional.empty());

Expand All @@ -717,18 +717,17 @@ public void createSyncCommitteeContribution() {
@Test
public void createAggregate_shouldReturnAggregateFromAttestationPool() {
final AttestationData attestationData = dataStructureUtil.randomAttestationData();
final Attestation aggregate = dataStructureUtil.randomAttestation();
final Optional<Attestation> aggregate = Optional.of(dataStructureUtil.randomAttestation());
when(attestationPool.createAggregateFor(
eq(attestationData.hashTreeRoot()), eq(Optional.empty())))
.thenReturn(Optional.of(ValidatableAttestation.from(spec, aggregate)));
.thenReturn(aggregate.map(attestation -> ValidatableAttestation.from(spec, attestation)));

assertThat(
validatorApiHandler.createAggregate(
aggregate.getData().getSlot(), attestationData.hashTreeRoot(), Optional.empty()))
.isCompletedWithValue(
Optional.of(
new ObjectAndMetaData<>(
aggregate, spec.getGenesisSpec().getMilestone(), false, false, false)));
aggregate.get().getData().getSlot(),
attestationData.hashTreeRoot(),
Optional.empty()))
.isCompletedWithValue(aggregate);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import tech.pegasys.teku.infrastructure.restapi.endpoints.RestApiRequest;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.datastructures.metadata.ObjectAndMetaData;
import tech.pegasys.teku.spec.datastructures.operations.Attestation;
import tech.pegasys.teku.spec.datastructures.operations.AttestationSchema;

Expand Down Expand Up @@ -72,16 +71,14 @@ public void handleRequest(final RestApiRequest request) throws JsonProcessingExc
final Bytes32 beaconBlockRoot = request.getQueryParameter(ATTESTATION_DATA_ROOT_PARAMETER);
final UInt64 slot = request.getQueryParameter(SLOT_PARAM);

final SafeFuture<Optional<ObjectAndMetaData<Attestation>>> future =
final SafeFuture<Optional<Attestation>> future =
provider.createAggregate(slot, beaconBlockRoot, Optional.empty());

request.respondAsync(
future.thenApply(
maybeAttestationAndMetaData ->
maybeAttestationAndMetaData
.map(
attestationObjectAndMetaData ->
AsyncApiResponse.respondOk(attestationObjectAndMetaData.getData()))
maybeAttestation ->
maybeAttestation
.map(AsyncApiResponse::respondOk)
.orElseGet(AsyncApiResponse::respondNotFound)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ private Optional<SyncCommitteeMessage> checkInternalCommitteeSignature(
return Optional.of(message);
}

public SafeFuture<Optional<ObjectAndMetaData<Attestation>>> createAggregate(
public SafeFuture<Optional<Attestation>> createAggregate(
final UInt64 slot,
final Bytes32 attestationHashTreeRoot,
final Optional<UInt64> committeeIndex) {
Expand All @@ -249,7 +249,8 @@ public SafeFuture<Optional<ObjectAndMetaData<Attestation>>> createAggregate(

public SafeFuture<Optional<ObjectAndMetaData<Attestation>>> createAggregateAndMetaData(
final UInt64 slot, final Bytes32 attestationHashTreeRoot, final UInt64 committeeIndex) {
return createAggregate(slot, attestationHashTreeRoot, Optional.of(committeeIndex));
return createAggregate(slot, attestationHashTreeRoot, Optional.of(committeeIndex))
.thenApply(maybeAttestation -> maybeAttestation.map(this::lookUpMetadata));
}

public SafeFuture<List<SubmitDataError>> sendAggregateAndProofs(
Expand Down Expand Up @@ -348,4 +349,13 @@ private static ValidatorBlockResult generateSubmitSignedBlockResponse(

return new ValidatorBlockResult(responseCode, result.getRejectionReason(), hashRoot);
}

private ObjectAndMetaData<Attestation> lookUpMetadata(final Attestation attestation) {
return new ObjectAndMetaData<>(
attestation,
spec.atSlot(attestation.getData().getSlot()).getMilestone(),
false,
false,
false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import tech.pegasys.teku.spec.datastructures.builder.SignedValidatorRegistration;
import tech.pegasys.teku.spec.datastructures.genesis.GenesisData;
import tech.pegasys.teku.spec.datastructures.metadata.BlockContainerAndMetaData;
import tech.pegasys.teku.spec.datastructures.metadata.ObjectAndMetaData;
import tech.pegasys.teku.spec.datastructures.operations.Attestation;
import tech.pegasys.teku.spec.datastructures.operations.AttestationData;
import tech.pegasys.teku.spec.datastructures.operations.SignedAggregateAndProof;
Expand Down Expand Up @@ -109,7 +108,7 @@ public SafeFuture<Optional<AttestationData>> createAttestationData(
}

@Override
public SafeFuture<Optional<ObjectAndMetaData<Attestation>>> createAggregate(
public SafeFuture<Optional<Attestation>> createAggregate(
UInt64 slot, Bytes32 attestationHashTreeRoot, Optional<UInt64> committeeIndex) {
return SafeFuture.completedFuture(Optional.empty());
}
Expand Down Expand Up @@ -232,7 +231,7 @@ SafeFuture<Optional<BlockContainerAndMetaData>> createUnsignedBlock(

SafeFuture<Optional<AttestationData>> createAttestationData(UInt64 slot, int committeeIndex);

SafeFuture<Optional<ObjectAndMetaData<Attestation>>> createAggregate(
SafeFuture<Optional<Attestation>> createAggregate(
UInt64 slot, Bytes32 attestationHashTreeRoot, Optional<UInt64> committeeIndex);

SafeFuture<Optional<SyncCommitteeContribution>> createSyncCommitteeContribution(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import tech.pegasys.teku.spec.datastructures.builder.SignedValidatorRegistration;
import tech.pegasys.teku.spec.datastructures.genesis.GenesisData;
import tech.pegasys.teku.spec.datastructures.metadata.BlockContainerAndMetaData;
import tech.pegasys.teku.spec.datastructures.metadata.ObjectAndMetaData;
import tech.pegasys.teku.spec.datastructures.operations.Attestation;
import tech.pegasys.teku.spec.datastructures.operations.AttestationData;
import tech.pegasys.teku.spec.datastructures.operations.SignedAggregateAndProof;
Expand Down Expand Up @@ -157,7 +156,7 @@ public SafeFuture<Optional<AttestationData>> createAttestationData(
}

@Override
public SafeFuture<Optional<ObjectAndMetaData<Attestation>>> createAggregate(
public SafeFuture<Optional<Attestation>> createAggregate(
final UInt64 slot,
final Bytes32 attestationHashTreeRoot,
final Optional<UInt64> committeeIndex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@
import tech.pegasys.teku.infrastructure.metrics.StubMetricsSystem;
import tech.pegasys.teku.infrastructure.metrics.TekuMetricCategory;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.SpecMilestone;
import tech.pegasys.teku.spec.TestSpecFactory;
import tech.pegasys.teku.spec.datastructures.genesis.GenesisData;
import tech.pegasys.teku.spec.datastructures.metadata.ObjectAndMetaData;
import tech.pegasys.teku.spec.datastructures.operations.Attestation;
import tech.pegasys.teku.spec.datastructures.operations.AttestationData;
import tech.pegasys.teku.spec.datastructures.operations.SignedAggregateAndProof;
Expand Down Expand Up @@ -210,8 +208,7 @@ public static Stream<Arguments> getDataRequestArguments() {
channel.createAggregate(
attestationData.getSlot(), attestationData.hashTreeRoot(), Optional.empty()),
BeaconNodeRequestLabels.CREATE_AGGREGATE_METHOD,
new ObjectAndMetaData<>(
dataStructureUtil.randomAttestation(), SpecMilestone.PHASE0, false, false, false)),
dataStructureUtil.randomAttestation()),
requestDataTest(
"createSyncCommitteeContribution",
channel ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import tech.pegasys.teku.infrastructure.metrics.Validator.ValidatorDutyMetricsSteps;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.datastructures.metadata.ObjectAndMetaData;
import tech.pegasys.teku.spec.datastructures.operations.AggregateAndProof;
import tech.pegasys.teku.spec.datastructures.operations.Attestation;
import tech.pegasys.teku.spec.datastructures.operations.AttestationData;
Expand Down Expand Up @@ -137,14 +136,10 @@ public SafeFuture<ProductionResult<SignedAggregateAndProof>> createAggregate(
final SafeFuture<Optional<Attestation>> createAggregationFuture =
validatorDutyMetrics.record(
() ->
validatorApiChannel
.createAggregate(
slot,
attestationData.hashTreeRoot(),
Optional.of(aggregator.attestationCommitteeIndex))
.thenApply(
maybeAttestationAndMetaData ->
maybeAttestationAndMetaData.map(ObjectAndMetaData::getData)),
validatorApiChannel.createAggregate(
slot,
attestationData.hashTreeRoot(),
Optional.of(aggregator.attestationCommitteeIndex)),
this,
ValidatorDutyMetricsSteps.CREATE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@
import tech.pegasys.teku.infrastructure.metrics.Validator.ValidatorDutyMetricsSteps;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.SpecMilestone;
import tech.pegasys.teku.spec.TestSpecContext;
import tech.pegasys.teku.spec.TestSpecInvocationContextProvider.SpecContext;
import tech.pegasys.teku.spec.datastructures.metadata.ObjectAndMetaData;
import tech.pegasys.teku.spec.datastructures.operations.AggregateAndProof;
import tech.pegasys.teku.spec.datastructures.operations.AggregateAndProof.AggregateAndProofSchema;
import tech.pegasys.teku.spec.datastructures.operations.Attestation;
Expand Down Expand Up @@ -80,7 +78,6 @@ class AggregationDutyTest {
spy(ValidatorDutyMetrics.create(new StubMetricsSystem()));

private Spec spec;
private SpecMilestone specMilestone;
private DataStructureUtil dataStructureUtil;
private AggregateAndProofSchema aggregateAndProofSchema;
private SignedAggregateAndProofSchema signedAggregateAndProofSchema;
Expand All @@ -92,7 +89,6 @@ class AggregationDutyTest {
@BeforeEach
public void setUp(final SpecContext specContext) {
spec = specContext.getSpec();
specMilestone = specContext.getSpecMilestone();
dataStructureUtil = specContext.getDataStructureUtil();
aggregateAndProofSchema = spec.getGenesisSchemaDefinitions().getAggregateAndProofSchema();
signedAggregateAndProofSchema =
Expand Down Expand Up @@ -145,7 +141,7 @@ public void shouldProduceAggregateAndProof() {
SLOT,
attestationData.hashTreeRoot(),
Optional.of(UInt64.valueOf(attestationCommitteeIndex))))
.thenReturn(completedFuture(Optional.of(addMetaData(aggregate))));
.thenReturn(completedFuture(Optional.of(aggregate)));

final AggregateAndProof expectedAggregateAndProof =
aggregateAndProofSchema.create(UInt64.valueOf(validatorIndex), aggregate, proof);
Expand Down Expand Up @@ -199,12 +195,12 @@ public void shouldProduceAggregateAndProofForMultipleCommittees() {
SLOT,
committee1AttestationData.hashTreeRoot(),
Optional.of(UInt64.valueOf(validator1CommitteeIndex))))
.thenReturn(completedFuture(Optional.of(addMetaData(committee1Aggregate))));
.thenReturn(completedFuture(Optional.of(committee1Aggregate)));
when(validatorApiChannel.createAggregate(
SLOT,
committee2AttestationData.hashTreeRoot(),
Optional.of(UInt64.valueOf(validator2CommitteeIndex))))
.thenReturn(completedFuture(Optional.of(addMetaData(committee2Aggregate))));
.thenReturn(completedFuture(Optional.of(committee2Aggregate)));

final AggregateAndProof aggregateAndProof1 =
aggregateAndProofSchema.create(
Expand Down Expand Up @@ -264,7 +260,7 @@ public void shouldProduceSingleAggregateAndProofWhenMultipleValidatorsAggregateS

when(validatorApiChannel.createAggregate(
SLOT, attestationData.hashTreeRoot(), Optional.of(UInt64.valueOf(committeeIndex))))
.thenReturn(completedFuture(Optional.of(addMetaData(aggregate))));
.thenReturn(completedFuture(Optional.of(aggregate)));
when(validatorApiChannel.sendAggregateAndProofs(anyList()))
.thenReturn(SafeFuture.completedFuture(Collections.emptyList()));

Expand Down Expand Up @@ -383,8 +379,4 @@ private void performAndReportDuty() {
assertThat(result).isCompleted();
safeJoin(result).report(TYPE, SLOT, validatorLogger);
}

private ObjectAndMetaData<Attestation> addMetaData(final Attestation aggregate) {
return new ObjectAndMetaData<>(aggregate, specMilestone, false, false, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
import tech.pegasys.teku.spec.datastructures.builder.SignedValidatorRegistration;
import tech.pegasys.teku.spec.datastructures.genesis.GenesisData;
import tech.pegasys.teku.spec.datastructures.metadata.BlockContainerAndMetaData;
import tech.pegasys.teku.spec.datastructures.metadata.ObjectAndMetaData;
import tech.pegasys.teku.spec.datastructures.operations.Attestation;
import tech.pegasys.teku.spec.datastructures.operations.AttestationData;
import tech.pegasys.teku.spec.datastructures.operations.SignedAggregateAndProof;
Expand Down Expand Up @@ -190,7 +189,7 @@ public SafeFuture<Optional<AttestationData>> createAttestationData(
}

@Override
public SafeFuture<Optional<ObjectAndMetaData<Attestation>>> createAggregate(
public SafeFuture<Optional<Attestation>> createAggregate(
final UInt64 slot,
final Bytes32 attestationHashTreeRoot,
final Optional<UInt64> committeeIndex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,28 +307,24 @@ private List<SubmitDataError> convertPostDataFailureResponseToSubmitDataErrors(
}

@Override
public SafeFuture<Optional<ObjectAndMetaData<Attestation>>> createAggregate(
public SafeFuture<Optional<Attestation>> createAggregate(
final UInt64 slot,
final Bytes32 attestationHashTreeRoot,
final Optional<UInt64> committeeIndex) {
if (spec.atSlot(slot).getMilestone().isGreaterThanOrEqualTo(SpecMilestone.ELECTRA)
&& committeeIndex.isPresent()) {
return sendRequest(
() -> typeDefClient.createAggregate(slot, attestationHashTreeRoot, committeeIndex.get()));
() ->
typeDefClient
.createAggregate(slot, attestationHashTreeRoot, committeeIndex.get())
.map(ObjectAndMetaData::getData));
}

return sendRequest(
() ->
apiClient
.createAggregate(slot, attestationHashTreeRoot)
.map(
attestation ->
new ObjectAndMetaData<>(
attestation.asInternalAttestation(spec),
spec.atSlot(attestation.data.slot).getMilestone(),
false,
false,
false)));
.map(attestation -> attestation.asInternalAttestation(spec)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import tech.pegasys.teku.spec.datastructures.builder.SignedValidatorRegistration;
import tech.pegasys.teku.spec.datastructures.genesis.GenesisData;
import tech.pegasys.teku.spec.datastructures.metadata.BlockContainerAndMetaData;
import tech.pegasys.teku.spec.datastructures.metadata.ObjectAndMetaData;
import tech.pegasys.teku.spec.datastructures.operations.Attestation;
import tech.pegasys.teku.spec.datastructures.operations.AttestationData;
import tech.pegasys.teku.spec.datastructures.operations.SignedAggregateAndProof;
Expand Down Expand Up @@ -127,7 +126,7 @@ public SafeFuture<Optional<AttestationData>> createAttestationData(
}

@Override
public SafeFuture<Optional<ObjectAndMetaData<Attestation>>> createAggregate(
public SafeFuture<Optional<Attestation>> createAggregate(
final UInt64 slot,
final Bytes32 attestationHashTreeRoot,
final Optional<UInt64> committeeIndex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
import tech.pegasys.teku.spec.datastructures.builder.SignedValidatorRegistration;
import tech.pegasys.teku.spec.datastructures.genesis.GenesisData;
import tech.pegasys.teku.spec.datastructures.metadata.BlockContainerAndMetaData;
import tech.pegasys.teku.spec.datastructures.metadata.ObjectAndMetaData;
import tech.pegasys.teku.spec.datastructures.operations.Attestation;
import tech.pegasys.teku.spec.datastructures.operations.AttestationData;
import tech.pegasys.teku.spec.datastructures.operations.SignedAggregateAndProof;
Expand Down Expand Up @@ -700,9 +699,7 @@ private static Stream<Arguments> getRequestsUsingFailover() {
"createAggregate",
apiChannel -> apiChannel.createAggregate(slot, randomBytes32, Optional.empty()),
BeaconNodeRequestLabels.CREATE_AGGREGATE_METHOD,
Optional.of(
new ObjectAndMetaData<>(
attestation, SPEC.atSlot(slot).getMilestone(), false, false, false))),
Optional.of(attestation)),
getArguments(
"createSyncCommitteeContribution",
apiChannel -> apiChannel.createSyncCommitteeContribution(slot, 0, randomBytes32),
Expand Down
Loading

0 comments on commit b5a758b

Please sign in to comment.