forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_test.go
115 lines (91 loc) · 3.94 KB
/
handler_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package crisis_test
import (
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
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 = 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 })
addrs = distr.TestAddrs
)
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(crisis.DefaultParamspace)
crisisKeeper := crisis.NewKeeper(paramSpace, 1, supplyKeeper, auth.FeeCollectorName)
constantFee := sdk.NewInt64Coin("stake", 10000000)
crisisKeeper.SetConstantFee(ctx, constantFee)
crisisKeeper.RegisterRoute(testModuleName, dummyRouteWhichPasses.Route, dummyRouteWhichPasses.Invar)
crisisKeeper.RegisterRoute(testModuleName, dummyRouteWhichFails.Route, dummyRouteWhichFails.Invar)
// set the community pool to pay back the constant fee
feePool := distr.InitialFeePool()
feePool.CommunityPool = sdk.NewDecCoins(sdk.NewCoins(constantFee))
distrKeeper.SetFeePool(ctx, feePool)
return ctx, crisisKeeper, accKeeper, distrKeeper
}
//____________________________________________________________________________
func TestHandleMsgVerifyInvariantWithNotEnoughSenderCoins(t *testing.T) {
ctx, crisisKeeper, accKeeper, _ := CreateTestInput(t)
sender := addrs[0]
coin := accKeeper.GetAccount(ctx, sender).GetCoins()[0]
excessCoins := sdk.NewCoin(coin.Denom, coin.Amount.AddRaw(1))
crisisKeeper.SetConstantFee(ctx, excessCoins)
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]
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]
h := crisis.NewHandler(crisisKeeper)
msg := crisis.NewMsgVerifyInvariant(sender, testModuleName, dummyRouteWhichFails.Route)
var res sdk.Result
require.Panics(t, func() {
res = h(ctx, msg)
}, fmt.Sprintf("%v", res))
}
func TestHandleMsgVerifyInvariantWithInvariantBrokenAndNotEnoughPoolCoins(t *testing.T) {
ctx, crisisKeeper, _, distrKeeper := CreateTestInput(t)
sender := addrs[0]
// set the community pool to empty
feePool := distrKeeper.GetFeePool(ctx)
feePool.CommunityPool = sdk.DecCoins{}
distrKeeper.SetFeePool(ctx, feePool)
h := crisis.NewHandler(crisisKeeper)
msg := crisis.NewMsgVerifyInvariant(sender, testModuleName, dummyRouteWhichFails.Route)
var res sdk.Result
require.Panics(t, func() {
res = h(ctx, msg)
}, fmt.Sprintf("%v", res))
}
func TestHandleMsgVerifyInvariantWithInvariantNotBroken(t *testing.T) {
ctx, crisisKeeper, _, _ := CreateTestInput(t)
sender := addrs[0]
h := crisis.NewHandler(crisisKeeper)
msg := crisis.NewMsgVerifyInvariant(sender, testModuleName, dummyRouteWhichPasses.Route)
require.True(t, h(ctx, msg).IsOK())
}
func TestInvalidMsg(t *testing.T) {
k := crisis.Keeper{}
h := crisis.NewHandler(k)
res := h(sdk.NewContext(nil, abci.Header{}, false, nil), sdk.NewTestMsg())
require.False(t, res.IsOK())
require.True(t, strings.Contains(res.Log, "unrecognized crisis message type"))
}