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 all 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 .pending/breaking/sdk/4649-Refactor-x-cris
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#4649 Refactor x/crisis as per modules new specs.
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)
}
14 changes: 7 additions & 7 deletions x/crisis/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ 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/keeper"
"github.com/cosmos/cosmos-sdk/x/crisis/internal/types"
)

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

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

Expand All @@ -25,12 +26,11 @@ 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.Keeper) sdk.Result {
// remove the constant fee
constantFee := sdk.NewCoins(k.GetConstantFee(ctx))

err := k.supplyKeeper.SendCoinsFromAccountToModule(ctx, msg.Sender, k.feeCollectorName, constantFee)
if err != nil {
if err := k.SendCoinsFromAccountToFeeCollector(ctx, msg.Sender, constantFee); err != nil {
return err.Result()
}

Expand All @@ -41,7 +41,7 @@ func handleMsgVerifyInvariant(ctx sdk.Context, msg types.MsgVerifyInvariant, k K
msgFullRoute := msg.FullInvariantRoute()

var invarianceErr error
for _, invarRoute := range k.routes {
for _, invarRoute := range k.Routes() {
if invarRoute.FullRoute() == msgFullRoute {
invarianceErr = invarRoute.Invar(cacheCtx)
found = true
Expand Down
44 changes: 24 additions & 20 deletions x/crisis/handler_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package crisis
package crisis_test

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

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/crisis"
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 +53,30 @@ 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)
require.False(t, res.IsOK())
h := crisis.NewHandler(crisisKeeper)
msg := crisis.NewMsgVerifyInvariant(sender, testModuleName, dummyRouteWhichPasses.Route)
require.False(t, h(ctx, msg).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)
h := crisis.NewHandler(crisisKeeper)
msg := crisis.NewMsgVerifyInvariant(sender, testModuleName, "route-that-doesnt-exist")
res := h(ctx, msg)
require.False(t, res.IsOK())
}

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

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

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

msg := NewMsgVerifyInvariant(sender, testModuleName, dummyRouteWhichFails.Route)
h := crisis.NewHandler(crisisKeeper)
msg := crisis.NewMsgVerifyInvariant(sender, testModuleName, dummyRouteWhichFails.Route)
var res sdk.Result
require.Panics(t, func() {
res = handleMsgVerifyInvariant(ctx, msg, crisisKeeper)
res = h(ctx, msg)
}, 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)
require.True(t, res.IsOK())
h := crisis.NewHandler(crisisKeeper)
msg := crisis.NewMsgVerifyInvariant(sender, testModuleName, dummyRouteWhichPasses.Route)
require.True(t, h(ctx, msg).IsOK())
}

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

res := h(sdk.NewContext(nil, abci.Header{}, false, nil), sdk.NewTestMsg())
require.False(t, res.IsOK())
Expand Down
12 changes: 10 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,12 @@ 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 }

// SendCoinsFromAccountToFeeCollector transfers amt to the fee collector account.
func (k Keeper) SendCoinsFromAccountToFeeCollector(ctx sdk.Context, senderAddr sdk.AccAddress, amt sdk.Coins) sdk.Error {
return k.supplyKeeper.SendCoinsFromAccountToModule(ctx, senderAddr, k.feeCollectorName, amt)
}

// 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.
7 changes: 4 additions & 3 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 Down