use of org.hyperledger.besu.cli.config.EthNetworkConfig in project besu by hyperledger.
the class RunnerBuilderTest method whenEngineApiAddedWebSocketReadyOnSamePort.
@Test
public void whenEngineApiAddedWebSocketReadyOnSamePort() {
WebSocketConfiguration wsRpc = WebSocketConfiguration.createDefault();
wsRpc.setEnabled(true);
EthNetworkConfig mockMainnet = mock(EthNetworkConfig.class);
when(mockMainnet.getNetworkId()).thenReturn(BigInteger.ONE);
MergeConfigOptions.setMergeEnabled(true);
when(besuController.getMiningCoordinator()).thenReturn(mock(MergeMiningCoordinator.class));
JsonRpcConfiguration engineConf = JsonRpcConfiguration.createEngineDefault();
engineConf.setEnabled(true);
final Runner runner = new RunnerBuilder().discovery(true).p2pListenInterface("0.0.0.0").p2pListenPort(30303).p2pAdvertisedHost("127.0.0.1").p2pEnabled(true).natMethod(NatMethod.NONE).besuController(besuController).ethNetworkConfig(mockMainnet).metricsSystem(mock(ObservableMetricsSystem.class)).permissioningService(mock(PermissioningServiceImpl.class)).jsonRpcConfiguration(JsonRpcConfiguration.createDefault()).engineJsonRpcConfiguration(engineConf).webSocketConfiguration(wsRpc).jsonRpcIpcConfiguration(mock(JsonRpcIpcConfiguration.class)).graphQLConfiguration(mock(GraphQLConfiguration.class)).metricsConfiguration(mock(MetricsConfiguration.class)).vertx(Vertx.vertx()).dataDir(dataDir.getRoot().toPath()).storageProvider(mock(KeyValueStorageProvider.class)).forkIdSupplier(() -> Collections.singletonList(Bytes.EMPTY)).rpcEndpointService(new RpcEndpointServiceImpl()).besuPluginContext(mock(BesuPluginContextImpl.class)).build();
assertThat(runner.getEngineJsonRpcPort()).isPresent();
}
use of org.hyperledger.besu.cli.config.EthNetworkConfig in project besu by hyperledger.
the class RunnerBuilderTest method whenEngineApiAddedListensOnDefaultPort.
@Test
public void whenEngineApiAddedListensOnDefaultPort() {
JsonRpcConfiguration jrpc = JsonRpcConfiguration.createDefault();
jrpc.setEnabled(true);
JsonRpcConfiguration engine = JsonRpcConfiguration.createEngineDefault();
engine.setEnabled(true);
EthNetworkConfig mockMainnet = mock(EthNetworkConfig.class);
when(mockMainnet.getNetworkId()).thenReturn(BigInteger.ONE);
MergeConfigOptions.setMergeEnabled(true);
when(besuController.getMiningCoordinator()).thenReturn(mock(MergeMiningCoordinator.class));
final Runner runner = new RunnerBuilder().discovery(true).p2pListenInterface("0.0.0.0").p2pListenPort(30303).p2pAdvertisedHost("127.0.0.1").p2pEnabled(true).natMethod(NatMethod.NONE).besuController(besuController).ethNetworkConfig(mockMainnet).metricsSystem(mock(ObservableMetricsSystem.class)).permissioningService(mock(PermissioningServiceImpl.class)).jsonRpcConfiguration(jrpc).engineJsonRpcConfiguration(engine).graphQLConfiguration(mock(GraphQLConfiguration.class)).webSocketConfiguration(mock(WebSocketConfiguration.class)).jsonRpcIpcConfiguration(mock(JsonRpcIpcConfiguration.class)).metricsConfiguration(mock(MetricsConfiguration.class)).vertx(Vertx.vertx()).dataDir(dataDir.getRoot().toPath()).storageProvider(mock(KeyValueStorageProvider.class)).forkIdSupplier(() -> Collections.singletonList(Bytes.EMPTY)).rpcEndpointService(new RpcEndpointServiceImpl()).besuPluginContext(mock(BesuPluginContextImpl.class)).build();
assertThat(runner.getJsonRpcPort()).isPresent();
assertThat(runner.getEngineJsonRpcPort()).isPresent();
}
use of org.hyperledger.besu.cli.config.EthNetworkConfig in project besu by hyperledger.
the class LocalPermissioningConfigurationValidatorTest method nodesAllowlistOptionWhichDoesNotIncludeBootnodesMustError.
@Test
public void nodesAllowlistOptionWhichDoesNotIncludeBootnodesMustError() throws Exception {
EthNetworkConfig ethNetworkConfig = EthNetworkConfig.getNetworkConfig(NetworkName.ROPSTEN);
final URL configFile = this.getClass().getResource(PERMISSIONING_CONFIG);
final Path toml = Files.createTempFile("toml", "");
toml.toFile().deleteOnExit();
Files.write(toml, Resources.toByteArray(configFile));
LocalPermissioningConfiguration permissioningConfiguration = PermissioningConfigurationBuilder.permissioningConfiguration(true, EnodeDnsConfiguration.DEFAULT_CONFIG, toml.toAbsolutePath().toString(), true, toml.toAbsolutePath().toString());
try {
final List<EnodeURL> enodeURIs = ethNetworkConfig.getBootNodes();
PermissioningConfigurationValidator.areAllNodesAreInAllowlist(enodeURIs, permissioningConfiguration);
fail("expected exception because ropsten bootnodes are not in node-allowlist");
} catch (Exception e) {
assertThat(e.getMessage()).startsWith("Specified node(s) not in nodes-allowlist");
assertThat(e.getMessage()).contains("enode://6332792c4a00e3e4ee0926ed89e0d27ef985424d97b6a45bf0f23e51f0dcb5e66b875777506458aea7af6f9e4ffb69f43f3778ee73c81ed9d34c51c4b16b0b0f@52.232.243.152:30303");
assertThat(e.getMessage()).contains("enode://94c15d1b9e2fe7ce56e458b9a3b672ef11894ddedd0c6f247e0f1d3487f52b66208fb4aeb8179fce6e3a749ea93ed147c37976d67af557508d199d9594c35f09@192.81.208.223:30303");
}
}
use of org.hyperledger.besu.cli.config.EthNetworkConfig in project besu by hyperledger.
the class ThreadBesuNodeRunner method startNode.
@Override
public void startNode(final BesuNode node) {
if (MDC.get("node") != null) {
LOG.error("ThreadContext node is already set to {}", MDC.get("node"));
}
MDC.put("node", node.getName());
if (!node.getRunCommand().isEmpty()) {
throw new UnsupportedOperationException("commands are not supported with thread runner");
}
final StorageServiceImpl storageService = new StorageServiceImpl();
final SecurityModuleServiceImpl securityModuleService = new SecurityModuleServiceImpl();
final Path dataDir = node.homeDirectory();
final BesuConfiguration commonPluginConfiguration = new BesuConfigurationImpl(dataDir, dataDir.resolve(DATABASE_PATH));
final BesuPluginContextImpl besuPluginContext = besuPluginContextMap.computeIfAbsent(node, n -> buildPluginContext(node, storageService, securityModuleService, commonPluginConfiguration));
final ObservableMetricsSystem metricsSystem = MetricsSystemFactory.create(node.getMetricsConfiguration());
final List<EnodeURL> bootnodes = node.getConfiguration().getBootnodes().stream().map(EnodeURLImpl::fromURI).collect(Collectors.toList());
final NetworkName network = node.getNetwork() == null ? NetworkName.DEV : node.getNetwork();
final EthNetworkConfig.Builder networkConfigBuilder = new EthNetworkConfig.Builder(EthNetworkConfig.getNetworkConfig(network)).setBootNodes(bootnodes);
node.getConfiguration().getGenesisConfig().ifPresent(networkConfigBuilder::setGenesisConfig);
final EthNetworkConfig ethNetworkConfig = networkConfigBuilder.build();
final BesuControllerBuilder builder = new BesuController.Builder().fromEthNetworkConfig(ethNetworkConfig);
final KeyValueStorageProvider storageProvider = new KeyValueStorageProviderBuilder().withStorageFactory(storageService.getByName("rocksdb").get()).withCommonConfiguration(commonPluginConfiguration).withMetricsSystem(metricsSystem).build();
final TransactionPoolConfiguration txPoolConfig = ImmutableTransactionPoolConfiguration.builder().strictTransactionReplayProtectionEnabled(node.isStrictTxReplayProtectionEnabled()).build();
final int maxPeers = 25;
builder.synchronizerConfiguration(new SynchronizerConfiguration.Builder().build()).dataDirectory(node.homeDirectory()).miningParameters(node.getMiningParameters()).privacyParameters(node.getPrivacyParameters()).nodeKey(new NodeKey(new KeyPairSecurityModule(KeyPairUtil.loadKeyPair(dataDir)))).metricsSystem(metricsSystem).transactionPoolConfiguration(txPoolConfig).ethProtocolConfiguration(EthProtocolConfiguration.defaultConfig()).clock(Clock.systemUTC()).isRevertReasonEnabled(node.isRevertReasonEnabled()).storageProvider(storageProvider).gasLimitCalculator(GasLimitCalculator.constant()).pkiBlockCreationConfiguration(node.getPkiKeyStoreConfiguration().map(pkiConfig -> new PkiBlockCreationConfigurationProvider().load(pkiConfig))).evmConfiguration(EvmConfiguration.DEFAULT).maxPeers(maxPeers);
node.getGenesisConfig().map(GenesisConfigFile::fromConfig).ifPresent(builder::genesisConfigFile);
final BesuController besuController = builder.build();
final RunnerBuilder runnerBuilder = new RunnerBuilder();
runnerBuilder.permissioningConfiguration(node.getPermissioningConfiguration());
final Runner runner = runnerBuilder.vertx(Vertx.vertx()).besuController(besuController).ethNetworkConfig(ethNetworkConfig).discovery(node.isDiscoveryEnabled()).p2pAdvertisedHost(node.getHostName()).p2pListenPort(0).maxPeers(maxPeers).networkingConfiguration(node.getNetworkingConfiguration()).jsonRpcConfiguration(node.jsonRpcConfiguration()).webSocketConfiguration(node.webSocketConfiguration()).jsonRpcIpcConfiguration(node.jsonRpcIpcConfiguration()).dataDir(node.homeDirectory()).metricsSystem(metricsSystem).permissioningService(new PermissioningServiceImpl()).metricsConfiguration(node.getMetricsConfiguration()).p2pEnabled(node.isP2pEnabled()).p2pTLSConfiguration(node.getTLSConfiguration()).graphQLConfiguration(GraphQLConfiguration.createDefault()).staticNodes(node.getStaticNodes().stream().map(EnodeURLImpl::fromString).collect(Collectors.toList())).besuPluginContext(new BesuPluginContextImpl()).autoLogBloomCaching(false).storageProvider(storageProvider).forkIdSupplier(() -> besuController.getProtocolManager().getForkIdAsBytesList()).rpcEndpointService(new RpcEndpointServiceImpl()).build();
besuPluginContext.beforeExternalServices();
runner.startExternalServices();
besuPluginContext.addService(BesuEvents.class, new BesuEventsImpl(besuController.getProtocolContext().getBlockchain(), besuController.getProtocolManager().getBlockBroadcaster(), besuController.getTransactionPool(), besuController.getSyncState()));
besuPluginContext.startPlugins();
runner.startEthereumMainLoop();
besuRunners.put(node.getName(), runner);
MDC.remove("node");
}
use of org.hyperledger.besu.cli.config.EthNetworkConfig in project besu by hyperledger.
the class BesuCommand method updateNetworkConfig.
private EthNetworkConfig updateNetworkConfig(final NetworkName network) {
final EthNetworkConfig.Builder builder = new EthNetworkConfig.Builder(EthNetworkConfig.getNetworkConfig(network));
if (genesisFile != null) {
if (commandLine.getParseResult().hasMatchedOption("network")) {
throw new ParameterException(this.commandLine, "--network option and --genesis-file option can't be used at the same time. Please " + "refer to CLI reference for more details about this constraint.");
}
builder.setGenesisConfig(genesisConfig());
if (networkId == null) {
// If no chain id is found in the genesis, use mainnet network id
try {
builder.setNetworkId(getGenesisConfigFile().getConfigOptions(genesisConfigOverrides).getChainId().orElse(EthNetworkConfig.getNetworkConfig(MAINNET).getNetworkId()));
} catch (final DecodeException e) {
throw new ParameterException(this.commandLine, String.format("Unable to parse genesis file %s.", genesisFile), e);
} catch (final ArithmeticException e) {
throw new ParameterException(this.commandLine, "No networkId specified and chainId in " + "genesis file is too large to be used as a networkId");
}
}
if (p2PDiscoveryOptionGroup.bootNodes == null) {
builder.setBootNodes(new ArrayList<>());
}
builder.setDnsDiscoveryUrl(null);
}
if (p2PDiscoveryOptionGroup.discoveryDnsUrl != null) {
builder.setDnsDiscoveryUrl(p2PDiscoveryOptionGroup.discoveryDnsUrl);
} else if (genesisConfigOptions != null) {
final Optional<String> discoveryDnsUrlFromGenesis = genesisConfigOptions.getDiscoveryOptions().getDiscoveryDnsUrl();
discoveryDnsUrlFromGenesis.ifPresent(builder::setDnsDiscoveryUrl);
}
if (networkId != null) {
builder.setNetworkId(networkId);
}
List<EnodeURL> listBootNodes = null;
if (p2PDiscoveryOptionGroup.bootNodes != null) {
try {
listBootNodes = buildEnodes(p2PDiscoveryOptionGroup.bootNodes, getEnodeDnsConfiguration());
} catch (final IllegalArgumentException e) {
throw new ParameterException(commandLine, e.getMessage());
}
} else if (genesisConfigOptions != null) {
final Optional<List<String>> bootNodesFromGenesis = genesisConfigOptions.getDiscoveryOptions().getBootNodes();
if (bootNodesFromGenesis.isPresent()) {
listBootNodes = buildEnodes(bootNodesFromGenesis.get(), getEnodeDnsConfiguration());
}
}
if (listBootNodes != null) {
if (!p2PDiscoveryOptionGroup.peerDiscoveryEnabled) {
logger.warn("Discovery disabled: bootnodes will be ignored.");
}
DiscoveryConfiguration.assertValidBootnodes(listBootNodes);
builder.setBootNodes(listBootNodes);
}
return builder.build();
}
Aggregations