Search in sources :

Example 21 with EnodeURL

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.");
    }
}
Also used : Path(java.nio.file.Path) EnodeURL(org.hyperledger.besu.plugin.data.EnodeURL) LocalPermissioningConfiguration(org.hyperledger.besu.ethereum.permissioning.LocalPermissioningConfiguration) URL(java.net.URL) EnodeURL(org.hyperledger.besu.plugin.data.EnodeURL) ImmutableEnodeDnsConfiguration(org.hyperledger.besu.ethereum.p2p.peers.ImmutableEnodeDnsConfiguration) Test(org.junit.Test)

Example 22 with EnodeURL

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);
    }
}
Also used : EnodeURL(org.hyperledger.besu.plugin.data.EnodeURL) NetworkingConfiguration(org.hyperledger.besu.ethereum.p2p.config.NetworkingConfiguration) Test(org.junit.Test)

Example 23 with EnodeURL

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);
    }
}
Also used : EnodeURL(org.hyperledger.besu.plugin.data.EnodeURL) Bytes(org.apache.tuweni.bytes.Bytes) PeerPermissionsDenylist(org.hyperledger.besu.ethereum.p2p.permissions.PeerPermissionsDenylist) CompletableFuture(java.util.concurrent.CompletableFuture) PeerConnection(org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection) DisconnectReason(org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.DisconnectMessage.DisconnectReason) Peer(org.hyperledger.besu.ethereum.p2p.peers.Peer) DefaultPeer(org.hyperledger.besu.ethereum.p2p.peers.DefaultPeer) Test(org.junit.Test)

Example 24 with EnodeURL

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();
    }
}
Also used : EnodeURL(org.hyperledger.besu.plugin.data.EnodeURL) Bytes(org.apache.tuweni.bytes.Bytes) PeerConnection(org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection) NodeKey(org.hyperledger.besu.crypto.NodeKey) Test(org.junit.Test)

Example 25 with EnodeURL

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);
    }
}
Also used : EnodeURL(org.hyperledger.besu.plugin.data.EnodeURL) Bytes(org.apache.tuweni.bytes.Bytes) Capability(org.hyperledger.besu.ethereum.p2p.rlpx.wire.Capability) PeerConnection(org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection) SubProtocol(org.hyperledger.besu.ethereum.p2p.rlpx.wire.SubProtocol) Peer(org.hyperledger.besu.ethereum.p2p.peers.Peer) DefaultPeer(org.hyperledger.besu.ethereum.p2p.peers.DefaultPeer) NodeKey(org.hyperledger.besu.crypto.NodeKey) Test(org.junit.Test)

Aggregations

EnodeURL (org.hyperledger.besu.plugin.data.EnodeURL)109 Test (org.junit.Test)83 Bytes (org.apache.tuweni.bytes.Bytes)26 PeerConnection (org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection)22 DefaultPeer (org.hyperledger.besu.ethereum.p2p.peers.DefaultPeer)17 Peer (org.hyperledger.besu.ethereum.p2p.peers.Peer)17 NodeKey (org.hyperledger.besu.crypto.NodeKey)14 Path (java.nio.file.Path)12 CompletableFuture (java.util.concurrent.CompletableFuture)12 Optional (java.util.Optional)11 List (java.util.List)10 Collectors (java.util.stream.Collectors)10 URL (java.net.URL)9 EnodeURLImpl (org.hyperledger.besu.ethereum.p2p.peers.EnodeURLImpl)9 DisconnectReason (org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.DisconnectMessage.DisconnectReason)9 File (java.io.File)8 Collections (java.util.Collections)8 LocalPermissioningConfiguration (org.hyperledger.besu.ethereum.permissioning.LocalPermissioningConfiguration)8 ArrayList (java.util.ArrayList)7 EthNetworkConfig (org.hyperledger.besu.cli.config.EthNetworkConfig)7