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 crisis package using internal #4649

Merged
merged 5 commits into from
Jul 1, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ func NewSimApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest bo
app.supplyKeeper, distr.DefaultCodespace, auth.FeeCollectorName)
app.slashingKeeper = slashing.NewKeeper(app.cdc, app.keySlashing, &stakingKeeper,
slashingSubspace, slashing.DefaultCodespace)
app.crisisKeeper = crisis.NewKeeper(crisisSubspace, invCheckPeriod, app.supplyKeeper, auth.FeeCollectorName)
app.crisisKeeper = crisis.NewKeeper(crisisSubspace, invCheckPeriod, app.supplyKeeper,
auth.FeeCollectorName)

// register the proposal types
govRouter := gov.NewRouter()
Expand Down
2 changes: 1 addition & 1 deletion x/crisis/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

// check all registered invariants
func EndBlocker(ctx sdk.Context, k Keeper) {
if k.invCheckPeriod == 0 || ctx.BlockHeight()%int64(k.invCheckPeriod) != 0 {
if k.InvCheckPeriod() == 0 || ctx.BlockHeight()%int64(k.InvCheckPeriod()) != 0 {
// skip running the invariant check
return
}
Expand Down
5 changes: 4 additions & 1 deletion x/crisis/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
package crisis

import (
"github.com/cosmos/cosmos-sdk/x/crisis/types"
"github.com/cosmos/cosmos-sdk/x/crisis/internal/keeper"
"github.com/cosmos/cosmos-sdk/x/crisis/internal/types"
)

const (
Expand All @@ -25,6 +26,7 @@ var (
NewMsgVerifyInvariant = types.NewMsgVerifyInvariant
ParamKeyTable = types.ParamKeyTable
NewInvarRoute = types.NewInvarRoute
NewKeeper = keeper.NewKeeper

// variable aliases
ModuleCdc = types.ModuleCdc
Expand All @@ -35,4 +37,5 @@ type (
GenesisState = types.GenesisState
MsgVerifyInvariant = types.MsgVerifyInvariant
InvarRoute = types.InvarRoute
Keeper = keeper.Keeper
)
2 changes: 1 addition & 1 deletion x/crisis/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/auth/client/utils"
"github.com/cosmos/cosmos-sdk/x/crisis/types"
"github.com/cosmos/cosmos-sdk/x/crisis/internal/types"
)

// command to replace a delegator's withdrawal address
Expand Down
7 changes: 4 additions & 3 deletions x/crisis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package crisis

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/crisis/types"
"github.com/cosmos/cosmos-sdk/x/crisis/internal/keeper"
"github.com/cosmos/cosmos-sdk/x/crisis/internal/types"
)

// new crisis genesis
func InitGenesis(ctx sdk.Context, keeper Keeper, data types.GenesisState) {
func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, data types.GenesisState) {
keeper.SetConstantFee(ctx, data.ConstantFee)
}

// ExportGenesis returns a GenesisState for a given context and keeper.
func ExportGenesis(ctx sdk.Context, keeper Keeper) types.GenesisState {
func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) types.GenesisState {
constantFee := keeper.GetConstantFee(ctx)
return types.NewGenesisState(constantFee)
}
10 changes: 5 additions & 5 deletions x/crisis/handler.go → x/crisis/internal/keeper/handler.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package crisis
package keeper

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/crisis/types"
"github.com/cosmos/cosmos-sdk/x/crisis/internal/types"
)

// RouterKey
const RouterKey = ModuleName
const RouterKey = types.ModuleName

func NewHandler(k Keeper) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
ctx = ctx.WithEventManager(sdk.NewEventManager())

switch msg := msg.(type) {
case types.MsgVerifyInvariant:
return handleMsgVerifyInvariant(ctx, msg, k)
return HandleMsgVerifyInvariant(ctx, msg, k)

default:
errMsg := fmt.Sprintf("unrecognized crisis message type: %T", msg)
Expand All @@ -25,7 +25,7 @@ func NewHandler(k Keeper) sdk.Handler {
}
}

func handleMsgVerifyInvariant(ctx sdk.Context, msg types.MsgVerifyInvariant, k Keeper) sdk.Result {
func HandleMsgVerifyInvariant(ctx sdk.Context, msg types.MsgVerifyInvariant, k Keeper) sdk.Result {
// remove the constant fee
constantFee := sdk.NewCoins(k.GetConstantFee(ctx))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package crisis
package keeper_test

import (
"errors"
Expand All @@ -11,24 +11,26 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/crisis"
"github.com/cosmos/cosmos-sdk/x/crisis/internal/keeper"
distr "github.com/cosmos/cosmos-sdk/x/distribution"
)

var (
testModuleName = "dummy"
dummyRouteWhichPasses = NewInvarRoute(testModuleName, "which-passes", func(_ sdk.Context) error { return nil })
dummyRouteWhichFails = NewInvarRoute(testModuleName, "which-fails", func(_ sdk.Context) error { return errors.New("whoops") })
dummyRouteWhichPasses = crisis.NewInvarRoute(testModuleName, "which-passes", func(_ sdk.Context) error { return nil })
dummyRouteWhichFails = crisis.NewInvarRoute(testModuleName, "which-fails", func(_ sdk.Context) error { return errors.New("whoops") })
addrs = distr.TestAddrs
)

func CreateTestInput(t *testing.T) (sdk.Context, Keeper, auth.AccountKeeper, distr.Keeper) {
func CreateTestInput(t *testing.T) (sdk.Context, crisis.Keeper, auth.AccountKeeper, distr.Keeper) {

communityTax := sdk.NewDecWithPrec(2, 2)
ctx, accKeeper, _, distrKeeper, _, paramsKeeper, supplyKeeper :=
distr.CreateTestInputAdvanced(t, false, 10, communityTax)

paramSpace := paramsKeeper.Subspace(DefaultParamspace)
crisisKeeper := NewKeeper(paramSpace, 1, supplyKeeper, auth.FeeCollectorName)
paramSpace := paramsKeeper.Subspace(crisis.DefaultParamspace)
crisisKeeper := crisis.NewKeeper(paramSpace, 1, supplyKeeper, auth.FeeCollectorName)
constantFee := sdk.NewInt64Coin("stake", 10000000)
crisisKeeper.SetConstantFee(ctx, constantFee)

Expand All @@ -52,28 +54,28 @@ func TestHandleMsgVerifyInvariantWithNotEnoughSenderCoins(t *testing.T) {
excessCoins := sdk.NewCoin(coin.Denom, coin.Amount.AddRaw(1))
crisisKeeper.SetConstantFee(ctx, excessCoins)

msg := NewMsgVerifyInvariant(sender, testModuleName, dummyRouteWhichPasses.Route)
res := handleMsgVerifyInvariant(ctx, msg, crisisKeeper)
msg := crisis.NewMsgVerifyInvariant(sender, testModuleName, dummyRouteWhichPasses.Route)
res := keeper.HandleMsgVerifyInvariant(ctx, msg, crisisKeeper)
require.False(t, res.IsOK())
}

func TestHandleMsgVerifyInvariantWithBadInvariant(t *testing.T) {
ctx, crisisKeeper, _, _ := CreateTestInput(t)
sender := addrs[0]

msg := NewMsgVerifyInvariant(sender, testModuleName, "route-that-doesnt-exist")
res := handleMsgVerifyInvariant(ctx, msg, crisisKeeper)
msg := crisis.NewMsgVerifyInvariant(sender, testModuleName, "route-that-doesnt-exist")
res := keeper.HandleMsgVerifyInvariant(ctx, msg, crisisKeeper)
require.False(t, res.IsOK())
}

func TestHandleMsgVerifyInvariantWithInvariantBroken(t *testing.T) {
ctx, crisisKeeper, _, _ := CreateTestInput(t)
sender := addrs[0]

msg := NewMsgVerifyInvariant(sender, testModuleName, dummyRouteWhichFails.Route)
msg := crisis.NewMsgVerifyInvariant(sender, testModuleName, dummyRouteWhichFails.Route)
var res sdk.Result
require.Panics(t, func() {
res = handleMsgVerifyInvariant(ctx, msg, crisisKeeper)
res = keeper.HandleMsgVerifyInvariant(ctx, msg, crisisKeeper)
}, fmt.Sprintf("%v", res))
}

Expand All @@ -86,25 +88,25 @@ func TestHandleMsgVerifyInvariantWithInvariantBrokenAndNotEnoughPoolCoins(t *tes
feePool.CommunityPool = sdk.DecCoins{}
distrKeeper.SetFeePool(ctx, feePool)

msg := NewMsgVerifyInvariant(sender, testModuleName, dummyRouteWhichFails.Route)
msg := crisis.NewMsgVerifyInvariant(sender, testModuleName, dummyRouteWhichFails.Route)
var res sdk.Result
require.Panics(t, func() {
res = handleMsgVerifyInvariant(ctx, msg, crisisKeeper)
res = keeper.HandleMsgVerifyInvariant(ctx, msg, crisisKeeper)
}, fmt.Sprintf("%v", res))
}

func TestHandleMsgVerifyInvariantWithInvariantNotBroken(t *testing.T) {
ctx, crisisKeeper, _, _ := CreateTestInput(t)
sender := addrs[0]

msg := NewMsgVerifyInvariant(sender, testModuleName, dummyRouteWhichPasses.Route)
res := handleMsgVerifyInvariant(ctx, msg, crisisKeeper)
msg := crisis.NewMsgVerifyInvariant(sender, testModuleName, dummyRouteWhichPasses.Route)
res := keeper.HandleMsgVerifyInvariant(ctx, msg, crisisKeeper)
require.True(t, res.IsOK())
}

func TestInvalidMsg(t *testing.T) {
k := Keeper{}
h := NewHandler(k)
k := crisis.Keeper{}
h := keeper.NewHandler(k)

res := h(sdk.NewContext(nil, abci.Header{}, false, nil), sdk.NewTestMsg())
require.False(t, res.IsOK())
Expand Down
7 changes: 5 additions & 2 deletions x/crisis/keeper.go → x/crisis/internal/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package crisis
package keeper

import (
"fmt"
Expand All @@ -7,7 +7,7 @@ import (
"github.com/tendermint/tendermint/libs/log"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/crisis/types"
"github.com/cosmos/cosmos-sdk/x/crisis/internal/types"
"github.com/cosmos/cosmos-sdk/x/params"
)

Expand Down Expand Up @@ -83,4 +83,7 @@ func (k Keeper) AssertInvariants(ctx sdk.Context) {
logger.Info("asserted all invariants", "duration", diff, "height", ctx.BlockHeight())
}

// InvCheckPeriod returns the invariant checks period.
func (k Keeper) InvCheckPeriod() uint { return k.invCheckPeriod }

// DONTCOVER
4 changes: 2 additions & 2 deletions x/crisis/params.go → x/crisis/internal/keeper/params.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package crisis
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/crisis/types"
"github.com/cosmos/cosmos-sdk/x/crisis/internal/types"
)

// GetConstantFee get's the constant fee from the paramSpace
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
11 changes: 6 additions & 5 deletions x/crisis/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/crisis/client/cli"
"github.com/cosmos/cosmos-sdk/x/crisis/types"
"github.com/cosmos/cosmos-sdk/x/crisis/internal/keeper"
"github.com/cosmos/cosmos-sdk/x/crisis/internal/types"
)

var (
Expand Down Expand Up @@ -65,11 +66,11 @@ func (AppModuleBasic) GetQueryCmd(_ *codec.Codec) *cobra.Command { return nil }
// app module for bank
type AppModule struct {
AppModuleBasic
keeper Keeper
keeper keeper.Keeper
}

// NewAppModule creates a new AppModule object
func NewAppModule(keeper Keeper) AppModule {
func NewAppModule(keeper keeper.Keeper) AppModule {
return AppModule{
AppModuleBasic: AppModuleBasic{},
keeper: keeper,
Expand All @@ -86,12 +87,12 @@ func (AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {}

// module querier route name
func (AppModule) Route() string {
return RouterKey
return keeper.RouterKey
}

// module handler
func (am AppModule) NewHandler() sdk.Handler {
return NewHandler(am.keeper)
return keeper.NewHandler(am.keeper)
}

// module querier route name
Expand Down