-
Notifications
You must be signed in to change notification settings - Fork 0
/
keeper.go
276 lines (252 loc) · 8.88 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
package keeper
import (
"fmt"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper"
ibcclienttypes "github.com/cosmos/cosmos-sdk/x/ibc/core/02-client/types"
channeltypes "github.com/cosmos/cosmos-sdk/x/ibc/core/04-channel/types"
host "github.com/cosmos/cosmos-sdk/x/ibc/core/24-host"
"github.com/gogo/protobuf/proto"
"github.com/tendermint/tendermint/libs/log"
clienttypes "github.com/datachainlab/cosmos-sdk-interchain-dns/x/ibc-dns/client/types"
"github.com/datachainlab/cosmos-sdk-interchain-dns/x/ibc-dns/common/types"
servertypes "github.com/datachainlab/cosmos-sdk-interchain-dns/x/ibc-dns/server/types"
)
// Keeper defines ibc-dns keeper
type Keeper struct {
cdc codec.BinaryMarshaler
storeKey sdk.StoreKey
clientKeeper types.ClientKeeper
connectionKeeper types.ConnectionKeeper
channelKeeper types.ChannelKeeper
scopedKeeper capabilitykeeper.ScopedKeeper
}
// NewKeeper creates a new ibc-dns Keeper
func NewKeeper(
cdc codec.BinaryMarshaler,
storeKey sdk.StoreKey,
clientKeeper types.ClientKeeper,
connectionKeeper types.ConnectionKeeper,
channelKeeper types.ChannelKeeper,
scopedKeeper capabilitykeeper.ScopedKeeper,
) Keeper {
return Keeper{
cdc: cdc,
storeKey: storeKey,
clientKeeper: clientKeeper,
connectionKeeper: connectionKeeper,
channelKeeper: channelKeeper,
scopedKeeper: scopedKeeper,
}
}
// SendPacketRegisterDomain sends a packet to register a channel name on DNS server
func (k Keeper) SendPacketRegisterDomain(
ctx sdk.Context,
name string,
sourcePort string,
sourceChannel string,
metadata []byte,
) (*channeltypes.Packet, error) {
data := servertypes.NewRegisterDomainPacketData(name, metadata)
c, found := k.channelKeeper.GetChannel(ctx, sourcePort, sourceChannel)
if !found {
return nil, fmt.Errorf("channel not found: port=%v channel=%v", sourcePort, sourceChannel)
}
return k.sendPacket(
ctx,
data.GetBytes(),
sourcePort, sourceChannel,
c.GetCounterparty().GetPortID(),
c.GetCounterparty().GetChannelID(),
data.GetTimeoutHeight(),
data.GetTimeoutTimestamp(),
)
}
// ReceiveRegisterDomainPacketAcknowledgement receive an ack to set self-domain name
func (k Keeper) ReceiveRegisterDomainPacketAcknowledgement(ctx sdk.Context, status uint32, domain string, packet channeltypes.Packet) error {
if status != servertypes.STATUS_OK {
k.Logger(ctx).Info("failed to register a channel domain", "status", status, "domain", domain)
return nil
}
return k.setSelfDomainName(ctx, types.NewLocalDNSID(packet.SourcePort, packet.SourceChannel), domain)
}
// SendDomainAssociationCreatePacketData sends a packet to associate a domain with client on DNS server
func (k Keeper) SendDomainAssociationCreatePacketData(
ctx sdk.Context,
dnsID types.LocalDNSID,
srcClient types.ClientDomain,
dstClient types.ClientDomain,
) (*channeltypes.Packet, error) {
_, found := k.GetSelfDomainName(ctx, dnsID)
if !found {
return nil, fmt.Errorf("this channel does not have a domain name: dnsID=%v", dnsID.String())
}
_, found = k.clientKeeper.GetClientState(ctx, srcClient.ClientId)
if !found {
return nil, fmt.Errorf("client state not found: clientID=%v", srcClient.ClientId)
}
data := servertypes.NewDomainAssociationCreatePacketData(srcClient, dstClient)
c, found := k.channelKeeper.GetChannel(ctx, dnsID.SourcePort, dnsID.SourceChannel)
if !found {
return nil, fmt.Errorf("channel not found: port=%v channel=%v", dnsID.SourcePort, dnsID.SourceChannel)
}
return k.sendPacket(
ctx, data.GetBytes(),
dnsID.SourcePort,
dnsID.SourceChannel,
c.GetCounterparty().GetPortID(),
c.GetCounterparty().GetChannelID(),
data.GetTimeoutHeight(),
data.GetTimeoutTimestamp(),
)
}
// ReceiveDomainAssociationCreatePacketAcknowledgement receive an ack
func (k Keeper) ReceiveDomainAssociationCreatePacketAcknowledgement(ctx sdk.Context, status uint32) error {
// TODO cleanup any state
if status != servertypes.STATUS_OK {
k.Logger(ctx).Info("failed to register a local channel", "status", status)
return nil
}
return nil
}
// ReceiveDomainAssociationResultPacketData receives a packet to save a domain info on local state
func (k Keeper) ReceiveDomainAssociationResultPacketData(
ctx sdk.Context,
packet channeltypes.Packet,
data *servertypes.DomainAssociationResultPacketData,
) error {
dnsID := types.NewLocalDNSID(packet.DestinationPort, packet.DestinationChannel)
_, found := k.GetSelfDomainName(ctx, dnsID)
if !found {
return fmt.Errorf("failed to GetSelfDomainName")
}
store := ctx.KVStore(k.storeKey)
bz0, err := proto.Marshal(&data.CounterpartyDomain.DNSID)
if err != nil {
return err
}
store.Set(clienttypes.KeyLocalDNSID(dnsID, data.CounterpartyDomain.Name), bz0)
store.Set(clienttypes.KeyClientDomain(dnsID, data.CounterpartyDomain.Name), []byte(data.ClientId))
return nil
}
// GetSelfDomainName returns self-domain name
func (k Keeper) GetSelfDomainName(ctx sdk.Context, dnsID types.LocalDNSID) (string, bool) {
store := ctx.KVStore(k.storeKey)
key := clienttypes.KeySelfDomain(dnsID)
domain := store.Get(key)
if domain == nil {
return "", false
}
return string(domain), true
}
// ResolveDNSID returns local DNS-ID corresponding to given a domain info
func (k Keeper) ResolveDNSID(ctx sdk.Context, domain types.LocalDomain) (types.LocalDNSID, bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(clienttypes.KeyLocalDNSID(domain.DNSID, domain.Name))
if bz == nil {
return types.LocalDNSID{}, false
}
var id types.LocalDNSID
err := proto.Unmarshal(bz, &id)
if err != nil {
panic(err)
}
return id, true
}
// ResolveChannel returns the channel corresponding to given a domain info
func (k Keeper) ResolveChannel(ctx sdk.Context, domain types.LocalDomain, portID string) (channeltypes.Channel, bool) {
key := clienttypes.KeyDomainChannel(domain.DNSID, domain.Name, portID)
store := ctx.KVStore(k.storeKey)
bz := store.Get(key)
if bz == nil {
return channeltypes.Channel{}, false
}
var lc types.LocalChannel
err := proto.Unmarshal(bz, &lc)
if err != nil {
return channeltypes.Channel{}, false
}
c, found := k.channelKeeper.GetChannel(ctx, lc.SourcePort, lc.SourceChannel)
if !found {
return channeltypes.Channel{}, false
}
return c, true
}
// SetDomainChannel sets a channel corresponding with the domain name
func (k Keeper) SetDomainChannel(ctx sdk.Context, dnsID types.LocalDNSID, domainName string, channel types.LocalChannel) error {
c, found := k.channelKeeper.GetChannel(ctx, channel.SourcePort, channel.SourceChannel)
if !found {
return fmt.Errorf("channel not found: port=%v channel=%v", channel.SourcePort, channel.SourceChannel)
}
connID := c.ConnectionHops[0]
conn, found := k.connectionKeeper.GetConnection(ctx, connID)
if !found {
return fmt.Errorf("connection not found: connectionID=%v", connID)
}
store := ctx.KVStore(k.storeKey)
counterpartyClientID := store.Get(clienttypes.KeyClientDomain(dnsID, domainName))
if counterpartyClientID == nil {
return fmt.Errorf("clientID not found: dnsID=%v domainName=%v", dnsID.String(), domainName)
}
if string(counterpartyClientID) != conn.Counterparty.ClientId {
return fmt.Errorf("clientID mismatch: %v != %v", string(counterpartyClientID), conn.Counterparty.ClientId)
}
bz, err := proto.Marshal(&channel)
if err != nil {
return err
}
store.Set(clienttypes.KeyDomainChannel(dnsID, domainName, channel.SourcePort), bz)
return nil
}
func (k Keeper) setSelfDomainName(ctx sdk.Context, dnsID types.LocalDNSID, domain string) error {
if _, found := k.GetSelfDomainName(ctx, dnsID); found {
return fmt.Errorf("Domain name is already set")
}
store := ctx.KVStore(k.storeKey)
key := clienttypes.KeySelfDomain(dnsID)
store.Set(key, []byte(domain))
return nil
}
func (k Keeper) Codec() codec.BinaryMarshaler {
return k.cdc
}
// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "ibc/dns/client")
}
func (k Keeper) sendPacket(
ctx sdk.Context,
data []byte,
sourcePort,
sourceChannel,
destinationPort,
destinationChannel string,
timeoutHeight ibcclienttypes.Height,
timeoutTimestamp uint64,
) (*channeltypes.Packet, error) {
// get the next sequence
seq, found := k.channelKeeper.GetNextSequenceSend(ctx, sourcePort, sourceChannel)
if !found {
return nil, channeltypes.ErrSequenceSendNotFound
}
packet := channeltypes.NewPacket(
data,
seq,
sourcePort,
sourceChannel,
destinationPort,
destinationChannel,
timeoutHeight,
timeoutTimestamp,
)
channelCap, ok := k.scopedKeeper.GetCapability(ctx, host.ChannelCapabilityPath(sourcePort, sourceChannel))
if !ok {
return nil, sdkerrors.Wrap(channeltypes.ErrChannelCapabilityNotFound, "module does not own channel capability")
}
if err := k.channelKeeper.SendPacket(ctx, channelCap, packet); err != nil {
return nil, err
}
return &packet, nil
}