-
Notifications
You must be signed in to change notification settings - Fork 291
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
|
@@ -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(); | ||
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()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?