-
Notifications
You must be signed in to change notification settings - Fork 206
/
keeper.go
227 lines (198 loc) · 8.69 KB
/
keeper.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package keeper
import (
"fmt"
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkioerrors "cosmossdk.io/errors"
capability "github.com/cosmos/cosmos-sdk/x/capability/types"
clienttypes "github.com/cosmos/ibc-go/v6/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v6/modules/core/04-channel/types"
porttypes "github.com/cosmos/ibc-go/v6/modules/core/05-port/types"
host "github.com/cosmos/ibc-go/v6/modules/core/24-host"
ibcexported "github.com/cosmos/ibc-go/v6/modules/core/exported"
"github.com/Agoric/agoric-sdk/golang/cosmos/vm"
"github.com/Agoric/agoric-sdk/golang/cosmos/x/vibc/types"
)
var (
_ porttypes.ICS4Wrapper = Keeper{}
_ types.IBCModuleImpl = Keeper{}
_ types.ReceiverImpl = Keeper{}
)
// Keeper maintains the link to data storage and exposes getter/setter methods for the various parts of the state machine
type Keeper struct {
cdc codec.Codec
channelKeeper types.ChannelKeeper
portKeeper types.PortKeeper
// Filled out by `WithScope`
scopedKeeper types.ScopedKeeper
storeKey storetypes.StoreKey
pushAction vm.ActionPusher
}
// NewKeeper creates a new vibc Keeper instance
func NewKeeper(
cdc codec.Codec,
channelKeeper types.ChannelKeeper,
portKeeper types.PortKeeper,
) Keeper {
return Keeper{
cdc: cdc,
channelKeeper: channelKeeper,
portKeeper: portKeeper,
}
}
// WithScope returns a new Keeper copied from the receiver, but with the given
// store key, scoped keeper, and push action.
func (k Keeper) WithScope(storeKey storetypes.StoreKey, scopedKeeper types.ScopedKeeper, pushAction vm.ActionPusher) Keeper {
k.storeKey = storeKey
k.scopedKeeper = scopedKeeper
k.pushAction = pushAction
return k
}
// PushAction sends a vm.Action to the VM controller.
func (k Keeper) PushAction(ctx sdk.Context, action vm.Action) error {
return k.pushAction(ctx, action)
}
// GetICS4Wrapper returns the ICS4Wrapper interface for the keeper.
func (k Keeper) GetICS4Wrapper() porttypes.ICS4Wrapper {
return k
}
// GetAppVersion defines a wrapper function for the channel Keeper's function
// in order to expose it to the vibc IBC handler.
func (k Keeper) GetAppVersion(ctx sdk.Context, portID, channelID string) (string, bool) {
return k.channelKeeper.GetAppVersion(ctx, portID, channelID)
}
// GetChannel defines a wrapper function for the channel Keeper's function
// in order to expose it to the vibc IBC handler.
func (k Keeper) GetChannel(ctx sdk.Context, portID, channelID string) (channeltypes.Channel, bool) {
return k.channelKeeper.GetChannel(ctx, portID, channelID)
}
// ReceiveChanOpenInit wraps the keeper's ChanOpenInit function.
func (k Keeper) ReceiveChanOpenInit(ctx sdk.Context, order channeltypes.Order, connectionHops []string,
portID, rPortID, version string,
) error {
capName := host.PortPath(portID)
portCap, ok := k.GetCapability(ctx, capName)
if !ok {
return sdkioerrors.Wrapf(porttypes.ErrInvalidPort, "could not retrieve port capability at: %s", capName)
}
counterparty := channeltypes.Counterparty{
PortId: rPortID,
}
channelID, chanCap, err := k.channelKeeper.ChanOpenInit(ctx, order, connectionHops, portID, portCap, counterparty, version)
if err != nil {
return err
}
chanCapName := host.ChannelCapabilityPath(portID, channelID)
err = k.ClaimCapability(ctx, chanCap, chanCapName)
if err != nil {
return err
}
k.channelKeeper.WriteOpenInitChannel(ctx, portID, channelID, order, connectionHops, counterparty, version)
return nil
}
// ReceiveSendPacket wraps the keeper's SendPacket function.
func (k Keeper) ReceiveSendPacket(ctx sdk.Context, packet ibcexported.PacketI) (uint64, error) {
sourcePort := packet.GetSourcePort()
sourceChannel := packet.GetSourceChannel()
timeoutHeight := packet.GetTimeoutHeight()
timeoutRevisionNumber := timeoutHeight.GetRevisionNumber()
timeoutRevisionHeight := timeoutHeight.GetRevisionHeight()
clientTimeoutHeight := clienttypes.NewHeight(timeoutRevisionNumber, timeoutRevisionHeight)
timeoutTimestamp := packet.GetTimeoutTimestamp()
data := packet.GetData()
capName := host.ChannelCapabilityPath(sourcePort, sourceChannel)
chanCap, ok := k.GetCapability(ctx, capName)
if !ok {
return 0, sdkioerrors.Wrapf(channeltypes.ErrChannelCapabilityNotFound, "could not retrieve channel capability at: %s", capName)
}
return k.SendPacket(ctx, chanCap, sourcePort, sourceChannel, clientTimeoutHeight, timeoutTimestamp, data)
}
// SendPacket defines a wrapper function for the channel Keeper's function
// in order to expose it to the vibc IBC handler.
func (k Keeper) SendPacket(
ctx sdk.Context,
chanCap *capability.Capability,
sourcePort string,
sourceChannel string,
timeoutHeight clienttypes.Height,
timeoutTimestamp uint64,
data []byte,
) (uint64, error) {
return k.channelKeeper.SendPacket(ctx, chanCap, sourcePort, sourceChannel, timeoutHeight, timeoutTimestamp, data)
}
// ReceiveWriteAcknowledgement wraps the keeper's WriteAcknowledgment function.
func (k Keeper) ReceiveWriteAcknowledgement(ctx sdk.Context, packet ibcexported.PacketI, ack ibcexported.Acknowledgement) error {
portID := packet.GetDestPort()
channelID := packet.GetDestChannel()
capName := host.ChannelCapabilityPath(portID, channelID)
chanCap, ok := k.GetCapability(ctx, capName)
if !ok {
return sdkioerrors.Wrapf(channeltypes.ErrChannelCapabilityNotFound, "could not retrieve channel capability at: %s", capName)
}
return k.WriteAcknowledgement(ctx, chanCap, packet, ack)
}
// WriteAcknowledgement defines a wrapper function for the channel Keeper's function
// in order to expose it to the vibc IBC handler.
func (k Keeper) WriteAcknowledgement(ctx sdk.Context, chanCap *capability.Capability, packet ibcexported.PacketI, ack ibcexported.Acknowledgement) error {
return k.channelKeeper.WriteAcknowledgement(ctx, chanCap, packet, ack)
}
// ReceiveWriteOpenTryChannel wraps the keeper's WriteOpenTryChannel function.
func (k Keeper) ReceiveWriteOpenTryChannel(ctx sdk.Context, packet ibcexported.PacketI, order channeltypes.Order, connectionHops []string, version string) error {
portID := packet.GetDestPort()
channelID := packet.GetDestChannel()
counterparty := channeltypes.NewCounterparty(packet.GetSourcePort(), packet.GetSourceChannel())
k.WriteOpenTryChannel(ctx, portID, channelID, order, connectionHops, counterparty, version)
return nil
}
// WriteOpenTryChannel is a wrapper function for the channel Keeper's function
func (k Keeper) WriteOpenTryChannel(ctx sdk.Context, portID, channelID string, order channeltypes.Order,
connectionHops []string, counterparty channeltypes.Counterparty, version string) {
k.channelKeeper.WriteOpenTryChannel(ctx, portID, channelID, order, connectionHops, counterparty, version)
}
// ReceiveChanCloseInit is a wrapper function for the channel Keeper's function
// in order to expose it to the vibc IBC handler.
func (k Keeper) ReceiveChanCloseInit(ctx sdk.Context, portID, channelID string) error {
capName := host.ChannelCapabilityPath(portID, channelID)
chanCap, ok := k.GetCapability(ctx, capName)
if !ok {
return sdkioerrors.Wrapf(channeltypes.ErrChannelCapabilityNotFound, "could not retrieve channel capability at: %s", capName)
}
err := k.channelKeeper.ChanCloseInit(ctx, portID, channelID, chanCap)
if err != nil {
return err
}
return nil
}
// ReceiveBindPort is a wrapper function for the port Keeper's function in order
// to expose it to the vibc IBC handler.
func (k Keeper) ReceiveBindPort(ctx sdk.Context, portID string) error {
portPath := host.PortPath(portID)
_, ok := k.GetCapability(ctx, portPath)
if ok {
return fmt.Errorf("port %s is already bound", portID)
}
cap := k.portKeeper.BindPort(ctx, portID)
return k.ClaimCapability(ctx, cap, portPath)
}
// ReceiveTimeoutExecuted is a wrapper function for the channel Keeper's
// function in order to expose it to the vibc IBC handler.
func (k Keeper) ReceiveTimeoutExecuted(ctx sdk.Context, packet ibcexported.PacketI) error {
portID := packet.GetSourcePort()
channelID := packet.GetSourceChannel()
capName := host.ChannelCapabilityPath(portID, channelID)
chanCap, ok := k.GetCapability(ctx, capName)
if !ok {
return sdkioerrors.Wrapf(channeltypes.ErrChannelCapabilityNotFound, "could not retrieve channel capability at: %s", capName)
}
return k.channelKeeper.TimeoutExecuted(ctx, chanCap, packet)
}
// ClaimCapability allows the vibc module to claim a capability that IBC module
// passes to it.
func (k Keeper) ClaimCapability(ctx sdk.Context, cap *capability.Capability, name string) error {
return k.scopedKeeper.ClaimCapability(ctx, cap, name)
}
// GetCapability allows the vibc module to retrieve a capability.
func (k Keeper) GetCapability(ctx sdk.Context, name string) (*capability.Capability, bool) {
return k.scopedKeeper.GetCapability(ctx, name)
}