use of tech.pegasys.teku.networking.eth2.peers.Eth2Peer in project teku by ConsenSys.
the class BeaconBlocksByRootIntegrationTest method requestBlockByRoot_shouldReturnEmptyWhenBlockIsNotKnown.
@Test
void requestBlockByRoot_shouldReturnEmptyWhenBlockIsNotKnown() throws Exception {
final Eth2Peer peer = createPeer();
final Optional<SignedBeaconBlock> result = waitFor(peer.requestBlockByRoot(Bytes32.fromHexStringLenient("0x123456789")));
assertThat(result).isEmpty();
}
use of tech.pegasys.teku.networking.eth2.peers.Eth2Peer in project teku by ConsenSys.
the class BeaconBlocksByRangeMessageHandler method onIncomingMessage.
@Override
public void onIncomingMessage(final String protocolId, final Eth2Peer peer, final BeaconBlocksByRangeRequestMessage message, final ResponseCallback<SignedBeaconBlock> callback) {
LOG.trace("Peer {} requested {} BeaconBlocks starting at slot {} with step {}", peer.getId(), message.getCount(), message.getStartSlot(), message.getStep());
if (message.getStep().compareTo(ONE) < 0) {
callback.completeWithErrorResponse(new RpcException(INVALID_REQUEST_CODE, "Step must be greater than zero"));
return;
}
if (message.getCount().compareTo(UInt64.valueOf(MAX_REQUEST_BLOCKS)) > 0) {
callback.completeWithErrorResponse(new RpcException(INVALID_REQUEST_CODE, "Only a maximum of " + MAX_REQUEST_BLOCKS + " blocks can be requested per request"));
return;
}
if (!peer.wantToMakeRequest() || !peer.wantToReceiveObjects(callback, maxRequestSize.min(message.getCount()).longValue())) {
return;
}
sendMatchingBlocks(message, callback).finish(callback::completeSuccessfully, error -> {
final Throwable rootCause = Throwables.getRootCause(error);
if (rootCause instanceof RpcException) {
// Keep full context
LOG.trace("Rejecting beacon blocks by range request", error);
callback.completeWithErrorResponse((RpcException) rootCause);
} else {
if (rootCause instanceof StreamClosedException || rootCause instanceof ClosedChannelException) {
LOG.trace("Stream closed while sending requested blocks", error);
} else {
LOG.error("Failed to process blocks by range request", error);
}
callback.completeWithUnexpectedError(error);
}
});
}
use of tech.pegasys.teku.networking.eth2.peers.Eth2Peer 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.networking.eth2.peers.Eth2Peer 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);
}
use of tech.pegasys.teku.networking.eth2.peers.Eth2Peer in project teku by ConsenSys.
the class FetchBlockTaskTest method cancel.
@Test
public void cancel() {
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.completedFuture(Optional.of(block)));
task.cancel();
final SafeFuture<FetchBlockResult> result = task.run();
assertThat(result).isDone();
final FetchBlockResult fetchBlockResult = result.getNow(null);
assertThat(fetchBlockResult.isSuccessful()).isFalse();
assertThat(fetchBlockResult.getStatus()).isEqualTo(Status.CANCELLED);
}
Aggregations