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: remove simapp from x/staking module #13101

Merged
merged 5 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
- [#12886](https://github.com/cosmos/cosmos-sdk/pull/12886) Amortize cost of processing cache KV store
- [#12953](https://github.com/cosmos/cosmos-sdk/pull/12953) Change the default priority mechanism to be based on gas price.
- [#13048](https://github.com/cosmos/cosmos-sdk/pull/13048) Add handling of AccountNumberStoreKeyPrefix to the x/auth simulation decoder.
- [#13101](https://github.com/cosmos/cosmos-sdk/pull/13101) Remove weight params from `simapp/params`.
cool-develope marked this conversation as resolved.
Show resolved Hide resolved

### State Machine Breaking

Expand Down
13 changes: 0 additions & 13 deletions simapp/params/weights.go

This file was deleted.

108 changes: 56 additions & 52 deletions x/staking/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,37 @@ package staking_test
import (
"testing"

"github.com/stretchr/testify/require"

"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/testutil/sims"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"

"github.com/stretchr/testify/require"
sdk "github.com/cosmos/cosmos-sdk/types"
abci "github.com/tendermint/tendermint/abci/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
bankKeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
stakingKeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
"github.com/cosmos/cosmos-sdk/x/staking/testutil"
"github.com/cosmos/cosmos-sdk/x/staking/types"
)

func checkValidator(t *testing.T, app *simapp.SimApp, addr sdk.ValAddress, expFound bool) types.Validator {
ctxCheck := app.BaseApp.NewContext(true, tmproto.Header{})
validator, found := app.StakingKeeper.GetValidator(ctxCheck, addr)
// nolint:deadcode,unused,varcheck
cool-develope marked this conversation as resolved.
Show resolved Hide resolved
var (
priv1 = secp256k1.GenPrivKey()
addr1 = sdk.AccAddress(priv1.PubKey().Address())
priv2 = secp256k1.GenPrivKey()
addr2 = sdk.AccAddress(priv2.PubKey().Address())

require.Equal(t, expFound, found)
return validator
}

func checkDelegation(
t *testing.T, app *simapp.SimApp, delegatorAddr sdk.AccAddress,
validatorAddr sdk.ValAddress, expFound bool, expShares sdk.Dec,
) {
ctxCheck := app.BaseApp.NewContext(true, tmproto.Header{})
delegation, found := app.StakingKeeper.GetDelegation(ctxCheck, delegatorAddr, validatorAddr)
if expFound {
require.True(t, found)
require.True(sdk.DecEq(t, expShares, delegation.Shares))

return
}
valKey = ed25519.GenPrivKey()
commissionRates = types.NewCommissionRates(math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec())

require.False(t, found)
}
PKs = simtestutil.CreateTestPubKeys(500)
)

func TestStakingMsgs(t *testing.T) {
genTokens := sdk.TokensFromConsensusPower(42, sdk.DefaultPowerReduction)
Expand All @@ -50,21 +43,25 @@ func TestStakingMsgs(t *testing.T) {

acc1 := &authtypes.BaseAccount{Address: addr1.String()}
acc2 := &authtypes.BaseAccount{Address: addr2.String()}
accs := authtypes.GenesisAccounts{acc1, acc2}
balances := []banktypes.Balance{
{
Address: addr1.String(),
Coins: sdk.Coins{genCoin},
},
{
Address: addr2.String(),
Coins: sdk.Coins{genCoin},
},
accs := []simtestutil.GenesisAccount{
{GenesisAccount: acc1, Coins: sdk.Coins{genCoin}},
{GenesisAccount: acc2, Coins: sdk.Coins{genCoin}},
}

app := simapp.SetupWithGenesisAccounts(t, accs, balances...)
simapp.CheckBalance(t, app, addr1, sdk.Coins{genCoin})
simapp.CheckBalance(t, app, addr2, sdk.Coins{genCoin})
var (
bankKeeper bankKeeper.Keeper
stakingKeeper *stakingKeeper.Keeper
)

startupCfg := simtestutil.DefaultStartUpConfig()
startupCfg.GenesisAccounts = accs

app, err := simtestutil.SetupWithConfiguration(testutil.AppConfig, startupCfg, &bankKeeper, &stakingKeeper)
require.NoError(t, err)
ctxCheck := app.BaseApp.NewContext(true, tmproto.Header{})

require.True(t, sdk.Coins{genCoin}.IsEqual(bankKeeper.GetAllBalances(ctxCheck, addr1)))
require.True(t, sdk.Coins{genCoin}.IsEqual(bankKeeper.GetAllBalances(ctxCheck, addr2)))

// create validator
description := types.NewDescription("foo_moniker", "", "", "", "")
Expand All @@ -75,14 +72,15 @@ func TestStakingMsgs(t *testing.T) {

header := tmproto.Header{Height: app.LastBlockHeight() + 1}
txConfig := moduletestutil.MakeTestEncodingConfig().TxConfig
_, _, err = sims.SignCheckDeliver(t, txConfig, app.BaseApp, header, []sdk.Msg{createValidatorMsg}, "", []uint64{0}, []uint64{0}, true, true, priv1)
_, _, err = simtestutil.SignCheckDeliver(t, txConfig, app.BaseApp, header, []sdk.Msg{createValidatorMsg}, "", []uint64{0}, []uint64{0}, true, true, priv1)
require.NoError(t, err)
simapp.CheckBalance(t, app, addr1, sdk.Coins{genCoin.Sub(bondCoin)})
require.True(t, sdk.Coins{genCoin.Sub(bondCoin)}.IsEqual(bankKeeper.GetAllBalances(ctxCheck, addr1)))

header = tmproto.Header{Height: app.LastBlockHeight() + 1}
app.BeginBlock(abci.RequestBeginBlock{Header: header})

validator := checkValidator(t, app, sdk.ValAddress(addr1), true)
ctxCheck = app.BaseApp.NewContext(true, tmproto.Header{})
validator, found := stakingKeeper.GetValidator(ctxCheck, sdk.ValAddress(addr1))
require.True(t, found)
require.Equal(t, sdk.ValAddress(addr1).String(), validator.OperatorAddress)
require.Equal(t, types.Bonded, validator.Status)
require.True(math.IntEq(t, bondTokens, validator.BondedTokens()))
Expand All @@ -95,32 +93,38 @@ func TestStakingMsgs(t *testing.T) {
editValidatorMsg := types.NewMsgEditValidator(sdk.ValAddress(addr1), description, nil, nil)

header = tmproto.Header{Height: app.LastBlockHeight() + 1}
_, _, err = sims.SignCheckDeliver(t, txConfig, app.BaseApp, header, []sdk.Msg{editValidatorMsg}, "", []uint64{0}, []uint64{1}, true, true, priv1)
_, _, err = simtestutil.SignCheckDeliver(t, txConfig, app.BaseApp, header, []sdk.Msg{editValidatorMsg}, "", []uint64{0}, []uint64{1}, true, true, priv1)
require.NoError(t, err)

validator = checkValidator(t, app, sdk.ValAddress(addr1), true)
ctxCheck = app.BaseApp.NewContext(true, tmproto.Header{})
validator, found = stakingKeeper.GetValidator(ctxCheck, sdk.ValAddress(addr1))
require.True(t, found)
require.Equal(t, description, validator.Description)

// delegate
simapp.CheckBalance(t, app, addr2, sdk.Coins{genCoin})
require.True(t, sdk.Coins{genCoin}.IsEqual(bankKeeper.GetAllBalances(ctxCheck, addr2)))
delegateMsg := types.NewMsgDelegate(addr2, sdk.ValAddress(addr1), bondCoin)

header = tmproto.Header{Height: app.LastBlockHeight() + 1}
_, _, err = sims.SignCheckDeliver(t, txConfig, app.BaseApp, header, []sdk.Msg{delegateMsg}, "", []uint64{1}, []uint64{0}, true, true, priv2)
_, _, err = simtestutil.SignCheckDeliver(t, txConfig, app.BaseApp, header, []sdk.Msg{delegateMsg}, "", []uint64{1}, []uint64{0}, true, true, priv2)
require.NoError(t, err)

simapp.CheckBalance(t, app, addr2, sdk.Coins{genCoin.Sub(bondCoin)})
checkDelegation(t, app, addr2, sdk.ValAddress(addr1), true, sdk.NewDecFromInt(bondTokens))
ctxCheck = app.BaseApp.NewContext(true, tmproto.Header{})
require.True(t, sdk.Coins{genCoin.Sub(bondCoin)}.IsEqual(bankKeeper.GetAllBalances(ctxCheck, addr2)))
_, found = stakingKeeper.GetDelegation(ctxCheck, addr2, sdk.ValAddress(addr1))
require.True(t, found)

// begin unbonding
beginUnbondingMsg := types.NewMsgUndelegate(addr2, sdk.ValAddress(addr1), bondCoin)
header = tmproto.Header{Height: app.LastBlockHeight() + 1}
_, _, err = sims.SignCheckDeliver(t, txConfig, app.BaseApp, header, []sdk.Msg{beginUnbondingMsg}, "", []uint64{1}, []uint64{1}, true, true, priv2)
_, _, err = simtestutil.SignCheckDeliver(t, txConfig, app.BaseApp, header, []sdk.Msg{beginUnbondingMsg}, "", []uint64{1}, []uint64{1}, true, true, priv2)
require.NoError(t, err)

// delegation should exist anymore
checkDelegation(t, app, addr2, sdk.ValAddress(addr1), false, sdk.Dec{})
ctxCheck = app.BaseApp.NewContext(true, tmproto.Header{})
_, found = stakingKeeper.GetDelegation(ctxCheck, addr2, sdk.ValAddress(addr1))
require.False(t, found)

// balance should be the same because bonding not yet complete
simapp.CheckBalance(t, app, addr2, sdk.Coins{genCoin.Sub(bondCoin)})
require.True(t, sdk.Coins{genCoin.Sub(bondCoin)}.IsEqual(bankKeeper.GetAllBalances(ctxCheck, addr2)))
}
68 changes: 0 additions & 68 deletions x/staking/common_test.go

This file was deleted.

33 changes: 7 additions & 26 deletions x/staking/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,25 @@ 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/client/flags"
"github.com/cosmos/cosmos-sdk/server"
"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/staking/testutil"
"github.com/cosmos/cosmos-sdk/x/staking/types"
)

func TestItCreatesModuleAccountOnInitBlock(t *testing.T) {
db := dbm.NewMemDB()

appOptions := make(simtestutil.AppOptionsMap, 0)
appOptions[flags.FlagHome] = simapp.DefaultNodeHome
appOptions[server.FlagInvCheckPeriod] = 5

app := simapp.NewSimApp(log.NewNopLogger(), db, nil, true, appOptions)

genesisState := simapp.GenesisStateWithSingleValidator(t, app)
stateBytes, err := tmjson.Marshal(genesisState)
var accountKeeper authKeeper.AccountKeeper
app, err := simtestutil.SetupAtGenesis(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.BondedPoolName))
acc := accountKeeper.GetAccount(ctx, authtypes.NewModuleAddress(types.BondedPoolName))

require.NotNil(t, acc)

acc = app.AccountKeeper.GetAccount(ctx, authtypes.NewModuleAddress(types.NotBondedPoolName))
acc = accountKeeper.GetAccount(ctx, authtypes.NewModuleAddress(types.NotBondedPoolName))
require.NotNil(t, acc)
}
Loading