Search in sources :

Example 1 with GenesisConfigFile

use of org.hyperledger.besu.config.GenesisConfigFile in project besu by hyperledger.

the class CliqueConditions method cliqueBlockPeriod.

private int cliqueBlockPeriod(final BesuNode node) {
    final String config = node.getGenesisConfigProvider().create(emptyList()).get();
    final GenesisConfigFile genesisConfigFile = GenesisConfigFile.fromConfig(config);
    final CliqueConfigOptions cliqueConfigOptions = genesisConfigFile.getConfigOptions().getCliqueConfigOptions();
    return cliqueConfigOptions.getBlockPeriodSeconds();
}
Also used : CliqueConfigOptions(org.hyperledger.besu.config.CliqueConfigOptions) GenesisConfigFile(org.hyperledger.besu.config.GenesisConfigFile) Hash.fromHexString(org.hyperledger.besu.datatypes.Hash.fromHexString)

Example 2 with GenesisConfigFile

use of org.hyperledger.besu.config.GenesisConfigFile in project besu by hyperledger.

the class ForkIdsTest method testForkId.

@Test
public void testForkId() {
    final GenesisConfigFile genesisConfigFile = GenesisConfigFile.fromConfig(EthNetworkConfig.jsonConfig(chainName));
    final GenesisConfigOptions configOptions = genesisConfigFile.getConfigOptions();
    final ProtocolSchedule schedule = MainnetProtocolSchedule.fromConfig(configOptions, EvmConfiguration.DEFAULT);
    final GenesisState genesisState = GenesisState.fromConfig(genesisConfigFile, schedule);
    final Blockchain mockBlockchain = mock(Blockchain.class);
    when(mockBlockchain.getGenesisBlock()).thenReturn(genesisState.getBlock());
    final AtomicLong blockNumber = new AtomicLong();
    when(mockBlockchain.getChainHeadBlockNumber()).thenAnswer(o -> blockNumber.get());
    final ForkIdManager forkIdManager = new ForkIdManager(mockBlockchain, genesisConfigFile.getForks(), false);
    final var actualForkIds = Streams.concat(((MutableProtocolSchedule) schedule).streamMilestoneBlocks(), Stream.of(Long.MAX_VALUE)).map(block -> {
        blockNumber.set(block);
        return forkIdManager.getForkIdForChainHead();
    }).collect(Collectors.toList());
    assertThat(actualForkIds).containsExactlyElementsOf(expectedForkIds);
}
Also used : EthNetworkConfig(org.hyperledger.besu.cli.config.EthNetworkConfig) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) GenesisConfigOptions(org.hyperledger.besu.config.GenesisConfigOptions) Bytes(org.apache.tuweni.bytes.Bytes) GenesisState(org.hyperledger.besu.ethereum.chain.GenesisState) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) NetworkName(org.hyperledger.besu.cli.config.NetworkName) GenesisConfigFile(org.hyperledger.besu.config.GenesisConfigFile) ForkId(org.hyperledger.besu.ethereum.eth.manager.ForkId) Parameterized(org.junit.runners.Parameterized) Collection(java.util.Collection) Blockchain(org.hyperledger.besu.ethereum.chain.Blockchain) MainnetProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.MainnetProtocolSchedule) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) ForkIdManager(org.hyperledger.besu.ethereum.eth.manager.ForkIdManager) Streams(com.google.common.collect.Streams) EvmConfiguration(org.hyperledger.besu.evm.internal.EvmConfiguration) Collectors(java.util.stream.Collectors) MutableProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.MutableProtocolSchedule) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Stream(java.util.stream.Stream) Mockito.mock(org.mockito.Mockito.mock) GenesisState(org.hyperledger.besu.ethereum.chain.GenesisState) AtomicLong(java.util.concurrent.atomic.AtomicLong) GenesisConfigFile(org.hyperledger.besu.config.GenesisConfigFile) Blockchain(org.hyperledger.besu.ethereum.chain.Blockchain) GenesisConfigOptions(org.hyperledger.besu.config.GenesisConfigOptions) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) MainnetProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.MainnetProtocolSchedule) MutableProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.MutableProtocolSchedule) ForkIdManager(org.hyperledger.besu.ethereum.eth.manager.ForkIdManager) Test(org.junit.Test)

Example 3 with GenesisConfigFile

use of org.hyperledger.besu.config.GenesisConfigFile in project besu by hyperledger.

the class BlockchainSetupUtil method create.

private static BlockchainSetupUtil create(final ChainResources chainResources, final DataStorageFormat storageFormat, final ProtocolScheduleProvider protocolScheduleProvider, final ProtocolContextProvider protocolContextProvider, final EthScheduler scheduler) {
    final TemporaryFolder temp = new TemporaryFolder();
    try {
        temp.create();
        final String genesisJson = Resources.toString(chainResources.getGenesisURL(), Charsets.UTF_8);
        final GenesisConfigFile genesisConfigFile = GenesisConfigFile.fromConfig(genesisJson);
        final ProtocolSchedule protocolSchedule = protocolScheduleProvider.get(genesisConfigFile);
        final GenesisState genesisState = GenesisState.fromJson(genesisJson, protocolSchedule);
        final MutableBlockchain blockchain = createInMemoryBlockchain(genesisState.getBlock());
        final WorldStateArchive worldArchive = storageFormat == DataStorageFormat.BONSAI ? createBonsaiInMemoryWorldStateArchive(blockchain) : createInMemoryWorldStateArchive();
        final TransactionPool transactionPool = mock(TransactionPool.class);
        genesisState.writeStateTo(worldArchive.getMutable());
        final ProtocolContext protocolContext = protocolContextProvider.get(blockchain, worldArchive);
        final Path blocksPath = Path.of(chainResources.getBlocksURL().toURI());
        final List<Block> blocks = new ArrayList<>();
        final BlockHeaderFunctions blockHeaderFunctions = ScheduleBasedBlockHeaderFunctions.create(protocolSchedule);
        try (final RawBlockIterator iterator = new RawBlockIterator(blocksPath, rlp -> BlockHeader.readFrom(rlp, blockHeaderFunctions))) {
            while (iterator.hasNext()) {
                blocks.add(iterator.next());
            }
        }
        return new BlockchainSetupUtil(genesisState, blockchain, protocolContext, protocolSchedule, worldArchive, transactionPool, blocks, scheduler);
    } catch (final IOException | URISyntaxException ex) {
        throw new IllegalStateException(ex);
    } finally {
        temp.delete();
    }
}
Also used : Path(java.nio.file.Path) GenesisState(org.hyperledger.besu.ethereum.chain.GenesisState) TransactionPool(org.hyperledger.besu.ethereum.eth.transactions.TransactionPool) ScheduleBasedBlockHeaderFunctions(org.hyperledger.besu.ethereum.mainnet.ScheduleBasedBlockHeaderFunctions) GenesisConfigFile(org.hyperledger.besu.config.GenesisConfigFile) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) MainnetProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.MainnetProtocolSchedule) InMemoryKeyValueStorageProvider.createBonsaiInMemoryWorldStateArchive(org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider.createBonsaiInMemoryWorldStateArchive) InMemoryKeyValueStorageProvider.createInMemoryWorldStateArchive(org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider.createInMemoryWorldStateArchive) WorldStateArchive(org.hyperledger.besu.ethereum.worldstate.WorldStateArchive) TemporaryFolder(org.junit.rules.TemporaryFolder) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) MutableBlockchain(org.hyperledger.besu.ethereum.chain.MutableBlockchain) RawBlockIterator(org.hyperledger.besu.ethereum.util.RawBlockIterator)

Example 4 with GenesisConfigFile

use of org.hyperledger.besu.config.GenesisConfigFile 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");
}
Also used : BesuPluginContextImpl(org.hyperledger.besu.services.BesuPluginContextImpl) KeyPairUtil(org.hyperledger.besu.crypto.KeyPairUtil) EnodeURLImpl(org.hyperledger.besu.ethereum.p2p.peers.EnodeURLImpl) LoggerFactory(org.slf4j.LoggerFactory) RocksDBPlugin(org.hyperledger.besu.plugin.services.storage.rocksdb.RocksDBPlugin) Runner(org.hyperledger.besu.Runner) NetworkName(org.hyperledger.besu.cli.config.NetworkName) GenesisConfigFile(org.hyperledger.besu.config.GenesisConfigFile) Map(java.util.Map) BesuPluginContextImpl(org.hyperledger.besu.services.BesuPluginContextImpl) Path(java.nio.file.Path) CommandLine(picocli.CommandLine) SecurityModuleService(org.hyperledger.besu.plugin.services.SecurityModuleService) EthProtocolConfiguration(org.hyperledger.besu.ethereum.eth.EthProtocolConfiguration) PkiBlockCreationConfigurationProvider(org.hyperledger.besu.consensus.qbft.pki.PkiBlockCreationConfigurationProvider) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PicoCLIOptions(org.hyperledger.besu.plugin.services.PicoCLIOptions) DATABASE_PATH(org.hyperledger.besu.controller.BesuController.DATABASE_PATH) EvmConfiguration(org.hyperledger.besu.evm.internal.EvmConfiguration) Collectors(java.util.stream.Collectors) BesuConfigurationImpl(org.hyperledger.besu.services.BesuConfigurationImpl) List(java.util.List) TransactionPoolConfiguration(org.hyperledger.besu.ethereum.eth.transactions.TransactionPoolConfiguration) KeyValueStorageProvider(org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueStorageProvider) BesuConfiguration(org.hyperledger.besu.plugin.services.BesuConfiguration) BesuEventsImpl(org.hyperledger.besu.services.BesuEventsImpl) GasLimitCalculator(org.hyperledger.besu.ethereum.GasLimitCalculator) NodeKey(org.hyperledger.besu.crypto.NodeKey) BesuEvents(org.hyperledger.besu.plugin.services.BesuEvents) KeyPairSecurityModule(org.hyperledger.besu.crypto.KeyPairSecurityModule) ImmutableTransactionPoolConfiguration(org.hyperledger.besu.ethereum.eth.transactions.ImmutableTransactionPoolConfiguration) EthNetworkConfig(org.hyperledger.besu.cli.config.EthNetworkConfig) BesuControllerBuilder(org.hyperledger.besu.controller.BesuControllerBuilder) HashMap(java.util.HashMap) SynchronizerConfiguration(org.hyperledger.besu.ethereum.eth.sync.SynchronizerConfiguration) RunnerBuilder(org.hyperledger.besu.RunnerBuilder) BesuController(org.hyperledger.besu.controller.BesuController) StorageService(org.hyperledger.besu.plugin.services.StorageService) HashSet(java.util.HashSet) PermissioningServiceImpl(org.hyperledger.besu.services.PermissioningServiceImpl) RpcEndpointServiceImpl(org.hyperledger.besu.services.RpcEndpointServiceImpl) Logger(org.slf4j.Logger) MetricsSystemFactory(org.hyperledger.besu.metrics.MetricsSystemFactory) SecurityModuleServiceImpl(org.hyperledger.besu.services.SecurityModuleServiceImpl) StorageServiceImpl(org.hyperledger.besu.services.StorageServiceImpl) KeyValueStorageProviderBuilder(org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueStorageProviderBuilder) Vertx(io.vertx.core.Vertx) GraphQLConfiguration(org.hyperledger.besu.ethereum.api.graphql.GraphQLConfiguration) File(java.io.File) EnodeURL(org.hyperledger.besu.plugin.data.EnodeURL) PicoCLIOptionsImpl(org.hyperledger.besu.services.PicoCLIOptionsImpl) ObservableMetricsSystem(org.hyperledger.besu.metrics.ObservableMetricsSystem) MDC(org.slf4j.MDC) Clock(java.time.Clock) CommandSpec(picocli.CommandLine.Model.CommandSpec) Runner(org.hyperledger.besu.Runner) NetworkName(org.hyperledger.besu.cli.config.NetworkName) BesuController(org.hyperledger.besu.controller.BesuController) KeyValueStorageProvider(org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueStorageProvider) TransactionPoolConfiguration(org.hyperledger.besu.ethereum.eth.transactions.TransactionPoolConfiguration) ImmutableTransactionPoolConfiguration(org.hyperledger.besu.ethereum.eth.transactions.ImmutableTransactionPoolConfiguration) BesuControllerBuilder(org.hyperledger.besu.controller.BesuControllerBuilder) RunnerBuilder(org.hyperledger.besu.RunnerBuilder) KeyValueStorageProviderBuilder(org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueStorageProviderBuilder) BesuEventsImpl(org.hyperledger.besu.services.BesuEventsImpl) BesuControllerBuilder(org.hyperledger.besu.controller.BesuControllerBuilder) KeyValueStorageProviderBuilder(org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueStorageProviderBuilder) EnodeURL(org.hyperledger.besu.plugin.data.EnodeURL) RunnerBuilder(org.hyperledger.besu.RunnerBuilder) SecurityModuleServiceImpl(org.hyperledger.besu.services.SecurityModuleServiceImpl) NodeKey(org.hyperledger.besu.crypto.NodeKey) StorageServiceImpl(org.hyperledger.besu.services.StorageServiceImpl) Path(java.nio.file.Path) PkiBlockCreationConfigurationProvider(org.hyperledger.besu.consensus.qbft.pki.PkiBlockCreationConfigurationProvider) RpcEndpointServiceImpl(org.hyperledger.besu.services.RpcEndpointServiceImpl) BesuConfiguration(org.hyperledger.besu.plugin.services.BesuConfiguration) EnodeURLImpl(org.hyperledger.besu.ethereum.p2p.peers.EnodeURLImpl) PermissioningServiceImpl(org.hyperledger.besu.services.PermissioningServiceImpl) ObservableMetricsSystem(org.hyperledger.besu.metrics.ObservableMetricsSystem) KeyPairSecurityModule(org.hyperledger.besu.crypto.KeyPairSecurityModule) EthNetworkConfig(org.hyperledger.besu.cli.config.EthNetworkConfig) BesuConfigurationImpl(org.hyperledger.besu.services.BesuConfigurationImpl)

Example 5 with GenesisConfigFile

use of org.hyperledger.besu.config.GenesisConfigFile in project besu by hyperledger.

the class BesuCommand method readGenesisConfigOptions.

private GenesisConfigOptions readGenesisConfigOptions() {
    try {
        final GenesisConfigFile genesisConfigFile = GenesisConfigFile.fromConfig(genesisConfig());
        genesisConfigOptions = genesisConfigFile.getConfigOptions(genesisConfigOverrides);
    } catch (final Exception e) {
        throw new ParameterException(this.commandLine, "Unable to load genesis file. " + e.getCause());
    }
    return genesisConfigOptions;
}
Also used : GenesisConfigFile(org.hyperledger.besu.config.GenesisConfigFile) ParameterException(picocli.CommandLine.ParameterException) ParameterException(picocli.CommandLine.ParameterException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ExecutionException(picocli.CommandLine.ExecutionException) DecodeException(io.vertx.core.json.DecodeException) KeyManagementException(java.security.KeyManagementException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SocketException(java.net.SocketException) StorageException(org.hyperledger.besu.plugin.services.exception.StorageException) LauncherException(net.consensys.quorum.mainnet.launcher.exception.LauncherException)

Aggregations

GenesisConfigFile (org.hyperledger.besu.config.GenesisConfigFile)7 GenesisState (org.hyperledger.besu.ethereum.chain.GenesisState)3 ProtocolSchedule (org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule)3 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 EthNetworkConfig (org.hyperledger.besu.cli.config.EthNetworkConfig)2 NetworkName (org.hyperledger.besu.cli.config.NetworkName)2 MainnetProtocolSchedule (org.hyperledger.besu.ethereum.mainnet.MainnetProtocolSchedule)2 EvmConfiguration (org.hyperledger.besu.evm.internal.EvmConfiguration)2 Streams (com.google.common.collect.Streams)1 Vertx (io.vertx.core.Vertx)1 DecodeException (io.vertx.core.json.DecodeException)1 File (java.io.File)1 SocketException (java.net.SocketException)1 URISyntaxException (java.net.URISyntaxException)1 UnknownHostException (java.net.UnknownHostException)1 KeyManagementException (java.security.KeyManagementException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1