-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathbatch.go
144 lines (128 loc) · 4.46 KB
/
batch.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
package funding
import (
"context"
"fmt"
"strings"
"github.com/btcsuite/btcd/wire"
"github.com/lightninglabs/lndclient"
"github.com/lightninglabs/pool/order"
"github.com/lightningnetwork/lnd/lnrpc"
)
// CancelPendingFundingShims cancels all funding shims we registered when
// preparing for the orders in a batch. This should be called if for any reason
// we need to reject the batch, so we're able to process any subsequent modified
// batches.
func CancelPendingFundingShims(matchedOrders map[order.Nonce][]*order.MatchedOrder,
baseClient BaseClient, fetchOrder order.Fetcher) error {
// Since we support partial matches, a given bid of ours could've been
// matched with multiple asks, so we'll iterate through all those to
// ensure we unregister all the created shims.
ctxb := context.Background()
for ourOrderNonce, matchedOrders := range matchedOrders {
ourOrder, err := fetchOrder(ourOrderNonce)
if err != nil {
return err
}
orderIsAsk := ourOrder.Type() == order.TypeAsk
// If the order as an ask, then we don't need to do anything,
// as we only register funding shims for incoming channels (so
// buys).
if orderIsAsk {
continue
}
// For each ask order that was matched with this bid, we'll
// re-derive the pending chan ID key used, then attempt to
// unregister it.
for _, matchedOrder := range matchedOrders {
bidNonce := ourOrder.Nonce()
askNonce := matchedOrder.Order.Nonce()
pendingChanID := order.PendingChanKey(
askNonce, bidNonce,
)
cancelShimMsg := &lnrpc.FundingTransitionMsg_ShimCancel{
ShimCancel: &lnrpc.FundingShimCancel{
PendingChanId: pendingChanID[:],
},
}
_, err = baseClient.FundingStateStep(
ctxb, &lnrpc.FundingTransitionMsg{
Trigger: cancelShimMsg,
},
)
if err != nil {
log.Warnf("Unable to unregister funding shim "+
"(pendingChanID=%x) for order=%v",
pendingChanID[:], bidNonce)
}
}
}
return nil
}
// AbandonCanceledChannels removes all channels from lnd's channel database that
// were created for an iteration of a batch that never made it to chain in the
// provided configuration. This should be called whenever a batch is replaced
// with an updated version because some traders were offline or rejected the
// batch. If a non-nil error is returned, something with reading the local order
// or extracting the channel outpoint went wrong and we should fail hard. If the
// channel cannot be abandoned for some reason, the error is just logged but not
// returned.
func AbandonCanceledChannels(matchedOrders map[order.Nonce][]*order.MatchedOrder,
batchTx *wire.MsgTx, wallet lndclient.WalletKitClient,
baseClient BaseClient, fetchOrder order.Fetcher) error {
// Since we support partial matches, a given bid of ours could've been
// matched with multiple asks, so we'll iterate through all those to
// ensure we remove all channels that never made it to chain.
ctxb := context.Background()
txHash := batchTx.TxHash()
for ourOrderNonce, matchedOrders := range matchedOrders {
ourOrder, err := fetchOrder(ourOrderNonce)
if err != nil {
return err
}
// For each ask order that was matched with this bid, we'll
// locate the channel outpoint then abandon it from lnd's
// channel database.
for _, matchedOrder := range matchedOrders {
_, idx, err := order.ChannelOutput(
batchTx, wallet, ourOrder, matchedOrder,
)
if err != nil {
return fmt.Errorf("error locating channel "+
"outpoint: %v", err)
}
channelPoint := &lnrpc.ChannelPoint{
OutputIndex: idx,
FundingTxid: &lnrpc.ChannelPoint_FundingTxidBytes{
FundingTxidBytes: txHash[:],
},
}
_, err = baseClient.AbandonChannel(
ctxb, &lnrpc.AbandonChannelRequest{
ChannelPoint: channelPoint,
PendingFundingShimOnly: true,
},
)
const notFoundErr = "unable to find closed channel"
if err != nil {
// If the channel was never created in the first
// place, it might just not exist. Therefore we
// ignore the "not found" error but fail on any
// other error.
if !strings.Contains(err.Error(), notFoundErr) {
log.Errorf("Unexpected error when "+
"trying to clean up pending "+
"channels: %v", err)
return err
}
log.Debugf("Cleaning up incomplete/replaced "+
"pending channel in lnd was "+
"unsuccessful for order=%v "+
"(channel_point=%v:%d), assuming "+
"timeout when funding: %v",
ourOrderNonce.String(), txHash, idx,
err)
}
}
}
return nil
}