Skip to content

Commit

Permalink
merkledb -- make tests use time as randomness seed (#2470)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Laine authored Dec 15, 2023
1 parent 8107f79 commit 8c47e3f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 43 deletions.
3 changes: 2 additions & 1 deletion x/sync/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var (
_ Client = (*client)(nil)

errInvalidRangeProof = errors.New("failed to verify range proof")
errInvalidChangeProof = errors.New("failed to verify change proof")
errTooManyKeys = errors.New("response contains more than requested keys")
errTooManyBytes = errors.New("response contains more than requested bytes")
errUnexpectedChangeProofResponse = errors.New("unexpected response type")
Expand Down Expand Up @@ -149,7 +150,7 @@ func (c *client) GetChangeProof(
endKey,
endRoot,
); err != nil {
return nil, fmt.Errorf("%w due to %w", errInvalidRangeProof, err)
return nil, fmt.Errorf("%w due to %w", errInvalidChangeProof, err)
}

return &merkledb.ChangeOrRangeProof{
Expand Down
52 changes: 10 additions & 42 deletions x/sync/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,9 @@ func sendRangeProofRequest(
}

func TestGetRangeProof(t *testing.T) {
// TODO use time as random seed instead of 1
// once we move to go 1.20 which allows for
// joining multiple errors with %w. Right now,
// for some of these tests, we may get different
// errors based on randomness but we can only
// assert one error.
r := rand.New(rand.NewSource(1)) // #nosec G404
now := time.Now().UnixNano()
t.Logf("seed: %d", now)
r := rand.New(rand.NewSource(now)) // #nosec G404

smallTrieKeyCount := defaultRequestKeyLimit
smallTrieDB, _, err := generateTrieWithMinKeyLen(t, r, smallTrieKeyCount, 1)
Expand Down Expand Up @@ -280,19 +276,7 @@ func TestGetRangeProof(t *testing.T) {
response.StartProof = proof.StartProof
response.EndProof = proof.EndProof
},
expectedErr: merkledb.ErrInvalidProof,
},
"removed last key in response": {
db: largeTrieDB,
request: &pb.SyncGetRangeProofRequest{
RootHash: largeTrieRoot[:],
KeyLimit: defaultRequestKeyLimit,
BytesLimit: defaultRequestByteSizeLimit,
},
modifyResponse: func(response *merkledb.RangeProof) {
response.KeyValues = response.KeyValues[:len(response.KeyValues)-2]
},
expectedErr: merkledb.ErrProofNodeNotForKey,
expectedErr: errInvalidRangeProof,
},
"removed key from middle of response": {
db: largeTrieDB,
Expand All @@ -319,7 +303,7 @@ func TestGetRangeProof(t *testing.T) {
},
expectedErr: merkledb.ErrNoEndProof,
},
"end proof nodes removed": {
"end proof removed": {
db: largeTrieDB,
request: &pb.SyncGetRangeProofRequest{
RootHash: largeTrieRoot[:],
Expand Down Expand Up @@ -503,13 +487,9 @@ func sendChangeProofRequest(
}

func TestGetChangeProof(t *testing.T) {
// TODO use time as random seed instead of 1
// once we move to go 1.20 which allows for
// joining multiple errors with %w. Right now,
// for some of these tests, we may get different
// errors based on randomness but we can only
// assert one error.
r := rand.New(rand.NewSource(1)) // #nosec G404
now := time.Now().UnixNano()
t.Logf("seed: %d", now)
r := rand.New(rand.NewSource(now)) // #nosec G404

serverDB, err := merkledb.New(
context.Background(),
Expand Down Expand Up @@ -625,19 +605,7 @@ func TestGetChangeProof(t *testing.T) {
modifyChangeProofResponse: func(response *merkledb.ChangeProof) {
response.KeyChanges = response.KeyChanges[1:]
},
expectedErr: merkledb.ErrInvalidProof,
},
"removed last key in response": {
request: &pb.SyncGetChangeProofRequest{
StartRootHash: startRoot[:],
EndRootHash: endRoot[:],
KeyLimit: defaultRequestKeyLimit,
BytesLimit: defaultRequestByteSizeLimit,
},
modifyChangeProofResponse: func(response *merkledb.ChangeProof) {
response.KeyChanges = response.KeyChanges[:len(response.KeyChanges)-2]
},
expectedErr: merkledb.ErrProofNodeNotForKey,
expectedErr: errInvalidChangeProof,
},
"removed key from middle of response": {
request: &pb.SyncGetChangeProofRequest{
Expand Down Expand Up @@ -677,7 +645,7 @@ func TestGetChangeProof(t *testing.T) {
modifyRangeProofResponse: func(response *merkledb.RangeProof) {
response.KeyValues = response.KeyValues[1:]
},
expectedErr: merkledb.ErrInvalidProof,
expectedErr: errInvalidRangeProof,
expectRangeProof: true,
},
}
Expand Down

0 comments on commit 8c47e3f

Please sign in to comment.