Skip to content
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

MerkleDB Compact Path Bytes #2010

Merged
merged 136 commits into from
Sep 29, 2023
Merged

MerkleDB Compact Path Bytes #2010

merged 136 commits into from
Sep 29, 2023

Conversation

dboehm-avalabs
Copy link
Contributor

@dboehm-avalabs dboehm-avalabs commented Sep 13, 2023

We wish to support merkle trie branch factors other than 16, so this PR attempts to change paths to support that. At the same time, it reworks them to be stored in compressed format so that they don't have to be converted back and forth when interacting with the database.

@dboehm-avalabs dboehm-avalabs changed the title [WIP] MerkleDB Compact Path Bytes MerkleDB Compact Path Bytes Sep 29, 2023
@danlaine danlaine merged commit f79d609 into dev Sep 29, 2023
14 of 15 checks passed
@danlaine danlaine deleted the CompactPath branch September 29, 2023 20:00
@@ -944,7 +959,7 @@ func (db *merkleDB) VerifyChangeProof(
}

// Note that if [start] is Nothing, smallestPath is the empty path.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment seems out of date now

Comment on lines +114 to +120
// check that the tokens in the partially filled final byte of [prefix] are
// equal to the tokens in the final byte of [p].
for i := prefix.tokensLength - remainderTokens; i < prefix.tokensLength; i++ {
if p.Token(i) != prefix.Token(i) {
return false
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like we should be able to do some masking here rather than explicitly checking each token

Comment on lines +206 to +210
bytesNeeded := tokens / p.tokensPerByte
if tokens%p.tokensPerByte > 0 {
bytesNeeded++
}
return bytesNeeded
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return (tokens + p.tokensPerByte - 1) / p.tokensPerByte

func shiftCopy(dst []byte, src string, shift byte) {
i := 0
for ; i < len(src)-1; i++ {
dst[i] = src[i]<<shift + src[i+1]>>(8-shift)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably have written this as:

dst[i] = src[i]<<shift | src[i+1]>>(8-shift)

// We want to zero out everything to the right of the last token, which is at index [tokensToTake] - 1
// Mask will be (8-bitsToShift) number of 1's followed by (bitsToShift) number of 0's
mask := byte(0xFF << p.bitsToShift(tokensToTake-1))
buffer[len(buffer)-1] = buffer[len(buffer)-1] & mask
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

buffer[len(buffer)-1] &= mask

Comment on lines +181 to +184
bytesKey := stringToByteSlice(key)
if !changes.ConsumeBytes {
bytesKey = slices.Clone(bytesKey)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either this doesn't feel needed - or this always feels needed... Either way I feel like something is weird here

Comment on lines +173 to +175
if !changes.ConsumeBytes {
key = slices.Clone(op.Key)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super nit - but the order of var declaration + updating seems really weird here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
No open projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

4 participants