forked from tendermint/spn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msg_server_request_add_validator.go
68 lines (58 loc) · 1.67 KB
/
msg_server_request_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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package keeper
import (
"context"
codec "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/tendermint/spn/x/launch/types"
)
func (k msgServer) RequestAddValidator(
goCtx context.Context,
msg *types.MsgRequestAddValidator,
) (*types.MsgRequestResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
chain, found := k.GetChain(ctx, msg.ChainID)
if !found {
return nil, sdkerrors.Wrap(types.ErrChainNotFound, msg.ChainID)
}
if chain.LaunchTriggered {
return nil, sdkerrors.Wrap(types.ErrTriggeredLaunch, msg.ChainID)
}
content, err := codec.NewAnyWithValue(&types.GenesisValidator{
ChainID: msg.ChainID,
Address: msg.ValAddress,
GenTx: msg.GenTx,
ConsPubKey: msg.ConsPubKey,
SelfDelegation: msg.SelfDelegation,
Peer: msg.Peer,
})
if err != nil {
return nil, sdkerrors.Wrap(types.ErrCodecNotPacked, msg.String())
}
coordAddress, found := k.profileKeeper.GetCoordinatorAddressFromID(ctx, chain.CoordinatorID)
if !found {
return nil, sdkerrors.Wrapf(types.ErrChainInactive,
"the chain %s coordinator has been deleted", chain.ChainID)
}
request := types.Request{
ChainID: msg.ChainID,
Creator: msg.ValAddress,
CreatedAt: ctx.BlockTime().Unix(),
Content: content,
}
var requestID uint64
approved := false
if msg.ValAddress == coordAddress {
err := ApplyRequest(ctx, k.Keeper, msg.ChainID, request)
if err != nil {
return nil, err
}
approved = true
} else {
requestID = k.AppendRequest(ctx, request)
}
return &types.MsgRequestResponse{
RequestID: requestID,
AutoApproved: approved,
}, nil
}