use of org.hyperledger.besu.plugin.data.EnodeURL in project besu by hyperledger.
the class LocalPermissioningConfigurationValidatorTest method nodeAllowlistCheckShouldWorkWithHostnameIfDnsEnabled.
@Test
public void nodeAllowlistCheckShouldWorkWithHostnameIfDnsEnabled() throws Exception {
final URL configFile = this.getClass().getResource(PERMISSIONING_CONFIG_VALID_HOSTNAME);
final Path toml = Files.createTempFile("toml", "");
toml.toFile().deleteOnExit();
Files.write(toml, Resources.toByteArray(configFile));
final ImmutableEnodeDnsConfiguration enodeDnsConfiguration = ImmutableEnodeDnsConfiguration.builder().dnsEnabled(true).updateEnabled(false).build();
final LocalPermissioningConfiguration permissioningConfiguration = PermissioningConfigurationBuilder.permissioningConfiguration(true, enodeDnsConfiguration, toml.toAbsolutePath().toString(), true, toml.toAbsolutePath().toString());
// This node is defined in the PERMISSIONING_CONFIG_DNS file without the discovery port
final EnodeURL enodeURL = EnodeURLImpl.fromString("enode://6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@localhost:4567?discport=30303", enodeDnsConfiguration);
// In an URI comparison the URLs should not match
boolean isInAllowlist = permissioningConfiguration.getNodeAllowlist().contains(enodeURL);
assertThat(isInAllowlist).isFalse();
// error
try {
PermissioningConfigurationValidator.areAllNodesAreInAllowlist(Lists.newArrayList(enodeURL), permissioningConfiguration);
} catch (Exception e) {
fail("Exception not expected. Validation of nodes in allowlist should ignore the optional discovery port param.");
}
}
use of org.hyperledger.besu.plugin.data.EnodeURL in project besu by hyperledger.
the class NetworkingServiceLifecycleTest method createP2PNetwork.
@Test
public void createP2PNetwork() throws IOException {
final NetworkingConfiguration config = configWithRandomPorts();
try (final P2PNetwork service = builder().build()) {
service.start();
final EnodeURL enode = service.getLocalEnode().get();
final int udpPort = enode.getDiscoveryPortOrZero();
final int tcpPort = enode.getListeningPortOrZero();
assertThat(enode.getIpAsString()).isEqualTo(config.getDiscovery().getAdvertisedHost());
assertThat(udpPort).isNotZero();
assertThat(tcpPort).isNotZero();
assertThat(service.streamDiscoveredPeers()).hasSize(0);
}
}
use of org.hyperledger.besu.plugin.data.EnodeURL in project besu by hyperledger.
the class P2PNetworkTest method rejectIncomingConnectionFromDenylistedPeer.
@Test
public void rejectIncomingConnectionFromDenylistedPeer() throws Exception {
final PeerPermissionsDenylist localDenylist = PeerPermissionsDenylist.create();
try (final P2PNetwork localNetwork = builder().peerPermissions(localDenylist).build();
final P2PNetwork remoteNetwork = builder().build()) {
localNetwork.start();
remoteNetwork.start();
final EnodeURL localEnode = localNetwork.getLocalEnode().get();
final Bytes localId = localEnode.getNodeId();
final int localPort = localEnode.getListeningPort().get();
final EnodeURL remoteEnode = remoteNetwork.getLocalEnode().get();
final Bytes remoteId = remoteEnode.getNodeId();
final int remotePort = remoteEnode.getListeningPort().get();
final Peer localPeer = createPeer(localId, localPort);
final Peer remotePeer = createPeer(remoteId, remotePort);
// Denylist the remote peer
localDenylist.add(remotePeer);
// 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
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 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.plugin.data.EnodeURL 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);
}
}
Aggregations