Search in sources :

Example 1 with FetchBlockResult

use of tech.pegasys.teku.beacon.sync.gossip.FetchBlockTask.FetchBlockResult in project teku by ConsenSys.

the class FetchRecentBlocksServiceTest method handleDuplicateRequiredBlocks.

@Test
public void handleDuplicateRequiredBlocks() {
    final Bytes32 root = dataStructureUtil.randomBytes32();
    recentBlockFetcher.requestRecentBlock(root);
    recentBlockFetcher.requestRecentBlock(root);
    assertTaskCounts(1, 1, 0);
    assertThat(importedBlocks).isEmpty();
    final SafeFuture<FetchBlockResult> future = taskFutures.get(0);
    final SignedBeaconBlock block = dataStructureUtil.randomSignedBeaconBlock(1);
    future.complete(FetchBlockResult.createSuccessful(block));
    assertThat(importedBlocks).containsExactly(block);
    assertTaskCounts(0, 0, 0);
}
Also used : FetchBlockResult(tech.pegasys.teku.beacon.sync.gossip.FetchBlockTask.FetchBlockResult) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) Bytes32(org.apache.tuweni.bytes.Bytes32) Test(org.junit.jupiter.api.Test)

Example 2 with FetchBlockResult

use of tech.pegasys.teku.beacon.sync.gossip.FetchBlockTask.FetchBlockResult in project teku by ConsenSys.

the class FetchBlockTaskTest method run_failAndRetryWithNoNewPeers.

@Test
public void run_failAndRetryWithNoNewPeers() {
    final SignedBeaconBlock block = dataStructureUtil.randomSignedBeaconBlock(10);
    final Bytes32 blockRoot = block.getMessage().hashTreeRoot();
    FetchBlockTask task = FetchBlockTask.create(eth2P2PNetwork, blockRoot);
    final Eth2Peer peer = registerNewPeer(1);
    when(peer.requestBlockByRoot(blockRoot)).thenReturn(SafeFuture.failedFuture(new RuntimeException("whoops")));
    final SafeFuture<FetchBlockResult> result = task.run();
    assertThat(result).isDone();
    final FetchBlockResult fetchBlockResult = result.getNow(null);
    assertThat(fetchBlockResult.isSuccessful()).isFalse();
    assertThat(fetchBlockResult.getStatus()).isEqualTo(Status.FETCH_FAILED);
    assertThat(task.getNumberOfRetries()).isEqualTo(0);
    // Retry
    final SafeFuture<FetchBlockResult> result2 = task.run();
    assertThat(result).isDone();
    final FetchBlockResult fetchBlockResult2 = result2.getNow(null);
    assertThat(fetchBlockResult2.isSuccessful()).isFalse();
    assertThat(fetchBlockResult2.getStatus()).isEqualTo(Status.NO_AVAILABLE_PEERS);
    assertThat(task.getNumberOfRetries()).isEqualTo(0);
}
Also used : Eth2Peer(tech.pegasys.teku.networking.eth2.peers.Eth2Peer) FetchBlockResult(tech.pegasys.teku.beacon.sync.gossip.FetchBlockTask.FetchBlockResult) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) Bytes32(org.apache.tuweni.bytes.Bytes32) Test(org.junit.jupiter.api.Test)

Example 3 with FetchBlockResult

use of tech.pegasys.teku.beacon.sync.gossip.FetchBlockTask.FetchBlockResult in project teku by ConsenSys.

the class FetchBlockTaskTest method run_successful.

@Test
public void run_successful() {
    final SignedBeaconBlock block = dataStructureUtil.randomSignedBeaconBlock(10);
    final Bytes32 blockRoot = block.getMessage().hashTreeRoot();
    FetchBlockTask task = FetchBlockTask.create(eth2P2PNetwork, blockRoot);
    assertThat(task.getBlockRoot()).isEqualTo(blockRoot);
    final Eth2Peer peer = registerNewPeer(1);
    when(peer.requestBlockByRoot(blockRoot)).thenReturn(SafeFuture.completedFuture(Optional.of(block)));
    final SafeFuture<FetchBlockResult> result = task.run();
    assertThat(result).isDone();
    final FetchBlockResult fetchBlockResult = result.getNow(null);
    assertThat(fetchBlockResult.isSuccessful()).isTrue();
    assertThat(fetchBlockResult.getBlock()).isEqualTo(block);
}
Also used : Eth2Peer(tech.pegasys.teku.networking.eth2.peers.Eth2Peer) FetchBlockResult(tech.pegasys.teku.beacon.sync.gossip.FetchBlockTask.FetchBlockResult) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) Bytes32(org.apache.tuweni.bytes.Bytes32) Test(org.junit.jupiter.api.Test)

Example 4 with FetchBlockResult

use of tech.pegasys.teku.beacon.sync.gossip.FetchBlockTask.FetchBlockResult in project teku by ConsenSys.

the class FetchBlockTaskTest method run_withMultiplesPeersAvailable.

@Test
public void run_withMultiplesPeersAvailable() {
    final SignedBeaconBlock block = dataStructureUtil.randomSignedBeaconBlock(10);
    final Bytes32 blockRoot = block.getMessage().hashTreeRoot();
    FetchBlockTask task = FetchBlockTask.create(eth2P2PNetwork, blockRoot);
    final Eth2Peer peer = registerNewPeer(1);
    when(peer.requestBlockByRoot(blockRoot)).thenReturn(SafeFuture.failedFuture(new RuntimeException("whoops")));
    when(peer.getOutstandingRequests()).thenReturn(1);
    // Add another peer
    final Eth2Peer peer2 = registerNewPeer(2);
    when(peer2.requestBlockByRoot(blockRoot)).thenReturn(SafeFuture.completedFuture(Optional.of(block)));
    when(peer2.getOutstandingRequests()).thenReturn(0);
    // We should choose the peer that is less busy, which successfully returns the block
    final SafeFuture<FetchBlockResult> result = task.run();
    assertThat(result).isDone();
    final FetchBlockResult fetchBlockResult = result.getNow(null);
    assertThat(fetchBlockResult.isSuccessful()).isTrue();
    assertThat(fetchBlockResult.getBlock()).isEqualTo(block);
}
Also used : Eth2Peer(tech.pegasys.teku.networking.eth2.peers.Eth2Peer) FetchBlockResult(tech.pegasys.teku.beacon.sync.gossip.FetchBlockTask.FetchBlockResult) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) Bytes32(org.apache.tuweni.bytes.Bytes32) Test(org.junit.jupiter.api.Test)

Example 5 with FetchBlockResult

use of tech.pegasys.teku.beacon.sync.gossip.FetchBlockTask.FetchBlockResult in project teku by ConsenSys.

the class FetchBlockTaskTest method run_failAndRetryWithNewPeer.

@Test
public void run_failAndRetryWithNewPeer() {
    final SignedBeaconBlock block = dataStructureUtil.randomSignedBeaconBlock(10);
    final Bytes32 blockRoot = block.getMessage().hashTreeRoot();
    FetchBlockTask task = FetchBlockTask.create(eth2P2PNetwork, blockRoot);
    final Eth2Peer peer = registerNewPeer(1);
    when(peer.requestBlockByRoot(blockRoot)).thenReturn(SafeFuture.failedFuture(new RuntimeException("whoops")));
    final SafeFuture<FetchBlockResult> result = task.run();
    assertThat(result).isDone();
    final FetchBlockResult fetchBlockResult = result.getNow(null);
    assertThat(fetchBlockResult.isSuccessful()).isFalse();
    assertThat(fetchBlockResult.getStatus()).isEqualTo(Status.FETCH_FAILED);
    assertThat(task.getNumberOfRetries()).isEqualTo(0);
    // Add another peer
    final Eth2Peer peer2 = registerNewPeer(2);
    when(peer2.requestBlockByRoot(blockRoot)).thenReturn(SafeFuture.completedFuture(Optional.of(block)));
    // Retry
    final SafeFuture<FetchBlockResult> result2 = task.run();
    assertThat(result).isDone();
    final FetchBlockResult fetchBlockResult2 = result2.getNow(null);
    assertThat(fetchBlockResult2.isSuccessful()).isTrue();
    assertThat(fetchBlockResult2.getBlock()).isEqualTo(block);
    assertThat(task.getNumberOfRetries()).isEqualTo(1);
}
Also used : Eth2Peer(tech.pegasys.teku.networking.eth2.peers.Eth2Peer) FetchBlockResult(tech.pegasys.teku.beacon.sync.gossip.FetchBlockTask.FetchBlockResult) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) Bytes32(org.apache.tuweni.bytes.Bytes32) Test(org.junit.jupiter.api.Test)

Aggregations

Bytes32 (org.apache.tuweni.bytes.Bytes32)13 FetchBlockResult (tech.pegasys.teku.beacon.sync.gossip.FetchBlockTask.FetchBlockResult)13 Test (org.junit.jupiter.api.Test)12 SignedBeaconBlock (tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock)9 Eth2Peer (tech.pegasys.teku.networking.eth2.peers.Eth2Peer)5 SafeFuture (tech.pegasys.teku.infrastructure.async.SafeFuture)1