-
Notifications
You must be signed in to change notification settings - Fork 1
/
msg_server.go
72 lines (55 loc) · 2.36 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
package keeper
import (
"context"
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/vipernet-xyz/ibc-go/v7/modules/apps/27-interchain-accounts/controller/types"
icatypes "github.com/vipernet-xyz/ibc-go/v7/modules/apps/27-interchain-accounts/types"
)
var _ types.MsgServer = msgServer{}
type msgServer struct {
*Keeper
}
// NewMsgServerImpl returns an implementation of the ICS27 MsgServer interface
// for the provided Keeper.
func NewMsgServerImpl(keeper *Keeper) types.MsgServer {
return &msgServer{Keeper: keeper}
}
// RegisterInterchainAccount defines a rpc handler for MsgRegisterInterchainAccount
func (s msgServer) RegisterInterchainAccount(goCtx context.Context, msg *types.MsgRegisterInterchainAccount) (*types.MsgRegisterInterchainAccountResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
portID, err := icatypes.NewControllerPortID(msg.Owner)
if err != nil {
return nil, err
}
if s.IsMiddlewareEnabled(ctx, portID, msg.ConnectionId) && !s.IsActiveChannelClosed(ctx, msg.ConnectionId, portID) {
return nil, errorsmod.Wrap(icatypes.ErrInvalidChannelFlow, "channel is already active or a handshake is in flight")
}
s.SetMiddlewareDisabled(ctx, portID, msg.ConnectionId)
channelID, err := s.registerInterchainAccount(ctx, msg.ConnectionId, portID, msg.Version)
if err != nil {
s.Logger(ctx).Error("error registering interchain account", "error", err.Error())
return nil, err
}
s.Logger(ctx).Info("successfully registered interchain account", "channel-id", channelID)
return &types.MsgRegisterInterchainAccountResponse{
ChannelId: channelID,
PortId: portID,
}, nil
}
// SendTx defines a rpc handler for MsgSendTx
func (s msgServer) SendTx(goCtx context.Context, msg *types.MsgSendTx) (*types.MsgSendTxResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
portID, err := icatypes.NewControllerPortID(msg.Owner)
if err != nil {
return nil, err
}
// the absolute timeout value is calculated using the controller chain block time + the relative timeout value
// this assumes time synchrony to a certain degree between the controller and counterparty host chain
absoluteTimeout := uint64(ctx.BlockTime().UnixNano()) + msg.RelativeTimeout
seq, err := s.sendTx(ctx, msg.ConnectionId, portID, msg.PacketData, absoluteTimeout)
if err != nil {
return nil, err
}
return &types.MsgSendTxResponse{Sequence: seq}, nil
}