Skip to content

Commit

Permalink
fix: integration tests for cosmos v0.47 upgrade (#2227)
Browse files Browse the repository at this point in the history
Co-authored-by: Pablo Estrada <[email protected]>
  • Loading branch information
PJEstrada and Pablo Estrada authored Dec 9, 2024
1 parent fee4996 commit 3914107
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 42 deletions.
12 changes: 6 additions & 6 deletions types/math/dec.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ func NewNonNegativeDecFromString(s string) (Dec, error) {
return d, nil
}

func NewNonNegativeFixedDecFromString(s string, max uint32) (Dec, error) {
func NewNonNegativeFixedDecFromString(s string, maxNum uint32) (Dec, error) {
d, err := NewNonNegativeDecFromString(s)
if err != nil {
return Dec{}, err
}
if d.NumDecimalPlaces() > max {
return Dec{}, fmt.Errorf("%s exceeds maximum decimal places: %d", s, max)
if d.NumDecimalPlaces() > maxNum {
return Dec{}, fmt.Errorf("%s exceeds maximum decimal places: %d", s, maxNum)
}
return d, nil
}
Expand All @@ -102,13 +102,13 @@ func NewPositiveDecFromString(s string) (Dec, error) {
return d, nil
}

func NewPositiveFixedDecFromString(s string, max uint32) (Dec, error) {
func NewPositiveFixedDecFromString(s string, maxNum uint32) (Dec, error) {
d, err := NewPositiveDecFromString(s)
if err != nil {
return Dec{}, err
}
if d.NumDecimalPlaces() > max {
return Dec{}, fmt.Errorf("%s exceeds maximum decimal places: %d", s, max)
if d.NumDecimalPlaces() > maxNum {
return Dec{}, fmt.Errorf("%s exceeds maximum decimal places: %d", s, maxNum)
}
return d, nil
}
Expand Down
23 changes: 20 additions & 3 deletions types/math/dec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package math

import (
"fmt"
"math"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -479,7 +480,11 @@ func testMulQuoExact(t *rapid.T) {
b := rapid.Uint32Range(0, 32).Draw(t, "b")
decPrec := func(d Dec) bool { return d.NumDecimalPlaces() <= b }
a := genDec.Filter(decPrec).Draw(t, "a")

var err error
if b > math.MaxInt32 {
err = fmt.Errorf("int32 overflow error")
}
require.NoError(t, err)
c := NewDecFinite(1, int32(b))

d, err := a.MulExact(c)
Expand All @@ -498,6 +503,11 @@ func testQuoMulExact(t *rapid.T) {
aDec, err := NewDecFromString(fmt.Sprintf("%d", a))
require.NoError(t, err)
b := rapid.Uint32Range(0, 32).Draw(t, "b")
// Check for overflow
if b > math.MaxInt32 {
err = fmt.Errorf("int32 overflow error")
}
require.NoError(t, err)
c := NewDecFinite(1, int32(b))

require.NoError(t, err)
Expand Down Expand Up @@ -595,6 +605,10 @@ func floatDecimalPlaces(t *rapid.T, f float64) uint32 {
if res <= 0 {
return 0
}
// Check for overflow
if res > math.MaxUint32 {
return math.MaxUint32
}

return uint32(res)
}
Expand All @@ -607,9 +621,12 @@ func TestIsFinite(t *testing.T) {

b, err := NewDecFromString("NaN")
require.EqualError(t, err, "not a number: invalid decimal string")
require.Equal(t, b, Dec{})
// empty decimal has finite form by default
b, err = NewDecFromString("")
require.True(t, b.IsFinite())
c, err := NewDecFromString("")
require.NoError(t, err)
require.True(t, c.IsFinite())
require.Equal(t, c, Dec{})
}

func TestReduce(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion types/math/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
package math

import (
"cosmossdk.io/errors"
"fmt"

"cosmossdk.io/errors"
"github.com/cockroachdb/apd/v2"
)

Expand Down
2 changes: 1 addition & 1 deletion types/ormstore/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package ormstore
import (
"testing"

"github.com/stretchr/testify/require"
dbm "github.com/cometbft/cometbft-db"
"github.com/stretchr/testify/require"

"github.com/cometbft/cometbft/libs/log"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
Expand Down
13 changes: 6 additions & 7 deletions types/ormutil/compatability.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,24 @@ import (
"google.golang.org/protobuf/proto"

queryv1beta1 "cosmossdk.io/api/cosmos/base/query/v1beta1"
queryapi "cosmossdk.io/api/cosmos/base/query/v1beta1"
"github.com/cosmos/cosmos-sdk/orm/model/ormlist"
"github.com/cosmos/cosmos-sdk/types/query"
)

// PageReqToCosmosApiLegacy is a temporal adapter for ORM v-alpha-*
func PageReqToCosmosApiLegacy(from *query.PageRequest) *queryapi.PageRequest {
// PageReqToCosmosAPILegacy is a temporal adapter for ORM v-alpha-*
func PageReqToCosmosAPILegacy(from *query.PageRequest) *queryv1beta1.PageRequest {
if from == nil {
return &queryapi.PageRequest{Limit: query.DefaultLimit}
return &queryv1beta1.PageRequest{Limit: query.DefaultLimit}
}
return &queryapi.PageRequest{
return &queryv1beta1.PageRequest{
Key: from.Key, Offset: from.Offset, Limit: from.Limit, CountTotal: from.CountTotal, Reverse: from.Reverse}
}

func PageReqToOrmPaginate(pg *query.PageRequest) ormlist.Option {
return ormlist.Paginate(PageReqToCosmosApiLegacy(pg))
return ormlist.Paginate(PageReqToCosmosAPILegacy(pg))
}

func PageResToCosmosTypes(from *queryapi.PageResponse) *query.PageResponse {
func PageResToCosmosTypes(from *queryv1beta1.PageResponse) *query.PageResponse {
if from == nil {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion types/testutil/fixture/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type router struct {
}

func (rtr *router) invoker(methodName string, writeCondition func(context.Context, string, sdk.Msg) error) (Invoker, error) {
return func(ctx context.Context, request interface{}, response interface{}, opts ...interface{}) error {
return func(ctx context.Context, request interface{}, response interface{}, _ ...interface{}) error {
req, ok := request.(proto.Message)
if !ok {
return fmt.Errorf("expected proto.Message, got %T for service method %s", request, methodName)
Expand Down
25 changes: 17 additions & 8 deletions x/data/client/testsuite/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func (s *IntegrationTestSuite) SetupSuite() {
s.T().Log("setting up integration test suite")

s.cfg.NumValidators = 2

nw, err := network.New(s.T(), s.T().TempDir(), s.cfg)
s.Require().NoError(err)
s.network = nw
Expand Down Expand Up @@ -100,7 +101,7 @@ func (s *IntegrationTestSuite) SetupSuite() {
),
)
s.Require().NoError(err)

s.Require().NoError(s.network.WaitForNextBlock())
_, err = cli.ExecTestCLICmd(s.val.ClientCtx, client.MsgAttestCmd(),
append(
[]string{
Expand All @@ -111,7 +112,7 @@ func (s *IntegrationTestSuite) SetupSuite() {
),
)
s.Require().NoError(err)

s.Require().NoError(s.network.WaitForNextBlock())
_, err = cli.ExecTestCLICmd(s.val.ClientCtx, client.MsgAttestCmd(),
append(
[]string{
Expand All @@ -122,6 +123,7 @@ func (s *IntegrationTestSuite) SetupSuite() {
),
)
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())
}

s.url = "https://foo.bar"
Expand Down Expand Up @@ -389,7 +391,10 @@ func (s *IntegrationTestSuite) TestRegisterResolverCmd() {
require.NoError(err)

filePath := testutil.WriteToNewTempFile(s.T(), string(bz)).Name()

// Fetch account sequence
accountRetriever := clientCtx.AccountRetriever
_, seq, err := accountRetriever.GetAccountNumberSequence(clientCtx, val.Address)
require.NoError(err)
testCases := []struct {
name string
args []string
Expand All @@ -413,10 +418,10 @@ func (s *IntegrationTestSuite) TestRegisterResolverCmd() {
},
{
"invalid resolver id",
[]string{fmt.Sprintf("%d", 12345), filePath},
false,
[]string{fmt.Sprintf("%s", "123a5"), filePath},
true,
"not found",
false,
"invalid resolver id",
},
{
"valid test",
Expand All @@ -429,9 +434,12 @@ func (s *IntegrationTestSuite) TestRegisterResolverCmd() {

cmd := client.MsgRegisterResolverCmd()
for _, tc := range testCases {
args := tc.args
s.Run(tc.name, func() {
args := append(args, commonFlags...)
// Increment sequence number for each transaction
args := tc.args
args = append(args, commonFlags...)
args = append(args, fmt.Sprintf("--%s=%d", flags.FlagSequence, seq))
seq++
res, err := cli.ExecTestCLICmd(clientCtx, cmd, args)
if tc.expErr {
require.Error(err)
Expand All @@ -443,6 +451,7 @@ func (s *IntegrationTestSuite) TestRegisterResolverCmd() {
require.NoError(err)
}
}
s.Require().NoError(s.network.WaitForNextBlock())
})
}
}
Expand Down
Loading

0 comments on commit 3914107

Please sign in to comment.