-
Notifications
You must be signed in to change notification settings - Fork 0
/
keeper.go
330 lines (274 loc) · 10.9 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package keeper
import (
"github.com/ci123chain/ci123chain/pkg/abci/codec"
sdk "github.com/ci123chain/ci123chain/pkg/abci/types"
capabilitykeeper "github.com/ci123chain/ci123chain/pkg/capability/keeper"
capabilitytypes "github.com/ci123chain/ci123chain/pkg/capability/types"
"github.com/ci123chain/ci123chain/pkg/ibc/core/channel/types"
"github.com/ci123chain/ci123chain/pkg/ibc/core/host"
porttypes "github.com/ci123chain/ci123chain/pkg/ibc/core/port/types"
"github.com/tendermint/tendermint/libs/log"
"strconv"
"strings"
)
// Keeper defines the IBC channel keeper
type Keeper struct {
// implements gRPC QueryServer interface
//types.QueryServer
storeKey sdk.StoreKey
cdc codec.BinaryMarshaler
clientKeeper types.ClientKeeper
connectionKeeper types.ConnectionKeeper
portKeeper types.PortKeeper
scopedKeeper capabilitykeeper.ScopedKeeper
}
// NewKeeper creates a new IBC channel Keeper instance
func NewKeeper(
cdc codec.BinaryMarshaler, key sdk.StoreKey,
clientKeeper types.ClientKeeper, connectionKeeper types.ConnectionKeeper,
portKeeper types.PortKeeper,
scopedKeeper capabilitykeeper.ScopedKeeper,
) Keeper {
return Keeper{
storeKey: key,
cdc: cdc,
clientKeeper: clientKeeper,
connectionKeeper: connectionKeeper,
portKeeper: portKeeper,
scopedKeeper: scopedKeeper,
}
}
// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "x/"+host.ModuleName+"/"+types.SubModuleName)
}
// GetChannel returns a channel with a particular identifier binded to a specific port
func (k Keeper) GetChannel(ctx sdk.Context, portID, channelID string) (types.Channel, bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(host.ChannelKey(portID, channelID))
if bz == nil {
return types.Channel{}, false
}
var channel types.Channel
k.cdc.MustUnmarshalBinaryBare(bz, &channel)
return channel, true
}
// SetChannel sets a channel to the store
func (k Keeper) SetChannel(ctx sdk.Context, portID, channelID string, channel types.Channel) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalBinaryBare(&channel)
store.Set(host.ChannelKey(portID, channelID), bz)
}
func (k Keeper) GetChannels(ctx sdk.Context, cb func(channel types.Channel, portID, channelID string) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, []byte(host.KeyChannelEndPrefix))
defer iter.Close()
for ; iter.Valid(); iter.Next() {
key := string(iter.Key())
keys := strings.Split(key, "/")
var v types.Channel
k.cdc.MustUnmarshalBinaryBare(iter.Value(), &v)
if cb(v, keys[1], keys[2]) {
break
}
}
}
// GenerateChannelIdentifier returns the next channel identifier.
func (k Keeper) GenerateChannelIdentifier(ctx sdk.Context) string {
nextChannelSeq := k.GetNextChannelSequence(ctx)
channelID := types.FormatChannelIdentifier(nextChannelSeq)
nextChannelSeq++
k.SetNextChannelSequence(ctx, nextChannelSeq)
return channelID
}
// GetNextChannelSequence gets the next channel sequence from the store.
func (k Keeper) GetNextChannelSequence(ctx sdk.Context) uint64 {
store := ctx.KVStore(k.storeKey)
bz := store.Get([]byte(types.KeyNextChannelSequence))
if bz == nil {
panic("next channel sequence is nil")
}
return sdk.BigEndianToUint64(bz)
}
// SetNextChannelSequence sets the next channel sequence to the store.
func (k Keeper) SetNextChannelSequence(ctx sdk.Context, sequence uint64) {
store := ctx.KVStore(k.storeKey)
bz := sdk.Uint64ToBigEndian(sequence)
store.Set([]byte(types.KeyNextChannelSequence), bz)
}
// GetNextSequenceSend gets a channel's next send sequence from the store
func (k Keeper) GetNextSequenceSend(ctx sdk.Context, portID, channelID string) (uint64, bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(host.NextSequenceSendKey(portID, channelID))
if bz == nil {
return 0, false
}
return sdk.BigEndianToUint64(bz), true
}
// SetNextSequenceSend sets a channel's next send sequence to the store
func (k Keeper) SetNextSequenceSend(ctx sdk.Context, portID, channelID string, sequence uint64) {
store := ctx.KVStore(k.storeKey)
bz := sdk.Uint64ToBigEndian(sequence)
store.Set(host.NextSequenceSendKey(portID, channelID), bz)
}
func (k Keeper) GetAllNextSequenceSend(ctx sdk.Context, cb func(seq uint64, portID, channelID string) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, []byte(host.KeyNextSeqSendPrefix))
defer iter.Close()
for ; iter.Valid(); iter.Next() {
key := string(iter.Key())
keys := strings.Split(key, "/")
if cb(sdk.BigEndianToUint64(iter.Value()), keys[1], keys[2]) {
break
}
}
}
// GetNextSequenceRecv gets a channel's next receive sequence from the store
func (k Keeper) GetNextSequenceRecv(ctx sdk.Context, portID, channelID string) (uint64, bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(host.NextSequenceRecvKey(portID, channelID))
if bz == nil {
return 0, false
}
return sdk.BigEndianToUint64(bz), true
}
// SetNextSequenceRecv sets a channel's next receive sequence to the store
func (k Keeper) SetNextSequenceRecv(ctx sdk.Context, portID, channelID string, sequence uint64) {
store := ctx.KVStore(k.storeKey)
bz := sdk.Uint64ToBigEndian(sequence)
store.Set(host.NextSequenceRecvKey(portID, channelID), bz)
}
func (k Keeper) GetAllNextSequenceRecv(ctx sdk.Context, cb func(seq uint64, portID, channelID string) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, []byte(host.KeyNextSeqRecvPrefix))
defer iter.Close()
for ; iter.Valid(); iter.Next() {
key := string(iter.Key())
keys := strings.Split(key, "/")
if cb(sdk.BigEndianToUint64(iter.Value()), keys[1], keys[2]) {
break
}
}
}
// GetNextSequenceAck gets a channel's next ack sequence from the store
func (k Keeper) GetNextSequenceAck(ctx sdk.Context, portID, channelID string) (uint64, bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(host.NextSequenceAckKey(portID, channelID))
if bz == nil {
return 0, false
}
return sdk.BigEndianToUint64(bz), true
}
// SetNextSequenceAck sets a channel's next ack sequence to the store
func (k Keeper) SetNextSequenceAck(ctx sdk.Context, portID, channelID string, sequence uint64) {
store := ctx.KVStore(k.storeKey)
bz := sdk.Uint64ToBigEndian(sequence)
store.Set(host.NextSequenceAckKey(portID, channelID), bz)
}
func (k Keeper) GetAllNextSequenceAck(ctx sdk.Context, cb func(seq uint64, portID, channelID string) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, []byte(host.KeyNextSeqAckPrefix))
defer iter.Close()
for ; iter.Valid(); iter.Next() {
key := string(iter.Key())
keys := strings.Split(key, "/")
if cb(sdk.BigEndianToUint64(iter.Value()), keys[1], keys[2]) {
break
}
}
}
// LookupModuleByChannel will return the IBCModule along with the capability associated with a given channel defined by its portID and channelID
func (k Keeper) LookupModuleByChannel(ctx sdk.Context, portID, channelID string) (string, *capabilitytypes.Capability, error) {
modules, cap, err := k.scopedKeeper.LookupModules(ctx, host.ChannelCapabilityPath(portID, channelID))
if err != nil {
return "", nil, err
}
return porttypes.GetModuleOwner(modules), cap, nil
}
// SetPacketCommitment sets the packet commitment hash to the store
func (k Keeper) SetPacketCommitment(ctx sdk.Context, portID, channelID string, sequence uint64, commitmentHash []byte) {
store := ctx.KVStore(k.storeKey)
store.Set(host.PacketCommitmentKey(portID, channelID, sequence), commitmentHash)
}
func (k Keeper) GetAllPacketCommitments(ctx sdk.Context, cb func(data []byte, portID, channelID string, Seq uint64) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, []byte(host.KeyPacketCommitmentPrefix))
defer iter.Close()
for ; iter.Valid(); iter.Next() {
key := string(iter.Key())
keys := strings.Split(key, "/")
seq, _ := strconv.ParseUint(keys[3], 10, 64)
if cb(iter.Value(), keys[1], keys[2], seq) {
break
}
}
}
// GetPacketCommitment gets the packet commitment hash from the store
func (k Keeper) GetPacketCommitment(ctx sdk.Context, portID, channelID string, sequence uint64) []byte {
store := ctx.KVStore(k.storeKey)
bz := store.Get(host.PacketCommitmentKey(portID, channelID, sequence))
return bz
}
func (k Keeper) deletePacketCommitment(ctx sdk.Context, portID, channelID string, sequence uint64) {
store := ctx.KVStore(k.storeKey)
store.Delete(host.PacketCommitmentKey(portID, channelID, sequence))
}
// GetPacketReceipt gets a packet receipt from the store
func (k Keeper) GetPacketReceipt(ctx sdk.Context, portID, channelID string, sequence uint64) (string, bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(host.PacketReceiptKey(portID, channelID, sequence))
if bz == nil {
return "", false
}
return string(bz), true
}
// SetPacketReceipt sets an empty packet receipt to the store
func (k Keeper) SetPacketReceipt(ctx sdk.Context, portID, channelID string, sequence uint64) {
store := ctx.KVStore(k.storeKey)
store.Set(host.PacketReceiptKey(portID, channelID, sequence), []byte{byte(1)})
}
func (k Keeper) GetAllPacketReceipts(ctx sdk.Context, cb func(data []byte, portID, channelID string, Seq uint64) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, []byte(host.KeyPacketReceiptPrefix))
defer iter.Close()
for ; iter.Valid(); iter.Next() {
key := string(iter.Key())
keys := strings.Split(key, "/")
seq, _ := strconv.ParseUint(keys[3], 10, 64)
if cb(iter.Value(), keys[1], keys[2], seq) {
break
}
}
}
// HasPacketAcknowledgement check if the packet ack hash is already on the store
func (k Keeper) HasPacketAcknowledgement(ctx sdk.Context, portID, channelID string, sequence uint64) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(host.PacketAcknowledgementKey(portID, channelID, sequence))
}
// GetPacketAcknowledgement gets the packet ack hash from the store
func (k Keeper) GetPacketAcknowledgement(ctx sdk.Context, portID, channelID string, sequence uint64) ([]byte, bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(host.PacketAcknowledgementKey(portID, channelID, sequence))
if bz == nil {
return nil, false
}
return bz, true
}
// SetPacketAcknowledgement sets the packet ack hash to the store
func (k Keeper) SetPacketAcknowledgement(ctx sdk.Context, portID, channelID string, sequence uint64, ackHash []byte) {
store := ctx.KVStore(k.storeKey)
store.Set(host.PacketAcknowledgementKey(portID, channelID, sequence), ackHash)
}
func (k Keeper) GetAllPacketAAcknowledgements(ctx sdk.Context, cb func(data []byte, portID, channelID string, Seq uint64) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, []byte(host.KeyPacketAckPrefix))
defer iter.Close()
for ; iter.Valid(); iter.Next() {
key := string(iter.Key())
keys := strings.Split(key, "/")
seq, _ := strconv.ParseUint(keys[3], 10, 64)
if cb(iter.Value(), keys[1], keys[2], seq) {
break
}
}
}