Search in sources :

Example 1 with StubSyncSource

use of tech.pegasys.teku.networking.eth2.peers.StubSyncSource in project teku by ConsenSys.

the class SyncSourceBatchTest method createBatch.

protected Batch createBatch(final long startSlot, final long count) {
    final List<StubSyncSource> syncSources = new ArrayList<>();
    final SyncSourceSelector syncSourceProvider = () -> {
        final StubSyncSource source = new StubSyncSource();
        syncSources.add(source);
        return Optional.of(source);
    };
    final SyncSourceBatch batch = new SyncSourceBatch(eventThread, syncSourceProvider, conflictResolutionStrategy, targetChain, UInt64.valueOf(startSlot), UInt64.valueOf(count));
    this.syncSources.put(batch, syncSources);
    return batch;
}
Also used : ArrayList(java.util.ArrayList) StubSyncSource(tech.pegasys.teku.networking.eth2.peers.StubSyncSource)

Example 2 with StubSyncSource

use of tech.pegasys.teku.networking.eth2.peers.StubSyncSource in project teku by ConsenSys.

the class SyncSourceBatchTest method requestMoreBlocks_shouldResetAndSelectNewPeerAfterDisconnection.

@Test
void requestMoreBlocks_shouldResetAndSelectNewPeerAfterDisconnection() {
    final Runnable callback = mock(Runnable.class);
    final Batch batch = createBatch(70, 50);
    batch.requestMoreBlocks(callback);
    // First request returns some data, so the batch isn't in initial state
    final StubSyncSource firstSyncSource = getSyncSource(batch);
    firstSyncSource.receiveBlocks(dataStructureUtil.randomSignedBeaconBlock(72));
    verify(callback).run();
    batch.markFirstBlockConfirmed();
    batch.markAsContested();
    // Second request should go to the same source
    batch.requestMoreBlocks(callback);
    firstSyncSource.assertRequestedBlocks(73, 47);
    assertThatBatch(batch).isNotEmpty();
    // But this requests fails
    firstSyncSource.failRequest(new PeerDisconnectedException());
    // The request is complete, so should call the callback
    verify(callback, times(2)).run();
    // And the batch should be back in initial state
    assertThatBatch(batch).isEmpty();
    assertThatBatch(batch).isNotContested();
    assertThatBatch(batch).hasUnconfirmedFirstBlock();
    // Third request selects a new peer to request data from
    batch.requestMoreBlocks(callback);
    assertThat(syncSources.get(batch)).hasSize(2);
    final StubSyncSource secondSyncSource = getSyncSource(batch);
    assertThat(secondSyncSource).isNotSameAs(firstSyncSource);
    secondSyncSource.assertRequestedBlocks(70, 50);
}
Also used : BatchAssert.assertThatBatch(tech.pegasys.teku.beacon.sync.forward.multipeer.batches.BatchAssert.assertThatBatch) PeerDisconnectedException(tech.pegasys.teku.networking.p2p.peer.PeerDisconnectedException) StubSyncSource(tech.pegasys.teku.networking.eth2.peers.StubSyncSource) Test(org.junit.jupiter.api.Test)

Aggregations

StubSyncSource (tech.pegasys.teku.networking.eth2.peers.StubSyncSource)2 ArrayList (java.util.ArrayList)1 Test (org.junit.jupiter.api.Test)1 BatchAssert.assertThatBatch (tech.pegasys.teku.beacon.sync.forward.multipeer.batches.BatchAssert.assertThatBatch)1 PeerDisconnectedException (tech.pegasys.teku.networking.p2p.peer.PeerDisconnectedException)1