-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_helpers.go
107 lines (89 loc) · 3.58 KB
/
test_helpers.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
package testutil
import (
"cosmossdk.io/math"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
"github.com/nephirim/fanfury-sdk/v2/app"
"github.com/nephirim/fanfury-sdk/v2/x/lsnative/staking"
stakingkeeper "github.com/nephirim/fanfury-sdk/v2/x/lsnative/staking/keeper"
stakingtypes "github.com/nephirim/fanfury-sdk/v2/x/lsnative/staking/types"
"github.com/tendermint/tendermint/crypto/secp256k1"
)
var (
ValidatorInitPower = int64(10000000000)
ValidatorInitTokens = sdk.TokensFromConsensusPower(ValidatorInitPower, sdk.DefaultPowerReduction)
ValidatorInitCoins = sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, ValidatorInitTokens))
ValidatorAmountBonded = sdk.TokensFromConsensusPower(100, sdk.DefaultPowerReduction)
)
// FundAccount is a utility function that funds an account by minting and
// sending the coins to the address. This should be used for testing purposes
// only!
func FundAccount(bankKeeper bankkeeper.Keeper, ctx sdk.Context, addr sdk.AccAddress, amounts sdk.Coins) error {
if err := bankKeeper.MintCoins(ctx, minttypes.ModuleName, amounts); err != nil {
return err
}
return bankKeeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, addr, amounts)
}
// FundModuleAccount is a utility function that funds a module account by
// minting and sending the coins to the address. This should be used for testing
// purposes only!
func FundModuleAccount(bankKeeper bankkeeper.Keeper, ctx sdk.Context, recipientMod string, amounts sdk.Coins) error {
if err := bankKeeper.MintCoins(ctx, minttypes.ModuleName, amounts); err != nil {
return err
}
return bankKeeper.SendCoinsFromModuleToModule(ctx, minttypes.ModuleName, recipientMod, amounts)
}
// StakingAddValidators generates N validators and adds them to staking keeper as bonded validators.
func StakingAddValidators(
bankKeeper bankkeeper.Keeper,
stakingKeeper stakingkeeper.Keeper,
ctx sdk.Context,
num int,
) (
[]sdk.AccAddress,
[]sdk.ValAddress,
error,
) {
accAddresses := make([]sdk.AccAddress, num)
valAddresses := make([]sdk.ValAddress, num)
stakingMsgServer := stakingkeeper.NewMsgServerImpl(stakingKeeper)
valPubKeys := furyapp.CreateTestPubKeys(num)
for i := 0; i < num; i++ {
var (
valPubKey = valPubKeys[i]
pubKey = secp256k1.GenPrivKey().PubKey()
accAddr = sdk.AccAddress(pubKey.Address())
valAddr = sdk.ValAddress(pubKey.Address())
)
// fund the validator account with initial coins
if err := FundAccount(bankKeeper, ctx, accAddr, ValidatorInitCoins); err != nil {
return nil, nil, err
}
// create validator in staking keeper with amount bonded
createValidatorMsg, err := newTestMsgCreateValidator(valAddr, valPubKey, ValidatorAmountBonded)
if err != nil {
return nil, nil, err
}
if _, err := stakingMsgServer.CreateValidator(ctx, createValidatorMsg); err != nil {
return nil, nil, err
}
accAddresses[i] = accAddr
valAddresses[i] = valAddr
}
// ensure that validators are updated
staking.EndBlocker(ctx, stakingKeeper)
return accAddresses, valAddresses, nil
}
func newTestMsgCreateValidator(address sdk.ValAddress, pubKey cryptotypes.PubKey, amt math.Int) (*stakingtypes.MsgCreateValidator, error) {
commission := stakingtypes.NewCommissionRates(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec())
msg, err := stakingtypes.NewMsgCreateValidator(
address, pubKey, sdk.NewCoin(sdk.DefaultBondDenom, amt),
stakingtypes.Description{}, commission,
)
if err != nil {
return nil, err
}
return msg, nil
}