Skip to content

Commit

Permalink
chore(sims): move sims with depinject to tests (#19121)
Browse files Browse the repository at this point in the history
  • Loading branch information
tac0turtle authored Jan 23, 2024
1 parent 9853db0 commit 7f0f405
Show file tree
Hide file tree
Showing 70 changed files with 314 additions and 580 deletions.
2 changes: 0 additions & 2 deletions client/v2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6r
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816 h1:41iFGWnSlI2gVpmOtVTJZNodLdLQLn/KsJqFvXwnd/s=
github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bits-and-blooms/bitset v1.10.0 h1:ePXTeiPEazB5+opbv5fr8umg2R/1NlzgDsyepwsSr88=
github.com/bits-and-blooms/bitset v1.10.0/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8=
github.com/btcsuite/btcd/btcec/v2 v2.3.2 h1:5n0X6hX0Zk+6omWcihdYvdAlGf2DfasC0GMf7DClJ3U=
github.com/btcsuite/btcd/btcec/v2 v2.3.2/go.mod h1:zYzJ8etWJQIv1Ogk7OzpWjowwOdXY1W/17j2MW85J04=
github.com/btcsuite/btcd/btcutil v1.1.3 h1:xfbtw8lwpp0G6NwSHb+UE67ryTFHJAiNuipusjXSohQ=
Expand Down
4 changes: 0 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,6 @@ replace (
cosmossdk.io/depinject => ./depinject
cosmossdk.io/x/auth => ./x/auth
cosmossdk.io/x/bank => ./x/bank
cosmossdk.io/x/distribution => ./x/distribution
cosmossdk.io/x/mint => ./x/mint
cosmossdk.io/x/protocolpool => ./x/protocolpool
cosmossdk.io/x/slashing => ./x/slashing
cosmossdk.io/x/staking => ./x/staking
)

Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6r
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816 h1:41iFGWnSlI2gVpmOtVTJZNodLdLQLn/KsJqFvXwnd/s=
github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bits-and-blooms/bitset v1.10.0 h1:ePXTeiPEazB5+opbv5fr8umg2R/1NlzgDsyepwsSr88=
github.com/bits-and-blooms/bitset v1.10.0/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8=
github.com/btcsuite/btcd/btcec/v2 v2.3.2 h1:5n0X6hX0Zk+6omWcihdYvdAlGf2DfasC0GMf7DClJ3U=
github.com/btcsuite/btcd/btcec/v2 v2.3.2/go.mod h1:zYzJ8etWJQIv1Ogk7OzpWjowwOdXY1W/17j2MW85J04=
github.com/btcsuite/btcd/btcutil v1.1.3 h1:xfbtw8lwpp0G6NwSHb+UE67ryTFHJAiNuipusjXSohQ=
Expand Down
27 changes: 27 additions & 0 deletions tests/integration/distribution/appconfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package distribution_test

import (
_ "cosmossdk.io/x/auth" // import as blank for app wiring
_ "cosmossdk.io/x/auth/tx/config" // import as blank for app wiring
_ "cosmossdk.io/x/bank" // import as blank for app wiring
_ "cosmossdk.io/x/distribution" // import as blank for app wiring
_ "cosmossdk.io/x/mint" // import as blank for app wiring
_ "cosmossdk.io/x/protocolpool" // import as blank for app wiring
_ "cosmossdk.io/x/staking" // import as blank for app wiring

"github.com/cosmos/cosmos-sdk/testutil/configurator"
_ "github.com/cosmos/cosmos-sdk/x/consensus" // import as blank for app wiring
_ "github.com/cosmos/cosmos-sdk/x/genutil" // import as blank for app wiring
)

var AppConfig = configurator.NewAppConfig(
configurator.AuthModule(),
configurator.BankModule(),
configurator.StakingModule(),
configurator.TxModule(),
configurator.ConsensusModule(),
configurator.GenutilModule(),
configurator.DistributionModule(),
configurator.MintModule(),
configurator.ProtocolPoolModule(),
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cli_test
package distribution_test

import (
"fmt"
Expand All @@ -11,7 +11,7 @@ import (

sdkmath "cosmossdk.io/math"
"cosmossdk.io/x/distribution/client/cli"
distrtestutil "cosmossdk.io/x/distribution/testutil"

minttypes "cosmossdk.io/x/mint/types"

"github.com/cosmos/cosmos-sdk/client"
Expand Down Expand Up @@ -62,7 +62,7 @@ func (s *CLITestSuite) SetupSuite() {
}
s.clientCtx = ctxGen()

cfg, err := network.DefaultConfigWithAppConfig(distrtestutil.AppConfig)
cfg, err := network.DefaultConfigWithAppConfig(AppConfig)
s.Require().NoError(err)

genesisState := cfg.GenesisState
Expand Down
3 changes: 1 addition & 2 deletions tests/integration/distribution/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"cosmossdk.io/log"
authkeeper "cosmossdk.io/x/auth/keeper"
authtypes "cosmossdk.io/x/auth/types"
"cosmossdk.io/x/distribution/testutil"
"cosmossdk.io/x/distribution/types"

simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
Expand All @@ -20,7 +19,7 @@ func TestItCreatesModuleAccountOnInitBlock(t *testing.T) {

app, err := simtestutil.SetupAtGenesis(
depinject.Configs(
testutil.AppConfig,
AppConfig,
depinject.Supply(log.NewNopLogger()),
),
&accountKeeper)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package testutil
package evidence_test

import (
_ "cosmossdk.io/x/auth" // import as blank for app wiring
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"cosmossdk.io/x/evidence"
"cosmossdk.io/x/evidence/exported"
"cosmossdk.io/x/evidence/keeper"
"cosmossdk.io/x/evidence/testutil"

"cosmossdk.io/x/evidence/types"

"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
Expand All @@ -34,7 +34,7 @@ func (suite *GenesisTestSuite) SetupTest() {
app, err := simtestutil.Setup(
depinject.Configs(
depinject.Supply(log.NewNopLogger()),
testutil.AppConfig,
AppConfig,
),
&evidenceKeeper)
require.NoError(suite.T(), err)
Expand Down
20 changes: 10 additions & 10 deletions x/gov/abci_test.go → tests/integration/gov/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

func TestUnregisteredProposal_InactiveProposalFails(t *testing.T) {
suite := createTestSuite(t)
ctx := suite.App.BaseApp.NewContext(false)
ctx := suite.app.BaseApp.NewContext(false)
addrs := simtestutil.AddTestAddrs(suite.BankKeeper, suite.StakingKeeper, ctx, 10, valTokens)

// manually set proposal in store
Expand All @@ -50,7 +50,7 @@ func TestUnregisteredProposal_InactiveProposalFails(t *testing.T) {

func TestUnregisteredProposal_ActiveProposalFails(t *testing.T) {
suite := createTestSuite(t)
ctx := suite.App.BaseApp.NewContext(false)
ctx := suite.app.BaseApp.NewContext(false)
addrs := simtestutil.AddTestAddrs(suite.BankKeeper, suite.StakingKeeper, ctx, 10, valTokens)

// manually set proposal in store
Expand Down Expand Up @@ -79,7 +79,7 @@ func TestUnregisteredProposal_ActiveProposalFails(t *testing.T) {

func TestTickExpiredDepositPeriod(t *testing.T) {
suite := createTestSuite(t)
app := suite.App
app := suite.app
ctx := app.BaseApp.NewContext(false)
addrs := simtestutil.AddTestAddrs(suite.BankKeeper, suite.StakingKeeper, ctx, 10, valTokens)

Expand Down Expand Up @@ -115,7 +115,7 @@ func TestTickExpiredDepositPeriod(t *testing.T) {

func TestTickMultipleExpiredDepositPeriod(t *testing.T) {
suite := createTestSuite(t)
app := suite.App
app := suite.app
ctx := app.BaseApp.NewContext(false)
addrs := simtestutil.AddTestAddrs(suite.BankKeeper, suite.StakingKeeper, ctx, 10, valTokens)
govMsgSvr := keeper.NewMsgServerImpl(suite.GovKeeper)
Expand Down Expand Up @@ -169,7 +169,7 @@ func TestTickMultipleExpiredDepositPeriod(t *testing.T) {

func TestTickPassedDepositPeriod(t *testing.T) {
suite := createTestSuite(t)
app := suite.App
app := suite.app
ctx := app.BaseApp.NewContext(false)
addrs := simtestutil.AddTestAddrs(suite.BankKeeper, suite.StakingKeeper, ctx, 10, valTokens)
govMsgSvr := keeper.NewMsgServerImpl(suite.GovKeeper)
Expand Down Expand Up @@ -204,7 +204,7 @@ func TestTickPassedDepositPeriod(t *testing.T) {

func TestProposalDepositRefundFailEndBlocker(t *testing.T) {
suite := createTestSuite(t)
app := suite.App
app := suite.app
ctx := app.BaseApp.NewContext(false)
addrs := simtestutil.AddTestAddrs(suite.BankKeeper, suite.StakingKeeper, ctx, 10, valTokens)
govMsgSvr := keeper.NewMsgServerImpl(suite.GovKeeper)
Expand Down Expand Up @@ -272,7 +272,7 @@ func TestTickPassedVotingPeriod(t *testing.T) {
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
suite := createTestSuite(t)
app := suite.App
app := suite.app
ctx := app.BaseApp.NewContext(false)
depositMultiplier := getDepositMultiplier(tc.proposalType)
addrs := simtestutil.AddTestAddrs(suite.BankKeeper, suite.StakingKeeper, ctx, 10, valTokens.Mul(math.NewInt(depositMultiplier)))
Expand Down Expand Up @@ -349,7 +349,7 @@ func TestProposalPassedEndblocker(t *testing.T) {
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
suite := createTestSuite(t)
app := suite.App
app := suite.app
ctx := app.BaseApp.NewContext(false)
depositMultiplier := getDepositMultiplier(tc.proposalType)
addrs := simtestutil.AddTestAddrs(suite.BankKeeper, suite.StakingKeeper, ctx, 10, valTokens.Mul(math.NewInt(depositMultiplier)))
Expand Down Expand Up @@ -404,7 +404,7 @@ func TestProposalPassedEndblocker(t *testing.T) {

func TestEndBlockerProposalHandlerFailed(t *testing.T) {
suite := createTestSuite(t)
app := suite.App
app := suite.app
ctx := app.BaseApp.NewContext(false)
addrs := simtestutil.AddTestAddrs(suite.BankKeeper, suite.StakingKeeper, ctx, 1, valTokens)

Expand Down Expand Up @@ -485,7 +485,7 @@ func TestExpeditedProposal_PassAndConversionToRegular(t *testing.T) {
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
suite := createTestSuite(t)
app := suite.App
app := suite.app
ctx := app.BaseApp.NewContext(false)
depositMultiplier := getDepositMultiplier(v1.ProposalType_PROPOSAL_TYPE_EXPEDITED)
addrs := simtestutil.AddTestAddrs(suite.BankKeeper, suite.StakingKeeper, ctx, 3, valTokens.Mul(math.NewInt(depositMultiplier)))
Expand Down
92 changes: 92 additions & 0 deletions tests/integration/gov/common_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
package gov_test

import (
"bytes"
"log"
"sort"
"testing"

"github.com/stretchr/testify/require"
"gotest.tools/v3/assert"

"cosmossdk.io/depinject"
sdklog "cosmossdk.io/log"
"cosmossdk.io/math"
_ "cosmossdk.io/x/auth"
authtypes "cosmossdk.io/x/auth/types"
_ "cosmossdk.io/x/bank"
"cosmossdk.io/x/gov/types"
v1 "cosmossdk.io/x/gov/types/v1"
"cosmossdk.io/x/gov/types/v1beta1"
_ "cosmossdk.io/x/protocolpool"
_ "cosmossdk.io/x/staking"
stakingtypes "cosmossdk.io/x/staking/types"

"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/testutil/configurator"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
_ "github.com/cosmos/cosmos-sdk/x/consensus"
)

var (
Expand All @@ -30,3 +45,80 @@ func mkTestLegacyContent(t *testing.T) *v1.MsgExecLegacyContent {

return msgContent
}

var pubkeys = []cryptotypes.PubKey{
ed25519.GenPrivKey().PubKey(),
ed25519.GenPrivKey().PubKey(),
ed25519.GenPrivKey().PubKey(),
}

// SortAddresses - Sorts Addresses
func SortAddresses(addrs []sdk.AccAddress) {
byteAddrs := make([][]byte, len(addrs))

for i, addr := range addrs {
byteAddrs[i] = addr.Bytes()
}

SortByteArrays(byteAddrs)

for i, byteAddr := range byteAddrs {
addrs[i] = byteAddr
}
}

// implement `Interface` in sort package.
type sortByteArrays [][]byte

func (b sortByteArrays) Len() int {
return len(b)
}

func (b sortByteArrays) Less(i, j int) bool {
// bytes package already implements Comparable for []byte.
switch bytes.Compare(b[i], b[j]) {
case -1:
return true
case 0, 1:
return false
default:
log.Panic("not fail-able with `bytes.Comparable` bounded [-1, 1].")
return false
}
}

func (b sortByteArrays) Swap(i, j int) {
b[j], b[i] = b[i], b[j]
}

// SortByteArrays - sorts the provided byte array
func SortByteArrays(src [][]byte) [][]byte {
sorted := sortByteArrays(src)
sort.Sort(sorted)
return sorted
}

func createTestSuite(t *testing.T) suite {
t.Helper()
res := suite{}

app, err := simtestutil.SetupWithConfiguration(
depinject.Configs(
configurator.NewAppConfig(
configurator.AuthModule(),
configurator.StakingModule(),
configurator.BankModule(),
configurator.GovModule(),
configurator.ConsensusModule(),
configurator.ProtocolPoolModule(),
),
depinject.Supply(sdklog.NewNopLogger()),
),
simtestutil.DefaultStartUpConfig(),
&res.AccountKeeper, &res.BankKeeper, &res.GovKeeper, &res.StakingKeeper,
)
require.NoError(t, err)

res.app = app
return res
}
28 changes: 28 additions & 0 deletions tests/integration/gov/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (

abci "github.com/cometbft/cometbft/abci/types"
dbm "github.com/cosmos/cosmos-db"
"github.com/stretchr/testify/require"
"gotest.tools/v3/assert"

"cosmossdk.io/core/header"
"cosmossdk.io/depinject"
"cosmossdk.io/log"
sdkmath "cosmossdk.io/math"
_ "cosmossdk.io/x/auth"
authkeeper "cosmossdk.io/x/auth/keeper"
authtypes "cosmossdk.io/x/auth/types"
Expand Down Expand Up @@ -196,3 +198,29 @@ func clearDB(t *testing.T, db *dbm.MemDB) {
assert.NilError(t, db.Delete(k))
}
}

func TestImportExportQueues_ErrorUnconsistentState(t *testing.T) {
suite := createTestSuite(t)
app := suite.app
ctx := app.BaseApp.NewContext(false)
require.Panics(t, func() {
gov.InitGenesis(ctx, suite.AccountKeeper, suite.BankKeeper, suite.GovKeeper, &v1.GenesisState{
Deposits: v1.Deposits{
{
ProposalId: 1234,
Depositor: "me",
Amount: sdk.Coins{
sdk.NewCoin(
"stake",
sdkmath.NewInt(1234),
),
},
},
},
})
})
gov.InitGenesis(ctx, suite.AccountKeeper, suite.BankKeeper, suite.GovKeeper, v1.DefaultGenesisState())
genState, err := gov.ExportGenesis(ctx, suite.GovKeeper)
require.NoError(t, err)
require.Equal(t, genState, v1.DefaultGenesisState())
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package testutil
package mint

import (
_ "cosmossdk.io/x/auth" // import as blank for app wiring
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mint_test
package mint

import (
"testing"
Expand All @@ -9,7 +9,6 @@ import (
"cosmossdk.io/log"
authkeeper "cosmossdk.io/x/auth/keeper"
authtypes "cosmossdk.io/x/auth/types"
"cosmossdk.io/x/mint/testutil"
"cosmossdk.io/x/mint/types"

simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
Expand All @@ -20,7 +19,7 @@ func TestItCreatesModuleAccountOnInitBlock(t *testing.T) {

app, err := simtestutil.SetupAtGenesis(
depinject.Configs(
testutil.AppConfig,
AppConfig,
depinject.Supply(log.NewNopLogger()),
), &accountKeeper)
require.NoError(t, err)
Expand Down
Loading

0 comments on commit 7f0f405

Please sign in to comment.