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