private PeerConnection mockPeerConnection(final BytesValue id) { final PeerInfo peerInfo = mock(PeerInfo.class); when(peerInfo.getNodeId()).thenReturn(id); final PeerConnection peerConnection = mock(PeerConnection.class); when(peerConnection.getPeer()).thenReturn(peerInfo); return peerConnection; }
private PeerConnection generatePeerConnection() { final BytesValue nodeId = BytesValue.of(nodeIdValue++); final PeerConnection peer = mock(PeerConnection.class); final PeerInfo peerInfo = mock(PeerInfo.class); when(peerInfo.getNodeId()).thenReturn(nodeId); when(peer.getPeer()).thenReturn(peerInfo); return peer; }
private boolean hasConnection(final TestNode node1, final TestNode node2) { for (final PeerConnection peer : node1.network.getPeers()) { if (node2.id().equals(peer.getPeer().getNodeId())) { return true; } } return false; }
/** Assert that there were no node disconnections reported from the P2P network */ public void assertNoNetworkDisconnections() { int errCnt = 0; final StringBuilder sb = new StringBuilder(); for (final TestNode node : nodes) { for (final Map.Entry<PeerConnection, DisconnectReason> entry : node.disconnections.entrySet()) { final PeerConnection peer = entry.getKey(); final String peerString = peer.getPeer().getNodeId() + "@" + peer.getRemoteAddress(); final String unsentTxMsg = "Node " + node.shortId() + " has received a disconnection from " + peerString + " for " + entry.getValue() + "\n"; sb.append(unsentTxMsg); errCnt++; } } final String header = "Nodes have received " + errCnt + " disconnections.\n"; assertThat(errCnt).describedAs(header + sb).isEqualTo(0); }
/** Logs the Peer connections for each node. */ public void logPeerConnections() { final List<String> connStr = new ArrayList<>(); for (final TestNode node : nodes) { for (final PeerConnection peer : node.network.getPeers()) { final String localString = node.shortId() + "@" + peer.getLocalAddress(); final String peerString = shortId(peer.getPeer().getNodeId()) + "@" + peer.getRemoteAddress(); connStr.add("Connection: " + localString + " to " + peerString); } } LOG.info("TestNodeList Connections:\n" + join(connStr, '\n')); }
private static void assertSedesWorks(final byte[] data) { final BytesValue input = BytesValue.wrap(data); final PeerInfo peerInfo = PeerInfo.readFrom(RLP.input(input)); assertThat(peerInfo.getClientId()).isNotBlank(); assertThat(peerInfo.getCapabilities()).isNotEmpty(); assertThat(peerInfo.getNodeId().extractArray().length).isEqualTo(64); assertThat(peerInfo.getPort()).isGreaterThanOrEqualTo(0); assertThat(peerInfo.getVersion()).isEqualTo(5); // Re-serialize and check that data matches final BytesValueRLPOutput out = new BytesValueRLPOutput(); peerInfo.writeTo(out); assertThat(out.encoded()).isEqualTo(input); } }
public void connectAndAssertAll() throws InterruptedException, ExecutionException, TimeoutException { for (int i = 0; i < nodes.size(); i++) { final TestNode source = nodes.get(i); for (int j = i + 1; j < nodes.size(); j++) { final TestNode destination = nodes.get(j); try { LOG.info("Attempting to connect source " + source.shortId() + " to dest " + destination); assertThat(source.connect(destination).get(30L, TimeUnit.SECONDS).getPeer().getNodeId()) .isEqualTo(destination.id()); // Wait for the destination node to finish bonding. Awaitility.await() .atMost(30, TimeUnit.SECONDS) .until(() -> hasConnection(destination, source)); LOG.info("Successfully connected " + source.shortId() + " to dest " + destination); } catch (final InterruptedException | ExecutionException | TimeoutException e) { final String msg = format( "Error connecting source node %s to destination node %s in time allotted.", source.shortId(), destination.shortId()); LOG.error(msg, e); throw e; } } } }
network1.subscribe(cap, messageFuture::complete); final Predicate<PeerConnection> isPeerOne = peerConnection -> peerConnection.getPeer().getNodeId().equals(one.getId()); final Predicate<PeerConnection> isPeerTwo = peerConnection -> peerConnection.getPeer().getNodeId().equals(two.getId()); Assertions.assertThat(network1.getPeers().stream().filter(isPeerTwo).findFirst()) .isNotPresent(); network2.subscribeConnect(peer1Future::complete); network1.connect(two).get(); Assertions.assertThat(peer1Future.get().getPeer().getNodeId()).isEqualTo(one.getId()); Assertions.assertThat(peer2Future.get().getPeer().getNodeId()).isEqualTo(two.getId()); Assertions.assertThat(network1.getPeers().stream().filter(isPeerTwo).findFirst()).isPresent(); final Optional<PeerConnection> optionalConnection = Assertions.assertThat(receivedMessageData.getData().compareTo(BytesValue.wrap(data))) .isEqualTo(0); Assertions.assertThat(receivedMessage.getConnection().getPeer().getNodeId()) .isEqualTo(two.getId()); Assertions.assertThat(receivedMessageData.getSize()).isEqualTo(size);
listenPort, OptionalInt.of(listenPort))); assertThat(connector1.connect(listeningPeer).get(30L, TimeUnit.SECONDS).getPeer().getNodeId()) .isEqualTo(listenId); }); connector2.run(); assertThat(connector2.connect(listeningPeer).get(30L, TimeUnit.SECONDS).getPeer().getNodeId()) .isEqualTo(listenId); assertThat(peerFuture.get(30L, TimeUnit.SECONDS).getPeer().getNodeId()).isEqualTo(listenId); assertThat(reasonFuture.get(30L, TimeUnit.SECONDS)) .isEqualByComparingTo(DisconnectReason.TOO_MANY_PEERS);
.get(30L, TimeUnit.SECONDS) .getPeer() .getNodeId()) .isEqualTo(listenId); final CompletableFuture<PeerConnection> secondConnectionFuture =
.get(30L, TimeUnit.SECONDS) .getPeer() .getNodeId()) .isEqualTo(listenId);