Skip to content
Open
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
a7a6224
ADD: BitSequence class for locations in BinTrie
thomas-quadratic Jun 13, 2025
8311477
ADD: nodes, get-put visitors, abstracting BitSequence
thomas-quadratic Jun 20, 2025
0faf211
ADD: SimpleBinTrie implementation with visitors skeletons
thomas-quadratic Jun 24, 2025
f3814ed
TEST: add tests and fixes to get and put visitors
thomas-quadratic Jul 1, 2025
01b1112
FIX: spotless
thomas-quadratic Jul 1, 2025
f9653c4
ADD: Remove, Hash, Flatten visitors
thomas-quadratic Jul 7, 2025
f1cc704
FIX: HashVisitor digest resets properly
thomas-quadratic Jul 8, 2025
39ad201
FIX: added the remaining tests
thomas-quadratic Jul 8, 2025
09a309e
ENH: spotlessApply
thomas-quadratic Jul 8, 2025
5196362
ENH: stored nodes and trie
thomas-quadratic Jul 17, 2025
c33872c
FIX: remove StoredValues and spotlessApply
thomas-quadratic Jul 18, 2025
bc26c0e
ADD: TrieKey adapter
thomas-quadratic Jul 23, 2025
8662171
fix issues on binary trie
matkt Jul 28, 2025
6183c75
remove logs
matkt Jul 28, 2025
5c64ab6
blake3 thread safe
matkt Jul 28, 2025
2fe4726
don't store leaf in DB
matkt Jul 30, 2025
b5694b9
check is dirty on FlattenVisitor
matkt Jul 31, 2025
dd07450
update old value logic for leaf node
matkt Aug 4, 2025
f926a72
prune correctly stem
matkt Aug 5, 2025
5a1cb89
add ref tests
matkt Aug 7, 2025
bd48cf0
spotless apply
matkt Aug 14, 2025
aa6c171
add sha256
matkt Aug 27, 2025
5968408
update bin trie tests
matkt Aug 27, 2025
53e93a6
update bin trie tests
matkt Aug 27, 2025
91107b8
fix binary trie by using SHA256 everywhere
matkt Oct 21, 2025
f19f01e
spotless
matkt Feb 27, 2026
41b7b58
address review comments
matkt Feb 27, 2026
ac04caf
Merge commit 'c78f1705729a5c7ce676bf5a542727d99514a2aa' into feature/…
matkt Feb 27, 2026
9a52702
good version
matkt Feb 27, 2026
334dfe5
good version
matkt Feb 27, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ dependencies {
implementation 'org.apache.logging.log4j:log4j-core'
implementation 'org.hyperledger.besu.internal:rlp'
implementation 'org.hyperledger.besu.internal:trie'
implementation 'org.bouncycastle:bcprov-jdk18on'


testImplementation 'org.mockito:mockito-core'
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version=0.0.3-SNAPSHOT
version=0.0.3-SNAPSHOT-BIN-TRIE-TEST-4
org.gradle.welcome=never
2 changes: 2 additions & 0 deletions gradle/versions.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ dependencyManagement {

dependency 'io.github.crate-crypto:java-verkle-cryptography:0.0.3'

dependency 'org.bouncycastle:bcprov-jdk18on:1.81'

dependencySet(group: 'org.hyperledger.besu.internal', version: '24.7.0') {
entry 'rlp'
entry 'trie'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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.stateless.bintrie;

import org.hyperledger.besu.ethereum.trie.NodeUpdater;

import java.util.Optional;

import org.apache.tuweni.bytes.Bytes32;

/** Binary Trie. */
public interface BinTrie<K extends BitSequence<K>, V> {

/**
* Returns an {@code Optional} of value mapped to the hash if it exists; otherwise empty.
*
* @param key The key for the value.
* @return an {@code Optional} of value mapped to the hash if it exists; otherwise empty
*/
Optional<V> get(K key);

/**
* Updates the value mapped to the specified key, creating the mapping if one does not already
* exist.
*
* @param key The key that corresponds to the value to be updated.
* @param value The value to associate the key with.
* @return Optional previous value before replacement if it exists.
*/
Optional<V> put(K key, V value);

/**
* Deletes the value mapped to the specified key, if such a value exists (Optional operation).
*
* @param key The key of the value to be deleted.
*/
void remove(K key);

/** Restructure tree to get minimal representation. */
void flatten();

/**
* Returns the hash of the root node of the trie.
*
* @return The hash of the root node of the trie.
*/
Bytes32 getRootHash();

/**
* Commits any pending changes to the underlying storage.
*
* @param nodeUpdater used to store the node values
*/
void commit(NodeUpdater nodeUpdater);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
/*
* 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.stateless.bintrie;

/** Interface representing a sequence of bits, used as prefixes in a Binary Trie. */
public abstract class BitSequence<T extends BitSequence<T>> implements Comparable<BitSequence<T>> {
/**
* Get a BitSequence factory
*
* @return A factory for building new BitSequences.
*/
public abstract BitSequenceFactory<T> factory();

/**
* Get an identical copy.
*
* @return A new BitSequence equal to initial one.
*/
public abstract T copy();

/**
* The binary string representation of the BitSequence.
*
* @return A string representation of the node.
*/
public abstract String toBinaryString();

/**
* The Hex+ string representation of the BitSequence.
*
* @return A byte array representation of the node.
*/
public abstract String toHexString();

/**
* The binary string representation of the BitSequence.
*
* @return A byte array representation of the node.
*/
public abstract byte[] toBytes();

/**
* Convert BitSequence to an int if possible.
*
* @return the big-endian integer value.
*/
public abstract int toInt();

/**
* Get length in bits
*
* @return Sequence's length in bits.
*/
public abstract int length();

/**
* bit at a given index to a given value
*
* @param bit The boolean value to set at the end.
* @return New BitSequence with added bit at the tail.
*/
public abstract T add(boolean bit);

/**
* Add minimal BitSequence representation of suffix to the sequence.
*
* @param suffix The integer value to add at the end of the sequence.
* @return New BitSequence with added bit at the tail.
*/
public abstract T add(int suffix);

/**
* Add fixed-width BitSequence representation of suffix to the sequence.
*
* @param suffix The integer value to add at the end of the sequence.
* @param width The fixed number of bits in the sequence.
* @return New BitSequence with added bit at the tail.
*/
public abstract T add(int suffix, int width);

/**
* Get a bit at a given index.
*
* @param bitIndex The bit position to set.
* @return The boolean value at given index.
*/
public abstract boolean get(int bitIndex);

/**
* Set a bit at a given index to a given value
*
* @param bitIndex The bit position to set.
* @param value The boolean value to set.
*/
public abstract void set(int bitIndex, boolean value);

/**
* Get a slice of the BitSequence, starting at start in bits until the end.
*
* @param from The starting position.
* @return A new BitSequence from the slice.
*/
public abstract T slice(int from);

/**
* Get a slice of the BitSequence.
*
* @param from The starting position.
* @param toExclusive The ending position.
* @return A new BitSequence from the slice.
*/
public abstract T slice(int from, int toExclusive);

/**
* Lexicographical comparaison of two BitSequences.
*
* @param other another BitSequence to compare to.
* @return Comparaison value.
*/
@Override
public int compareTo(BitSequence<T> other) {
int minLength = Math.min(this.length(), other.length());
for (int i = 0; i < minLength; i++) {
boolean bitA = this.get(i);
boolean bitB = other.get(i);
if (bitA != bitB) {
return bitA ? 1 : -1;
}
}
return Integer.compare(this.length(), other.length());
}

/**
* Get the common prefix of two BitSequences.
*
* @param other The BitSequence to compare to.
* @return BitSequence of the common prefix.
*/
public T commonPrefix(BitSequence<T> other) {
int maxSize = Math.min(this.length(), other.length());
T result = factory().empty();
for (int i = 0; i < maxSize; i++) {
boolean thisBit = this.get(i);
boolean otherBit = other.get(i);
if (thisBit != otherBit) {
break;
}
result = result.add(thisBit);
}
return result;
}

/**
* Concatenate 2 BitSequences
*
* @param other BitSequence to concatenate
* @return Concatenates BitSequence
*/
public T concatenate(BitSequence<T> other) {
T result = copy();
for (int i = 0; i < other.length(); i++) {
result = result.add(other.get(i));
}
return result;
}

/**
* Concatenate an encoded BitSequence
*
* @param encodedOther BitSequence to concatenate
* @return Concatenates BitSequence
*/
public T concatenate(byte[] encodedOther) {
T other = factory().decode(encodedOther);
return concatenate(other);
}

/**
* Encode a BitSequence from the encoded form.
*
* @return Encoded byte array representation of the BitSequence.
*/
public abstract byte[] encode();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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.stateless.bintrie;

import org.apache.tuweni.bytes.Bytes;

public interface BitSequenceFactory<K extends BitSequence<K>> {
/**
* Default empty Sequence.
*
* @return Empty BitSequence.
*/
public K empty();

/**
* Get a BitSequence from a binary string representation.
*
* @param bits Binary string representation.
* @return A string representation of the node.
*/
public K fromBinaryString(String bits);

/**
* Get a BitSequence from a binary string representation.
*
* @param bits Binary string representation.
* @return A string representation of the node.
*/
public K fromHexString(String bits);

/**
* Get a BitSequence from an Integer.
*
* @param value Integer value.
* @return BitSequence representing value in big-endian format.
*/
public K fromInteger(int value);

/**
* Get a BitSequence from Bytes.
*
* @param value Integer value.
* @return BitSequence representing value in big-endian format.
*/
public K fromBytes(Bytes value);

/**
* Decode a BitSequence from the encoded form.
*
* @param encoded The encoded representation of a BytesPackedBitSequence
* @return Decoded BitSequence.
*/
public K decode(byte[] encoded);
}
Loading
Loading