-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathnode_settled_handler.go
161 lines (131 loc) · 3.31 KB
/
node_settled_handler.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
package lnmux
import (
"context"
"fmt"
"time"
"github.com/bottlepay/lnmux/lnd"
"github.com/bottlepay/lnmux/persistence"
"github.com/bottlepay/lnmux/types"
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
"github.com/lightningnetwork/lnd/lntypes"
"go.uber.org/zap"
)
const subscribeEventsRetryDelay = time.Second
type NodeSettledHandlerConfig struct {
Persister *persistence.PostgresPersister
Logger *zap.SugaredLogger
Lnd lnd.LndClient
FinalCallback func(lntypes.Hash, bool)
}
type NodeSettledHandler struct {
persister *persistence.PostgresPersister
logger *zap.SugaredLogger
lnd lnd.LndClient
finalCallback func(lntypes.Hash, bool)
}
func NewNodeSettledHandler(cfg *NodeSettledHandlerConfig) *NodeSettledHandler {
logger := cfg.Logger.With("node", cfg.Lnd.PubKey())
return &NodeSettledHandler{
logger: logger,
persister: cfg.Persister,
lnd: cfg.Lnd,
finalCallback: cfg.FinalCallback,
}
}
func (p *NodeSettledHandler) Run(ctx context.Context) {
p.logger.Infow("Starting node settled handler")
for {
err := p.subscribeEvents(ctx)
switch {
case err == context.DeadlineExceeded:
continue
case err == context.Canceled:
return
case err != nil:
p.logger.Infow("Htlc notifier error", "err", err)
}
select {
case <-time.After(subscribeEventsRetryDelay):
case <-ctx.Done():
return
}
}
}
func (p *NodeSettledHandler) subscribeEvents(ctx context.Context) error {
// First subscribe to the htlc notification stream to prevent missing
// updates.
recv, err := p.lnd.HtlcNotifier(ctx)
if err != nil {
return err
}
// Retrieve all htlcs that are not yet settled.
htlcs, err := p.persister.GetPendingHtlcs(ctx, p.lnd.PubKey())
if err != nil {
return err
}
// Look up each htlc to see if it has been settled in the mean time.
for key := range htlcs {
settled, err := p.lnd.LookupHtlc(ctx, key)
switch {
// No final resolution yet.
case err == lnd.ErrHtlcNotFound:
continue
case err != nil:
return err
}
if err := p.handleFinalHtlc(ctx, key, settled); err != nil {
return err
}
}
// Start processing newly settled htlcs.
for {
event, err := recv()
if err != nil {
return err
}
finalEvent, ok := event.Event.(*routerrpc.HtlcEvent_FinalHtlcEvent)
if !ok {
continue
}
key := types.CircuitKey{
ChanID: event.IncomingChannelId,
HtlcID: event.IncomingHtlcId,
}
err = p.handleFinalHtlc(
ctx, key, finalEvent.FinalHtlcEvent.Settled,
)
if err != nil {
return err
}
}
}
func (p *NodeSettledHandler) handleFinalHtlc(ctx context.Context,
key types.CircuitKey, settled bool) error {
htlcKey := types.HtlcKey{
ChanID: key.ChanID,
HtlcID: key.HtlcID,
Node: p.lnd.PubKey(),
}
invoiceHash, err := p.persister.MarkHtlcFinal(ctx, htlcKey, settled)
switch {
case err == persistence.ErrHtlcAlreadyFinal:
return nil
// If the htlc is not found, the final resolution was for an htlc that
// isn't managed by lnmux.
case err == types.ErrHtlcNotFound:
return nil
case err != nil:
return fmt.Errorf("unable to finalize htlc %v: %w",
key, err)
}
p.logger.Infow("Htlc final resolution received",
"chanID", key.ChanID,
"htlcID", key.HtlcID,
"hash", invoiceHash,
"settled", settled,
)
if invoiceHash != nil {
p.finalCallback(*invoiceHash, settled)
}
return nil
}