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