-
Notifications
You must be signed in to change notification settings - Fork 670
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
x/sync
-- use for sending Range Proofs
#1537
Merged
Merged
Changes from 53 commits
Commits
Show all changes
58 commits
Select commit
Hold shift + click to select a range
d6426bf
Update sync.proto
dboehm-avalabs 51e75d8
Update sync.proto
dboehm-avalabs dd74d3b
Update sync.proto
dboehm-avalabs b3b68ff
sync.proto recommended changes
rkuris f1eb6de
Review comments
rkuris 6cce0d3
rename KeyProof to ProofNode; rename ProofNode.value_hash to ProofNod…
ec51aeb
actually add SerializedPath message
63386d4
int --> uint32 in proto
e108976
regenerate .pb.go files
7845dfd
Merge remote-tracking branch 'upstream/dev' into MerkleSyncProtos
d04c7a1
Merge branch 'MerkleSyncProtos' into MerkleSyncProtos-use-proto
dfe894d
add ToProto for ProofNode and RangeProof; send range proof repsonse a…
5af51ac
add FromProto for ProofNode; add tests
42422d1
add tests; add proto MaybeBytes type
35267b5
add proto MaybeBytes type
0d08f96
Merge branch 'MerkleSyncProtos' into MerkleSyncProtos-use-proto
ef94cf1
add to test
860ee03
disallow maybe with nothing and value
b3d3387
add comment
70e4ab4
add more proofNode validity checks
c0b2bdc
remove EncodeRangeProof and DecodeRangeProof
6dae5b5
Merge remote-tracking branch 'upstream/dev' into MerkleSyncProtos
e0f48b7
remove unused functions
7c1eb7c
import nits
e493c27
Merge branch 'MerkleSyncProtos' into MerkleSyncProtos-use-proto
d33831f
make value in MaybeBytes non-optional; make value in KeyChange a Mayb…
3d6b53a
Merge branch 'MerkleSyncProtos' into MerkleSyncProtos-use-proto
a47ad58
regenerate .pb.go files
2859a54
add had_roots_in_history to ChangeProof
1115354
Merge branch 'MerkleSyncProtos-use-proto' of github.com:ava-labs/aval…
d0cde74
Merge branch 'MerkleSyncProtos' into MerkleSyncProtos-use-proto
d6daa7b
fix proto ordering
4d47288
Merge branch 'MerkleSyncProtos' into MerkleSyncProtos-use-proto
35fbcec
update .pb.go files
de32cda
comment nit
86a1b63
Merge branch 'MerkleSyncProtos' into MerkleSyncProtos-use-proto
73bcfd0
/* --> // for proto comments
7b434ea
Merge remote-tracking branch 'upstream/dev' into MerkleSyncProtos
4dffa64
Merge branch 'MerkleSyncProtos' into MerkleSyncProtos-use-proto
72b748c
/* --> // for proto comments
bb98fc5
Merge branch 'MerkleSyncProtos' into MerkleSyncProtos-use-proto
f7d39a7
nit
0524de5
Merge branch 'dev' into MerkleSyncProtos
0050f21
Merge remote-tracking branch 'upstream/dev' into MerkleSyncProtos
c99236d
Merge branch 'MerkleSyncProtos' of github.com:ava-labs/avalanchego in…
61f4c27
Merge branch 'MerkleSyncProtos' into MerkleSyncProtos-use-proto
cad3a80
rename RangeProofResponse --> RangeProof
52af5a6
Merge branch 'MerkleSyncProtos' into MerkleSyncProtos-use-proto
4417a28
update .pb.go files
f0c7833
Merge remote-tracking branch 'upstream/dev' into MerkleSyncProtos-use…
c2d6692
add nil checks in ProofNode.UnmarshalProto
78da8e0
Merge remote-tracking branch 'upstream/dev' into MerkleSyncProtos-use…
be0f6bd
add nil checks in RangeProof.UnmarshalProto
d05f98c
Merge remote-tracking branch 'upstream/dev' into MerkleSyncProtos-use…
dfea9e5
change nibble length field from uint32 to uint64
cea0f50
import nit
9022216
appease linter
b7263eb
Merge branch 'dev' into MerkleSyncProtos-use-proto
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
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 |
---|---|---|
|
@@ -21,6 +21,8 @@ import ( | |
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 | ||
|
||
|
@@ -32,22 +34,28 @@ func newRandomProofNode(r *rand.Rand) ProofNode { | |
children[byte(j)] = childID | ||
} | ||
} | ||
// 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 | ||
|
||
hasValue := rand.Intn(2) == 1 // #nosec G404 | ||
var valueOrHash 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 = Some(val) | ||
Comment on lines
+38
to
+53
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. Changed so that the |
||
} | ||
|
||
return ProofNode{ | ||
KeyPath: newPath(key).Serialize(), | ||
ValueOrHash: Some(val), | ||
KeyPath: serializedKey, | ||
ValueOrHash: valueOrHash, | ||
Children: children, | ||
} | ||
} | ||
|
@@ -293,29 +301,6 @@ func FuzzCodecChangeProofCanonical(f *testing.F) { | |
) | ||
} | ||
|
||
func FuzzCodecRangeProofCanonical(f *testing.F) { | ||
f.Fuzz( | ||
func( | ||
t *testing.T, | ||
b []byte, | ||
) { | ||
require := require.New(t) | ||
|
||
codec := Codec.(*codecImpl) | ||
proof := &RangeProof{} | ||
got, err := codec.DecodeRangeProof(b, proof) | ||
if err != nil { | ||
return | ||
} | ||
|
||
// Encoding [proof] should be the same as [b]. | ||
buf, err := codec.EncodeRangeProof(got, proof) | ||
require.NoError(err) | ||
require.Equal(b, buf) | ||
}, | ||
) | ||
} | ||
|
||
func FuzzCodecDBNodeCanonical(f *testing.F) { | ||
f.Fuzz( | ||
func( | ||
|
@@ -434,66 +419,6 @@ func FuzzCodecChangeProofDeterministic(f *testing.F) { | |
) | ||
} | ||
|
||
func FuzzCodecRangeProofDeterministic(f *testing.F) { | ||
f.Fuzz( | ||
func( | ||
t *testing.T, | ||
randSeed int, | ||
numStartProofNodes uint, | ||
numEndProofNodes uint, | ||
numKeyValues uint, | ||
) { | ||
r := rand.New(rand.NewSource(int64(randSeed))) // #nosec G404 | ||
|
||
var rootID ids.ID | ||
_, _ = r.Read(rootID[:]) // #nosec G404 | ||
|
||
startProofNodes := make([]ProofNode, numStartProofNodes) | ||
for i := range startProofNodes { | ||
startProofNodes[i] = newRandomProofNode(r) | ||
} | ||
|
||
endProofNodes := make([]ProofNode, numEndProofNodes) | ||
for i := range endProofNodes { | ||
endProofNodes[i] = newRandomProofNode(r) | ||
} | ||
|
||
keyValues := make([]KeyValue, numKeyValues) | ||
for i := range keyValues { | ||
key := make([]byte, r.Intn(32)) // #nosec G404 | ||
_, _ = r.Read(key) // #nosec G404 | ||
val := make([]byte, r.Intn(32)) // #nosec G404 | ||
_, _ = r.Read(val) // #nosec G404 | ||
keyValues[i] = KeyValue{ | ||
Key: key, | ||
Value: val, | ||
} | ||
} | ||
|
||
proof := RangeProof{ | ||
StartProof: startProofNodes, | ||
EndProof: endProofNodes, | ||
KeyValues: keyValues, | ||
} | ||
|
||
proofBytes, err := Codec.EncodeRangeProof(Version, &proof) | ||
require.NoError(t, err) | ||
|
||
var gotProof RangeProof | ||
_, err = Codec.DecodeRangeProof(proofBytes, &gotProof) | ||
require.NoError(t, err) | ||
|
||
nilEmptySlices(&proof) | ||
nilEmptySlices(&gotProof) | ||
require.Equal(t, proof, gotProof) | ||
|
||
proofBytes2, err := Codec.EncodeRangeProof(Version, &gotProof) | ||
require.NoError(t, err) | ||
require.Equal(t, proofBytes, proofBytes2) | ||
}, | ||
) | ||
} | ||
|
||
func FuzzCodecDBNodeDeterministic(f *testing.F) { | ||
f.Fuzz( | ||
func( | ||
|
@@ -608,39 +533,6 @@ func TestCodec_DecodeChangeProof(t *testing.T) { | |
require.ErrorIs(err, errNegativeNumKeyValues) | ||
} | ||
|
||
func TestCodec_DecodeRangeProof(t *testing.T) { | ||
require := require.New(t) | ||
|
||
_, err := Codec.DecodeRangeProof([]byte{1}, nil) | ||
require.ErrorIs(err, errDecodeNil) | ||
|
||
var ( | ||
parsedProof RangeProof | ||
tooShortBytes = make([]byte, minRangeProofLen-1) | ||
) | ||
_, err = Codec.DecodeRangeProof(tooShortBytes, &parsedProof) | ||
require.ErrorIs(err, io.ErrUnexpectedEOF) | ||
|
||
proof := RangeProof{ | ||
StartProof: nil, | ||
EndProof: nil, | ||
KeyValues: nil, | ||
} | ||
|
||
proofBytes, err := Codec.EncodeRangeProof(Version, &proof) | ||
require.NoError(err) | ||
|
||
// Remove key-values length (0) from end | ||
proofBytes = proofBytes[:len(proofBytes)-minVarIntLen] | ||
proofBytesBuf := bytes.NewBuffer(proofBytes) | ||
// Put key-value length (-1) at end | ||
err = Codec.(*codecImpl).encodeInt(proofBytesBuf, -1) | ||
require.NoError(err) | ||
|
||
_, err = Codec.DecodeRangeProof(proofBytesBuf.Bytes(), &parsedProof) | ||
require.ErrorIs(err, errNegativeNumKeyValues) | ||
} | ||
|
||
func TestCodec_DecodeDBNode(t *testing.T) { | ||
require := require.New(t) | ||
|
||
|
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.
added so that we can assert this error in a
sync
test