Search in sources :

Example 1 with PoWSolver

use of org.hyperledger.besu.ethereum.mainnet.PoWSolver in project besu by hyperledger.

the class PoWBlockCreatorTest method createMainnetBlock1_fixedDifficulty1.

@Test
public void createMainnetBlock1_fixedDifficulty1() {
    final GenesisConfigOptions genesisConfigOptions = GenesisConfigFile.fromConfig("{\"config\": {\"ethash\": {\"fixeddifficulty\":1}}}").getConfigOptions();
    final ExecutionContextTestFixture executionContextTestFixture = ExecutionContextTestFixture.builder().protocolSchedule(new ProtocolScheduleBuilder(genesisConfigOptions, BigInteger.valueOf(42), ProtocolSpecAdapters.create(0, Function.identity()), PrivacyParameters.DEFAULT, false, genesisConfigOptions.isQuorum(), EvmConfiguration.DEFAULT).createProtocolSchedule()).build();
    final PoWSolver solver = new PoWSolver(Lists.newArrayList(BLOCK_1_NONCE), PoWHasher.ETHASH_LIGHT, false, Subscribers.none(), new EpochCalculator.DefaultEpochCalculator(), 1000, 8);
    final BaseFeePendingTransactionsSorter pendingTransactions = new BaseFeePendingTransactionsSorter(TransactionPoolConfiguration.DEFAULT_TX_RETENTION_HOURS, 1, TestClock.fixed(), metricsSystem, executionContextTestFixture.getProtocolContext().getBlockchain()::getChainHeadHeader, TransactionPoolConfiguration.DEFAULT_PRICE_BUMP);
    final PoWBlockCreator blockCreator = new PoWBlockCreator(BLOCK_1_COINBASE, () -> Optional.empty(), parent -> BLOCK_1_EXTRA_DATA, pendingTransactions, executionContextTestFixture.getProtocolContext(), executionContextTestFixture.getProtocolSchedule(), solver, Wei.ZERO, 0.8, executionContextTestFixture.getBlockchain().getChainHeadHeader());
    blockCreator.createBlock(BLOCK_1_TIMESTAMP);
// If we weren't setting difficulty to 2^256-1 a difficulty of 1 would have caused a
// IllegalArgumentException at the previous line, as 2^256 is 33 bytes.
}
Also used : EpochCalculator(org.hyperledger.besu.ethereum.mainnet.EpochCalculator) BaseFeePendingTransactionsSorter(org.hyperledger.besu.ethereum.eth.transactions.sorter.BaseFeePendingTransactionsSorter) ExecutionContextTestFixture(org.hyperledger.besu.ethereum.core.ExecutionContextTestFixture) GenesisConfigOptions(org.hyperledger.besu.config.GenesisConfigOptions) ProtocolScheduleBuilder(org.hyperledger.besu.ethereum.mainnet.ProtocolScheduleBuilder) PoWSolver(org.hyperledger.besu.ethereum.mainnet.PoWSolver) Test(org.junit.Test)

Example 2 with PoWSolver

use of org.hyperledger.besu.ethereum.mainnet.PoWSolver in project besu by hyperledger.

the class PoWBlockCreatorTest method rewardBeneficiary_zeroReward_skipZeroRewardsTrue.

@Test
public void rewardBeneficiary_zeroReward_skipZeroRewardsTrue() {
    final GenesisConfigOptions genesisConfigOptions = GenesisConfigFile.fromConfig("{\"config\": {\"ethash\": {\"fixeddifficulty\":1}}}").getConfigOptions();
    final ExecutionContextTestFixture executionContextTestFixture = ExecutionContextTestFixture.builder().protocolSchedule(new ProtocolScheduleBuilder(genesisConfigOptions, BigInteger.valueOf(42), ProtocolSpecAdapters.create(0, Function.identity()), PrivacyParameters.DEFAULT, false, genesisConfigOptions.isQuorum(), EvmConfiguration.DEFAULT).createProtocolSchedule()).build();
    final PoWSolver solver = new PoWSolver(Lists.newArrayList(BLOCK_1_NONCE), PoWHasher.ETHASH_LIGHT, false, Subscribers.none(), new EpochCalculator.DefaultEpochCalculator(), 1000, 8);
    final BaseFeePendingTransactionsSorter pendingTransactions = new BaseFeePendingTransactionsSorter(TransactionPoolConfiguration.DEFAULT_TX_RETENTION_HOURS, 1, TestClock.fixed(), metricsSystem, executionContextTestFixture.getProtocolContext().getBlockchain()::getChainHeadHeader, TransactionPoolConfiguration.DEFAULT_PRICE_BUMP);
    final PoWBlockCreator blockCreator = new PoWBlockCreator(BLOCK_1_COINBASE, () -> Optional.of(10_000_000L), parent -> BLOCK_1_EXTRA_DATA, pendingTransactions, executionContextTestFixture.getProtocolContext(), executionContextTestFixture.getProtocolSchedule(), solver, Wei.ZERO, 0.8, executionContextTestFixture.getBlockchain().getChainHeadHeader());
    final MutableWorldState mutableWorldState = executionContextTestFixture.getStateArchive().getMutable();
    assertThat(mutableWorldState.get(BLOCK_1_COINBASE)).isNull();
    final ProcessableBlockHeader header = BlockHeaderBuilder.create().parentHash(Hash.ZERO).coinbase(BLOCK_1_COINBASE).difficulty(Difficulty.ONE).number(1).gasLimit(1).timestamp(1).buildProcessableBlockHeader();
    blockCreator.rewardBeneficiary(mutableWorldState, header, Collections.emptyList(), BLOCK_1_COINBASE, Wei.ZERO, true);
    assertThat(mutableWorldState.get(BLOCK_1_COINBASE)).isNull();
}
Also used : MutableWorldState(org.hyperledger.besu.ethereum.core.MutableWorldState) EpochCalculator(org.hyperledger.besu.ethereum.mainnet.EpochCalculator) BaseFeePendingTransactionsSorter(org.hyperledger.besu.ethereum.eth.transactions.sorter.BaseFeePendingTransactionsSorter) ProcessableBlockHeader(org.hyperledger.besu.ethereum.core.ProcessableBlockHeader) ExecutionContextTestFixture(org.hyperledger.besu.ethereum.core.ExecutionContextTestFixture) GenesisConfigOptions(org.hyperledger.besu.config.GenesisConfigOptions) ProtocolScheduleBuilder(org.hyperledger.besu.ethereum.mainnet.ProtocolScheduleBuilder) PoWSolver(org.hyperledger.besu.ethereum.mainnet.PoWSolver) Test(org.junit.Test)

Example 3 with PoWSolver

use of org.hyperledger.besu.ethereum.mainnet.PoWSolver in project besu by hyperledger.

the class PoWBlockCreatorTest method rewardBeneficiary_zeroReward_skipZeroRewardsFalse.

@Test
public void rewardBeneficiary_zeroReward_skipZeroRewardsFalse() {
    final GenesisConfigOptions genesisConfigOptions = GenesisConfigFile.fromConfig("{\"config\": {\"ethash\": {\"fixeddifficulty\":1}}}").getConfigOptions();
    final ExecutionContextTestFixture executionContextTestFixture = ExecutionContextTestFixture.builder().protocolSchedule(new ProtocolScheduleBuilder(genesisConfigOptions, BigInteger.valueOf(42), ProtocolSpecAdapters.create(0, Function.identity()), PrivacyParameters.DEFAULT, false, genesisConfigOptions.isQuorum(), EvmConfiguration.DEFAULT).createProtocolSchedule()).build();
    final PoWSolver solver = new PoWSolver(Lists.newArrayList(BLOCK_1_NONCE), PoWHasher.ETHASH_LIGHT, false, Subscribers.none(), new EpochCalculator.DefaultEpochCalculator(), 1000, 8);
    final BaseFeePendingTransactionsSorter pendingTransactions = new BaseFeePendingTransactionsSorter(TransactionPoolConfiguration.DEFAULT_TX_RETENTION_HOURS, 1, TestClock.fixed(), metricsSystem, executionContextTestFixture.getProtocolContext().getBlockchain()::getChainHeadHeader, TransactionPoolConfiguration.DEFAULT_PRICE_BUMP);
    final PoWBlockCreator blockCreator = new PoWBlockCreator(BLOCK_1_COINBASE, () -> Optional.of(10_000_000L), parent -> BLOCK_1_EXTRA_DATA, pendingTransactions, executionContextTestFixture.getProtocolContext(), executionContextTestFixture.getProtocolSchedule(), solver, Wei.ZERO, 0.8, executionContextTestFixture.getBlockchain().getChainHeadHeader());
    final MutableWorldState mutableWorldState = executionContextTestFixture.getStateArchive().getMutable();
    assertThat(mutableWorldState.get(BLOCK_1_COINBASE)).isNull();
    final ProcessableBlockHeader header = BlockHeaderBuilder.create().parentHash(Hash.ZERO).coinbase(BLOCK_1_COINBASE).difficulty(Difficulty.ONE).number(1).gasLimit(1).timestamp(1).buildProcessableBlockHeader();
    blockCreator.rewardBeneficiary(mutableWorldState, header, Collections.emptyList(), BLOCK_1_COINBASE, Wei.ZERO, false);
    assertThat(mutableWorldState.get(BLOCK_1_COINBASE)).isNotNull();
    assertThat(mutableWorldState.get(BLOCK_1_COINBASE).getBalance()).isEqualTo(Wei.ZERO);
}
Also used : MutableWorldState(org.hyperledger.besu.ethereum.core.MutableWorldState) EpochCalculator(org.hyperledger.besu.ethereum.mainnet.EpochCalculator) BaseFeePendingTransactionsSorter(org.hyperledger.besu.ethereum.eth.transactions.sorter.BaseFeePendingTransactionsSorter) ProcessableBlockHeader(org.hyperledger.besu.ethereum.core.ProcessableBlockHeader) ExecutionContextTestFixture(org.hyperledger.besu.ethereum.core.ExecutionContextTestFixture) GenesisConfigOptions(org.hyperledger.besu.config.GenesisConfigOptions) ProtocolScheduleBuilder(org.hyperledger.besu.ethereum.mainnet.ProtocolScheduleBuilder) PoWSolver(org.hyperledger.besu.ethereum.mainnet.PoWSolver) Test(org.junit.Test)

Example 4 with PoWSolver

use of org.hyperledger.besu.ethereum.mainnet.PoWSolver 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)

Example 5 with PoWSolver

use of org.hyperledger.besu.ethereum.mainnet.PoWSolver in project besu by hyperledger.

the class PoWBlockCreatorTest method createMainnetBlock1.

@Test
public void createMainnetBlock1() throws IOException {
    final GenesisConfigOptions genesisConfigOptions = GenesisConfigFile.DEFAULT.getConfigOptions();
    final ExecutionContextTestFixture executionContextTestFixture = ExecutionContextTestFixture.builder().protocolSchedule(new ProtocolScheduleBuilder(genesisConfigOptions, BigInteger.valueOf(42), ProtocolSpecAdapters.create(0, Function.identity()), PrivacyParameters.DEFAULT, false, genesisConfigOptions.isQuorum(), EvmConfiguration.DEFAULT).createProtocolSchedule()).build();
    final PoWSolver solver = new PoWSolver(Lists.newArrayList(BLOCK_1_NONCE), PoWHasher.ETHASH_LIGHT, false, Subscribers.none(), new EpochCalculator.DefaultEpochCalculator(), 1000, 8);
    final BaseFeePendingTransactionsSorter pendingTransactions = new BaseFeePendingTransactionsSorter(TransactionPoolConfiguration.DEFAULT_TX_RETENTION_HOURS, 1, TestClock.fixed(), metricsSystem, executionContextTestFixture.getProtocolContext().getBlockchain()::getChainHeadHeader, TransactionPoolConfiguration.DEFAULT_PRICE_BUMP);
    final PoWBlockCreator blockCreator = new PoWBlockCreator(BLOCK_1_COINBASE, () -> Optional.empty(), parent -> BLOCK_1_EXTRA_DATA, pendingTransactions, executionContextTestFixture.getProtocolContext(), executionContextTestFixture.getProtocolSchedule(), solver, Wei.ZERO, 0.8, executionContextTestFixture.getBlockchain().getChainHeadHeader());
    // A Hashrate should not exist in the block creator prior to creating a block
    assertThat(blockCreator.getHashesPerSecond().isPresent()).isFalse();
    final Block actualBlock = blockCreator.createBlock(BLOCK_1_TIMESTAMP);
    final Block expectedBlock = ValidationTestUtils.readBlock(1);
    assertThat(actualBlock).isEqualTo(expectedBlock);
    assertThat(blockCreator.getHashesPerSecond().isPresent()).isTrue();
}
Also used : EpochCalculator(org.hyperledger.besu.ethereum.mainnet.EpochCalculator) BaseFeePendingTransactionsSorter(org.hyperledger.besu.ethereum.eth.transactions.sorter.BaseFeePendingTransactionsSorter) ExecutionContextTestFixture(org.hyperledger.besu.ethereum.core.ExecutionContextTestFixture) Block(org.hyperledger.besu.ethereum.core.Block) GenesisConfigOptions(org.hyperledger.besu.config.GenesisConfigOptions) ProtocolScheduleBuilder(org.hyperledger.besu.ethereum.mainnet.ProtocolScheduleBuilder) PoWSolver(org.hyperledger.besu.ethereum.mainnet.PoWSolver) Test(org.junit.Test)

Aggregations

EpochCalculator (org.hyperledger.besu.ethereum.mainnet.EpochCalculator)6 PoWSolver (org.hyperledger.besu.ethereum.mainnet.PoWSolver)6 GenesisConfigOptions (org.hyperledger.besu.config.GenesisConfigOptions)4 ExecutionContextTestFixture (org.hyperledger.besu.ethereum.core.ExecutionContextTestFixture)4 BaseFeePendingTransactionsSorter (org.hyperledger.besu.ethereum.eth.transactions.sorter.BaseFeePendingTransactionsSorter)4 ProtocolScheduleBuilder (org.hyperledger.besu.ethereum.mainnet.ProtocolScheduleBuilder)4 Test (org.junit.Test)4 MutableWorldState (org.hyperledger.besu.ethereum.core.MutableWorldState)3 ProtocolContext (org.hyperledger.besu.ethereum.ProtocolContext)2 ProcessableBlockHeader (org.hyperledger.besu.ethereum.core.ProcessableBlockHeader)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Optional (java.util.Optional)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Function (java.util.function.Function)1 JsonGenesisConfigOptions (org.hyperledger.besu.config.JsonGenesisConfigOptions)1 MergeConfigOptions (org.hyperledger.besu.config.MergeConfigOptions)1 Address (org.hyperledger.besu.datatypes.Address)1 BlockReplay (org.hyperledger.besu.ethereum.api.jsonrpc.internal.processor.BlockReplay)1 BlockchainQueries (org.hyperledger.besu.ethereum.api.query.BlockchainQueries)1 IncrementingNonceGenerator (org.hyperledger.besu.ethereum.blockcreation.IncrementingNonceGenerator)1