use of org.hyperledger.besu.crypto.NodeKey in project besu by hyperledger.
the class P2PNetworkTest method preventMultipleConnections.
@Test
public void preventMultipleConnections() throws Exception {
final NodeKey listenNodeKey = NodeKeyUtils.generate();
try (final P2PNetwork listener = builder().nodeKey(listenNodeKey).build();
final P2PNetwork connector = builder().build()) {
listener.start();
connector.start();
final EnodeURL listenerEnode = listener.getLocalEnode().get();
final Bytes listenId = listenerEnode.getNodeId();
final int listenPort = listenerEnode.getListeningPort().get();
final CompletableFuture<PeerConnection> firstFuture = connector.connect(createPeer(listenId, listenPort));
final CompletableFuture<PeerConnection> secondFuture = connector.connect(createPeer(listenId, listenPort));
final PeerConnection firstConnection = firstFuture.get(30L, TimeUnit.SECONDS);
final PeerConnection secondConnection = secondFuture.get(30L, TimeUnit.SECONDS);
Assertions.assertThat(firstConnection.getPeerInfo().getNodeId()).isEqualTo(listenId);
// Connections should reference the same instance - i.e. we shouldn't create 2 distinct
// connections
assertThat(firstConnection == secondConnection).isTrue();
}
}
use of org.hyperledger.besu.crypto.NodeKey in project besu by hyperledger.
the class P2PNetworkTest method rejectPeerWithNoSharedCaps.
@Test
public void rejectPeerWithNoSharedCaps() throws Exception {
final NodeKey listenerNodeKey = NodeKeyUtils.generate();
final NodeKey connectorNodeKey = NodeKeyUtils.generate();
final SubProtocol subprotocol1 = subProtocol("eth");
final Capability cap1 = Capability.create(subprotocol1.getName(), 63);
final SubProtocol subprotocol2 = subProtocol("oth");
final Capability cap2 = Capability.create(subprotocol2.getName(), 63);
try (final P2PNetwork listener = builder().nodeKey(listenerNodeKey).supportedCapabilities(cap1).build();
final P2PNetwork connector = builder().nodeKey(connectorNodeKey).supportedCapabilities(cap2).build()) {
listener.start();
connector.start();
final EnodeURL listenerEnode = listener.getLocalEnode().get();
final Bytes listenId = listenerEnode.getNodeId();
final int listenPort = listenerEnode.getListeningPort().get();
final Peer listenerPeer = createPeer(listenId, listenPort);
final CompletableFuture<PeerConnection> connectFuture = connector.connect(listenerPeer);
assertThatThrownBy(connectFuture::get).hasCauseInstanceOf(IncompatiblePeerException.class);
}
}
use of org.hyperledger.besu.crypto.NodeKey in project besu by hyperledger.
the class P2PNetworkTest method limitMaxPeers.
/**
* Tests that max peers setting is honoured and inbound connections that would exceed the limit
* are correctly disconnected.
*
* @throws Exception On Failure
*/
@Test
public void limitMaxPeers() throws Exception {
final NodeKey nodeKey = NodeKeyUtils.generate();
final int maxPeers = 1;
final NetworkingConfiguration listenerConfig = NetworkingConfiguration.create().setDiscovery(DiscoveryConfiguration.create().setActive(false)).setRlpx(RlpxConfiguration.create().setBindPort(0).setMaxPeers(maxPeers).setSupportedProtocols(subProtocol()));
try (final P2PNetwork listener = builder().nodeKey(nodeKey).config(listenerConfig).build();
final P2PNetwork connector1 = builder().build();
final P2PNetwork connector2 = builder().build()) {
// Setup listener and first connection
listener.start();
connector1.start();
final EnodeURL listenerEnode = listener.getLocalEnode().get();
final Bytes listenId = listenerEnode.getNodeId();
final int listenPort = listenerEnode.getListeningPort().get();
final Peer listeningPeer = createPeer(listenId, listenPort);
Assertions.assertThat(connector1.connect(listeningPeer).get(30L, TimeUnit.SECONDS).getPeerInfo().getNodeId()).isEqualTo(listenId);
// Setup second connection and check that connection is not accepted
final CompletableFuture<PeerConnection> peerFuture = new CompletableFuture<>();
final CompletableFuture<DisconnectReason> reasonFuture = new CompletableFuture<>();
connector2.subscribeDisconnect((peerConnection, reason, initiatedByPeer) -> {
peerFuture.complete(peerConnection);
reasonFuture.complete(reason);
});
connector2.start();
Assertions.assertThat(connector2.connect(listeningPeer).get(30L, TimeUnit.SECONDS).getPeerInfo().getNodeId()).isEqualTo(listenId);
Assertions.assertThat(peerFuture.get(30L, TimeUnit.SECONDS).getPeerInfo().getNodeId()).isEqualTo(listenId);
assertThat(reasonFuture.get(30L, TimeUnit.SECONDS)).isEqualByComparingTo(DisconnectReason.TOO_MANY_PEERS);
}
}
use of org.hyperledger.besu.crypto.NodeKey in project besu by hyperledger.
the class P2PPlainNetworkTest method p2pOverTlsCanHandleRecordFragmentation.
@Test
public void p2pOverTlsCanHandleRecordFragmentation() throws Exception {
// Given
final int tlsRecordSize = 16 * 1024;
final int threeTlsRecords = 2 * tlsRecordSize + 1;
final LargeMessageData largeMessageData = new LargeMessageData(Bytes.of(buildPaddedMessage(threeTlsRecords)));
final NodeKey nodeKey = NodeKeyUtils.generate();
try (final P2PNetwork listener = builder("partner1client1").nodeKey(nodeKey).build();
final P2PNetwork connector = builder("partner2client1").build()) {
final CompletableFuture<DisconnectReason> disconnectReasonFuture = new CompletableFuture<>();
listener.subscribeDisconnect((peerConnection, reason, initiatedByPeer) -> {
if (!DisconnectReason.CLIENT_QUITTING.equals(reason)) {
// client quitting is the valid end state
disconnectReasonFuture.complete(reason);
}
});
final CompletableFuture<Message> successfulMessageFuture = new CompletableFuture<>();
listener.subscribe(Capability.create("eth", 63), (capability, message) -> {
if (message.getData().getCode() == LargeMessageData.VALID_ETH_MESSAGE_CODE) {
successfulMessageFuture.complete(message);
}
});
listener.start();
connector.start();
final EnodeURL listenerEnode = listener.getLocalEnode().get();
final Bytes listenId = listenerEnode.getNodeId();
final int listenPort = listenerEnode.getListeningPort().get();
final PeerConnection peerConnection = connector.connect(createPeer(listenId, listenPort)).get(30000L, TimeUnit.SECONDS);
// When
peerConnection.sendForProtocol("eth", largeMessageData);
// Then
CompletableFuture.anyOf(disconnectReasonFuture, successfulMessageFuture).thenAccept(successOrFailure -> {
if (successOrFailure instanceof DisconnectReason) {
fail("listener disconnected due to " + ((DisconnectReason) successOrFailure).name());
} else {
final Message receivedMessage = (Message) successOrFailure;
assertThat(receivedMessage.getData().getData()).isEqualTo(largeMessageData.getData());
}
}).get(30L, TimeUnit.SECONDS);
}
}
use of org.hyperledger.besu.crypto.NodeKey in project besu by hyperledger.
the class P2PPlainNetworkTest method handshaking.
@Test
public void handshaking() throws Exception {
final NodeKey nodeKey = NodeKeyUtils.generate();
try (final P2PNetwork listener = builder("partner1client1").nodeKey(nodeKey).build();
final P2PNetwork connector = builder("partner2client1").build()) {
listener.start();
connector.start();
final EnodeURL listenerEnode = listener.getLocalEnode().get();
final Bytes listenId = listenerEnode.getNodeId();
final int listenPort = listenerEnode.getListeningPort().get();
Assertions.assertThat(connector.connect(createPeer(listenId, listenPort)).get(30L, TimeUnit.SECONDS).getPeerInfo().getNodeId()).isEqualTo(listenId);
}
}
Aggregations