-
Notifications
You must be signed in to change notification settings - Fork 28
/
htlc.go
406 lines (350 loc) · 11.8 KB
/
htlc.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package keeper
import (
"bytes"
"encoding/hex"
"fmt"
"time"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/irisnet/irismod/modules/htlc/types"
)
// CreateHTLC creates an HTLC
func (k Keeper) CreateHTLC(
ctx sdk.Context,
sender sdk.AccAddress,
to sdk.AccAddress,
receiverOnOtherChain string,
senderOnOtherChain string,
amount sdk.Coins,
hashLock tmbytes.HexBytes,
timestamp uint64,
timeLock uint64,
transfer bool,
) (
id tmbytes.HexBytes,
err error,
) {
id = types.GetID(sender, to, amount, hashLock)
// check if the HTLC already exists
if k.HasHTLC(ctx, id) {
return id, sdkerrors.Wrap(types.ErrHTLCExists, id.String())
}
expirationHeight := uint64(ctx.BlockHeight()) + timeLock
var direction types.SwapDirection
if transfer {
// create HTLT
if direction, err = k.createHTLT(
ctx, sender, to, receiverOnOtherChain, senderOnOtherChain,
amount, hashLock, timestamp, timeLock,
); err != nil {
return id, err
}
} else {
// create HTLT
if err = k.createHTLC(ctx, sender, amount); err != nil {
return id, err
}
}
htlc := types.NewHTLC(
id, sender, to, receiverOnOtherChain,
senderOnOtherChain, amount, hashLock,
nil, timestamp, expirationHeight,
types.Open, 0, transfer, direction,
)
// set the HTLC
k.SetHTLC(ctx, htlc, id)
// add to the expiration queue
k.AddHTLCToExpiredQueue(ctx, htlc.ExpirationHeight, id)
return id, nil
}
func (k Keeper) createHTLC(
ctx sdk.Context,
sender sdk.AccAddress,
amount sdk.Coins,
) error {
// transfer the specified tokens to the HTLC module account
return k.bankKeeper.SendCoinsFromAccountToModule(ctx, sender, types.ModuleName, amount)
}
func (k Keeper) createHTLT(
ctx sdk.Context,
sender sdk.AccAddress,
to sdk.AccAddress,
receiverOnOtherChain string,
senderOnOtherChain string,
amount sdk.Coins,
hashLock tmbytes.HexBytes,
timestamp uint64,
timeLock uint64,
) (
types.SwapDirection,
error,
) {
var direction types.SwapDirection
if len(amount) != 1 {
return direction, sdkerrors.Wrapf(types.ErrInvalidAmount, "amount %s must contain exactly one coin", amount.String())
}
asset, err := k.GetAsset(ctx, amount[0].Denom)
if err != nil {
return direction, err
}
if err = k.ValidateLiveAsset(ctx, amount[0]); err != nil {
return direction, err
}
// Swap amount must be within the specified swap amount limits
if amount[0].Amount.LT(asset.MinSwapAmount) || amount[0].Amount.GT(asset.MaxSwapAmount) {
return direction, sdkerrors.Wrapf(types.ErrInvalidAmount, "amount %s outside range [%s, %s]", amount[0].Amount, asset.MinSwapAmount.String(), asset.MaxSwapAmount)
}
// Unix timestamp must be in range [-15 mins, 30 mins) of the current time
pastTimestampLimit := ctx.BlockTime().Add(-15 * time.Minute).Unix()
futureTimestampLimit := ctx.BlockTime().Add(30 * time.Minute).Unix()
if timestamp < uint64(pastTimestampLimit) || timestamp >= uint64(futureTimestampLimit) {
return direction, sdkerrors.Wrap(
types.ErrInvalidTimestamp,
fmt.Sprintf(
"timestamp can neither be 15 minutes ahead of the current time, nor 30 minutes later. block time: %s, timestamp: %s",
ctx.BlockTime().String(), time.Unix(int64(timestamp), 0).UTC().String(),
),
)
}
deputyAddress, _ := sdk.AccAddressFromBech32(asset.DeputyAddress)
if sender.Equals(deputyAddress) {
if to.Equals(deputyAddress) {
return direction, sdkerrors.Wrapf(types.ErrInvalidAccount, "deputy cannot be both sender and receiver: %s", asset.DeputyAddress)
}
direction = types.Incoming
} else {
if !to.Equals(deputyAddress) {
return direction, sdkerrors.Wrapf(types.ErrInvalidAccount, "deputy must be recipient for outgoing account: %s", to)
}
direction = types.Outgoing
}
switch direction {
case types.Incoming:
// If recipient's account doesn't exist, register it in state so that the address can send
// a claim swap tx without needing to be registered in state by receiving a coin transfer.
recipientAcc := k.accountKeeper.GetAccount(ctx, deputyAddress)
if recipientAcc == nil {
acc := k.accountKeeper.NewAccountWithAddress(ctx, deputyAddress)
k.accountKeeper.SetAccount(ctx, acc)
}
// Incoming swaps have already had their fees collected by the deputy during the relay process.
if err := k.IncrementIncomingAssetSupply(ctx, amount[0]); err != nil {
return direction, err
}
case types.Outgoing:
// Outgoing swaps must have a time lock within the accepted range
if timeLock < asset.MinBlockLock || timeLock > asset.MaxBlockLock {
return direction, sdkerrors.Wrapf(types.ErrInvalidTimeLock, "time lock %d outside range [%d, %d]", timeLock, asset.MinBlockLock, asset.MaxBlockLock)
}
// Amount in outgoing swaps must be able to pay the deputy's fixed fee.
if amount[0].Amount.LT(asset.FixedFee.Add(asset.MinSwapAmount)) {
return direction, sdkerrors.Wrapf(
types.ErrInsufficientAmount,
"amount %s is less than fixed fee %s add min swap amount %s",
amount[0].String(), asset.FixedFee.String(), asset.MinSwapAmount.String(),
)
}
if err := k.IncrementOutgoingAssetSupply(ctx, amount[0]); err != nil {
return direction, err
}
// Transfer coins to module - only needed for outgoing swaps
if err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, sender, types.ModuleName, amount); err != nil {
return direction, err
}
default:
return direction, sdkerrors.Wrapf(types.ErrInvalidDirection, direction.String())
}
return direction, nil
}
// ClaimHTLC claims the specified HTLC with the given secret
func (k Keeper) ClaimHTLC(
ctx sdk.Context,
id tmbytes.HexBytes,
secret tmbytes.HexBytes,
) (
string,
bool,
types.SwapDirection,
error,
) {
// query the HTLC
htlc, found := k.GetHTLC(ctx, id)
if !found {
return "", false, types.None, sdkerrors.Wrap(types.ErrUnknownHTLC, id.String())
}
// check if the HTLC is open
if htlc.State != types.Open {
return "", false, types.None, sdkerrors.Wrap(types.ErrHTLCNotOpen, id.String())
}
hashLock, _ := hex.DecodeString(htlc.HashLock)
// check if the secret matches with the hash lock
if !bytes.Equal(types.GetHashLock(secret, htlc.Timestamp), hashLock) {
return "", false, types.None, sdkerrors.Wrap(types.ErrInvalidSecret, secret.String())
}
to, err := sdk.AccAddressFromBech32(htlc.To)
if err != nil {
return "", false, types.None, err
}
if htlc.Transfer {
if err := k.claimHTLT(ctx, htlc); err != nil {
return "", false, types.None, err
}
} else {
if err := k.claimHTLC(ctx, htlc.Amount, to); err != nil {
return "", false, types.None, err
}
}
// update the secret and state of the HTLC
htlc.Secret = secret.String()
htlc.State = types.Completed
htlc.ClosedBlock = uint64(ctx.BlockHeight())
k.SetHTLC(ctx, htlc, id)
// delete from the expiration queue
k.DeleteHTLCFromExpiredQueue(ctx, htlc.ExpirationHeight, id)
return htlc.HashLock, htlc.Transfer, htlc.Direction, nil
}
func (k Keeper) claimHTLC(ctx sdk.Context, amount sdk.Coins, to sdk.AccAddress) error {
return k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, to, amount)
}
func (k Keeper) claimHTLT(ctx sdk.Context, htlc types.HTLC) error {
switch htlc.Direction {
case types.Incoming:
if err := k.DecrementIncomingAssetSupply(ctx, htlc.Amount[0]); err != nil {
return err
}
if err := k.IncrementCurrentAssetSupply(ctx, htlc.Amount[0]); err != nil {
return err
}
// incoming case - coins should be MINTED, then sent to user
if err := k.bankKeeper.MintCoins(ctx, types.ModuleName, htlc.Amount); err != nil {
return err
}
// Send intended recipient coins
toAddr, _ := sdk.AccAddressFromBech32(htlc.To)
if err := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, toAddr, htlc.Amount); err != nil {
return err
}
case types.Outgoing:
if err := k.DecrementOutgoingAssetSupply(ctx, htlc.Amount[0]); err != nil {
return err
}
if err := k.DecrementCurrentAssetSupply(ctx, htlc.Amount[0]); err != nil {
return err
}
// outgoing case - coins should be burned
if err := k.bankKeeper.BurnCoins(ctx, types.ModuleName, htlc.Amount); err != nil {
return err
}
default:
return sdkerrors.Wrapf(types.ErrInvalidDirection, htlc.Direction.String())
}
return nil
}
// RefundHTLC refunds the specified HTLC
func (k Keeper) RefundHTLC(ctx sdk.Context, h types.HTLC, id tmbytes.HexBytes) error {
sender, err := sdk.AccAddressFromBech32(h.Sender)
if err != nil {
return err
}
if h.Transfer {
if err := k.refundHTLT(ctx, h.Direction, sender, h.Amount); err != nil {
return err
}
} else {
if err := k.refundHTLC(ctx, sender, h.Amount); err != nil {
return err
}
}
// update the state of the HTLC
h.State = types.Refunded
h.ClosedBlock = uint64(ctx.BlockHeight())
k.SetHTLC(ctx, h, id)
return nil
}
func (k Keeper) refundHTLC(ctx sdk.Context, sender sdk.AccAddress, amount sdk.Coins) error {
return k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, sender, amount)
}
func (k Keeper) refundHTLT(ctx sdk.Context, direction types.SwapDirection, sender sdk.AccAddress, amount sdk.Coins) error {
switch direction {
case types.Incoming:
if err := k.DecrementIncomingAssetSupply(ctx, amount[0]); err != nil {
return err
}
case types.Outgoing:
if err := k.DecrementOutgoingAssetSupply(ctx, amount[0]); err != nil {
return err
}
// Refund coins to original swap sender for outgoing swaps
if err := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, sender, amount); err != nil {
return err
}
default:
return fmt.Errorf("invalid direction")
}
return nil
}
// HasHTLC checks if the given HTLC exists
func (k Keeper) HasHTLC(ctx sdk.Context, id tmbytes.HexBytes) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(types.GetHTLCKey(id))
}
// SetHTLC sets the given HTLC
func (k Keeper) SetHTLC(ctx sdk.Context, htlc types.HTLC, id tmbytes.HexBytes) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&htlc)
store.Set(types.GetHTLCKey(id), bz)
}
// GetHTLC retrieves the specified HTLC
func (k Keeper) GetHTLC(ctx sdk.Context, id tmbytes.HexBytes) (htlc types.HTLC, found bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetHTLCKey(id))
if bz == nil {
return htlc, false
}
k.cdc.MustUnmarshal(bz, &htlc)
return htlc, true
}
// AddHTLCToExpiredQueue adds the specified HTLC to the expiration queue
func (k Keeper) AddHTLCToExpiredQueue(ctx sdk.Context, expirationHeight uint64, id tmbytes.HexBytes) {
store := ctx.KVStore(k.storeKey)
store.Set(types.GetHTLCExpiredQueueKey(expirationHeight, id), []byte{})
}
// DeleteHTLCFromExpiredQueue removes the specified HTLC from the expiration queue
func (k Keeper) DeleteHTLCFromExpiredQueue(ctx sdk.Context, expirationHeight uint64, id tmbytes.HexBytes) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetHTLCExpiredQueueKey(expirationHeight, id))
}
// IterateHTLCs iterates through the HTLCs
func (k Keeper) IterateHTLCs(
ctx sdk.Context,
op func(id tmbytes.HexBytes, h types.HTLC) (stop bool),
) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.HTLCKey)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
id := tmbytes.HexBytes(iterator.Key()[1:])
var htlc types.HTLC
k.cdc.MustUnmarshal(iterator.Value(), &htlc)
if stop := op(id, htlc); stop {
break
}
}
}
// IterateHTLCExpiredQueueByHeight iterates through the HTLC expiration queue by the specified height
func (k Keeper) IterateHTLCExpiredQueueByHeight(
ctx sdk.Context, height uint64,
op func(id tmbytes.HexBytes, h types.HTLC) (stop bool),
) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.GetHTLCExpiredQueueSubspace(height))
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
id := tmbytes.HexBytes(iterator.Key()[9:])
htlc, _ := k.GetHTLC(ctx, id)
if stop := op(id, htlc); stop {
break
}
}
}