-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement incremental packed list (basic type list/vector) hashing #122
Merged
+360
−77
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
54f72dc
Add serializing sublist for incremental hash
Nashatyrev a7ffb9e
Implement incremental hashing for packed lists
Nashatyrev cb37db1
Fix IncrementalHasher bug
Nashatyrev 9d5dd7f
Adjust random list test
Nashatyrev eb3771a
A couple of minor incremental hasher fixes
Nashatyrev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -1,14 +1,17 @@ | ||
package org.ethereum.beacon.ssz.visitor; | ||
|
||
import static java.lang.Math.min; | ||
import static java.util.stream.Collectors.toList; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.SortedSet; | ||
import java.util.TreeSet; | ||
import java.util.function.BiFunction; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
import org.ethereum.beacon.ssz.incremental.ObservableComposite; | ||
import org.ethereum.beacon.ssz.incremental.UpdateListener; | ||
import org.ethereum.beacon.ssz.type.SSZCompositeType; | ||
|
@@ -64,10 +67,8 @@ public MerkleTrie visitComposite(SSZCompositeType type, Object rawValue, | |
tracker.merkleTree = super.visitComposite(type, rawValue, childVisitor); | ||
} else if (!tracker.elementsUpdated.isEmpty()){ | ||
if (type.isList() && ((SSZListType) type).getElementType().isBasicType()) { | ||
// tracker.merkleTree = | ||
// updatePackedTrie(value, childVisitor, tracker.merkleTree, tracker.elementsUpdated); | ||
// TODO fallback to full recalculation for now | ||
tracker.merkleTree = super.visitComposite(type, rawValue, childVisitor); | ||
tracker.merkleTree = | ||
updatePackedTrie((SSZListType) type, rawValue, childVisitor, tracker.merkleTree, tracker.elementsUpdated); | ||
} else { | ||
tracker.merkleTree = | ||
updateNonPackedTrie(type, rawValue, childVisitor, tracker.merkleTree, tracker.elementsUpdated); | ||
|
@@ -88,19 +89,55 @@ private MerkleTrie updateNonPackedTrie( | |
MerkleTrie merkleTree, | ||
SortedSet<Integer> elementsUpdated) { | ||
|
||
int newChildrenCount = type.getChildrenCount(value); | ||
MerkleTrie newTrie = copyWithSize(merkleTree, newChildrenCount); | ||
int newChunksCount = newTrie.nodes.length / 2; | ||
return updateTrie( | ||
type, | ||
value, | ||
idx -> childVisitor.apply(idx, type.getChild(value, idx)).getFinalRoot(), | ||
type.getChildrenCount(value), | ||
merkleTree, | ||
elementsUpdated); | ||
} | ||
|
||
private MerkleTrie updatePackedTrie( | ||
SSZListType type, | ||
Object value, | ||
BiFunction<Integer, Object, MerkleTrie> childVisitor, | ||
MerkleTrie oldTrie, | ||
SortedSet<Integer> elementsUpdated) { | ||
|
||
int typeSize = type.getElementType().getSize(); | ||
int valsPerChunk = bytesPerChunk / typeSize; | ||
|
||
return updateTrie( | ||
type, | ||
value, | ||
idx -> serializePackedChunk(type, value, idx), | ||
(type.getChildrenCount(value) - 1) / valsPerChunk + 1, | ||
oldTrie, | ||
elementsUpdated.stream().map(i -> i / valsPerChunk).distinct().collect(toList())); | ||
} | ||
|
||
private MerkleTrie updateTrie( | ||
SSZCompositeType type, | ||
Object value, | ||
Function<Integer, BytesValue> childChunkSupplier, | ||
int newChunksCount, | ||
MerkleTrie oldTrie, | ||
Collection<Integer> chunksUpdated) { | ||
|
||
MerkleTrie newTrie = copyWithSize(oldTrie, newChunksCount); | ||
int newTrieWidth = newTrie.nodes.length / 2; | ||
|
||
int pos = newChunksCount; | ||
int pos = newTrieWidth; | ||
|
||
List<Integer> elementsToRecalc = new ArrayList<>(); | ||
for (int i: elementsUpdated) { | ||
if (i < newChunksCount) { | ||
for (int i: chunksUpdated) { | ||
if (i < newTrieWidth) { | ||
elementsToRecalc.add(i); | ||
if (i < newChildrenCount) { | ||
MerkleTrie childHash = childVisitor.apply(i, type.getChild(value, i)); | ||
newTrie.nodes[pos + i] = childHash.getFinalRoot(); | ||
if (i < newChunksCount) { | ||
newTrie.nodes[pos + i] = childChunkSupplier.apply(i); | ||
} else { | ||
newTrie.nodes[pos + i] = getZeroHash(0); | ||
} | ||
} | ||
} | ||
|
@@ -130,15 +167,6 @@ private MerkleTrie updateNonPackedTrie( | |
return newTrie; | ||
} | ||
|
||
private MerkleTrie updatePackedTrie( | ||
SSZCompositeType type, | ||
Object value, | ||
Function<Long, MerkleTrie> childVisitor, | ||
MerkleTrie merkleTree, | ||
SortedSet<Integer> elementsUpdated) { | ||
|
||
throw new UnsupportedOperationException(); | ||
} | ||
private MerkleTrie copyWithSize(MerkleTrie trie, int newChunksCount) { | ||
int newSize = (int) nextPowerOf2(newChunksCount) * 2; | ||
if (newSize == trie.nodes.length) { | ||
|
@@ -162,4 +190,20 @@ private MerkleTrie copyWithSize(MerkleTrie trie, int newChunksCount) { | |
return new MerkleTrie(newNodes); | ||
} | ||
} | ||
|
||
private BytesValue serializePackedChunk(SSZListType basicListType, Object listValue, int chunkIndex) { | ||
int typeSize = basicListType.getElementType().getSize(); | ||
int valsPerChunk = bytesPerChunk / typeSize; | ||
if (valsPerChunk * typeSize != bytesPerChunk) { | ||
throw new UnsupportedOperationException(""); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add some message here. |
||
} | ||
int idx = chunkIndex * valsPerChunk; | ||
int len = Math.min(valsPerChunk, basicListType.getChildrenCount(listValue) - idx); | ||
BytesValue chunk = serializer.visitList(basicListType, listValue, idx, len) | ||
.getSerializedBody(); | ||
if (len < valsPerChunk) { | ||
chunk = BytesValue.concat(chunk, BytesValue.wrap(new byte[bytesPerChunk - chunk.size()])); | ||
} | ||
return chunk; | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It appears that
childVisitor
is not used.