-
Notifications
You must be signed in to change notification settings - Fork 28
/
htlc.go
191 lines (151 loc) · 5.16 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
package keeper
import (
"bytes"
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,
to sdk.AccAddress,
receiverOnOtherChain string,
amount sdk.Coins,
hashLock tmbytes.HexBytes,
timestamp,
timeLock uint64,
) error {
// check if the HTLC already exists
if k.HasHTLC(ctx, hashLock) {
return sdkerrors.Wrap(types.ErrHTLCExists, hashLock.String())
}
// transfer the specified tokens to the HTLC module account
err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, sender, types.ModuleName, amount)
if err != nil {
return err
}
expirationHeight := uint64(ctx.BlockHeight()) + timeLock
state := types.Open
htlc := types.NewHTLC(sender, to, receiverOnOtherChain, amount, nil, timestamp, expirationHeight, state)
// set the HTLC
k.SetHTLC(ctx, htlc, hashLock)
// add to the expiration queue
k.AddHTLCToExpiredQueue(ctx, htlc.ExpirationHeight, hashLock)
return nil
}
// ClaimHTLC claims the specified HTLC with the given secret
func (k Keeper) ClaimHTLC(ctx sdk.Context, hashLock tmbytes.HexBytes, secret tmbytes.HexBytes) error {
// query the HTLC
htlc, found := k.GetHTLC(ctx, hashLock)
if !found {
return sdkerrors.Wrap(types.ErrUnknownHTLC, hashLock.String())
}
// check if the HTLC is open
if htlc.State != types.Open {
return sdkerrors.Wrap(types.ErrHTLCNotOpen, hashLock.String())
}
// check if the secret matches with the hash lock
if !bytes.Equal(types.GetHashLock(secret, htlc.Timestamp), hashLock) {
return sdkerrors.Wrap(types.ErrInvalidSecret, secret.String())
}
// do the claim
err := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, htlc.To, htlc.Amount)
if err != nil {
return err
}
// update the secret and state of the HTLC
htlc.Secret = secret
htlc.State = types.Completed
k.SetHTLC(ctx, htlc, hashLock)
// delete from the expiration queue
k.DeleteHTLCFromExpiredQueue(ctx, htlc.ExpirationHeight, hashLock)
return nil
}
// RefundHTLC refunds the specified HTLC
func (k Keeper) RefundHTLC(ctx sdk.Context, hashLock tmbytes.HexBytes) error {
// query the HTLC
htlc, found := k.GetHTLC(ctx, hashLock)
if !found {
return sdkerrors.Wrap(types.ErrUnknownHTLC, hashLock.String())
}
// check if the HTLC is expired
if htlc.State != types.Expired {
return sdkerrors.Wrap(types.ErrHTLCNotExpired, hashLock.String())
}
// do the refund
err := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, htlc.Sender, htlc.Amount)
if err != nil {
return err
}
// update the state of the HTLC
htlc.State = types.Refunded
k.SetHTLC(ctx, htlc, hashLock)
return nil
}
// HasHTLC checks if the given HTLC exists
func (k Keeper) HasHTLC(ctx sdk.Context, hashLock tmbytes.HexBytes) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(types.GetHTLCKey(hashLock))
}
// SetHTLC sets the given HTLC
func (k Keeper) SetHTLC(ctx sdk.Context, htlc types.HTLC, hashLock tmbytes.HexBytes) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalBinaryBare(&htlc)
store.Set(types.GetHTLCKey(hashLock), bz)
}
// GetHTLC retrieves the specified HTLC
func (k Keeper) GetHTLC(ctx sdk.Context, hashLock tmbytes.HexBytes) (htlc types.HTLC, found bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetHTLCKey(hashLock))
if bz == nil {
return htlc, false
}
k.cdc.MustUnmarshalBinaryBare(bz, &htlc)
return htlc, true
}
// AddHTLCToExpiredQueue adds the specified HTLC to the expiration queue
func (k Keeper) AddHTLCToExpiredQueue(ctx sdk.Context, expirationHeight uint64, hashLock tmbytes.HexBytes) {
store := ctx.KVStore(k.storeKey)
store.Set(types.GetHTLCExpiredQueueKey(expirationHeight, hashLock), []byte{})
}
// DeleteHTLCFromExpiredQueue removes the specified HTLC from the expiration queue
func (k Keeper) DeleteHTLCFromExpiredQueue(ctx sdk.Context, expirationHeight uint64, hashLock tmbytes.HexBytes) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetHTLCExpiredQueueKey(expirationHeight, hashLock))
}
// IterateHTLCs iterates through the HTLCs
func (k Keeper) IterateHTLCs(
ctx sdk.Context,
op func(hlock 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() {
hashLock := tmbytes.HexBytes(iterator.Key()[1:])
var htlc types.HTLC
k.cdc.MustUnmarshalBinaryBare(iterator.Value(), &htlc)
if stop := op(hashLock, 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(hlock 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() {
hashLock := tmbytes.HexBytes(iterator.Key()[9:])
htlc, _ := k.GetHTLC(ctx, hashLock)
if stop := op(hashLock, htlc); stop {
break
}
}
}