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

refactor: deterministic tests and add gas tests #13539

Merged
merged 21 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
4e1fd4f
refactor: x/auth deterministic tests
atheeshp Oct 13, 2022
844a9c9
changes
atheeshp Oct 14, 2022
1a8bbee
Merge branch 'main' of github.com:cosmos/cosmos-sdk into ap/auth-dete…
atheeshp Oct 14, 2022
b499d09
x/bank refactor
atheeshp Oct 14, 2022
2d01ef1
Merge branch 'main' of github.com:cosmos/cosmos-sdk into ap/auth-dete…
atheeshp Oct 14, 2022
9f62f50
changes
atheeshp Oct 18, 2022
5534539
review changes
atheeshp Oct 18, 2022
0896820
Merge branch 'main' of github.com:cosmos/cosmos-sdk into ap/auth-dete…
atheeshp Oct 19, 2022
a358ede
refactor `x/staking`
atheeshp Oct 19, 2022
7878a4b
fix tests
atheeshp Oct 19, 2022
bc50c7b
fix tests
atheeshp Oct 19, 2022
0a50d87
fix tests
atheeshp Oct 19, 2022
df974a4
Merge branch 'main' of github.com:cosmos/cosmos-sdk into ap/auth-dete…
atheeshp Oct 19, 2022
efeb909
review changes
atheeshp Oct 19, 2022
61bbc34
Merge branch 'main' of github.com:cosmos/cosmos-sdk into ap/auth-dete…
atheeshp Oct 19, 2022
86ee95e
Merge branch 'main' into ap/auth-deterministic-refactor
atheeshp Oct 21, 2022
2378b50
Merge branch 'main' into ap/auth-deterministic-refactor
alexanderbez Oct 21, 2022
96dde32
Update testutil/testdata/grpc_query.go
amaury1093 Oct 24, 2022
60e3725
Update testutil/testdata/grpc_query.go
amaury1093 Oct 24, 2022
773778d
Merge branch 'main' of github.com:cosmos/cosmos-sdk into ap/auth-dete…
atheeshp Oct 25, 2022
672a9d7
Merge branch 'ap/auth-deterministic-refactor' of github.com:cosmos/co…
atheeshp Oct 25, 2022
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
9 changes: 4 additions & 5 deletions tests/integration/bank/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (
"github.com/cosmos/cosmos-sdk/x/bank/keeper"
"github.com/cosmos/cosmos-sdk/x/bank/testutil"
"github.com/cosmos/cosmos-sdk/x/bank/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
)
Expand Down Expand Up @@ -1616,11 +1615,11 @@ func (suite *IntegrationTestSuite) TestGetAllSendEnabledEntries() {
}

type mockSubspace struct {
ps banktypes.Params
ps types.Params
}

func (ms mockSubspace) GetParamSet(ctx sdk.Context, ps exported.ParamSet) {
*ps.(*banktypes.Params) = ms.ps
*ps.(*types.Params) = ms.ps
}

func (suite *IntegrationTestSuite) TestMigrator_Migrate3to4() {
Expand All @@ -1632,7 +1631,7 @@ func (suite *IntegrationTestSuite) TestMigrator_Migrate3to4() {
suite.T().Run(fmt.Sprintf("default %t does not change", def), func(t *testing.T) {
legacySubspace := func(ps types.Params) mockSubspace {
return mockSubspace{ps: ps}
}(banktypes.NewParams(def))
}(types.NewParams(def))
migrator := keeper.NewMigrator(bankKeeper, legacySubspace)
require.NoError(t, migrator.Migrate3to4(ctx))
actual := bankKeeper.GetParams(ctx)
Expand All @@ -1651,7 +1650,7 @@ func (suite *IntegrationTestSuite) TestMigrator_Migrate3to4() {
suite.T().Run(fmt.Sprintf("default %t send enabled info moved to store", def), func(t *testing.T) {
legacySubspace := func(ps types.Params) mockSubspace {
return mockSubspace{ps: ps}
}(banktypes.NewParams(def))
}(types.NewParams(def))
migrator := keeper.NewMigrator(bankKeeper, legacySubspace)
require.NoError(t, migrator.Migrate3to4(ctx))
newParams := bankKeeper.GetParams(ctx)
Expand Down
74 changes: 42 additions & 32 deletions x/auth/keeper/deterministic_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package keeper_test

import (
"context"
"encoding/hex"
"sort"
"testing"

"github.com/stretchr/testify/suite"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"google.golang.org/grpc"
"pgregory.net/rapid"

"github.com/cosmos/cosmos-sdk/baseapp"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
"github.com/cosmos/cosmos-sdk/testutil"
Expand Down Expand Up @@ -76,6 +77,30 @@ func (suite *DeterministicTestSuite) SetupTest() {
suite.maccPerms = maccPerms
}

type QueryRequest interface {
*types.QueryAccountRequest | *types.QueryAccountsRequest
}

type QueryResponse interface {
*types.QueryAccountResponse | *types.QueryAccountsResponse
}

func queryReq[request QueryRequest, response QueryResponse](
atheeshp marked this conversation as resolved.
Show resolved Hide resolved
suite *DeterministicTestSuite,
req request, prevRes response,
grpcFn func(context.Context, request, ...grpc.CallOption) (response, error),
gasConsumed uint64,
) {

for i := 0; i < iterCount; i++ {
before := suite.ctx.GasMeter().GasConsumed()
res, err := grpcFn(suite.ctx, req)
suite.Require().Equal(suite.ctx.GasMeter().GasConsumed()-before, gasConsumed)
suite.Require().NoError(err)
suite.Require().Equal(res, prevRes)
}
}

// createAndSetAccount creates a random account and sets to the keeper store.
func (suite *DeterministicTestSuite) createAndSetAccounts(t *rapid.T, count int) []types.AccountI {
accs := make([]types.AccountI, 0, count)
Expand All @@ -97,22 +122,16 @@ func (suite *DeterministicTestSuite) createAndSetAccounts(t *rapid.T, count int)
return accs
}

func (suite *DeterministicTestSuite) runAccountIterations(addr sdk.AccAddress, prevRes *codectypes.Any) {
for i := 0; i < iterCount; i++ {
acc, err := suite.queryClient.Account(suite.ctx, &types.QueryAccountRequest{Address: addr.String()})
suite.Require().NoError(err)
suite.Require().NotNil(acc)
suite.Require().Equal(acc.Account, prevRes)
}
}

func (suite *DeterministicTestSuite) TestGRPCQueryAccount() {
rapid.Check(suite.T(), func(t *rapid.T) {
accs := suite.createAndSetAccounts(t, 1)
suite.Require().Len(accs, 1)
any, err := codectypes.NewAnyWithValue(accs[0])

req := &types.QueryAccountRequest{Address: accs[0].GetAddress().String()}
before := suite.ctx.GasMeter().GasConsumed()
res, err := suite.queryClient.Account(suite.ctx, req)
suite.Require().NoError(err)
suite.runAccountIterations(accs[0].GetAddress(), any)

queryReq(suite, req, res, suite.queryClient.Account, suite.ctx.GasMeter().GasConsumed()-before)
})

// Regression tests
Expand All @@ -122,10 +141,11 @@ func (suite *DeterministicTestSuite) TestGRPCQueryAccount() {
acc1 := types.NewBaseAccount(addr, &secp256k1.PubKey{Key: pub}, accNum, seq)
suite.accountKeeper.SetAccount(suite.ctx, acc1)

any, err := codectypes.NewAnyWithValue(acc1)
req := &types.QueryAccountRequest{Address: acc1.GetAddress().String()}
res, err := suite.queryClient.Account(suite.ctx, req)
suite.Require().NoError(err)

suite.runAccountIterations(addr, any)
queryReq(suite, req, res, suite.queryClient.Account, 1543)
}

// pubkeyGenerator creates and returns a random pubkey generator using rapid.
Expand All @@ -136,30 +156,20 @@ func pubkeyGenerator(t *rapid.T) *rapid.Generator[secp256k1.PubKey] {
})
}

func (suite *DeterministicTestSuite) runAccountsIterations(req *types.QueryAccountsRequest, prevRes *types.QueryAccountsResponse) {
for i := 0; i < iterCount; i++ {
res, err := suite.queryClient.Accounts(suite.ctx, req)
suite.Require().NoError(err)
suite.Require().NotNil(res)

suite.Require().Len(res.GetAccounts(), len(prevRes.GetAccounts()))
suite.Require().Equal(res, prevRes)
}
}

func (suite *DeterministicTestSuite) TestGRPCQueryAccounts() {
rapid.Check(suite.T(), func(t *rapid.T) {
numAccs := rapid.IntRange(1, 10).Draw(t, "accounts")
accs := suite.createAndSetAccounts(t, numAccs)

req := types.QueryAccountsRequest{
req := &types.QueryAccountsRequest{
Pagination: testdata.PaginationGenerator(t, uint64(numAccs)).Draw(t, "accounts"),
}

res, err := suite.queryClient.Accounts(suite.ctx, &req)
before := suite.ctx.GasMeter().GasConsumed()
res, err := suite.queryClient.Accounts(suite.ctx, req)
suite.Require().NoError(err)

suite.runAccountsIterations(&req, res)
queryReq(suite, req, res, suite.queryClient.Accounts, suite.ctx.GasMeter().GasConsumed()-before)

for i := 0; i < numAccs; i++ {
suite.accountKeeper.RemoveAccount(suite.ctx, accs[i])
Expand All @@ -183,11 +193,11 @@ func (suite *DeterministicTestSuite) TestGRPCQueryAccounts() {
suite.accountKeeper.SetAccount(suite.ctx, acc1)
suite.accountKeeper.SetAccount(suite.ctx, acc2)

req := types.QueryAccountsRequest{}
res, err := suite.queryClient.Accounts(suite.ctx, &req)
req := &types.QueryAccountsRequest{}
res, err := suite.queryClient.Accounts(suite.ctx, req)
suite.Require().NoError(err)

suite.runAccountsIterations(&req, res)
queryReq(suite, req, res, suite.queryClient.Accounts, 1716)
}

func (suite *DeterministicTestSuite) runAccountAddressByIDIterations(id int64, prevRes string) {
Expand Down