use of org.hyperledger.besu.plugin.data.EnodeURL 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.plugin.data.EnodeURL in project besu by hyperledger.
the class P2PPlainNetworkTest method rejectIncomingConnectionFromDisallowedPeer.
@Test
public void rejectIncomingConnectionFromDisallowedPeer() throws Exception {
final PeerPermissions peerPermissions = mock(PeerPermissions.class);
when(peerPermissions.isPermitted(any(), any(), any())).thenReturn(true);
try (final P2PNetwork localNetwork = builder("partner1client1").peerPermissions(peerPermissions).build();
final P2PNetwork remoteNetwork = builder("partner2client1").build()) {
localNetwork.start();
remoteNetwork.start();
final EnodeURL localEnode = localNetwork.getLocalEnode().get();
final Peer localPeer = DefaultPeer.fromEnodeURL(localEnode);
final Peer remotePeer = DefaultPeer.fromEnodeURL(remoteNetwork.getLocalEnode().get());
// Deny incoming connection permissions for remotePeer
when(peerPermissions.isPermitted(eq(localPeer), eq(remotePeer), eq(PeerPermissions.Action.RLPX_ALLOW_NEW_INBOUND_CONNECTION))).thenReturn(false);
// Setup disconnect listener
final CompletableFuture<PeerConnection> peerFuture = new CompletableFuture<>();
final CompletableFuture<DisconnectReason> reasonFuture = new CompletableFuture<>();
remoteNetwork.subscribeDisconnect((peerConnection, reason, initiatedByPeer) -> {
peerFuture.complete(peerConnection);
reasonFuture.complete(reason);
});
// Remote connect to local
final CompletableFuture<PeerConnection> connectFuture = remoteNetwork.connect(localPeer);
// Check connection is made, and then a disconnect is registered at remote
final Bytes localId = localEnode.getNodeId();
Assertions.assertThat(connectFuture.get(5L, TimeUnit.SECONDS).getPeerInfo().getNodeId()).isEqualTo(localId);
Assertions.assertThat(peerFuture.get(5L, TimeUnit.SECONDS).getPeerInfo().getNodeId()).isEqualTo(localId);
assertThat(reasonFuture.get(5L, TimeUnit.SECONDS)).isEqualByComparingTo(DisconnectReason.UNKNOWN);
}
}
use of org.hyperledger.besu.plugin.data.EnodeURL 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.plugin.data.EnodeURL 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);
}
}
use of org.hyperledger.besu.plugin.data.EnodeURL in project besu by hyperledger.
the class P2PPlainNetworkTest 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("partner1client1").nodeKey(listenerNodeKey).supportedCapabilities(cap1).build();
final P2PNetwork connector = builder("partner2client1").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);
}
}
Aggregations