Skip to content

Commit

Permalink
fix evidence integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
akhilkumarpilli committed Dec 2, 2024
1 parent 5bc66c3 commit 243eb86
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 67 deletions.
2 changes: 1 addition & 1 deletion tests/integration/v2/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,13 @@ func NewApp(
"minimum-gas-prices": "0stake",
},
},
services.NewGenesisHeaderService(stf.HeaderService{}),
cometService,
kvFactory,
&eventService{},
storeBuilder,
startupConfig.BranchService,
startupConfig.RouterServiceBuilder,
startupConfig.HeaderService,
),
depinject.Invoke(
std.RegisterInterfaces,
Expand Down
157 changes: 157 additions & 0 deletions tests/integration/v2/evidence/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package evidence

import (
"context"
"fmt"
"testing"
"time"

"github.com/stretchr/testify/suite"

"cosmossdk.io/x/evidence"
"cosmossdk.io/x/evidence/exported"
"cosmossdk.io/x/evidence/keeper"
"cosmossdk.io/x/evidence/types"

"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
)

type GenesisTestSuite struct {
suite.Suite

ctx context.Context
keeper keeper.Keeper
}

func (suite *GenesisTestSuite) SetupTest() {
f := initFixture(suite.T())

suite.ctx = f.ctx
suite.keeper = f.evidenceKeeper
}

func (suite *GenesisTestSuite) TestInitGenesis() {
var (
genesisState *types.GenesisState
testEvidence []exported.Evidence
pk = ed25519.GenPrivKey()
)

testCases := []struct {
msg string
malleate func()
expPass bool
posttests func()
}{
{
"valid",
func() {
testEvidence = make([]exported.Evidence, 100)
for i := 0; i < 100; i++ {
testEvidence[i] = &types.Equivocation{
Height: int64(i + 1),
Power: 100,
Time: time.Now().UTC(),
ConsensusAddress: pk.PubKey().Address().String(),
}
}
genesisState = types.NewGenesisState(testEvidence)
},
true,
func() {
for _, e := range testEvidence {
_, err := suite.keeper.Evidences.Get(suite.ctx, e.Hash())
suite.NoError(err)
}
},
},
{
"invalid",
func() {
testEvidence = make([]exported.Evidence, 100)
for i := 0; i < 100; i++ {
testEvidence[i] = &types.Equivocation{
Power: 100,
Time: time.Now().UTC(),
ConsensusAddress: pk.PubKey().Address().String(),
}
}
genesisState = types.NewGenesisState(testEvidence)
},
false,
func() {
_, err := suite.keeper.Evidences.Iterate(suite.ctx, nil)
suite.Require().NoError(err)
},
},
}

for _, tc := range testCases {
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
suite.SetupTest()

tc.malleate()

if tc.expPass {
err := evidence.InitGenesis(suite.ctx, suite.keeper, genesisState)
suite.NoError(err)
} else {
err := evidence.InitGenesis(suite.ctx, suite.keeper, genesisState)
suite.Error(err)

}

tc.posttests()
})
}
}

func (suite *GenesisTestSuite) TestExportGenesis() {
pk := ed25519.GenPrivKey()

testCases := []struct {
msg string
malleate func()
expPass bool
posttests func()
}{
{
"success",
func() {
ev := &types.Equivocation{
Height: 1,
Power: 100,
Time: time.Now().UTC(),
ConsensusAddress: pk.PubKey().Address().String(),
}
suite.Require().NoError(suite.keeper.Evidences.Set(suite.ctx, ev.Hash(), ev))
},
true,
func() {},
},
}

for _, tc := range testCases {
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
suite.SetupTest()

tc.malleate()

if tc.expPass {

_, err := evidence.ExportGenesis(suite.ctx, suite.keeper)
suite.Require().NoError(err)

} else {
_, err := evidence.ExportGenesis(suite.ctx, suite.keeper)
suite.Require().Error(err)
}

tc.posttests()
})
}
}

func TestGenesisTestSuite(t *testing.T) {
suite.Run(t, new(GenesisTestSuite))
}
98 changes: 32 additions & 66 deletions tests/integration/v2/evidence/infraction_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package evidence_test
package evidence

import (
"bytes"
Expand All @@ -14,12 +14,10 @@ import (
"cosmossdk.io/core/comet"
corecontext "cosmossdk.io/core/context"
"cosmossdk.io/core/header"
"cosmossdk.io/core/router"
"cosmossdk.io/core/transaction"
"cosmossdk.io/depinject"
"cosmossdk.io/log"
bankkeeper "cosmossdk.io/x/bank/keeper"
banktypes "cosmossdk.io/x/bank/types"
consensuskeeper "cosmossdk.io/x/consensus/keeper"
"cosmossdk.io/x/evidence/exported"
"cosmossdk.io/x/evidence/keeper"
evidencetypes "cosmossdk.io/x/evidence/types"
Expand All @@ -29,8 +27,8 @@ import (
stakingkeeper "cosmossdk.io/x/staking/keeper"
stakingtestutil "cosmossdk.io/x/staking/testutil"
stakingtypes "cosmossdk.io/x/staking/types"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"

"cosmossdk.io/runtime/v2"
"cosmossdk.io/runtime/v2/services"
_ "cosmossdk.io/x/accounts" // import as blank for app wiring
_ "cosmossdk.io/x/bank" // import as blank for app wiring
Expand Down Expand Up @@ -77,11 +75,12 @@ type fixture struct {
ctx context.Context
cdc codec.Codec

accountKeeper authkeeper.AccountKeeper
bankKeeper bankkeeper.Keeper
evidenceKeeper keeper.Keeper
slashingKeeper slashingkeeper.Keeper
stakingKeeper *stakingkeeper.Keeper
accountKeeper authkeeper.AccountKeeper
bankKeeper bankkeeper.Keeper
evidenceKeeper keeper.Keeper
slashingKeeper slashingkeeper.Keeper
stakingKeeper *stakingkeeper.Keeper
consensusKeeper consensuskeeper.Keeper
}

func initFixture(t *testing.T) *fixture {
Expand All @@ -102,59 +101,22 @@ func initFixture(t *testing.T) *fixture {
configurator.GenutilModule(),
}

var err error
startupCfg := integration.DefaultStartUpConfig(t)

msgRouterService := integration.NewRouterService()
res.registerMsgRouterService(msgRouterService)

var routerFactory runtime.RouterServiceFactory = func(_ []byte) router.Service {
return msgRouterService
}

queryRouterService := integration.NewRouterService()
res.registerQueryRouterService(queryRouterService)

serviceBuilder := runtime.NewRouterBuilder(routerFactory, queryRouterService)

startupCfg.BranchService = &integration.BranchService{}
startupCfg.RouterServiceBuilder = serviceBuilder
startupCfg.HeaderService = &integration.HeaderService{}

var err error
res.app, err = integration.NewApp(
depinject.Configs(configurator.NewAppV2Config(moduleConfigs...), depinject.Supply(log.NewNopLogger())),
startupCfg,
&res.bankKeeper, &res.accountKeeper, &res.stakingKeeper, &res.slashingKeeper, &res.evidenceKeeper, &res.cdc)
&res.bankKeeper, &res.accountKeeper, &res.stakingKeeper, &res.slashingKeeper, &res.evidenceKeeper, &res.consensusKeeper, &res.cdc)
require.NoError(t, err)

router := evidencetypes.NewRouter()
router = router.AddRoute(evidencetypes.RouteEquivocation, testEquivocationHandler(res.evidenceKeeper))
res.evidenceKeeper.SetRouter(router)

res.ctx = res.app.StateLatestContext(t)

return &res
}

func (s *fixture) registerMsgRouterService(router *integration.RouterService) {
// register custom router service
bankSendHandler := func(ctx context.Context, req transaction.Msg) (transaction.Msg, error) {
msg, ok := req.(*banktypes.MsgSend)
if !ok {
return nil, integration.ErrInvalidMsgType
}
msgServer := bankkeeper.NewMsgServerImpl(s.bankKeeper)
resp, err := msgServer.Send(ctx, msg)
return resp, err
}

router.RegisterHandler(bankSendHandler, "cosmos.bank.v1beta1.MsgSend")
}

func (s *fixture) registerQueryRouterService(router *integration.RouterService) {
// register custom router service
}

func TestHandleDoubleSign(t *testing.T) {
t.Parallel()
f := initFixture(t)
Expand Down Expand Up @@ -185,7 +147,7 @@ func TestHandleDoubleSign(t *testing.T) {

consaddrStr, err := f.stakingKeeper.ConsensusAddressCodec().BytesToString(valpubkey.Address())
assert.NilError(t, err)
height := f.app.LastBlockHeight() + 1
height := f.app.LastBlockHeight()
info := slashingtypes.NewValidatorSigningInfo(consaddrStr, int64(height), time.Unix(0, 0), false, int64(0))
err = f.slashingKeeper.ValidatorSigningInfo.Set(f.ctx, sdk.ConsAddress(valpubkey.Address()), info)
assert.NilError(t, err)
Expand Down Expand Up @@ -280,18 +242,22 @@ func TestHandleDoubleSign_TooOld(t *testing.T) {
assert.NilError(t, err)
assert.DeepEqual(t, amt, val.GetBondedTokens())

// nci := comet.Info{Evidence: []comet.Evidence{{
// Validator: comet.Validator{Address: valpubkey.Address(), Power: power},
// Type: comet.DuplicateVote, //
// Time: ctx.HeaderInfo().Time,
// Height: 0,
// }}}
nci := comet.Info{Evidence: []comet.Evidence{{
Validator: comet.Validator{Address: valpubkey.Address(), Power: power},
Type: comet.DuplicateVote, //
Time: integration.HeaderInfoFromContext(ctx).Time,
Height: 0,
}}}

// cp := simtestutil.DefaultConsensusParams
require.NotNil(t, f.consensusKeeper.ParamsStore)
require.NoError(t, f.consensusKeeper.ParamsStore.Set(ctx, *simtestutil.DefaultConsensusParams))
cp, err := f.consensusKeeper.ParamsStore.Get(ctx)

// ctx = ctx.WithCometInfo(nci)
// ctx = ctx.WithConsensusParams(*simtestutil.DefaultConsensusParams)
// ctx = ctx.WithHeaderInfo(header.Info{Height: ctx.BlockHeight() + cp.Evidence.MaxAgeNumBlocks + 1, Time: ctx.HeaderInfo().Time.Add(cp.Evidence.MaxAgeDuration + 1)})
ctx = context.WithValue(ctx, corecontext.CometInfoKey, nci)
ctx = integration.SetHeaderInfo(ctx, header.Info{
Height: int64(f.app.LastBlockHeight()) + cp.Evidence.MaxAgeNumBlocks + 1,
Time: integration.HeaderInfoFromContext(ctx).Time.Add(cp.Evidence.MaxAgeDuration + 1),
})

assert.NilError(t, f.evidenceKeeper.BeginBlocker(ctx, cometInfoService))

Expand All @@ -305,8 +271,7 @@ func TestHandleDoubleSignAfterRotation(t *testing.T) {
t.Parallel()
f := initFixture(t)

sdkCtx := sdk.UnwrapSDKContext(f.ctx)
ctx := sdkCtx.WithIsCheckTx(false).WithBlockHeight(1).WithHeaderInfo(header.Info{Time: time.Now()})
ctx := integration.SetHeaderInfo(f.ctx, header.Info{Time: time.Now()})
populateValidators(t, f)

power := int64(100)
Expand Down Expand Up @@ -369,7 +334,9 @@ func TestHandleDoubleSignAfterRotation(t *testing.T) {
}},
}

err = f.evidenceKeeper.BeginBlocker(ctx.WithCometInfo(nci), cometInfoService)
ctxWithCometInfo := context.WithValue(ctx, corecontext.CometInfoKey, nci)

err = f.evidenceKeeper.BeginBlocker(ctxWithCometInfo, cometInfoService)
assert.NilError(t, err)

// should be jailed and tombstoned
Expand All @@ -385,7 +352,7 @@ func TestHandleDoubleSignAfterRotation(t *testing.T) {
assert.Assert(t, newTokens.LT(oldTokens))

// submit duplicate evidence
err = f.evidenceKeeper.BeginBlocker(ctx.WithCometInfo(nci), cometInfoService)
err = f.evidenceKeeper.BeginBlocker(ctxWithCometInfo, cometInfoService)
assert.NilError(t, err)

// tokens should be the same (capped slash)
Expand All @@ -394,13 +361,12 @@ func TestHandleDoubleSignAfterRotation(t *testing.T) {
assert.Assert(t, valInfo.GetTokens().Equal(newTokens))

// jump to past the unbonding period
ctx = ctx.WithHeaderInfo(header.Info{Time: time.Unix(1, 0).Add(stakingParams.UnbondingTime)})
ctx = integration.SetHeaderInfo(ctx, header.Info{Time: time.Unix(1, 0).Add(stakingParams.UnbondingTime)})

// require we cannot unjail
assert.Error(t, f.slashingKeeper.Unjail(ctx, operatorAddr), slashingtypes.ErrValidatorJailed.Error())

// require we be able to unbond now
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1)
del, _ := f.stakingKeeper.Delegations.Get(ctx, collections.Join(sdk.AccAddress(operatorAddr), operatorAddr))
validator, _ := f.stakingKeeper.GetValidator(ctx, operatorAddr)
totalBond := validator.TokensFromShares(del.GetShares()).TruncateInt()
Expand Down
8 changes: 8 additions & 0 deletions tests/integration/v2/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ func SetHeaderInfo(ctx context.Context, h header.Info) context.Context {
return ctx
}

func HeaderInfoFromContext(ctx context.Context) header.Info {
iCtx, ok := ctx.Value(contextKey).(*integrationContext)
if ok {
return iCtx.header
}
return header.Info{}
}

func GasMeterFromContext(ctx context.Context) gas.Meter {
iCtx, ok := ctx.Value(contextKey).(*integrationContext)
if !ok {
Expand Down

0 comments on commit 243eb86

Please sign in to comment.