-
Notifications
You must be signed in to change notification settings - Fork 8
/
msg_server.go
77 lines (63 loc) · 2.46 KB
/
msg_server.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
package keeper
import (
"context"
addresscodec "cosmossdk.io/core/address"
errorsmod "cosmossdk.io/errors"
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"
"github.com/sedaprotocol/seda-chain/x/staking/types"
)
var _ types.MsgServer = msgServer{}
type msgServer struct {
stakingtypes.MsgServer
keeper *Keeper
accountKeeper types.AccountKeeper
randomnessKeeper types.RandomnessKeeper
validatorAddressCodec addresscodec.Codec
}
func NewMsgServerImpl(sdkMsgServer stakingtypes.MsgServer, keeper *Keeper, accKeeper types.AccountKeeper, randKeeper types.RandomnessKeeper) types.MsgServer {
ms := &msgServer{
MsgServer: sdkMsgServer,
keeper: keeper,
accountKeeper: accKeeper,
randomnessKeeper: randKeeper,
validatorAddressCodec: keeper.ValidatorAddressCodec(),
}
return ms
}
func (k msgServer) CreateValidatorWithVRF(ctx context.Context, msg *types.MsgCreateValidatorWithVRF) (*types.MsgCreateValidatorWithVRFResponse, error) {
if err := msg.Validate(k.validatorAddressCodec); err != nil {
return nil, err
}
// create an account based on VRF public key to send NewSeed txs when proposing blocks
vrfPubKey, ok := msg.VrfPubkey.GetCachedValue().(cryptotypes.PubKey)
if !ok {
return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidType, "Expecting cryptotypes.PubKey, got %T", vrfPubKey)
}
addr := sdk.AccAddress(vrfPubKey.Address().Bytes())
acc := k.accountKeeper.NewAccountWithAddress(ctx, addr)
k.accountKeeper.SetAccount(ctx, acc)
// register VRF public key to validator consensus address
consPubKey, ok := msg.Pubkey.GetCachedValue().(cryptotypes.PubKey)
if !ok {
return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidType, "Expecting cryptotypes.PubKey, got %T", consPubKey)
}
err := k.randomnessKeeper.SetValidatorVRFPubKey(ctx, sdk.GetConsAddress(consPubKey).String(), vrfPubKey)
if err != nil {
return nil, err
}
sdkMsg := new(stakingtypes.MsgCreateValidator)
sdkMsg.Description = msg.Description
sdkMsg.Commission = msg.Commission
sdkMsg.MinSelfDelegation = msg.MinSelfDelegation
sdkMsg.ValidatorAddress = msg.ValidatorAddress
sdkMsg.Pubkey = msg.Pubkey
sdkMsg.Value = msg.Value
_, err = k.MsgServer.CreateValidator(ctx, sdkMsg)
if err != nil {
return nil, err
}
return &types.MsgCreateValidatorWithVRFResponse{}, nil
}