Search in sources :

Example 1 with KeyValueStorageTransaction

use of org.hyperledger.besu.plugin.services.storage.KeyValueStorageTransaction in project besu by hyperledger.

the class LogRollingTests method rollBackOnce.

@Test
public void rollBackOnce() {
    final BonsaiPersistedWorldState worldState = new BonsaiPersistedWorldState(archive, new BonsaiWorldStateKeyValueStorage(accountStorage, codeStorage, storageStorage, trieBranchStorage, trieLogStorage));
    final WorldUpdater updater = worldState.updater();
    final MutableAccount mutableAccount = updater.createAccount(addressOne, 1, Wei.of(1L)).getMutable();
    mutableAccount.setCode(Bytes.of(0, 1, 2));
    mutableAccount.setStorageValue(UInt256.ONE, UInt256.ONE);
    updater.commit();
    worldState.persist(headerOne);
    final WorldUpdater updater2 = worldState.updater();
    final MutableAccount mutableAccount2 = updater2.getAccount(addressOne).getMutable();
    mutableAccount2.setStorageValue(UInt256.ONE, UInt256.valueOf(2));
    updater2.commit();
    worldState.persist(headerTwo);
    final BonsaiWorldStateUpdater firstRollbackUpdater = (BonsaiWorldStateUpdater) worldState.updater();
    final TrieLogLayer layerTwo = getTrieLogLayer(trieLogStorage, headerTwo.getHash());
    firstRollbackUpdater.rollBack(layerTwo);
    worldState.persist(headerOne);
    final BonsaiPersistedWorldState secondWorldState = new BonsaiPersistedWorldState(secondArchive, new BonsaiWorldStateKeyValueStorage(secondAccountStorage, secondCodeStorage, secondStorageStorage, secondTrieBranchStorage, secondTrieLogStorage));
    final WorldUpdater secondUpdater = secondWorldState.updater();
    final MutableAccount secondMutableAccount = secondUpdater.createAccount(addressOne, 1, Wei.of(1L)).getMutable();
    secondMutableAccount.setCode(Bytes.of(0, 1, 2));
    secondMutableAccount.setStorageValue(UInt256.ONE, UInt256.ONE);
    secondUpdater.commit();
    secondWorldState.persist(null);
    assertKeyValueStorageEqual(accountStorage, secondAccountStorage);
    assertKeyValueStorageEqual(codeStorage, secondCodeStorage);
    assertKeyValueStorageEqual(storageStorage, secondStorageStorage);
    final KeyValueStorageTransaction tx = trieBranchStorage.startTransaction();
    tx.remove(BonsaiWorldStateKeyValueStorage.WORLD_BLOCK_HASH_KEY);
    tx.commit();
    assertKeyValueStorageEqual(trieBranchStorage, secondTrieBranchStorage);
    // trie logs won't be the same, we don't delete the roll back log
    assertKeyValueSubset(trieLogStorage, secondTrieLogStorage);
    assertThat(secondWorldState.rootHash()).isEqualByComparingTo(worldState.rootHash());
}
Also used : WorldUpdater(org.hyperledger.besu.evm.worldstate.WorldUpdater) KeyValueStorageTransaction(org.hyperledger.besu.plugin.services.storage.KeyValueStorageTransaction) MutableAccount(org.hyperledger.besu.evm.account.MutableAccount) Test(org.junit.Test)

Example 2 with KeyValueStorageTransaction

use of org.hyperledger.besu.plugin.services.storage.KeyValueStorageTransaction in project besu by hyperledger.

the class GenericKeyValueStorageFacade method put.

public void put(final byte[] key, final V value) {
    final KeyValueStorageTransaction keyValueStorageTransaction = storage.startTransaction();
    keyValueStorageTransaction.put(key, valueConvertor.toBytes(value));
    keyValueStorageTransaction.commit();
}
Also used : KeyValueStorageTransaction(org.hyperledger.besu.plugin.services.storage.KeyValueStorageTransaction)

Example 3 with KeyValueStorageTransaction

use of org.hyperledger.besu.plugin.services.storage.KeyValueStorageTransaction in project besu by hyperledger.

the class MarkSweepPruner method flushPendingMarks.

private void flushPendingMarks() {
    final KeyValueStorageTransaction transaction = markStorage.startTransaction();
    pendingMarks.forEach(node -> transaction.put(node.toArrayUnsafe(), IN_USE));
    transaction.commit();
    pendingMarks.clear();
}
Also used : KeyValueStorageTransaction(org.hyperledger.besu.plugin.services.storage.KeyValueStorageTransaction)

Example 4 with KeyValueStorageTransaction

use of org.hyperledger.besu.plugin.services.storage.KeyValueStorageTransaction in project besu by hyperledger.

the class PeerDiscoveryAgent method updateNodeRecord.

public void updateNodeRecord() {
    if (!config.isActive()) {
        return;
    }
    final KeyValueStorage keyValueStorage = storageProvider.getStorageBySegmentIdentifier(KeyValueSegmentIdentifier.BLOCKCHAIN);
    final NodeRecordFactory nodeRecordFactory = NodeRecordFactory.DEFAULT;
    final Optional<NodeRecord> existingNodeRecord = keyValueStorage.get(Bytes.of(SEQ_NO_STORE_KEY.getBytes(UTF_8)).toArray()).map(Bytes::of).map(nodeRecordFactory::fromBytes);
    final Bytes addressBytes = Bytes.of(InetAddresses.forString(advertisedAddress).getAddress());
    final Optional<EnodeURL> maybeEnodeURL = localNode.map(DiscoveryPeer::getEnodeURL);
    final Integer discoveryPort = maybeEnodeURL.flatMap(EnodeURL::getDiscoveryPort).orElse(0);
    final Integer listeningPort = maybeEnodeURL.flatMap(EnodeURL::getListeningPort).orElse(0);
    final String forkIdEnrField = "eth";
    final NodeRecord newNodeRecord = existingNodeRecord.filter(nodeRecord -> id.equals(nodeRecord.get(EnrField.PKEY_SECP256K1)) && addressBytes.equals(nodeRecord.get(EnrField.IP_V4)) && discoveryPort.equals(nodeRecord.get(EnrField.UDP)) && listeningPort.equals(nodeRecord.get(EnrField.TCP)) && forkIdSupplier.get().equals(nodeRecord.get(forkIdEnrField))).orElseGet(() -> {
        final UInt64 sequenceNumber = existingNodeRecord.map(NodeRecord::getSeq).orElse(UInt64.ZERO).add(1);
        final NodeRecord nodeRecord = nodeRecordFactory.createFromValues(sequenceNumber, new EnrField(EnrField.ID, IdentitySchema.V4), new EnrField(SIGNATURE_ALGORITHM.get().getCurveName(), SIGNATURE_ALGORITHM.get().compressPublicKey(SIGNATURE_ALGORITHM.get().createPublicKey(id))), new EnrField(EnrField.IP_V4, addressBytes), new EnrField(EnrField.TCP, listeningPort), new EnrField(EnrField.UDP, discoveryPort), new EnrField(forkIdEnrField, Collections.singletonList(forkIdSupplier.get())));
        nodeRecord.setSignature(nodeKey.sign(Hash.keccak256(nodeRecord.serializeNoSignature())).encodedBytes().slice(0, 64));
        LOG.info("Writing node record to disk. {}", nodeRecord);
        final KeyValueStorageTransaction keyValueStorageTransaction = keyValueStorage.startTransaction();
        keyValueStorageTransaction.put(Bytes.wrap(SEQ_NO_STORE_KEY.getBytes(UTF_8)).toArray(), nodeRecord.serialize().toArray());
        keyValueStorageTransaction.commit();
        return nodeRecord;
    });
    localNode.orElseThrow(() -> new IllegalStateException("Local node should be set here")).setNodeRecord(newNodeRecord);
}
Also used : EnodeURLImpl(org.hyperledger.besu.ethereum.p2p.peers.EnodeURLImpl) LoggerFactory(org.slf4j.LoggerFactory) Peer(org.hyperledger.besu.ethereum.p2p.peers.Peer) CompletableFuture(java.util.concurrent.CompletableFuture) Bytes(org.apache.tuweni.bytes.Bytes) Subscribers(org.hyperledger.besu.util.Subscribers) Supplier(java.util.function.Supplier) Hash(org.hyperledger.besu.crypto.Hash) UInt64(org.apache.tuweni.units.bigints.UInt64) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) StorageProvider(org.hyperledger.besu.ethereum.storage.StorageProvider) NodeRecordFactory(org.ethereum.beacon.discovery.schema.NodeRecordFactory) IdentitySchema(org.ethereum.beacon.discovery.schema.IdentitySchema) DiscoveryConfiguration(org.hyperledger.besu.ethereum.p2p.config.DiscoveryConfiguration) Suppliers(com.google.common.base.Suppliers) PingPacketData(org.hyperledger.besu.ethereum.p2p.discovery.internal.PingPacketData) NetworkUtility(org.hyperledger.besu.util.NetworkUtility) EnrField(org.ethereum.beacon.discovery.schema.EnrField) Logger(org.slf4j.Logger) AsyncExecutor(org.hyperledger.besu.ethereum.p2p.discovery.internal.PeerDiscoveryController.AsyncExecutor) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Packet(org.hyperledger.besu.ethereum.p2p.discovery.internal.Packet) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) KeyValueSegmentIdentifier(org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueSegmentIdentifier) SignatureAlgorithmFactory(org.hyperledger.besu.crypto.SignatureAlgorithmFactory) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) EnodeURL(org.hyperledger.besu.plugin.data.EnodeURL) List(java.util.List) Stream(java.util.stream.Stream) SignatureAlgorithm(org.hyperledger.besu.crypto.SignatureAlgorithm) KeyValueStorage(org.hyperledger.besu.plugin.services.storage.KeyValueStorage) NodeRecord(org.ethereum.beacon.discovery.schema.NodeRecord) PeerId(org.hyperledger.besu.ethereum.p2p.peers.PeerId) KeyValueStorageTransaction(org.hyperledger.besu.plugin.services.storage.KeyValueStorageTransaction) Optional(java.util.Optional) TimerUtil(org.hyperledger.besu.ethereum.p2p.discovery.internal.TimerUtil) PeerPermissions(org.hyperledger.besu.ethereum.p2p.permissions.PeerPermissions) MetricsSystem(org.hyperledger.besu.plugin.services.MetricsSystem) VisibleForTesting(com.google.common.annotations.VisibleForTesting) InetAddresses(com.google.common.net.InetAddresses) PeerRequirement(org.hyperledger.besu.ethereum.p2p.discovery.internal.PeerRequirement) Collections(java.util.Collections) NatService(org.hyperledger.besu.nat.NatService) NodeKey(org.hyperledger.besu.crypto.NodeKey) PeerDiscoveryController(org.hyperledger.besu.ethereum.p2p.discovery.internal.PeerDiscoveryController) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) EnrField(org.ethereum.beacon.discovery.schema.EnrField) KeyValueStorage(org.hyperledger.besu.plugin.services.storage.KeyValueStorage) KeyValueStorageTransaction(org.hyperledger.besu.plugin.services.storage.KeyValueStorageTransaction) EnodeURL(org.hyperledger.besu.plugin.data.EnodeURL) NodeRecord(org.ethereum.beacon.discovery.schema.NodeRecord) Bytes(org.apache.tuweni.bytes.Bytes) NodeRecordFactory(org.ethereum.beacon.discovery.schema.NodeRecordFactory) UInt64(org.apache.tuweni.units.bigints.UInt64)

Example 5 with KeyValueStorageTransaction

use of org.hyperledger.besu.plugin.services.storage.KeyValueStorageTransaction in project besu by hyperledger.

the class TrieNodeDecoderTest method breadthFirstDecode_singleNodeTrie.

@Test
public void breadthFirstDecode_singleNodeTrie() {
    final InMemoryKeyValueStorage storage = new InMemoryKeyValueStorage();
    final MerklePatriciaTrie<Bytes, Bytes> trie = new StoredMerklePatriciaTrie<>(new BytesToByteNodeLoader(storage), Function.identity(), Function.identity());
    trie.put(Bytes.fromHexString("0x100000"), Bytes.of(1));
    // Save nodes to storage
    final KeyValueStorageTransaction tx = storage.startTransaction();
    trie.commit((location, key, value) -> tx.put(key.toArrayUnsafe(), value.toArrayUnsafe()));
    tx.commit();
    final List<Node<Bytes>> result = TrieNodeDecoder.breadthFirstDecoder(new BytesToByteNodeLoader(storage), trie.getRootHash()).collect(Collectors.toList());
    assertThat(result.size()).isEqualTo(1);
    assertThat(result.get(0).getValue()).contains(Bytes.of(1));
    final Bytes actualPath = CompactEncoding.pathToBytes(result.get(0).getPath());
    assertThat(actualPath).isEqualTo(Bytes.fromHexString("0x100000"));
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) InMemoryKeyValueStorage(org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage) KeyValueStorageTransaction(org.hyperledger.besu.plugin.services.storage.KeyValueStorageTransaction) Test(org.junit.Test)

Aggregations

KeyValueStorageTransaction (org.hyperledger.besu.plugin.services.storage.KeyValueStorageTransaction)37 Test (org.junit.Test)31 KeyValueStorage (org.hyperledger.besu.plugin.services.storage.KeyValueStorage)24 Bytes (org.apache.tuweni.bytes.Bytes)8 Optional (java.util.Optional)5 List (java.util.List)3 Stream (java.util.stream.Stream)3 InMemoryKeyValueStorage (org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage)3 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Map (java.util.Map)2 Set (java.util.Set)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Function (java.util.function.Function)2 Collectors.toUnmodifiableList (java.util.stream.Collectors.toUnmodifiableList)2 Collectors.toUnmodifiableSet (java.util.stream.Collectors.toUnmodifiableSet)2 Bytes32 (org.apache.tuweni.bytes.Bytes32)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 MutableAccount (org.hyperledger.besu.evm.account.MutableAccount)2 WorldUpdater (org.hyperledger.besu.evm.worldstate.WorldUpdater)2