Skip to content

Commit

Permalink
fix batcher issue
Browse files Browse the repository at this point in the history
Signed-off-by: Karim Taam <[email protected]>
  • Loading branch information
matkt committed Jun 11, 2024
1 parent 0de0eac commit 1f13566
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,19 @@ public class VerkleTrieBatchHasher {
* Adds a node for future batching. If the node is a NullNode or NullLeafNode and the location is
* not empty, it removes the node from the batch.
*
* @param location The location of the node.
* @param maybeLocation The location of the node.
* @param node The node to add.
*/
public void addNodeToBatch(final Optional<Bytes> location, final Node<?> node) {
if ((node instanceof NullNode<?> || node instanceof NullLeafNode<?>)
&& !location.orElseThrow().isEmpty()) {
updatedNodes.remove(location.orElseThrow());
} else {
updatedNodes.put(location.orElseThrow(), node);
}
public void addNodeToBatch(final Optional<Bytes> maybeLocation, final Node<?> node) {
maybeLocation.ifPresent(
location -> {
if ((node instanceof NullNode<?> || node instanceof NullLeafNode<?>)
&& !location.isEmpty()) {
updatedNodes.remove(location);
} else {
updatedNodes.put(location, node);
}
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ public void testOneValue() {
assertThat(trie.getRootHash()).as("Retrieve root hash").isEqualByComparingTo(expectedRootHash);
}

@Test
public void testDeleteAlreadyDeletedValue() {
final VerkleTrieBatchHasher batchProcessor = new VerkleTrieBatchHasher();
SimpleBatchedVerkleTrie<Bytes32, Bytes32> trie =
new SimpleBatchedVerkleTrie<Bytes32, Bytes32>(batchProcessor);
Bytes32 key =
Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff");
Bytes32 value =
Bytes32.fromHexString("0x1000000000000000000000000000000000000000000000000000000000000000");
trie.put(key, value);
trie.remove(key);
trie.remove(key);
assertThat(trie.getRootHash()).isEqualTo(Bytes32.ZERO);
}

@Test
public void testTwoValuesAtSameStem() throws Exception {
final VerkleTrieBatchHasher batchProcessor = new VerkleTrieBatchHasher();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ public void testOneValue() {
assertThat(trie.getRootHash()).as("Retrieve root hash").isEqualByComparingTo(expectedRootHash);
}

@Test
public void testDeleteAlreadyDeletedValue() {
SimpleVerkleTrie<Bytes32, Bytes32> trie = new SimpleVerkleTrie<Bytes32, Bytes32>();
Bytes32 key =
Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff");
Bytes32 value =
Bytes32.fromHexString("0x1000000000000000000000000000000000000000000000000000000000000000");
trie.put(key, value);
trie.remove(key);
trie.remove(key);
assertThat(trie.getRootHash()).isEqualTo(Bytes32.ZERO);
}

@Test
public void testTwoValuesAtSameStem() throws Exception {
SimpleVerkleTrie<Bytes32, Bytes32> trie = new SimpleVerkleTrie<Bytes32, Bytes32>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,27 @@ public void testOneValue() {
assertThat(storedTrie.get(key).orElse(null)).as("Retrieved value").isEqualTo(value);
}

@Test
public void testDeleteAlreadyDeletedValue() {
NodeUpdaterMock nodeUpdater = new NodeUpdaterMock();
NodeLoaderMock nodeLoader = new NodeLoaderMock(nodeUpdater.storage);
VerkleTrieBatchHasher batchProcessor = new VerkleTrieBatchHasher();
StoredNodeFactory<Bytes32> nodeFactory =
new StoredNodeFactory<>(nodeLoader, value -> (Bytes32) value);
StoredBatchedVerkleTrie<Bytes32, Bytes32> trie =
new StoredBatchedVerkleTrie<Bytes32, Bytes32>(batchProcessor, nodeFactory);
Bytes32 key =
Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff");
Bytes32 value =
Bytes32.fromHexString("0x1000000000000000000000000000000000000000000000000000000000000000");
trie.put(key, value);
trie.remove(key);
trie.remove(key);
StoredBatchedVerkleTrie<Bytes32, Bytes32> storedTrie =
new StoredBatchedVerkleTrie<Bytes32, Bytes32>(batchProcessor, nodeFactory);
assertThat(storedTrie.getRootHash()).isEqualTo(Bytes32.ZERO);
}

@Test
public void testTwoValuesAtSameStem() throws Exception {
NodeUpdaterMock nodeUpdater = new NodeUpdaterMock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,23 @@ public void testOneValue() {
assertThat(storedTrie.get(key).orElse(null)).as("Retrieved value").isEqualTo(value);
}

@Test
public void testDeleteAlreadyDeletedValue() {
NodeUpdaterMock nodeUpdater = new NodeUpdaterMock();
NodeLoaderMock nodeLoader = new NodeLoaderMock(nodeUpdater.storage);
StoredNodeFactory<Bytes32> nodeFactory =
new StoredNodeFactory<>(nodeLoader, value -> (Bytes32) value);
StoredVerkleTrie<Bytes32, Bytes32> trie = new StoredVerkleTrie<Bytes32, Bytes32>(nodeFactory);
Bytes32 key =
Bytes32.fromHexString("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff");
Bytes32 value =
Bytes32.fromHexString("0x1000000000000000000000000000000000000000000000000000000000000000");
trie.put(key, value);
trie.remove(key);
trie.remove(key);
assertThat(trie.getRootHash()).isEqualTo(Bytes32.ZERO);
}

@Test
public void testTwoValuesAtSameStem() throws Exception {
NodeUpdaterMock nodeUpdater = new NodeUpdaterMock();
Expand Down

0 comments on commit 1f13566

Please sign in to comment.