use of org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider in project besu by hyperledger.
the class RollingImport method main.
public static void main(final String[] arg) throws IOException {
checkArgument(arg.length == 1, "Single argument is file prefix, like `./layer/besu-layer`");
final RollingFileReader reader = new RollingFileReader((i, c) -> Path.of(String.format(arg[0] + "-%04d.rdat", i)), false);
final InMemoryKeyValueStorageProvider provider = new InMemoryKeyValueStorageProvider();
final BonsaiWorldStateArchive archive = new BonsaiWorldStateArchive(provider, null);
final InMemoryKeyValueStorage accountStorage = (InMemoryKeyValueStorage) provider.getStorageBySegmentIdentifier(KeyValueSegmentIdentifier.ACCOUNT_INFO_STATE);
final InMemoryKeyValueStorage codeStorage = (InMemoryKeyValueStorage) provider.getStorageBySegmentIdentifier(KeyValueSegmentIdentifier.CODE_STORAGE);
final InMemoryKeyValueStorage storageStorage = (InMemoryKeyValueStorage) provider.getStorageBySegmentIdentifier(KeyValueSegmentIdentifier.ACCOUNT_STORAGE_STORAGE);
final InMemoryKeyValueStorage trieBranchStorage = (InMemoryKeyValueStorage) provider.getStorageBySegmentIdentifier(KeyValueSegmentIdentifier.TRIE_BRANCH_STORAGE);
final InMemoryKeyValueStorage trieLogStorage = (InMemoryKeyValueStorage) provider.getStorageBySegmentIdentifier(KeyValueSegmentIdentifier.TRIE_LOG_STORAGE);
final BonsaiPersistedWorldState bonsaiState = new BonsaiPersistedWorldState(archive, new BonsaiWorldStateKeyValueStorage(accountStorage, codeStorage, storageStorage, trieBranchStorage, trieLogStorage));
int count = 0;
while (!reader.isDone()) {
try {
final byte[] bytes = reader.readBytes();
if (bytes.length < 1) {
continue;
}
final TrieLogLayer layer = TrieLogLayer.readFrom(new BytesValueRLPInput(Bytes.wrap(bytes), false));
final BonsaiWorldStateUpdater updater = (BonsaiWorldStateUpdater) bonsaiState.updater();
updater.rollForward(layer);
updater.commit();
bonsaiState.persist(null);
if (count % 10000 == 0) {
System.out.println(". - " + count);
} else if (count % 100 == 0) {
System.out.print(".");
System.out.flush();
}
} catch (final Exception e) {
// e.printStackTrace(System.out);
System.out.println(count);
throw e;
}
count++;
}
System.out.printf("%nCount %d - now going backwards!%n", count);
while (count > 0) {
try {
count--;
reader.seek(count);
final byte[] bytes = reader.readBytes();
final TrieLogLayer layer = TrieLogLayer.readFrom(new BytesValueRLPInput(Bytes.wrap(bytes), false));
final BonsaiWorldStateUpdater updater = (BonsaiWorldStateUpdater) bonsaiState.updater();
updater.rollBack(layer);
updater.commit();
bonsaiState.persist(null);
if (count % 10000 == 0) {
System.out.println(". - " + count);
} else if (count % 100 == 0) {
System.out.print(".");
System.out.flush();
}
} catch (final Exception e) {
System.out.println(count);
throw e;
}
}
System.out.printf("Back to zero!%n");
accountStorage.dump(System.out);
codeStorage.dump(System.out);
storageStorage.dump(System.out);
trieBranchStorage.dump(System.out);
trieLogStorage.dump(System.out);
}
use of org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider in project besu by hyperledger.
the class LogRollingTests method createStorage.
@Before
public void createStorage() {
final InMemoryKeyValueStorageProvider provider = new InMemoryKeyValueStorageProvider();
archive = new BonsaiWorldStateArchive(provider, blockchain);
accountStorage = (InMemoryKeyValueStorage) provider.getStorageBySegmentIdentifier(KeyValueSegmentIdentifier.ACCOUNT_INFO_STATE);
codeStorage = (InMemoryKeyValueStorage) provider.getStorageBySegmentIdentifier(KeyValueSegmentIdentifier.CODE_STORAGE);
storageStorage = (InMemoryKeyValueStorage) provider.getStorageBySegmentIdentifier(KeyValueSegmentIdentifier.ACCOUNT_STORAGE_STORAGE);
trieBranchStorage = (InMemoryKeyValueStorage) provider.getStorageBySegmentIdentifier(KeyValueSegmentIdentifier.TRIE_BRANCH_STORAGE);
trieLogStorage = (InMemoryKeyValueStorage) provider.getStorageBySegmentIdentifier(KeyValueSegmentIdentifier.TRIE_LOG_STORAGE);
final InMemoryKeyValueStorageProvider secondProvider = new InMemoryKeyValueStorageProvider();
secondArchive = new BonsaiWorldStateArchive(secondProvider, blockchain);
secondAccountStorage = (InMemoryKeyValueStorage) secondProvider.getStorageBySegmentIdentifier(KeyValueSegmentIdentifier.ACCOUNT_INFO_STATE);
secondCodeStorage = (InMemoryKeyValueStorage) secondProvider.getStorageBySegmentIdentifier(KeyValueSegmentIdentifier.CODE_STORAGE);
secondStorageStorage = (InMemoryKeyValueStorage) secondProvider.getStorageBySegmentIdentifier(KeyValueSegmentIdentifier.ACCOUNT_STORAGE_STORAGE);
secondTrieBranchStorage = (InMemoryKeyValueStorage) secondProvider.getStorageBySegmentIdentifier(KeyValueSegmentIdentifier.TRIE_BRANCH_STORAGE);
secondTrieLogStorage = (InMemoryKeyValueStorage) secondProvider.getStorageBySegmentIdentifier(KeyValueSegmentIdentifier.TRIE_LOG_STORAGE);
}
use of org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider in project besu by hyperledger.
the class RlpBlockImporterTest method blockImportRejectsBadPow.
@Test
public void blockImportRejectsBadPow() throws IOException {
final Path dataDir = folder.newFolder().toPath();
final Path source = dataDir.resolve("badpow.blocks");
BlockTestUtil.writeBadPowBlocks(source);
final BesuController targetController = new BesuController.Builder().fromGenesisConfig(GenesisConfigFile.mainnet()).synchronizerConfiguration(SynchronizerConfiguration.builder().build()).ethProtocolConfiguration(EthProtocolConfiguration.defaultConfig()).storageProvider(new InMemoryKeyValueStorageProvider()).networkId(BigInteger.ONE).miningParameters(new MiningParameters.Builder().miningEnabled(false).build()).nodeKey(NodeKeyUtils.generate()).metricsSystem(new NoOpMetricsSystem()).privacyParameters(PrivacyParameters.DEFAULT).dataDirectory(dataDir).clock(TestClock.fixed()).transactionPoolConfiguration(TransactionPoolConfiguration.DEFAULT).gasLimitCalculator(GasLimitCalculator.constant()).evmConfiguration(EvmConfiguration.DEFAULT).build();
assertThatThrownBy(() -> rlpBlockImporter.importBlockchain(source, targetController, false), "Invalid header at block number 2.", CompletionException.class);
}
use of org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider in project besu by hyperledger.
the class RlpBlockImporterTest method blockImportCanSkipPow.
@Test
public void blockImportCanSkipPow() throws IOException {
final Path dataDir = folder.newFolder().toPath();
final Path source = dataDir.resolve("badpow.blocks");
BlockTestUtil.writeBadPowBlocks(source);
final BesuController targetController = new BesuController.Builder().fromGenesisConfig(GenesisConfigFile.mainnet()).synchronizerConfiguration(SynchronizerConfiguration.builder().build()).ethProtocolConfiguration(EthProtocolConfiguration.defaultConfig()).storageProvider(new InMemoryKeyValueStorageProvider()).networkId(BigInteger.ONE).miningParameters(new MiningParameters.Builder().miningEnabled(false).build()).nodeKey(NodeKeyUtils.generate()).metricsSystem(new NoOpMetricsSystem()).privacyParameters(PrivacyParameters.DEFAULT).dataDirectory(dataDir).clock(TestClock.fixed()).transactionPoolConfiguration(TransactionPoolConfiguration.DEFAULT).gasLimitCalculator(GasLimitCalculator.constant()).evmConfiguration(EvmConfiguration.DEFAULT).build();
final RlpBlockImporter.ImportResult result = rlpBlockImporter.importBlockchain(source, targetController, true);
assertThat(result.count).isEqualTo(1);
assertThat(result.td).isEqualTo(UInt256.valueOf(34351349760L));
}
use of org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider in project besu by hyperledger.
the class RlpBlockImporterTest method ibftImport.
@Test
public void ibftImport() throws IOException {
final Path dataDir = folder.newFolder().toPath();
final Path source = dataDir.resolve("ibft.blocks");
final String config = Resources.toString(this.getClass().getResource("/ibftlegacy_genesis.json"), UTF_8);
try {
Files.write(source, Resources.toByteArray(this.getClass().getResource("/ibft.blocks")), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
} catch (final IOException ex) {
throw new IllegalStateException(ex);
}
final BesuController controller = new BesuController.Builder().fromGenesisConfig(GenesisConfigFile.fromConfig(config)).synchronizerConfiguration(SynchronizerConfiguration.builder().build()).ethProtocolConfiguration(EthProtocolConfiguration.defaultConfig()).storageProvider(new InMemoryKeyValueStorageProvider()).networkId(BigInteger.valueOf(10)).miningParameters(new MiningParameters.Builder().miningEnabled(false).build()).nodeKey(NodeKeyUtils.generate()).metricsSystem(new NoOpMetricsSystem()).privacyParameters(PrivacyParameters.DEFAULT).dataDirectory(dataDir).clock(TestClock.fixed()).transactionPoolConfiguration(TransactionPoolConfiguration.DEFAULT).gasLimitCalculator(GasLimitCalculator.constant()).evmConfiguration(EvmConfiguration.DEFAULT).build();
final RlpBlockImporter.ImportResult result = rlpBlockImporter.importBlockchain(source, controller, false);
// Don't count the Genesis block
assertThat(result.count).isEqualTo(958);
}
Aggregations