Search in sources :

Example 1 with INITIAL

use of tech.pegasys.teku.networking.eth2.rpc.core.Eth2OutgoingRequestHandler.State.INITIAL 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

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