Search in sources :

Example 1 with GenesisState

use of org.hyperledger.besu.ethereum.chain.GenesisState in project besu by hyperledger.

the class TransactionSmartContractPermissioningControllerTest method setupController.

private TransactionSmartContractPermissioningController setupController(final String resourceName, final String contractAddressString) throws IOException {
    final ProtocolSchedule protocolSchedule = ProtocolScheduleFixture.MAINNET;
    final String emptyContractFile = Resources.toString(this.getClass().getResource(resourceName), UTF_8);
    final GenesisState genesisState = GenesisState.fromConfig(GenesisConfigFile.fromConfig(emptyContractFile), protocolSchedule);
    final MutableBlockchain blockchain = createInMemoryBlockchain(genesisState.getBlock());
    final WorldStateArchive worldArchive = createInMemoryWorldStateArchive();
    genesisState.writeStateTo(worldArchive.getMutable());
    final TransactionSimulator ts = new TransactionSimulator(blockchain, worldArchive, protocolSchedule);
    final Address contractAddress = Address.fromHexString(contractAddressString);
    when(metricsSystem.createCounter(BesuMetricCategory.PERMISSIONING, "transaction_smart_contract_check_count", "Number of times the transaction smart contract permissioning provider has been checked")).thenReturn(checkCounter);
    when(metricsSystem.createCounter(BesuMetricCategory.PERMISSIONING, "transaction_smart_contract_check_count_permitted", "Number of times the transaction smart contract permissioning provider has been checked and returned permitted")).thenReturn(checkPermittedCounter);
    when(metricsSystem.createCounter(BesuMetricCategory.PERMISSIONING, "transaction_smart_contract_check_count_unpermitted", "Number of times the transaction smart contract permissioning provider has been checked and returned unpermitted")).thenReturn(checkUnpermittedCounter);
    return new TransactionSmartContractPermissioningController(contractAddress, ts, metricsSystem);
}
Also used : GenesisState(org.hyperledger.besu.ethereum.chain.GenesisState) Address(org.hyperledger.besu.datatypes.Address) InMemoryKeyValueStorageProvider.createInMemoryWorldStateArchive(org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider.createInMemoryWorldStateArchive) WorldStateArchive(org.hyperledger.besu.ethereum.worldstate.WorldStateArchive) MutableBlockchain(org.hyperledger.besu.ethereum.chain.MutableBlockchain) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) TransactionSimulator(org.hyperledger.besu.ethereum.transaction.TransactionSimulator)

Example 2 with GenesisState

use of org.hyperledger.besu.ethereum.chain.GenesisState 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 GenesisState

use of org.hyperledger.besu.ethereum.chain.GenesisState in project besu by hyperledger.

the class MergeBesuControllerBuilderTest method assertBuiltContextMonitorsTTD.

@Test
public void assertBuiltContextMonitorsTTD() {
    final GenesisState genesisState = GenesisState.fromConfig(genesisConfigFile, this.besuControllerBuilder.createProtocolSchedule());
    final MutableBlockchain blockchain = createInMemoryBlockchain(genesisState.getBlock());
    final MergeContext mergeContext = spy(besuControllerBuilder.createConsensusContext(blockchain, mock(WorldStateArchive.class), this.besuControllerBuilder.createProtocolSchedule()));
    assertThat(mergeContext).isNotNull();
    Difficulty over = Difficulty.of(10000L);
    Difficulty under = Difficulty.of(10L);
    BlockHeader parent = headerGenerator.difficulty(under).parentHash(genesisState.getBlock().getHash()).number(genesisState.getBlock().getHeader().getNumber() + 1).gasLimit(genesisState.getBlock().getHeader().getGasLimit()).stateRoot(genesisState.getBlock().getHeader().getStateRoot()).buildHeader();
    blockchain.appendBlock(new Block(parent, BlockBody.empty()), Collections.emptyList());
    BlockHeader terminal = headerGenerator.difficulty(over).parentHash(parent.getHash()).number(parent.getNumber() + 1).gasLimit(parent.getGasLimit()).stateRoot(parent.getStateRoot()).buildHeader();
    blockchain.appendBlock(new Block(terminal, BlockBody.empty()), Collections.emptyList());
    assertThat(mergeContext.isPostMerge()).isTrue();
}
Also used : GenesisState(org.hyperledger.besu.ethereum.chain.GenesisState) Difficulty(org.hyperledger.besu.ethereum.core.Difficulty) Block(org.hyperledger.besu.ethereum.core.Block) MutableBlockchain(org.hyperledger.besu.ethereum.chain.MutableBlockchain) MergeContext(org.hyperledger.besu.consensus.merge.MergeContext) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Test(org.junit.Test)

Example 4 with GenesisState

use of org.hyperledger.besu.ethereum.chain.GenesisState 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 5 with GenesisState

use of org.hyperledger.besu.ethereum.chain.GenesisState in project besu by hyperledger.

the class RetestethContext method buildContext.

private boolean buildContext(final String genesisConfigString, final String sealEngine, final Optional<Long> clockTime) {
    final ObjectNode genesisConfig = normalizeKeys(JsonUtil.objectNodeFromString(genesisConfigString));
    retestethClock = new RetestethClock();
    clockTime.ifPresent(retestethClock::resetTime);
    final MetricsSystem metricsSystem = new NoOpMetricsSystem();
    final JsonGenesisConfigOptions jsonGenesisConfigOptions = JsonGenesisConfigOptions.fromJsonObject(JsonUtil.getObjectNode(genesisConfig, "config").get());
    protocolSchedule = MainnetProtocolSchedule.fromConfig(jsonGenesisConfigOptions, EvmConfiguration.DEFAULT);
    if ("NoReward".equalsIgnoreCase(sealEngine)) {
        protocolSchedule = new NoRewardProtocolScheduleWrapper(protocolSchedule);
    }
    blockHeaderFunctions = ScheduleBasedBlockHeaderFunctions.create(protocolSchedule);
    final GenesisState genesisState = GenesisState.fromJson(genesisConfigString, protocolSchedule);
    coinbase = genesisState.getBlock().getHeader().getCoinbase();
    extraData = genesisState.getBlock().getHeader().getExtraData();
    final WorldStateArchive worldStateArchive = new DefaultWorldStateArchive(new WorldStateKeyValueStorage(new InMemoryKeyValueStorage()), new WorldStatePreimageKeyValueStorage(new InMemoryKeyValueStorage()));
    final MutableWorldState worldState = worldStateArchive.getMutable();
    genesisState.writeStateTo(worldState);
    blockchain = createInMemoryBlockchain(genesisState.getBlock());
    protocolContext = new ProtocolContext(blockchain, worldStateArchive, null);
    blockchainQueries = new BlockchainQueries(blockchain, worldStateArchive, ethScheduler);
    final String sealengine = JsonUtil.getString(genesisConfig, "sealengine", "");
    headerValidationMode = "NoProof".equals(sealengine) || "NoReward".equals(sealEngine) ? HeaderValidationMode.LIGHT : HeaderValidationMode.FULL;
    final Iterable<Long> nonceGenerator = new IncrementingNonceGenerator(0);
    poWSolver = ("NoProof".equals(sealengine) || "NoReward".equals(sealEngine)) ? new PoWSolver(nonceGenerator, NO_WORK_HASHER, false, Subscribers.none(), new EpochCalculator.DefaultEpochCalculator(), 1000, 8) : new PoWSolver(nonceGenerator, PoWHasher.ETHASH_LIGHT, false, Subscribers.none(), new EpochCalculator.DefaultEpochCalculator(), 1000, 8);
    blockReplay = new BlockReplay(protocolSchedule, blockchainQueries.getBlockchain(), blockchainQueries.getWorldStateArchive());
    // mining support
    final EthPeers ethPeers = new EthPeers("reteseth", retestethClock, metricsSystem, 0);
    final SyncState syncState = new SyncState(blockchain, ethPeers);
    ethScheduler = new EthScheduler(1, 1, 1, 1, metricsSystem);
    final EthContext ethContext = new EthContext(ethPeers, new EthMessages(), ethScheduler);
    final TransactionPoolConfiguration transactionPoolConfiguration = ImmutableTransactionPoolConfiguration.builder().build();
    transactionPool = TransactionPoolFactory.createTransactionPool(protocolSchedule, protocolContext, ethContext, retestethClock, metricsSystem, syncState, new MiningParameters.Builder().minTransactionGasPrice(Wei.ZERO).build(), transactionPoolConfiguration);
    if (LOG.isTraceEnabled()) {
        LOG.trace("Genesis Block {} ", genesisState.getBlock());
    }
    return true;
}
Also used : GenesisState(org.hyperledger.besu.ethereum.chain.GenesisState) EthContext(org.hyperledger.besu.ethereum.eth.manager.EthContext) MutableWorldState(org.hyperledger.besu.ethereum.core.MutableWorldState) TransactionPoolConfiguration(org.hyperledger.besu.ethereum.eth.transactions.TransactionPoolConfiguration) ImmutableTransactionPoolConfiguration(org.hyperledger.besu.ethereum.eth.transactions.ImmutableTransactionPoolConfiguration) EpochCalculator(org.hyperledger.besu.ethereum.mainnet.EpochCalculator) EthPeers(org.hyperledger.besu.ethereum.eth.manager.EthPeers) WorldStatePreimageKeyValueStorage(org.hyperledger.besu.ethereum.storage.keyvalue.WorldStatePreimageKeyValueStorage) WorldStateKeyValueStorage(org.hyperledger.besu.ethereum.storage.keyvalue.WorldStateKeyValueStorage) BlockchainQueries(org.hyperledger.besu.ethereum.api.query.BlockchainQueries) SyncState(org.hyperledger.besu.ethereum.eth.sync.state.SyncState) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) MetricsSystem(org.hyperledger.besu.plugin.services.MetricsSystem) NoOpMetricsSystem(org.hyperledger.besu.metrics.noop.NoOpMetricsSystem) EthMessages(org.hyperledger.besu.ethereum.eth.manager.EthMessages) DefaultWorldStateArchive(org.hyperledger.besu.ethereum.worldstate.DefaultWorldStateArchive) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) InMemoryKeyValueStorage(org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage) BlockReplay(org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.BlockReplay) EthScheduler(org.hyperledger.besu.ethereum.eth.manager.EthScheduler) JsonGenesisConfigOptions(org.hyperledger.besu.config.JsonGenesisConfigOptions) DefaultWorldStateArchive(org.hyperledger.besu.ethereum.worldstate.DefaultWorldStateArchive) WorldStateArchive(org.hyperledger.besu.ethereum.worldstate.WorldStateArchive) NoOpMetricsSystem(org.hyperledger.besu.metrics.noop.NoOpMetricsSystem) IncrementingNonceGenerator(org.hyperledger.besu.ethereum.blockcreation.IncrementingNonceGenerator) PoWSolver(org.hyperledger.besu.ethereum.mainnet.PoWSolver)

Aggregations

GenesisState (org.hyperledger.besu.ethereum.chain.GenesisState)9 ProtocolSchedule (org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule)7 MutableBlockchain (org.hyperledger.besu.ethereum.chain.MutableBlockchain)6 WorldStateArchive (org.hyperledger.besu.ethereum.worldstate.WorldStateArchive)6 ProtocolContext (org.hyperledger.besu.ethereum.ProtocolContext)4 InMemoryKeyValueStorageProvider.createInMemoryWorldStateArchive (org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider.createInMemoryWorldStateArchive)4 ArrayList (java.util.ArrayList)3 GenesisConfigFile (org.hyperledger.besu.config.GenesisConfigFile)3 Address (org.hyperledger.besu.datatypes.Address)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Bytes (org.apache.tuweni.bytes.Bytes)2 GenesisConfigOptions (org.hyperledger.besu.config.GenesisConfigOptions)2 InMemoryKeyValueStorageProvider.createInMemoryBlockchain (org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider.createInMemoryBlockchain)2 MainnetProtocolSchedule (org.hyperledger.besu.ethereum.mainnet.MainnetProtocolSchedule)2 TransactionSimulator (org.hyperledger.besu.ethereum.transaction.TransactionSimulator)2 DefaultWorldStateArchive (org.hyperledger.besu.ethereum.worldstate.DefaultWorldStateArchive)2