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);
}
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);
}
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();
}
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();
}
}
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;
}
Aggregations