Skip to content

Commit

Permalink
feat: decouple x/slashing from simapp (#12315)
Browse files Browse the repository at this point in the history
## Description

Closes: #12195 



---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
  • Loading branch information
atheeshp committed Jun 23, 2022
1 parent 464fd17 commit 69c9e3e
Show file tree
Hide file tree
Showing 13 changed files with 427 additions and 331 deletions.
9 changes: 9 additions & 0 deletions testutil/sims/address_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ import (

type GenerateAccountStrategy func(int) []sdk.AccAddress

// AddTestAddrsFromPubKeys adds the addresses into the SimApp providing only the public keys.
func AddTestAddrsFromPubKeys(bankKeeper bankkeeper.Keeper, stakingKeeper *stakingkeeper.Keeper, ctx sdk.Context, pubKeys []cryptotypes.PubKey, accAmt math.Int) {
initCoins := sdk.NewCoins(sdk.NewCoin(stakingKeeper.BondDenom(ctx), accAmt))

for _, pk := range pubKeys {
initAccountWithCoins(bankKeeper, ctx, sdk.AccAddress(pk.Address()), initCoins)
}
}

// AddTestAddrs constructs and returns accNum amount of accounts with an
// initial balance of accAmt in random order
func AddTestAddrs(bankKeeper bankkeeper.Keeper, stakingKeeper *stakingkeeper.Keeper, ctx sdk.Context, accNum int, accAmt math.Int) []sdk.AccAddress {
Expand Down
49 changes: 33 additions & 16 deletions x/slashing/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,50 @@ import (
abci "github.com/tendermint/tendermint/abci/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

"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"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
"github.com/cosmos/cosmos-sdk/x/slashing"
slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
"github.com/cosmos/cosmos-sdk/x/slashing/testutil"
"github.com/cosmos/cosmos-sdk/x/staking"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
"github.com/cosmos/cosmos-sdk/x/staking/teststaking"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

func TestBeginBlocker(t *testing.T) {
app := simapp.Setup(t, false)
var interfaceRegistry codectypes.InterfaceRegistry
var bankKeeper bankkeeper.Keeper
var stakingKeeper *stakingkeeper.Keeper
var slashingKeeper slashingkeeper.Keeper

app, err := simtestutil.Setup(
testutil.AppConfig,
&interfaceRegistry,
&bankKeeper,
&stakingKeeper,
&slashingKeeper,
)
require.NoError(t, err)

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

pks := simtestutil.CreateTestPubKeys(1)
simapp.AddTestAddrsFromPubKeys(app, ctx, pks, app.StakingKeeper.TokensFromConsensusPower(ctx, 200))
simtestutil.AddTestAddrsFromPubKeys(bankKeeper, stakingKeeper, ctx, pks, stakingKeeper.TokensFromConsensusPower(ctx, 200))
addr, pk := sdk.ValAddress(pks[0].Address()), pks[0]
tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper)
tstaking := teststaking.NewHelper(t, ctx, stakingKeeper)

// bond the validator
power := int64(100)
amt := tstaking.CreateValidatorWithValPower(addr, pk, power, true)
staking.EndBlocker(ctx, app.StakingKeeper)
staking.EndBlocker(ctx, stakingKeeper)
require.Equal(
t, app.BankKeeper.GetAllBalances(ctx, sdk.AccAddress(addr)),
sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.GetParams(ctx).BondDenom, InitTokens.Sub(amt))),
t, bankKeeper.GetAllBalances(ctx, sdk.AccAddress(addr)),
sdk.NewCoins(sdk.NewCoin(stakingKeeper.GetParams(ctx).BondDenom, InitTokens.Sub(amt))),
)
require.Equal(t, amt, app.StakingKeeper.Validator(ctx, addr).GetBondedTokens())
require.Equal(t, amt, stakingKeeper.Validator(ctx, addr).GetBondedTokens())

val := abci.Validator{
Address: pk.Address(),
Expand All @@ -51,9 +68,9 @@ func TestBeginBlocker(t *testing.T) {
},
}

slashing.BeginBlocker(ctx, req, app.SlashingKeeper)
slashing.BeginBlocker(ctx, req, slashingKeeper)

info, found := app.SlashingKeeper.GetValidatorSigningInfo(ctx, sdk.ConsAddress(pk.Address()))
info, found := slashingKeeper.GetValidatorSigningInfo(ctx, sdk.ConsAddress(pk.Address()))
require.True(t, found)
require.Equal(t, ctx.BlockHeight(), info.StartHeight)
require.Equal(t, int64(1), info.IndexOffset)
Expand All @@ -63,7 +80,7 @@ func TestBeginBlocker(t *testing.T) {
height := int64(0)

// for 1000 blocks, mark the validator as having signed
for ; height < app.SlashingKeeper.SignedBlocksWindow(ctx); height++ {
for ; height < slashingKeeper.SignedBlocksWindow(ctx); height++ {
ctx = ctx.WithBlockHeight(height)
req = abci.RequestBeginBlock{
LastCommitInfo: abci.LastCommitInfo{
Expand All @@ -74,11 +91,11 @@ func TestBeginBlocker(t *testing.T) {
},
}

slashing.BeginBlocker(ctx, req, app.SlashingKeeper)
slashing.BeginBlocker(ctx, req, slashingKeeper)
}

// for 500 blocks, mark the validator as having not signed
for ; height < ((app.SlashingKeeper.SignedBlocksWindow(ctx) * 2) - app.SlashingKeeper.MinSignedPerWindow(ctx) + 1); height++ {
for ; height < ((slashingKeeper.SignedBlocksWindow(ctx) * 2) - slashingKeeper.MinSignedPerWindow(ctx) + 1); height++ {
ctx = ctx.WithBlockHeight(height)
req = abci.RequestBeginBlock{
LastCommitInfo: abci.LastCommitInfo{
Expand All @@ -89,14 +106,14 @@ func TestBeginBlocker(t *testing.T) {
},
}

slashing.BeginBlocker(ctx, req, app.SlashingKeeper)
slashing.BeginBlocker(ctx, req, slashingKeeper)
}

// end block
staking.EndBlocker(ctx, app.StakingKeeper)
staking.EndBlocker(ctx, stakingKeeper)

// validator should be jailed
validator, found := app.StakingKeeper.GetValidatorByConsAddr(ctx, sdk.GetConsAddress(pk))
validator, found := stakingKeeper.GetValidatorByConsAddr(ctx, sdk.GetConsAddress(pk))
require.True(t, found)
require.Equal(t, stakingtypes.Unbonding, validator.GetStatus())
}
45 changes: 30 additions & 15 deletions x/slashing/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,67 @@ import (
"github.com/stretchr/testify/require"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

"github.com/cosmos/cosmos-sdk/simapp"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
"github.com/cosmos/cosmos-sdk/x/slashing/testslashing"
"github.com/cosmos/cosmos-sdk/x/slashing/testutil"
"github.com/cosmos/cosmos-sdk/x/slashing/types"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
)

func TestExportAndInitGenesis(t *testing.T) {
app := simapp.Setup(t, false)
var slashingKeeper slashingkeeper.Keeper
var stakingKeeper *stakingkeeper.Keeper
var bankKeeper bankkeeper.Keeper

app, err := simtestutil.Setup(
testutil.AppConfig,
&slashingKeeper,
&stakingKeeper,
&bankKeeper,
)
require.NoError(t, err)

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

app.SlashingKeeper.SetParams(ctx, testslashing.TestParams())
slashingKeeper.SetParams(ctx, testslashing.TestParams())

addrDels := simapp.AddTestAddrsIncremental(app, ctx, 2, app.StakingKeeper.TokensFromConsensusPower(ctx, 200))
addrDels := simtestutil.AddTestAddrsIncremental(bankKeeper, stakingKeeper, ctx, 2, stakingKeeper.TokensFromConsensusPower(ctx, 200))

info1 := types.NewValidatorSigningInfo(sdk.ConsAddress(addrDels[0]), int64(4), int64(3),
time.Now().UTC().Add(100000000000), false, int64(10))
info2 := types.NewValidatorSigningInfo(sdk.ConsAddress(addrDels[1]), int64(5), int64(4),
time.Now().UTC().Add(10000000000), false, int64(10))

app.SlashingKeeper.SetValidatorSigningInfo(ctx, sdk.ConsAddress(addrDels[0]), info1)
app.SlashingKeeper.SetValidatorSigningInfo(ctx, sdk.ConsAddress(addrDels[1]), info2)
genesisState := app.SlashingKeeper.ExportGenesis(ctx)
slashingKeeper.SetValidatorSigningInfo(ctx, sdk.ConsAddress(addrDels[0]), info1)
slashingKeeper.SetValidatorSigningInfo(ctx, sdk.ConsAddress(addrDels[1]), info2)
genesisState := slashingKeeper.ExportGenesis(ctx)

require.Equal(t, genesisState.Params, testslashing.TestParams())
require.Len(t, genesisState.SigningInfos, 2)
require.Equal(t, genesisState.SigningInfos[0].ValidatorSigningInfo, info1)

// Tombstone validators after genesis shouldn't effect genesis state
app.SlashingKeeper.Tombstone(ctx, sdk.ConsAddress(addrDels[0]))
app.SlashingKeeper.Tombstone(ctx, sdk.ConsAddress(addrDels[1]))
slashingKeeper.Tombstone(ctx, sdk.ConsAddress(addrDels[0]))
slashingKeeper.Tombstone(ctx, sdk.ConsAddress(addrDels[1]))

ok := app.SlashingKeeper.IsTombstoned(ctx, sdk.ConsAddress(addrDels[0]))
ok := slashingKeeper.IsTombstoned(ctx, sdk.ConsAddress(addrDels[0]))
require.True(t, ok)

newInfo1, ok := app.SlashingKeeper.GetValidatorSigningInfo(ctx, sdk.ConsAddress(addrDels[0]))
newInfo1, ok := slashingKeeper.GetValidatorSigningInfo(ctx, sdk.ConsAddress(addrDels[0]))
require.NotEqual(t, info1, newInfo1)
// Initialise genesis with genesis state before tombstone

app.SlashingKeeper.InitGenesis(ctx, app.StakingKeeper, genesisState)
slashingKeeper.InitGenesis(ctx, stakingKeeper, genesisState)

// Validator isTombstoned should return false as GenesisState is initialised
ok = app.SlashingKeeper.IsTombstoned(ctx, sdk.ConsAddress(addrDels[0]))
ok = slashingKeeper.IsTombstoned(ctx, sdk.ConsAddress(addrDels[0]))
require.False(t, ok)

newInfo1, ok = app.SlashingKeeper.GetValidatorSigningInfo(ctx, sdk.ConsAddress(addrDels[0]))
newInfo2, ok := app.SlashingKeeper.GetValidatorSigningInfo(ctx, sdk.ConsAddress(addrDels[1]))
newInfo1, ok = slashingKeeper.GetValidatorSigningInfo(ctx, sdk.ConsAddress(addrDels[0]))
newInfo2, ok := slashingKeeper.GetValidatorSigningInfo(ctx, sdk.ConsAddress(addrDels[1]))
require.True(t, ok)
require.Equal(t, info1, newInfo1)
require.Equal(t, info2, newInfo2)
Expand Down
60 changes: 5 additions & 55 deletions x/slashing/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,76 +2,30 @@ package keeper_test

import (
gocontext "context"
"testing"
"time"

"github.com/stretchr/testify/suite"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/slashing/testslashing"
"github.com/cosmos/cosmos-sdk/x/slashing/types"
)

type SlashingTestSuite struct {
suite.Suite

app *simapp.SimApp
ctx sdk.Context
queryClient types.QueryClient
addrDels []sdk.AccAddress
}

func (suite *SlashingTestSuite) SetupTest() {
app := simapp.Setup(suite.T(), false)
ctx := app.BaseApp.NewContext(false, tmproto.Header{})

app.AccountKeeper.SetParams(ctx, authtypes.DefaultParams())
app.BankKeeper.SetParams(ctx, banktypes.DefaultParams())
app.SlashingKeeper.SetParams(ctx, testslashing.TestParams())

addrDels := simapp.AddTestAddrsIncremental(app, ctx, 2, app.StakingKeeper.TokensFromConsensusPower(ctx, 200))

info1 := types.NewValidatorSigningInfo(sdk.ConsAddress(addrDels[0]), int64(4), int64(3),
time.Unix(2, 0), false, int64(10))
info2 := types.NewValidatorSigningInfo(sdk.ConsAddress(addrDels[1]), int64(5), int64(4),
time.Unix(2, 0), false, int64(10))

app.SlashingKeeper.SetValidatorSigningInfo(ctx, sdk.ConsAddress(addrDels[0]), info1)
app.SlashingKeeper.SetValidatorSigningInfo(ctx, sdk.ConsAddress(addrDels[1]), info2)

suite.app = app
suite.ctx = ctx
suite.addrDels = addrDels

queryHelper := baseapp.NewQueryServerTestHelper(ctx, app.InterfaceRegistry())
types.RegisterQueryServer(queryHelper, app.SlashingKeeper)
queryClient := types.NewQueryClient(queryHelper)
suite.queryClient = queryClient
}

func (suite *SlashingTestSuite) TestGRPCQueryParams() {
func (suite *KeeperTestSuite) TestGRPCQueryParams() {
queryClient := suite.queryClient
paramsResp, err := queryClient.Params(gocontext.Background(), &types.QueryParamsRequest{})

suite.NoError(err)
suite.Equal(testslashing.TestParams(), paramsResp.Params)
}

func (suite *SlashingTestSuite) TestGRPCSigningInfo() {
func (suite *KeeperTestSuite) TestGRPCSigningInfo() {
queryClient := suite.queryClient

infoResp, err := queryClient.SigningInfo(gocontext.Background(), &types.QuerySigningInfoRequest{ConsAddress: ""})
suite.Error(err)
suite.Nil(infoResp)

consAddr := sdk.ConsAddress(suite.addrDels[0])
info, found := suite.app.SlashingKeeper.GetValidatorSigningInfo(suite.ctx, consAddr)
info, found := suite.slashingKeeper.GetValidatorSigningInfo(suite.ctx, consAddr)
suite.True(found)

infoResp, err = queryClient.SigningInfo(gocontext.Background(),
Expand All @@ -80,12 +34,12 @@ func (suite *SlashingTestSuite) TestGRPCSigningInfo() {
suite.Equal(info, infoResp.ValSigningInfo)
}

func (suite *SlashingTestSuite) TestGRPCSigningInfos() {
func (suite *KeeperTestSuite) TestGRPCSigningInfos() {
queryClient := suite.queryClient

var signingInfos []types.ValidatorSigningInfo

suite.app.SlashingKeeper.IterateValidatorSigningInfos(suite.ctx, func(consAddr sdk.ConsAddress, info types.ValidatorSigningInfo) (stop bool) {
suite.slashingKeeper.IterateValidatorSigningInfos(suite.ctx, func(consAddr sdk.ConsAddress, info types.ValidatorSigningInfo) (stop bool) {
signingInfos = append(signingInfos, info)
return false
})
Expand All @@ -104,7 +58,3 @@ func (suite *SlashingTestSuite) TestGRPCSigningInfos() {
suite.NotNil(infoResp.Pagination.NextKey)
suite.Equal(uint64(2), infoResp.Pagination.Total)
}

func TestSlashingTestSuite(t *testing.T) {
suite.Run(t, new(SlashingTestSuite))
}
Loading

0 comments on commit 69c9e3e

Please sign in to comment.