2019-07-01 09:50:04 -07:00
|
|
|
package crisis_test
|
2019-03-28 16:27:47 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2019-04-04 07:36:39 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-06-26 09:03:25 -07:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2019-08-27 06:19:26 -07:00
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
|
|
|
dbm "github.com/tendermint/tm-db"
|
2019-04-04 07:36:39 -07:00
|
|
|
|
2019-08-27 06:19:26 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/simapp"
|
2019-03-28 16:27:47 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-07-01 09:50:04 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/crisis"
|
2019-03-28 16:27:47 -07:00
|
|
|
distr "github.com/cosmos/cosmos-sdk/x/distribution"
|
2019-08-27 06:19:26 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/supply"
|
2019-03-28 16:27:47 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
testModuleName = "dummy"
|
2019-07-11 03:56:43 -07:00
|
|
|
dummyRouteWhichPasses = crisis.NewInvarRoute(testModuleName, "which-passes", func(_ sdk.Context) (string, bool) { return "", false })
|
|
|
|
dummyRouteWhichFails = crisis.NewInvarRoute(testModuleName, "which-fails", func(_ sdk.Context) (string, bool) { return "whoops", true })
|
2019-03-28 16:27:47 -07:00
|
|
|
)
|
|
|
|
|
2019-08-27 06:19:26 -07:00
|
|
|
func createTestApp() (*simapp.SimApp, sdk.Context, []sdk.AccAddress) {
|
|
|
|
db := dbm.NewMemDB()
|
|
|
|
app := simapp.NewSimApp(log.NewNopLogger(), db, nil, true, 1)
|
|
|
|
ctx := app.NewContext(true, abci.Header{})
|
2019-03-28 16:27:47 -07:00
|
|
|
|
2019-08-27 06:19:26 -07:00
|
|
|
constantFee := sdk.NewInt64Coin(sdk.DefaultBondDenom, 10)
|
|
|
|
app.CrisisKeeper.SetConstantFee(ctx, constantFee)
|
|
|
|
app.StakingKeeper.SetParams(ctx, staking.DefaultParams())
|
2019-03-28 16:27:47 -07:00
|
|
|
|
2019-08-27 06:19:26 -07:00
|
|
|
app.CrisisKeeper.RegisterRoute(testModuleName, dummyRouteWhichPasses.Route, dummyRouteWhichPasses.Invar)
|
|
|
|
app.CrisisKeeper.RegisterRoute(testModuleName, dummyRouteWhichFails.Route, dummyRouteWhichFails.Invar)
|
2019-03-28 16:27:47 -07:00
|
|
|
|
|
|
|
feePool := distr.InitialFeePool()
|
|
|
|
feePool.CommunityPool = sdk.NewDecCoins(sdk.NewCoins(constantFee))
|
2019-08-27 06:19:26 -07:00
|
|
|
app.DistrKeeper.SetFeePool(ctx, feePool)
|
|
|
|
app.SupplyKeeper.SetSupply(ctx, supply.NewSupply(sdk.Coins{}))
|
|
|
|
|
|
|
|
addrs := simapp.AddTestAddrs(app, ctx, 1, sdk.NewInt(10000))
|
2019-03-28 16:27:47 -07:00
|
|
|
|
2019-08-27 06:19:26 -07:00
|
|
|
return app, ctx, addrs
|
2019-03-28 16:27:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//____________________________________________________________________________
|
|
|
|
|
2019-08-27 06:19:26 -07:00
|
|
|
func TestHandleMsgVerifyInvariant(t *testing.T) {
|
|
|
|
app, ctx, addrs := createTestApp()
|
2019-03-28 16:27:47 -07:00
|
|
|
sender := addrs[0]
|
|
|
|
|
2019-08-27 06:19:26 -07:00
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
msg sdk.Msg
|
|
|
|
expectedResult string
|
|
|
|
}{
|
|
|
|
{"bad invariant route", crisis.NewMsgVerifyInvariant(sender, testModuleName, "route-that-doesnt-exist"), "fail"},
|
|
|
|
{"invariant broken", crisis.NewMsgVerifyInvariant(sender, testModuleName, dummyRouteWhichFails.Route), "panic"},
|
|
|
|
{"invariant passing", crisis.NewMsgVerifyInvariant(sender, testModuleName, dummyRouteWhichPasses.Route), "pass"},
|
|
|
|
{"invalid msg", sdk.NewTestMsg(), "fail"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
2019-10-17 06:47:35 -07:00
|
|
|
tc := tc
|
2019-08-27 06:19:26 -07:00
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
h := crisis.NewHandler(app.CrisisKeeper)
|
|
|
|
|
|
|
|
switch tc.expectedResult {
|
|
|
|
case "fail":
|
2019-12-27 09:57:54 -08:00
|
|
|
res, err := h(ctx, tc.msg)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Nil(t, res)
|
|
|
|
|
2019-08-27 06:19:26 -07:00
|
|
|
case "pass":
|
2019-12-27 09:57:54 -08:00
|
|
|
res, err := h(ctx, tc.msg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, res)
|
|
|
|
|
2019-08-27 06:19:26 -07:00
|
|
|
case "panic":
|
|
|
|
require.Panics(t, func() {
|
2019-12-27 09:57:54 -08:00
|
|
|
h(ctx, tc.msg)
|
2019-08-27 06:19:26 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2019-03-28 16:27:47 -07:00
|
|
|
}
|
|
|
|
|
2019-08-27 06:19:26 -07:00
|
|
|
func TestHandleMsgVerifyInvariantWithNotEnoughSenderCoins(t *testing.T) {
|
|
|
|
app, ctx, addrs := createTestApp()
|
2019-03-28 16:27:47 -07:00
|
|
|
sender := addrs[0]
|
2019-08-27 06:19:26 -07:00
|
|
|
coin := app.AccountKeeper.GetAccount(ctx, sender).GetCoins()[0]
|
|
|
|
excessCoins := sdk.NewCoin(coin.Denom, coin.Amount.AddRaw(1))
|
|
|
|
app.CrisisKeeper.SetConstantFee(ctx, excessCoins)
|
2019-03-28 16:27:47 -07:00
|
|
|
|
2019-08-27 06:19:26 -07:00
|
|
|
h := crisis.NewHandler(app.CrisisKeeper)
|
|
|
|
msg := crisis.NewMsgVerifyInvariant(sender, testModuleName, dummyRouteWhichPasses.Route)
|
2019-12-27 09:57:54 -08:00
|
|
|
|
|
|
|
res, err := h(ctx, msg)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Nil(t, res)
|
2019-03-28 16:27:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandleMsgVerifyInvariantWithInvariantBrokenAndNotEnoughPoolCoins(t *testing.T) {
|
2019-08-27 06:19:26 -07:00
|
|
|
app, ctx, addrs := createTestApp()
|
2019-03-28 16:27:47 -07:00
|
|
|
sender := addrs[0]
|
|
|
|
|
|
|
|
// set the community pool to empty
|
2019-08-27 06:19:26 -07:00
|
|
|
feePool := app.DistrKeeper.GetFeePool(ctx)
|
2019-03-28 16:27:47 -07:00
|
|
|
feePool.CommunityPool = sdk.DecCoins{}
|
2019-08-27 06:19:26 -07:00
|
|
|
app.DistrKeeper.SetFeePool(ctx, feePool)
|
2019-03-28 16:27:47 -07:00
|
|
|
|
2019-08-27 06:19:26 -07:00
|
|
|
h := crisis.NewHandler(app.CrisisKeeper)
|
2019-07-01 09:50:04 -07:00
|
|
|
msg := crisis.NewMsgVerifyInvariant(sender, testModuleName, dummyRouteWhichFails.Route)
|
2019-12-27 09:57:54 -08:00
|
|
|
|
|
|
|
var res *sdk.Result
|
2019-03-28 16:27:47 -07:00
|
|
|
require.Panics(t, func() {
|
2019-12-27 09:57:54 -08:00
|
|
|
res, _ = h(ctx, msg)
|
2019-03-28 16:27:47 -07:00
|
|
|
}, fmt.Sprintf("%v", res))
|
|
|
|
}
|