-
Notifications
You must be signed in to change notification settings - Fork 655
/
Copy pathkeeper.go
256 lines (215 loc) · 8.41 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
package keeper
import (
"context"
"errors"
corestore "cosmossdk.io/core/store"
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/log"
storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types"
"github.com/cosmos/ibc-go/v9/modules/core/03-connection/types"
commitmenttypes "github.com/cosmos/ibc-go/v9/modules/core/23-commitment/types"
host "github.com/cosmos/ibc-go/v9/modules/core/24-host"
"github.com/cosmos/ibc-go/v9/modules/core/exported"
)
// Keeper defines the IBC connection keeper
type Keeper struct {
// implements gRPC QueryServer interface
types.QueryServer
storeService corestore.KVStoreService
legacySubspace types.ParamSubspace
cdc codec.BinaryCodec
clientKeeper types.ClientKeeper
}
// NewKeeper creates a new IBC connection Keeper instance
func NewKeeper(cdc codec.BinaryCodec, storeService corestore.KVStoreService, legacySubspace types.ParamSubspace, ck types.ClientKeeper) *Keeper {
return &Keeper{
storeService: storeService,
cdc: cdc,
legacySubspace: legacySubspace,
clientKeeper: ck,
}
}
// Logger returns a module-specific logger.
func (Keeper) Logger(ctx context.Context) log.Logger {
sdkCtx := sdk.UnwrapSDKContext(ctx)
return sdkCtx.Logger().With("module", "x/"+exported.ModuleName+"/"+types.SubModuleName)
}
// GetCommitmentPrefix returns the IBC connection store prefix as a commitment
// Prefix
func (*Keeper) GetCommitmentPrefix() exported.Prefix {
return commitmenttypes.NewMerklePrefix([]byte(exported.StoreKey))
}
// GenerateConnectionIdentifier returns the next connection identifier.
func (k *Keeper) GenerateConnectionIdentifier(ctx context.Context) string {
nextConnSeq := k.GetNextConnectionSequence(ctx)
connectionID := types.FormatConnectionIdentifier(nextConnSeq)
nextConnSeq++
k.SetNextConnectionSequence(ctx, nextConnSeq)
return connectionID
}
// GetConnection returns a connection with a particular identifier
func (k *Keeper) GetConnection(ctx context.Context, connectionID string) (types.ConnectionEnd, bool) {
store := k.storeService.OpenKVStore(ctx)
bz, err := store.Get(host.ConnectionKey(connectionID))
if err != nil {
panic(err)
}
if len(bz) == 0 {
return types.ConnectionEnd{}, false
}
var connection types.ConnectionEnd
k.cdc.MustUnmarshal(bz, &connection)
return connection, true
}
// HasConnection returns a true if the connection with the given identifier
// exists in the store.
func (k *Keeper) HasConnection(ctx context.Context, connectionID string) bool {
store := k.storeService.OpenKVStore(ctx)
has, err := store.Has(host.ConnectionKey(connectionID))
if err != nil {
return false
}
return has
}
// SetConnection sets a connection to the store
func (k *Keeper) SetConnection(ctx context.Context, connectionID string, connection types.ConnectionEnd) {
store := k.storeService.OpenKVStore(ctx)
bz := k.cdc.MustMarshal(&connection)
if err := store.Set(host.ConnectionKey(connectionID), bz); err != nil {
panic(err)
}
}
// GetClientConnectionPaths returns all the connection paths stored under a
// particular client
func (k *Keeper) GetClientConnectionPaths(ctx context.Context, clientID string) ([]string, bool) {
store := k.storeService.OpenKVStore(ctx)
bz, err := store.Get(host.ClientConnectionsKey(clientID))
if err != nil {
panic(err)
}
if len(bz) == 0 {
return nil, false
}
var clientPaths types.ClientPaths
k.cdc.MustUnmarshal(bz, &clientPaths)
return clientPaths.Paths, true
}
// SetClientConnectionPaths sets the connections paths for client
func (k *Keeper) SetClientConnectionPaths(ctx context.Context, clientID string, paths []string) {
store := k.storeService.OpenKVStore(ctx)
clientPaths := types.ClientPaths{Paths: paths}
bz := k.cdc.MustMarshal(&clientPaths)
if err := store.Set(host.ClientConnectionsKey(clientID), bz); err != nil {
panic(err)
}
}
// GetNextConnectionSequence gets the next connection sequence from the store.
func (k *Keeper) GetNextConnectionSequence(ctx context.Context) uint64 {
store := k.storeService.OpenKVStore(ctx)
bz, err := store.Get([]byte(types.KeyNextConnectionSequence))
if err != nil {
panic(err)
}
if len(bz) == 0 {
panic(errors.New("next connection sequence is nil"))
}
return sdk.BigEndianToUint64(bz)
}
// SetNextConnectionSequence sets the next connection sequence to the store.
func (k *Keeper) SetNextConnectionSequence(ctx context.Context, sequence uint64) {
store := k.storeService.OpenKVStore(ctx)
bz := sdk.Uint64ToBigEndian(sequence)
if err := store.Set([]byte(types.KeyNextConnectionSequence), bz); err != nil {
panic(err)
}
}
// GetAllClientConnectionPaths returns all stored clients connection id paths. It
// will ignore the clients that haven't initialized a connection handshake since
// no paths are stored.
func (k *Keeper) GetAllClientConnectionPaths(ctx context.Context) []types.ConnectionPaths {
var allConnectionPaths []types.ConnectionPaths
sdkCtx := sdk.UnwrapSDKContext(ctx)
k.clientKeeper.IterateClientStates(sdkCtx, nil, func(clientID string, cs exported.ClientState) bool {
paths, found := k.GetClientConnectionPaths(ctx, clientID)
if !found {
// continue when connection handshake is not initialized
return false
}
connPaths := types.NewConnectionPaths(clientID, paths)
allConnectionPaths = append(allConnectionPaths, connPaths)
return false
})
return allConnectionPaths
}
// IterateConnections provides an iterator over all ConnectionEnd objects.
// For each ConnectionEnd, cb will be called. If the cb returns true, the
// iterator will close and stop.
func (k *Keeper) IterateConnections(ctx context.Context, cb func(types.IdentifiedConnection) bool) {
store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
iterator := storetypes.KVStorePrefixIterator(store, []byte(host.KeyConnectionPrefix))
defer sdk.LogDeferred(k.Logger(ctx), func() error { return iterator.Close() })
for ; iterator.Valid(); iterator.Next() {
var connection types.ConnectionEnd
k.cdc.MustUnmarshal(iterator.Value(), &connection)
connectionID := host.MustParseConnectionPath(string(iterator.Key()))
identifiedConnection := types.NewIdentifiedConnection(connectionID, connection)
if cb(identifiedConnection) {
break
}
}
}
// GetAllConnections returns all stored ConnectionEnd objects.
func (k *Keeper) GetAllConnections(ctx context.Context) (connections []types.IdentifiedConnection) {
k.IterateConnections(ctx, func(connection types.IdentifiedConnection) bool {
connections = append(connections, connection)
return false
})
return connections
}
// CreateSentinelLocalhostConnection creates and sets the sentinel localhost connection end in the IBC store.
func (k *Keeper) CreateSentinelLocalhostConnection(ctx context.Context) {
counterparty := types.NewCounterparty(exported.LocalhostClientID, exported.LocalhostConnectionID, commitmenttypes.NewMerklePrefix(k.GetCommitmentPrefix().Bytes()))
connectionEnd := types.NewConnectionEnd(types.OPEN, exported.LocalhostClientID, counterparty, types.GetCompatibleVersions(), 0)
k.SetConnection(ctx, exported.LocalhostConnectionID, connectionEnd)
}
// addConnectionToClient is used to add a connection identifier to the set of
// connections associated with a client.
func (k *Keeper) addConnectionToClient(ctx context.Context, clientID, connectionID string) error {
_, found := k.clientKeeper.GetClientState(ctx, clientID)
if !found {
return errorsmod.Wrap(clienttypes.ErrClientNotFound, clientID)
}
conns, found := k.GetClientConnectionPaths(ctx, clientID)
if !found {
conns = []string{}
}
conns = append(conns, connectionID)
k.SetClientConnectionPaths(ctx, clientID, conns)
return nil
}
// GetParams returns the total set of ibc-connection parameters.
func (k *Keeper) GetParams(ctx context.Context) types.Params {
store := k.storeService.OpenKVStore(ctx)
bz, err := store.Get([]byte(types.ParamsKey))
if err != nil {
panic(err)
}
if bz == nil { // only panic on unset params and not on empty params
panic(errors.New("connection params are not set in store"))
}
var params types.Params
k.cdc.MustUnmarshal(bz, ¶ms)
return params
}
// SetParams sets the total set of ibc-connection parameters.
func (k *Keeper) SetParams(ctx context.Context, params types.Params) {
store := k.storeService.OpenKVStore(ctx)
bz := k.cdc.MustMarshal(¶ms)
if err := store.Set([]byte(types.ParamsKey), bz); err != nil {
panic(err)
}
}