Skip to content

Commit

Permalink
Merge branch 'dev' into version-bump-v1.10.10
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph committed Sep 18, 2023
2 parents 933fa9d + 63271f8 commit ad65a52
Show file tree
Hide file tree
Showing 69 changed files with 774 additions and 573 deletions.
8 changes: 3 additions & 5 deletions codec/hierarchycodec/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ func TestMultipleTags(t *testing.T) {
}
}

func FuzzVectors(f *testing.F) {
for _, test := range codec.FuzzTests {
c := NewDefault()
test(c, f)
}
func FuzzStructUnmarshalHierarchyCodec(f *testing.F) {
c := NewDefault()
codec.FuzzStructUnmarshal(c, f)
}
8 changes: 3 additions & 5 deletions codec/linearcodec/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ func TestMultipleTags(t *testing.T) {
}
}

func FuzzVectors(f *testing.F) {
for _, test := range codec.FuzzTests {
c := NewDefault()
test(c, f)
}
func FuzzStructUnmarshalLinearCodec(f *testing.F) {
c := NewDefault()
codec.FuzzStructUnmarshal(c, f)
}
4 changes: 0 additions & 4 deletions codec/test_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ var (
MultipleTagsTests = []func(c GeneralCodec, t testing.TB){
TestMultipleTags,
}

FuzzTests = []func(c GeneralCodec, f *testing.F){
FuzzStructUnmarshal,
}
)

// The below structs and interfaces exist
Expand Down
16 changes: 10 additions & 6 deletions database/corruptabledb/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@ func TestInterface(t *testing.T) {
}
}

func FuzzInterface(f *testing.F) {
for _, test := range database.FuzzTests {
baseDB := memdb.New()
db := New(baseDB)
test(f, db)
}
func FuzzKeyValue(f *testing.F) {
baseDB := memdb.New()
db := New(baseDB)
database.FuzzKeyValue(f, db)
}

func FuzzNewIteratorWithPrefix(f *testing.F) {
baseDB := memdb.New()
db := New(baseDB)
database.FuzzNewIteratorWithPrefix(f, db)
}

// TestCorruption tests to make sure corruptabledb wrapper works as expected.
Expand Down
21 changes: 12 additions & 9 deletions database/encdb/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,18 @@ func TestInterface(t *testing.T) {
}
}

func FuzzInterface(f *testing.F) {
for _, test := range database.FuzzTests {
unencryptedDB := memdb.New()
db, err := New([]byte(testPassword), unencryptedDB)
if err != nil {
require.NoError(f, err)
}
test(f, db)
}
func FuzzKeyValue(f *testing.F) {
unencryptedDB := memdb.New()
db, err := New([]byte(testPassword), unencryptedDB)
require.NoError(f, err)
database.FuzzKeyValue(f, db)
}

func FuzzNewIteratorWithPrefix(f *testing.F) {
unencryptedDB := memdb.New()
db, err := New([]byte(testPassword), unencryptedDB)
require.NoError(f, err)
database.FuzzNewIteratorWithPrefix(f, db)
}

func BenchmarkInterface(b *testing.B) {
Expand Down
32 changes: 15 additions & 17 deletions database/leveldb/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,30 @@ func TestInterface(t *testing.T) {
db, err := New(folder, nil, logging.NoLog{}, "", prometheus.NewRegistry())
require.NoError(t, err)

defer db.Close()

test(t, db)

// The database may have been closed by the test, so we don't care if it
// errors here.
_ = db.Close()
}
}

func FuzzInterface(f *testing.F) {
for _, test := range database.FuzzTests {
folder := f.TempDir()
db, err := New(folder, nil, logging.NoLog{}, "", prometheus.NewRegistry())
if err != nil {
require.NoError(f, err)
}
func FuzzKeyValue(f *testing.F) {
folder := f.TempDir()
db, err := New(folder, nil, logging.NoLog{}, "", prometheus.NewRegistry())
require.NoError(f, err)

defer db.Close()

defer db.Close()
database.FuzzKeyValue(f, db)
}

test(f, db)
func FuzzNewIteratorWithPrefix(f *testing.F) {
folder := f.TempDir()
db, err := New(folder, nil, logging.NoLog{}, "", prometheus.NewRegistry())
require.NoError(f, err)

// The database may have been closed by the test, so we don't care if it
// errors here.
_ = db.Close()
}
defer db.Close()

database.FuzzNewIteratorWithPrefix(f, db)
}

func BenchmarkInterface(b *testing.B) {
Expand Down
10 changes: 6 additions & 4 deletions database/memdb/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ func TestInterface(t *testing.T) {
}
}

func FuzzInterface(f *testing.F) {
for _, test := range database.FuzzTests {
test(f, New())
}
func FuzzKeyValue(f *testing.F) {
database.FuzzKeyValue(f, New())
}

func FuzzNewIteratorWithPrefix(f *testing.F) {
database.FuzzNewIteratorWithPrefix(f, New())
}

func BenchmarkInterface(b *testing.B) {
Expand Down
21 changes: 12 additions & 9 deletions database/meterdb/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,18 @@ func TestInterface(t *testing.T) {
}
}

func FuzzInterface(f *testing.F) {
for _, test := range database.FuzzTests {
baseDB := memdb.New()
db, err := New("", prometheus.NewRegistry(), baseDB)
if err != nil {
require.NoError(f, err)
}
test(f, db)
}
func FuzzKeyValue(f *testing.F) {
baseDB := memdb.New()
db, err := New("", prometheus.NewRegistry(), baseDB)
require.NoError(f, err)
database.FuzzKeyValue(f, db)
}

func FuzzNewIteratorWithPrefix(f *testing.F) {
baseDB := memdb.New()
db, err := New("", prometheus.NewRegistry(), baseDB)
require.NoError(f, err)
database.FuzzNewIteratorWithPrefix(f, db)
}

func BenchmarkInterface(b *testing.B) {
Expand Down
10 changes: 6 additions & 4 deletions database/prefixdb/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ func TestInterface(t *testing.T) {
}
}

func FuzzInterface(f *testing.F) {
for _, test := range database.FuzzTests {
test(f, New([]byte(""), memdb.New()))
}
func FuzzKeyValue(f *testing.F) {
database.FuzzKeyValue(f, New([]byte(""), memdb.New()))
}

func FuzzNewIteratorWithPrefix(f *testing.F) {
database.FuzzNewIteratorWithPrefix(f, New([]byte(""), memdb.New()))
}

func BenchmarkInterface(b *testing.B) {
Expand Down
17 changes: 11 additions & 6 deletions database/rpcdb/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,18 @@ func TestInterface(t *testing.T) {
}
}

func FuzzInterface(f *testing.F) {
for _, test := range database.FuzzTests {
db := setupDB(f)
test(f, db.client)
func FuzzKeyValue(f *testing.F) {
db := setupDB(f)
database.FuzzKeyValue(f, db.client)

db.closeFn()
}
db.closeFn()
}

func FuzzNewIteratorWithPrefix(f *testing.F) {
db := setupDB(f)
database.FuzzNewIteratorWithPrefix(f, db.client)

db.closeFn()
}

func BenchmarkInterface(b *testing.B) {
Expand Down
69 changes: 65 additions & 4 deletions database/test_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
"bytes"
"io"
"math"
"math/rand"
"testing"

"github.com/stretchr/testify/require"

"go.uber.org/mock/gomock"

"golang.org/x/exp/maps"
"golang.org/x/exp/slices"

"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -62,10 +64,6 @@ var Tests = []func(t *testing.T, db Database){
TestPutGetEmpty,
}

var FuzzTests = []func(*testing.F, Database){
FuzzKeyValue,
}

// TestSimpleKeyValue tests to make sure that simple Put + Get + Delete + Has
// calls return the expected values.
func TestSimpleKeyValue(t *testing.T, db Database) {
Expand Down Expand Up @@ -1203,3 +1201,66 @@ func FuzzKeyValue(f *testing.F, db Database) {
require.Equal(ErrNotFound, err)
})
}

func FuzzNewIteratorWithPrefix(f *testing.F, db Database) {
const (
maxKeyLen = 32
maxValueLen = 32
)

f.Fuzz(func(
t *testing.T,
randSeed int64,
prefix []byte,
numKeyValues uint,
) {
require := require.New(t)
r := rand.New(rand.NewSource(randSeed)) // #nosec G404

// Put a bunch of key-values
expected := map[string][]byte{}
for i := 0; i < int(numKeyValues); i++ {
key := make([]byte, r.Intn(maxKeyLen))
_, _ = r.Read(key) // #nosec G404

value := make([]byte, r.Intn(maxValueLen))
_, _ = r.Read(value) // #nosec G404

if len(value) == 0 {
// Consistently treat zero length values as nil
// so that we can compare [expected] and [got] with
// require.Equal, which treats nil and empty byte
// as being unequal, whereas the database treats
// them as being equal.
value = nil
}

if bytes.HasPrefix(key, prefix) {
expected[string(key)] = value
}

require.NoError(db.Put(key, value))
}
expectedList := maps.Keys(expected)
slices.Sort(expectedList)

iter := db.NewIteratorWithPrefix(prefix)
defer iter.Release()

// Assert the iterator returns the expected key-values.
numIterElts := 0
for iter.Next() {
val := iter.Value()
if len(val) == 0 {
val = nil
}
require.Equal(expectedList[numIterElts], string(iter.Key()))
require.Equal(expected[string(iter.Key())], val)
numIterElts++
}
require.Equal(len(expectedList), numIterElts)

// Clear the database for the next fuzz iteration.
require.NoError(AtomicClear(db, db))
})
}
11 changes: 6 additions & 5 deletions database/versiondb/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ func TestInterface(t *testing.T) {
}
}

func FuzzInterface(f *testing.F) {
for _, test := range database.FuzzTests {
baseDB := memdb.New()
test(f, New(baseDB))
}
func FuzzKeyValue(f *testing.F) {
database.FuzzKeyValue(f, New(memdb.New()))
}

func FuzzNewIteratorWithPrefix(f *testing.F) {
database.FuzzNewIteratorWithPrefix(f, New(memdb.New()))
}

func TestIterate(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion scripts/mocks.mockgen.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ github.com/ava-labs/avalanchego/utils/filesystem=Reader=utils/filesystem/mock_io
github.com/ava-labs/avalanchego/utils/hashing=Hasher=utils/hashing/mock_hasher.go
github.com/ava-labs/avalanchego/utils/logging=Logger=utils/logging/mock_logger.go
github.com/ava-labs/avalanchego/utils/resource=User=utils/resource/mock_user.go
github.com/ava-labs/avalanchego/vms/avm/blocks=Block=vms/avm/blocks/mock_block.go
github.com/ava-labs/avalanchego/vms/avm/block=Block=vms/avm/block/mock_block.go
github.com/ava-labs/avalanchego/vms/avm/metrics=Metrics=vms/avm/metrics/mock_metrics.go
github.com/ava-labs/avalanchego/vms/avm/states=Chain,State,Diff=vms/avm/states/mock_states.go
github.com/ava-labs/avalanchego/vms/avm/txs/mempool=Mempool=vms/avm/txs/mempool/mock_mempool.go
Expand Down
20 changes: 16 additions & 4 deletions snow/consensus/snowman/poll/early_term_no_traversal.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,26 @@ func (p *earlyTermNoTraversalPoll) Drop(vdr ids.NodeID) {
p.polled.Remove(vdr)
}

// Finished returns true when all validators have voted
// Finished returns true when one of the following conditions is met.
//
// 1. There are no outstanding votes.
// 2. It is impossible for the poll to achieve an alpha majority after applying
// transitive voting.
// 3. A single element has achieved an alpha majority.
func (p *earlyTermNoTraversalPoll) Finished() bool {
remaining := p.polled.Len()
if remaining == 0 {
return true // Case 1
}

received := p.votes.Len()
maxPossibleVotes := received + remaining
if maxPossibleVotes < p.alpha {
return true // Case 2
}

_, freq := p.votes.Mode()
return remaining == 0 || // All k nodes responded
freq >= p.alpha || // An alpha majority has returned
received+remaining < p.alpha // An alpha majority can never return
return freq >= p.alpha // Case 3
}

// Result returns the result of this poll
Expand Down
Loading

0 comments on commit ad65a52

Please sign in to comment.