forked from hyperledger/besu-verkle-trie
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize Trie Node Processing with BatchProcessor Enhancements (hyper…
…ledger#54) Signed-off-by: Karim Taam <[email protected]>
- Loading branch information
Showing
19 changed files
with
1,226 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/main/java/org/hyperledger/besu/ethereum/trie/verkle/SimpleBatchedVerkleTrie.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/main/java/org/hyperledger/besu/ethereum/trie/verkle/StoredBatchedVerkleTrie.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.