Skip to content

Commit 46a803b

Browse files
authored
Merge pull request #8 from neutron-org/fix/unit-tests
Fix: unit tests
2 parents c7f18f4 + cc3f3f2 commit 46a803b

11 files changed

+292
-104
lines changed

app/app.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ var (
150150
authtypes.FeeCollectorName: nil,
151151
distrtypes.ModuleName: nil,
152152
minttypes.ModuleName: {authtypes.Minter},
153+
banktypes.ModuleName: {authtypes.Minter},
153154
stakingtypes.BondedPoolName: {authtypes.Burner, authtypes.Staking},
154155
stakingtypes.NotBondedPoolName: {authtypes.Burner, authtypes.Staking},
155156
govtypes.ModuleName: {authtypes.Burner},
@@ -179,7 +180,7 @@ type App struct {
179180
*baseapp.BaseApp
180181

181182
cdc *codec.LegacyAmino
182-
appCodec codec.Codec
183+
AppCodec codec.Codec
183184
interfaceRegistry types.InterfaceRegistry
184185

185186
invCheckPeriod uint
@@ -254,6 +255,7 @@ func New(
254255
minttypes.StoreKey, distrtypes.StoreKey, slashingtypes.StoreKey,
255256
govtypes.StoreKey, paramstypes.StoreKey, ibchost.StoreKey, upgradetypes.StoreKey,
256257
evidencetypes.StoreKey, ibctransfertypes.StoreKey, capabilitytypes.StoreKey, feegrant.StoreKey,
258+
consensusparamtypes.StoreKey, crisistypes.StoreKey,
257259
// this line is used by starport scaffolding # stargate/app/storeKey
258260
adminmodulemoduletypes.StoreKey,
259261
)
@@ -263,7 +265,7 @@ func New(
263265
app := &App{
264266
BaseApp: bApp,
265267
cdc: legacyAmino,
266-
appCodec: appCodec,
268+
AppCodec: appCodec,
267269
interfaceRegistry: interfaceRegistry,
268270
invCheckPeriod: invCheckPeriod,
269271
keys: keys,
@@ -427,13 +429,12 @@ func New(
427429
AddRoute(upgradetypes.RouterKey, upgrade.NewSoftwareUpgradeProposalHandler(&app.UpgradeKeeper)).
428430
AddRoute(ibchost.RouterKey, ibcclient.NewClientProposalHandler(app.IBCKeeper.ClientKeeper))
429431

430-
adminRouter := baseapp.NewMsgServiceRouter()
431432
app.AdminmoduleKeeper = *adminmodulemodulekeeper.NewKeeper(
432433
appCodec,
433434
keys[adminmodulemoduletypes.StoreKey],
434435
keys[adminmodulemoduletypes.MemStoreKey],
435436
adminRouterLegacy,
436-
adminRouter,
437+
app.MsgServiceRouter(),
437438
// this allows any type of proposal to be submitted to the admin module (everything is whitelisted)
438439
// projects will implement their functions to define what is allowed for admins.
439440
func(govv1beta1.Content) bool { return true },
@@ -558,7 +559,7 @@ func New(
558559
)
559560

560561
app.mm.RegisterInvariants(&app.CrisisKeeper)
561-
app.configurator = module.NewConfigurator(app.appCodec, app.MsgServiceRouter(), app.GRPCQueryRouter())
562+
app.configurator = module.NewConfigurator(app.AppCodec, app.MsgServiceRouter(), app.GRPCQueryRouter())
562563
app.mm.RegisterServices(app.configurator)
563564

564565
// initialize stores
@@ -626,7 +627,7 @@ func (app *App) InitChainer(ctx sdk.Context, req abci.RequestInitChain) abci.Res
626627
if err := tmjson.Unmarshal(req.AppStateBytes, &genesisState); err != nil {
627628
panic(err)
628629
}
629-
return app.mm.InitGenesis(ctx, app.appCodec, genesisState)
630+
return app.mm.InitGenesis(ctx, app.AppCodec, genesisState)
630631
}
631632

632633
// LoadHeight loads a particular height

app/export.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func (app *App) ExportAppStateAndValidators(
3030
app.prepForZeroHeightGenesis(ctx, jailAllowedAddrs)
3131
}
3232

33-
genState := app.mm.ExportGenesisForModules(ctx, app.appCodec, modulesToExport)
33+
genState := app.mm.ExportGenesisForModules(ctx, app.AppCodec, modulesToExport)
3434
appState, err := json.MarshalIndent(genState, "", " ")
3535
if err != nil {
3636
return servertypes.ExportedApp{}, err

app/test_helpers.go

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package app
2+
3+
import (
4+
"encoding/json"
5+
"time"
6+
7+
cometbftdb "github.com/cometbft/cometbft-db"
8+
abci "github.com/cometbft/cometbft/abci/types"
9+
"github.com/cometbft/cometbft/crypto/secp256k1"
10+
"github.com/cometbft/cometbft/libs/log"
11+
tmtypes "github.com/cometbft/cometbft/types"
12+
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
13+
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
14+
"github.com/cosmos/cosmos-sdk/testutil/mock"
15+
"github.com/cosmos/cosmos-sdk/testutil/sims"
16+
sdk "github.com/cosmos/cosmos-sdk/types"
17+
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
18+
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
19+
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
20+
)
21+
22+
func GenesisStateWithValSet(app *App) GenesisState {
23+
privVal := mock.NewPV()
24+
pubKey, _ := privVal.GetPubKey()
25+
validator := tmtypes.NewValidator(pubKey, 1)
26+
valSet := tmtypes.NewValidatorSet([]*tmtypes.Validator{validator})
27+
28+
// generate genesis account
29+
senderPrivKey := secp256k1.GenPrivKey()
30+
senderPrivKey.PubKey().Address()
31+
acc := authtypes.NewBaseAccountWithAddress(senderPrivKey.PubKey().Address().Bytes())
32+
33+
//////////////////////
34+
balances := []banktypes.Balance{}
35+
genesisState := NewDefaultGenesisState(app.AppCodec)
36+
genAccs := []authtypes.GenesisAccount{acc}
37+
authGenesis := authtypes.NewGenesisState(authtypes.DefaultParams(), genAccs)
38+
genesisState[authtypes.ModuleName] = app.AppCodec.MustMarshalJSON(authGenesis)
39+
40+
validators := make([]stakingtypes.Validator, 0, len(valSet.Validators))
41+
delegations := make([]stakingtypes.Delegation, 0, len(valSet.Validators))
42+
43+
bondAmt := sdk.DefaultPowerReduction
44+
initValPowers := []abci.ValidatorUpdate{}
45+
46+
for _, val := range valSet.Validators {
47+
pk, _ := cryptocodec.FromTmPubKeyInterface(val.PubKey)
48+
pkAny, _ := codectypes.NewAnyWithValue(pk)
49+
validator := stakingtypes.Validator{
50+
OperatorAddress: sdk.ValAddress(val.Address).String(),
51+
ConsensusPubkey: pkAny,
52+
Jailed: false,
53+
Status: stakingtypes.Bonded,
54+
Tokens: bondAmt,
55+
DelegatorShares: sdk.OneDec(),
56+
Description: stakingtypes.Description{},
57+
UnbondingHeight: int64(0),
58+
UnbondingTime: time.Unix(0, 0).UTC(),
59+
Commission: stakingtypes.NewCommission(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()),
60+
MinSelfDelegation: sdk.NewInt(0),
61+
}
62+
validators = append(validators, validator)
63+
delegations = append(delegations, stakingtypes.NewDelegation(genAccs[0].GetAddress(), val.Address.Bytes(), sdk.OneDec()))
64+
65+
// add initial validator powers so consumer InitGenesis runs correctly
66+
pub, _ := val.ToProto()
67+
initValPowers = append(initValPowers, abci.ValidatorUpdate{
68+
Power: val.VotingPower,
69+
PubKey: pub.PubKey,
70+
})
71+
}
72+
// set validators and delegations
73+
stakingGenesis := stakingtypes.NewGenesisState(stakingtypes.DefaultParams(), validators, delegations)
74+
genesisState[stakingtypes.ModuleName] = app.AppCodec.MustMarshalJSON(stakingGenesis)
75+
76+
totalSupply := sdk.NewCoins()
77+
for _, b := range balances {
78+
// add genesis acc tokens to total supply
79+
totalSupply = totalSupply.Add(b.Coins...)
80+
}
81+
82+
for range delegations {
83+
// add delegated tokens to total supply
84+
totalSupply = totalSupply.Add(sdk.NewCoin(sdk.DefaultBondDenom, bondAmt))
85+
}
86+
87+
// add bonded amount to bonded pool module account
88+
balances = append(balances, banktypes.Balance{
89+
Address: authtypes.NewModuleAddress(stakingtypes.BondedPoolName).String(),
90+
Coins: sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, bondAmt)},
91+
})
92+
93+
// update total supply
94+
bankGenesis := banktypes.NewGenesisState(
95+
banktypes.DefaultGenesisState().Params,
96+
balances,
97+
totalSupply,
98+
[]banktypes.Metadata{},
99+
[]banktypes.SendEnabled{},
100+
)
101+
genesisState[banktypes.ModuleName] = app.AppCodec.MustMarshalJSON(bankGenesis)
102+
103+
_, err := tmtypes.PB2TM.ValidatorUpdates(initValPowers)
104+
if err != nil {
105+
panic("failed to get vals")
106+
}
107+
108+
return genesisState
109+
}
110+
111+
func GetTestApp() *App {
112+
db := cometbftdb.NewMemDB()
113+
testApp := New(
114+
log.NewNopLogger(),
115+
db,
116+
nil,
117+
true,
118+
map[int64]bool{},
119+
sims.EmptyAppOptions{},
120+
)
121+
122+
genesisState := GenesisStateWithValSet(&testApp)
123+
stateBytes, err := json.MarshalIndent(genesisState, "", " ")
124+
if err != nil {
125+
panic(err)
126+
}
127+
128+
testApp.InitChain(
129+
abci.RequestInitChain{
130+
ChainId: "",
131+
Validators: []abci.ValidatorUpdate{},
132+
ConsensusParams: sims.DefaultConsensusParams,
133+
AppStateBytes: stateBytes,
134+
},
135+
)
136+
137+
return &testApp
138+
}

x/adminmodule/keeper/keeper_test.go

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,16 @@ package keeper_test
22

33
import (
44
"fmt"
5-
"testing"
65

7-
tmdb "github.com/cometbft/cometbft-db"
8-
"github.com/cometbft/cometbft/libs/log"
96
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
7+
"github.com/cosmos/admin-module/app"
108
"github.com/cosmos/admin-module/x/adminmodule/keeper"
11-
"github.com/cosmos/admin-module/x/adminmodule/types"
12-
"github.com/cosmos/cosmos-sdk/codec"
13-
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
14-
"github.com/cosmos/cosmos-sdk/store"
15-
storetypes "github.com/cosmos/cosmos-sdk/store/types"
169
sdk "github.com/cosmos/cosmos-sdk/types"
17-
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
18-
govv1types "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
19-
"github.com/stretchr/testify/require"
2010
)
2111

22-
func setupKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
23-
storeKey := sdk.NewKVStoreKey(types.StoreKey)
24-
memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey)
25-
26-
// TODO Add more routes
27-
rtr := govv1types.NewRouter()
28-
rtr.AddRoute(govtypes.RouterKey, govv1types.ProposalHandler)
29-
30-
db := tmdb.NewMemDB()
31-
stateStore := store.NewCommitMultiStore(db)
32-
stateStore.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db)
33-
stateStore.MountStoreWithDB(memStoreKey, storetypes.StoreTypeMemory, nil)
34-
require.NoError(t, stateStore.LoadLatestVersion())
35-
36-
registry := codectypes.NewInterfaceRegistry()
37-
//cdc := codec.NewProtoCodec(registry)
38-
39-
types.RegisterInterfaces(registry)
40-
k := keeper.NewKeeper(
41-
codec.NewProtoCodec(registry),
42-
storeKey,
43-
memStoreKey,
44-
rtr,
45-
func(govv1types.Content) bool { return true },
46-
)
47-
ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())
48-
return k, ctx
12+
func setupKeeper() (*keeper.Keeper, sdk.Context) {
13+
testApp := app.GetTestApp()
14+
return &testApp.AdminmoduleKeeper, testApp.BaseApp.NewContext(false, tmproto.Header{})
4915
}
5016

5117
// Using for setting admins before tests

x/adminmodule/keeper/msg_server_add_admin_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ import (
99
)
1010

1111
func TestAddAdmin(t *testing.T) {
12-
// queryClient := suite.queryClient
13-
// keeper, ctx := setupKeeper(t)
14-
msgServer, ctx, keeper := setupMsgServer(t)
12+
msgServer, ctx, keeper := setupMsgServer()
1513

1614
var (
1715
req *types.QueryAdminsRequest

x/adminmodule/keeper/msg_server_delete_admin_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ import (
99
)
1010

1111
func TestDeleteAdmin(t *testing.T) {
12-
// queryClient := suite.queryClient
13-
// keeper, ctx := setupKeeper(t)
14-
msgServer, ctx, keeper := setupMsgServer(t)
12+
msgServer, ctx, keeper := setupMsgServer()
1513

1614
var req *types.QueryAdminsRequest
1715

x/adminmodule/keeper/msg_server_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ package keeper_test
22

33
import (
44
"context"
5-
"testing"
65

76
"github.com/cosmos/admin-module/x/adminmodule/keeper"
87
"github.com/cosmos/admin-module/x/adminmodule/types"
98
sdk "github.com/cosmos/cosmos-sdk/types"
109
)
1110

12-
func setupMsgServer(t testing.TB) (types.MsgServer, context.Context, *keeper.Keeper) {
13-
k, ctx := setupKeeper(t)
11+
func setupMsgServer() (types.MsgServer, context.Context, *keeper.Keeper) {
12+
k, ctx := setupKeeper()
1413
return keeper.NewMsgServerImpl(*k), sdk.WrapSDKContext(ctx), k
1514
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package keeper_test
2+
3+
import (
4+
"testing"
5+
6+
sdk "github.com/cosmos/cosmos-sdk/types"
7+
govv1types "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestAddToArchiveLegacy(t *testing.T) {
12+
_, ctx, keeper := setupMsgServer()
13+
keeper.SetProposalIDLegacy(sdk.UnwrapSDKContext(ctx), 1)
14+
15+
tp := &govv1types.TextProposal{Title: "Test", Description: "Test Description"}
16+
proposal, err := keeper.SubmitProposalLegacy(sdk.UnwrapSDKContext(ctx), tp)
17+
require.NoError(t, err)
18+
19+
keeper.AddToArchiveLegacy(sdk.UnwrapSDKContext(ctx), proposal)
20+
21+
proposals := keeper.GetArchivedProposalsLegacy(sdk.UnwrapSDKContext(ctx))
22+
require.True(t, len(proposals) == 1)
23+
24+
t.Log(tp, proposals[0].GetContent())
25+
require.Equal(t, tp, proposals[0].GetContent())
26+
27+
}

x/adminmodule/keeper/proposal_archive_test.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,43 @@ package keeper_test
33
import (
44
"testing"
55

6+
"github.com/cometbft/cometbft/proto/tendermint/types"
7+
"github.com/cosmos/admin-module/app"
68
sdk "github.com/cosmos/cosmos-sdk/types"
7-
govv1types "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
9+
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
810
"github.com/stretchr/testify/require"
911
)
1012

1113
func TestAddToArchive(t *testing.T) {
12-
_, ctx, keeper := setupMsgServer(t)
14+
testApp := app.GetTestApp()
15+
keeper := testApp.AdminmoduleKeeper
16+
bankKeeper := testApp.BankKeeper
17+
18+
acc1 := sdk.AccAddress("acc1")
19+
acc2 := sdk.AccAddress("acc2")
20+
coins := sdk.NewCoins(sdk.NewInt64Coin("denom", 10))
21+
22+
ctx := testApp.NewContext(false, types.Header{})
23+
1324
keeper.SetProposalID(sdk.UnwrapSDKContext(ctx), 1)
1425

15-
tp := &govv1types.TextProposal{Title: "Test", Description: "Test Description"}
16-
proposal, err := keeper.SubmitProposal(sdk.UnwrapSDKContext(ctx), tp)
26+
if err := bankKeeper.MintCoins(ctx, banktypes.ModuleName, coins); err != nil {
27+
t.Fatal(err.Error())
28+
}
29+
30+
if err := bankKeeper.SendCoinsFromModuleToAccount(ctx, banktypes.ModuleName, acc1, coins); err != nil {
31+
t.Fatal(err.Error())
32+
}
33+
34+
msgs := []sdk.Msg{banktypes.NewMsgSend(acc1, acc2, coins)}
35+
proposal, err := keeper.SubmitProposal(sdk.UnwrapSDKContext(ctx), msgs)
1736
require.NoError(t, err)
1837

1938
keeper.AddToArchive(sdk.UnwrapSDKContext(ctx), proposal)
2039

2140
proposals := keeper.GetArchivedProposals(sdk.UnwrapSDKContext(ctx))
22-
require.True(t, len(proposals) == 1)
23-
24-
t.Log(tp, proposals[0].GetContent())
25-
require.Equal(t, tp, proposals[0].GetContent())
2641

42+
require.True(t, len(proposals) == 1)
43+
require.Equal(t, 1, len(proposals[0].GetMessages()))
44+
require.Equal(t, bankKeeper.GetAllBalances(ctx, sdk.AccAddress("acc2")), coins)
2745
}

0 commit comments

Comments
 (0)