use of tech.pegasys.teku.storage.client.ChainHead in project teku by ConsenSys.
the class ActiveEth2P2PNetwork method getEth2Context.
private SafeFuture<Eth2Context> getEth2Context() {
final ChainHead chainHead = recentChainData.getChainHead().orElseThrow();
final Bytes4 forkDigest = recentChainData.getCurrentForkInfo().orElseThrow().getForkDigest(spec);
final UInt64 currentSlot = recentChainData.getCurrentSlot().orElseThrow();
final UInt64 currentEpoch = spec.computeEpochAtSlot(currentSlot);
return chainHead.getState().thenApply(chainHeadState -> {
final UInt64 activeValidatorsEpoch = spec.getMaxLookaheadEpoch(chainHeadState).min(currentEpoch);
final int activeValidators = spec.countActiveValidators(chainHeadState, activeValidatorsEpoch);
return Eth2Context.builder().currentSlot(currentSlot).activeValidatorCount(activeValidators).forkDigest(forkDigest).gossipEncoding(gossipEncoding).build();
});
}
use of tech.pegasys.teku.storage.client.ChainHead in project teku by ConsenSys.
the class BeaconChainUtil method finalizeChainAtEpoch.
public void finalizeChainAtEpoch(final UInt64 epoch) throws Exception {
if (recentChainData.getStore().getFinalizedCheckpoint().getEpoch().compareTo(epoch) >= 0) {
throw new Exception("Chain already finalized at this or higher epoch");
}
AttestationGenerator attestationGenerator = new AttestationGenerator(spec, validatorKeys);
createAndImportBlockAtSlot(recentChainData.getHeadSlot().plus(spec.getGenesisSpecConfig().getMinAttestationInclusionDelay()));
while (recentChainData.getStore().getFinalizedCheckpoint().getEpoch().compareTo(epoch) < 0) {
ChainHead head = recentChainData.getChainHead().orElseThrow();
final StateAndBlockSummary headStateAndBlockSummary = safeJoin(head.asStateAndBlockSummary());
UInt64 slot = recentChainData.getHeadSlot();
SszList<Attestation> currentSlotAssignments = beaconBlockBodySchema.getAttestationsSchema().createFromElements(attestationGenerator.getAttestationsForSlot(headStateAndBlockSummary, slot));
createAndImportBlockAtSlot(recentChainData.getHeadSlot().plus(UInt64.ONE), Optional.of(currentSlotAssignments), Optional.empty(), Optional.empty(), Optional.empty());
}
}
use of tech.pegasys.teku.storage.client.ChainHead in project teku by ConsenSys.
the class BeaconChainUtil method createBlockAndStateAtSlot.
private SignedBlockAndState createBlockAndStateAtSlot(final UInt64 slot, boolean withValidProposer, Optional<SszList<Attestation>> attestations, Optional<SszList<Deposit>> deposits, Optional<SszList<SignedVoluntaryExit>> exits, Optional<Eth1Data> eth1Data) throws Exception {
checkState(withValidProposer || validatorKeys.size() > 1, "Must have >1 validator in order to create a block from an invalid proposer.");
final ChainHead bestBlockAndState = recentChainData.getChainHead().orElseThrow();
final Bytes32 bestBlockRoot = bestBlockAndState.getRoot();
final BeaconState preState = safeJoin(bestBlockAndState.getState());
checkArgument(bestBlockAndState.getSlot().compareTo(slot) < 0, "Slot must be in the future.");
final int correctProposerIndex = blockCreator.getProposerIndexForSlot(preState, slot);
final int proposerIndex = withValidProposer ? correctProposerIndex : getWrongProposerIndex(correctProposerIndex);
final Signer signer = getSigner(proposerIndex);
return blockCreator.createBlock(signer, slot, preState, bestBlockRoot, attestations, deposits, exits, eth1Data, Optional.empty(), Optional.empty(), Optional.empty(), false);
}
use of tech.pegasys.teku.storage.client.ChainHead in project teku by ConsenSys.
the class StateSelectorFactoryTest method headSelector_shouldGetBestState.
@Test
public void headSelector_shouldGetBestState() throws ExecutionException, InterruptedException {
final SignedBlockAndState blockAndState = data.randomSignedBlockAndState(10);
final ChainHead chainHead = ChainHead.create(blockAndState);
when(client.getChainHead()).thenReturn(Optional.of(chainHead));
Optional<StateAndMetaData> result = factory.headSelector().getState().get();
assertThat(result).contains(withMetaData(blockAndState.getState()));
}
use of tech.pegasys.teku.storage.client.ChainHead in project teku by ConsenSys.
the class StateSelectorFactoryTest method forSlot_shouldGetStateAtSlotExact.
@Test
public void forSlot_shouldGetStateAtSlotExact() throws ExecutionException, InterruptedException {
final SignedBlockAndState blockAndState = data.randomSignedBlockAndState(15);
final ChainHead chainHead = ChainHead.create(blockAndState);
when(client.getChainHead()).thenReturn(Optional.of(chainHead));
when(client.getStateAtSlotExact(state.getSlot(), chainHead.getRoot())).thenReturn(SafeFuture.completedFuture(Optional.of(state)));
Optional<StateAndMetaData> result = factory.forSlot(state.getSlot()).getState().get();
assertThat(result).contains(withMetaData(state));
}
Aggregations