-
Notifications
You must be signed in to change notification settings - Fork 25
/
notifier.go
329 lines (278 loc) · 9.61 KB
/
notifier.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
package lnnode
import (
"context"
"encoding/json"
"io"
"time"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnrpc/backuprpc"
"github.com/lightningnetwork/lnd/lnrpc/breezbackuprpc"
"github.com/lightningnetwork/lnd/lnrpc/chainrpc"
"github.com/lightningnetwork/lnd/lnrpc/invoicesrpc"
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
"github.com/lightningnetwork/lnd/lnrpc/signrpc"
"github.com/lightningnetwork/lnd/lnrpc/submarineswaprpc"
"github.com/lightningnetwork/lnd/lnrpc/walletrpc"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/subscribe"
)
// DaemonReadyEvent is sent when the daemon is ready for RPC requests
type DaemonReadyEvent struct {
IdentityPubkey string
}
// DaemonDownEvent is sent when the daemon stops
type DaemonDownEvent struct{}
// ChannelEvent is sent whenever a channel is created/closed or active/inactive.
type ChannelEvent struct {
*lnrpc.ChannelEventUpdate
}
// PeerEvent is sent whenever a peer is connected/disconnected.
type PeerEvent struct {
*lnrpc.PeerEvent
}
// TransactionEvent is sent when a new transaction is received.
type TransactionEvent struct {
*lnrpc.Transaction
}
// InvoiceEvent is sent when a new invoice is created/settled.
type InvoiceEvent struct {
*lnrpc.Invoice
}
// ChainSyncedEvent is sent when the chain gets into synced state.
type ChainSyncedEvent struct{}
// ResumeEvent is sent when the app resumes.
type ResumeEvent struct{}
// BackupNeededEvent is sent when the node signals backup is needed.
type BackupNeededEvent struct{}
// RoutingNodeChannelOpened is sent when a channel with the routing
// node is opened.
type RoutingNodeChannelOpened struct{}
// SubscribeEvents subscribe to various application events
func (d *Daemon) SubscribeEvents() (*subscribe.Client, error) {
return d.ntfnServer.Subscribe()
}
func (d *Daemon) startSubscriptions() error {
var err error
grpcCon, err := newLightningClient(d.cfg)
if err != nil {
return err
}
d.Lock()
d.lightningClient = lnrpc.NewLightningClient(grpcCon)
d.subswapClient = submarineswaprpc.NewSubmarineSwapperClient(grpcCon)
d.breezBackupClient = breezbackuprpc.NewBreezBackuperClient(grpcCon)
d.routerClient = routerrpc.NewRouterClient(grpcCon)
d.walletKitClient = walletrpc.NewWalletKitClient(grpcCon)
d.chainNotifierClient = chainrpc.NewChainNotifierClient(grpcCon)
d.signerClient = signrpc.NewSignerClient(grpcCon)
d.invoicesClient = invoicesrpc.NewInvoicesClient(grpcCon)
d.Unlock()
info, chainErr := d.lightningClient.GetInfo(context.Background(), &lnrpc.GetInfoRequest{})
if chainErr != nil {
d.log.Warnf("Failed get chain info", chainErr)
return chainErr
}
d.Lock()
d.nodePubkey = info.IdentityPubkey
d.Unlock()
backupEventClient := backuprpc.NewBackupClient(grpcCon)
ctx, cancel := context.WithCancel(context.Background())
d.wg.Add(7)
go d.subscribeChannels(d.lightningClient, ctx)
go d.subscribePeers(d.lightningClient, ctx)
go d.subscribeTransactions(ctx)
go d.subscribeInvoices(ctx)
go d.subscribeChannelAcceptor(ctx, d.lightningClient)
go d.watchBackupEvents(backupEventClient, ctx)
go d.syncToChain(ctx)
// cancel subscriptions on quit
go func() {
<-d.quitChan
cancel()
}()
if err := d.ntfnServer.SendUpdate(DaemonReadyEvent{IdentityPubkey: info.IdentityPubkey}); err != nil {
return err
}
d.log.Infof("Daemon ready! subscriptions started")
return nil
}
func (d *Daemon) subscribeChannelAcceptor(ctx context.Context, client lnrpc.LightningClient) error {
defer d.wg.Done()
channelAcceptorClient, err := client.ChannelAcceptor(ctx)
if err != nil {
d.log.Errorf("Failed to get a channel acceptor %v", err)
return err
}
for {
request, err := channelAcceptorClient.Recv()
if err == io.EOF || ctx.Err() == context.Canceled {
d.log.Errorf("channelAcceptorClient cancelled, shutting down")
return err
}
if err != nil {
d.log.Errorf("channelAcceptorClient failed to get notification %v", err)
// in case of unexpected error, we will wait a bit so we won't get
// into infinite loop.
time.Sleep(2 * time.Second)
continue
}
private := request.ChannelFlags&uint32(lnwire.FFAnnounceChannel) == 0
d.log.Infof("channel creation requested from node: %v private: %v", request.NodePubkey, private)
resp := &lnrpc.ChannelAcceptResponse{
PendingChanId: request.PendingChanId,
Accept: private,
}
if request.WantsZeroConf {
d.log.Infof("channel requested wants zero conf")
resp.MinAcceptDepth = 0
resp.ZeroConf = true
}
serialized, err := json.Marshal(resp)
if err == nil {
d.log.Infof("sending response to channel acceptor: %v", serialized)
}
err = channelAcceptorClient.Send(resp)
if err != nil {
d.log.Errorf("Error in channelAcceptorClient.Send(%v, %v): %v", request.PendingChanId, private, err)
return err
}
}
}
func (d *Daemon) subscribePeers(client lnrpc.LightningClient, ctx context.Context) error {
defer d.wg.Done()
subscription, err := client.SubscribePeerEvents(ctx, &lnrpc.PeerEventSubscription{})
if err != nil {
d.log.Errorf("Failed to subscribe peers %v", err)
return err
}
d.log.Infof("Peers subscription created")
for {
notification, err := subscription.Recv()
if err == io.EOF || ctx.Err() == context.Canceled {
d.log.Errorf("subscribePeers cancelled, shutting down")
return err
}
d.log.Infof("peer event type %v received for peer = %v", notification.Type, notification.PubKey)
if err != nil {
d.log.Errorf("subscribe peers Failed to get notification %v", err)
// in case of unexpected error, we will wait a bit so we won't get
// into infinite loop.
time.Sleep(2 * time.Second)
continue
}
d.ntfnServer.SendUpdate(PeerEvent{notification})
}
}
func (d *Daemon) subscribeChannels(client lnrpc.LightningClient, ctx context.Context) error {
defer d.wg.Done()
subscription, err := client.SubscribeChannelEvents(ctx, &lnrpc.ChannelEventSubscription{})
if err != nil {
d.log.Errorf("Failed to subscribe channels %v", err)
return err
}
d.log.Infof("Channels subscription created")
for {
notification, err := subscription.Recv()
if err == io.EOF || ctx.Err() == context.Canceled {
d.log.Errorf("subscribeChannels cancelled, shutting down")
return err
}
d.log.Infof("Channel event type %v received for channel = %v", notification.Type, notification.Channel)
if err != nil {
d.log.Errorf("subscribe channels Failed to get notification %v", err)
// in case of unexpected error, we will wait a bit so we won't get
// into infinite loop.
time.Sleep(2 * time.Second)
continue
}
d.ntfnServer.SendUpdate(ChannelEvent{notification})
}
}
func (d *Daemon) subscribeTransactions(ctx context.Context) error {
defer d.wg.Done()
stream, err := d.lightningClient.SubscribeTransactions(ctx, &lnrpc.GetTransactionsRequest{})
if err != nil {
d.log.Criticalf("Failed to call SubscribeTransactions %v, %v", stream, err)
}
d.log.Infof("Wallet transactions subscription created")
for {
notification, err := stream.Recv()
if err == io.EOF || ctx.Err() == context.Canceled {
d.log.Errorf("subscribeTransactions cancelled, shutting down")
return err
}
d.log.Infof("subscribeTransactions received new transaction")
if err != nil {
d.log.Errorf("Failed to receive a transaction : %v", err)
// in case of unexpected error, we will wait a bit so we won't get
// into infinite loop.
time.Sleep(2 * time.Second)
}
d.log.Infof("watchOnChainState sending account change notification")
d.ntfnServer.SendUpdate(TransactionEvent{notification})
}
}
func (d *Daemon) subscribeInvoices(ctx context.Context) error {
defer d.wg.Done()
stream, err := d.lightningClient.SubscribeInvoices(ctx, &lnrpc.InvoiceSubscription{})
if err != nil {
d.log.Criticalf("Failed to call SubscribeInvoices %v, %v", stream, err)
return err
}
d.log.Infof("Invoices subscription created")
for {
invoice, err := stream.Recv()
if err == io.EOF || ctx.Err() == context.Canceled {
d.log.Errorf("subscribeInvoices cancelled, shutting down")
return err
}
if err != nil {
d.log.Criticalf("Failed to receive an invoice : %v", err)
return err
}
d.log.Infof("watchPayments - Invoice received by subscription")
d.ntfnServer.SendUpdate(InvoiceEvent{invoice})
}
}
func (d *Daemon) watchBackupEvents(client backuprpc.BackupClient, ctx context.Context) error {
defer d.wg.Done()
stream, err := client.SubscribeBackupEvents(ctx, &backuprpc.BackupEventSubscription{})
if err != nil {
d.log.Criticalf("Failed to call SubscribeBackupEvents %v, %v", stream, err)
}
d.log.Infof("Backup events subscription created")
for {
_, err := stream.Recv()
if err == io.EOF || ctx.Err() == context.Canceled {
d.log.Errorf("watchBackupEvents cancelled, shutting down")
return err
}
d.log.Infof("watchBackupEvents received new event")
if err != nil {
d.log.Errorf("watchBackupEvents failed to receive a new event: %v, %v", stream, err)
return err
}
d.ntfnServer.SendUpdate(BackupNeededEvent{})
}
}
func (d *Daemon) syncToChain(ctx context.Context) error {
defer d.wg.Done()
for {
chainInfo, chainErr := d.lightningClient.GetInfo(ctx, &lnrpc.GetInfoRequest{})
if chainErr != nil {
d.log.Warnf("Failed get chain info", chainErr)
return chainErr
}
d.log.Infof("Sync to chain interval Synced=%v BlockHeight=%v", chainInfo.SyncedToChain, chainInfo.BlockHeight)
if err := d.breezDB.SetLastSyncedHeaderTimestamp(chainInfo.BestHeaderTimestamp); err != nil {
d.log.Errorf("Failed to set last header timestamp")
}
if chainInfo.SyncedToChain {
d.log.Infof("Synchronized to chain finshed BlockHeight=%v", chainInfo.BlockHeight)
break
}
time.Sleep(time.Second * 3)
}
d.ntfnServer.SendUpdate(ChainSyncedEvent{})
return nil
}