From 987098cbdd9d045083f1e2b6ba2363033ba4f67d Mon Sep 17 00:00:00 2001 From: Gabriel-Trintinalia Date: Tue, 4 Jun 2024 09:00:08 +1000 Subject: [PATCH 1/7] Give p2p its own configuration option Signed-off-by: Gabriel-Trintinalia --- .../tests/acceptance/dsl/node/BesuNode.java | 40 +--- .../dsl/node/ThreadBesuNodeRunner.java | 5 +- .../configuration/BesuNodeConfiguration.java | 25 +-- .../BesuNodeConfigurationBuilder.java | 28 +-- .../acceptance/dsl/privacy/PrivacyNode.java | 4 +- .../org/hyperledger/besu/RunnerBuilder.java | 97 ++------ .../org/hyperledger/besu/cli/BesuCommand.java | 211 +++--------------- .../besu/cli/options/stable/P2POptions.java | 180 +++++++++++++++ .../besu/cli/CommandTestAbstract.java | 7 +- .../p2p/config/AutoDiscoverDefaultIP.java | 33 +++ .../ethereum/p2p/config/P2PConfiguration.java | 205 +++++++++++++++++ 11 files changed, 501 insertions(+), 334 deletions(-) create mode 100644 besu/src/main/java/org/hyperledger/besu/cli/options/stable/P2POptions.java create mode 100644 ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/config/AutoDiscoverDefaultIP.java create mode 100644 ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/config/P2PConfiguration.java diff --git a/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/BesuNode.java b/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/BesuNode.java index 5f30941f232..89bf6215485 100644 --- a/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/BesuNode.java +++ b/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/BesuNode.java @@ -31,6 +31,7 @@ import org.hyperledger.besu.ethereum.core.Util; import org.hyperledger.besu.ethereum.eth.transactions.TransactionPoolConfiguration; import org.hyperledger.besu.ethereum.p2p.config.NetworkingConfiguration; +import org.hyperledger.besu.ethereum.p2p.config.P2PConfiguration; import org.hyperledger.besu.ethereum.p2p.rlpx.connections.netty.TLSConfiguration; import org.hyperledger.besu.ethereum.permissioning.PermissioningConfiguration; import org.hyperledger.besu.ethereum.worldstate.DataStorageConfiguration; @@ -94,8 +95,8 @@ public class BesuNode implements NodeConfiguration, RunnableNode, AutoCloseable private final Path homeDirectory; private KeyPair keyPair; private final Properties portsProperties = new Properties(); - private final Boolean p2pEnabled; - private final int p2pPort; + + private final P2PConfiguration p2PConfiguration; private final Optional tlsConfiguration; private final NetworkingConfiguration networkingConfiguration; private final boolean revertReasonEnabled; @@ -116,7 +117,6 @@ public class BesuNode implements NodeConfiguration, RunnableNode, AutoCloseable private final GenesisConfigurationProvider genesisConfigProvider; private final boolean devMode; private final NetworkName network; - private final boolean discoveryEnabled; private final List bootnodes = new ArrayList<>(); private final boolean bootnodeEligible; private final boolean secp256k1Native; @@ -151,11 +151,9 @@ public BesuNode( final boolean devMode, final NetworkName network, final GenesisConfigurationProvider genesisConfigProvider, - final boolean p2pEnabled, - final int p2pPort, + final P2PConfiguration p2PConfiguration, final Optional tlsConfiguration, final NetworkingConfiguration networkingConfiguration, - final boolean discoveryEnabled, final boolean bootnodeEligible, final boolean revertReasonEnabled, final boolean secp256k1Native, @@ -200,11 +198,9 @@ public BesuNode( this.genesisConfigProvider = genesisConfigProvider; this.devMode = devMode; this.network = network; - this.p2pEnabled = p2pEnabled; - this.p2pPort = p2pPort; + this.p2PConfiguration = p2PConfiguration; this.tlsConfiguration = tlsConfiguration; this.networkingConfiguration = networkingConfiguration; - this.discoveryEnabled = discoveryEnabled; this.bootnodeEligible = bootnodeEligible; this.revertReasonEnabled = revertReasonEnabled; this.secp256k1Native = secp256k1Native; @@ -289,10 +285,6 @@ public URI enodeUrl() { "enode://" + getNodeId() + "@" + LOCALHOST + ":" + getRuntimeP2pPort() + discport); } - public String getP2pPort() { - return String.valueOf(p2pPort); - } - private String getRuntimeP2pPort() { final String port = portsProperties.getProperty("p2p"); if (port == null) { @@ -301,6 +293,10 @@ private String getRuntimeP2pPort() { return port; } + public P2PConfiguration getP2PConfiguration() { + return p2PConfiguration; + } + private String getDiscoveryPort() { final String port = portsProperties.getProperty("discovery"); if (port == null) { @@ -390,11 +386,6 @@ public Optional getEngineJsonRpcPort() { } } - @Override - public String getHostName() { - return LOCALHOST; - } - public NodeRequests nodeRequests() { Optional websocketService = Optional.empty(); if (nodeRequests == null) { @@ -645,10 +636,6 @@ public List getBootnodes() { return unmodifiableList(bootnodes); } - @Override - public boolean isP2pEnabled() { - return p2pEnabled; - } public Optional getTLSConfiguration() { return tlsConfiguration; @@ -714,11 +701,6 @@ public boolean isAltbn128Native() { return altbn128Native; } - @Override - public boolean isDiscoveryEnabled() { - return discoveryEnabled; - } - Optional getPermissioningConfiguration() { return permissioningConfiguration; } @@ -769,8 +751,8 @@ public String toString() { .add("name", name) .add("homeDirectory", homeDirectory) .add("keyPair", keyPair) - .add("p2pEnabled", p2pEnabled) - .add("discoveryEnabled", discoveryEnabled) + .add("p2pEnabled", p2PConfiguration.isP2pEnabled()) + .add("discoveryEnabled", p2PConfiguration.isPeerDiscoveryEnabled()) .add("privacyEnabled", privacyParameters.isEnabled()) .toString(); } diff --git a/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/ThreadBesuNodeRunner.java b/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/ThreadBesuNodeRunner.java index 0448d324e64..7f7cf64e922 100644 --- a/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/ThreadBesuNodeRunner.java +++ b/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/ThreadBesuNodeRunner.java @@ -276,9 +276,7 @@ public void startNode(final BesuNode node) { .vertx(Vertx.vertx()) .besuController(besuController) .ethNetworkConfig(ethNetworkConfig) - .discovery(node.isDiscoveryEnabled()) - .p2pAdvertisedHost(node.getHostName()) - .p2pListenPort(0) + .p2pConfiguration(node.getP2PConfiguration()) .networkingConfiguration(node.getNetworkingConfiguration()) .jsonRpcConfiguration(node.jsonRpcConfiguration()) .webSocketConfiguration(node.webSocketConfiguration()) @@ -287,7 +285,6 @@ public void startNode(final BesuNode node) { .metricsSystem(metricsSystem) .permissioningService(permissioningService) .metricsConfiguration(node.getMetricsConfiguration()) - .p2pEnabled(node.isP2pEnabled()) .p2pTLSConfiguration(node.getTLSConfiguration()) .graphQLConfiguration(GraphQLConfiguration.createDefault()) .staticNodes( diff --git a/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/configuration/BesuNodeConfiguration.java b/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/configuration/BesuNodeConfiguration.java index d1e398f7215..63ff5e99f78 100644 --- a/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/configuration/BesuNodeConfiguration.java +++ b/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/configuration/BesuNodeConfiguration.java @@ -24,6 +24,7 @@ import org.hyperledger.besu.ethereum.core.PrivacyParameters; import org.hyperledger.besu.ethereum.eth.transactions.TransactionPoolConfiguration; import org.hyperledger.besu.ethereum.p2p.config.NetworkingConfiguration; +import org.hyperledger.besu.ethereum.p2p.config.P2PConfiguration; import org.hyperledger.besu.ethereum.p2p.rlpx.connections.netty.TLSConfiguration; import org.hyperledger.besu.ethereum.permissioning.PermissioningConfiguration; import org.hyperledger.besu.ethereum.worldstate.DataStorageConfiguration; @@ -53,11 +54,9 @@ public class BesuNodeConfiguration { private final Optional keyFilePath; private final boolean devMode; private final GenesisConfigurationProvider genesisConfigProvider; - private final boolean p2pEnabled; - private final int p2pPort; + private P2PConfiguration p2pConfiguration; private final Optional tlsConfiguration; private final NetworkingConfiguration networkingConfiguration; - private final boolean discoveryEnabled; private final boolean bootnodeEligible; private final boolean revertReasonEnabled; private final boolean secp256k1Native; @@ -91,11 +90,9 @@ public class BesuNodeConfiguration { final boolean devMode, final NetworkName network, final GenesisConfigurationProvider genesisConfigProvider, - final boolean p2pEnabled, - final int p2pPort, + final P2PConfiguration p2pConfiguration, final Optional tlsConfiguration, final NetworkingConfiguration networkingConfiguration, - final boolean discoveryEnabled, final boolean bootnodeEligible, final boolean revertReasonEnabled, final boolean secp256k1Native, @@ -126,11 +123,9 @@ public class BesuNodeConfiguration { this.devMode = devMode; this.network = network; this.genesisConfigProvider = genesisConfigProvider; - this.p2pEnabled = p2pEnabled; - this.p2pPort = p2pPort; + this.p2pConfiguration = p2pConfiguration; this.tlsConfiguration = tlsConfiguration; this.networkingConfiguration = networkingConfiguration; - this.discoveryEnabled = discoveryEnabled; this.bootnodeEligible = bootnodeEligible; this.revertReasonEnabled = revertReasonEnabled; this.secp256k1Native = secp256k1Native; @@ -203,20 +198,12 @@ public boolean isDevMode() { return devMode; } - public boolean isDiscoveryEnabled() { - return discoveryEnabled; - } - public GenesisConfigurationProvider getGenesisConfigProvider() { return genesisConfigProvider; } - public boolean isP2pEnabled() { - return p2pEnabled; - } - - public int getP2pPort() { - return p2pPort; + public P2PConfiguration getP2PConfiguration() { + return p2pConfiguration; } public Optional getTLSConfiguration() { diff --git a/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/configuration/BesuNodeConfigurationBuilder.java b/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/configuration/BesuNodeConfigurationBuilder.java index 1a9a16f36f5..7f91ba6f654 100644 --- a/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/configuration/BesuNodeConfigurationBuilder.java +++ b/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/configuration/BesuNodeConfigurationBuilder.java @@ -37,6 +37,7 @@ import org.hyperledger.besu.ethereum.core.PrivacyParameters; import org.hyperledger.besu.ethereum.eth.transactions.TransactionPoolConfiguration; import org.hyperledger.besu.ethereum.p2p.config.NetworkingConfiguration; +import org.hyperledger.besu.ethereum.p2p.config.P2PConfiguration; import org.hyperledger.besu.ethereum.p2p.rlpx.connections.netty.TLSConfiguration; import org.hyperledger.besu.ethereum.permissioning.PermissioningConfiguration; import org.hyperledger.besu.ethereum.worldstate.DataStorageConfiguration; @@ -67,6 +68,7 @@ public class BesuNodeConfigurationBuilder { .build(); private TransactionPoolConfiguration transactionPoolConfiguration = TransactionPoolConfiguration.DEFAULT; + private P2PConfiguration p2pConfiguration = new P2PConfiguration.Builder().build(); private JsonRpcConfiguration jsonRpcConfiguration = JsonRpcConfiguration.createDefault(); private JsonRpcConfiguration engineRpcConfiguration = JsonRpcConfiguration.createEngineDefault(); private WebSocketConfiguration webSocketConfiguration = WebSocketConfiguration.createDefault(); @@ -79,11 +81,8 @@ public class BesuNodeConfigurationBuilder { private String keyFilePath = null; private boolean devMode = true; private GenesisConfigurationProvider genesisConfigProvider = ignore -> Optional.empty(); - private Boolean p2pEnabled = true; - private int p2pPort = 0; private Optional tlsConfiguration = Optional.empty(); private final NetworkingConfiguration networkingConfiguration = NetworkingConfiguration.create(); - private boolean discoveryEnabled = true; private boolean bootnodeEligible = true; private boolean revertReasonEnabled = false; private NetworkName network = null; @@ -248,6 +247,12 @@ public BesuNodeConfigurationBuilder jsonRpcAuthenticationUsingECDSA() throws URI return this; } + public BesuNodeConfigurationBuilder p2pConfiguration( + final P2PConfiguration p2pConfiguration) { + this.p2pConfiguration = p2pConfiguration; + return this; + } + public BesuNodeConfigurationBuilder webSocketConfiguration( final WebSocketConfiguration webSocketConfiguration) { this.webSocketConfiguration = webSocketConfiguration; @@ -361,15 +366,6 @@ public BesuNodeConfigurationBuilder genesisConfigProvider( return this; } - public BesuNodeConfigurationBuilder p2pEnabled(final Boolean p2pEnabled) { - this.p2pEnabled = p2pEnabled; - return this; - } - - public BesuNodeConfigurationBuilder p2pPort(final int p2pPort) { - this.p2pPort = p2pPort; - return this; - } private static Path toPath(final String path) throws Exception { return Path.of(BesuNodeConfigurationBuilder.class.getResource(path).toURI()); @@ -436,10 +432,6 @@ public BesuNodeConfigurationBuilder pkiBlockCreationEnabled( return this; } - public BesuNodeConfigurationBuilder discoveryEnabled(final boolean discoveryEnabled) { - this.discoveryEnabled = discoveryEnabled; - return this; - } public BesuNodeConfigurationBuilder plugins(final List plugins) { this.plugins.clear(); @@ -533,11 +525,9 @@ public BesuNodeConfiguration build() { devMode, network, genesisConfigProvider, - p2pEnabled, - p2pPort, + p2pConfiguration, tlsConfiguration, networkingConfiguration, - discoveryEnabled, bootnodeEligible, revertReasonEnabled, secp256K1Native, diff --git a/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/privacy/PrivacyNode.java b/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/privacy/PrivacyNode.java index 32a5a0fbf4a..f55d8f90f60 100644 --- a/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/privacy/PrivacyNode.java +++ b/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/privacy/PrivacyNode.java @@ -114,11 +114,9 @@ public PrivacyNode( besuConfig.isDevMode(), besuConfig.getNetwork(), besuConfig.getGenesisConfigProvider(), - besuConfig.isP2pEnabled(), - besuConfig.getP2pPort(), + besuConfig.getP2PConfiguration(), besuConfig.getTLSConfiguration(), besuConfig.getNetworkingConfiguration(), - besuConfig.isDiscoveryEnabled(), besuConfig.isBootnodeEligible(), besuConfig.isRevertReasonEnabled(), besuConfig.isSecp256k1Native(), diff --git a/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java b/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java index eb734ba606a..305e697d2ee 100644 --- a/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java +++ b/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java @@ -78,6 +78,7 @@ import org.hyperledger.besu.ethereum.mainnet.precompiles.privacy.FlexiblePrivacyPrecompiledContract; import org.hyperledger.besu.ethereum.p2p.config.DiscoveryConfiguration; import org.hyperledger.besu.ethereum.p2p.config.NetworkingConfiguration; +import org.hyperledger.besu.ethereum.p2p.config.P2PConfiguration; import org.hyperledger.besu.ethereum.p2p.config.RlpxConfiguration; import org.hyperledger.besu.ethereum.p2p.config.SubProtocolConfiguration; import org.hyperledger.besu.ethereum.p2p.network.DefaultP2PNetwork; @@ -124,7 +125,6 @@ import org.hyperledger.besu.services.BesuPluginContextImpl; import org.hyperledger.besu.services.PermissioningServiceImpl; import org.hyperledger.besu.services.RpcEndpointServiceImpl; -import org.hyperledger.besu.util.NetworkUtility; import java.io.IOException; import java.nio.file.Path; @@ -160,13 +160,8 @@ public class RunnerBuilder { private BesuController besuController; private NetworkingConfiguration networkingConfiguration = NetworkingConfiguration.create(); - private final Collection bannedNodeIds = new ArrayList<>(); - private boolean p2pEnabled = true; + private P2PConfiguration p2pConfiguration; private Optional p2pTLSConfiguration = Optional.empty(); - private boolean discovery; - private String p2pAdvertisedHost; - private String p2pListenInterface = NetworkUtility.INADDR_ANY; - private int p2pListenPort; private NatMethod natMethod = NatMethod.AUTO; private String natManagerServiceName; private boolean natMethodFallbackEnabled; @@ -219,13 +214,16 @@ public RunnerBuilder besuController(final BesuController besuController) { } /** - * P2p enabled. + * TLSConfiguration p2pTLSConfiguration. * - * @param p2pEnabled the p 2 p enabled + * @param p2pConfiguration the TLSConfiguration p2pTLSConfiguration * @return the runner builder */ - public RunnerBuilder p2pEnabled(final boolean p2pEnabled) { - this.p2pEnabled = p2pEnabled; + public RunnerBuilder p2pConfiguration(final P2PConfiguration p2pConfiguration) { + checkArgument( + !isNull(p2pConfiguration.getP2pInterface()), + "Invalid null value supplied for p2pListenInterface"); + this.p2pConfiguration = p2pConfiguration; return this; } @@ -253,17 +251,6 @@ public RunnerBuilder p2pTLSConfiguration(final Optional p2pTLS return this; } - /** - * Enable Discovery. - * - * @param discovery the discovery - * @return the runner builder - */ - public RunnerBuilder discovery(final boolean discovery) { - this.discovery = discovery; - return this; - } - /** * Add Eth network config. * @@ -287,40 +274,6 @@ public RunnerBuilder networkingConfiguration( return this; } - /** - * Add P2p advertised host. - * - * @param p2pAdvertisedHost the P2P advertised host - * @return the runner builder - */ - public RunnerBuilder p2pAdvertisedHost(final String p2pAdvertisedHost) { - this.p2pAdvertisedHost = p2pAdvertisedHost; - return this; - } - - /** - * Add P2P Listener interface ip/host name. - * - * @param ip the ip - * @return the runner builder - */ - public RunnerBuilder p2pListenInterface(final String ip) { - checkArgument(!isNull(ip), "Invalid null value supplied for p2pListenInterface"); - this.p2pListenInterface = ip; - return this; - } - - /** - * Add P2P listen port. - * - * @param p2pListenPort the p 2 p listen port - * @return the runner builder - */ - public RunnerBuilder p2pListenPort(final int p2pListenPort) { - this.p2pListenPort = p2pListenPort; - return this; - } - /** * Add Nat method. * @@ -455,17 +408,6 @@ public RunnerBuilder dataDir(final Path dataDir) { return this; } - /** - * Add list of Banned node id. - * - * @param bannedNodeIds the banned node ids - * @return the runner builder - */ - public RunnerBuilder bannedNodeIds(final Collection bannedNodeIds) { - this.bannedNodeIds.addAll(bannedNodeIds); - return this; - } - /** * Add Metrics configuration. * @@ -600,10 +542,10 @@ public Runner build() { final DiscoveryConfiguration discoveryConfiguration = DiscoveryConfiguration.create() - .setBindHost(p2pListenInterface) - .setBindPort(p2pListenPort) - .setAdvertisedHost(p2pAdvertisedHost); - if (discovery) { + .setBindHost(p2pConfiguration.getP2pInterface()) + .setBindPort(p2pConfiguration.getP2pPort()) + .setAdvertisedHost(p2pConfiguration.getP2pHost()); + if (p2pConfiguration.isPeerDiscoveryEnabled()) { final List bootstrap; if (ethNetworkConfig.bootNodes() == null) { bootstrap = EthNetworkConfig.getNetworkConfig(NetworkName.MAINNET).bootNodes(); @@ -639,14 +581,14 @@ public Runner build() { final RlpxConfiguration rlpxConfiguration = RlpxConfiguration.create() - .setBindHost(p2pListenInterface) - .setBindPort(p2pListenPort) + .setBindHost(p2pConfiguration.getP2pInterface()) + .setBindPort(p2pConfiguration.getP2pPort()) .setSupportedProtocols(subProtocols) .setClientId(BesuInfo.nodeName(identityString)); networkingConfiguration.setRlpx(rlpxConfiguration).setDiscovery(discoveryConfiguration); final PeerPermissionsDenylist bannedNodes = PeerPermissionsDenylist.create(); - bannedNodeIds.forEach(bannedNodes::add); + p2pConfiguration.getBannedNodeIds().forEach(bannedNodes::add); final List bootnodes = discoveryConfiguration.getBootnodes(); @@ -701,7 +643,7 @@ public Runner build() { NetworkRunner.builder() .protocolManagers(protocolManagers) .subProtocols(subProtocols) - .network(p2pEnabled ? activeNetwork : inactiveNetwork) + .network(p2pConfiguration.isP2pEnabled() ? activeNetwork : inactiveNetwork) .metricsSystem(metricsSystem) .build(); @@ -1155,7 +1097,10 @@ private Optional buildNatManager(final NatMethod natMethod) { return Optional.of(new UpnpNatManager()); case DOCKER: return Optional.of( - new DockerNatManager(p2pAdvertisedHost, p2pListenPort, jsonRpcConfiguration.getPort())); + new DockerNatManager( + p2pConfiguration.getP2pHost(), + p2pConfiguration.getP2pPort(), + jsonRpcConfiguration.getPort())); case KUBERNETES: return Optional.of(new KubernetesNatManager(natManagerServiceName)); case NONE: diff --git a/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java b/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java index bd47630fad6..75c4cbda54c 100644 --- a/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java +++ b/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java @@ -26,6 +26,7 @@ import static org.hyperledger.besu.controller.BesuController.DATABASE_PATH; import static org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcConfiguration.DEFAULT_ENGINE_JSON_RPC_PORT; import static org.hyperledger.besu.ethereum.api.jsonrpc.authentication.EngineAuthService.EPHEMERAL_JWT_FILE; +import static org.hyperledger.besu.ethereum.p2p.config.AutoDiscoverDefaultIP.autoDiscoverDefaultIP; import static org.hyperledger.besu.metrics.BesuMetricCategory.DEFAULT_METRIC_CATEGORIES; import static org.hyperledger.besu.metrics.MetricsProtocol.PROMETHEUS; import static org.hyperledger.besu.metrics.prometheus.MetricsConfiguration.DEFAULT_METRICS_PORT; @@ -42,7 +43,6 @@ import org.hyperledger.besu.cli.config.NetworkName; import org.hyperledger.besu.cli.config.ProfileName; import org.hyperledger.besu.cli.converter.MetricCategoryConverter; -import org.hyperledger.besu.cli.converter.PercentageConverter; import org.hyperledger.besu.cli.custom.JsonRPCAllowlistHostsProperty; import org.hyperledger.besu.cli.error.BesuExecutionExceptionHandler; import org.hyperledger.besu.cli.error.BesuParameterExceptionHandler; @@ -55,6 +55,7 @@ import org.hyperledger.besu.cli.options.stable.JsonRpcHttpOptions; import org.hyperledger.besu.cli.options.stable.LoggingLevelOption; import org.hyperledger.besu.cli.options.stable.NodePrivateKeyFileOption; +import org.hyperledger.besu.cli.options.stable.P2POptions; import org.hyperledger.besu.cli.options.stable.P2PTLSConfigOptions; import org.hyperledger.besu.cli.options.stable.PermissionsOptions; import org.hyperledger.besu.cli.options.stable.PluginsConfigurationOptions; @@ -128,6 +129,7 @@ import org.hyperledger.besu.ethereum.eth.transactions.TransactionPoolConfiguration; import org.hyperledger.besu.ethereum.mainnet.FrontierTargetingGasLimitCalculator; import org.hyperledger.besu.ethereum.p2p.config.DiscoveryConfiguration; +import org.hyperledger.besu.ethereum.p2p.config.P2PConfiguration; import org.hyperledger.besu.ethereum.p2p.peers.EnodeDnsConfiguration; import org.hyperledger.besu.ethereum.p2p.peers.EnodeURLImpl; import org.hyperledger.besu.ethereum.p2p.peers.StaticNodesParser; @@ -211,7 +213,6 @@ import java.io.IOException; import java.io.InputStream; import java.math.BigInteger; -import java.net.InetAddress; import java.net.SocketException; import java.net.URI; import java.net.URL; @@ -387,148 +388,13 @@ public class BesuCommand implements DefaultCommandValues, Runnable { // P2P Discovery Option Group @CommandLine.ArgGroup(validate = false, heading = "@|bold P2P Discovery Options|@%n") - P2PDiscoveryOptionGroup p2PDiscoveryOptionGroup = new P2PDiscoveryOptionGroup(); + P2POptions p2PDiscoveryOptionGroup = new P2POptions(); private final TransactionSelectionServiceImpl transactionSelectionServiceImpl; private final TransactionPoolValidatorServiceImpl transactionValidatorServiceImpl; private final TransactionSimulationServiceImpl transactionSimulationServiceImpl; private final BlockchainServiceImpl blockchainServiceImpl; - static class P2PDiscoveryOptionGroup { - - // Public IP stored to prevent having to research it each time we need it. - private InetAddress autoDiscoveredDefaultIP = null; - - // Completely disables P2P within Besu. - @Option( - names = {"--p2p-enabled"}, - description = "Enable P2P functionality (default: ${DEFAULT-VALUE})", - arity = "1") - private final Boolean p2pEnabled = true; - - // Boolean option to indicate if peers should NOT be discovered, default to - // false indicates that - // the peers should be discovered by default. - // - // This negative option is required because of the nature of the option that is - // true when - // added on the command line. You can't do --option=false, so false is set as - // default - // and you have not to set the option at all if you want it false. - // This seems to be the only way it works with Picocli. - // Also many other software use the same negative option scheme for false - // defaults - // meaning that it's probably the right way to handle disabling options. - @Option( - names = {"--discovery-enabled"}, - description = "Enable P2P discovery (default: ${DEFAULT-VALUE})", - arity = "1") - private final Boolean peerDiscoveryEnabled = true; - - // A list of bootstrap nodes can be passed - // and a hardcoded list will be used otherwise by the Runner. - // NOTE: we have no control over default value here. - @Option( - names = {"--bootnodes"}, - paramLabel = "", - description = - "Comma separated enode URLs for P2P discovery bootstrap. " - + "Default is a predefined list.", - split = ",", - arity = "0..*") - private final List bootNodes = null; - - @SuppressWarnings({"FieldCanBeFinal", "FieldMayBeFinal"}) // PicoCLI requires non-final Strings. - @Option( - names = {"--p2p-host"}, - paramLabel = MANDATORY_HOST_FORMAT_HELP, - description = "IP address this node advertises to its peers (default: ${DEFAULT-VALUE})", - arity = "1") - private String p2pHost = autoDiscoverDefaultIP().getHostAddress(); - - @SuppressWarnings({"FieldCanBeFinal", "FieldMayBeFinal"}) // PicoCLI requires non-final Strings. - @Option( - names = {"--p2p-interface"}, - paramLabel = MANDATORY_HOST_FORMAT_HELP, - description = - "The network interface address on which this node listens for P2P communication (default: ${DEFAULT-VALUE})", - arity = "1") - private String p2pInterface = NetworkUtility.INADDR_ANY; - - @Option( - names = {"--p2p-port"}, - paramLabel = MANDATORY_PORT_FORMAT_HELP, - description = "Port on which to listen for P2P communication (default: ${DEFAULT-VALUE})", - arity = "1") - private final Integer p2pPort = EnodeURLImpl.DEFAULT_LISTENING_PORT; - - @Option( - names = {"--max-peers", "--p2p-peer-upper-bound"}, - paramLabel = MANDATORY_INTEGER_FORMAT_HELP, - description = "Maximum P2P connections that can be established (default: ${DEFAULT-VALUE})") - private final Integer maxPeers = DEFAULT_MAX_PEERS; - - @Option( - names = {"--remote-connections-limit-enabled"}, - description = - "Whether to limit the number of P2P connections initiated remotely. (default: ${DEFAULT-VALUE})") - private final Boolean isLimitRemoteWireConnectionsEnabled = true; - - @Option( - names = {"--remote-connections-max-percentage"}, - paramLabel = MANDATORY_DOUBLE_FORMAT_HELP, - description = - "The maximum percentage of P2P connections that can be initiated remotely. Must be between 0 and 100 inclusive. (default: ${DEFAULT-VALUE})", - arity = "1", - converter = PercentageConverter.class) - private final Percentage maxRemoteConnectionsPercentage = - Fraction.fromFloat(DEFAULT_FRACTION_REMOTE_WIRE_CONNECTIONS_ALLOWED).toPercentage(); - - @SuppressWarnings({"FieldCanBeFinal", "FieldMayBeFinal"}) // PicoCLI requires non-final Strings. - @CommandLine.Option( - names = {"--discovery-dns-url"}, - description = "Specifies the URL to use for DNS discovery") - private String discoveryDnsUrl = null; - - @Option( - names = {"--random-peer-priority-enabled"}, - description = - "Allow for incoming connections to be prioritized randomly. This will prevent (typically small, stable) networks from forming impenetrable peer cliques. (default: ${DEFAULT-VALUE})") - private final Boolean randomPeerPriority = Boolean.FALSE; - - @Option( - names = {"--banned-node-ids", "--banned-node-id"}, - paramLabel = MANDATORY_NODE_ID_FORMAT_HELP, - description = "A list of node IDs to ban from the P2P network.", - split = ",", - arity = "1..*") - void setBannedNodeIds(final List values) { - try { - bannedNodeIds = - values.stream() - .filter(value -> !value.isEmpty()) - .map(EnodeURLImpl::parseNodeId) - .collect(Collectors.toList()); - } catch (final IllegalArgumentException e) { - throw new ParameterException( - new CommandLine(this), - "Invalid ids supplied to '--banned-node-ids'. " + e.getMessage()); - } - } - - private Collection bannedNodeIds = new ArrayList<>(); - - // Used to discover the default IP of the client. - // Loopback IP is used by default as this is how smokeTests require it to be - // and it's probably a good security behaviour to default only on the localhost. - private InetAddress autoDiscoverDefaultIP() { - autoDiscoveredDefaultIP = - Optional.ofNullable(autoDiscoveredDefaultIP).orElseGet(InetAddress::getLoopbackAddress); - - return autoDiscoveredDefaultIP; - } - } - @Option( names = {"--sync-mode"}, paramLabel = MANDATORY_MODE_FORMAT_HELP, @@ -895,6 +761,9 @@ static class MetricsOptionGroup { PluginsConfigurationOptions pluginsConfigurationOptions = new PluginsConfigurationOptions(); private EthNetworkConfig ethNetworkConfig; + + private P2PConfiguration p2pConfiguration; + private JsonRpcConfiguration jsonRpcConfiguration; private JsonRpcConfiguration engineJsonRpcConfiguration; private GraphQLConfiguration graphQLConfiguration; @@ -1319,13 +1188,9 @@ private void preSynchronization() { private Runner buildRunner() { return synchronize( besuController, - p2PDiscoveryOptionGroup.p2pEnabled, + p2pConfiguration, p2pTLSConfiguration, - p2PDiscoveryOptionGroup.peerDiscoveryEnabled, ethNetworkConfig, - p2PDiscoveryOptionGroup.p2pHost, - p2PDiscoveryOptionGroup.p2pInterface, - p2PDiscoveryOptionGroup.p2pPort, graphQLConfiguration, jsonRpcConfiguration, engineJsonRpcConfiguration, @@ -1520,7 +1385,7 @@ private void configureNativeLibs() { private void validateOptions() { validateRequiredOptions(); issueOptionWarnings(); - validateP2PInterface(p2PDiscoveryOptionGroup.p2pInterface); + validateP2PInterface(p2pConfiguration.getP2pInterface()); validateMiningParams(); validateNatParams(); validateNetStatsParams(); @@ -1644,16 +1509,15 @@ private void validateDnsOptionsParams() { } private void ensureValidPeerBoundParams() { - maxPeers = p2PDiscoveryOptionGroup.maxPeers; + maxPeers = p2pConfiguration.getMaxPeers(); final Boolean isLimitRemoteWireConnectionsEnabled = - p2PDiscoveryOptionGroup.isLimitRemoteWireConnectionsEnabled; + p2pConfiguration.isLimitRemoteWireConnectionsEnabled(); if (isOptionSet(commandLine, PEER_LOWER_BOUND_FLAG)) { logger.warn(PEER_LOWER_BOUND_FLAG + " is deprecated and will be removed soon."); } if (isLimitRemoteWireConnectionsEnabled) { final float fraction = - Fraction.fromPercentage(p2PDiscoveryOptionGroup.maxRemoteConnectionsPercentage) - .getValue(); + Fraction.fromPercentage(p2pConfiguration.getMaxRemoteConnectionsPercentage()).getValue(); checkState( fraction >= 0.0 && fraction <= 1.0, "Fraction of remote connections allowed must be between 0.0 and 1.0 (inclusive)."); @@ -1715,7 +1579,7 @@ private void issueOptionWarnings() { logger, commandLine, "--p2p-enabled", - !p2PDiscoveryOptionGroup.p2pEnabled, + !p2pConfiguration.isP2pEnabled(), asList( "--bootnodes", "--discovery-enabled", @@ -1761,12 +1625,12 @@ private void configure() throws Exception { syncMode = getDefaultSyncModeIfNotSet(); versionCompatibilityProtection = getDefaultVersionCompatibilityProtectionIfNotSet(); + p2pConfiguration = p2PDiscoveryOptionGroup.toDomainObject(); ethNetworkConfig = updateNetworkConfig(network); - jsonRpcConfiguration = jsonRpcHttpOptions.jsonRpcConfiguration( hostsAllowlist, - p2PDiscoveryOptionGroup.autoDiscoverDefaultIP().getHostAddress(), + autoDiscoverDefaultIP().getHostAddress(), unstableRPCOptions.getHttpTimeoutSec()); if (isEngineApiEnabled()) { engineJsonRpcConfiguration = @@ -1777,12 +1641,12 @@ private void configure() throws Exception { graphQLConfiguration = graphQlOptions.graphQLConfiguration( hostsAllowlist, - p2PDiscoveryOptionGroup.autoDiscoverDefaultIP().getHostAddress(), + autoDiscoverDefaultIP().getHostAddress(), unstableRPCOptions.getHttpTimeoutSec()); webSocketConfiguration = rpcWebsocketOptions.webSocketConfiguration( hostsAllowlist, - p2PDiscoveryOptionGroup.autoDiscoverDefaultIP().getHostAddress(), + autoDiscoverDefaultIP().getHostAddress(), unstableRPCOptions.getWsTimeoutSec()); jsonRpcIpcConfiguration = jsonRpcIpcConfiguration( @@ -1903,9 +1767,9 @@ public BesuControllerBuilder getControllerBuilder() { .requiredBlocks(requiredBlocks) .reorgLoggingThreshold(reorgLoggingThreshold) .evmConfiguration(unstableEvmOptions.toDomainObject()) - .maxPeers(p2PDiscoveryOptionGroup.maxPeers) + .maxPeers(p2pConfiguration.getMaxPeers()) .maxRemotelyInitiatedPeers(maxRemoteInitiatedPeers) - .randomPeerPriority(p2PDiscoveryOptionGroup.randomPeerPriority) + .randomPeerPriority(p2pConfiguration.isRandomPeerPriority()) .chainPruningConfiguration(unstableChainPruningOptions.toDomainObject()) .cacheLastBlocks(numberOfblocksToCache) .genesisStateHashCacheEnabled(genesisStateHashCacheEnabled); @@ -1917,7 +1781,7 @@ private JsonRpcConfiguration createEngineJsonRpcConfiguration( final JsonRpcConfiguration engineConfig = jsonRpcHttpOptions.jsonRpcConfiguration( allowCallsFrom, - p2PDiscoveryOptionGroup.autoDiscoverDefaultIP().getHostAddress(), + autoDiscoverDefaultIP().getHostAddress(), unstableRPCOptions.getWsTimeoutSec()); engineConfig.setPort(engineListenPort); engineConfig.setRpcApis(Arrays.asList("ENGINE", "ETH")); @@ -1984,7 +1848,7 @@ public MetricsConfiguration metricsConfiguration() { .enabled(metricsOptionGroup.isMetricsEnabled) .host( Strings.isNullOrEmpty(metricsOptionGroup.metricsHost) - ? p2PDiscoveryOptionGroup.autoDiscoverDefaultIP().getHostAddress() + ? autoDiscoverDefaultIP().getHostAddress() : metricsOptionGroup.metricsHost) .port(metricsOptionGroup.metricsPort) .protocol(metricsOptionGroup.metricsProtocol) @@ -1992,7 +1856,7 @@ public MetricsConfiguration metricsConfiguration() { .pushEnabled(metricsOptionGroup.isMetricsPushEnabled) .pushHost( Strings.isNullOrEmpty(metricsOptionGroup.metricsPushHost) - ? p2PDiscoveryOptionGroup.autoDiscoverDefaultIP().getHostAddress() + ? autoDiscoverDefaultIP().getHostAddress() : metricsOptionGroup.metricsPushHost) .pushPort(metricsOptionGroup.metricsPushPort) .pushInterval(metricsOptionGroup.metricsPushInterval) @@ -2260,13 +2124,9 @@ private OptionalInt getGenesisBlockPeriodSeconds( // Blockchain synchronization from peers. private Runner synchronize( final BesuController controller, - final boolean p2pEnabled, + final P2PConfiguration p2pConfiguration, final Optional p2pTLSConfiguration, - final boolean peerDiscoveryEnabled, final EthNetworkConfig ethNetworkConfig, - final String p2pAdvertisedHost, - final String p2pListenInterface, - final int p2pListenPort, final GraphQLConfiguration graphQLConfiguration, final JsonRpcConfiguration jsonRpcConfiguration, final JsonRpcConfiguration engineJsonRpcConfiguration, @@ -2287,16 +2147,12 @@ private Runner synchronize( runnerBuilder .vertx(vertx) .besuController(controller) - .p2pEnabled(p2pEnabled) + .p2pConfiguration(p2pConfiguration) .natMethod(natMethod) .natManagerServiceName(unstableNatOptions.getNatManagerServiceName()) .natMethodFallbackEnabled(unstableNatOptions.getNatMethodFallbackEnabled()) - .discovery(peerDiscoveryEnabled) .ethNetworkConfig(ethNetworkConfig) .permissioningConfiguration(permissioningConfiguration) - .p2pAdvertisedHost(p2pAdvertisedHost) - .p2pListenInterface(p2pListenInterface) - .p2pListenPort(p2pListenPort) .networkingConfiguration(unstableNetworkingOptions.toDomainObject()) .legacyForkId(unstableEthProtocolOptions.toDomainObject().isLegacyEth64ForkIdEnabled()) .graphQLConfiguration(graphQLConfiguration) @@ -2307,7 +2163,6 @@ private Runner synchronize( .apiConfiguration(apiConfiguration) .pidPath(pidPath) .dataDir(dataDir()) - .bannedNodeIds(p2PDiscoveryOptionGroup.bannedNodeIds) .metricsSystem(metricsSystem) .permissioningService(permissioningService) .metricsConfiguration(metricsConfiguration) @@ -2393,7 +2248,7 @@ private EthNetworkConfig updateNetworkConfig(final NetworkName network) { } } - if (p2PDiscoveryOptionGroup.bootNodes == null) { + if (p2pConfiguration.getBootNodes() == null) { builder.setBootNodes(new ArrayList<>()); } builder.setDnsDiscoveryUrl(null); @@ -2405,8 +2260,8 @@ private EthNetworkConfig updateNetworkConfig(final NetworkName network) { builder.setNetworkId(networkId); } - if (p2PDiscoveryOptionGroup.discoveryDnsUrl != null) { - builder.setDnsDiscoveryUrl(p2PDiscoveryOptionGroup.discoveryDnsUrl); + if (p2pConfiguration.getDiscoveryDnsUrl() != null) { + builder.setDnsDiscoveryUrl(p2pConfiguration.getDiscoveryDnsUrl()); } else { final Optional discoveryDnsUrlFromGenesis = genesisConfigOptionsSupplier.get().getDiscoveryOptions().getDiscoveryDnsUrl(); @@ -2414,9 +2269,9 @@ private EthNetworkConfig updateNetworkConfig(final NetworkName network) { } List listBootNodes = null; - if (p2PDiscoveryOptionGroup.bootNodes != null) { + if (p2pConfiguration.getBootNodes() != null) { try { - listBootNodes = buildEnodes(p2PDiscoveryOptionGroup.bootNodes, getEnodeDnsConfiguration()); + listBootNodes = buildEnodes(p2pConfiguration.getBootNodes(), getEnodeDnsConfiguration()); } catch (final IllegalArgumentException e) { throw new ParameterException(commandLine, e.getMessage()); } @@ -2428,7 +2283,7 @@ private EthNetworkConfig updateNetworkConfig(final NetworkName network) { } } if (listBootNodes != null) { - if (!p2PDiscoveryOptionGroup.peerDiscoveryEnabled) { + if (!p2pConfiguration.isPeerDiscoveryEnabled()) { logger.warn("Discovery disabled: bootnodes will be ignored."); } DiscoveryConfiguration.assertValidBootnodes(listBootNodes); @@ -2563,12 +2418,12 @@ protected void checkIfRequiredPortsAreAvailable() { .filter(port -> port > 0) .forEach( port -> { - if (port.equals(p2PDiscoveryOptionGroup.p2pPort) + if (port.equals(p2pConfiguration.getP2pPort()) && (NetworkUtility.isPortUnavailableForTcp(port) || NetworkUtility.isPortUnavailableForUdp(port))) { unavailablePorts.add(port); } - if (!port.equals(p2PDiscoveryOptionGroup.p2pPort) + if (!port.equals(p2pConfiguration.getP2pPort()) && NetworkUtility.isPortUnavailableForTcp(port)) { unavailablePorts.add(port); } @@ -2589,7 +2444,7 @@ protected void checkIfRequiredPortsAreAvailable() { private List getEffectivePorts() { final List effectivePorts = new ArrayList<>(); addPortIfEnabled( - effectivePorts, p2PDiscoveryOptionGroup.p2pPort, p2PDiscoveryOptionGroup.p2pEnabled); + effectivePorts, p2pConfiguration.getP2pPort(), p2pConfiguration.isP2pEnabled()); addPortIfEnabled( effectivePorts, graphQlOptions.getGraphQLHttpPort(), graphQlOptions.isGraphQLHttpEnabled()); addPortIfEnabled( diff --git a/besu/src/main/java/org/hyperledger/besu/cli/options/stable/P2POptions.java b/besu/src/main/java/org/hyperledger/besu/cli/options/stable/P2POptions.java new file mode 100644 index 00000000000..72f88b2f75e --- /dev/null +++ b/besu/src/main/java/org/hyperledger/besu/cli/options/stable/P2POptions.java @@ -0,0 +1,180 @@ +/* + * Copyright contributors to Hyperledger Besu. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ +package org.hyperledger.besu.cli.options.stable; + +import static org.hyperledger.besu.ethereum.p2p.config.AutoDiscoverDefaultIP.autoDiscoverDefaultIP; + +import org.hyperledger.besu.cli.DefaultCommandValues; +import org.hyperledger.besu.cli.converter.PercentageConverter; +import org.hyperledger.besu.cli.options.CLIOptions; +import org.hyperledger.besu.cli.util.CommandLineUtils; +import org.hyperledger.besu.ethereum.p2p.config.P2PConfiguration; +import org.hyperledger.besu.ethereum.p2p.peers.EnodeURLImpl; +import org.hyperledger.besu.util.NetworkUtility; +import org.hyperledger.besu.util.number.Fraction; +import org.hyperledger.besu.util.number.Percentage; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.stream.Collectors; + +import org.apache.tuweni.bytes.Bytes; +import picocli.CommandLine; + +public class P2POptions implements CLIOptions { + + // Completely disables P2P within Besu. + @CommandLine.Option( + names = {"--p2p-enabled"}, + description = "Enable P2P functionality (default: ${DEFAULT-VALUE})", + arity = "1") + private final Boolean p2pEnabled = true; + + // Boolean option to indicate if peers should NOT be discovered, default to + // false indicates that + // the peers should be discovered by default. + // + // This negative option is required because of the nature of the option that is + // true when + // added on the command line. You can't do --option=false, so false is set as + // default + // and you have not to set the option at all if you want it false. + // This seems to be the only way it works with Picocli. + // Also many other software use the same negative option scheme for false + // defaults + // meaning that it's probably the right way to handle disabling options. + @CommandLine.Option( + names = {"--discovery-enabled"}, + description = "Enable P2P discovery (default: ${DEFAULT-VALUE})", + arity = "1") + private final Boolean peerDiscoveryEnabled = true; + + // A list of bootstrap nodes can be passed + // and a hardcoded list will be used otherwise by the Runner. + // NOTE: we have no control over default value here. + @CommandLine.Option( + names = {"--bootnodes"}, + paramLabel = "", + description = + "Comma separated enode URLs for P2P discovery bootstrap. " + + "Default is a predefined list.", + split = ",", + arity = "0..*") + private final List bootNodes = null; + + @SuppressWarnings({"FieldCanBeFinal", "FieldMayBeFinal"}) // PicoCLI requires non-final Strings. + @CommandLine.Option( + names = {"--p2p-host"}, + paramLabel = DefaultCommandValues.MANDATORY_HOST_FORMAT_HELP, + description = "IP address this node advertises to its peers (default: ${DEFAULT-VALUE})", + arity = "1") + private String p2pHost = autoDiscoverDefaultIP().getHostAddress(); + + @SuppressWarnings({"FieldCanBeFinal", "FieldMayBeFinal"}) // PicoCLI requires non-final Strings. + @CommandLine.Option( + names = {"--p2p-interface"}, + paramLabel = DefaultCommandValues.MANDATORY_HOST_FORMAT_HELP, + description = + "The network interface address on which this node listens for P2P communication (default: ${DEFAULT-VALUE})", + arity = "1") + private String p2pInterface = NetworkUtility.INADDR_ANY; + + @CommandLine.Option( + names = {"--p2p-port"}, + paramLabel = DefaultCommandValues.MANDATORY_PORT_FORMAT_HELP, + description = "Port on which to listen for P2P communication (default: ${DEFAULT-VALUE})", + arity = "1") + private final Integer p2pPort = EnodeURLImpl.DEFAULT_LISTENING_PORT; + + @CommandLine.Option( + names = {"--max-peers", "--p2p-peer-upper-bound"}, + paramLabel = DefaultCommandValues.MANDATORY_INTEGER_FORMAT_HELP, + description = "Maximum P2P connections that can be established (default: ${DEFAULT-VALUE})") + private final Integer maxPeers = DefaultCommandValues.DEFAULT_MAX_PEERS; + + @CommandLine.Option( + names = {"--remote-connections-limit-enabled"}, + description = + "Whether to limit the number of P2P connections initiated remotely. (default: ${DEFAULT-VALUE})") + private final Boolean isLimitRemoteWireConnectionsEnabled = true; + + @CommandLine.Option( + names = {"--remote-connections-max-percentage"}, + paramLabel = DefaultCommandValues.MANDATORY_DOUBLE_FORMAT_HELP, + description = + "The maximum percentage of P2P connections that can be initiated remotely. Must be between 0 and 100 inclusive. (default: ${DEFAULT-VALUE})", + arity = "1", + converter = PercentageConverter.class) + private final Percentage maxRemoteConnectionsPercentage = + Fraction.fromFloat(DefaultCommandValues.DEFAULT_FRACTION_REMOTE_WIRE_CONNECTIONS_ALLOWED) + .toPercentage(); + + @SuppressWarnings({"FieldCanBeFinal", "FieldMayBeFinal"}) // PicoCLI requires non-final Strings. + @CommandLine.Option( + names = {"--discovery-dns-url"}, + description = "Specifies the URL to use for DNS discovery") + private String discoveryDnsUrl = null; + + @CommandLine.Option( + names = {"--random-peer-priority-enabled"}, + description = + "Allow for incoming connections to be prioritized randomly. This will prevent (typically small, stable) networks from forming impenetrable peer cliques. (default: ${DEFAULT-VALUE})") + private final Boolean randomPeerPriority = Boolean.FALSE; + + @CommandLine.Option( + names = {"--banned-node-ids", "--banned-node-id"}, + paramLabel = DefaultCommandValues.MANDATORY_NODE_ID_FORMAT_HELP, + description = "A list of node IDs to ban from the P2P network.", + split = ",", + arity = "1..*") + void setBannedNodeIds(final List values) { + try { + bannedNodeIds = + values.stream() + .filter(value -> !value.isEmpty()) + .map(EnodeURLImpl::parseNodeId) + .collect(Collectors.toList()); + } catch (final IllegalArgumentException e) { + throw new CommandLine.ParameterException( + new CommandLine(this), "Invalid ids supplied to '--banned-node-ids'. " + e.getMessage()); + } + } + + private Collection bannedNodeIds = new ArrayList<>(); + + @Override + public P2PConfiguration toDomainObject() { + return new P2PConfiguration.Builder() + .p2pEnabled(p2pEnabled) + .peerDiscoveryEnabled(peerDiscoveryEnabled) + .bootNodes(bootNodes) + .p2pHost(p2pHost) + .p2pInterface(p2pInterface) + .p2pPort(p2pPort) + .maxPeers(maxPeers) + .isLimitRemoteWireConnectionsEnabled(isLimitRemoteWireConnectionsEnabled) + .maxRemoteConnectionsPercentage(maxRemoteConnectionsPercentage) + .discoveryDnsUrl(discoveryDnsUrl) + .randomPeerPriority(randomPeerPriority) + .bannedNodeIds(bannedNodeIds) + .build(); + } + + @Override + public List getCLIOptions() { + return CommandLineUtils.getCLIOptions(this, new P2POptions()); + } +} diff --git a/besu/src/test/java/org/hyperledger/besu/cli/CommandTestAbstract.java b/besu/src/test/java/org/hyperledger/besu/cli/CommandTestAbstract.java index 1dfb5f449b7..ffa3bec1e1e 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/CommandTestAbstract.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/CommandTestAbstract.java @@ -321,14 +321,10 @@ public void initMocks() throws Exception { when(mockRunnerBuilder.vertx(any())).thenReturn(mockRunnerBuilder); when(mockRunnerBuilder.besuController(any())).thenReturn(mockRunnerBuilder); - when(mockRunnerBuilder.discovery(anyBoolean())).thenReturn(mockRunnerBuilder); + when(mockRunnerBuilder.p2pConfiguration(any())).thenReturn(mockRunnerBuilder); when(mockRunnerBuilder.ethNetworkConfig(any())).thenReturn(mockRunnerBuilder); when(mockRunnerBuilder.networkingConfiguration(any())).thenReturn(mockRunnerBuilder); - when(mockRunnerBuilder.p2pAdvertisedHost(anyString())).thenReturn(mockRunnerBuilder); - when(mockRunnerBuilder.p2pListenPort(anyInt())).thenReturn(mockRunnerBuilder); - when(mockRunnerBuilder.p2pListenInterface(anyString())).thenReturn(mockRunnerBuilder); when(mockRunnerBuilder.permissioningConfiguration(any())).thenReturn(mockRunnerBuilder); - when(mockRunnerBuilder.p2pEnabled(anyBoolean())).thenReturn(mockRunnerBuilder); when(mockRunnerBuilder.natMethod(any())).thenReturn(mockRunnerBuilder); when(mockRunnerBuilder.natManagerServiceName(any())).thenReturn(mockRunnerBuilder); when(mockRunnerBuilder.natMethodFallbackEnabled(anyBoolean())).thenReturn(mockRunnerBuilder); @@ -339,7 +335,6 @@ public void initMocks() throws Exception { when(mockRunnerBuilder.jsonRpcIpcConfiguration(any())).thenReturn(mockRunnerBuilder); when(mockRunnerBuilder.apiConfiguration(any())).thenReturn(mockRunnerBuilder); when(mockRunnerBuilder.dataDir(any())).thenReturn(mockRunnerBuilder); - when(mockRunnerBuilder.bannedNodeIds(any())).thenReturn(mockRunnerBuilder); when(mockRunnerBuilder.metricsSystem(any())).thenReturn(mockRunnerBuilder); when(mockRunnerBuilder.permissioningService(any())).thenReturn(mockRunnerBuilder); when(mockRunnerBuilder.metricsConfiguration(any())).thenReturn(mockRunnerBuilder); diff --git a/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/config/AutoDiscoverDefaultIP.java b/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/config/AutoDiscoverDefaultIP.java new file mode 100644 index 00000000000..59887997033 --- /dev/null +++ b/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/config/AutoDiscoverDefaultIP.java @@ -0,0 +1,33 @@ +/* + * Copyright contributors to Hyperledger Besu. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ +package org.hyperledger.besu.ethereum.p2p.config; + +import java.net.InetAddress; +import java.util.Optional; + +public class AutoDiscoverDefaultIP { + private static InetAddress autoDiscoveredDefaultIP = null; + + // Used to discover the default IP of the client. + // Loopback IP is used by default as this is how smokeTests require it to be + // and it's probably a good security behaviour to default only on the localhost. + public static InetAddress autoDiscoverDefaultIP() { + + autoDiscoveredDefaultIP = + Optional.ofNullable(autoDiscoveredDefaultIP).orElseGet(InetAddress::getLoopbackAddress); + + return autoDiscoveredDefaultIP; + } +} diff --git a/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/config/P2PConfiguration.java b/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/config/P2PConfiguration.java new file mode 100644 index 00000000000..7040f8b3ee9 --- /dev/null +++ b/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/config/P2PConfiguration.java @@ -0,0 +1,205 @@ +/* + * Copyright contributors to Hyperledger Besu. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ +package org.hyperledger.besu.ethereum.p2p.config; + +import org.hyperledger.besu.util.number.Percentage; + +import java.util.Collection; +import java.util.List; + +import org.apache.tuweni.bytes.Bytes; + +public class P2PConfiguration { + // Public IP stored to prevent having to research it each time we need it. + private final boolean p2pEnabled; + private final boolean peerDiscoveryEnabled; + private final List bootNodes; + private final String p2pHost; + private final String p2pInterface; + private final int p2pPort; + private final int maxPeers; + private final boolean isLimitRemoteWireConnectionsEnabled; + private final Percentage maxRemoteConnectionsPercentage; + private final String discoveryDnsUrl; + private final boolean randomPeerPriority; + private final Collection bannedNodeIds; + + public P2PConfiguration( + final boolean p2pEnabled, + final boolean peerDiscoveryEnabled, + final List bootNodes, + final String p2pHost, + final String p2pInterface, + final int p2pPort, + final int maxPeers, + final boolean isLimitRemoteWireConnectionsEnabled, + final Percentage maxRemoteConnectionsPercentage, + final String discoveryDnsUrl, + final boolean randomPeerPriority, + final Collection bannedNodeIds) { + this.p2pEnabled = p2pEnabled; + this.peerDiscoveryEnabled = peerDiscoveryEnabled; + this.bootNodes = bootNodes; + this.p2pHost = p2pHost; + this.p2pInterface = p2pInterface; + this.p2pPort = p2pPort; + this.maxPeers = maxPeers; + this.isLimitRemoteWireConnectionsEnabled = isLimitRemoteWireConnectionsEnabled; + this.maxRemoteConnectionsPercentage = maxRemoteConnectionsPercentage; + this.discoveryDnsUrl = discoveryDnsUrl; + this.randomPeerPriority = randomPeerPriority; + this.bannedNodeIds = bannedNodeIds; + } + + public boolean isP2pEnabled() { + return p2pEnabled; + } + + public boolean isPeerDiscoveryEnabled() { + return peerDiscoveryEnabled; + } + + public List getBootNodes() { + return bootNodes; + } + + public String getP2pHost() { + return p2pHost; + } + + public String getP2pInterface() { + return p2pInterface; + } + + public int getP2pPort() { + return p2pPort; + } + + public int getMaxPeers() { + return maxPeers; + } + + public boolean isLimitRemoteWireConnectionsEnabled() { + return isLimitRemoteWireConnectionsEnabled; + } + + public Percentage getMaxRemoteConnectionsPercentage() { + return maxRemoteConnectionsPercentage; + } + + public String getDiscoveryDnsUrl() { + return discoveryDnsUrl; + } + + public boolean isRandomPeerPriority() { + return randomPeerPriority; + } + + public Collection getBannedNodeIds() { + return bannedNodeIds; + } + + public static class Builder { + private boolean p2pEnabled = true; + private boolean peerDiscoveryEnabled = true; + private List bootNodes; + private String p2pHost; + private String p2pInterface; + private int p2pPort; + private int maxPeers; + private boolean isLimitRemoteWireConnectionsEnabled = true; + private Percentage maxRemoteConnectionsPercentage; + private String discoveryDnsUrl; + private boolean randomPeerPriority = false; + private Collection bannedNodeIds; + + public Builder p2pEnabled(final boolean p2pEnabled) { + this.p2pEnabled = p2pEnabled; + return this; + } + + public Builder peerDiscoveryEnabled(final boolean peerDiscoveryEnabled) { + this.peerDiscoveryEnabled = peerDiscoveryEnabled; + return this; + } + + public Builder bootNodes(final List bootNodes) { + this.bootNodes = bootNodes; + return this; + } + + public Builder p2pHost(final String p2pHost) { + this.p2pHost = p2pHost; + return this; + } + + public Builder p2pInterface(final String p2pInterface) { + this.p2pInterface = p2pInterface; + return this; + } + + public Builder p2pPort(final int p2pPort) { + this.p2pPort = p2pPort; + return this; + } + + public Builder maxPeers(final int maxPeers) { + this.maxPeers = maxPeers; + return this; + } + + public Builder isLimitRemoteWireConnectionsEnabled( + final boolean isLimitRemoteWireConnectionsEnabled) { + this.isLimitRemoteWireConnectionsEnabled = isLimitRemoteWireConnectionsEnabled; + return this; + } + + public Builder maxRemoteConnectionsPercentage(final Percentage maxRemoteConnectionsPercentage) { + this.maxRemoteConnectionsPercentage = maxRemoteConnectionsPercentage; + return this; + } + + public Builder discoveryDnsUrl(final String discoveryDnsUrl) { + this.discoveryDnsUrl = discoveryDnsUrl; + return this; + } + + public Builder randomPeerPriority(final boolean randomPeerPriority) { + this.randomPeerPriority = randomPeerPriority; + return this; + } + + public Builder bannedNodeIds(final Collection bannedNodeIds) { + this.bannedNodeIds = bannedNodeIds; + return this; + } + + public P2PConfiguration build() { + return new P2PConfiguration( + p2pEnabled, + peerDiscoveryEnabled, + bootNodes, + p2pHost, + p2pInterface, + p2pPort, + maxPeers, + isLimitRemoteWireConnectionsEnabled, + maxRemoteConnectionsPercentage, + discoveryDnsUrl, + randomPeerPriority, + bannedNodeIds); + } + } +} From cc7c398cccb7ed6c9c2b78b2e058d1b4563b8bc7 Mon Sep 17 00:00:00 2001 From: Gabriel-Trintinalia Date: Tue, 18 Jun 2024 15:34:12 +1000 Subject: [PATCH 2/7] Move subnet and fix builders Signed-off-by: Gabriel-Trintinalia --- .../tests/acceptance/dsl/node/BesuNode.java | 20 ++++- .../BesuNodeConfigurationBuilder.java | 5 +- .../node/configuration/BesuNodeFactory.java | 27 ++++--- .../acceptance/dsl/privacy/PrivacyNode.java | 2 +- .../org/hyperledger/besu/RunnerBuilder.java | 14 +--- .../org/hyperledger/besu/cli/BesuCommand.java | 4 - .../besu/cli/options/stable/P2POptions.java | 16 ++++ .../hyperledger/besu/RunnerBuilderTest.java | 79 ++++++++++++------- .../hyperledger/besu/cli/BesuCommandTest.java | 65 ++++++++------- .../besu/cli/CommandTestAbstract.java | 4 +- .../ethereum/p2p/config/P2PConfiguration.java | 47 +++++++---- 11 files changed, 173 insertions(+), 110 deletions(-) diff --git a/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/BesuNode.java b/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/BesuNode.java index 89bf6215485..07b348d0bf4 100644 --- a/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/BesuNode.java +++ b/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/BesuNode.java @@ -137,6 +137,7 @@ public class BesuNode implements NodeConfiguration, RunnableNode, AutoCloseable public BesuNode( final String name, final Optional dataPath, + final P2PConfiguration p2PConfiguration, final MiningParameters miningParameters, final TransactionPoolConfiguration txPoolConfiguration, final JsonRpcConfiguration jsonRpcConfiguration, @@ -151,7 +152,6 @@ public BesuNode( final boolean devMode, final NetworkName network, final GenesisConfigurationProvider genesisConfigProvider, - final P2PConfiguration p2PConfiguration, final Optional tlsConfiguration, final NetworkingConfiguration networkingConfiguration, final boolean bootnodeEligible, @@ -285,6 +285,10 @@ public URI enodeUrl() { "enode://" + getNodeId() + "@" + LOCALHOST + ":" + getRuntimeP2pPort() + discport); } + public String getP2pPort() { + return String.valueOf(p2PConfiguration.getP2pPort()); + } + private String getRuntimeP2pPort() { final String port = portsProperties.getProperty("p2p"); if (port == null) { @@ -386,6 +390,11 @@ public Optional getEngineJsonRpcPort() { } } + @Override + public String getHostName() { + return LOCALHOST; + } + public NodeRequests nodeRequests() { Optional websocketService = Optional.empty(); if (nodeRequests == null) { @@ -636,6 +645,10 @@ public List getBootnodes() { return unmodifiableList(bootnodes); } + @Override + public boolean isP2pEnabled() { + return p2PConfiguration.isP2pEnabled(); + } public Optional getTLSConfiguration() { return tlsConfiguration; @@ -701,6 +714,11 @@ public boolean isAltbn128Native() { return altbn128Native; } + @Override + public boolean isDiscoveryEnabled() { + return p2PConfiguration.isPeerDiscoveryEnabled(); + } + Optional getPermissioningConfiguration() { return permissioningConfiguration; } diff --git a/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/configuration/BesuNodeConfigurationBuilder.java b/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/configuration/BesuNodeConfigurationBuilder.java index 7f91ba6f654..424c1d76e56 100644 --- a/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/configuration/BesuNodeConfigurationBuilder.java +++ b/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/configuration/BesuNodeConfigurationBuilder.java @@ -247,8 +247,7 @@ public BesuNodeConfigurationBuilder jsonRpcAuthenticationUsingECDSA() throws URI return this; } - public BesuNodeConfigurationBuilder p2pConfiguration( - final P2PConfiguration p2pConfiguration) { + public BesuNodeConfigurationBuilder p2pConfiguration(final P2PConfiguration p2pConfiguration) { this.p2pConfiguration = p2pConfiguration; return this; } @@ -366,7 +365,6 @@ public BesuNodeConfigurationBuilder genesisConfigProvider( return this; } - private static Path toPath(final String path) throws Exception { return Path.of(BesuNodeConfigurationBuilder.class.getResource(path).toURI()); } @@ -432,7 +430,6 @@ public BesuNodeConfigurationBuilder pkiBlockCreationEnabled( return this; } - public BesuNodeConfigurationBuilder plugins(final List plugins) { this.plugins.clear(); this.plugins.addAll(plugins); diff --git a/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/configuration/BesuNodeFactory.java b/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/configuration/BesuNodeFactory.java index 96ab9cf6235..b46697d8e9c 100644 --- a/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/configuration/BesuNodeFactory.java +++ b/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/configuration/BesuNodeFactory.java @@ -28,6 +28,7 @@ import org.hyperledger.besu.ethereum.core.InMemoryPrivacyStorageProvider; import org.hyperledger.besu.ethereum.core.MiningParameters; import org.hyperledger.besu.ethereum.core.PrivacyParameters; +import org.hyperledger.besu.ethereum.p2p.config.P2PConfiguration; import org.hyperledger.besu.ethereum.permissioning.LocalPermissioningConfiguration; import org.hyperledger.besu.ethereum.permissioning.PermissioningConfiguration; import org.hyperledger.besu.pki.keystore.KeyStoreWrapper; @@ -58,6 +59,7 @@ public BesuNode create(final BesuNodeConfiguration config) throws IOException { return new BesuNode( config.getName(), config.getDataPath(), + config.getP2PConfiguration(), config.getMiningParameters(), config.getTransactionPoolConfiguration(), config.getJsonRpcConfiguration(), @@ -72,11 +74,8 @@ public BesuNode create(final BesuNodeConfiguration config) throws IOException { config.isDevMode(), config.getNetwork(), config.getGenesisConfigProvider(), - config.isP2pEnabled(), - config.getP2pPort(), config.getTLSConfiguration(), config.getNetworkingConfiguration(), - config.isDiscoveryEnabled(), config.isBootnodeEligible(), config.isRevertReasonEnabled(), config.isSecp256k1Native(), @@ -181,9 +180,9 @@ public BesuNode createArchiveNodeWithDiscoveryDisabledAndAdmin(final String name return create( new BesuNodeConfigurationBuilder() .name(name) + .p2pConfiguration(P2PConfiguration.builder().peerDiscoveryEnabled(false).build()) .jsonRpcConfiguration(node.jsonRpcConfigWithAdmin()) .webSocketEnabled() - .discoveryEnabled(false) .build()); } @@ -198,7 +197,7 @@ public BesuNode createArchiveNodeNetServicesEnabled(final String name) throws IO // .setMetricsConfiguration(metricsConfiguration) .jsonRpcConfiguration(node.jsonRpcConfigWithAdmin()) .webSocketEnabled() - .p2pEnabled(true) + .p2pConfiguration(P2PConfiguration.builder().p2pEnabled(true).build()) .build()); } @@ -207,7 +206,7 @@ public BesuNode createArchiveNodeNetServicesDisabled(final String name) throws I new BesuNodeConfigurationBuilder() .name(name) .jsonRpcConfiguration(node.jsonRpcConfigWithAdmin()) - .p2pEnabled(false) + .p2pConfiguration(P2PConfiguration.builder().p2pEnabled(false).build()) .build()); } @@ -277,7 +276,7 @@ public BesuNode createNodeWithP2pDisabled(final String name) throws IOException return create( new BesuNodeConfigurationBuilder() .name(name) - .p2pEnabled(false) + .p2pConfiguration(P2PConfiguration.builder().p2pEnabled(false).build()) .jsonRpcConfiguration(node.createJsonRpcEnabledConfig()) .build()); } @@ -359,7 +358,7 @@ public BesuNode createNodeWithNoDiscovery(final String name) throws IOException return create( new BesuNodeConfigurationBuilder() .name(name) - .discoveryEnabled(false) + .p2pConfiguration(P2PConfiguration.builder().peerDiscoveryEnabled(false).build()) .engineRpcEnabled(false) .build()); } @@ -478,11 +477,12 @@ public BesuNode createIbft2Node(final String name, final boolean fixedPort) thro .devMode(false) .genesisConfigProvider(GenesisConfigurationFactory::createIbft2GenesisConfig); if (fixedPort) { - builder.p2pPort( + var port = Math.abs(name.hashCode() % 60000) + 1024 - + 500); // Generate a consistent port for p2p based on node name (+ 500 to avoid + + 500; // Generate a consistent port for p2p based on node name (+ 500 to avoid // clashing with RPC port or other nodes with a similar name) + builder.p2pConfiguration(P2PConfiguration.builder().p2pPort(port).build()); } return create(builder.build()); } @@ -530,11 +530,12 @@ public BesuNode createQbftNode(final String name, final boolean fixedPort) throw .devMode(false) .genesisConfigProvider(GenesisConfigurationFactory::createQbftGenesisConfig); if (fixedPort) { - builder.p2pPort( + var port = Math.abs(name.hashCode() % 60000) + 1024 - + 500); // Generate a consistent port for p2p based on node name (+ 500 to avoid + + 500; // Generate a consistent port for p2p based on node name (+ 500 to avoid // clashing with RPC port or other nodes with a similar name) + builder.p2pConfiguration(P2PConfiguration.builder().p2pPort(port).build()); } return create(builder.build()); } @@ -717,7 +718,7 @@ private BesuNodeConfigurationBuilder createConfigurationBuilderWithStaticNodes( .name(name) .jsonRpcEnabled() .webSocketEnabled() - .discoveryEnabled(false) + .p2pConfiguration(P2PConfiguration.builder().peerDiscoveryEnabled(false).build()) .staticNodes(staticNodesUrls) .bootnodeEligible(false); } diff --git a/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/privacy/PrivacyNode.java b/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/privacy/PrivacyNode.java index f55d8f90f60..4c39e6ecfe5 100644 --- a/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/privacy/PrivacyNode.java +++ b/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/privacy/PrivacyNode.java @@ -100,6 +100,7 @@ public PrivacyNode( new BesuNode( besuConfig.getName(), besuConfig.getDataPath(), + besuConfig.getP2PConfiguration(), besuConfig.getMiningParameters(), besuConfig.getTransactionPoolConfiguration(), besuConfig.getJsonRpcConfiguration(), @@ -114,7 +115,6 @@ public PrivacyNode( besuConfig.isDevMode(), besuConfig.getNetwork(), besuConfig.getGenesisConfigProvider(), - besuConfig.getP2PConfiguration(), besuConfig.getTLSConfiguration(), besuConfig.getNetworkingConfiguration(), besuConfig.isBootnodeEligible(), diff --git a/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java b/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java index 909d896af20..29647e6daf9 100644 --- a/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java +++ b/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java @@ -189,7 +189,6 @@ public class RunnerBuilder { private JsonRpcIpcConfiguration jsonRpcIpcConfiguration; private boolean legacyForkIdEnabled; private Optional enodeDnsConfiguration; - private List allowedSubnets = new ArrayList<>(); /** Instantiates a new Runner builder. */ public RunnerBuilder() {} @@ -534,17 +533,6 @@ public RunnerBuilder enodeDnsConfiguration(final EnodeDnsConfiguration enodeDnsC return this; } - /** - * Add subnet configuration - * - * @param allowedSubnets the allowedSubnets - * @return the runner builder - */ - public RunnerBuilder allowedSubnets(final List allowedSubnets) { - this.allowedSubnets = allowedSubnets; - return this; - } - /** * Build Runner instance. * @@ -604,7 +592,7 @@ public Runner build() { final PeerPermissionsDenylist bannedNodes = PeerPermissionsDenylist.create(); p2pConfiguration.getBannedNodeIds().forEach(bannedNodes::add); - PeerPermissionSubnet peerPermissionSubnet = new PeerPermissionSubnet(allowedSubnets); + PeerPermissionSubnet peerPermissionSubnet = new PeerPermissionSubnet(p2pConfiguration.getAllowedSubnets()); final PeerPermissions defaultPeerPermissions = PeerPermissions.combine(peerPermissionSubnet, bannedNodes); diff --git a/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java b/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java index 71bae215cdb..1ac4e432088 100644 --- a/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java +++ b/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java @@ -43,8 +43,6 @@ import org.hyperledger.besu.cli.config.NetworkName; import org.hyperledger.besu.cli.config.ProfileName; import org.hyperledger.besu.cli.converter.MetricCategoryConverter; -import org.hyperledger.besu.cli.converter.PercentageConverter; -import org.hyperledger.besu.cli.converter.SubnetInfoConverter; import org.hyperledger.besu.cli.custom.JsonRPCAllowlistHostsProperty; import org.hyperledger.besu.cli.error.BesuExecutionExceptionHandler; import org.hyperledger.besu.cli.error.BesuParameterExceptionHandler; @@ -246,7 +244,6 @@ import io.vertx.core.VertxOptions; import io.vertx.core.json.DecodeException; import io.vertx.core.metrics.MetricsOptions; -import org.apache.commons.net.util.SubnetUtils.SubnetInfo; import org.apache.tuweni.bytes.Bytes; import org.apache.tuweni.units.bigints.UInt256; import org.slf4j.Logger; @@ -2178,7 +2175,6 @@ private Runner synchronize( .storageProvider(keyValueStorageProvider(keyValueStorageName)) .rpcEndpointService(rpcEndpointServiceImpl) .enodeDnsConfiguration(getEnodeDnsConfiguration()) - .allowedSubnets(p2PDiscoveryOptionGroup.allowedSubnets) .build(); addShutdownHook(runner); diff --git a/besu/src/main/java/org/hyperledger/besu/cli/options/stable/P2POptions.java b/besu/src/main/java/org/hyperledger/besu/cli/options/stable/P2POptions.java index 72f88b2f75e..4da0b775da8 100644 --- a/besu/src/main/java/org/hyperledger/besu/cli/options/stable/P2POptions.java +++ b/besu/src/main/java/org/hyperledger/besu/cli/options/stable/P2POptions.java @@ -18,6 +18,7 @@ import org.hyperledger.besu.cli.DefaultCommandValues; import org.hyperledger.besu.cli.converter.PercentageConverter; +import org.hyperledger.besu.cli.converter.SubnetInfoConverter; import org.hyperledger.besu.cli.options.CLIOptions; import org.hyperledger.besu.cli.util.CommandLineUtils; import org.hyperledger.besu.ethereum.p2p.config.P2PConfiguration; @@ -31,9 +32,11 @@ import java.util.List; import java.util.stream.Collectors; +import org.apache.commons.net.util.SubnetUtils.SubnetInfo; import org.apache.tuweni.bytes.Bytes; import picocli.CommandLine; +/** The P2POptions Config Cli Options. */ public class P2POptions implements CLIOptions { // Completely disables P2P within Besu. @@ -153,8 +156,20 @@ void setBannedNodeIds(final List values) { } } + @CommandLine.Option( + names = {"--net-restrict"}, + arity = "1..*", + split = ",", + converter = SubnetInfoConverter.class, + description = + "Comma-separated list of allowed IP subnets (e.g., '192.168.1.0/24,10.0.0.0/8').") + private List allowedSubnets; + private Collection bannedNodeIds = new ArrayList<>(); + /** Default constructor. */ + public P2POptions() {} + @Override public P2PConfiguration toDomainObject() { return new P2PConfiguration.Builder() @@ -170,6 +185,7 @@ public P2PConfiguration toDomainObject() { .discoveryDnsUrl(discoveryDnsUrl) .randomPeerPriority(randomPeerPriority) .bannedNodeIds(bannedNodeIds) + .allowedSubnets(allowedSubnets) .build(); } diff --git a/besu/src/test/java/org/hyperledger/besu/RunnerBuilderTest.java b/besu/src/test/java/org/hyperledger/besu/RunnerBuilderTest.java index c1770640801..c8d875ec0d1 100644 --- a/besu/src/test/java/org/hyperledger/besu/RunnerBuilderTest.java +++ b/besu/src/test/java/org/hyperledger/besu/RunnerBuilderTest.java @@ -59,6 +59,7 @@ import org.hyperledger.besu.ethereum.mainnet.MainnetBlockHeaderFunctions; import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule; import org.hyperledger.besu.ethereum.p2p.config.NetworkingConfiguration; +import org.hyperledger.besu.ethereum.p2p.config.P2PConfiguration; import org.hyperledger.besu.ethereum.p2p.config.SubProtocolConfiguration; import org.hyperledger.besu.ethereum.p2p.peers.EnodeURLImpl; import org.hyperledger.besu.ethereum.storage.StorageProvider; @@ -150,11 +151,14 @@ public void enodeUrlShouldHaveAdvertisedHostWhenDiscoveryDisabled() { final Runner runner = new RunnerBuilder() - .p2pListenInterface("0.0.0.0") - .p2pListenPort(p2pListenPort) - .p2pAdvertisedHost(p2pAdvertisedHost) - .p2pEnabled(true) - .discovery(false) + .p2pConfiguration( + P2PConfiguration.builder() + .p2pInterface("0.0.0.0") + .p2pPort(p2pListenPort) + .p2pHost(p2pAdvertisedHost) + .p2pEnabled(true) + .peerDiscoveryEnabled(false) + .build()) .besuController(besuController) .ethNetworkConfig(mock(EthNetworkConfig.class)) .metricsSystem(mock(ObservableMetricsSystem.class)) @@ -194,11 +198,14 @@ public void movingAcrossProtocolSpecsUpdatesNodeRecord() { when(protocolContext.getBlockchain()).thenReturn(inMemoryBlockchain); final Runner runner = new RunnerBuilder() - .discovery(true) - .p2pListenInterface("0.0.0.0") - .p2pListenPort(p2pListenPort) - .p2pAdvertisedHost(p2pAdvertisedHost) - .p2pEnabled(true) + .p2pConfiguration( + P2PConfiguration.builder() + .p2pInterface("0.0.0.0") + .p2pPort(p2pListenPort) + .p2pHost(p2pAdvertisedHost) + .p2pEnabled(true) + .peerDiscoveryEnabled(true) + .build()) .natMethod(NatMethod.NONE) .besuController(besuController) .ethNetworkConfig(mock(EthNetworkConfig.class)) @@ -252,11 +259,14 @@ public void whenEngineApiAddedListensOnDefaultPort() { final Runner runner = new RunnerBuilder() - .discovery(true) - .p2pListenInterface("0.0.0.0") - .p2pListenPort(30303) - .p2pAdvertisedHost("127.0.0.1") - .p2pEnabled(true) + .p2pConfiguration( + P2PConfiguration.builder() + .p2pInterface("0.0.0.0") + .p2pPort(30303) + .p2pHost("127.0.0.1") + .p2pEnabled(true) + .peerDiscoveryEnabled(true) + .build()) .natMethod(NatMethod.NONE) .besuController(besuController) .ethNetworkConfig(mockMainnet) @@ -295,11 +305,14 @@ public void whenEngineApiAddedWebSocketReadyOnSamePort() { final Runner runner = new RunnerBuilder() - .discovery(true) - .p2pListenInterface("0.0.0.0") - .p2pListenPort(30303) - .p2pAdvertisedHost("127.0.0.1") - .p2pEnabled(true) + .p2pConfiguration( + P2PConfiguration.builder() + .p2pInterface("0.0.0.0") + .p2pPort(30303) + .p2pHost("127.0.0.1") + .p2pEnabled(true) + .peerDiscoveryEnabled(true) + .build()) .natMethod(NatMethod.NONE) .besuController(besuController) .ethNetworkConfig(mockMainnet) @@ -337,11 +350,14 @@ public void whenEngineApiAddedEthSubscribeAvailable() { final Runner runner = new RunnerBuilder() - .discovery(true) - .p2pListenInterface("0.0.0.0") - .p2pListenPort(30303) - .p2pAdvertisedHost("127.0.0.1") - .p2pEnabled(true) + .p2pConfiguration( + P2PConfiguration.builder() + .p2pInterface("0.0.0.0") + .p2pPort(30303) + .p2pHost("127.0.0.1") + .p2pEnabled(true) + .peerDiscoveryEnabled(true) + .build()) .natMethod(NatMethod.NONE) .besuController(besuController) .ethNetworkConfig(mockMainnet) @@ -381,11 +397,14 @@ public void noEngineApiNoServiceForMethods() { final Runner runner = new RunnerBuilder() - .discovery(true) - .p2pListenInterface("0.0.0.0") - .p2pListenPort(30303) - .p2pAdvertisedHost("127.0.0.1") - .p2pEnabled(true) + .p2pConfiguration( + P2PConfiguration.builder() + .p2pInterface("0.0.0.0") + .p2pPort(30303) + .p2pHost("127.0.0.1") + .p2pEnabled(true) + .peerDiscoveryEnabled(true) + .build()) .natMethod(NatMethod.NONE) .besuController(besuController) .ethNetworkConfig(mockMainnet) diff --git a/besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java b/besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java index 1e8d15cdb4f..dd8ee1d14ce 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java @@ -57,6 +57,7 @@ import org.hyperledger.besu.ethereum.core.MiningParameters; import org.hyperledger.besu.ethereum.eth.sync.SyncMode; import org.hyperledger.besu.ethereum.eth.sync.SynchronizerConfiguration; +import org.hyperledger.besu.ethereum.p2p.config.P2PConfiguration; import org.hyperledger.besu.ethereum.p2p.peers.EnodeURLImpl; import org.hyperledger.besu.ethereum.worldstate.DataStorageConfiguration; import org.hyperledger.besu.evm.precompile.AbstractAltBnPrecompiledContract; @@ -110,6 +111,7 @@ public class BesuCommandTest extends CommandTestAbstract { private static final WebSocketConfiguration DEFAULT_WEB_SOCKET_CONFIGURATION; private static final MetricsConfiguration DEFAULT_METRICS_CONFIGURATION; private static final ApiConfiguration DEFAULT_API_CONFIGURATION; + private static final P2PConfiguration DEFAULT_P2P_CONFIGURATION; private static final int GENESIS_CONFIG_TEST_CHAINID = 3141592; private static final JsonObject GENESIS_VALID_JSON = @@ -149,6 +151,7 @@ public class BesuCommandTest extends CommandTestAbstract { DEFAULT_WEB_SOCKET_CONFIGURATION = WebSocketConfiguration.createDefault(); DEFAULT_METRICS_CONFIGURATION = MetricsConfiguration.builder().build(); DEFAULT_API_CONFIGURATION = ImmutableApiConfiguration.builder().build(); + DEFAULT_P2P_CONFIGURATION = P2PConfiguration.builder().build(); } @BeforeEach @@ -200,7 +203,6 @@ public void callingBesuCommandWithoutOptionsMustSyncWithDefaultValues() { final ArgumentCaptor ethNetworkArg = ArgumentCaptor.forClass(EthNetworkConfig.class); - verify(mockRunnerBuilder).discovery(eq(true)); verify(mockRunnerBuilder) .ethNetworkConfig( new EthNetworkConfig( @@ -208,8 +210,6 @@ public void callingBesuCommandWithoutOptionsMustSyncWithDefaultValues() { MAINNET.getNetworkId(), MAINNET_BOOTSTRAP_NODES, MAINNET_DISCOVERY_URL)); - verify(mockRunnerBuilder).p2pAdvertisedHost(eq("127.0.0.1")); - verify(mockRunnerBuilder).p2pListenPort(eq(30303)); verify(mockRunnerBuilder).jsonRpcConfiguration(eq(DEFAULT_JSON_RPC_CONFIGURATION)); verify(mockRunnerBuilder).graphQLConfiguration(eq(DEFAULT_GRAPH_QL_CONFIGURATION)); verify(mockRunnerBuilder).webSocketConfiguration(eq(DEFAULT_WEB_SOCKET_CONFIGURATION)); @@ -217,6 +217,7 @@ public void callingBesuCommandWithoutOptionsMustSyncWithDefaultValues() { verify(mockRunnerBuilder).ethNetworkConfig(ethNetworkArg.capture()); verify(mockRunnerBuilder).autoLogBloomCaching(eq(true)); verify(mockRunnerBuilder).apiConfiguration(DEFAULT_API_CONFIGURATION); + verify(mockRunnerBuilder).p2pConfiguration(DEFAULT_P2P_CONFIGURATION); verify(mockRunnerBuilder).build(); verify(mockControllerBuilderFactory).fromEthNetworkConfig(ethNetworkArg.capture(), any()); @@ -634,9 +635,11 @@ public void identityValueTrueMustBeUsed() { public void p2pEnabledOptionValueTrueMustBeUsed() { parseCommand("--p2p-enabled", "true"); - verify(mockRunnerBuilder).p2pEnabled(eq(true)); + verify(mockRunnerBuilder).p2pConfiguration(p2PConfigurationArgumentCaptor.capture()); verify(mockRunnerBuilder).build(); + assertThat(p2PConfigurationArgumentCaptor.getValue().isP2pEnabled()).isTrue(); + assertThat(commandOutput.toString(UTF_8)).isEmpty(); assertThat(commandErrorOutput.toString(UTF_8)).isEmpty(); } @@ -645,9 +648,11 @@ public void p2pEnabledOptionValueTrueMustBeUsed() { public void p2pEnabledOptionValueFalseMustBeUsed() { parseCommand("--p2p-enabled", "false"); - verify(mockRunnerBuilder).p2pEnabled(eq(false)); + verify(mockRunnerBuilder).p2pConfiguration(p2PConfigurationArgumentCaptor.capture()); verify(mockRunnerBuilder).build(); + assertThat(p2PConfigurationArgumentCaptor.getValue().isP2pEnabled()).isFalse(); + assertThat(commandOutput.toString(UTF_8)).isEmpty(); assertThat(commandErrorOutput.toString(UTF_8)).isEmpty(); } @@ -727,9 +732,11 @@ public void p2pOptionsRequiresServiceToBeEnabledToml() throws IOException { public void discoveryOptionValueTrueMustBeUsed() { parseCommand("--discovery-enabled", "true"); - verify(mockRunnerBuilder).discovery(eq(true)); + verify(mockRunnerBuilder).p2pConfiguration(p2PConfigurationArgumentCaptor.capture()); verify(mockRunnerBuilder).build(); + assertThat(p2PConfigurationArgumentCaptor.getValue().isPeerDiscoveryEnabled()).isTrue(); + assertThat(commandOutput.toString(UTF_8)).isEmpty(); assertThat(commandErrorOutput.toString(UTF_8)).isEmpty(); } @@ -738,9 +745,11 @@ public void discoveryOptionValueTrueMustBeUsed() { public void discoveryOptionValueFalseMustBeUsed() { parseCommand("--discovery-enabled", "false"); - verify(mockRunnerBuilder).discovery(eq(false)); + verify(mockRunnerBuilder).p2pConfiguration(p2PConfigurationArgumentCaptor.capture()); verify(mockRunnerBuilder).build(); + assertThat(p2PConfigurationArgumentCaptor.getValue().isPeerDiscoveryEnabled()).isFalse(); + assertThat(commandOutput.toString(UTF_8)).isEmpty(); assertThat(commandErrorOutput.toString(UTF_8)).isEmpty(); } @@ -905,10 +914,11 @@ public void bannedNodeIdsOptionMustBeUsed() { Arrays.stream(nodes).map(Bytes::toShortHexString).collect(Collectors.joining(",")); parseCommand("--banned-node-ids", nodeIdsArg); - verify(mockRunnerBuilder).bannedNodeIds(bytesCollectionCollector.capture()); + verify(mockRunnerBuilder).p2pConfiguration(p2PConfigurationArgumentCaptor.capture()); verify(mockRunnerBuilder).build(); - assertThat(bytesCollectionCollector.getValue().toArray()).isEqualTo(nodes); + assertThat(p2PConfigurationArgumentCaptor.getValue().getBannedNodeIds().toArray()) + .isEqualTo(nodes); assertThat(commandOutput.toString(UTF_8)).isEmpty(); assertThat(commandErrorOutput.toString(UTF_8)).isEmpty(); @@ -939,12 +949,11 @@ public void p2pHostAndPortOptionsAreRespected() { final int port = 1234; parseCommand("--p2p-host", host, "--p2p-port", String.valueOf(port)); - verify(mockRunnerBuilder).p2pAdvertisedHost(stringArgumentCaptor.capture()); - verify(mockRunnerBuilder).p2pListenPort(intArgumentCaptor.capture()); + verify(mockRunnerBuilder).p2pConfiguration(p2PConfigurationArgumentCaptor.capture()); verify(mockRunnerBuilder).build(); - assertThat(stringArgumentCaptor.getValue()).isEqualTo(host); - assertThat(intArgumentCaptor.getValue()).isEqualTo(port); + assertThat(p2PConfigurationArgumentCaptor.getValue().getP2pHost()).isEqualTo(host); + assertThat(p2PConfigurationArgumentCaptor.getValue().getP2pPort()).isEqualTo(port); assertThat(commandOutput.toString(UTF_8)).isEmpty(); assertThat(commandErrorOutput.toString(UTF_8)).isEmpty(); @@ -956,13 +965,12 @@ public void p2pInterfaceOptionIsRespected() { final String ip = "1.2.3.4"; parseCommand("--p2p-interface", ip); - assertThat(commandOutput.toString(UTF_8)).isEmpty(); - assertThat(commandErrorOutput.toString(UTF_8)).isEmpty(); - - verify(mockRunnerBuilder).p2pListenInterface(stringArgumentCaptor.capture()); + verify(mockRunnerBuilder).p2pConfiguration(p2PConfigurationArgumentCaptor.capture()); verify(mockRunnerBuilder).build(); - assertThat(stringArgumentCaptor.getValue()).isEqualTo(ip); + assertThat(p2PConfigurationArgumentCaptor.getValue().getP2pInterface()).isEqualTo(ip); + assertThat(commandOutput.toString(UTF_8)).isEmpty(); + assertThat(commandErrorOutput.toString(UTF_8)).isEmpty(); } @Test @@ -971,10 +979,10 @@ public void p2pHostMayBeLocalhost() { final String host = "localhost"; parseCommand("--p2p-host", host); - verify(mockRunnerBuilder).p2pAdvertisedHost(stringArgumentCaptor.capture()); + verify(mockRunnerBuilder).p2pConfiguration(p2PConfigurationArgumentCaptor.capture()); verify(mockRunnerBuilder).build(); - assertThat(stringArgumentCaptor.getValue()).isEqualTo(host); + assertThat(p2PConfigurationArgumentCaptor.getValue().getP2pHost()).isEqualTo(host); assertThat(commandOutput.toString(UTF_8)).isEmpty(); assertThat(commandErrorOutput.toString(UTF_8)).isEmpty(); @@ -986,10 +994,10 @@ public void p2pHostMayBeIPv6() { final String host = "2600:DB8::8545"; parseCommand("--p2p-host", host); - verify(mockRunnerBuilder).p2pAdvertisedHost(stringArgumentCaptor.capture()); + verify(mockRunnerBuilder).p2pConfiguration(p2PConfigurationArgumentCaptor.capture()); verify(mockRunnerBuilder).build(); - assertThat(stringArgumentCaptor.getValue()).isEqualTo(host); + assertThat(p2PConfigurationArgumentCaptor.getValue().getP2pHost()).isEqualTo(host); assertThat(commandOutput.toString(UTF_8)).isEmpty(); assertThat(commandErrorOutput.toString(UTF_8)).isEmpty(); @@ -1222,12 +1230,13 @@ public void netRestrictParsedCorrectly() { final String subnet1 = "127.0.0.1/24"; final String subnet2 = "10.0.0.1/24"; parseCommand("--net-restrict", String.join(",", subnet1, subnet2)); - verify(mockRunnerBuilder).allowedSubnets(allowedSubnetsArgumentCaptor.capture()); - assertThat(allowedSubnetsArgumentCaptor.getValue().size()).isEqualTo(2); - assertThat(allowedSubnetsArgumentCaptor.getValue().get(0).getCidrSignature()) - .isEqualTo(subnet1); - assertThat(allowedSubnetsArgumentCaptor.getValue().get(1).getCidrSignature()) - .isEqualTo(subnet2); + verify(mockRunnerBuilder).p2pConfiguration(p2PConfigurationArgumentCaptor.capture()); + verify(mockRunnerBuilder).build(); + + var subnets = p2PConfigurationArgumentCaptor.getValue().getAllowedSubnets(); + assertThat(subnets.size()).isEqualTo(2); + assertThat(subnets.get(0).getCidrSignature()).isEqualTo(subnet1); + assertThat(subnets.get(1).getCidrSignature()).isEqualTo(subnet2); } @Test diff --git a/besu/src/test/java/org/hyperledger/besu/cli/CommandTestAbstract.java b/besu/src/test/java/org/hyperledger/besu/cli/CommandTestAbstract.java index f9d9b9aad78..9181336358a 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/CommandTestAbstract.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/CommandTestAbstract.java @@ -21,7 +21,6 @@ import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyLong; -import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.atLeast; import static org.mockito.Mockito.doReturn; @@ -69,6 +68,7 @@ import org.hyperledger.besu.ethereum.eth.transactions.TransactionPool; import org.hyperledger.besu.ethereum.eth.transactions.TransactionPoolConfiguration; import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule; +import org.hyperledger.besu.ethereum.p2p.config.P2PConfiguration; import org.hyperledger.besu.ethereum.permissioning.PermissioningConfiguration; import org.hyperledger.besu.ethereum.storage.StorageProvider; import org.hyperledger.besu.ethereum.worldstate.DataStorageConfiguration; @@ -253,6 +253,7 @@ public abstract class CommandTestAbstract { @Captor protected ArgumentCaptor ethProtocolConfigurationArgumentCaptor; @Captor protected ArgumentCaptor dataStorageConfigurationArgumentCaptor; @Captor protected ArgumentCaptor pkiKeyStoreConfigurationArgumentCaptor; + @Captor protected ArgumentCaptor p2PConfigurationArgumentCaptor; @Captor protected ArgumentCaptor> @@ -351,7 +352,6 @@ public void initMocks() throws Exception { when(mockRunnerBuilder.legacyForkId(anyBoolean())).thenReturn(mockRunnerBuilder); when(mockRunnerBuilder.apiConfiguration(any())).thenReturn(mockRunnerBuilder); when(mockRunnerBuilder.enodeDnsConfiguration(any())).thenReturn(mockRunnerBuilder); - when(mockRunnerBuilder.allowedSubnets(any())).thenReturn(mockRunnerBuilder); when(mockRunnerBuilder.build()).thenReturn(mockRunner); final SignatureAlgorithm signatureAlgorithm = SignatureAlgorithmFactory.getInstance(); diff --git a/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/config/P2PConfiguration.java b/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/config/P2PConfiguration.java index 7040f8b3ee9..2cf9c6fe5bf 100644 --- a/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/config/P2PConfiguration.java +++ b/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/config/P2PConfiguration.java @@ -19,6 +19,7 @@ import java.util.Collection; import java.util.List; +import org.apache.commons.net.util.SubnetUtils.SubnetInfo; import org.apache.tuweni.bytes.Bytes; public class P2PConfiguration { @@ -35,20 +36,22 @@ public class P2PConfiguration { private final String discoveryDnsUrl; private final boolean randomPeerPriority; private final Collection bannedNodeIds; + private final List allowedSubnets; public P2PConfiguration( - final boolean p2pEnabled, - final boolean peerDiscoveryEnabled, - final List bootNodes, - final String p2pHost, - final String p2pInterface, - final int p2pPort, - final int maxPeers, - final boolean isLimitRemoteWireConnectionsEnabled, - final Percentage maxRemoteConnectionsPercentage, - final String discoveryDnsUrl, - final boolean randomPeerPriority, - final Collection bannedNodeIds) { + final boolean p2pEnabled, + final boolean peerDiscoveryEnabled, + final List bootNodes, + final String p2pHost, + final String p2pInterface, + final int p2pPort, + final int maxPeers, + final boolean isLimitRemoteWireConnectionsEnabled, + final Percentage maxRemoteConnectionsPercentage, + final String discoveryDnsUrl, + final boolean randomPeerPriority, + final Collection bannedNodeIds, + final List allowedSubnets) { this.p2pEnabled = p2pEnabled; this.peerDiscoveryEnabled = peerDiscoveryEnabled; this.bootNodes = bootNodes; @@ -61,6 +64,7 @@ public P2PConfiguration( this.discoveryDnsUrl = discoveryDnsUrl; this.randomPeerPriority = randomPeerPriority; this.bannedNodeIds = bannedNodeIds; + this.allowedSubnets = allowedSubnets; } public boolean isP2pEnabled() { @@ -111,6 +115,14 @@ public Collection getBannedNodeIds() { return bannedNodeIds; } + public List getAllowedSubnets() { + return allowedSubnets; + } + + public static P2PConfiguration.Builder builder() { + return new P2PConfiguration.Builder(); + } + public static class Builder { private boolean p2pEnabled = true; private boolean peerDiscoveryEnabled = true; @@ -124,6 +136,7 @@ public static class Builder { private String discoveryDnsUrl; private boolean randomPeerPriority = false; private Collection bannedNodeIds; + private List allowedSubnets; public Builder p2pEnabled(final boolean p2pEnabled) { this.p2pEnabled = p2pEnabled; @@ -161,7 +174,7 @@ public Builder maxPeers(final int maxPeers) { } public Builder isLimitRemoteWireConnectionsEnabled( - final boolean isLimitRemoteWireConnectionsEnabled) { + final boolean isLimitRemoteWireConnectionsEnabled) { this.isLimitRemoteWireConnectionsEnabled = isLimitRemoteWireConnectionsEnabled; return this; } @@ -186,6 +199,11 @@ public Builder bannedNodeIds(final Collection bannedNodeIds) { return this; } + public Builder allowedSubnets(final List allowedSubnets) { + this.allowedSubnets = allowedSubnets; + return this; + } + public P2PConfiguration build() { return new P2PConfiguration( p2pEnabled, @@ -199,7 +217,8 @@ public P2PConfiguration build() { maxRemoteConnectionsPercentage, discoveryDnsUrl, randomPeerPriority, - bannedNodeIds); + bannedNodeIds, + allowedSubnets); } } } From 4ec55c649da4fc1f4865374cc5262f8b3632dba0 Mon Sep 17 00:00:00 2001 From: Gabriel-Trintinalia Date: Tue, 18 Jun 2024 15:50:24 +1000 Subject: [PATCH 3/7] Renaming Signed-off-by: Gabriel-Trintinalia --- .../tests/acceptance/dsl/node/BesuNode.java | 6 ++--- .../node/configuration/BesuNodeFactory.java | 2 +- .../org/hyperledger/besu/RunnerBuilder.java | 16 ++++++------ .../org/hyperledger/besu/cli/BesuCommand.java | 9 +++---- .../hyperledger/besu/cli/BesuCommandTest.java | 12 ++++----- .../ethereum/p2p/config/P2PConfiguration.java | 26 +++++++++---------- 6 files changed, 35 insertions(+), 36 deletions(-) diff --git a/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/BesuNode.java b/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/BesuNode.java index 07b348d0bf4..2cf7410669b 100644 --- a/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/BesuNode.java +++ b/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/BesuNode.java @@ -286,7 +286,7 @@ public URI enodeUrl() { } public String getP2pPort() { - return String.valueOf(p2PConfiguration.getP2pPort()); + return String.valueOf(p2PConfiguration.getPort()); } private String getRuntimeP2pPort() { @@ -716,7 +716,7 @@ public boolean isAltbn128Native() { @Override public boolean isDiscoveryEnabled() { - return p2PConfiguration.isPeerDiscoveryEnabled(); + return p2PConfiguration.isDiscoveryEnabled(); } Optional getPermissioningConfiguration() { @@ -770,7 +770,7 @@ public String toString() { .add("homeDirectory", homeDirectory) .add("keyPair", keyPair) .add("p2pEnabled", p2PConfiguration.isP2pEnabled()) - .add("discoveryEnabled", p2PConfiguration.isPeerDiscoveryEnabled()) + .add("discoveryEnabled", p2PConfiguration.isDiscoveryEnabled()) .add("privacyEnabled", privacyParameters.isEnabled()) .toString(); } diff --git a/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/configuration/BesuNodeFactory.java b/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/configuration/BesuNodeFactory.java index b46697d8e9c..b1807d4cb65 100644 --- a/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/configuration/BesuNodeFactory.java +++ b/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/configuration/BesuNodeFactory.java @@ -180,9 +180,9 @@ public BesuNode createArchiveNodeWithDiscoveryDisabledAndAdmin(final String name return create( new BesuNodeConfigurationBuilder() .name(name) - .p2pConfiguration(P2PConfiguration.builder().peerDiscoveryEnabled(false).build()) .jsonRpcConfiguration(node.jsonRpcConfigWithAdmin()) .webSocketEnabled() + .p2pConfiguration(P2PConfiguration.builder().peerDiscoveryEnabled(false).build()) .build()); } diff --git a/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java b/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java index 29647e6daf9..b1df9f919fb 100644 --- a/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java +++ b/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java @@ -147,7 +147,6 @@ import graphql.GraphQL; import io.vertx.core.Vertx; import io.vertx.core.VertxOptions; -import org.apache.commons.net.util.SubnetUtils.SubnetInfo; import org.apache.tuweni.bytes.Bytes; import org.apache.tuweni.units.bigints.UInt256; import org.slf4j.Logger; @@ -545,9 +544,9 @@ public Runner build() { final DiscoveryConfiguration discoveryConfiguration = DiscoveryConfiguration.create() .setBindHost(p2pConfiguration.getP2pInterface()) - .setBindPort(p2pConfiguration.getP2pPort()) - .setAdvertisedHost(p2pConfiguration.getP2pHost()); - if (p2pConfiguration.isPeerDiscoveryEnabled()) { + .setBindPort(p2pConfiguration.getPort()) + .setAdvertisedHost(p2pConfiguration.getHost()); + if (p2pConfiguration.isDiscoveryEnabled()) { final List bootstrap; if (ethNetworkConfig.bootNodes() == null) { bootstrap = EthNetworkConfig.getNetworkConfig(NetworkName.MAINNET).bootNodes(); @@ -584,7 +583,7 @@ public Runner build() { final RlpxConfiguration rlpxConfiguration = RlpxConfiguration.create() .setBindHost(p2pConfiguration.getP2pInterface()) - .setBindPort(p2pConfiguration.getP2pPort()) + .setBindPort(p2pConfiguration.getPort()) .setSupportedProtocols(subProtocols) .setClientId(BesuInfo.nodeName(identityString)); networkingConfiguration.setRlpx(rlpxConfiguration).setDiscovery(discoveryConfiguration); @@ -592,7 +591,8 @@ public Runner build() { final PeerPermissionsDenylist bannedNodes = PeerPermissionsDenylist.create(); p2pConfiguration.getBannedNodeIds().forEach(bannedNodes::add); - PeerPermissionSubnet peerPermissionSubnet = new PeerPermissionSubnet(p2pConfiguration.getAllowedSubnets()); + PeerPermissionSubnet peerPermissionSubnet = + new PeerPermissionSubnet(p2pConfiguration.getAllowedSubnets()); final PeerPermissions defaultPeerPermissions = PeerPermissions.combine(peerPermissionSubnet, bannedNodes); @@ -1104,8 +1104,8 @@ private Optional buildNatManager(final NatMethod natMethod) { case DOCKER: return Optional.of( new DockerNatManager( - p2pConfiguration.getP2pHost(), - p2pConfiguration.getP2pPort(), + p2pConfiguration.getHost(), + p2pConfiguration.getPort(), jsonRpcConfiguration.getPort())); case KUBERNETES: return Optional.of(new KubernetesNatManager(natManagerServiceName)); diff --git a/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java b/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java index 1ac4e432088..f69c7773711 100644 --- a/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java +++ b/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java @@ -2284,7 +2284,7 @@ private EthNetworkConfig updateNetworkConfig(final NetworkName network) { } } if (listBootNodes != null) { - if (!p2pConfiguration.isPeerDiscoveryEnabled()) { + if (!p2pConfiguration.isDiscoveryEnabled()) { logger.warn("Discovery disabled: bootnodes will be ignored."); } DiscoveryConfiguration.assertValidBootnodes(listBootNodes); @@ -2419,12 +2419,12 @@ protected void checkIfRequiredPortsAreAvailable() { .filter(port -> port > 0) .forEach( port -> { - if (port.equals(p2pConfiguration.getP2pPort()) + if (port.equals(p2pConfiguration.getPort()) && (NetworkUtility.isPortUnavailableForTcp(port) || NetworkUtility.isPortUnavailableForUdp(port))) { unavailablePorts.add(port); } - if (!port.equals(p2pConfiguration.getP2pPort()) + if (!port.equals(p2pConfiguration.getPort()) && NetworkUtility.isPortUnavailableForTcp(port)) { unavailablePorts.add(port); } @@ -2444,8 +2444,7 @@ protected void checkIfRequiredPortsAreAvailable() { */ private List getEffectivePorts() { final List effectivePorts = new ArrayList<>(); - addPortIfEnabled( - effectivePorts, p2pConfiguration.getP2pPort(), p2pConfiguration.isP2pEnabled()); + addPortIfEnabled(effectivePorts, p2pConfiguration.getPort(), p2pConfiguration.isP2pEnabled()); addPortIfEnabled( effectivePorts, graphQlOptions.getGraphQLHttpPort(), graphQlOptions.isGraphQLHttpEnabled()); addPortIfEnabled( diff --git a/besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java b/besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java index dd8ee1d14ce..9bcc659782c 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java @@ -735,7 +735,7 @@ public void discoveryOptionValueTrueMustBeUsed() { verify(mockRunnerBuilder).p2pConfiguration(p2PConfigurationArgumentCaptor.capture()); verify(mockRunnerBuilder).build(); - assertThat(p2PConfigurationArgumentCaptor.getValue().isPeerDiscoveryEnabled()).isTrue(); + assertThat(p2PConfigurationArgumentCaptor.getValue().isDiscoveryEnabled()).isTrue(); assertThat(commandOutput.toString(UTF_8)).isEmpty(); assertThat(commandErrorOutput.toString(UTF_8)).isEmpty(); @@ -748,7 +748,7 @@ public void discoveryOptionValueFalseMustBeUsed() { verify(mockRunnerBuilder).p2pConfiguration(p2PConfigurationArgumentCaptor.capture()); verify(mockRunnerBuilder).build(); - assertThat(p2PConfigurationArgumentCaptor.getValue().isPeerDiscoveryEnabled()).isFalse(); + assertThat(p2PConfigurationArgumentCaptor.getValue().isDiscoveryEnabled()).isFalse(); assertThat(commandOutput.toString(UTF_8)).isEmpty(); assertThat(commandErrorOutput.toString(UTF_8)).isEmpty(); @@ -952,8 +952,8 @@ public void p2pHostAndPortOptionsAreRespected() { verify(mockRunnerBuilder).p2pConfiguration(p2PConfigurationArgumentCaptor.capture()); verify(mockRunnerBuilder).build(); - assertThat(p2PConfigurationArgumentCaptor.getValue().getP2pHost()).isEqualTo(host); - assertThat(p2PConfigurationArgumentCaptor.getValue().getP2pPort()).isEqualTo(port); + assertThat(p2PConfigurationArgumentCaptor.getValue().getHost()).isEqualTo(host); + assertThat(p2PConfigurationArgumentCaptor.getValue().getPort()).isEqualTo(port); assertThat(commandOutput.toString(UTF_8)).isEmpty(); assertThat(commandErrorOutput.toString(UTF_8)).isEmpty(); @@ -982,7 +982,7 @@ public void p2pHostMayBeLocalhost() { verify(mockRunnerBuilder).p2pConfiguration(p2PConfigurationArgumentCaptor.capture()); verify(mockRunnerBuilder).build(); - assertThat(p2PConfigurationArgumentCaptor.getValue().getP2pHost()).isEqualTo(host); + assertThat(p2PConfigurationArgumentCaptor.getValue().getHost()).isEqualTo(host); assertThat(commandOutput.toString(UTF_8)).isEmpty(); assertThat(commandErrorOutput.toString(UTF_8)).isEmpty(); @@ -997,7 +997,7 @@ public void p2pHostMayBeIPv6() { verify(mockRunnerBuilder).p2pConfiguration(p2PConfigurationArgumentCaptor.capture()); verify(mockRunnerBuilder).build(); - assertThat(p2PConfigurationArgumentCaptor.getValue().getP2pHost()).isEqualTo(host); + assertThat(p2PConfigurationArgumentCaptor.getValue().getHost()).isEqualTo(host); assertThat(commandOutput.toString(UTF_8)).isEmpty(); assertThat(commandErrorOutput.toString(UTF_8)).isEmpty(); diff --git a/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/config/P2PConfiguration.java b/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/config/P2PConfiguration.java index 2cf9c6fe5bf..7001bdad75b 100644 --- a/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/config/P2PConfiguration.java +++ b/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/config/P2PConfiguration.java @@ -25,11 +25,11 @@ public class P2PConfiguration { // Public IP stored to prevent having to research it each time we need it. private final boolean p2pEnabled; - private final boolean peerDiscoveryEnabled; + private final boolean discoveryEnabled; private final List bootNodes; - private final String p2pHost; + private final String host; private final String p2pInterface; - private final int p2pPort; + private final int port; private final int maxPeers; private final boolean isLimitRemoteWireConnectionsEnabled; private final Percentage maxRemoteConnectionsPercentage; @@ -42,7 +42,7 @@ public P2PConfiguration( final boolean p2pEnabled, final boolean peerDiscoveryEnabled, final List bootNodes, - final String p2pHost, + final String host, final String p2pInterface, final int p2pPort, final int maxPeers, @@ -53,11 +53,11 @@ public P2PConfiguration( final Collection bannedNodeIds, final List allowedSubnets) { this.p2pEnabled = p2pEnabled; - this.peerDiscoveryEnabled = peerDiscoveryEnabled; + this.discoveryEnabled = peerDiscoveryEnabled; this.bootNodes = bootNodes; - this.p2pHost = p2pHost; + this.host = host; this.p2pInterface = p2pInterface; - this.p2pPort = p2pPort; + this.port = p2pPort; this.maxPeers = maxPeers; this.isLimitRemoteWireConnectionsEnabled = isLimitRemoteWireConnectionsEnabled; this.maxRemoteConnectionsPercentage = maxRemoteConnectionsPercentage; @@ -71,24 +71,24 @@ public boolean isP2pEnabled() { return p2pEnabled; } - public boolean isPeerDiscoveryEnabled() { - return peerDiscoveryEnabled; + public boolean isDiscoveryEnabled() { + return discoveryEnabled; } public List getBootNodes() { return bootNodes; } - public String getP2pHost() { - return p2pHost; + public String getHost() { + return host; } public String getP2pInterface() { return p2pInterface; } - public int getP2pPort() { - return p2pPort; + public int getPort() { + return port; } public int getMaxPeers() { From 8c78b6a65e82f7138b99987650448f3bca22c6bc Mon Sep 17 00:00:00 2001 From: Gabriel-Trintinalia Date: Wed, 19 Jun 2024 13:59:14 +1000 Subject: [PATCH 4/7] Fix tests Signed-off-by: Gabriel-Trintinalia --- .../dsl/node/ThreadBesuNodeRunner.java | 11 +- .../configuration/BesuNodeConfiguration.java | 2 +- .../config/BootNodesGenesisSetupTest.java | 3 +- .../org/hyperledger/besu/RunnerBuilder.java | 32 ++--- .../org/hyperledger/besu/cli/BesuCommand.java | 108 ++++----------- .../besu/cli/options/stable/P2POptions.java | 68 ++++++++- .../controller/BesuControllerBuilder.java | 44 ++---- .../hyperledger/besu/RunnerBuilderTest.java | 12 +- .../java/org/hyperledger/besu/RunnerTest.java | 14 +- .../hyperledger/besu/cli/BesuCommandTest.java | 53 ++++--- .../cli/CascadingDefaultProviderTest.java | 18 ++- .../besu/cli/CommandTestAbstract.java | 11 +- .../ethereum/p2p/config/P2PConfiguration.java | 131 +++++++++++++++--- 13 files changed, 294 insertions(+), 213 deletions(-) diff --git a/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/ThreadBesuNodeRunner.java b/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/ThreadBesuNodeRunner.java index 7f7cf64e922..c6ac5ceb074 100644 --- a/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/ThreadBesuNodeRunner.java +++ b/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/ThreadBesuNodeRunner.java @@ -35,6 +35,7 @@ import org.hyperledger.besu.ethereum.eth.sync.SynchronizerConfiguration; import org.hyperledger.besu.ethereum.eth.transactions.ImmutableTransactionPoolConfiguration; import org.hyperledger.besu.ethereum.eth.transactions.TransactionPoolConfiguration; +import org.hyperledger.besu.ethereum.p2p.config.P2PConfiguration; import org.hyperledger.besu.ethereum.p2p.peers.EnodeURLImpl; import org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueStorageProvider; import org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueStorageProviderBuilder; @@ -236,8 +237,6 @@ public void startNode(final BesuNode node) { .transactionPoolValidatorService(transactionPoolValidatorServiceImpl) .build(); - final int maxPeers = 25; - builder .synchronizerConfiguration(new SynchronizerConfiguration.Builder().build()) .dataDirectory(node.homeDirectory()) @@ -253,10 +252,8 @@ public void startNode(final BesuNode node) { .storageProvider(storageProvider) .gasLimitCalculator(GasLimitCalculator.constant()) .evmConfiguration(EvmConfiguration.DEFAULT) - .maxPeers(maxPeers) - .maxRemotelyInitiatedPeers(15) - .networkConfiguration(node.getNetworkingConfiguration()) - .randomPeerPriority(false); + .p2PConfiguration(P2PConfiguration.createDefault()) + .networkConfiguration(node.getNetworkingConfiguration()); node.getGenesisConfig() .map(GenesisConfigFile::fromConfig) @@ -276,7 +273,7 @@ public void startNode(final BesuNode node) { .vertx(Vertx.vertx()) .besuController(besuController) .ethNetworkConfig(ethNetworkConfig) - .p2pConfiguration(node.getP2PConfiguration()) + .p2PConfiguration(node.getP2PConfiguration()) .networkingConfiguration(node.getNetworkingConfiguration()) .jsonRpcConfiguration(node.jsonRpcConfiguration()) .webSocketConfiguration(node.webSocketConfiguration()) diff --git a/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/configuration/BesuNodeConfiguration.java b/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/configuration/BesuNodeConfiguration.java index 63ff5e99f78..e4b9c0fd9db 100644 --- a/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/configuration/BesuNodeConfiguration.java +++ b/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/configuration/BesuNodeConfiguration.java @@ -54,7 +54,7 @@ public class BesuNodeConfiguration { private final Optional keyFilePath; private final boolean devMode; private final GenesisConfigurationProvider genesisConfigProvider; - private P2PConfiguration p2pConfiguration; + private final P2PConfiguration p2pConfiguration; private final Optional tlsConfiguration; private final NetworkingConfiguration networkingConfiguration; private final boolean bootnodeEligible; diff --git a/acceptance-tests/tests/src/test/java/org/hyperledger/besu/tests/acceptance/config/BootNodesGenesisSetupTest.java b/acceptance-tests/tests/src/test/java/org/hyperledger/besu/tests/acceptance/config/BootNodesGenesisSetupTest.java index 616afc19669..a2db6670908 100644 --- a/acceptance-tests/tests/src/test/java/org/hyperledger/besu/tests/acceptance/config/BootNodesGenesisSetupTest.java +++ b/acceptance-tests/tests/src/test/java/org/hyperledger/besu/tests/acceptance/config/BootNodesGenesisSetupTest.java @@ -18,6 +18,7 @@ import org.hyperledger.besu.crypto.SECPPrivateKey; import org.hyperledger.besu.crypto.SECPPublicKey; import org.hyperledger.besu.crypto.SignatureAlgorithm; +import org.hyperledger.besu.ethereum.p2p.config.P2PConfiguration; import org.hyperledger.besu.tests.acceptance.dsl.AcceptanceTestBase; import org.hyperledger.besu.tests.acceptance.dsl.node.Node; import org.hyperledger.besu.tests.acceptance.dsl.node.configuration.BesuNodeConfigurationBuilder; @@ -108,7 +109,7 @@ private BesuNodeConfigurationBuilder configureNode( return nodeBuilder .devMode(false) .keyPair(keyPair) - .p2pPort(p2pBindingPort) + .p2pConfiguration(P2PConfiguration.builder().p2pPort(p2pBindingPort).build()) .genesisConfigProvider( (nodes) -> Optional.of( diff --git a/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java b/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java index b1df9f919fb..09cd6c622af 100644 --- a/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java +++ b/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java @@ -161,7 +161,7 @@ public class RunnerBuilder { private BesuController besuController; private NetworkingConfiguration networkingConfiguration = NetworkingConfiguration.create(); - private P2PConfiguration p2pConfiguration; + private P2PConfiguration p2PConfiguration; private Optional p2pTLSConfiguration = Optional.empty(); private NatMethod natMethod = NatMethod.AUTO; private String natManagerServiceName; @@ -217,14 +217,14 @@ public RunnerBuilder besuController(final BesuController besuController) { /** * TLSConfiguration p2pTLSConfiguration. * - * @param p2pConfiguration the TLSConfiguration p2pTLSConfiguration + * @param p2PConfiguration the TLSConfiguration p2pTLSConfiguration * @return the runner builder */ - public RunnerBuilder p2pConfiguration(final P2PConfiguration p2pConfiguration) { + public RunnerBuilder p2PConfiguration(final P2PConfiguration p2PConfiguration) { checkArgument( - !isNull(p2pConfiguration.getP2pInterface()), + !isNull(p2PConfiguration.getP2pInterface()), "Invalid null value supplied for p2pListenInterface"); - this.p2pConfiguration = p2pConfiguration; + this.p2PConfiguration = p2PConfiguration; return this; } @@ -543,10 +543,10 @@ public Runner build() { final DiscoveryConfiguration discoveryConfiguration = DiscoveryConfiguration.create() - .setBindHost(p2pConfiguration.getP2pInterface()) - .setBindPort(p2pConfiguration.getPort()) - .setAdvertisedHost(p2pConfiguration.getHost()); - if (p2pConfiguration.isDiscoveryEnabled()) { + .setBindHost(p2PConfiguration.getP2pInterface()) + .setBindPort(p2PConfiguration.getPort()) + .setAdvertisedHost(p2PConfiguration.getHost()); + if (p2PConfiguration.isDiscoveryEnabled()) { final List bootstrap; if (ethNetworkConfig.bootNodes() == null) { bootstrap = EthNetworkConfig.getNetworkConfig(NetworkName.MAINNET).bootNodes(); @@ -582,17 +582,17 @@ public Runner build() { final RlpxConfiguration rlpxConfiguration = RlpxConfiguration.create() - .setBindHost(p2pConfiguration.getP2pInterface()) - .setBindPort(p2pConfiguration.getPort()) + .setBindHost(p2PConfiguration.getP2pInterface()) + .setBindPort(p2PConfiguration.getPort()) .setSupportedProtocols(subProtocols) .setClientId(BesuInfo.nodeName(identityString)); networkingConfiguration.setRlpx(rlpxConfiguration).setDiscovery(discoveryConfiguration); final PeerPermissionsDenylist bannedNodes = PeerPermissionsDenylist.create(); - p2pConfiguration.getBannedNodeIds().forEach(bannedNodes::add); + p2PConfiguration.getBannedNodeIds().forEach(bannedNodes::add); PeerPermissionSubnet peerPermissionSubnet = - new PeerPermissionSubnet(p2pConfiguration.getAllowedSubnets()); + new PeerPermissionSubnet(p2PConfiguration.getAllowedSubnets()); final PeerPermissions defaultPeerPermissions = PeerPermissions.combine(peerPermissionSubnet, bannedNodes); @@ -649,7 +649,7 @@ public Runner build() { NetworkRunner.builder() .protocolManagers(protocolManagers) .subProtocols(subProtocols) - .network(p2pConfiguration.isP2pEnabled() ? activeNetwork : inactiveNetwork) + .network(p2PConfiguration.isP2pEnabled() ? activeNetwork : inactiveNetwork) .metricsSystem(metricsSystem) .build(); @@ -1104,8 +1104,8 @@ private Optional buildNatManager(final NatMethod natMethod) { case DOCKER: return Optional.of( new DockerNatManager( - p2pConfiguration.getHost(), - p2pConfiguration.getPort(), + p2PConfiguration.getHost(), + p2PConfiguration.getPort(), jsonRpcConfiguration.getPort())); case KUBERNETES: return Optional.of(new KubernetesNatManager(natManagerServiceName)); diff --git a/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java b/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java index f69c7773711..1f915d953dc 100644 --- a/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java +++ b/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java @@ -15,12 +15,10 @@ package org.hyperledger.besu.cli; import static com.google.common.base.Preconditions.checkNotNull; -import static com.google.common.base.Preconditions.checkState; import static java.util.Arrays.asList; import static java.util.Collections.singletonList; import static org.hyperledger.besu.cli.DefaultCommandValues.getDefaultBesuDataPath; import static org.hyperledger.besu.cli.config.NetworkName.MAINNET; -import static org.hyperledger.besu.cli.options.unstable.NetworkingOptions.PEER_LOWER_BOUND_FLAG; import static org.hyperledger.besu.cli.util.CommandLineUtils.DEPENDENCY_WARNING_MSG; import static org.hyperledger.besu.cli.util.CommandLineUtils.isOptionSet; import static org.hyperledger.besu.controller.BesuController.DATABASE_PATH; @@ -205,7 +203,6 @@ import org.hyperledger.besu.util.LogConfigurator; import org.hyperledger.besu.util.NetworkUtility; import org.hyperledger.besu.util.PermissioningConfigurationValidator; -import org.hyperledger.besu.util.number.Fraction; import org.hyperledger.besu.util.number.Percentage; import org.hyperledger.besu.util.number.PositiveNumber; @@ -213,10 +210,8 @@ import java.io.IOException; import java.io.InputStream; import java.math.BigInteger; -import java.net.SocketException; import java.net.URI; import java.net.URL; -import java.net.UnknownHostException; import java.nio.file.Path; import java.time.Clock; import java.util.ArrayList; @@ -341,9 +336,6 @@ public class BesuCommand implements DefaultCommandValues, Runnable { private RocksDBPlugin rocksDBPlugin; - private int maxPeers; - private int maxRemoteInitiatedPeers; - // CLI options defined by user at runtime. // Options parsing is done with CLI library Picocli https://picocli.info/ @@ -388,7 +380,7 @@ public class BesuCommand implements DefaultCommandValues, Runnable { // P2P Discovery Option Group @CommandLine.ArgGroup(validate = false, heading = "@|bold P2P Discovery Options|@%n") - P2POptions p2PDiscoveryOptionGroup = new P2POptions(); + P2POptions p2POptions = new P2POptions(); private final TransactionSelectionServiceImpl transactionSelectionServiceImpl; private final TransactionPoolValidatorServiceImpl transactionValidatorServiceImpl; @@ -761,9 +753,7 @@ static class MetricsOptionGroup { PluginsConfigurationOptions pluginsConfigurationOptions = new PluginsConfigurationOptions(); private EthNetworkConfig ethNetworkConfig; - private P2PConfiguration p2pConfiguration; - private JsonRpcConfiguration jsonRpcConfiguration; private JsonRpcConfiguration engineJsonRpcConfiguration; private GraphQLConfiguration graphQLConfiguration; @@ -1386,12 +1376,11 @@ private void configureNativeLibs() { private void validateOptions() { validateRequiredOptions(); issueOptionWarnings(); - validateP2PInterface(p2pConfiguration.getP2pInterface()); + validateP2POptions(); validateMiningParams(); validateNatParams(); validateNetStatsParams(); validateDnsOptionsParams(); - ensureValidPeerBoundParams(); validateRpcOptionsParams(); validateRpcWsOptions(); validateChainDataPruningParams(); @@ -1421,6 +1410,16 @@ private void validateConsensusSyncCompatibilityOptions() { } } + private void validateP2POptions() { + p2POptions.validate(commandLine, logger); + validateP2PInterface(); + } + + /** Validates P2P interface IP address/host name. Visible for testing. */ + protected void validateP2PInterface() { + p2POptions.validateP2PInterface(commandLine, logger); + } + private void validateApiOptions() { apiConfigurationOptions.validate(commandLine, logger); } @@ -1451,22 +1450,6 @@ private void validateMiningParams() { commandLine, genesisConfigOptionsSupplier.get(), isMergeEnabled(), logger); } - /** - * Validates P2P interface IP address/host name. Visible for testing. - * - * @param p2pInterface IP Address/host name - */ - protected void validateP2PInterface(final String p2pInterface) { - final String failMessage = "The provided --p2p-interface is not available: " + p2pInterface; - try { - if (!NetworkUtility.isNetworkInterfaceAvailable(p2pInterface)) { - throw new ParameterException(commandLine, failMessage); - } - } catch (final UnknownHostException | SocketException e) { - throw new ParameterException(commandLine, failMessage, e); - } - } - private void validateGraphQlOptions() { graphQlOptions.validate(logger, commandLine); } @@ -1509,25 +1492,6 @@ private void validateDnsOptionsParams() { } } - private void ensureValidPeerBoundParams() { - maxPeers = p2pConfiguration.getMaxPeers(); - final Boolean isLimitRemoteWireConnectionsEnabled = - p2pConfiguration.isLimitRemoteWireConnectionsEnabled(); - if (isOptionSet(commandLine, PEER_LOWER_BOUND_FLAG)) { - logger.warn(PEER_LOWER_BOUND_FLAG + " is deprecated and will be removed soon."); - } - if (isLimitRemoteWireConnectionsEnabled) { - final float fraction = - Fraction.fromPercentage(p2pConfiguration.getMaxRemoteConnectionsPercentage()).getValue(); - checkState( - fraction >= 0.0 && fraction <= 1.0, - "Fraction of remote connections allowed must be between 0.0 and 1.0 (inclusive)."); - maxRemoteInitiatedPeers = Math.round(fraction * maxPeers); - } else { - maxRemoteInitiatedPeers = maxPeers; - } - } - private void validateRpcOptionsParams() { final Predicate configuredApis = apiName -> @@ -1574,24 +1538,6 @@ private GenesisConfigOptions readGenesisConfigOptions() { } private void issueOptionWarnings() { - - // Check that P2P options are able to work - CommandLineUtils.checkOptionDependencies( - logger, - commandLine, - "--p2p-enabled", - !p2pConfiguration.isP2pEnabled(), - asList( - "--bootnodes", - "--discovery-enabled", - "--max-peers", - "--banned-node-id", - "--banned-node-ids", - "--p2p-host", - "--p2p-interface", - "--p2p-port", - "--remote-connections-max-percentage")); - if (SyncMode.isFullSync(getDefaultSyncModeIfNotSet()) && isOptionSet(commandLine, "--sync-min-peers")) { logger.warn("--sync-min-peers is ignored in FULL sync-mode"); @@ -1621,13 +1567,14 @@ && isOptionSet(commandLine, "--sync-min-peers")) { } private void configure() throws Exception { + p2pConfiguration = p2POptions.toDomainObject(); + checkPortClash(); checkIfRequiredPortsAreAvailable(); syncMode = getDefaultSyncModeIfNotSet(); versionCompatibilityProtection = getDefaultVersionCompatibilityProtectionIfNotSet(); - p2pConfiguration = p2PDiscoveryOptionGroup.toDomainObject(); - ethNetworkConfig = updateNetworkConfig(network); + ethNetworkConfig = updateNetworkConfig(network, p2pConfiguration); jsonRpcConfiguration = jsonRpcHttpOptions.jsonRpcConfiguration( hostsAllowlist, @@ -1744,8 +1691,11 @@ public BesuControllerBuilder getControllerBuilder() { .withMiningParameters(miningParametersSupplier.get()) .withJsonRpcHttpOptions(jsonRpcHttpOptions); final KeyValueStorageProvider storageProvider = keyValueStorageProvider(keyValueStorageName); + final P2PConfiguration p2PConfiguration = p2POptions.toDomainObject(); return controllerBuilderFactory - .fromEthNetworkConfig(updateNetworkConfig(network), getDefaultSyncModeIfNotSet()) + .fromEthNetworkConfig( + updateNetworkConfig(network, p2PConfiguration), getDefaultSyncModeIfNotSet()) + .p2PConfiguration(p2PConfiguration) .synchronizerConfiguration(buildSyncConfig()) .ethProtocolConfiguration(unstableEthProtocolOptions.toDomainObject()) .networkConfiguration(unstableNetworkingOptions.toDomainObject()) @@ -1768,9 +1718,6 @@ public BesuControllerBuilder getControllerBuilder() { .requiredBlocks(requiredBlocks) .reorgLoggingThreshold(reorgLoggingThreshold) .evmConfiguration(unstableEvmOptions.toDomainObject()) - .maxPeers(p2pConfiguration.getMaxPeers()) - .maxRemotelyInitiatedPeers(maxRemoteInitiatedPeers) - .randomPeerPriority(p2pConfiguration.isRandomPeerPriority()) .chainPruningConfiguration(unstableChainPruningOptions.toDomainObject()) .cacheLastBlocks(numberOfblocksToCache) .genesisStateHashCacheEnabled(genesisStateHashCacheEnabled); @@ -2148,7 +2095,6 @@ private Runner synchronize( runnerBuilder .vertx(vertx) .besuController(controller) - .p2pConfiguration(p2pConfiguration) .natMethod(natMethod) .natManagerServiceName(unstableNatOptions.getNatManagerServiceName()) .natMethodFallbackEnabled(unstableNatOptions.getNatMethodFallbackEnabled()) @@ -2175,6 +2121,7 @@ private Runner synchronize( .storageProvider(keyValueStorageProvider(keyValueStorageName)) .rpcEndpointService(rpcEndpointServiceImpl) .enodeDnsConfiguration(getEnodeDnsConfiguration()) + .p2PConfiguration(p2pConfiguration) .build(); addShutdownHook(runner); @@ -2218,7 +2165,8 @@ private void addShutdownHook(final Runner runner) { "BesuCommand-Shutdown-Hook")); } - private EthNetworkConfig updateNetworkConfig(final NetworkName network) { + private EthNetworkConfig updateNetworkConfig( + final NetworkName network, final P2PConfiguration p2pConfig) { final EthNetworkConfig.Builder builder = new EthNetworkConfig.Builder(EthNetworkConfig.getNetworkConfig(network)); @@ -2249,7 +2197,7 @@ private EthNetworkConfig updateNetworkConfig(final NetworkName network) { } } - if (p2pConfiguration.getBootNodes() == null) { + if (p2pConfig.getBootNodes() == null) { builder.setBootNodes(new ArrayList<>()); } builder.setDnsDiscoveryUrl(null); @@ -2261,8 +2209,8 @@ private EthNetworkConfig updateNetworkConfig(final NetworkName network) { builder.setNetworkId(networkId); } - if (p2pConfiguration.getDiscoveryDnsUrl() != null) { - builder.setDnsDiscoveryUrl(p2pConfiguration.getDiscoveryDnsUrl()); + if (p2pConfig.getDiscoveryDnsUrl() != null) { + builder.setDnsDiscoveryUrl(p2pConfig.getDiscoveryDnsUrl()); } else { final Optional discoveryDnsUrlFromGenesis = genesisConfigOptionsSupplier.get().getDiscoveryOptions().getDiscoveryDnsUrl(); @@ -2270,9 +2218,9 @@ private EthNetworkConfig updateNetworkConfig(final NetworkName network) { } List listBootNodes = null; - if (p2pConfiguration.getBootNodes() != null) { + if (p2pConfig.getBootNodes() != null) { try { - listBootNodes = buildEnodes(p2pConfiguration.getBootNodes(), getEnodeDnsConfiguration()); + listBootNodes = buildEnodes(p2pConfig.getBootNodes(), getEnodeDnsConfiguration()); } catch (final IllegalArgumentException e) { throw new ParameterException(commandLine, e.getMessage()); } @@ -2284,7 +2232,7 @@ private EthNetworkConfig updateNetworkConfig(final NetworkName network) { } } if (listBootNodes != null) { - if (!p2pConfiguration.isDiscoveryEnabled()) { + if (!p2pConfig.isDiscoveryEnabled()) { logger.warn("Discovery disabled: bootnodes will be ignored."); } DiscoveryConfiguration.assertValidBootnodes(listBootNodes); diff --git a/besu/src/main/java/org/hyperledger/besu/cli/options/stable/P2POptions.java b/besu/src/main/java/org/hyperledger/besu/cli/options/stable/P2POptions.java index 4da0b775da8..6d4c67fc5e4 100644 --- a/besu/src/main/java/org/hyperledger/besu/cli/options/stable/P2POptions.java +++ b/besu/src/main/java/org/hyperledger/besu/cli/options/stable/P2POptions.java @@ -14,6 +14,9 @@ */ package org.hyperledger.besu.cli.options.stable; +import static java.util.Arrays.asList; +import static org.hyperledger.besu.cli.options.unstable.NetworkingOptions.PEER_LOWER_BOUND_FLAG; +import static org.hyperledger.besu.cli.util.CommandLineUtils.isOptionSet; import static org.hyperledger.besu.ethereum.p2p.config.AutoDiscoverDefaultIP.autoDiscoverDefaultIP; import org.hyperledger.besu.cli.DefaultCommandValues; @@ -27,13 +30,15 @@ import org.hyperledger.besu.util.number.Fraction; import org.hyperledger.besu.util.number.Percentage; +import java.net.SocketException; +import java.net.UnknownHostException; import java.util.ArrayList; -import java.util.Collection; import java.util.List; import java.util.stream.Collectors; import org.apache.commons.net.util.SubnetUtils.SubnetInfo; import org.apache.tuweni.bytes.Bytes; +import org.slf4j.Logger; import picocli.CommandLine; /** The P2POptions Config Cli Options. */ @@ -165,11 +170,70 @@ void setBannedNodeIds(final List values) { "Comma-separated list of allowed IP subnets (e.g., '192.168.1.0/24,10.0.0.0/8').") private List allowedSubnets; - private Collection bannedNodeIds = new ArrayList<>(); + private List bannedNodeIds = new ArrayList<>(); /** Default constructor. */ public P2POptions() {} + /** + * Validates P2P options + * + * @param commandLine the commandLine + * @param logger the logger + */ + public void validate(final CommandLine commandLine, final Logger logger) { + final float fraction = Fraction.fromPercentage(maxRemoteConnectionsPercentage).getValue(); + if (!(fraction >= 0.0 && fraction <= 1.0)) { + throw new CommandLine.ParameterException( + commandLine, + "Fraction of remote connections allowed must be between 0.0 and 1.0 (inclusive)."); + } + checkP2pOptionsDependencies(commandLine, logger); + } + + /** + * Validates P2P interface IP address/host name. Visible for testing. + * + * @param commandLine the commandLine + * @param logger the logger + */ + public void validateP2PInterface(final CommandLine commandLine, final Logger logger) { + final String failMessage = "The provided --p2p-interface is not available: " + p2pInterface; + try { + if (!NetworkUtility.isNetworkInterfaceAvailable(p2pInterface)) { + throw new CommandLine.ParameterException(commandLine, failMessage); + } + } catch (final UnknownHostException | SocketException e) { + throw new CommandLine.ParameterException(commandLine, failMessage, e); + } + ensureValidPeerBoundParams(commandLine, logger); + } + + private void ensureValidPeerBoundParams(final CommandLine commandLine, final Logger logger) { + if (isOptionSet(commandLine, PEER_LOWER_BOUND_FLAG)) { + logger.warn(PEER_LOWER_BOUND_FLAG + " is deprecated and will be removed soon."); + } + } + + private void checkP2pOptionsDependencies(final CommandLine commandLine, final Logger logger) { + // Check that P2P options are able to work + CommandLineUtils.checkOptionDependencies( + logger, + commandLine, + "--p2p-enabled", + !p2pEnabled, + asList( + "--bootnodes", + "--discovery-enabled", + "--max-peers", + "--banned-node-id", + "--banned-node-ids", + "--p2p-host", + "--p2p-interface", + "--p2p-port", + "--remote-connections-max-percentage")); + } + @Override public P2PConfiguration toDomainObject() { return new P2PConfiguration.Builder() diff --git a/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java b/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java index aab0000592f..c8b7219c5a4 100644 --- a/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java +++ b/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java @@ -79,6 +79,7 @@ import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule; import org.hyperledger.besu.ethereum.mainnet.ProtocolSpec; import org.hyperledger.besu.ethereum.p2p.config.NetworkingConfiguration; +import org.hyperledger.besu.ethereum.p2p.config.P2PConfiguration; import org.hyperledger.besu.ethereum.p2p.config.SubProtocolConfiguration; import org.hyperledger.besu.ethereum.storage.StorageProvider; import org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueSegmentIdentifier; @@ -187,19 +188,16 @@ public abstract class BesuControllerBuilder implements MiningParameterOverrides /** The Evm configuration. */ protected EvmConfiguration evmConfiguration; - /** The Max peers. */ - protected int maxPeers; + /** The P2P configuration. */ + protected P2PConfiguration p2PConfiguration = P2PConfiguration.createDefault(); /** Manages a cache of bad blocks globally */ protected final BadBlockManager badBlockManager = new BadBlockManager(); - private int maxRemotelyInitiatedPeers; - /** The Chain pruner configuration. */ protected ChainPrunerConfiguration chainPrunerConfiguration = ChainPrunerConfiguration.DEFAULT; private NetworkingConfiguration networkingConfiguration; - private Boolean randomPeerPriority; /** the Dagger configured context that can provide dependencies */ protected Optional besuComponent = Optional.empty(); @@ -460,24 +458,13 @@ public BesuControllerBuilder evmConfiguration(final EvmConfiguration evmConfigur } /** - * Max peers besu controller builder. - * - * @param maxPeers the max peers - * @return the besu controller builder - */ - public BesuControllerBuilder maxPeers(final int maxPeers) { - this.maxPeers = maxPeers; - return this; - } - - /** - * Maximum number of remotely initiated peer connections + * P2P Configuration besu controller builder. * - * @param maxRemotelyInitiatedPeers maximum number of remotely initiated peer connections + * @param p2PConfiguration p2PConfiguration * @return the besu controller builder */ - public BesuControllerBuilder maxRemotelyInitiatedPeers(final int maxRemotelyInitiatedPeers) { - this.maxRemotelyInitiatedPeers = maxRemotelyInitiatedPeers; + public BesuControllerBuilder p2PConfiguration(final P2PConfiguration p2PConfiguration) { + this.p2PConfiguration = p2PConfiguration; return this; } @@ -516,17 +503,6 @@ public BesuControllerBuilder networkConfiguration( return this; } - /** - * sets the randomPeerPriority flag in the builder - * - * @param randomPeerPriority the random peer priority flag - * @return the besu controller builder - */ - public BesuControllerBuilder randomPeerPriority(final Boolean randomPeerPriority) { - this.randomPeerPriority = randomPeerPriority; - return this; - } - /** * Build besu controller. * @@ -611,9 +587,9 @@ public BesuController build() { maxMessageSize, messagePermissioningProviders, nodeKey.getPublicKey().getEncodedBytes(), - maxPeers, - maxRemotelyInitiatedPeers, - randomPeerPriority); + p2PConfiguration.getMaxPeers(), + p2PConfiguration.getMaxRemoteInitiatedPeers(), + p2PConfiguration.isRandomPeerPriority()); final EthMessages ethMessages = new EthMessages(); final EthMessages snapMessages = new EthMessages(); diff --git a/besu/src/test/java/org/hyperledger/besu/RunnerBuilderTest.java b/besu/src/test/java/org/hyperledger/besu/RunnerBuilderTest.java index c8d875ec0d1..80a5a375d73 100644 --- a/besu/src/test/java/org/hyperledger/besu/RunnerBuilderTest.java +++ b/besu/src/test/java/org/hyperledger/besu/RunnerBuilderTest.java @@ -151,7 +151,7 @@ public void enodeUrlShouldHaveAdvertisedHostWhenDiscoveryDisabled() { final Runner runner = new RunnerBuilder() - .p2pConfiguration( + .p2PConfiguration( P2PConfiguration.builder() .p2pInterface("0.0.0.0") .p2pPort(p2pListenPort) @@ -198,7 +198,7 @@ public void movingAcrossProtocolSpecsUpdatesNodeRecord() { when(protocolContext.getBlockchain()).thenReturn(inMemoryBlockchain); final Runner runner = new RunnerBuilder() - .p2pConfiguration( + .p2PConfiguration( P2PConfiguration.builder() .p2pInterface("0.0.0.0") .p2pPort(p2pListenPort) @@ -259,7 +259,7 @@ public void whenEngineApiAddedListensOnDefaultPort() { final Runner runner = new RunnerBuilder() - .p2pConfiguration( + .p2PConfiguration( P2PConfiguration.builder() .p2pInterface("0.0.0.0") .p2pPort(30303) @@ -305,7 +305,7 @@ public void whenEngineApiAddedWebSocketReadyOnSamePort() { final Runner runner = new RunnerBuilder() - .p2pConfiguration( + .p2PConfiguration( P2PConfiguration.builder() .p2pInterface("0.0.0.0") .p2pPort(30303) @@ -350,7 +350,7 @@ public void whenEngineApiAddedEthSubscribeAvailable() { final Runner runner = new RunnerBuilder() - .p2pConfiguration( + .p2PConfiguration( P2PConfiguration.builder() .p2pInterface("0.0.0.0") .p2pPort(30303) @@ -397,7 +397,7 @@ public void noEngineApiNoServiceForMethods() { final Runner runner = new RunnerBuilder() - .p2pConfiguration( + .p2PConfiguration( P2PConfiguration.builder() .p2pInterface("0.0.0.0") .p2pPort(30303) diff --git a/besu/src/test/java/org/hyperledger/besu/RunnerTest.java b/besu/src/test/java/org/hyperledger/besu/RunnerTest.java index f6c6aac5f2f..ff4c9d9ee08 100644 --- a/besu/src/test/java/org/hyperledger/besu/RunnerTest.java +++ b/besu/src/test/java/org/hyperledger/besu/RunnerTest.java @@ -54,6 +54,7 @@ import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule; import org.hyperledger.besu.ethereum.mainnet.ProtocolSpec; import org.hyperledger.besu.ethereum.p2p.config.NetworkingConfiguration; +import org.hyperledger.besu.ethereum.p2p.config.P2PConfiguration; import org.hyperledger.besu.ethereum.p2p.peers.EnodeURLImpl; import org.hyperledger.besu.ethereum.storage.StorageProvider; import org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueSegmentIdentifier; @@ -192,9 +193,7 @@ private void syncFromGenesis(final SyncMode mode, final GenesisConfigFile genesi final RunnerBuilder runnerBuilder = new RunnerBuilder() .vertx(vertx) - .discovery(true) - .p2pAdvertisedHost(listenHost) - .p2pListenPort(0) + .p2PConfiguration(P2PConfiguration.builder().p2pHost(listenHost).p2pPort(0).build()) .metricsSystem(noOpMetricsSystem) .permissioningService(new PermissioningServiceImpl()) .staticNodes(emptySet()) @@ -480,9 +479,12 @@ private BesuController getController( .gasLimitCalculator(GasLimitCalculator.constant()) .evmConfiguration(EvmConfiguration.DEFAULT) .networkConfiguration(NetworkingConfiguration.create()) - .randomPeerPriority(Boolean.FALSE) - .maxPeers(25) - .maxRemotelyInitiatedPeers(15) + .p2PConfiguration( + P2PConfiguration.builder() + .randomPeerPriority(false) + .maxPeers(25) + .randomPeerPriority(false) + .build()) .build(); } } diff --git a/besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java b/besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java index 9bcc659782c..f8d84544400 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java @@ -151,7 +151,7 @@ public class BesuCommandTest extends CommandTestAbstract { DEFAULT_WEB_SOCKET_CONFIGURATION = WebSocketConfiguration.createDefault(); DEFAULT_METRICS_CONFIGURATION = MetricsConfiguration.builder().build(); DEFAULT_API_CONFIGURATION = ImmutableApiConfiguration.builder().build(); - DEFAULT_P2P_CONFIGURATION = P2PConfiguration.builder().build(); + DEFAULT_P2P_CONFIGURATION = P2PConfiguration.createDefault(); } @BeforeEach @@ -199,8 +199,6 @@ public void callingVersionDisplayBesuInfoVersion() { public void callingBesuCommandWithoutOptionsMustSyncWithDefaultValues() { parseCommand(); - final int maxPeers = 25; - final ArgumentCaptor ethNetworkArg = ArgumentCaptor.forClass(EthNetworkConfig.class); verify(mockRunnerBuilder) @@ -216,8 +214,8 @@ public void callingBesuCommandWithoutOptionsMustSyncWithDefaultValues() { verify(mockRunnerBuilder).metricsConfiguration(eq(DEFAULT_METRICS_CONFIGURATION)); verify(mockRunnerBuilder).ethNetworkConfig(ethNetworkArg.capture()); verify(mockRunnerBuilder).autoLogBloomCaching(eq(true)); - verify(mockRunnerBuilder).apiConfiguration(DEFAULT_API_CONFIGURATION); - verify(mockRunnerBuilder).p2pConfiguration(DEFAULT_P2P_CONFIGURATION); + verify(mockRunnerBuilder).apiConfiguration(eq(DEFAULT_API_CONFIGURATION)); + verify(mockRunnerBuilder).p2PConfiguration(eq(DEFAULT_P2P_CONFIGURATION)); verify(mockRunnerBuilder).build(); verify(mockControllerBuilderFactory).fromEthNetworkConfig(ethNetworkArg.capture(), any()); @@ -229,8 +227,6 @@ public void callingBesuCommandWithoutOptionsMustSyncWithDefaultValues() { verify(mockControllerBuilder).nodeKey(isNotNull()); verify(mockControllerBuilder).storageProvider(storageProviderArgumentCaptor.capture()); verify(mockControllerBuilder).gasLimitCalculator(eq(GasLimitCalculator.constant())); - verify(mockControllerBuilder).maxPeers(eq(maxPeers)); - verify(mockControllerBuilder).maxRemotelyInitiatedPeers(eq((int) Math.floor(0.6 * maxPeers))); verify(mockControllerBuilder).build(); assertThat(storageProviderArgumentCaptor.getValue()).isNotNull(); @@ -635,7 +631,7 @@ public void identityValueTrueMustBeUsed() { public void p2pEnabledOptionValueTrueMustBeUsed() { parseCommand("--p2p-enabled", "true"); - verify(mockRunnerBuilder).p2pConfiguration(p2PConfigurationArgumentCaptor.capture()); + verify(mockRunnerBuilder).p2PConfiguration(p2PConfigurationArgumentCaptor.capture()); verify(mockRunnerBuilder).build(); assertThat(p2PConfigurationArgumentCaptor.getValue().isP2pEnabled()).isTrue(); @@ -648,7 +644,7 @@ public void p2pEnabledOptionValueTrueMustBeUsed() { public void p2pEnabledOptionValueFalseMustBeUsed() { parseCommand("--p2p-enabled", "false"); - verify(mockRunnerBuilder).p2pConfiguration(p2PConfigurationArgumentCaptor.capture()); + verify(mockRunnerBuilder).p2PConfiguration(p2PConfigurationArgumentCaptor.capture()); verify(mockRunnerBuilder).build(); assertThat(p2PConfigurationArgumentCaptor.getValue().isP2pEnabled()).isFalse(); @@ -732,7 +728,7 @@ public void p2pOptionsRequiresServiceToBeEnabledToml() throws IOException { public void discoveryOptionValueTrueMustBeUsed() { parseCommand("--discovery-enabled", "true"); - verify(mockRunnerBuilder).p2pConfiguration(p2PConfigurationArgumentCaptor.capture()); + verify(mockRunnerBuilder).p2PConfiguration(p2PConfigurationArgumentCaptor.capture()); verify(mockRunnerBuilder).build(); assertThat(p2PConfigurationArgumentCaptor.getValue().isDiscoveryEnabled()).isTrue(); @@ -745,7 +741,7 @@ public void discoveryOptionValueTrueMustBeUsed() { public void discoveryOptionValueFalseMustBeUsed() { parseCommand("--discovery-enabled", "false"); - verify(mockRunnerBuilder).p2pConfiguration(p2PConfigurationArgumentCaptor.capture()); + verify(mockRunnerBuilder).p2PConfiguration(p2PConfigurationArgumentCaptor.capture()); verify(mockRunnerBuilder).build(); assertThat(p2PConfigurationArgumentCaptor.getValue().isDiscoveryEnabled()).isFalse(); @@ -914,7 +910,7 @@ public void bannedNodeIdsOptionMustBeUsed() { Arrays.stream(nodes).map(Bytes::toShortHexString).collect(Collectors.joining(",")); parseCommand("--banned-node-ids", nodeIdsArg); - verify(mockRunnerBuilder).p2pConfiguration(p2PConfigurationArgumentCaptor.capture()); + verify(mockRunnerBuilder).p2PConfiguration(p2PConfigurationArgumentCaptor.capture()); verify(mockRunnerBuilder).build(); assertThat(p2PConfigurationArgumentCaptor.getValue().getBannedNodeIds().toArray()) @@ -949,7 +945,7 @@ public void p2pHostAndPortOptionsAreRespected() { final int port = 1234; parseCommand("--p2p-host", host, "--p2p-port", String.valueOf(port)); - verify(mockRunnerBuilder).p2pConfiguration(p2PConfigurationArgumentCaptor.capture()); + verify(mockRunnerBuilder).p2PConfiguration(p2PConfigurationArgumentCaptor.capture()); verify(mockRunnerBuilder).build(); assertThat(p2PConfigurationArgumentCaptor.getValue().getHost()).isEqualTo(host); @@ -965,7 +961,7 @@ public void p2pInterfaceOptionIsRespected() { final String ip = "1.2.3.4"; parseCommand("--p2p-interface", ip); - verify(mockRunnerBuilder).p2pConfiguration(p2PConfigurationArgumentCaptor.capture()); + verify(mockRunnerBuilder).p2PConfiguration(p2PConfigurationArgumentCaptor.capture()); verify(mockRunnerBuilder).build(); assertThat(p2PConfigurationArgumentCaptor.getValue().getP2pInterface()).isEqualTo(ip); @@ -979,7 +975,7 @@ public void p2pHostMayBeLocalhost() { final String host = "localhost"; parseCommand("--p2p-host", host); - verify(mockRunnerBuilder).p2pConfiguration(p2PConfigurationArgumentCaptor.capture()); + verify(mockRunnerBuilder).p2PConfiguration(p2PConfigurationArgumentCaptor.capture()); verify(mockRunnerBuilder).build(); assertThat(p2PConfigurationArgumentCaptor.getValue().getHost()).isEqualTo(host); @@ -994,7 +990,7 @@ public void p2pHostMayBeIPv6() { final String host = "2600:DB8::8545"; parseCommand("--p2p-host", host); - verify(mockRunnerBuilder).p2pConfiguration(p2PConfigurationArgumentCaptor.capture()); + verify(mockRunnerBuilder).p2PConfiguration(p2PConfigurationArgumentCaptor.capture()); verify(mockRunnerBuilder).build(); assertThat(p2PConfigurationArgumentCaptor.getValue().getHost()).isEqualTo(host); @@ -1009,10 +1005,10 @@ public void maxpeersOptionMustBeUsed() { final int maxPeers = 123; parseCommand("--max-peers", String.valueOf(maxPeers)); - verify(mockControllerBuilder).maxPeers(intArgumentCaptor.capture()); - verify(mockControllerBuilder).build(); + verify(mockRunnerBuilder).p2PConfiguration(p2PConfigurationArgumentCaptor.capture()); + verify(mockRunnerBuilder).build(); - assertThat(intArgumentCaptor.getValue()).isEqualTo(maxPeers); + assertThat(p2PConfigurationArgumentCaptor.getValue().getMaxPeers()).isEqualTo(maxPeers); assertThat(commandOutput.toString(UTF_8)).isEmpty(); assertThat(commandErrorOutput.toString(UTF_8)).isEmpty(); @@ -1027,11 +1023,11 @@ public void p2pPeerUpperBound_without_p2pPeerLowerBound_shouldSetMaxPeers() { assertThat(commandOutput.toString(UTF_8)).isEmpty(); assertThat(commandErrorOutput.toString(UTF_8)).isEmpty(); - verify(mockControllerBuilder).maxPeers(intArgumentCaptor.capture()); - assertThat(intArgumentCaptor.getValue()).isEqualTo(maxPeers); - + verify(mockRunnerBuilder).p2PConfiguration(p2PConfigurationArgumentCaptor.capture()); verify(mockRunnerBuilder).build(); + assertThat(p2PConfigurationArgumentCaptor.getValue().getMaxPeers()).isEqualTo(maxPeers); + assertThat(commandOutput.toString(UTF_8)).isEmpty(); assertThat(commandErrorOutput.toString(UTF_8)).isEmpty(); } @@ -1045,12 +1041,13 @@ public void remoteConnectionsPercentageOptionMustBeUsed() { "--remote-connections-max-percentage", String.valueOf(remoteConnectionsPercentage)); - verify(mockControllerBuilder).maxRemotelyInitiatedPeers(intArgumentCaptor.capture()); - verify(mockControllerBuilder).build(); + verify(mockRunnerBuilder).p2PConfiguration(p2PConfigurationArgumentCaptor.capture()); + verify(mockRunnerBuilder).build(); - assertThat(intArgumentCaptor.getValue()) - .isEqualTo( - (int) Math.floor(25 * Fraction.fromPercentage(remoteConnectionsPercentage).getValue())); + var maxRemotelyInitiatedPeers = + (int) Math.floor(25 * Fraction.fromPercentage(remoteConnectionsPercentage).getValue()); + assertThat(p2PConfigurationArgumentCaptor.getValue().getMaxRemoteInitiatedPeers()) + .isEqualTo(maxRemotelyInitiatedPeers); assertThat(commandOutput.toString(UTF_8)).isEmpty(); assertThat(commandErrorOutput.toString(UTF_8)).isEmpty(); @@ -1230,7 +1227,7 @@ public void netRestrictParsedCorrectly() { final String subnet1 = "127.0.0.1/24"; final String subnet2 = "10.0.0.1/24"; parseCommand("--net-restrict", String.join(",", subnet1, subnet2)); - verify(mockRunnerBuilder).p2pConfiguration(p2PConfigurationArgumentCaptor.capture()); + verify(mockRunnerBuilder).p2PConfiguration(p2PConfigurationArgumentCaptor.capture()); verify(mockRunnerBuilder).build(); var subnets = p2PConfigurationArgumentCaptor.getValue().getAllowedSubnets(); diff --git a/besu/src/test/java/org/hyperledger/besu/cli/CascadingDefaultProviderTest.java b/besu/src/test/java/org/hyperledger/besu/cli/CascadingDefaultProviderTest.java index c2e26344a0b..7ca205117d9 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/CascadingDefaultProviderTest.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/CascadingDefaultProviderTest.java @@ -36,6 +36,7 @@ import org.hyperledger.besu.ethereum.core.MiningParameters; import org.hyperledger.besu.ethereum.eth.sync.SyncMode; import org.hyperledger.besu.ethereum.eth.sync.SynchronizerConfiguration; +import org.hyperledger.besu.ethereum.p2p.config.P2PConfiguration; import org.hyperledger.besu.ethereum.p2p.peers.EnodeURLImpl; import org.hyperledger.besu.metrics.prometheus.MetricsConfiguration; import org.hyperledger.besu.plugin.data.EnodeURL; @@ -108,10 +109,12 @@ public void overrideDefaultValuesIfKeyIsPresentInConfigFile(final @TempDir File parseCommand("--config-file", toml.toString()); - verify(mockRunnerBuilder).discovery(eq(false)); + verify(mockRunnerBuilder).p2PConfiguration(p2PConfigurationArgumentCaptor.capture()); + P2PConfiguration p2PConfiguration = p2PConfigurationArgumentCaptor.getValue(); + assertThat(p2PConfiguration.isDiscoveryEnabled()).isFalse(); + assertThat(p2PConfiguration.getHost()).isEqualTo("1.2.3.4"); + assertThat(p2PConfiguration.getPort()).isEqualTo(1234); verify(mockRunnerBuilder).ethNetworkConfig(ethNetworkConfigArgumentCaptor.capture()); - verify(mockRunnerBuilder).p2pAdvertisedHost(eq("1.2.3.4")); - verify(mockRunnerBuilder).p2pListenPort(eq(1234)); verify(mockRunnerBuilder).jsonRpcConfiguration(eq(jsonRpcConfiguration)); verify(mockRunnerBuilder).graphQLConfiguration(eq(graphQLConfiguration)); verify(mockRunnerBuilder).webSocketConfiguration(eq(webSocketConfiguration)); @@ -162,7 +165,12 @@ public void noOverrideDefaultValuesIfKeyIsNotPresentInConfigFile() { final MetricsConfiguration metricsConfiguration = MetricsConfiguration.builder().build(); - verify(mockRunnerBuilder).discovery(eq(true)); + verify(mockRunnerBuilder).p2PConfiguration(p2PConfigurationArgumentCaptor.capture()); + P2PConfiguration p2PConfiguration = p2PConfigurationArgumentCaptor.getValue(); + assertThat(p2PConfiguration.isDiscoveryEnabled()).isTrue(); + assertThat(p2PConfiguration.getHost()).isEqualTo("127.0.0.1"); + assertThat(p2PConfiguration.getPort()).isEqualTo(30303); + verify(mockRunnerBuilder) .ethNetworkConfig( new EthNetworkConfig( @@ -170,8 +178,6 @@ public void noOverrideDefaultValuesIfKeyIsNotPresentInConfigFile() { MAINNET.getNetworkId(), MAINNET_BOOTSTRAP_NODES, MAINNET_DISCOVERY_URL)); - verify(mockRunnerBuilder).p2pAdvertisedHost(eq("127.0.0.1")); - verify(mockRunnerBuilder).p2pListenPort(eq(30303)); verify(mockRunnerBuilder).jsonRpcConfiguration(eq(jsonRpcConfiguration)); verify(mockRunnerBuilder).graphQLConfiguration(eq(graphQLConfiguration)); verify(mockRunnerBuilder).webSocketConfiguration(eq(webSocketConfiguration)); diff --git a/besu/src/test/java/org/hyperledger/besu/cli/CommandTestAbstract.java b/besu/src/test/java/org/hyperledger/besu/cli/CommandTestAbstract.java index 9181336358a..0566620975c 100644 --- a/besu/src/test/java/org/hyperledger/besu/cli/CommandTestAbstract.java +++ b/besu/src/test/java/org/hyperledger/besu/cli/CommandTestAbstract.java @@ -19,7 +19,6 @@ import static org.hyperledger.besu.cli.util.CommandLineUtils.DEPENDENCY_WARNING_MSG; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; -import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.atLeast; @@ -292,13 +291,9 @@ public void initMocks() throws Exception { when(mockControllerBuilder.reorgLoggingThreshold(anyLong())).thenReturn(mockControllerBuilder); when(mockControllerBuilder.dataStorageConfiguration(any())).thenReturn(mockControllerBuilder); when(mockControllerBuilder.evmConfiguration(any())).thenReturn(mockControllerBuilder); + when(mockControllerBuilder.p2PConfiguration(any())).thenReturn(mockControllerBuilder); when(mockControllerBuilder.networkConfiguration(any())).thenReturn(mockControllerBuilder); - when(mockControllerBuilder.randomPeerPriority(any())).thenReturn(mockControllerBuilder); - when(mockControllerBuilder.maxPeers(anyInt())).thenReturn(mockControllerBuilder); when(mockControllerBuilder.chainPruningConfiguration(any())).thenReturn(mockControllerBuilder); - when(mockControllerBuilder.maxPeers(anyInt())).thenReturn(mockControllerBuilder); - when(mockControllerBuilder.maxRemotelyInitiatedPeers(anyInt())) - .thenReturn(mockControllerBuilder); when(mockControllerBuilder.besuComponent(any(BesuComponent.class))) .thenReturn(mockControllerBuilder); when(mockControllerBuilder.cacheLastBlocks(any())).thenReturn(mockControllerBuilder); @@ -324,7 +319,6 @@ public void initMocks() throws Exception { when(mockRunnerBuilder.vertx(any())).thenReturn(mockRunnerBuilder); when(mockRunnerBuilder.besuController(any())).thenReturn(mockRunnerBuilder); - when(mockRunnerBuilder.p2pConfiguration(any())).thenReturn(mockRunnerBuilder); when(mockRunnerBuilder.ethNetworkConfig(any())).thenReturn(mockRunnerBuilder); when(mockRunnerBuilder.networkingConfiguration(any())).thenReturn(mockRunnerBuilder); when(mockRunnerBuilder.permissioningConfiguration(any())).thenReturn(mockRunnerBuilder); @@ -351,6 +345,7 @@ public void initMocks() throws Exception { when(mockRunnerBuilder.rpcEndpointService(any())).thenReturn(mockRunnerBuilder); when(mockRunnerBuilder.legacyForkId(anyBoolean())).thenReturn(mockRunnerBuilder); when(mockRunnerBuilder.apiConfiguration(any())).thenReturn(mockRunnerBuilder); + when(mockRunnerBuilder.p2PConfiguration(any())).thenReturn(mockRunnerBuilder); when(mockRunnerBuilder.enodeDnsConfiguration(any())).thenReturn(mockRunnerBuilder); when(mockRunnerBuilder.build()).thenReturn(mockRunner); @@ -577,7 +572,7 @@ public static class TestBesuCommand extends BesuCommand { } @Override - protected void validateP2PInterface(final String p2pInterface) { + protected void validateP2PInterface() { // For testing, don't actually query for networking interfaces to validate this option } diff --git a/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/config/P2PConfiguration.java b/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/config/P2PConfiguration.java index 7001bdad75b..1f735290fe6 100644 --- a/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/config/P2PConfiguration.java +++ b/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/config/P2PConfiguration.java @@ -14,10 +14,16 @@ */ package org.hyperledger.besu.ethereum.p2p.config; +import static org.hyperledger.besu.ethereum.p2p.config.AutoDiscoverDefaultIP.autoDiscoverDefaultIP; +import static org.hyperledger.besu.ethereum.p2p.config.RlpxConfiguration.DEFAULT_FRACTION_REMOTE_CONNECTIONS_ALLOWED; + +import org.hyperledger.besu.ethereum.p2p.peers.EnodeURLImpl; +import org.hyperledger.besu.util.NetworkUtility; +import org.hyperledger.besu.util.number.Fraction; import org.hyperledger.besu.util.number.Percentage; -import java.util.Collection; import java.util.List; +import java.util.Objects; import org.apache.commons.net.util.SubnetUtils.SubnetInfo; import org.apache.tuweni.bytes.Bytes; @@ -35,10 +41,10 @@ public class P2PConfiguration { private final Percentage maxRemoteConnectionsPercentage; private final String discoveryDnsUrl; private final boolean randomPeerPriority; - private final Collection bannedNodeIds; + private final List bannedNodeIds; private final List allowedSubnets; - public P2PConfiguration( + private P2PConfiguration( final boolean p2pEnabled, final boolean peerDiscoveryEnabled, final List bootNodes, @@ -50,7 +56,7 @@ public P2PConfiguration( final Percentage maxRemoteConnectionsPercentage, final String discoveryDnsUrl, final boolean randomPeerPriority, - final Collection bannedNodeIds, + final List bannedNodeIds, final List allowedSubnets) { this.p2pEnabled = p2pEnabled; this.discoveryEnabled = peerDiscoveryEnabled; @@ -95,14 +101,6 @@ public int getMaxPeers() { return maxPeers; } - public boolean isLimitRemoteWireConnectionsEnabled() { - return isLimitRemoteWireConnectionsEnabled; - } - - public Percentage getMaxRemoteConnectionsPercentage() { - return maxRemoteConnectionsPercentage; - } - public String getDiscoveryDnsUrl() { return discoveryDnsUrl; } @@ -111,7 +109,7 @@ public boolean isRandomPeerPriority() { return randomPeerPriority; } - public Collection getBannedNodeIds() { + public List getBannedNodeIds() { return bannedNodeIds; } @@ -119,6 +117,28 @@ public List getAllowedSubnets() { return allowedSubnets; } + public int getMaxRemoteInitiatedPeers() { + if (isLimitRemoteWireConnectionsEnabled) { + final float fraction = Fraction.fromPercentage(maxRemoteConnectionsPercentage).getValue(); + return Math.round(fraction * maxPeers); + } + return maxPeers; + } + + public static P2PConfiguration createDefault() { + return new Builder() + .p2pEnabled(true) + .peerDiscoveryEnabled(true) + .p2pHost(autoDiscoverDefaultIP().getHostAddress()) + .p2pInterface(NetworkUtility.INADDR_ANY) + .p2pPort(EnodeURLImpl.DEFAULT_LISTENING_PORT) + .maxPeers(25) + .maxRemoteConnectionsPercentage( + Fraction.fromFloat(DEFAULT_FRACTION_REMOTE_CONNECTIONS_ALLOWED).toPercentage()) + .isLimitRemoteWireConnectionsEnabled(true) + .build(); + } + public static P2PConfiguration.Builder builder() { return new P2PConfiguration.Builder(); } @@ -128,14 +148,15 @@ public static class Builder { private boolean peerDiscoveryEnabled = true; private List bootNodes; private String p2pHost; - private String p2pInterface; - private int p2pPort; + private String p2pInterface = NetworkUtility.INADDR_ANY; + private int p2pPort = EnodeURLImpl.DEFAULT_LISTENING_PORT; private int maxPeers; private boolean isLimitRemoteWireConnectionsEnabled = true; - private Percentage maxRemoteConnectionsPercentage; + private Percentage maxRemoteConnectionsPercentage = + Fraction.fromFloat(DEFAULT_FRACTION_REMOTE_CONNECTIONS_ALLOWED).toPercentage(); private String discoveryDnsUrl; private boolean randomPeerPriority = false; - private Collection bannedNodeIds; + private List bannedNodeIds = List.of(); private List allowedSubnets; public Builder p2pEnabled(final boolean p2pEnabled) { @@ -194,7 +215,7 @@ public Builder randomPeerPriority(final boolean randomPeerPriority) { return this; } - public Builder bannedNodeIds(final Collection bannedNodeIds) { + public Builder bannedNodeIds(final List bannedNodeIds) { this.bannedNodeIds = bannedNodeIds; return this; } @@ -221,4 +242,78 @@ public P2PConfiguration build() { allowedSubnets); } } + + @Override + public String toString() { + return "P2PConfiguration{" + + "p2pEnabled=" + + p2pEnabled + + ", discoveryEnabled=" + + discoveryEnabled + + ", bootNodes=" + + bootNodes + + ", host='" + + host + + '\'' + + ", p2pInterface='" + + p2pInterface + + '\'' + + ", port=" + + port + + ", maxPeers=" + + maxPeers + + ", isLimitRemoteWireConnectionsEnabled=" + + isLimitRemoteWireConnectionsEnabled + + ", maxRemoteConnectionsPercentage=" + + maxRemoteConnectionsPercentage + + ", discoveryDnsUrl='" + + discoveryDnsUrl + + '\'' + + ", randomPeerPriority=" + + randomPeerPriority + + ", bannedNodeIds=" + + bannedNodeIds + + ", allowedSubnets=" + + allowedSubnets + + '}'; + } + + @Override + public int hashCode() { + return Objects.hash( + p2pEnabled, + discoveryEnabled, + bootNodes, + host, + p2pInterface, + port, + maxPeers, + isLimitRemoteWireConnectionsEnabled, + maxRemoteConnectionsPercentage, + discoveryDnsUrl, + randomPeerPriority, + bannedNodeIds, + allowedSubnets); + } + + @Override + public boolean equals(final Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + P2PConfiguration that = (P2PConfiguration) o; + return p2pEnabled == that.p2pEnabled + && discoveryEnabled == that.discoveryEnabled + && port == that.port + && maxPeers == that.maxPeers + && isLimitRemoteWireConnectionsEnabled == that.isLimitRemoteWireConnectionsEnabled + && randomPeerPriority == that.randomPeerPriority + && Objects.equals(bootNodes, that.bootNodes) + && Objects.equals(host, that.host) + && Objects.equals(p2pInterface, that.p2pInterface) + && Objects.equals(maxRemoteConnectionsPercentage, that.maxRemoteConnectionsPercentage) + && Objects.equals(discoveryDnsUrl, that.discoveryDnsUrl) + && Objects.equals(bannedNodeIds, that.bannedNodeIds) + && Objects.equals(allowedSubnets, that.allowedSubnets); + } } From 9a5ca857ad128de9203435de404fbae446b0e517 Mon Sep 17 00:00:00 2001 From: Gabriel-Trintinalia Date: Thu, 20 Jun 2024 19:28:59 +1000 Subject: [PATCH 5/7] Fix tests Signed-off-by: Gabriel-Trintinalia --- .../dsl/node/ThreadBesuNodeRunner.java | 4 +-- .../BesuNodeConfigurationBuilder.java | 2 +- .../node/configuration/BesuNodeFactory.java | 24 +++++++------ .../config/BootNodesGenesisSetupTest.java | 2 +- .../org/hyperledger/besu/RunnerBuilder.java | 4 +-- .../org/hyperledger/besu/cli/BesuCommand.java | 2 +- .../besu/cli/options/stable/P2POptions.java | 6 ++-- .../hyperledger/besu/RunnerBuilderTest.java | 36 +++++++++---------- .../java/org/hyperledger/besu/RunnerTest.java | 2 +- .../ethereum/p2p/config/P2PConfiguration.java | 26 +++++++------- 10 files changed, 56 insertions(+), 52 deletions(-) diff --git a/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/ThreadBesuNodeRunner.java b/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/ThreadBesuNodeRunner.java index c6ac5ceb074..58f7f09e4ee 100644 --- a/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/ThreadBesuNodeRunner.java +++ b/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/ThreadBesuNodeRunner.java @@ -252,8 +252,8 @@ public void startNode(final BesuNode node) { .storageProvider(storageProvider) .gasLimitCalculator(GasLimitCalculator.constant()) .evmConfiguration(EvmConfiguration.DEFAULT) - .p2PConfiguration(P2PConfiguration.createDefault()) - .networkConfiguration(node.getNetworkingConfiguration()); + .networkConfiguration(node.getNetworkingConfiguration()) + .p2PConfiguration(P2PConfiguration.createDefault()); node.getGenesisConfig() .map(GenesisConfigFile::fromConfig) diff --git a/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/configuration/BesuNodeConfigurationBuilder.java b/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/configuration/BesuNodeConfigurationBuilder.java index 424c1d76e56..262174799cf 100644 --- a/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/configuration/BesuNodeConfigurationBuilder.java +++ b/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/configuration/BesuNodeConfigurationBuilder.java @@ -247,7 +247,7 @@ public BesuNodeConfigurationBuilder jsonRpcAuthenticationUsingECDSA() throws URI return this; } - public BesuNodeConfigurationBuilder p2pConfiguration(final P2PConfiguration p2pConfiguration) { + public BesuNodeConfigurationBuilder p2PConfiguration(final P2PConfiguration p2pConfiguration) { this.p2pConfiguration = p2pConfiguration; return this; } diff --git a/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/configuration/BesuNodeFactory.java b/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/configuration/BesuNodeFactory.java index b1807d4cb65..7b9dbf86490 100644 --- a/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/configuration/BesuNodeFactory.java +++ b/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/configuration/BesuNodeFactory.java @@ -55,6 +55,11 @@ public class BesuNodeFactory { private final NodeConfigurationFactory node = new NodeConfigurationFactory(); + private final P2PConfiguration P2P_DISABLED = + P2PConfiguration.builder().p2pEnabled(false).build(); + private final P2PConfiguration DISCOVERY_DISABLED = + P2PConfiguration.builder().discoveryEnabled(false).build(); + public BesuNode create(final BesuNodeConfiguration config) throws IOException { return new BesuNode( config.getName(), @@ -182,7 +187,7 @@ public BesuNode createArchiveNodeWithDiscoveryDisabledAndAdmin(final String name .name(name) .jsonRpcConfiguration(node.jsonRpcConfigWithAdmin()) .webSocketEnabled() - .p2pConfiguration(P2PConfiguration.builder().peerDiscoveryEnabled(false).build()) + .p2PConfiguration(DISCOVERY_DISABLED) .build()); } @@ -197,7 +202,6 @@ public BesuNode createArchiveNodeNetServicesEnabled(final String name) throws IO // .setMetricsConfiguration(metricsConfiguration) .jsonRpcConfiguration(node.jsonRpcConfigWithAdmin()) .webSocketEnabled() - .p2pConfiguration(P2PConfiguration.builder().p2pEnabled(true).build()) .build()); } @@ -206,7 +210,7 @@ public BesuNode createArchiveNodeNetServicesDisabled(final String name) throws I new BesuNodeConfigurationBuilder() .name(name) .jsonRpcConfiguration(node.jsonRpcConfigWithAdmin()) - .p2pConfiguration(P2PConfiguration.builder().p2pEnabled(false).build()) + .p2PConfiguration(P2P_DISABLED) .build()); } @@ -276,7 +280,7 @@ public BesuNode createNodeWithP2pDisabled(final String name) throws IOException return create( new BesuNodeConfigurationBuilder() .name(name) - .p2pConfiguration(P2PConfiguration.builder().p2pEnabled(false).build()) + .p2PConfiguration(P2P_DISABLED) .jsonRpcConfiguration(node.createJsonRpcEnabledConfig()) .build()); } @@ -358,7 +362,7 @@ public BesuNode createNodeWithNoDiscovery(final String name) throws IOException return create( new BesuNodeConfigurationBuilder() .name(name) - .p2pConfiguration(P2PConfiguration.builder().peerDiscoveryEnabled(false).build()) + .p2PConfiguration(DISCOVERY_DISABLED) .engineRpcEnabled(false) .build()); } @@ -477,12 +481,12 @@ public BesuNode createIbft2Node(final String name, final boolean fixedPort) thro .devMode(false) .genesisConfigProvider(GenesisConfigurationFactory::createIbft2GenesisConfig); if (fixedPort) { - var port = + int port = Math.abs(name.hashCode() % 60000) + 1024 + 500; // Generate a consistent port for p2p based on node name (+ 500 to avoid // clashing with RPC port or other nodes with a similar name) - builder.p2pConfiguration(P2PConfiguration.builder().p2pPort(port).build()); + builder.p2PConfiguration(P2PConfiguration.builder().port(port).build()); } return create(builder.build()); } @@ -530,12 +534,12 @@ public BesuNode createQbftNode(final String name, final boolean fixedPort) throw .devMode(false) .genesisConfigProvider(GenesisConfigurationFactory::createQbftGenesisConfig); if (fixedPort) { - var port = + int port = Math.abs(name.hashCode() % 60000) + 1024 + 500; // Generate a consistent port for p2p based on node name (+ 500 to avoid // clashing with RPC port or other nodes with a similar name) - builder.p2pConfiguration(P2PConfiguration.builder().p2pPort(port).build()); + builder.p2PConfiguration(P2PConfiguration.builder().port(port).build()); } return create(builder.build()); } @@ -718,7 +722,7 @@ private BesuNodeConfigurationBuilder createConfigurationBuilderWithStaticNodes( .name(name) .jsonRpcEnabled() .webSocketEnabled() - .p2pConfiguration(P2PConfiguration.builder().peerDiscoveryEnabled(false).build()) + .p2PConfiguration(DISCOVERY_DISABLED) .staticNodes(staticNodesUrls) .bootnodeEligible(false); } diff --git a/acceptance-tests/tests/src/test/java/org/hyperledger/besu/tests/acceptance/config/BootNodesGenesisSetupTest.java b/acceptance-tests/tests/src/test/java/org/hyperledger/besu/tests/acceptance/config/BootNodesGenesisSetupTest.java index a2db6670908..ce3046f9c9e 100644 --- a/acceptance-tests/tests/src/test/java/org/hyperledger/besu/tests/acceptance/config/BootNodesGenesisSetupTest.java +++ b/acceptance-tests/tests/src/test/java/org/hyperledger/besu/tests/acceptance/config/BootNodesGenesisSetupTest.java @@ -109,7 +109,7 @@ private BesuNodeConfigurationBuilder configureNode( return nodeBuilder .devMode(false) .keyPair(keyPair) - .p2pConfiguration(P2PConfiguration.builder().p2pPort(p2pBindingPort).build()) + .p2PConfiguration(P2PConfiguration.builder().port(p2pBindingPort).build()) .genesisConfigProvider( (nodes) -> Optional.of( diff --git a/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java b/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java index 09cd6c622af..fbbf04442cc 100644 --- a/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java +++ b/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java @@ -215,9 +215,9 @@ public RunnerBuilder besuController(final BesuController besuController) { } /** - * TLSConfiguration p2pTLSConfiguration. + * P2P Configuration Configuration. * - * @param p2PConfiguration the TLSConfiguration p2pTLSConfiguration + * @param p2PConfiguration the p2pConfiguration * @return the runner builder */ public RunnerBuilder p2PConfiguration(final P2PConfiguration p2PConfiguration) { diff --git a/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java b/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java index 1f915d953dc..9aa2aa73831 100644 --- a/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java +++ b/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java @@ -2095,6 +2095,7 @@ private Runner synchronize( runnerBuilder .vertx(vertx) .besuController(controller) + .p2PConfiguration(p2pConfiguration) .natMethod(natMethod) .natManagerServiceName(unstableNatOptions.getNatManagerServiceName()) .natMethodFallbackEnabled(unstableNatOptions.getNatMethodFallbackEnabled()) @@ -2121,7 +2122,6 @@ private Runner synchronize( .storageProvider(keyValueStorageProvider(keyValueStorageName)) .rpcEndpointService(rpcEndpointServiceImpl) .enodeDnsConfiguration(getEnodeDnsConfiguration()) - .p2PConfiguration(p2pConfiguration) .build(); addShutdownHook(runner); diff --git a/besu/src/main/java/org/hyperledger/besu/cli/options/stable/P2POptions.java b/besu/src/main/java/org/hyperledger/besu/cli/options/stable/P2POptions.java index 6d4c67fc5e4..2ddc691c03a 100644 --- a/besu/src/main/java/org/hyperledger/besu/cli/options/stable/P2POptions.java +++ b/besu/src/main/java/org/hyperledger/besu/cli/options/stable/P2POptions.java @@ -238,11 +238,11 @@ private void checkP2pOptionsDependencies(final CommandLine commandLine, final Lo public P2PConfiguration toDomainObject() { return new P2PConfiguration.Builder() .p2pEnabled(p2pEnabled) - .peerDiscoveryEnabled(peerDiscoveryEnabled) + .discoveryEnabled(peerDiscoveryEnabled) .bootNodes(bootNodes) - .p2pHost(p2pHost) + .host(p2pHost) .p2pInterface(p2pInterface) - .p2pPort(p2pPort) + .port(p2pPort) .maxPeers(maxPeers) .isLimitRemoteWireConnectionsEnabled(isLimitRemoteWireConnectionsEnabled) .maxRemoteConnectionsPercentage(maxRemoteConnectionsPercentage) diff --git a/besu/src/test/java/org/hyperledger/besu/RunnerBuilderTest.java b/besu/src/test/java/org/hyperledger/besu/RunnerBuilderTest.java index 80a5a375d73..d1e8589dadd 100644 --- a/besu/src/test/java/org/hyperledger/besu/RunnerBuilderTest.java +++ b/besu/src/test/java/org/hyperledger/besu/RunnerBuilderTest.java @@ -154,10 +154,10 @@ public void enodeUrlShouldHaveAdvertisedHostWhenDiscoveryDisabled() { .p2PConfiguration( P2PConfiguration.builder() .p2pInterface("0.0.0.0") - .p2pPort(p2pListenPort) - .p2pHost(p2pAdvertisedHost) + .port(p2pListenPort) + .host(p2pAdvertisedHost) .p2pEnabled(true) - .peerDiscoveryEnabled(false) + .discoveryEnabled(false) .build()) .besuController(besuController) .ethNetworkConfig(mock(EthNetworkConfig.class)) @@ -201,10 +201,10 @@ public void movingAcrossProtocolSpecsUpdatesNodeRecord() { .p2PConfiguration( P2PConfiguration.builder() .p2pInterface("0.0.0.0") - .p2pPort(p2pListenPort) - .p2pHost(p2pAdvertisedHost) + .port(p2pListenPort) + .host(p2pAdvertisedHost) .p2pEnabled(true) - .peerDiscoveryEnabled(true) + .discoveryEnabled(true) .build()) .natMethod(NatMethod.NONE) .besuController(besuController) @@ -262,10 +262,10 @@ public void whenEngineApiAddedListensOnDefaultPort() { .p2PConfiguration( P2PConfiguration.builder() .p2pInterface("0.0.0.0") - .p2pPort(30303) - .p2pHost("127.0.0.1") + .port(30303) + .host("127.0.0.1") .p2pEnabled(true) - .peerDiscoveryEnabled(true) + .discoveryEnabled(true) .build()) .natMethod(NatMethod.NONE) .besuController(besuController) @@ -308,10 +308,10 @@ public void whenEngineApiAddedWebSocketReadyOnSamePort() { .p2PConfiguration( P2PConfiguration.builder() .p2pInterface("0.0.0.0") - .p2pPort(30303) - .p2pHost("127.0.0.1") + .port(30303) + .host("127.0.0.1") .p2pEnabled(true) - .peerDiscoveryEnabled(true) + .discoveryEnabled(true) .build()) .natMethod(NatMethod.NONE) .besuController(besuController) @@ -353,10 +353,10 @@ public void whenEngineApiAddedEthSubscribeAvailable() { .p2PConfiguration( P2PConfiguration.builder() .p2pInterface("0.0.0.0") - .p2pPort(30303) - .p2pHost("127.0.0.1") + .port(30303) + .host("127.0.0.1") .p2pEnabled(true) - .peerDiscoveryEnabled(true) + .discoveryEnabled(true) .build()) .natMethod(NatMethod.NONE) .besuController(besuController) @@ -400,10 +400,10 @@ public void noEngineApiNoServiceForMethods() { .p2PConfiguration( P2PConfiguration.builder() .p2pInterface("0.0.0.0") - .p2pPort(30303) - .p2pHost("127.0.0.1") + .port(30303) + .host("127.0.0.1") .p2pEnabled(true) - .peerDiscoveryEnabled(true) + .discoveryEnabled(true) .build()) .natMethod(NatMethod.NONE) .besuController(besuController) diff --git a/besu/src/test/java/org/hyperledger/besu/RunnerTest.java b/besu/src/test/java/org/hyperledger/besu/RunnerTest.java index ff4c9d9ee08..4dc31dcf223 100644 --- a/besu/src/test/java/org/hyperledger/besu/RunnerTest.java +++ b/besu/src/test/java/org/hyperledger/besu/RunnerTest.java @@ -193,7 +193,7 @@ private void syncFromGenesis(final SyncMode mode, final GenesisConfigFile genesi final RunnerBuilder runnerBuilder = new RunnerBuilder() .vertx(vertx) - .p2PConfiguration(P2PConfiguration.builder().p2pHost(listenHost).p2pPort(0).build()) + .p2PConfiguration(P2PConfiguration.builder().host(listenHost).port(0).build()) .metricsSystem(noOpMetricsSystem) .permissioningService(new PermissioningServiceImpl()) .staticNodes(emptySet()) diff --git a/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/config/P2PConfiguration.java b/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/config/P2PConfiguration.java index 1f735290fe6..017ed7a5145 100644 --- a/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/config/P2PConfiguration.java +++ b/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/config/P2PConfiguration.java @@ -128,10 +128,10 @@ public int getMaxRemoteInitiatedPeers() { public static P2PConfiguration createDefault() { return new Builder() .p2pEnabled(true) - .peerDiscoveryEnabled(true) - .p2pHost(autoDiscoverDefaultIP().getHostAddress()) + .discoveryEnabled(true) + .host(autoDiscoverDefaultIP().getHostAddress()) .p2pInterface(NetworkUtility.INADDR_ANY) - .p2pPort(EnodeURLImpl.DEFAULT_LISTENING_PORT) + .port(EnodeURLImpl.DEFAULT_LISTENING_PORT) .maxPeers(25) .maxRemoteConnectionsPercentage( Fraction.fromFloat(DEFAULT_FRACTION_REMOTE_CONNECTIONS_ALLOWED).toPercentage()) @@ -145,9 +145,9 @@ public static P2PConfiguration.Builder builder() { public static class Builder { private boolean p2pEnabled = true; - private boolean peerDiscoveryEnabled = true; + private boolean discoveryEnabled = true; private List bootNodes; - private String p2pHost; + private String host; private String p2pInterface = NetworkUtility.INADDR_ANY; private int p2pPort = EnodeURLImpl.DEFAULT_LISTENING_PORT; private int maxPeers; @@ -164,8 +164,8 @@ public Builder p2pEnabled(final boolean p2pEnabled) { return this; } - public Builder peerDiscoveryEnabled(final boolean peerDiscoveryEnabled) { - this.peerDiscoveryEnabled = peerDiscoveryEnabled; + public Builder discoveryEnabled(final boolean discoveryEnabled) { + this.discoveryEnabled = discoveryEnabled; return this; } @@ -174,8 +174,8 @@ public Builder bootNodes(final List bootNodes) { return this; } - public Builder p2pHost(final String p2pHost) { - this.p2pHost = p2pHost; + public Builder host(final String host) { + this.host = host; return this; } @@ -184,8 +184,8 @@ public Builder p2pInterface(final String p2pInterface) { return this; } - public Builder p2pPort(final int p2pPort) { - this.p2pPort = p2pPort; + public Builder port(final int listeningPort) { + this.p2pPort = listeningPort; return this; } @@ -228,9 +228,9 @@ public Builder allowedSubnets(final List allowedSubnets) { public P2PConfiguration build() { return new P2PConfiguration( p2pEnabled, - peerDiscoveryEnabled, + discoveryEnabled, bootNodes, - p2pHost, + host, p2pInterface, p2pPort, maxPeers, From a32d8e644729d292a2dc49e8ca59d4e681cd0c4e Mon Sep 17 00:00:00 2001 From: Gabriel-Trintinalia Date: Tue, 23 Jul 2024 12:05:31 +1000 Subject: [PATCH 6/7] fix javadoc Signed-off-by: Gabriel-Trintinalia --- besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java b/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java index 6ade37d7112..09910914b3c 100644 --- a/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java +++ b/besu/src/main/java/org/hyperledger/besu/RunnerBuilder.java @@ -215,7 +215,7 @@ public RunnerBuilder besuController(final BesuController besuController) { } /** - * P2P Configuration Configuration. + * P2P Configuration. * * @param p2PConfiguration the p2pConfiguration * @return the runner builder From 05f68818f0e67dd56ad89e0cb93b09a9ecce66a3 Mon Sep 17 00:00:00 2001 From: Gabriel-Trintinalia Date: Tue, 23 Jul 2024 14:24:00 +1000 Subject: [PATCH 7/7] Fix port Signed-off-by: Gabriel-Trintinalia --- .../acceptance/dsl/node/configuration/BesuNodeFactory.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/configuration/BesuNodeFactory.java b/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/configuration/BesuNodeFactory.java index f462d9f6a83..b64a2e8a15d 100644 --- a/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/configuration/BesuNodeFactory.java +++ b/acceptance-tests/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/node/configuration/BesuNodeFactory.java @@ -56,9 +56,9 @@ public class BesuNodeFactory { private final NodeConfigurationFactory node = new NodeConfigurationFactory(); private final P2PConfiguration P2P_DISABLED = - P2PConfiguration.builder().p2pEnabled(false).build(); + P2PConfiguration.builder().port(0).p2pEnabled(false).build(); private final P2PConfiguration DISCOVERY_DISABLED = - P2PConfiguration.builder().discoveryEnabled(false).build(); + P2PConfiguration.builder().port(0).discoveryEnabled(false).build(); public BesuNode create(final BesuNodeConfiguration config) throws IOException { return new BesuNode(