-
Notifications
You must be signed in to change notification settings - Fork 1
/
htlc.go
217 lines (174 loc) · 5.5 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
package keeper
import (
"bytes"
"encoding/hex"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/komodoplatform/nucleus/x/htlc/types"
)
// CreateHTLC creates an HTLC
func (k Keeper) CreateHTLC(
ctx sdk.Context,
sender sdk.AccAddress,
to sdk.AccAddress,
amount sdk.Coins,
hashLock tmbytes.HexBytes,
timestamp uint64,
timeLock uint64,
) (
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
if err = k.createHTLC(ctx, sender, amount); err != nil {
return id, err
}
htlc := types.NewHTLC(
id, sender, to, amount, hashLock,
nil, timestamp, expirationHeight,
types.Open, 0,
)
// 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)
}
// ClaimHTLC claims the specified HTLC with the given secret
func (k Keeper) ClaimHTLC(
ctx sdk.Context,
id tmbytes.HexBytes,
secret tmbytes.HexBytes,
) (
string,
error,
) {
// query the HTLC
htlc, found := k.GetHTLC(ctx, id)
if !found {
return "", sdkerrors.Wrap(types.ErrUnknownHTLC, id.String())
}
// check if the HTLC is open
if htlc.State != types.Open {
return "", 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 "", sdkerrors.Wrap(types.ErrInvalidSecret, secret.String())
}
to, err := sdk.AccAddressFromBech32(htlc.To)
if err != nil {
return "", err
}
if err := k.claimHTLC(ctx, htlc.Amount, to); err != nil {
return "", 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, nil
}
func (k Keeper) claimHTLC(ctx sdk.Context, amount sdk.Coins, to sdk.AccAddress) error {
return k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, to, amount)
}
// 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 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)
}
// 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
}
}
}