Skip to content

Commit

Permalink
Optimize Trie Node Processing with BatchProcessor Enhancements (hyper…
Browse files Browse the repository at this point in the history
…ledger#54)

Signed-off-by: Karim Taam <[email protected]>
  • Loading branch information
matkt authored Apr 16, 2024
1 parent e14f047 commit 86b3574
Show file tree
Hide file tree
Showing 19 changed files with 1,226 additions and 77 deletions.
2 changes: 1 addition & 1 deletion gradle/versions.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ dependencyManagement {

dependency 'org.assertj:assertj-core:3.24.2'

dependencySet(group: 'org.apache.logging.log4j', version: '2.20.0') {
dependencySet(group: 'org.apache.logging.log4j', version: '2.22.1') {
entry 'log4j-api'
entry 'log4j-core'
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright Hyperledger Besu Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*
*/
package org.hyperledger.besu.ethereum.trie.verkle;

import static com.google.common.base.Preconditions.checkNotNull;

import org.hyperledger.besu.ethereum.trie.NodeUpdater;
import org.hyperledger.besu.ethereum.trie.verkle.node.Node;
import org.hyperledger.besu.ethereum.trie.verkle.visitor.CommitVisitor;
import org.hyperledger.besu.ethereum.trie.verkle.visitor.PutVisitor;
import org.hyperledger.besu.ethereum.trie.verkle.visitor.RemoveVisitor;

import java.util.Optional;

import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;

/**
* A simple implementation of a Verkle Trie.
*
* @param <K> The type of keys in the Verkle Trie.
* @param <V> The type of values in the Verkle Trie.
*/
public class SimpleBatchedVerkleTrie<K extends Bytes, V extends Bytes>
extends SimpleVerkleTrie<K, V> implements VerkleTrie<K, V> {

private final VerkleTreeBatchHasher batchProcessor;

public SimpleBatchedVerkleTrie(final VerkleTreeBatchHasher batchProcessor) {
super();
this.batchProcessor = batchProcessor;
this.batchProcessor.addNodeToBatch(Optional.of(Bytes.EMPTY), this.root);
}

public SimpleBatchedVerkleTrie(
final Node<V> providedRoot, final VerkleTreeBatchHasher batchProcessor) {
super(providedRoot);
this.batchProcessor = batchProcessor;
this.batchProcessor.addNodeToBatch(root.getLocation(), root);
}

public SimpleBatchedVerkleTrie(
final Optional<Node<V>> maybeRoot, final VerkleTreeBatchHasher batchProcessor) {
super(maybeRoot);
this.batchProcessor = batchProcessor;
this.batchProcessor.addNodeToBatch(root.getLocation(), root);
}

@Override
public Optional<V> put(final K key, final V value) {
checkNotNull(key);
checkNotNull(value);
PutVisitor<V> visitor = new PutVisitor<>(value, Optional.of(batchProcessor));
this.root = root.accept(visitor, key);
return visitor.getOldValue();
}

@Override
public void remove(final K key) {
checkNotNull(key);
this.root = root.accept(new RemoveVisitor<V>(Optional.of(batchProcessor)), key);
}

@Override
public void commit(final NodeUpdater nodeUpdater) {
batchProcessor.calculateStateRoot();
root = root.accept(new CommitVisitor<V>(nodeUpdater), Bytes.EMPTY);
}

@Override
public Bytes32 getRootHash() {
batchProcessor.calculateStateRoot();
return root.getHash().get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* @param <V> The type of values in the Verkle Trie.
*/
public class SimpleVerkleTrie<K extends Bytes, V extends Bytes> implements VerkleTrie<K, V> {
private Node<V> root;
protected Node<V> root;

/** Creates a new Verkle Trie with a null node as the root. */
public SimpleVerkleTrie() {
Expand Down Expand Up @@ -96,7 +96,7 @@ public Optional<V> get(final K key) {
public Optional<V> put(final K key, final V value) {
checkNotNull(key);
checkNotNull(value);
PutVisitor<V> visitor = new PutVisitor<V>(value);
final PutVisitor<V> visitor = new PutVisitor<V>(value, Optional.empty());
this.root = root.accept(visitor, key);
return visitor.getOldValue();
}
Expand All @@ -109,7 +109,7 @@ public Optional<V> put(final K key, final V value) {
@Override
public void remove(final K key) {
checkNotNull(key);
this.root = root.accept(new RemoveVisitor<V>(), key);
this.root = root.accept(new RemoveVisitor<V>(Optional.empty()), key);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright Hyperledger Besu Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*
*/
package org.hyperledger.besu.ethereum.trie.verkle;

import org.hyperledger.besu.ethereum.trie.verkle.factory.NodeFactory;

import org.apache.tuweni.bytes.Bytes;

/**
* Implements a Verkle Trie that batches node hashing by level.
*
* @param <K> The type of keys in the Verkle Trie.
* @param <V> The type of values in the Verkle Trie.
*/
public class StoredBatchedVerkleTrie<K extends Bytes, V extends Bytes>
extends SimpleBatchedVerkleTrie<K, V> {
/** NodeFactory that load nodes from storage */
protected final NodeFactory<V> nodeFactory;

/**
* Constructs a new trie with a node factory and batch processor.
*
* @param batchProcessor The processor for batching node hashing.
* @param nodeFactory The {@link NodeFactory} to retrieve node.
*/
public StoredBatchedVerkleTrie(
final VerkleTreeBatchHasher batchProcessor, final NodeFactory<V> nodeFactory) {
super(nodeFactory.retrieve(Bytes.EMPTY, null), batchProcessor);
this.nodeFactory = nodeFactory;
}
}
Loading

0 comments on commit 86b3574

Please sign in to comment.