-
Notifications
You must be signed in to change notification settings - Fork 72
/
redemptions.go
449 lines (384 loc) · 12.1 KB
/
redemptions.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
package tbtcpg
import (
"fmt"
"math/big"
"sort"
"time"
"github.com/ipfs/go-log/v2"
"go.uber.org/zap"
"github.com/keep-network/keep-core/internal/hexutils"
"github.com/keep-network/keep-core/pkg/bitcoin"
"github.com/keep-network/keep-core/pkg/tbtc"
)
// RedemptionTask is a task that may produce a redemption proposal.
type RedemptionTask struct {
chain Chain
btcChain bitcoin.Chain
}
func NewRedemptionTask(
chain Chain,
btcChain bitcoin.Chain,
) *RedemptionTask {
return &RedemptionTask{
chain: chain,
btcChain: btcChain,
}
}
func (rt *RedemptionTask) Run(request *tbtc.CoordinationProposalRequest) (
tbtc.CoordinationProposal,
bool,
error,
) {
walletPublicKeyHash := request.WalletPublicKeyHash
taskLogger := logger.With(
zap.String("task", rt.ActionType().String()),
zap.String("walletPKH", fmt.Sprintf("0x%x", walletPublicKeyHash)),
)
redemptionMaxSize, err := rt.chain.GetRedemptionMaxSize()
if err != nil {
return nil, false, fmt.Errorf(
"failed to get redemption max size: [%w]",
err,
)
}
redeemersOutputScripts, err := rt.FindPendingRedemptions(
taskLogger,
walletPublicKeyHash,
redemptionMaxSize,
)
if err != nil {
return nil, false, fmt.Errorf(
"cannot find pending redemption requests: [%w]",
err,
)
}
if len(redeemersOutputScripts) == 0 {
taskLogger.Info("no pending redemption requests")
return nil, false, nil
}
proposal, err := rt.ProposeRedemption(
taskLogger,
walletPublicKeyHash,
redeemersOutputScripts,
0,
)
if err != nil {
return nil, false, fmt.Errorf(
"cannot prepare redemption proposal: [%w]",
err,
)
}
return proposal, true, nil
}
func (rt *RedemptionTask) ActionType() tbtc.WalletActionType {
return tbtc.ActionRedemption
}
// RedemptionRequest represents a redemption request.
type RedemptionRequest struct {
WalletPublicKeyHash [20]byte
RedemptionKey string
RedeemerOutputScript bitcoin.Script
RequestedAt time.Time
RequestedAmount uint64
}
// FindPendingRedemptions finds pending redemptions requests for the
// provided wallet. The returned value is a list of redeemers output
// scripts that come from detected pending requests targeting this wallet.
// The maxNumberOfRequests parameter is used as a ceiling for the number of
// requests in the result. If number of discovered requests meets the
// maxNumberOfRequests the function will stop fetching more requests.
func (rt *RedemptionTask) FindPendingRedemptions(
taskLogger log.StandardLogger,
walletPublicKeyHash [20]byte,
maxNumberOfRequests uint16,
) ([]bitcoin.Script, error) {
if walletPublicKeyHash == [20]byte{} {
return nil, fmt.Errorf("wallet public key hash is required")
}
taskLogger.Infof(
"fetching max [%d] redemption requests",
maxNumberOfRequests,
)
blockCounter, err := rt.chain.BlockCounter()
if err != nil {
return nil, fmt.Errorf(
"failed to get block counter: [%w]",
err,
)
}
currentBlockNumber, err := blockCounter.CurrentBlock()
if err != nil {
return nil, fmt.Errorf(
"failed to get current block number: [%w]",
err,
)
}
requestMinAge, err := rt.chain.GetRedemptionRequestMinAge()
if err != nil {
return nil, fmt.Errorf(
"failed to get redemption request minimum age: [%w]",
err,
)
}
_, _, _, _, requestTimeout, _, _, err := rt.chain.GetRedemptionParameters()
if err != nil {
return nil, fmt.Errorf(
"failed to get redemption parameters: [%w]",
err,
)
}
taskLogger.Infof("fetching pending redemption requests")
pendingRedemptions, err := findPendingRedemptions(
taskLogger,
rt.chain,
walletPublicKeyHash,
currentBlockNumber,
maxNumberOfRequests,
requestTimeout,
requestMinAge,
)
if err != nil {
return nil, fmt.Errorf("cannot get pending redemptions: [%w]", err)
}
taskLogger.Infof("found [%d] redemption requests", len(pendingRedemptions))
result := make([]bitcoin.Script, 0)
for _, pendingRedemption := range pendingRedemptions {
taskLogger.Infof(
"redemption request [%s] - requested at: [%s]",
pendingRedemption.RedemptionKey,
pendingRedemption.RequestedAt,
)
result = append(result, pendingRedemption.RedeemerOutputScript)
}
return result, nil
}
// ProposeRedemption returns a redemption proposal.
func (rt *RedemptionTask) ProposeRedemption(
taskLogger log.StandardLogger,
walletPublicKeyHash [20]byte,
redeemersOutputScripts []bitcoin.Script,
fee int64,
) (*tbtc.RedemptionProposal, error) {
if len(redeemersOutputScripts) == 0 {
return nil, fmt.Errorf("redemptions list is empty")
}
taskLogger.Infof("preparing a redemption proposal")
// Estimate fee if it's missing. Do not check the estimated fee against
// the maximum total and per-request fees allowed by the Bridge. This
// is done during the on-chain validation of the proposal so there is no
// need to do it here.
if fee <= 0 {
taskLogger.Infof("estimating redemption transaction fee")
estimatedFee, err := EstimateRedemptionFee(
rt.btcChain,
redeemersOutputScripts,
)
if err != nil {
return nil, fmt.Errorf(
"cannot estimate redemption transaction fee: [%w]",
err,
)
}
fee = estimatedFee
}
taskLogger.Infof("redemption transaction fee: [%d]", fee)
proposal := &tbtc.RedemptionProposal{
RedeemersOutputScripts: redeemersOutputScripts,
RedemptionTxFee: big.NewInt(fee),
}
taskLogger.Infof("validating the redemption proposal")
if _, err := tbtc.ValidateRedemptionProposal(
taskLogger,
walletPublicKeyHash,
proposal,
rt.chain,
); err != nil {
return nil, fmt.Errorf("failed to verify redemption proposal: %v", err)
}
return proposal, nil
}
func findPendingRedemptions(
fnLogger log.StandardLogger,
chain Chain,
walletPublicKeyHash [20]byte,
currentBlockNumber uint64,
requestsLimit uint16,
requestTimeout uint32,
requestMinAge uint32,
) ([]*RedemptionRequest, error) {
// We are interested with `RedemptionRequested` events that are not
// timed out yet. That means there is no sense to look for events that
// occurred earlier than `now - requestTimeout`. However,
// the event filter expects a block range while `requestTimeout`
// is in seconds. To overcome that problem, we estimate the redemption
// request timeout in blocks, using the average block time of the host chain.
// Note that this estimation is not 100% accurate as the actual block time
// may differ from the assumed one.
requestTimeoutBlocks :=
uint64(requestTimeout) / uint64(chain.AverageBlockTime().Seconds())
// Then, we set the start block of the filter using the estimated redemption
// request timeout in blocks. Note that if the actual average block time is
// lesser than the assumed one, some events being on the edge of the block
// range may be omitted. To avoid that, we make the block range a little
// wider by using a constant factor of 1000 blocks.
filterStartBlock := uint64(0)
if filterLookbackBlocks := requestTimeoutBlocks + 1000; currentBlockNumber > filterLookbackBlocks {
filterStartBlock = currentBlockNumber - filterLookbackBlocks
}
filter := &tbtc.RedemptionRequestedEventFilter{
StartBlock: filterStartBlock,
}
if walletPublicKeyHash != [20]byte{} {
filter.WalletPublicKeyHash = [][20]byte{walletPublicKeyHash}
}
events, err := chain.PastRedemptionRequestedEvents(filter)
if err != nil {
return nil, fmt.Errorf(
"failed to get past redemption requested events: [%w]",
err,
)
}
// Take the oldest first.
sort.SliceStable(
events, func(i, j int) bool {
return events[i].BlockNumber < events[j].BlockNumber
},
)
// There may be multiple events targeting the same redemption key
// (i.e. the same wallet and output script pair). The Bridge contract
// allows only for one pending request with the given redemption key
// at the same time. That means we need to deduplicate the events list
// and take only the latest event for the given redemption key.
eventsSet := make(map[string]*tbtc.RedemptionRequestedEvent)
for _, event := range events {
redemptionKey, err := chain.BuildRedemptionKey(
event.WalletPublicKeyHash,
event.RedeemerOutputScript,
)
if err != nil {
return nil, fmt.Errorf("failed to build redemption key: [%v]", err)
}
// Events are sorted from the oldest to the newest so there is no
// need to check for existence. We can just overwrite.
eventsSet[hexutils.Encode(redemptionKey.Bytes())] = event
}
fnLogger.Infof("found [%d] RedemptionRequested events", len(eventsSet))
fnLogger.Infof("checking pending redemptions details")
pendingRedemptions := make([]*RedemptionRequest, 0)
eventIndex := 0
redemptionRequestedLoop:
for redemptionKey, event := range eventsSet {
eventIndex++
fnLogger.Debugf(
"getting pending redemption details [%s]",
redemptionKey,
)
// Check if there is still a pending redemption for the given redemption
// requested event.
pendingRedemption, found, err := chain.GetPendingRedemptionRequest(
event.WalletPublicKeyHash,
event.RedeemerOutputScript,
)
if err != nil {
return nil, fmt.Errorf(
"failed to get pending redemption request: [%w]",
err,
)
}
if !found {
fnLogger.Infof(
"redemption request [%s] is no longer pending",
redemptionKey,
)
continue redemptionRequestedLoop
}
pendingRedemptions = append(
pendingRedemptions, &RedemptionRequest{
WalletPublicKeyHash: event.WalletPublicKeyHash,
RedemptionKey: redemptionKey,
RedeemerOutputScript: event.RedeemerOutputScript,
RequestedAt: pendingRedemption.RequestedAt,
RequestedAmount: pendingRedemption.RequestedAmount,
},
)
}
resultSliceCapacity := len(pendingRedemptions)
if requestsLimit > 0 {
resultSliceCapacity = int(requestsLimit)
}
// Sort the pending redemptions from oldest to newest.
sort.SliceStable(
pendingRedemptions, func(i, j int) bool {
return pendingRedemptions[i].RequestedAt.Before(pendingRedemptions[j].RequestedAt)
},
)
// Only redemption requests in range:
// [now - requestTimeout, now - requestMinAge]
// should be taken into consideration.
redemptionRequestsRangeStartTimestamp := time.Now().Add(
-time.Duration(requestTimeout) * time.Second,
)
redemptionRequestsRangeEndTimestamp := time.Now().Add(
-time.Duration(requestMinAge) * time.Second,
)
result := make([]*RedemptionRequest, 0, resultSliceCapacity)
for _, pendingRedemption := range pendingRedemptions {
if len(result) == cap(result) {
break
}
// Check if timeout passed for the redemption request.
if pendingRedemption.RequestedAt.Before(redemptionRequestsRangeStartTimestamp) {
fnLogger.Infof(
"redemption request [%s] has already timed out",
pendingRedemption.RedemptionKey,
)
continue
}
// Check if enough time elapsed since the redemption request.
if pendingRedemption.RequestedAt.After(redemptionRequestsRangeEndTimestamp) {
fnLogger.Infof(
"redemption request [%s] is not old enough",
pendingRedemption.RedemptionKey,
)
continue
}
result = append(result, pendingRedemption)
}
return result, nil
}
// EstimateRedemptionFee estimates fee for the redemption transaction that pays
// the provided redeemers output scripts.
func EstimateRedemptionFee(
btcChain bitcoin.Chain,
redeemersOutputScripts []bitcoin.Script,
) (int64, error) {
sizeEstimator := bitcoin.NewTransactionSizeEstimator().
// 1 P2WPKH main UTXO input.
AddPublicKeyHashInputs(1, true).
// 1 P2WPKH change output.
AddPublicKeyHashOutputs(1, true)
for _, script := range redeemersOutputScripts {
switch bitcoin.GetScriptType(script) {
case bitcoin.P2PKHScript:
sizeEstimator.AddPublicKeyHashOutputs(1, false)
case bitcoin.P2WPKHScript:
sizeEstimator.AddPublicKeyHashOutputs(1, true)
case bitcoin.P2SHScript:
sizeEstimator.AddScriptHashOutputs(1, false)
case bitcoin.P2WSHScript:
sizeEstimator.AddScriptHashOutputs(1, true)
default:
return 0, fmt.Errorf("non-standard redeemer output script type")
}
}
transactionSize, err := sizeEstimator.VirtualSize()
if err != nil {
return 0, fmt.Errorf("cannot estimate transaction virtual size: [%v]", err)
}
feeEstimator := bitcoin.NewTransactionFeeEstimator(btcChain)
totalFee, err := feeEstimator.EstimateFee(transactionSize)
if err != nil {
return 0, fmt.Errorf("cannot estimate transaction fee: [%v]", err)
}
return totalFee, nil
}