-
Notifications
You must be signed in to change notification settings - Fork 6
/
staking.go
107 lines (89 loc) · 3.05 KB
/
staking.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 keeper
import (
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)
func (k Keeper) CreateValidator(ctx sdk.Context, msg stakingtypes.MsgCreateValidator) error {
valAddr, err := sdk.ValAddressFromBech32(msg.ValidatorAddress)
if err != nil {
return err
}
// check to see if the pubkey or sender has been registered before
if _, found := k.stakingKeeper.GetValidator(ctx, valAddr); found {
return stakingtypes.ErrValidatorOwnerExists
}
pk, ok := msg.Pubkey.GetCachedValue().(cryptotypes.PubKey)
if !ok {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "Expecting cryptostakingtypes.PubKey, got %T", pk)
}
if _, found := k.stakingKeeper.GetValidatorByConsAddr(ctx, sdk.GetConsAddress(pk)); found {
return stakingtypes.ErrValidatorPubKeyExists
}
bondDenom := k.stakingKeeper.BondDenom(ctx)
if msg.Value.Denom != bondDenom {
return sdkerrors.Wrapf(
sdkerrors.ErrInvalidRequest, "invalid coin denomination: got %s, expected %s", msg.Value.Denom, bondDenom,
)
}
if _, err := msg.Description.EnsureLength(); err != nil {
return err
}
cp := ctx.ConsensusParams()
if cp != nil && cp.Validator != nil {
pkType := pk.Type()
hasKeyType := false
for _, keyType := range cp.Validator.PubKeyTypes {
if pkType == keyType {
hasKeyType = true
break
}
}
if !hasKeyType {
return sdkerrors.Wrapf(
stakingtypes.ErrValidatorPubKeyTypeNotSupported,
"got: %s, expected: %s", pk.Type(), cp.Validator.PubKeyTypes,
)
}
}
validator, err := stakingtypes.NewValidator(valAddr, pk, msg.Description)
if err != nil {
return err
}
commission := stakingtypes.NewCommissionWithTime(
msg.Commission.Rate, msg.Commission.MaxRate,
msg.Commission.MaxChangeRate, ctx.BlockHeader().Time,
)
validator, err = validator.SetInitialCommission(commission)
if err != nil {
return err
}
delegatorAddress, err := sdk.AccAddressFromBech32(msg.DelegatorAddress)
if err != nil {
return err
}
validator.MinSelfDelegation = msg.MinSelfDelegation
k.stakingKeeper.SetValidator(ctx, validator)
k.stakingKeeper.SetValidatorByConsAddr(ctx, validator)
k.stakingKeeper.SetNewValidatorByPowerIndex(ctx, validator)
// call the after-creation hook
if err := k.stakingKeeper.Hooks().AfterValidatorCreated(ctx, validator.GetOperator()); err != nil {
return err
}
// move coins from the msg.Address account to a (self-delegation) delegator account
// the validator account and global shares are updated within here
// NOTE source will always be from a wallet which are unbonded
_, err = k.stakingKeeper.Delegate(ctx, delegatorAddress, msg.Value.Amount, stakingtypes.Unbonded, validator, true)
if err != nil {
return err
}
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
stakingtypes.EventTypeCreateValidator,
sdk.NewAttribute(stakingtypes.AttributeKeyValidator, msg.ValidatorAddress),
sdk.NewAttribute(sdk.AttributeKeyAmount, msg.Value.String()),
),
})
return nil
}