Search in sources :

Example 1 with InMemoryKeyValueStorageProvider

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);
}
Also used : RollingFileReader(org.hyperledger.besu.util.io.RollingFileReader) InMemoryKeyValueStorage(org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage) InMemoryKeyValueStorageProvider(org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider) BytesValueRLPInput(org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput) IOException(java.io.IOException)

Example 2 with InMemoryKeyValueStorageProvider

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);
}
Also used : InMemoryKeyValueStorageProvider(org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider) Before(org.junit.Before)

Example 3 with InMemoryKeyValueStorageProvider

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);
}
Also used : Path(java.nio.file.Path) BesuController(org.hyperledger.besu.controller.BesuController) NoOpMetricsSystem(org.hyperledger.besu.metrics.noop.NoOpMetricsSystem) InMemoryKeyValueStorageProvider(org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider) Test(org.junit.Test)

Example 4 with InMemoryKeyValueStorageProvider

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));
}
Also used : Path(java.nio.file.Path) BesuController(org.hyperledger.besu.controller.BesuController) NoOpMetricsSystem(org.hyperledger.besu.metrics.noop.NoOpMetricsSystem) InMemoryKeyValueStorageProvider(org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider) Test(org.junit.Test)

Example 5 with InMemoryKeyValueStorageProvider

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);
}
Also used : Path(java.nio.file.Path) BesuController(org.hyperledger.besu.controller.BesuController) NoOpMetricsSystem(org.hyperledger.besu.metrics.noop.NoOpMetricsSystem) IOException(java.io.IOException) InMemoryKeyValueStorageProvider(org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider) Test(org.junit.Test)

Aggregations

InMemoryKeyValueStorageProvider (org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider)15 NoOpMetricsSystem (org.hyperledger.besu.metrics.noop.NoOpMetricsSystem)9 Path (java.nio.file.Path)7 BesuController (org.hyperledger.besu.controller.BesuController)6 Test (org.junit.Test)6 Before (org.junit.Before)4 InMemoryKeyValueStorage (org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage)3 IOException (java.io.IOException)2 Bytes (org.apache.tuweni.bytes.Bytes)2 EnclaveFactory (org.hyperledger.besu.enclave.EnclaveFactory)2 JsonRpcConfiguration (org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcConfiguration)2 WebSocketConfiguration (org.hyperledger.besu.ethereum.api.jsonrpc.websocket.WebSocketConfiguration)2 BonsaiWorldStateKeyValueStorage (org.hyperledger.besu.ethereum.bonsai.BonsaiWorldStateKeyValueStorage)2 MainnetBlockHeaderFunctions (org.hyperledger.besu.ethereum.mainnet.MainnetBlockHeaderFunctions)2 StorageProvider (org.hyperledger.besu.ethereum.storage.StorageProvider)2 WorldStateKeyValueStorage (org.hyperledger.besu.ethereum.storage.keyvalue.WorldStateKeyValueStorage)2 ObservableMetricsSystem (org.hyperledger.besu.metrics.ObservableMetricsSystem)2 MetricsConfiguration (org.hyperledger.besu.metrics.prometheus.MetricsConfiguration)2 RpcEndpointServiceImpl (org.hyperledger.besu.services.RpcEndpointServiceImpl)2 MockitoJUnitRunner (org.mockito.junit.MockitoJUnitRunner)2