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-08-27 06:19:26 -07:00
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
2020-08-14 10:58:53 -07:00
|
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
2019-08-27 06:19:26 -07:00
|
|
|
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"
|
2020-08-14 10:58:53 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/testutil/testdata"
|
2019-03-28 16:27:47 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-06-14 16:06:16 -07:00
|
|
|
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
|
2019-07-01 09:50:04 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/crisis"
|
2020-06-14 06:59:25 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/crisis/types"
|
2020-06-14 04:01:47 -07:00
|
|
|
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
|
2020-06-12 07:54:37 -07:00
|
|
|
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
2019-03-28 16:27:47 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
testModuleName = "dummy"
|
2020-06-14 06:59:25 -07:00
|
|
|
dummyRouteWhichPasses = types.NewInvarRoute(testModuleName, "which-passes", func(_ sdk.Context) (string, bool) { return "", false })
|
|
|
|
dummyRouteWhichFails = types.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()
|
2020-08-11 07:22:30 -07:00
|
|
|
app := simapp.NewSimApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, simapp.DefaultNodeHome, 1, simapp.MakeEncodingConfig())
|
2020-08-14 10:58:53 -07:00
|
|
|
ctx := app.NewContext(true, tmproto.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)
|
2020-06-12 07:54:37 -07:00
|
|
|
app.StakingKeeper.SetParams(ctx, stakingtypes.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
|
|
|
|
2020-06-14 04:01:47 -07:00
|
|
|
feePool := distrtypes.InitialFeePool()
|
2020-01-03 12:44:53 -08:00
|
|
|
feePool.CommunityPool = sdk.NewDecCoinsFromCoins(sdk.NewCoins(constantFee)...)
|
2019-08-27 06:19:26 -07:00
|
|
|
app.DistrKeeper.SetFeePool(ctx, feePool)
|
2020-06-14 16:06:16 -07:00
|
|
|
app.BankKeeper.SetSupply(ctx, banktypes.NewSupply(sdk.Coins{}))
|
2019-08-27 06:19:26 -07:00
|
|
|
|
|
|
|
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
|
|
|
|
}{
|
2020-06-14 06:59:25 -07:00
|
|
|
{"bad invariant route", types.NewMsgVerifyInvariant(sender, testModuleName, "route-that-doesnt-exist"), "fail"},
|
|
|
|
{"invariant broken", types.NewMsgVerifyInvariant(sender, testModuleName, dummyRouteWhichFails.Route), "panic"},
|
|
|
|
{"invariant passing", types.NewMsgVerifyInvariant(sender, testModuleName, dummyRouteWhichPasses.Route), "pass"},
|
2020-07-06 10:03:45 -07:00
|
|
|
{"invalid msg", testdata.NewTestMsg(), "fail"},
|
2019-08-27 06:19:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2020-03-20 12:24:18 -07:00
|
|
|
h(ctx, tc.msg) // nolint:errcheck
|
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]
|
2020-01-30 13:31:16 -08:00
|
|
|
coin := app.BankKeeper.GetAllBalances(ctx, sender)[0]
|
2019-08-27 06:19:26 -07:00
|
|
|
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)
|
2020-06-14 06:59:25 -07:00
|
|
|
msg := types.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)
|
2020-06-14 06:59:25 -07:00
|
|
|
msg := types.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))
|
|
|
|
}
|