/
helper.go
118 lines (99 loc) · 3.95 KB
/
helper.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
116
117
118
package teststaking
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/ed25519"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/staking"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
customStaking "github.com/certikfoundation/shentu/x/staking"
)
// Helper is a structure which wraps the staking handler
// and provides methods useful in tests
type Helper struct {
t *testing.T
h sdk.Handler
k customStaking.Keeper
ctx sdk.Context
Commission stakingtypes.CommissionRates
// Coin Denomination
Denom string
}
// NewHelper creates staking Handler wrapper for tests
func NewHelper(t *testing.T, ctx sdk.Context, k customStaking.Keeper) *Helper {
return &Helper{t, staking.NewHandler(k.Keeper), k, ctx, ZeroCommission(), k.Keeper.BondDenom(ctx)}
}
// CreateValidator calls handler to create a new staking validator
func (sh *Helper) CreateValidator(addr sdk.ValAddress, pk crypto.PubKey, stakeAmount int64, ok bool) {
coin := sdk.NewCoin(sh.Denom, sdk.NewInt(stakeAmount))
sh.createValidator(addr, pk, coin, ok)
}
// CreateValidatorWithValPower calls handler to create a new staking validator with zero
// commission
func (sh *Helper) CreateValidatorWithValPower(addr sdk.ValAddress, valPower int64, ok bool) sdk.Int {
amount := sdk.TokensFromConsensusPower(valPower)
pubKey := ed25519.GenPrivKey().PubKey()
coin := sdk.NewCoin(sh.Denom, amount)
sh.createValidator(addr, pubKey, coin, ok)
return amount
}
func (sh *Helper) createValidator(addr sdk.ValAddress, pk crypto.PubKey, coin sdk.Coin, ok bool) {
msg := stakingtypes.NewMsgCreateValidator(addr, pk, coin, stakingtypes.Description{}, sh.Commission, sdk.OneInt())
sh.Handle(msg, ok)
}
// Delegate calls handler to delegate stake for a validator
func (sh *Helper) Delegate(delegator sdk.AccAddress, val sdk.ValAddress, amount int64) {
coin := sdk.NewCoin(sh.Denom, sdk.NewInt(amount))
msg := stakingtypes.NewMsgDelegate(delegator, val, coin)
sh.Handle(msg, true)
}
// Redelegate calls handler to begin redelegation.
func (sh *Helper) Redelegate(delegator sdk.AccAddress, srcVal, dstVal sdk.ValAddress, amount int64, ok bool) {
coin := sdk.NewCoin(sh.Denom, sdk.NewInt(amount))
msg := stakingtypes.NewMsgBeginRedelegate(delegator, srcVal, dstVal, coin)
sh.Handle(msg, ok)
}
// Undelegate calls handler to unbound some stake from a validator.
func (sh *Helper) Undelegate(delegator sdk.AccAddress, val sdk.ValAddress, amount int64, ok bool) *sdk.Result {
unbondAmt := sdk.NewInt64Coin(sh.Denom, amount)
msg := stakingtypes.NewMsgUndelegate(delegator, val, unbondAmt)
return sh.Handle(msg, ok)
}
// Handle calls staking handler on a given message
func (sh *Helper) Handle(msg sdk.Msg, ok bool) *sdk.Result {
res, err := sh.h(sh.ctx, msg)
if ok {
require.NoError(sh.t, err)
require.NotNil(sh.t, res)
} else {
require.Error(sh.t, err)
require.Nil(sh.t, res)
}
return res
}
// CheckValidator asserts that a validor exists and has a given status (if status!="")
// and if has a right jailed flag.
func (sh *Helper) CheckValidator(addr sdk.ValAddress, status sdk.BondStatus, jailed bool) stakingtypes.Validator {
v, ok := sh.k.GetValidator(sh.ctx, addr)
require.True(sh.t, ok)
require.Equal(sh.t, jailed, v.Jailed, "wrong Jalied status")
if status >= 0 {
require.Equal(sh.t, status, v.Status)
}
return v
}
// CheckDelegator asserts that a delegator exists
func (sh *Helper) CheckDelegator(delegator sdk.AccAddress, val sdk.ValAddress, found bool) {
_, ok := sh.k.GetDelegation(sh.ctx, delegator, val)
require.Equal(sh.t, ok, found)
}
// TurnBlock updates context and calls endblocker.
func (sh *Helper) TurnBlock(ctx sdk.Context) {
sh.ctx = ctx
staking.EndBlocker(sh.ctx, sh.k.Keeper)
}
// ZeroCommission constructs a commission rates with all zeros.
func ZeroCommission() stakingtypes.CommissionRates {
return stakingtypes.NewCommissionRates(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec())
}