Skip to content

Commit

Permalink
feat: decouple x/mint from simapp (#12255)
Browse files Browse the repository at this point in the history
* feat: decouple `x/mint` from simapp

* update docs
  • Loading branch information
julienrbrt authored Jun 14, 2022
1 parent 015bbed commit 4c3b7af
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 71 deletions.
39 changes: 22 additions & 17 deletions x/mint/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,52 @@ import (
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/simapp"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/mint/keeper"
"github.com/cosmos/cosmos-sdk/x/mint/testutil"
"github.com/cosmos/cosmos-sdk/x/mint/types"
)

type MintTestSuite struct {
suite.Suite

app *simapp.SimApp
ctx sdk.Context
queryClient types.QueryClient
mintKeeper keeper.Keeper
}

func (suite *MintTestSuite) SetupTest() {
app := simapp.Setup(suite.T(), false)
var interfaceRegistry codectypes.InterfaceRegistry

app, err := simtestutil.Setup(testutil.AppConfig,
&interfaceRegistry,
&suite.mintKeeper,
)
suite.Require().NoError(err)

ctx := app.BaseApp.NewContext(false, tmproto.Header{})

queryHelper := baseapp.NewQueryServerTestHelper(ctx, app.InterfaceRegistry())
types.RegisterQueryServer(queryHelper, app.MintKeeper)
queryClient := types.NewQueryClient(queryHelper)
queryHelper := baseapp.NewQueryServerTestHelper(ctx, interfaceRegistry)
types.RegisterQueryServer(queryHelper, suite.mintKeeper)

suite.app = app
suite.ctx = ctx

suite.queryClient = queryClient
suite.queryClient = types.NewQueryClient(queryHelper)
}

func (suite *MintTestSuite) TestGRPCParams() {
app, ctx, queryClient := suite.app, suite.ctx, suite.queryClient

params, err := queryClient.Params(gocontext.Background(), &types.QueryParamsRequest{})
params, err := suite.queryClient.Params(gocontext.Background(), &types.QueryParamsRequest{})
suite.Require().NoError(err)
suite.Require().Equal(params.Params, app.MintKeeper.GetParams(ctx))
suite.Require().Equal(params.Params, suite.mintKeeper.GetParams(suite.ctx))

inflation, err := queryClient.Inflation(gocontext.Background(), &types.QueryInflationRequest{})
inflation, err := suite.queryClient.Inflation(gocontext.Background(), &types.QueryInflationRequest{})
suite.Require().NoError(err)
suite.Require().Equal(inflation.Inflation, app.MintKeeper.GetMinter(ctx).Inflation)
suite.Require().Equal(inflation.Inflation, suite.mintKeeper.GetMinter(suite.ctx).Inflation)

annualProvisions, err := queryClient.AnnualProvisions(gocontext.Background(), &types.QueryAnnualProvisionsRequest{})
annualProvisions, err := suite.queryClient.AnnualProvisions(gocontext.Background(), &types.QueryAnnualProvisionsRequest{})
suite.Require().NoError(err)
suite.Require().Equal(annualProvisions.AnnualProvisions, app.MintKeeper.GetMinter(ctx).AnnualProvisions)
suite.Require().Equal(annualProvisions.AnnualProvisions, suite.mintKeeper.GetMinter(suite.ctx).AnnualProvisions)
}

func TestMintTestSuite(t *testing.T) {
Expand Down
65 changes: 31 additions & 34 deletions x/mint/keeper/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,97 +9,94 @@ import (
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/simapp"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/mint/keeper"
keep "github.com/cosmos/cosmos-sdk/x/mint/keeper"
"github.com/cosmos/cosmos-sdk/x/mint/testutil"
"github.com/cosmos/cosmos-sdk/x/mint/types"
)

type MintKeeperTestSuite struct {
suite.Suite

app *simapp.SimApp
ctx sdk.Context
legacyQuerierCdc *codec.AminoCodec
ctx sdk.Context
legacyAmino *codec.LegacyAmino
mintKeeper keeper.Keeper
}

func (suite *MintKeeperTestSuite) SetupTest() {
app := simapp.Setup(suite.T(), true)
ctx := app.BaseApp.NewContext(true, tmproto.Header{})
app, err := simtestutil.Setup(testutil.AppConfig,
&suite.legacyAmino,
&suite.mintKeeper,
)
suite.Require().NoError(err)

app.MintKeeper.SetParams(ctx, types.DefaultParams())
app.MintKeeper.SetMinter(ctx, types.DefaultInitialMinter())
suite.ctx = app.BaseApp.NewContext(true, tmproto.Header{})

legacyQuerierCdc := codec.NewAminoCodec(app.LegacyAmino())

suite.app = app
suite.ctx = ctx
suite.legacyQuerierCdc = legacyQuerierCdc
suite.mintKeeper.SetParams(suite.ctx, types.DefaultParams())
suite.mintKeeper.SetMinter(suite.ctx, types.DefaultInitialMinter())
}

func (suite *MintKeeperTestSuite) TestNewQuerier(t *testing.T) {
app, ctx, legacyQuerierCdc := suite.app, suite.ctx, suite.legacyQuerierCdc
querier := keep.NewQuerier(app.MintKeeper, legacyQuerierCdc.LegacyAmino)
querier := keep.NewQuerier(suite.mintKeeper, suite.legacyAmino)

query := abci.RequestQuery{
Path: "",
Data: []byte{},
}

_, err := querier(ctx, []string{types.QueryParameters}, query)
_, err := querier(suite.ctx, []string{types.QueryParameters}, query)
require.NoError(t, err)

_, err = querier(ctx, []string{types.QueryInflation}, query)
_, err = querier(suite.ctx, []string{types.QueryInflation}, query)
require.NoError(t, err)

_, err = querier(ctx, []string{types.QueryAnnualProvisions}, query)
_, err = querier(suite.ctx, []string{types.QueryAnnualProvisions}, query)
require.NoError(t, err)

_, err = querier(ctx, []string{"foo"}, query)
_, err = querier(suite.ctx, []string{"foo"}, query)
require.Error(t, err)
}

func (suite *MintKeeperTestSuite) TestQueryParams(t *testing.T) {
app, ctx, legacyQuerierCdc := suite.app, suite.ctx, suite.legacyQuerierCdc
querier := keep.NewQuerier(app.MintKeeper, legacyQuerierCdc.LegacyAmino)
querier := keep.NewQuerier(suite.mintKeeper, suite.legacyAmino)

var params types.Params

res, sdkErr := querier(ctx, []string{types.QueryParameters}, abci.RequestQuery{})
res, sdkErr := querier(suite.ctx, []string{types.QueryParameters}, abci.RequestQuery{})
require.NoError(t, sdkErr)

err := app.LegacyAmino().UnmarshalJSON(res, &params)
err := suite.legacyAmino.UnmarshalJSON(res, &params)
require.NoError(t, err)

require.Equal(t, app.MintKeeper.GetParams(ctx), params)
require.Equal(t, suite.mintKeeper.GetParams(suite.ctx), params)
}

func (suite *MintKeeperTestSuite) TestQueryInflation(t *testing.T) {
app, ctx, legacyQuerierCdc := suite.app, suite.ctx, suite.legacyQuerierCdc
querier := keep.NewQuerier(app.MintKeeper, legacyQuerierCdc.LegacyAmino)
querier := keep.NewQuerier(suite.mintKeeper, suite.legacyAmino)

var inflation sdk.Dec

res, sdkErr := querier(ctx, []string{types.QueryInflation}, abci.RequestQuery{})
res, sdkErr := querier(suite.ctx, []string{types.QueryInflation}, abci.RequestQuery{})
require.NoError(t, sdkErr)

err := app.LegacyAmino().UnmarshalJSON(res, &inflation)
err := suite.legacyAmino.UnmarshalJSON(res, &inflation)
require.NoError(t, err)

require.Equal(t, app.MintKeeper.GetMinter(ctx).Inflation, inflation)
require.Equal(t, suite.mintKeeper.GetMinter(suite.ctx).Inflation, inflation)
}

func (suite *MintKeeperTestSuite) TestQueryAnnualProvisions(t *testing.T) {
app, ctx, legacyQuerierCdc := suite.app, suite.ctx, suite.legacyQuerierCdc
querier := keep.NewQuerier(app.MintKeeper, legacyQuerierCdc.LegacyAmino)
querier := keep.NewQuerier(suite.mintKeeper, suite.legacyAmino)

var annualProvisions sdk.Dec

res, sdkErr := querier(ctx, []string{types.QueryAnnualProvisions}, abci.RequestQuery{})
res, sdkErr := querier(suite.ctx, []string{types.QueryAnnualProvisions}, abci.RequestQuery{})
require.NoError(t, sdkErr)

err := app.LegacyAmino().UnmarshalJSON(res, &annualProvisions)
err := suite.legacyAmino.UnmarshalJSON(res, &annualProvisions)
require.NoError(t, err)

require.Equal(t, app.MintKeeper.GetMinter(ctx).AnnualProvisions, annualProvisions)
require.Equal(t, suite.mintKeeper.GetMinter(suite.ctx).AnnualProvisions, annualProvisions)
}
24 changes: 6 additions & 18 deletions x/mint/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,22 @@ import (
"testing"

"github.com/stretchr/testify/require"
abcitypes "github.com/tendermint/tendermint/abci/types"
tmjson "github.com/tendermint/tendermint/libs/json"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
dbm "github.com/tendermint/tm-db"

"github.com/cosmos/cosmos-sdk/simapp"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/mint/testutil"
"github.com/cosmos/cosmos-sdk/x/mint/types"
)

func TestItCreatesModuleAccountOnInitBlock(t *testing.T) {
db := dbm.NewMemDB()
encCdc := simapp.MakeTestEncodingConfig()
app := simapp.NewSimApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, simapp.DefaultNodeHome, 5, encCdc, simapp.EmptyAppOptions{})
var accountKeeper authkeeper.AccountKeeper

genesisState := simapp.GenesisStateWithSingleValidator(t, app)
stateBytes, err := tmjson.Marshal(genesisState)
app, err := simtestutil.Setup(testutil.AppConfig, &accountKeeper)
require.NoError(t, err)

app.InitChain(
abcitypes.RequestInitChain{
AppStateBytes: stateBytes,
ChainId: "test-chain-id",
},
)

ctx := app.BaseApp.NewContext(false, tmproto.Header{})
acc := app.AccountKeeper.GetAccount(ctx, authtypes.NewModuleAddress(types.ModuleName))
acc := accountKeeper.GetAccount(ctx, authtypes.NewModuleAddress(types.ModuleName))
require.NotNil(t, acc)
}
9 changes: 7 additions & 2 deletions x/mint/simulation/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@ import (

"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/depinject"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/kv"
"github.com/cosmos/cosmos-sdk/x/mint/simulation"
"github.com/cosmos/cosmos-sdk/x/mint/testutil"
"github.com/cosmos/cosmos-sdk/x/mint/types"
)

func TestDecodeStore(t *testing.T) {
cdc := simapp.MakeTestEncodingConfig().Codec
var cdc codec.Codec
err := depinject.Inject(testutil.AppConfig, &cdc)
require.NoError(t, err)

dec := simulation.NewDecodeStore(cdc)

minter := types.NewMinter(sdk.OneDec(), sdk.NewDec(15))
Expand Down
6 changes: 6 additions & 0 deletions x/mint/spec/01_concepts.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,9 @@ It can be broken down in the following way:
rate will stay constant
* If the inflation rate is above the goal %-bonded the inflation rate will
decrease until a minimum value is reached

## App Wiring

The minimal app-wiring configuration for `x/mint` is as follows:

+++ https://github.com/cosmos/cosmos-sdk/blob/main/x/mint/testutil/app.yaml
7 changes: 7 additions & 0 deletions x/mint/testutil/app_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ import (
_ "embed"

"cosmossdk.io/core/appconfig"
_ "github.com/cosmos/cosmos-sdk/x/auth"
_ "github.com/cosmos/cosmos-sdk/x/auth/tx/module"
_ "github.com/cosmos/cosmos-sdk/x/bank"
_ "github.com/cosmos/cosmos-sdk/x/genutil"
_ "github.com/cosmos/cosmos-sdk/x/mint"
_ "github.com/cosmos/cosmos-sdk/x/params"
_ "github.com/cosmos/cosmos-sdk/x/staking"
)

//go:embed app.yaml
Expand Down

0 comments on commit 4c3b7af

Please sign in to comment.