Search in sources :

Example 1 with ChainHead

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();
    });
}
Also used : Bytes4(tech.pegasys.teku.infrastructure.bytes.Bytes4) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) ChainHead(tech.pegasys.teku.storage.client.ChainHead)

Example 2 with ChainHead

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());
    }
}
Also used : StateAndBlockSummary(tech.pegasys.teku.spec.datastructures.blocks.StateAndBlockSummary) AttestationGenerator(tech.pegasys.teku.core.AttestationGenerator) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) ChainHead(tech.pegasys.teku.storage.client.ChainHead) Attestation(tech.pegasys.teku.spec.datastructures.operations.Attestation)

Example 3 with ChainHead

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);
}
Also used : Signer(tech.pegasys.teku.core.signatures.Signer) LocalSigner(tech.pegasys.teku.core.signatures.LocalSigner) ChainHead(tech.pegasys.teku.storage.client.ChainHead) Bytes32(org.apache.tuweni.bytes.Bytes32) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState)

Example 4 with ChainHead

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()));
}
Also used : SignedBlockAndState(tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState) ChainHead(tech.pegasys.teku.storage.client.ChainHead) Test(org.junit.jupiter.api.Test)

Example 5 with ChainHead

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));
}
Also used : SignedBlockAndState(tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState) ChainHead(tech.pegasys.teku.storage.client.ChainHead) Test(org.junit.jupiter.api.Test)

Aggregations

ChainHead (tech.pegasys.teku.storage.client.ChainHead)9 Optional (java.util.Optional)3 Test (org.junit.jupiter.api.Test)3 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)3 SignedBlockAndState (tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState)2 SlotAndBlockRoot (tech.pegasys.teku.spec.datastructures.blocks.SlotAndBlockRoot)2 Bytes32 (org.apache.tuweni.bytes.Bytes32)1 AttestationGenerator (tech.pegasys.teku.core.AttestationGenerator)1 LocalSigner (tech.pegasys.teku.core.signatures.LocalSigner)1 Signer (tech.pegasys.teku.core.signatures.Signer)1 Bytes4 (tech.pegasys.teku.infrastructure.bytes.Bytes4)1 BeaconBlockHeader (tech.pegasys.teku.spec.datastructures.blocks.BeaconBlockHeader)1 StateAndBlockSummary (tech.pegasys.teku.spec.datastructures.blocks.StateAndBlockSummary)1 Attestation (tech.pegasys.teku.spec.datastructures.operations.Attestation)1 BeaconState (tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState)1 BeaconStateAltair (tech.pegasys.teku.spec.datastructures.state.beaconstate.versions.altair.BeaconStateAltair)1