-
Notifications
You must be signed in to change notification settings - Fork 201
/
msg_server_add_validator.go
48 lines (43 loc) · 1.63 KB
/
msg_server_add_validator.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
package keeper
import (
"context"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/Stride-Labs/stride/x/stakeibc/types"
)
func (k msgServer) AddValidator(goCtx context.Context, msg *types.MsgAddValidator) (*types.MsgAddValidatorResponse, error) {
// TODO TEST-129 restrict this to governance module. add gov module whitelist hooks more broadly
ctx := sdk.UnwrapSDKContext(goCtx)
hostZone, found := k.GetHostZone(ctx, msg.HostZone)
if !found {
errMsg := fmt.Sprintf("Host Zone (%s) not found", msg.HostZone)
k.Logger(ctx).Error(errMsg)
return nil, sdkerrors.Wrap(types.ErrHostZoneNotFound, errMsg)
}
validators := hostZone.Validators
// check that we don't already have this validator
for _, validator := range validators {
if validator.GetAddress() == msg.Address {
errMsg := fmt.Sprintf("Validator address (%s) already exists on Host Zone (%s)", msg.Address, msg.HostZone)
k.Logger(ctx).Error(errMsg)
return nil, sdkerrors.Wrap(types.ErrValidatorAlreadyExists, errMsg)
}
if validator.Name == msg.Name {
errMsg := fmt.Sprintf("Validator name (%s) already exists on Host Zone (%s)", msg.Name, msg.HostZone)
k.Logger(ctx).Error(errMsg)
return nil, sdkerrors.Wrap(types.ErrValidatorAlreadyExists, errMsg)
}
}
// add the validator
hostZone.Validators = append(validators, &types.Validator{
Name: msg.Name,
Address: msg.Address,
Status: types.Validator_Active,
CommissionRate: msg.Commission,
DelegationAmt: 0,
Weight: msg.Weight,
})
k.SetHostZone(ctx, hostZone)
return &types.MsgAddValidatorResponse{}, nil
}