Search in sources :

Example 16 with EnodeURL

use of org.hyperledger.besu.plugin.data.EnodeURL in project besu by hyperledger.

the class AdminNodeInfoTest method handlesLocalEnodeWithListeningDisabled.

@Test
public void handlesLocalEnodeWithListeningDisabled() {
    final EnodeURL localEnode = EnodeURLImpl.builder().nodeId(nodeId).ipAddress("1.2.3.4").discoveryAndListeningPorts(0).discoveryPort(7890).build();
    when(p2pNetwork.isP2pEnabled()).thenReturn(true);
    when(p2pNetwork.getLocalEnode()).thenReturn(Optional.of(localEnode));
    final JsonRpcRequestContext request = adminNodeInfo();
    final Map<String, Object> expected = new HashMap<>();
    expected.put("enode", "enode://0f1b319e32017c3fcb221841f0f978701b4e9513fe6a567a2db43d43381a9c7e3dfe7cae13cbc2f56943400bacaf9082576ab087cd51983b17d729ae796f6807@1.2.3.4:0?discport=7890");
    expected.put("id", "0f1b319e32017c3fcb221841f0f978701b4e9513fe6a567a2db43d43381a9c7e3dfe7cae13cbc2f56943400bacaf9082576ab087cd51983b17d729ae796f6807");
    expected.put("ip", "1.2.3.4");
    expected.put("name", "testnet/1.0/this/that");
    expected.put("ports", ImmutableMap.of("discovery", 7890));
    expected.put("protocols", ImmutableMap.of("eth", ImmutableMap.of("config", genesisConfigOptions.asMap(), "difficulty", BigInteger.ONE, "genesis", Hash.EMPTY.toString(), "head", Hash.EMPTY.toString(), "network", BigInteger.valueOf(2018))));
    final JsonRpcResponse response = method.response(request);
    assertThat(response).isInstanceOf(JsonRpcSuccessResponse.class);
    final JsonRpcSuccessResponse actual = (JsonRpcSuccessResponse) response;
    assertThat(actual.getResult()).isEqualTo(expected);
}
Also used : EnodeURL(org.hyperledger.besu.plugin.data.EnodeURL) JsonRpcResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse) JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) HashMap(java.util.HashMap) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) Test(org.junit.Test)

Example 17 with EnodeURL

use of org.hyperledger.besu.plugin.data.EnodeURL in project besu by hyperledger.

the class BesuCommandTest method errorIsRaisedIfStaticNodesAreNotAllowed.

@Test
public void errorIsRaisedIfStaticNodesAreNotAllowed() throws IOException {
    final File staticNodesFile = testFolder.newFile("static-nodes.json");
    staticNodesFile.deleteOnExit();
    final File permissioningConfig = testFolder.newFile("permissioning");
    permissioningConfig.deleteOnExit();
    final EnodeURL staticNodeURI = EnodeURLImpl.builder().nodeId("50203c6bfca6874370e71aecc8958529fd723feb05013dc1abca8fc1fff845c5259faba05852e9dfe5ce172a7d6e7c2a3a5eaa8b541c8af15ea5518bbff5f2fa").ipAddress("127.0.0.1").useDefaultPorts().build();
    final EnodeURL allowedNode = EnodeURLImpl.builder().nodeId("50203c6bfca6874370e71aecc8958529fd723feb05013dc1abca8fc1fff845c5259faba05852e9dfe5ce172a7d6e7c2a3a5eaa8b541c8af15ea5518bbff5f2fa").useDefaultPorts().ipAddress("127.0.0.1").listeningPort(30304).build();
    Files.write(staticNodesFile.toPath(), ("[\"" + staticNodeURI.toString() + "\"]").getBytes(UTF_8));
    Files.write(permissioningConfig.toPath(), ("nodes-allowlist=[\"" + allowedNode.toString() + "\"]").getBytes(UTF_8));
    parseCommand("--data-path=" + testFolder.getRoot().getPath(), "--bootnodes", "--permissions-nodes-config-file-enabled=true", "--permissions-nodes-config-file=" + permissioningConfig.getPath());
    assertThat(commandErrorOutput.toString(UTF_8)).contains(staticNodeURI.toString(), "not in nodes-allowlist");
}
Also used : EnodeURL(org.hyperledger.besu.plugin.data.EnodeURL) GenesisConfigFile(org.hyperledger.besu.config.GenesisConfigFile) File(java.io.File) Test(org.junit.Test)

Example 18 with EnodeURL

use of org.hyperledger.besu.plugin.data.EnodeURL in project besu by hyperledger.

the class RunnerBuilderTest method enodeUrlShouldHaveAdvertisedHostWhenDiscoveryDisabled.

@Test
public void enodeUrlShouldHaveAdvertisedHostWhenDiscoveryDisabled() {
    final String p2pAdvertisedHost = "172.0.0.1";
    final int p2pListenPort = 30302;
    final Runner runner = new RunnerBuilder().p2pListenInterface("0.0.0.0").p2pListenPort(p2pListenPort).p2pAdvertisedHost(p2pAdvertisedHost).p2pEnabled(true).discovery(false).besuController(besuController).ethNetworkConfig(mock(EthNetworkConfig.class)).metricsSystem(mock(ObservableMetricsSystem.class)).jsonRpcConfiguration(mock(JsonRpcConfiguration.class)).permissioningService(mock(PermissioningServiceImpl.class)).graphQLConfiguration(mock(GraphQLConfiguration.class)).webSocketConfiguration(mock(WebSocketConfiguration.class)).jsonRpcIpcConfiguration(mock(JsonRpcIpcConfiguration.class)).metricsConfiguration(mock(MetricsConfiguration.class)).vertx(vertx).dataDir(dataDir.getRoot().toPath()).storageProvider(mock(KeyValueStorageProvider.class)).forkIdSupplier(() -> Collections.singletonList(Bytes.EMPTY)).rpcEndpointService(new RpcEndpointServiceImpl()).build();
    runner.startEthereumMainLoop();
    final EnodeURL expectedEodeURL = EnodeURLImpl.builder().ipAddress(p2pAdvertisedHost).discoveryPort(0).listeningPort(p2pListenPort).nodeId(besuController.getNodeKey().getPublicKey().getEncoded()).build();
    assertThat(runner.getLocalEnode().orElseThrow()).isEqualTo(expectedEodeURL);
}
Also used : EnodeURL(org.hyperledger.besu.plugin.data.EnodeURL) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) PermissioningServiceImpl(org.hyperledger.besu.services.PermissioningServiceImpl) RpcEndpointServiceImpl(org.hyperledger.besu.services.RpcEndpointServiceImpl) ObservableMetricsSystem(org.hyperledger.besu.metrics.ObservableMetricsSystem) MetricsConfiguration(org.hyperledger.besu.metrics.prometheus.MetricsConfiguration) WebSocketConfiguration(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.WebSocketConfiguration) Test(org.junit.Test)

Example 19 with EnodeURL

use of org.hyperledger.besu.plugin.data.EnodeURL in project besu by hyperledger.

the class LocalPermissioningConfigurationValidatorTest method nodeAllowlistCheckShouldIgnoreDiscoveryPortParam.

@Test
public void nodeAllowlistCheckShouldIgnoreDiscoveryPortParam() throws Exception {
    final URL configFile = this.getClass().getResource(PERMISSIONING_CONFIG);
    final Path toml = Files.createTempFile("toml", "");
    toml.toFile().deleteOnExit();
    Files.write(toml, Resources.toByteArray(configFile));
    final LocalPermissioningConfiguration permissioningConfiguration = PermissioningConfigurationBuilder.permissioningConfiguration(true, EnodeDnsConfiguration.DEFAULT_CONFIG, toml.toAbsolutePath().toString(), true, toml.toAbsolutePath().toString());
    // This node is defined in the PERMISSIONING_CONFIG file without the discovery port
    final EnodeURL enodeURL = EnodeURLImpl.fromString("enode://6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@192.168.0.9:4567?discport=30303");
    // 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) Test(org.junit.Test)

Example 20 with EnodeURL

use of org.hyperledger.besu.plugin.data.EnodeURL in project besu by hyperledger.

the class LocalPermissioningConfigurationValidatorTest method nodesAllowlistOptionWhichDoesNotIncludeBootnodesMustError.

@Test
public void nodesAllowlistOptionWhichDoesNotIncludeBootnodesMustError() throws Exception {
    EthNetworkConfig ethNetworkConfig = EthNetworkConfig.getNetworkConfig(NetworkName.ROPSTEN);
    final URL configFile = this.getClass().getResource(PERMISSIONING_CONFIG);
    final Path toml = Files.createTempFile("toml", "");
    toml.toFile().deleteOnExit();
    Files.write(toml, Resources.toByteArray(configFile));
    LocalPermissioningConfiguration permissioningConfiguration = PermissioningConfigurationBuilder.permissioningConfiguration(true, EnodeDnsConfiguration.DEFAULT_CONFIG, toml.toAbsolutePath().toString(), true, toml.toAbsolutePath().toString());
    try {
        final List<EnodeURL> enodeURIs = ethNetworkConfig.getBootNodes();
        PermissioningConfigurationValidator.areAllNodesAreInAllowlist(enodeURIs, permissioningConfiguration);
        fail("expected exception because ropsten bootnodes are not in node-allowlist");
    } catch (Exception e) {
        assertThat(e.getMessage()).startsWith("Specified node(s) not in nodes-allowlist");
        assertThat(e.getMessage()).contains("enode://6332792c4a00e3e4ee0926ed89e0d27ef985424d97b6a45bf0f23e51f0dcb5e66b875777506458aea7af6f9e4ffb69f43f3778ee73c81ed9d34c51c4b16b0b0f@52.232.243.152:30303");
        assertThat(e.getMessage()).contains("enode://94c15d1b9e2fe7ce56e458b9a3b672ef11894ddedd0c6f247e0f1d3487f52b66208fb4aeb8179fce6e3a749ea93ed147c37976d67af557508d199d9594c35f09@192.81.208.223:30303");
    }
}
Also used : Path(java.nio.file.Path) EnodeURL(org.hyperledger.besu.plugin.data.EnodeURL) LocalPermissioningConfiguration(org.hyperledger.besu.ethereum.permissioning.LocalPermissioningConfiguration) EthNetworkConfig(org.hyperledger.besu.cli.config.EthNetworkConfig) URL(java.net.URL) EnodeURL(org.hyperledger.besu.plugin.data.EnodeURL) 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