-
Notifications
You must be signed in to change notification settings - Fork 674
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
-- add codec test and move helper
#1944
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e8a8992
remove unneeded helper
f087179
remove stale comment
94ef119
add test; nit remove _ from test name
8d6de7e
move helper function to other file
9aaa1a6
Merge remote-tracking branch 'upstream/dev' into merkledb-codec-test-2
72d3b2a
appease linter
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,54 +12,9 @@ import ( | |
"github.com/stretchr/testify/require" | ||
|
||
"github.com/ava-labs/avalanchego/ids" | ||
"github.com/ava-labs/avalanchego/utils/hashing" | ||
"github.com/ava-labs/avalanchego/utils/maybe" | ||
) | ||
|
||
// TODO add more codec tests | ||
|
||
func newRandomProofNode(r *rand.Rand) ProofNode { | ||
key := make([]byte, r.Intn(32)) // #nosec G404 | ||
_, _ = r.Read(key) // #nosec G404 | ||
serializedKey := newPath(key).Serialize() | ||
|
||
val := make([]byte, r.Intn(64)) // #nosec G404 | ||
_, _ = r.Read(val) // #nosec G404 | ||
|
||
children := map[byte]ids.ID{} | ||
for j := 0; j < NodeBranchFactor; j++ { | ||
if r.Float64() < 0.5 { | ||
var childID ids.ID | ||
_, _ = r.Read(childID[:]) // #nosec G404 | ||
children[byte(j)] = childID | ||
} | ||
} | ||
|
||
hasValue := rand.Intn(2) == 1 // #nosec G404 | ||
var valueOrHash maybe.Maybe[[]byte] | ||
if hasValue { | ||
// use the hash instead when length is greater than the hash length | ||
if len(val) >= HashLength { | ||
val = hashing.ComputeHash256(val) | ||
} else if len(val) == 0 { | ||
// We do this because when we encode a value of []byte{} we will later | ||
// decode it as nil. | ||
// Doing this prevents inconsistency when comparing the encoded and | ||
// decoded values. | ||
// Calling nilEmptySlices doesn't set this because it is a private | ||
// variable on the struct | ||
val = nil | ||
} | ||
valueOrHash = maybe.Some(val) | ||
} | ||
|
||
return ProofNode{ | ||
KeyPath: serializedKey, | ||
ValueOrHash: valueOrHash, | ||
Children: children, | ||
} | ||
} | ||
|
||
func FuzzCodecBool(f *testing.F) { | ||
f.Fuzz( | ||
func( | ||
|
@@ -224,7 +179,7 @@ func FuzzCodecDBNodeDeterministic(f *testing.F) { | |
) | ||
} | ||
|
||
func TestCodec_DecodeDBNode(t *testing.T) { | ||
func TestCodecDecodeDBNode(t *testing.T) { | ||
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. Removed |
||
require := require.New(t) | ||
|
||
var ( | ||
|
@@ -260,3 +215,59 @@ func TestCodec_DecodeDBNode(t *testing.T) { | |
err = codec.decodeDBNode(proofBytesBuf.Bytes(), &parsedDBNode) | ||
require.ErrorIs(err, errTooManyChildren) | ||
} | ||
|
||
// Ensure that encodeHashValues is deterministic | ||
func FuzzEncodeHashValues(f *testing.F) { | ||
codec1 := newCodec() | ||
codec2 := newCodec() | ||
|
||
f.Fuzz( | ||
func( | ||
t *testing.T, | ||
randSeed int, | ||
) { | ||
require := require.New(t) | ||
|
||
// Create a random *hashValues | ||
r := rand.New(rand.NewSource(int64(randSeed))) // #nosec G404 | ||
|
||
children := map[byte]child{} | ||
numChildren := r.Intn(NodeBranchFactor) // #nosec G404 | ||
for i := 0; i < numChildren; i++ { | ||
compressedPathLen := r.Intn(32) // #nosec G404 | ||
compressedPathBytes := make([]byte, compressedPathLen) | ||
_, _ = r.Read(compressedPathBytes) // #nosec G404 | ||
|
||
children[byte(i)] = child{ | ||
compressedPath: newPath(compressedPathBytes), | ||
id: ids.GenerateTestID(), | ||
hasValue: r.Intn(2) == 1, // #nosec G404 | ||
} | ||
} | ||
|
||
hasValue := r.Intn(2) == 1 // #nosec G404 | ||
value := maybe.Nothing[[]byte]() | ||
if hasValue { | ||
valueBytes := make([]byte, r.Intn(64)) // #nosec G404 | ||
_, _ = r.Read(valueBytes) // #nosec G404 | ||
value = maybe.Some(valueBytes) | ||
} | ||
|
||
key := make([]byte, r.Intn(32)) // #nosec G404 | ||
_, _ = r.Read(key) // #nosec G404 | ||
|
||
hv := &hashValues{ | ||
Children: children, | ||
Value: value, | ||
Key: newPath(key).Serialize(), | ||
} | ||
|
||
// Serialize the *hashValues with both codecs | ||
hvBytes1 := codec1.encodeHashValues(hv) | ||
hvBytes2 := codec2.encodeHashValues(hv) | ||
|
||
// Make sure they're the same | ||
require.Equal(hvBytes1, hvBytes2) | ||
}, | ||
) | ||
} |
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.
moved to
proof_test.go