use of org.hyperledger.besu.ethereum.storage.keyvalue.WorldStatePreimageKeyValueStorage 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;
}
use of org.hyperledger.besu.ethereum.storage.keyvalue.WorldStatePreimageKeyValueStorage in project besu by hyperledger.
the class DefaultMutableWorldStateTest method commitAndPersist.
@Test
public void commitAndPersist() {
final KeyValueStorage storage = new InMemoryKeyValueStorage();
final WorldStateKeyValueStorage kvWorldStateStorage = new WorldStateKeyValueStorage(storage);
final MutableWorldState worldState = createEmpty(kvWorldStateStorage);
final WorldUpdater updater = worldState.updater();
final Wei newBalance = Wei.of(100000);
final Hash expectedRootHash = Hash.fromHexString("0xa3e1c133a5a51b03399ed9ad0380f3182e9e18322f232b816dd4b9094f871e1b");
// Update account and assert we get the expected response from updater
updater.createAccount(ADDRESS).getMutable().setBalance(newBalance);
assertThat(updater.get(ADDRESS)).isNotNull();
assertThat(updater.get(ADDRESS).getBalance()).isEqualTo(newBalance);
// Commit and check assertions
updater.commit();
assertThat(worldState.rootHash()).isEqualTo(expectedRootHash);
assertThat(worldState.get(ADDRESS)).isNotNull();
assertThat(worldState.get(ADDRESS).getBalance()).isEqualTo(newBalance);
// Check that storage is empty before persisting
assertThat(kvWorldStateStorage.isWorldStateAvailable(worldState.rootHash(), null)).isFalse();
// Persist and re-run assertions
worldState.persist(null);
assertThat(kvWorldStateStorage.isWorldStateAvailable(worldState.rootHash(), null)).isTrue();
assertThat(worldState.rootHash()).isEqualTo(expectedRootHash);
assertThat(worldState.get(ADDRESS)).isNotNull();
assertThat(worldState.get(ADDRESS).getBalance()).isEqualTo(newBalance);
// Create new world state and check that it can access modified address
final MutableWorldState newWorldState = new DefaultMutableWorldState(expectedRootHash, new WorldStateKeyValueStorage(storage), new WorldStatePreimageKeyValueStorage(new InMemoryKeyValueStorage()));
assertThat(newWorldState.rootHash()).isEqualTo(expectedRootHash);
assertThat(newWorldState.get(ADDRESS)).isNotNull();
assertThat(newWorldState.get(ADDRESS).getBalance()).isEqualTo(newBalance);
}
use of org.hyperledger.besu.ethereum.storage.keyvalue.WorldStatePreimageKeyValueStorage in project besu by hyperledger.
the class GenesisState method calculateGenesisStateHash.
private static Hash calculateGenesisStateHash(final List<GenesisAccount> genesisAccounts) {
final WorldStateKeyValueStorage stateStorage = new WorldStateKeyValueStorage(new InMemoryKeyValueStorage());
final WorldStatePreimageKeyValueStorage preimageStorage = new WorldStatePreimageKeyValueStorage(new InMemoryKeyValueStorage());
final MutableWorldState worldState = new DefaultMutableWorldState(stateStorage, preimageStorage);
writeAccountsTo(worldState, genesisAccounts, null);
return worldState.rootHash();
}
Aggregations