Search in sources :

Example 1 with AnchorPoint

use of tech.pegasys.teku.spec.datastructures.state.AnchorPoint in project teku by ConsenSys.

the class HistoricalBlockSyncServiceTest method shouldOnlySendOneRequestAtATime.

@Test
public void shouldOnlySendOneRequestAtATime() {
    currentSyncState.set(SyncState.IN_SYNC);
    // Setup chain
    final long epochHeight = 10;
    storageSystem.chainBuilder().generateGenesis();
    storageSystem.chainBuilder().generateBlocksUpToSlot(slotsPerEpoch * epochHeight + 3);
    final AnchorPoint anchor = initializeChainAtEpoch(storageSystem.chainBuilder().getLatestEpoch());
    final List<SignedBeaconBlock> expectedBlocks = storageSystem.chainBuilder().streamBlocksAndStates(0, anchor.getBlockSlot().longValue()).map(SignedBlockAndState::getBlock).collect(Collectors.toList());
    // Set up a peer to respond
    final RespondingEth2Peer peer = RespondingEth2Peer.create(spec, storageSystem.chainBuilder());
    peer.updateStatus(new Checkpoint(UInt64.valueOf(epochHeight * 2), Bytes32.ZERO), new Checkpoint(UInt64.valueOf(epochHeight * 2), Bytes32.ZERO));
    when(network.streamPeers()).thenAnswer(i -> Stream.of(peer));
    startService();
    final int maxRequests = storageSystem.chainBuilder().getLatestSlot().dividedBy(batchSize).plus(1).intValue();
    int requestCount = 0;
    while (service.isRunning() && requestCount <= maxRequests) {
        // Trigger some sync events
        updateSyncState(SyncState.SYNCING);
        updateSyncState(SyncState.IN_SYNC);
        // Peer should only have 1 outstanding request
        assertThat(peer.getOutstandingRequests()).isEqualTo(1);
        assertThat(asyncRunner.countDelayedActions()).isEqualTo(0);
        peer.completePendingRequests();
        requestCount++;
    }
    assertServiceFinished();
    assertBlocksSaved(expectedBlocks);
}
Also used : Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) AnchorPoint(tech.pegasys.teku.spec.datastructures.state.AnchorPoint) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) AnchorPoint(tech.pegasys.teku.spec.datastructures.state.AnchorPoint) Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) RespondingEth2Peer(tech.pegasys.teku.networking.eth2.peers.RespondingEth2Peer) Test(org.junit.jupiter.api.Test)

Example 2 with AnchorPoint

use of tech.pegasys.teku.spec.datastructures.state.AnchorPoint in project teku by ConsenSys.

the class HistoricalBlockSyncServiceTest method initializeChainAtEpoch.

private AnchorPoint initializeChainAtEpoch(final UInt64 epoch, final boolean includeAnchorBlock) {
    final Checkpoint anchorCheckpoint = storageSystem.chainBuilder().getCurrentCheckpointForEpoch(epoch);
    final SignedBlockAndState anchorStateAndBlock = storageSystem.chainBuilder().getBlockAndState(anchorCheckpoint.getRoot()).orElseThrow();
    final Optional<SignedBeaconBlock> block = includeAnchorBlock ? Optional.of(anchorStateAndBlock.getBlock()) : Optional.empty();
    final AnchorPoint anchorPoint = AnchorPoint.create(spec, anchorCheckpoint, anchorStateAndBlock.getState(), block);
    storageSystem.recentChainData().initializeFromAnchorPoint(anchorPoint, UInt64.ZERO);
    return anchorPoint;
}
Also used : Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) AnchorPoint(tech.pegasys.teku.spec.datastructures.state.AnchorPoint) SignedBlockAndState(tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock)

Example 3 with AnchorPoint

use of tech.pegasys.teku.spec.datastructures.state.AnchorPoint in project teku by ConsenSys.

the class HistoricalBlockSyncServiceTest method shouldWaitToRunTillNodeIsInSync.

@Test
public void shouldWaitToRunTillNodeIsInSync() {
    currentSyncState.set(SyncState.SYNCING);
    // Setup chain
    final long epochHeight = 10;
    storageSystem.chainBuilder().generateGenesis();
    storageSystem.chainBuilder().generateBlocksUpToSlot(slotsPerEpoch * epochHeight + 3);
    final AnchorPoint anchor = initializeChainAtEpoch(storageSystem.chainBuilder().getLatestEpoch());
    final List<SignedBeaconBlock> expectedBlocks = storageSystem.chainBuilder().streamBlocksAndStates(0, anchor.getBlockSlot().longValue()).map(SignedBlockAndState::getBlock).collect(Collectors.toList());
    // Set up a peer to respond
    final RespondingEth2Peer peer = RespondingEth2Peer.create(spec, storageSystem.chainBuilder());
    peer.updateStatus(new Checkpoint(UInt64.valueOf(epochHeight * 2), Bytes32.ZERO), new Checkpoint(UInt64.valueOf(epochHeight * 2), Bytes32.ZERO));
    when(network.streamPeers()).thenAnswer(i -> Stream.of(peer));
    startService();
    // We should be waiting to actually start the historic sync
    assertServiceNotActive(peer);
    verify(storageUpdateChannel, never()).onFinalizedBlocks(any());
    // When we switch to in sync, the service should run and complete
    updateSyncState(SyncState.IN_SYNC);
    // We should start sending requests to pull data
    assertThat(peer.getOutstandingRequests()).isEqualTo(1);
    finishSyncing(peer, expectedBlocks);
}
Also used : Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) AnchorPoint(tech.pegasys.teku.spec.datastructures.state.AnchorPoint) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) RespondingEth2Peer(tech.pegasys.teku.networking.eth2.peers.RespondingEth2Peer) Test(org.junit.jupiter.api.Test)

Example 4 with AnchorPoint

use of tech.pegasys.teku.spec.datastructures.state.AnchorPoint in project teku by ConsenSys.

the class ForkChoiceStrategyTest method findHead_worksForChainInitializedFromNonGenesisAnchor.

@Test
public void findHead_worksForChainInitializedFromNonGenesisAnchor() {
    // Set up store with an anchor point that has justified and finalized checkpoints prior to its
    // epoch
    final UInt64 initialEpoch = UInt64.valueOf(100);
    final BeaconState anchorState = dataStructureUtil.stateBuilderPhase0().setJustifiedCheckpointsToEpoch(initialEpoch.minus(2)).setFinalizedCheckpointToEpoch(initialEpoch.minus(3)).setSlotToStartOfEpoch(initialEpoch).build();
    AnchorPoint anchor = dataStructureUtil.createAnchorFromState(anchorState);
    final ProtoArray protoArray = ProtoArray.builder().initialCheckpoint(Optional.of(anchor.getCheckpoint())).justifiedCheckpoint(anchorState.getCurrent_justified_checkpoint()).finalizedCheckpoint(anchorState.getFinalized_checkpoint()).build();
    protoArray.onBlock(anchor.getBlockSlot(), anchor.getRoot(), anchor.getParentRoot(), anchor.getStateRoot(), anchor.getEpoch(), anchor.getEpoch(), Bytes32.ZERO, false);
    final ForkChoiceStrategy forkChoiceStrategy = ForkChoiceStrategy.initialize(spec, protoArray);
    TestStoreImpl store = new TestStoreFactory().createAnchorStore(anchor);
    assertThat(forkChoiceStrategy.getTotalTrackedNodeCount()).isEqualTo(1);
    final List<UInt64> effectiveBalances = dataStructureUtil.getSpec().getBeaconStateUtil(anchor.getState().getSlot()).getEffectiveBalances(anchor.getState());
    final Bytes32 head = forkChoiceStrategy.applyPendingVotes(store, Optional.empty(), anchor.getCheckpoint(), anchor.getCheckpoint(), effectiveBalances, ZERO);
    assertThat(head).isEqualTo(anchor.getRoot());
}
Also used : TestStoreImpl(tech.pegasys.teku.spec.datastructures.forkchoice.TestStoreImpl) AnchorPoint(tech.pegasys.teku.spec.datastructures.state.AnchorPoint) ReadOnlyForkChoiceStrategy(tech.pegasys.teku.spec.datastructures.forkchoice.ReadOnlyForkChoiceStrategy) TestStoreFactory(tech.pegasys.teku.spec.datastructures.forkchoice.TestStoreFactory) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) Bytes32(org.apache.tuweni.bytes.Bytes32) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) Test(org.junit.jupiter.api.Test)

Example 5 with AnchorPoint

use of tech.pegasys.teku.spec.datastructures.state.AnchorPoint in project teku by ConsenSys.

the class PeerChainValidator method isFinalizedCheckpointValid.

private SafeFuture<Boolean> isFinalizedCheckpointValid(final Eth2Peer peer, final PeerStatus status) {
    final UInt64 remoteFinalizedEpoch = status.getFinalizedEpoch();
    final AnchorPoint localFinalized = chainDataClient.getStore().getLatestFinalized();
    final UInt64 localFinalizedEpoch = localFinalized.getEpoch();
    final UInt64 currentEpoch = chainDataClient.getCurrentEpoch();
    // Make sure remote finalized epoch is reasonable
    if (remoteEpochIsInvalid(currentEpoch, remoteFinalizedEpoch)) {
        LOG.debug("Peer is advertising invalid finalized epoch {} which is at or ahead of our current epoch {}: {}", remoteFinalizedEpoch, currentEpoch, peer.getId());
        return SafeFuture.completedFuture(false);
    }
    // Check whether finalized checkpoints are compatible
    if (localFinalizedEpoch.equals(remoteFinalizedEpoch)) {
        LOG.trace("Finalized epoch for peer {} matches our own finalized epoch {}, verify blocks roots match", peer.getId(), localFinalizedEpoch);
        return verifyFinalizedCheckpointsAreTheSame(localFinalized, status);
    } else if (localFinalizedEpoch.isGreaterThan(remoteFinalizedEpoch)) {
        // We're ahead of our peer, check that we agree with our peer's finalized epoch
        LOG.trace("Our finalized epoch {} is ahead of our peer's ({}) finalized epoch {}, check that we consider our peer's finalized block to be canonical.", localFinalizedEpoch, peer.getId(), remoteFinalizedEpoch);
        return verifyPeersFinalizedCheckpointIsCanonical(peer, status);
    } else {
        // Our peer is ahead of us, check that they agree on our finalized epoch
        LOG.trace("Our finalized epoch {} is behind of our peer's ({}) finalized epoch {}, check that our peer considers our latest finalized block to be canonical.", localFinalizedEpoch, peer.getId(), remoteFinalizedEpoch);
        return verifyPeerAgreesWithOurFinalizedCheckpoint(peer, localFinalized);
    }
}
Also used : AnchorPoint(tech.pegasys.teku.spec.datastructures.state.AnchorPoint) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64)

Aggregations

AnchorPoint (tech.pegasys.teku.spec.datastructures.state.AnchorPoint)33 Test (org.junit.jupiter.api.Test)16 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)16 Checkpoint (tech.pegasys.teku.spec.datastructures.state.Checkpoint)14 SignedBlockAndState (tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState)12 SignedBeaconBlock (tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock)11 BeaconState (tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState)5 DataStructureUtil (tech.pegasys.teku.spec.util.DataStructureUtil)5 ChainBuilder (tech.pegasys.teku.core.ChainBuilder)4 RespondingEth2Peer (tech.pegasys.teku.networking.eth2.peers.RespondingEth2Peer)4 UpdatableStore (tech.pegasys.teku.storage.store.UpdatableStore)4 Bytes32 (org.apache.tuweni.bytes.Bytes32)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 ArgumentsSource (org.junit.jupiter.params.provider.ArgumentsSource)3 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 Optional (java.util.Optional)2 InvalidConfigurationException (tech.pegasys.teku.infrastructure.exceptions.InvalidConfigurationException)2 Spec (tech.pegasys.teku.spec.Spec)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1