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);
}
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;
}
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);
}
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());
}
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);
}
}
Aggregations