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

Transition finalized state until the first slot after the finalized epoch #8782

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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 @@ -16,20 +16,26 @@
import static tech.pegasys.teku.spec.config.SpecConfig.GENESIS_SLOT;

import java.util.Optional;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.tuweni.bytes.Bytes32;
import tech.pegasys.teku.api.AbstractSelectorFactory;
import tech.pegasys.teku.infrastructure.async.SafeFuture;
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.datastructures.blocks.BeaconBlockHeader;
import tech.pegasys.teku.spec.datastructures.metadata.StateAndMetaData;
import tech.pegasys.teku.spec.datastructures.state.AnchorPoint;
import tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState;
import tech.pegasys.teku.spec.logic.common.statetransition.exceptions.EpochProcessingException;
import tech.pegasys.teku.spec.logic.common.statetransition.exceptions.SlotProcessingException;
import tech.pegasys.teku.storage.client.ChainHead;
import tech.pegasys.teku.storage.client.CombinedChainDataClient;

public class StateSelectorFactory extends AbstractSelectorFactory<StateSelector> {

private final Spec spec;
private static final Logger LOG = LogManager.getLogger();

public StateSelectorFactory(final Spec spec, final CombinedChainDataClient client) {
super(client);
Expand Down Expand Up @@ -79,20 +85,45 @@ public StateSelector genesisSelector() {

@Override
public StateSelector finalizedSelector() {

return () ->
SafeFuture.completedFuture(
client
.getLatestFinalized()
.map(
finalized ->
addMetaData(
finalized.getState(),
// The finalized checkpoint may change because of optimistically
// imported blocks at the head and if the head isn't optimistic, the
// finalized block can't be optimistic.
client.isChainHeadOptimistic(),
true,
true)));
finalized -> {
final UInt64 slot = finalized.getSlot();
final Spec spec = finalized.getSpec();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have spec already at StateSelectorFactory

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how did I miss that?

final BeaconState finalizedState;
//if this isn't the first slot after the finalized epoch than
// we'll compute empty slots until we get there
if (!slot.dividedBy(spec.getSlotsPerEpoch(slot)).isZero()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd avoid checks like this especially we consider changes in slots time or maybe slots per epoch in the future. Use spec helpers like computeStartSlotAtEpoch and compare actual and expected slot

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, that is much better, thanks for the heads up.

finalizedState = processEmptySlots(finalized, spec);
} else {
finalizedState = finalized.getState();
}
return addMetaData(
finalizedState,
// The finalized checkpoint may change because of optimistically
// imported blocks at the head and if the head isn't optimistic, the
// finalized block can't be optimistic.
client.isChainHeadOptimistic(),
true,
true);
}));
}

private BeaconState processEmptySlots(final AnchorPoint finalized, final Spec spec) {
final BeaconState finalizedState;
try {
UInt64 slotToBeReached =
spec.computeStartSlotAtEpoch(finalized.getEpoch());
finalizedState =
spec.processSlots(finalized.getState(), slotToBeReached);
} catch (SlotProcessingException | EpochProcessingException e) {
throw new RuntimeException(e);
}
return finalizedState;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,10 @@ public int hashCode() {

@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("blockSummary", blockSummary).toString();
return MoreObjects.toStringHelper(this)
.add("blockSummary", blockSummary)
.add("state", state)
.toString();
}

private Optional<ExecutionPayloadHeader> getLatestExecutionPayloadHeader() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ public Checkpoint getCheckpoint() {
return checkpoint;
}

public Spec getSpec() {
return spec;
}

public UInt64 getBlockSlot() {
return blockSummary.getSlot();
}
Expand Down
Loading