Skip to content
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

Improve secp256k1fx + merkledb error handling #1402

Merged
merged 5 commits into from
Apr 22, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions snow/choices/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ func TestStatusValid(t *testing.T) {
require.NoError(Rejected.Valid())
require.NoError(Processing.Valid())
require.NoError(Unknown.Valid())

require.Error(Status(math.MaxInt32).Valid())
require.ErrorIs(Status(math.MaxInt32).Valid(), errUnknownStatus)
}

func TestStatusDecided(t *testing.T) {
Expand All @@ -28,7 +27,6 @@ func TestStatusDecided(t *testing.T) {
require.True(Rejected.Decided())
require.False(Processing.Decided())
require.False(Unknown.Decided())

require.False(Status(math.MaxInt32).Decided())
}

Expand All @@ -39,7 +37,6 @@ func TestStatusFetched(t *testing.T) {
require.True(Rejected.Fetched())
require.True(Processing.Fetched())
require.False(Unknown.Fetched())

require.False(Status(math.MaxInt32).Fetched())
}

Expand All @@ -50,6 +47,5 @@ func TestStatusString(t *testing.T) {
require.Equal("Rejected", Rejected.String())
require.Equal("Processing", Processing.String())
require.Equal("Unknown", Unknown.String())

require.Equal("Invalid status", Status(math.MaxInt32).String())
}
18 changes: 11 additions & 7 deletions snow/engine/avalanche/vertex/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (
"github.com/ava-labs/avalanchego/ids"
)

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

chainID := ids.ID{1}
height := uint64(2)
parentIDs := []ids.ID{{4}, {5}}
Expand All @@ -22,10 +24,12 @@ func TestBuildInvalid(t *testing.T) {
parentIDs,
txs,
)
require.Error(t, err, "build should have errored because restrictions were provided in epoch 0")
require.ErrorIs(err, errInvalidTxs)
}

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

chainID := ids.ID{1}
height := uint64(2)
parentIDs := []ids.ID{{4}, {5}}
Expand All @@ -36,9 +40,9 @@ func TestBuildValid(t *testing.T) {
parentIDs,
txs,
)
require.NoError(t, err)
require.Equal(t, chainID, vtx.ChainID())
require.Equal(t, height, vtx.Height())
require.Equal(t, parentIDs, vtx.ParentIDs())
require.Equal(t, txs, vtx.Txs())
require.NoError(err)
require.Equal(chainID, vtx.ChainID())
require.Equal(height, vtx.Height())
require.Equal(parentIDs, vtx.ParentIDs())
require.Equal(txs, vtx.Txs())
}
5 changes: 3 additions & 2 deletions snow/engine/avalanche/vertex/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import (

"github.com/stretchr/testify/require"

"github.com/ava-labs/avalanchego/codec"
"github.com/ava-labs/avalanchego/ids"
)

func TestParseInvalid(t *testing.T) {
vtxBytes := []byte{}
vtxBytes := []byte{1, 2, 3, 4, 5}
_, err := Parse(vtxBytes)
require.Error(t, err, "parse on an invalid vertex should have errored")
require.ErrorIs(t, err, codec.ErrUnknownVersion)
}

func TestParseValid(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion snow/engine/common/queue/jobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ func TestHandleJobWithMissingDependencyOnRunnableStack(t *testing.T) {
_, err = jobs.ExecuteAll(context.Background(), snow.DefaultConsensusContextTest(), &common.Halter{}, false)
// Assert that the database closed error on job1 causes ExecuteAll
// to fail in the middle of execution.
require.Error(err)
require.ErrorIs(err, database.ErrClosed)
require.True(executed0)
require.False(executed1)

Expand Down
13 changes: 7 additions & 6 deletions snow/uptime/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/stretchr/testify/require"

"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
)

Expand Down Expand Up @@ -56,7 +57,7 @@ func TestStartTrackingDBError(t *testing.T) {
up.clock.Set(currentTime)

err := up.StartTracking([]ids.NodeID{nodeID0}, subnetID)
require.Error(err)
require.ErrorIs(err, errTest)
}

func TestStartTrackingNonValidator(t *testing.T) {
Expand All @@ -69,7 +70,7 @@ func TestStartTrackingNonValidator(t *testing.T) {
subnetID := ids.GenerateTestID()

err := up.StartTracking([]ids.NodeID{nodeID0}, subnetID)
require.Error(err)
require.ErrorIs(err, database.ErrNotFound)
}

func TestStartTrackingInThePast(t *testing.T) {
Expand Down Expand Up @@ -182,7 +183,7 @@ func TestStopTrackingDisconnectedNonValidator(t *testing.T) {
require.NoError(err)

err = up.StopTracking([]ids.NodeID{nodeID0}, subnetID)
require.Error(err)
require.ErrorIs(err, database.ErrNotFound)
}

func TestStopTrackingConnectedDBError(t *testing.T) {
Expand All @@ -204,7 +205,7 @@ func TestStopTrackingConnectedDBError(t *testing.T) {

s.dbReadError = errTest
err = up.StopTracking([]ids.NodeID{nodeID0}, subnetID)
require.Error(err)
require.ErrorIs(err, errTest)
}

func TestStopTrackingNonConnectedPast(t *testing.T) {
Expand Down Expand Up @@ -256,7 +257,7 @@ func TestStopTrackingNonConnectedDBError(t *testing.T) {

s.dbWriteError = errTest
err = up.StopTracking([]ids.NodeID{nodeID0}, subnetID)
require.Error(err)
require.ErrorIs(err, errTest)
}

func TestConnectAndDisconnect(t *testing.T) {
Expand Down Expand Up @@ -552,7 +553,7 @@ func TestCalculateUptimeNonValidator(t *testing.T) {
up := NewManager(s).(*manager)

_, err := up.CalculateUptimePercentFrom(nodeID0, subnetID, startTime)
require.Error(err)
require.ErrorIs(err, database.ErrNotFound)
}

func TestCalculateUptimePercentageDivBy0(t *testing.T) {
Expand Down
12 changes: 8 additions & 4 deletions snow/validators/gvalidators/validator_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ func TestGetMinimumHeight(t *testing.T) {
state.server.EXPECT().GetMinimumHeight(gomock.Any()).Return(expectedHeight, errCustom)

_, err = state.client.GetMinimumHeight(context.Background())
require.Error(err)
// TODO: require specific error
require.Error(err) //nolint:forbidigo // currently returns grpc error
}

func TestGetCurrentHeight(t *testing.T) {
Expand All @@ -105,7 +106,8 @@ func TestGetCurrentHeight(t *testing.T) {
state.server.EXPECT().GetCurrentHeight(gomock.Any()).Return(expectedHeight, errCustom)

_, err = state.client.GetCurrentHeight(context.Background())
require.Error(err)
// TODO: require specific error
require.Error(err) //nolint:forbidigo // currently returns grpc error
}

func TestGetSubnetID(t *testing.T) {
Expand All @@ -129,7 +131,8 @@ func TestGetSubnetID(t *testing.T) {
state.server.EXPECT().GetSubnetID(gomock.Any(), chainID).Return(expectedSubnetID, errCustom)

_, err = state.client.GetSubnetID(context.Background(), chainID)
require.Error(err)
// TODO: require specific error
require.Error(err) //nolint:forbidigo // currently returns grpc error
}

func TestGetValidatorSet(t *testing.T) {
Expand Down Expand Up @@ -180,7 +183,8 @@ func TestGetValidatorSet(t *testing.T) {
state.server.EXPECT().GetValidatorSet(gomock.Any(), height, subnetID).Return(expectedVdrs, errCustom)

_, err = state.client.GetValidatorSet(context.Background(), height, subnetID)
require.Error(err)
// TODO: require specific error
require.Error(err) //nolint:forbidigo // currently returns grpc error
}

func TestPublicKeyDeserialize(t *testing.T) {
Expand Down
21 changes: 12 additions & 9 deletions snow/validators/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
package validators

import (
"math"
"testing"

stdmath "math"

"github.com/stretchr/testify/require"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/crypto/bls"
"github.com/ava-labs/avalanchego/utils/math"
"github.com/ava-labs/avalanchego/utils/sampler"
"github.com/ava-labs/avalanchego/utils/set"
)

Expand Down Expand Up @@ -42,8 +45,8 @@ func TestSetAddOverflow(t *testing.T) {
err := s.Add(ids.GenerateTestNodeID(), nil, ids.Empty, 1)
require.NoError(err)

err = s.Add(ids.GenerateTestNodeID(), nil, ids.Empty, math.MaxUint64)
require.Error(err)
err = s.Add(ids.GenerateTestNodeID(), nil, ids.Empty, stdmath.MaxUint64)
require.ErrorIs(err, math.ErrOverflow)

weight := s.Weight()
require.EqualValues(1, weight)
Expand Down Expand Up @@ -74,8 +77,8 @@ func TestSetAddWeightOverflow(t *testing.T) {
err = s.Add(nodeID, nil, ids.Empty, 1)
require.NoError(err)

err = s.AddWeight(nodeID, math.MaxUint64-1)
require.Error(err)
err = s.AddWeight(nodeID, stdmath.MaxUint64-1)
require.ErrorIs(err, math.ErrOverflow)

weight := s.Weight()
require.EqualValues(2, weight)
Expand Down Expand Up @@ -166,7 +169,7 @@ func TestSetRemoveWeightUnderflow(t *testing.T) {
require.NoError(err)

err = s.RemoveWeight(nodeID, 2)
require.Error(err)
require.ErrorIs(err, math.ErrUnderflow)

weight := s.Weight()
require.EqualValues(2, weight)
Expand Down Expand Up @@ -384,10 +387,10 @@ func TestSetSample(t *testing.T) {
require.Equal([]ids.NodeID{nodeID0}, sampled)

_, err = s.Sample(2)
require.Error(err)
require.ErrorIs(err, sampler.ErrOutOfRange)

nodeID1 := ids.GenerateTestNodeID()
err = s.Add(nodeID1, nil, ids.Empty, math.MaxInt64-1)
err = s.Add(nodeID1, nil, ids.Empty, stdmath.MaxInt64-1)
require.NoError(err)

sampled, err = s.Sample(1)
Expand Down Expand Up @@ -416,7 +419,7 @@ func TestSetString(t *testing.T) {
err := s.Add(nodeID0, nil, ids.Empty, 1)
require.NoError(err)

err = s.Add(nodeID1, nil, ids.Empty, math.MaxInt64-1)
err = s.Add(nodeID1, nil, ids.Empty, stdmath.MaxInt64-1)
require.NoError(err)

expected := "Validator Set: (Size = 2, Weight = 9223372036854775807)\n" +
Expand Down
2 changes: 1 addition & 1 deletion utils/buffer/bounded_nonblocking_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestNewBoundedQueue(t *testing.T) {

// Case: maxSize < 1
_, err := NewBoundedQueue[bool](0, nil)
require.Error(err)
require.ErrorIs(err, errInvalidMaxSize)

// Case: maxSize == 1 and nil onEvict
b, err := NewBoundedQueue[bool](1, nil)
Expand Down
12 changes: 6 additions & 6 deletions utils/cb58/cb58.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ package cb58
import (
"bytes"
"errors"
"fmt"
"math"

"github.com/mr-tron/base58/base58"

"github.com/ava-labs/avalanchego/utils/hashing"
)

const (
checksumLen = 4
)
const checksumLen = 4

var (
ErrBase58Decoding = errors.New("base58 decoding error")
ErrMissingChecksum = errors.New("input string is smaller than the checksum size")
errEncodingOverFlow = errors.New("encoding overflow")
errMissingChecksum = errors.New("input string is smaller than the checksum size")
errBadChecksum = errors.New("invalid input checksum")
)

Expand All @@ -41,10 +41,10 @@ func Encode(bytes []byte) (string, error) {
func Decode(str string) ([]byte, error) {
decodedBytes, err := base58.Decode(str)
if err != nil {
return nil, err
return nil, fmt.Errorf("%w: %s", ErrBase58Decoding, err)
}
if len(decodedBytes) < checksumLen {
return nil, errMissingChecksum
return nil, ErrMissingChecksum
}
// Verify the checksum
rawBytes := decodedBytes[:len(decodedBytes)-checksumLen]
Expand Down
14 changes: 8 additions & 6 deletions utils/compression/compressor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,21 +114,23 @@ func TestSizeLimiting(t *testing.T) {
continue
}
t.Run(compressionType.String(), func(t *testing.T) {
require := require.New(t)

compressor, err := compressorFunc(maxMessageSize)
require.NoError(t, err)
require.NoError(err)

data := make([]byte, maxMessageSize+1)
_, err = compressor.Compress(data) // should be too large
require.Error(t, err)
require.ErrorIs(err, ErrMsgTooLarge)

compressor2, err := compressorFunc(2 * maxMessageSize)
require.NoError(t, err)
require.NoError(err)

dataCompressed, err := compressor2.Compress(data)
require.NoError(t, err)
require.NoError(err)

_, err = compressor.Decompress(dataCompressed) // should be too large
require.Error(t, err)
require.ErrorIs(err, ErrDecompressedMsgTooLarge)
})
}
}
Expand Down Expand Up @@ -178,7 +180,7 @@ func fuzzHelper(f *testing.F, compressionType Type) {

if len(data) > maxMessageSize {
_, err := compressor.Compress(data)
require.Error(err)
require.ErrorIs(err, ErrMsgTooLarge)
}

compressed, err := compressor.Compress(data)
Expand Down
8 changes: 5 additions & 3 deletions utils/compression/type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ import (
)

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

for _, compressionType := range []Type{TypeNone, TypeGzip, TypeZstd} {
s := compressionType.String()
parsedType, err := TypeFromString(s)
require.NoError(t, err)
require.Equal(t, compressionType, parsedType)
require.NoError(err)
require.Equal(compressionType, parsedType)
}

_, err := TypeFromString("unknown")
require.Error(t, err)
require.ErrorIs(err, errUnknownCompressionType)
}

func TestTypeMarshalJSON(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions utils/crypto/bls/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const PublicKeyLen = blst.BLST_P1_COMPRESS_BYTES

var (
ErrNoPublicKeys = errors.New("no public keys")
errFailedPublicKeyDecompress = errors.New("couldn't decompress public key")
ErrFailedPublicKeyDecompress = errors.New("couldn't decompress public key")
errInvalidPublicKey = errors.New("invalid public key")
errFailedPublicKeyAggregation = errors.New("couldn't aggregate public keys")
)
Expand All @@ -33,7 +33,7 @@ func PublicKeyToBytes(pk *PublicKey) []byte {
func PublicKeyFromBytes(pkBytes []byte) (*PublicKey, error) {
pk := new(PublicKey).Uncompress(pkBytes)
if pk == nil {
return nil, errFailedPublicKeyDecompress
return nil, ErrFailedPublicKeyDecompress
}
if !pk.KeyValidate() {
return nil, errInvalidPublicKey
Expand Down
Loading