Skip to content

Commit

Permalink
merkledb and sync -- use time based rand seed (ava-labs#1607)
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Laine <daniel.laine@avalabs.org>
Co-authored-by: Stephen Buttolph <stephen@avalabs.org>
  • Loading branch information
Dan Laine and StephenButtolph authored Jun 12, 2023
1 parent 13863e3 commit b157612
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 25 deletions.
13 changes: 10 additions & 3 deletions x/merkledb/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"math/rand"
"strconv"
"testing"
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -613,7 +614,9 @@ func Test_MerkleDB_Random_Insert_Ordering(t *testing.T) {
}

for i := 0; i < 3; i++ {
r := rand.New(rand.NewSource(int64(i))) // #nosec G404
now := time.Now().UnixNano()
t.Logf("seed for iter %d: %d", i, now)
r := rand.New(rand.NewSource(now)) // #nosec G404

ops := make([]*testOperation, 0, totalState)
allKeys = [][]byte{}
Expand Down Expand Up @@ -675,15 +678,19 @@ func Test_MerkleDB_RandomCases(t *testing.T) {
require := require.New(t)

for i := 150; i < 500; i += 10 {
r := rand.New(rand.NewSource(int64(i))) // #nosec G404
now := time.Now().UnixNano()
t.Logf("seed for iter %d: %d", i, now)
r := rand.New(rand.NewSource(now)) // #nosec G404
runRandDBTest(require, r, generate(require, r, i, .01))
}
}

func Test_MerkleDB_RandomCases_InitialValues(t *testing.T) {
require := require.New(t)

r := rand.New(rand.NewSource(int64(0))) // #nosec G404
now := time.Now().UnixNano()
t.Logf("seed: %d", now)
r := rand.New(rand.NewSource(now)) // #nosec G404
runRandDBTest(require, r, generateInitialValues(require, r, 1000, 2500, 0.0))
}

Expand Down
5 changes: 4 additions & 1 deletion x/merkledb/history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"math/rand"
"testing"
"time"

"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -87,7 +88,9 @@ func Test_History_Large(t *testing.T) {
require := require.New(t)

for i := 1; i < 10; i++ {
r := rand.New(rand.NewSource(int64(i))) // #nosec G404
now := time.Now().UnixNano()
t.Logf("seed for iter %d: %d", i, now)
r := rand.New(rand.NewSource(now)) // #nosec G404
db, err := New(
context.Background(),
memdb.New(),
Expand Down
37 changes: 28 additions & 9 deletions x/merkledb/proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"math/rand"
"testing"
"time"

"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -1204,7 +1205,9 @@ func TestVerifyProofPath(t *testing.T) {
}

func TestProofNodeUnmarshalProtoInvalidMaybe(t *testing.T) {
rand := rand.New(rand.NewSource(1337)) // #nosec G404
now := time.Now().UnixNano()
t.Logf("seed: %d", now)
rand := rand.New(rand.NewSource(now)) // #nosec G404

node := newRandomProofNode(rand)
protoNode := node.ToProto()
Expand All @@ -1221,7 +1224,9 @@ func TestProofNodeUnmarshalProtoInvalidMaybe(t *testing.T) {
}

func TestProofNodeUnmarshalProtoInvalidChildBytes(t *testing.T) {
rand := rand.New(rand.NewSource(1337)) // #nosec G404
now := time.Now().UnixNano()
t.Logf("seed: %d", now)
rand := rand.New(rand.NewSource(now)) // #nosec G404

node := newRandomProofNode(rand)
protoNode := node.ToProto()
Expand All @@ -1236,7 +1241,9 @@ func TestProofNodeUnmarshalProtoInvalidChildBytes(t *testing.T) {
}

func TestProofNodeUnmarshalProtoInvalidChildIndex(t *testing.T) {
rand := rand.New(rand.NewSource(1337)) // #nosec G404
now := time.Now().UnixNano()
t.Logf("seed: %d", now)
rand := rand.New(rand.NewSource(now)) // #nosec G404

node := newRandomProofNode(rand)
protoNode := node.ToProto()
Expand All @@ -1250,7 +1257,9 @@ func TestProofNodeUnmarshalProtoInvalidChildIndex(t *testing.T) {
}

func TestProofNodeUnmarshalProtoMissingFields(t *testing.T) {
rand := rand.New(rand.NewSource(1337)) // #nosec G404
now := time.Now().UnixNano()
t.Logf("seed: %d", now)
rand := rand.New(rand.NewSource(now)) // #nosec G404

type test struct {
name string
Expand Down Expand Up @@ -1299,7 +1308,9 @@ func TestProofNodeUnmarshalProtoMissingFields(t *testing.T) {

func TestProofNodeProtoMarshalUnmarshal(t *testing.T) {
require := require.New(t)
rand := rand.New(rand.NewSource(1337)) // #nosec G404
now := time.Now().UnixNano()
t.Logf("seed: %d", now)
rand := rand.New(rand.NewSource(now)) // #nosec G404

for i := 0; i < 1_000; i++ {
node := newRandomProofNode(rand)
Expand All @@ -1319,7 +1330,9 @@ func TestProofNodeProtoMarshalUnmarshal(t *testing.T) {

func TestRangeProofProtoMarshalUnmarshal(t *testing.T) {
require := require.New(t)
rand := rand.New(rand.NewSource(1337)) // #nosec G404
now := time.Now().UnixNano()
t.Logf("seed: %d", now)
rand := rand.New(rand.NewSource(now)) // #nosec G404

for i := 0; i < 500; i++ {
// Make a random range proof.
Expand Down Expand Up @@ -1373,7 +1386,9 @@ func TestRangeProofProtoMarshalUnmarshal(t *testing.T) {

func TestChangeProofProtoMarshalUnmarshal(t *testing.T) {
require := require.New(t)
rand := rand.New(rand.NewSource(1337)) // #nosec G404
now := time.Now().UnixNano()
t.Logf("seed: %d", now)
rand := rand.New(rand.NewSource(now)) // #nosec G404

for i := 0; i < 500; i++ {
// Make a random change proof.
Expand Down Expand Up @@ -1437,7 +1452,9 @@ func TestChangeProofUnmarshalProtoNil(t *testing.T) {
}

func TestChangeProofUnmarshalProtoNilValue(t *testing.T) {
rand := rand.New(rand.NewSource(1337)) // #nosec G404
now := time.Now().UnixNano()
t.Logf("seed: %d", now)
rand := rand.New(rand.NewSource(now)) // #nosec G404

// Make a random change proof.
startProofLen := rand.Intn(32)
Expand Down Expand Up @@ -1508,7 +1525,9 @@ func TestChangeProofUnmarshalProtoInvalidMaybe(t *testing.T) {

func TestProofProtoMarshalUnmarshal(t *testing.T) {
require := require.New(t)
rand := rand.New(rand.NewSource(1337)) // #nosec G404
now := time.Now().UnixNano()
t.Logf("seed: %d", now)
rand := rand.New(rand.NewSource(now)) // #nosec G404

for i := 0; i < 500; i++ {
// Make a random proof.
Expand Down
5 changes: 4 additions & 1 deletion x/merkledb/view_iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"math/rand"
"sort"
"testing"
"time"

"github.com/stretchr/testify/require"
"golang.org/x/exp/maps"
Expand Down Expand Up @@ -158,7 +159,9 @@ func Test_TrieView_IteratorStartPrefix(t *testing.T) {
// iterating over the last view.
func Test_TrieView_Iterator_Random(t *testing.T) {
require := require.New(t)
rand := rand.New(rand.NewSource(1337)) // #nosec G404
now := time.Now().UnixNano()
t.Logf("seed: %d", now)
rand := rand.New(rand.NewSource(now)) // #nosec G404

var (
numKeyChanges = 5_000
Expand Down
16 changes: 15 additions & 1 deletion x/sync/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ func sendRangeRequest(
}

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

smallTrieKeyCount := defaultRequestKeyLimit
Expand Down Expand Up @@ -383,6 +389,12 @@ func sendChangeRequest(
}

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

trieDB, err := merkledb.New(
Expand Down Expand Up @@ -560,7 +572,9 @@ func TestGetChangeProof(t *testing.T) {
}

func TestRangeProofRetries(t *testing.T) {
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
require := require.New(t)

keyCount := defaultRequestKeyLimit
Expand Down
9 changes: 7 additions & 2 deletions x/sync/network_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"math/rand"
"testing"
"time"

"github.com/golang/mock/gomock"

Expand All @@ -23,7 +24,9 @@ import (
)

func Test_Server_GetRangeProof(t *testing.T) {
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

smallTrieDB, _, err := generateTrieWithMinKeyLen(t, r, defaultRequestKeyLimit, 1)
require.NoError(t, err)
Expand Down Expand Up @@ -143,7 +146,9 @@ func Test_Server_GetRangeProof(t *testing.T) {
}

func Test_Server_GetChangeProof(t *testing.T) {
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
trieDB, _, err := generateTrieWithMinKeyLen(t, r, defaultRequestKeyLimit, 1)
require.NoError(t, err)

Expand Down
32 changes: 24 additions & 8 deletions x/sync/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,9 @@ func Test_Midpoint(t *testing.T) {

func Test_Sync_FindNextKey_InSync(t *testing.T) {
for i := 0; i < 3; i++ {
r := rand.New(rand.NewSource(int64(i))) // #nosec G404
now := time.Now().UnixNano()
t.Logf("seed: %d", now)
r := rand.New(rand.NewSource(now)) // #nosec G404
dbToSync, err := generateTrie(t, r, 1000)
require.NoError(t, err)
syncRoot, err := dbToSync.GetMerkleRoot(context.Background())
Expand Down Expand Up @@ -352,7 +354,9 @@ func Test_Sync_FindNextKey_BranchInReceived(t *testing.T) {

func Test_Sync_FindNextKey_ExtraValues(t *testing.T) {
for i := 0; i < 10; i++ {
r := rand.New(rand.NewSource(int64(i))) // #nosec G404
now := time.Now().UnixNano()
t.Logf("seed: %d", now)
r := rand.New(rand.NewSource(now)) // #nosec G404
dbToSync, err := generateTrie(t, r, 1000)
require.NoError(t, err)
syncRoot, err := dbToSync.GetMerkleRoot(context.Background())
Expand Down Expand Up @@ -425,7 +429,9 @@ func isPrefix(data []byte, prefix []byte) bool {

func Test_Sync_FindNextKey_DifferentChild(t *testing.T) {
for i := 0; i < 10; i++ {
r := rand.New(rand.NewSource(int64(i))) // #nosec G404
now := time.Now().UnixNano()
t.Logf("seed: %d", now)
r := rand.New(rand.NewSource(now)) // #nosec G404
dbToSync, err := generateTrie(t, r, 500)
require.NoError(t, err)
syncRoot, err := dbToSync.GetMerkleRoot(context.Background())
Expand Down Expand Up @@ -471,7 +477,9 @@ func Test_Sync_FindNextKey_DifferentChild(t *testing.T) {
// Test findNextKey by computing the expected result in a naive, inefficient
// way and comparing it to the actual result
func TestFindNextKeyRandom(t *testing.T) {
rand := rand.New(rand.NewSource(1337)) //nolint:gosec
now := time.Now().UnixNano()
t.Logf("seed: %d", now)
rand := rand.New(rand.NewSource(now)) // #nosec G404
require := require.New(t)

// Create a "remote" database and "local" database
Expand Down Expand Up @@ -676,7 +684,9 @@ func TestFindNextKeyRandom(t *testing.T) {

func Test_Sync_Result_Correct_Root(t *testing.T) {
for i := 0; i < 3; i++ {
r := rand.New(rand.NewSource(int64(i))) // #nosec G404
now := time.Now().UnixNano()
t.Logf("seed: %d", now)
r := rand.New(rand.NewSource(now)) // #nosec G404
dbToSync, err := generateTrie(t, r, 1000)
require.NoError(t, err)
syncRoot, err := dbToSync.GetMerkleRoot(context.Background())
Expand Down Expand Up @@ -730,7 +740,9 @@ func Test_Sync_Result_Correct_Root(t *testing.T) {

func Test_Sync_Result_Correct_Root_With_Sync_Restart(t *testing.T) {
for i := 0; i < 3; i++ {
r := rand.New(rand.NewSource(int64(i))) // #nosec G404
now := time.Now().UnixNano()
t.Logf("seed: %d", now)
r := rand.New(rand.NewSource(now)) // #nosec G404
dbToSync, err := generateTrie(t, r, 3*maxKeyValuesLimit)
require.NoError(t, err)
syncRoot, err := dbToSync.GetMerkleRoot(context.Background())
Expand Down Expand Up @@ -793,7 +805,9 @@ func Test_Sync_Error_During_Sync(t *testing.T) {
require := require.New(t)
ctrl := gomock.NewController(t)
defer ctrl.Finish()
r := rand.New(rand.NewSource(int64(0))) // #nosec G404
now := time.Now().UnixNano()
t.Logf("seed: %d", now)
r := rand.New(rand.NewSource(now)) // #nosec G404

dbToSync, err := generateTrie(t, r, 100)
require.NoError(err)
Expand Down Expand Up @@ -846,7 +860,9 @@ func Test_Sync_Result_Correct_Root_Update_Root_During(t *testing.T) {
defer ctrl.Finish()

for i := 0; i < 3; i++ {
r := rand.New(rand.NewSource(int64(i))) // #nosec G404
now := time.Now().UnixNano()
t.Logf("seed: %d", now)
r := rand.New(rand.NewSource(now)) // #nosec G404

dbToSync, err := generateTrie(t, r, 3*maxKeyValuesLimit)
require.NoError(err)
Expand Down

0 comments on commit b157612

Please sign in to comment.