Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merge] Add ForkChoiceNotifier to combine fork choice updates with beacon proposers #4688

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import tech.pegasys.teku.statetransition.attestation.AggregatingAttestationPool;
import tech.pegasys.teku.statetransition.attestation.AttestationManager;
import tech.pegasys.teku.statetransition.block.BlockImportChannel;
import tech.pegasys.teku.statetransition.forkchoice.ForkChoiceNotifier;
import tech.pegasys.teku.statetransition.forkchoice.ForkChoiceTrigger;
import tech.pegasys.teku.statetransition.synccommittee.SyncCommitteeContributionPool;
import tech.pegasys.teku.statetransition.synccommittee.SyncCommitteeMessagePool;
Expand Down Expand Up @@ -73,6 +74,7 @@ public class ValidatorApiHandlerIntegrationTest {
private final BlockGossipChannel blockGossipChannel = mock(BlockGossipChannel.class);
private final ChainDataProvider chainDataProvider = mock(ChainDataProvider.class);
private final ForkChoiceTrigger forkChoiceTrigger = mock(ForkChoiceTrigger.class);
private final ForkChoiceNotifier forkChoiceNotifier = mock(ForkChoiceNotifier.class);

private final ChainUpdater chainUpdater = storageSystem.chainUpdater();
private final SyncCommitteeMessagePool syncCommitteeMessagePool =
Expand All @@ -97,6 +99,7 @@ public class ValidatorApiHandlerIntegrationTest {
performanceTracker,
spec,
forkChoiceTrigger,
forkChoiceNotifier,
syncCommitteeMessagePool,
syncCommitteeContributionPool,
syncCommitteeSubscriptionManager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import tech.pegasys.teku.spec.datastructures.operations.versions.altair.SyncCommitteeContribution;
import tech.pegasys.teku.spec.datastructures.operations.versions.altair.SyncCommitteeMessage;
import tech.pegasys.teku.spec.datastructures.operations.versions.altair.ValidateableSyncCommitteeMessage;
import tech.pegasys.teku.spec.datastructures.operations.versions.merge.BeaconPreparableProposer;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState;
import tech.pegasys.teku.spec.datastructures.util.AttestationProcessingResult;
import tech.pegasys.teku.spec.datastructures.validator.SubnetSubscription;
Expand All @@ -68,6 +69,7 @@
import tech.pegasys.teku.statetransition.attestation.AggregatingAttestationPool;
import tech.pegasys.teku.statetransition.attestation.AttestationManager;
import tech.pegasys.teku.statetransition.block.BlockImportChannel;
import tech.pegasys.teku.statetransition.forkchoice.ForkChoiceNotifier;
import tech.pegasys.teku.statetransition.forkchoice.ForkChoiceTrigger;
import tech.pegasys.teku.statetransition.synccommittee.SyncCommitteeContributionPool;
import tech.pegasys.teku.statetransition.synccommittee.SyncCommitteeMessagePool;
Expand All @@ -76,7 +78,6 @@
import tech.pegasys.teku.sync.events.SyncStateProvider;
import tech.pegasys.teku.validator.api.AttesterDuties;
import tech.pegasys.teku.validator.api.AttesterDuty;
import tech.pegasys.teku.validator.api.BeaconPreparableProposer;
import tech.pegasys.teku.validator.api.CommitteeSubscriptionRequest;
import tech.pegasys.teku.validator.api.NodeSyncingException;
import tech.pegasys.teku.validator.api.ProposerDuties;
Expand Down Expand Up @@ -116,6 +117,7 @@ public class ValidatorApiHandler implements ValidatorApiChannel {
private final SyncCommitteeMessagePool syncCommitteeMessagePool;
private final SyncCommitteeSubscriptionManager syncCommitteeSubscriptionManager;
private final SyncCommitteeContributionPool syncCommitteeContributionPool;
private final ForkChoiceNotifier forkChoiceNotifier;

public ValidatorApiHandler(
final ChainDataProvider chainDataProvider,
Expand All @@ -132,6 +134,7 @@ public ValidatorApiHandler(
final PerformanceTracker performanceTracker,
final Spec spec,
final ForkChoiceTrigger forkChoiceTrigger,
final ForkChoiceNotifier forkChoiceNotifier,
final SyncCommitteeMessagePool syncCommitteeMessagePool,
final SyncCommitteeContributionPool syncCommitteeContributionPool,
final SyncCommitteeSubscriptionManager syncCommitteeSubscriptionManager) {
Expand All @@ -152,6 +155,7 @@ public ValidatorApiHandler(
this.syncCommitteeMessagePool = syncCommitteeMessagePool;
this.syncCommitteeContributionPool = syncCommitteeContributionPool;
this.syncCommitteeSubscriptionManager = syncCommitteeSubscriptionManager;
this.forkChoiceNotifier = forkChoiceNotifier;
}

@Override
Expand Down Expand Up @@ -593,7 +597,9 @@ public SafeFuture<Void> sendSignedContributionAndProofs(

@Override
public void prepareBeaconProposer(
Collection<BeaconPreparableProposer> beaconPreparableProposers) {}
Collection<BeaconPreparableProposer> beaconPreparableProposers) {
forkChoiceNotifier.onUpdatePreparableProposers(beaconPreparableProposers);
}

private Optional<SubmitDataError> fromInternalValidationResult(
final InternalValidationResult internalValidationResult, final int resultIndex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import tech.pegasys.teku.statetransition.attestation.AggregatingAttestationPool;
import tech.pegasys.teku.statetransition.attestation.AttestationManager;
import tech.pegasys.teku.statetransition.block.BlockImportChannel;
import tech.pegasys.teku.statetransition.forkchoice.ForkChoiceNotifier;
import tech.pegasys.teku.statetransition.forkchoice.ForkChoiceTrigger;
import tech.pegasys.teku.statetransition.synccommittee.SyncCommitteeContributionPool;
import tech.pegasys.teku.statetransition.synccommittee.SyncCommitteeMessagePool;
Expand Down Expand Up @@ -119,6 +120,7 @@ class ValidatorApiHandlerTest {
private final ChainDataProvider chainDataProvider = mock(ChainDataProvider.class);
private final DutyMetrics dutyMetrics = mock(DutyMetrics.class);
private final ForkChoiceTrigger forkChoiceTrigger = mock(ForkChoiceTrigger.class);
private final ForkChoiceNotifier forkChoiceNotifier = mock(ForkChoiceNotifier.class);
private final SyncCommitteeMessagePool syncCommitteeMessagePool =
mock(SyncCommitteeMessagePool.class);
private final SyncCommitteeContributionPool syncCommitteeContributionPool =
Expand All @@ -142,6 +144,7 @@ class ValidatorApiHandlerTest {
performanceTracker,
spec,
forkChoiceTrigger,
forkChoiceNotifier,
syncCommitteeMessagePool,
syncCommitteeContributionPool,
syncCommitteeSubscriptionManager);
Expand Down Expand Up @@ -387,6 +390,7 @@ void getSyncCommitteeDuties_shouldNotUseEpochPriorToFork() {
performanceTracker,
spec,
forkChoiceTrigger,
forkChoiceNotifier,
syncCommitteeMessagePool,
syncCommitteeContributionPool,
syncCommitteeSubscriptionManager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@
import io.javalin.plugin.openapi.annotations.OpenApiContent;
import io.javalin.plugin.openapi.annotations.OpenApiRequestBody;
import io.javalin.plugin.openapi.annotations.OpenApiResponse;
import java.util.Arrays;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.List;
import org.jetbrains.annotations.NotNull;
import tech.pegasys.teku.api.DataProvider;
import tech.pegasys.teku.api.ValidatorDataProvider;
Expand All @@ -43,15 +39,17 @@

public class PostPrepareBeaconProposer extends AbstractHandler implements Handler {
public static final String ROUTE = "/eth/v1/validator/prepare_beacon_proposer";
private static final Logger LOG = LogManager.getLogger();

private final ValidatorDataProvider validatorDataProvider;

public PostPrepareBeaconProposer(final DataProvider provider, final JsonProvider jsonProvider) {
this(provider.getValidatorDataProvider(), jsonProvider);
}

public PostPrepareBeaconProposer(
final ValidatorDataProvider provider, final JsonProvider jsonProvider) {
final ValidatorDataProvider validatorDataProvider, final JsonProvider jsonProvider) {
super(jsonProvider);
this.validatorDataProvider = validatorDataProvider;
}

@OpenApi(
Expand All @@ -78,13 +76,7 @@ public void handle(@NotNull final Context ctx) throws Exception {
final BeaconPreparableProposer[] request =
parseRequestBody(ctx.body(), BeaconPreparableProposer[].class);

LOG.trace(
"received: {}",
(Supplier<String>)
() ->
Arrays.stream(request)
.map(BeaconPreparableProposer::toString)
.collect(Collectors.joining(",")));
validatorDataProvider.prepareBeaconProposer(List.of(request));

ctx.status(SC_OK);
} catch (final IllegalArgumentException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import tech.pegasys.teku.api.schema.altair.SignedContributionAndProof;
import tech.pegasys.teku.api.schema.altair.SyncCommitteeMessage;
import tech.pegasys.teku.api.schema.altair.SyncCommitteeSubnetSubscription;
import tech.pegasys.teku.api.schema.merge.BeaconPreparableProposer;
import tech.pegasys.teku.api.schema.merge.SignedBeaconBlockMerge;
import tech.pegasys.teku.api.schema.phase0.SignedBeaconBlockPhase0;
import tech.pegasys.teku.infrastructure.async.SafeFuture;
Expand Down Expand Up @@ -368,6 +369,17 @@ public SafeFuture<Void> sendContributionAndProofs(
.collect(toList()));
}

public void prepareBeaconProposer(
Collection<BeaconPreparableProposer> beaconPreparableProposers) {
List<tech.pegasys.teku.spec.datastructures.operations.versions.merge.BeaconPreparableProposer>
internalBeaconPreparableProposer =
beaconPreparableProposers.stream()
.map(BeaconPreparableProposer::asInternalBeaconPreparableProposer)
.collect(Collectors.toUnmodifiableList());

validatorApiChannel.prepareBeaconProposer(internalBeaconPreparableProposer);
}

public boolean isPhase0Slot(final UInt64 slot) {
return spec.atSlot(slot).getMilestone() == SpecMilestone.PHASE0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.MoreObjects;
import io.swagger.v3.oas.annotations.media.Schema;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.ssz.type.Bytes20;
Expand All @@ -45,11 +44,11 @@ public BeaconPreparableProposer(
this.fee_recipient = fee_recipient;
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("validator_index", validator_index)
.add("fee_recipient", fee_recipient)
.toString();
public static tech.pegasys.teku.spec.datastructures.operations.versions.merge
.BeaconPreparableProposer
asInternalBeaconPreparableProposer(BeaconPreparableProposer beaconPreparableProposer) {
return new tech.pegasys.teku.spec.datastructures.operations.versions.merge
.BeaconPreparableProposer(
beaconPreparableProposer.validator_index, beaconPreparableProposer.fee_recipient);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static ExecutionEngineChannelImpl create(String eeEndpoint, Spec spec) {
return new ExecutionEngineChannelImpl(new Web3JExecutionEngineClient(eeEndpoint), spec);
}

public ExecutionEngineChannelImpl(ExecutionEngineClient executionEngineClient, Spec spec) {
private ExecutionEngineChannelImpl(ExecutionEngineClient executionEngineClient, Spec spec) {
this.spec = spec;
this.executionEngineClient = executionEngineClient;
}
Expand Down
8 changes: 8 additions & 0 deletions ethereum/spec/src/main/java/tech/pegasys/teku/spec/Spec.java
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,10 @@ public UInt64 computeEpochAtSlot(final UInt64 slot) {
return atSlot(slot).miscHelpers().computeEpochAtSlot(slot);
}

public UInt64 computeTimeAtSlot(BeaconState state, UInt64 slot) {
return atSlot(slot).miscHelpers().computeTimeAtSlot(state, slot);
}

public Bytes computeSigningRoot(BeaconBlock block, Bytes32 domain) {
return atBlock(block).miscHelpers().computeSigningRoot(block, domain);
}
Expand Down Expand Up @@ -328,6 +332,10 @@ public Bytes32 getDomain(
.getDomain(domainType, epoch, fork, genesisValidatorsRoot);
}

public Bytes32 getRandaoMix(final BeaconState state, final UInt64 epoch) {
return atEpoch(epoch).beaconStateAccessors().getRandaoMix(state, epoch);
}

public boolean verifyProposerSlashingSignature(
BeaconState state,
ProposerSlashing proposerSlashing,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* specific language governing permissions and limitations under the License.
*/

package tech.pegasys.teku.validator.api;
package tech.pegasys.teku.spec.datastructures.operations.versions.merge;

import com.google.common.base.MoreObjects;
import java.util.Objects;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package tech.pegasys.teku.spec.executionengine;

import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
import java.util.Optional;
import org.apache.tuweni.bytes.Bytes32;

Expand Down Expand Up @@ -54,4 +55,19 @@ public String toString() {
.add("finalizedBlockHash", finalizedBlockHash)
.toString();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ForkChoiceState)) return false;
ForkChoiceState that = (ForkChoiceState) o;
return headBlockHash == that.headBlockHash
&& safeBlockHash == that.safeBlockHash
&& finalizedBlockHash == that.finalizedBlockHash;
}

@Override
public int hashCode() {
return Objects.hashCode(headBlockHash, safeBlockHash, finalizedBlockHash);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package tech.pegasys.teku.spec.executionengine;

import com.google.common.base.MoreObjects;
import java.util.Objects;
import java.util.Optional;
import tech.pegasys.teku.ssz.type.Bytes8;
Expand Down Expand Up @@ -46,4 +47,12 @@ public boolean equals(final Object o) {
public int hashCode() {
return Objects.hash(status, payloadId);
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("status", status)
.add("payloadId", payloadId)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package tech.pegasys.teku.spec.executionengine;

import com.google.common.base.MoreObjects;
import java.util.Objects;
import org.apache.tuweni.bytes.Bytes32;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.ssz.type.Bytes20;
Expand Down Expand Up @@ -41,6 +42,25 @@ public Bytes20 getFeeRecipient() {
return feeRecipient;
}

@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final PayloadAttributes that = (PayloadAttributes) o;
return Objects.equals(timestamp, that.timestamp)
&& Objects.equals(random, that.random)
&& Objects.equals(feeRecipient, that.feeRecipient);
}

@Override
public int hashCode() {
return Objects.hash(timestamp, random, feeRecipient);
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ public UInt64 computeStartSlotAtEpoch(UInt64 epoch) {
return epoch.times(specConfig.getSlotsPerEpoch());
}

public UInt64 computeTimeAtSlot(BeaconState state, UInt64 slot) {
UInt64 slotsSinceGenesis = slot.minus(SpecConfig.GENESIS_SLOT);
return state.getGenesis_time().plus(slotsSinceGenesis.times(specConfig.getSecondsPerSlot()));
}

public boolean isSlotAtNthEpochBoundary(
final UInt64 blockSlot, final UInt64 parentSlot, final int n) {
checkArgument(n > 0, "Parameter n must be greater than 0");
Expand Down
Loading