use of tech.pegasys.teku.networking.eth2.Eth2P2PNetwork 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.networking.eth2.Eth2P2PNetwork 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.networking.eth2.Eth2P2PNetwork in project teku by ConsenSys.
the class PeerStatusIntegrationTest method shouldExchangeStatusMessagesOnConnection.
@Test
public void shouldExchangeStatusMessagesOnConnection() throws Exception {
final StorageSystem system2 = createStorageSystem();
final RecentChainData recentChainData2 = system2.recentChainData();
assertThat(recentChainData1.getBestBlockRoot()).isEqualTo(recentChainData2.getBestBlockRoot());
final Eth2P2PNetwork network1 = networkFactory.builder().spec(spec).rpcEncoding(rpcEncoding).recentChainData(recentChainData1).startNetwork();
final Eth2P2PNetwork network2 = networkFactory.builder().spec(spec).rpcEncoding(rpcEncoding).recentChainData(recentChainData2).startNetwork();
waitFor(network1.connect(network1.createPeerAddress(network2.getNodeAddress())));
waitFor(() -> {
assertThat(network1.getPeerCount()).isEqualTo(1);
assertThat(network2.getPeerCount()).isEqualTo(1);
});
final Eth2Peer network2ViewOfPeer1 = network2.getPeer(network1.getNodeId()).orElseThrow();
assertStatusMatchesStorage(recentChainData1, network2ViewOfPeer1.getStatus());
final Eth2Peer network1ViewOfPeer2 = network1.getPeer(network2.getNodeId()).orElseThrow();
assertStatusMatchesStorage(recentChainData2, network1ViewOfPeer2.getStatus());
// When the finalized epoch is genesis we should use a zero finalized root (from the state)
// This differs from what recentChainData.getFinalizedCheckpoint will have at genesis
assertThat(network1ViewOfPeer2.getStatus().getFinalizedRoot()).isEqualTo(Bytes32.ZERO);
}
use of tech.pegasys.teku.networking.eth2.Eth2P2PNetwork in project teku by ConsenSys.
the class PeerStatusIntegrationTest method shouldUpdatePeerStatus.
@Test
public void shouldUpdatePeerStatus() throws Exception {
final Eth2P2PNetwork network1 = networkFactory.builder().spec(spec).rpcEncoding(rpcEncoding).recentChainData(recentChainData1).startNetwork();
final StorageSystem storageSystem2 = createStorageSystem();
final RecentChainData recentChainData2 = storageSystem2.recentChainData();
final Eth2P2PNetwork network2 = networkFactory.builder().spec(spec).rpcEncoding(rpcEncoding).recentChainData(recentChainData2).peer(network1).startNetwork();
final Eth2Peer network2ViewOfPeer1 = network2.getPeer(network1.getNodeId()).orElseThrow();
assertStatusMatchesStorage(recentChainData1, network2ViewOfPeer1.getStatus());
// Peer 1 advances
this.storageSystem.chainUpdater().advanceChain(10);
final PeerStatus updatedStatusData = waitFor(network2ViewOfPeer1.sendStatus());
assertStatusMatchesStorage(recentChainData1, updatedStatusData);
assertStatusMatchesStorage(recentChainData1, network2ViewOfPeer1.getStatus());
}
use of tech.pegasys.teku.networking.eth2.Eth2P2PNetwork in project teku by ConsenSys.
the class GossipMessageHandlerIntegrationTest method shouldNotGossipAttestationsWhenPeerDeregistersFromTopic.
@Test
public void shouldNotGossipAttestationsWhenPeerDeregistersFromTopic() throws Exception {
final GossipEncoding gossipEncoding = GossipEncoding.SSZ_SNAPPY;
List<ValidateableAttestation> node2attestations = new ArrayList<>();
Subscribers<ProcessedAttestationListener> processedAttestationSubscribers = Subscribers.create(false);
final Consumer<Eth2P2PNetworkBuilder> networkBuilder1 = b -> {
b.gossipEncoding(gossipEncoding);
b.gossipedAttestationProcessor((__) -> SafeFuture.completedFuture(InternalValidationResult.ACCEPT));
b.processedAttestationSubscriptionProvider(processedAttestationSubscribers::subscribe);
};
final Consumer<Eth2P2PNetworkBuilder> networkBuilder2 = b -> {
b.gossipEncoding(gossipEncoding);
b.gossipedAttestationProcessor((attestation) -> {
node2attestations.add(attestation);
return SafeFuture.completedFuture(InternalValidationResult.ACCEPT);
});
};
// Setup network 1
final NodeManager node1 = createNodeManager(networkBuilder1);
final Eth2P2PNetwork network1 = node1.network();
// Setup network 2
final NodeManager node2 = createNodeManager(networkBuilder2);
final Eth2P2PNetwork network2 = node2.network();
// Connect networks 1 -> 2
waitFor(node1.connect(node2));
// Wait for connections to get set up
Waiter.waitFor(() -> {
assertThat(network1.getPeerCount()).isEqualTo(1);
assertThat(network2.getPeerCount()).isEqualTo(1);
});
// Propagate attestation from network 1
AttestationGenerator attestationGenerator = new AttestationGenerator(spec, validatorKeys);
final StateAndBlockSummary bestBlockAndState = getChainHead(node1);
Attestation attestation = attestationGenerator.validAttestation(bestBlockAndState);
final int subnetId = spec.computeSubnetForAttestation(bestBlockAndState.getState(), attestation);
ValidateableAttestation validAttestation = ValidateableAttestation.fromNetwork(spec, attestation, subnetId);
node1.network().subscribeToAttestationSubnetId(subnetId);
node2.network().subscribeToAttestationSubnetId(subnetId);
waitForTopicRegistration();
processedAttestationSubscribers.forEach(s -> s.accept(validAttestation));
waitForMessageToBeDelivered();
assertThat(node2attestations.size()).isEqualTo(1);
assertThat(node2attestations.get(0)).isEqualTo(validAttestation);
node1.network().unsubscribeFromAttestationSubnetId(subnetId);
node2.network().unsubscribeFromAttestationSubnetId(subnetId);
waitForTopicDeregistration();
processedAttestationSubscribers.forEach(s -> s.accept(validAttestation));
ensureConditionRemainsMet(() -> {
assertThat(node2attestations.size()).isEqualTo(1);
assertThat(node2attestations.get(0)).isEqualTo(validAttestation);
});
}
Aggregations