-
Notifications
You must be signed in to change notification settings - Fork 43
/
chainnotifier_client.go
323 lines (277 loc) · 7.9 KB
/
chainnotifier_client.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
package lndclient
import (
"context"
"fmt"
"sync"
"time"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/lnrpc/chainrpc"
"google.golang.org/grpc"
)
// notifierOptions is a set of functional options that allow callers to further
// modify the type of chain even notifications they receive.
type notifierOptions struct {
// includeBlock if true, then the dispatched confirmation notification
// will include the block that mined the transaction.
includeBlock bool
// reOrgChan if set, will be sent on if the transaction is re-organized
// out of the chain. This channel being set will also imply that we
// don't cancel the notification listener after having received one
// confirmation event. That means the caller manually needs to cancel
// the passed in context to cancel being notified once the required
// number of confirmations have been reached.
reOrgChan chan struct{}
}
// defaultNotifierOptions returns the set of default options for the notifier.
func defaultNotifierOptions() *notifierOptions {
return ¬ifierOptions{}
}
// NotifierOption is a functional option that allows a caller to modify the
// events received from the notifier.
type NotifierOption func(*notifierOptions)
// WithIncludeBlock is an optional argument that allows the caller to specify
// that the block that mined a transaction should be included in the response.
func WithIncludeBlock() NotifierOption {
return func(o *notifierOptions) {
o.includeBlock = true
}
}
// WithReOrgChan configures a channel that will be sent on if the transaction is
// re-organized out of the chain. This channel being set will also imply that we
// don't cancel the notification listener after having received one confirmation
// event. That means the caller manually needs to cancel the passed in context
// to cancel being notified once the required number of confirmations have been
// reached.
func WithReOrgChan(reOrgChan chan struct{}) NotifierOption {
return func(o *notifierOptions) {
o.reOrgChan = reOrgChan
}
}
// ChainNotifierClient exposes base lightning functionality.
type ChainNotifierClient interface {
RegisterBlockEpochNtfn(ctx context.Context) (
chan int32, chan error, error)
RegisterConfirmationsNtfn(ctx context.Context, txid *chainhash.Hash,
pkScript []byte, numConfs, heightHint int32,
opts ...NotifierOption) (chan *chainntnfs.TxConfirmation,
chan error, error)
RegisterSpendNtfn(ctx context.Context,
outpoint *wire.OutPoint, pkScript []byte, heightHint int32) (
chan *chainntnfs.SpendDetail, chan error, error)
}
type chainNotifierClient struct {
client chainrpc.ChainNotifierClient
chainMac serializedMacaroon
timeout time.Duration
wg sync.WaitGroup
}
func newChainNotifierClient(conn grpc.ClientConnInterface,
chainMac serializedMacaroon, timeout time.Duration) *chainNotifierClient {
return &chainNotifierClient{
client: chainrpc.NewChainNotifierClient(conn),
chainMac: chainMac,
timeout: timeout,
}
}
func (s *chainNotifierClient) WaitForFinished() {
s.wg.Wait()
}
func (s *chainNotifierClient) RegisterSpendNtfn(ctx context.Context,
outpoint *wire.OutPoint, pkScript []byte, heightHint int32) (
chan *chainntnfs.SpendDetail, chan error, error) {
var rpcOutpoint *chainrpc.Outpoint
if outpoint != nil {
rpcOutpoint = &chainrpc.Outpoint{
Hash: outpoint.Hash[:],
Index: outpoint.Index,
}
}
macaroonAuth := s.chainMac.WithMacaroonAuth(ctx)
resp, err := s.client.RegisterSpendNtfn(macaroonAuth, &chainrpc.SpendRequest{
HeightHint: uint32(heightHint),
Outpoint: rpcOutpoint,
Script: pkScript,
})
if err != nil {
return nil, nil, err
}
spendChan := make(chan *chainntnfs.SpendDetail, 1)
errChan := make(chan error, 1)
processSpendDetail := func(d *chainrpc.SpendDetails) error {
outpointHash, err := chainhash.NewHash(d.SpendingOutpoint.Hash)
if err != nil {
return err
}
txHash, err := chainhash.NewHash(d.SpendingTxHash)
if err != nil {
return err
}
tx, err := decodeTx(d.RawSpendingTx)
if err != nil {
return err
}
spendChan <- &chainntnfs.SpendDetail{
SpentOutPoint: &wire.OutPoint{
Hash: *outpointHash,
Index: d.SpendingOutpoint.Index,
},
SpenderTxHash: txHash,
SpenderInputIndex: d.SpendingInputIndex,
SpendingTx: tx,
SpendingHeight: int32(d.SpendingHeight),
}
return nil
}
s.wg.Add(1)
go func() {
defer s.wg.Done()
for {
spendEvent, err := resp.Recv()
if err != nil {
errChan <- err
return
}
c, ok := spendEvent.Event.(*chainrpc.SpendEvent_Spend)
if ok {
err := processSpendDetail(c.Spend)
if err != nil {
errChan <- err
}
return
}
}
}()
return spendChan, errChan, nil
}
func (s *chainNotifierClient) RegisterConfirmationsNtfn(ctx context.Context,
txid *chainhash.Hash, pkScript []byte, numConfs, heightHint int32,
optFuncs ...NotifierOption) (chan *chainntnfs.TxConfirmation,
chan error, error) {
opts := defaultNotifierOptions()
for _, optFunc := range optFuncs {
optFunc(opts)
}
var txidSlice []byte
if txid != nil {
txidSlice = txid[:]
}
confStream, err := s.client.RegisterConfirmationsNtfn(
s.chainMac.WithMacaroonAuth(ctx), &chainrpc.ConfRequest{
Script: pkScript,
NumConfs: uint32(numConfs),
HeightHint: uint32(heightHint),
Txid: txidSlice,
IncludeBlock: opts.includeBlock,
},
)
if err != nil {
return nil, nil, err
}
confChan := make(chan *chainntnfs.TxConfirmation, 1)
errChan := make(chan error, 1)
s.wg.Add(1)
go func() {
defer s.wg.Done()
for {
var confEvent *chainrpc.ConfEvent
confEvent, err := confStream.Recv()
if err != nil {
errChan <- err
return
}
switch c := confEvent.Event.(type) {
// Script confirmed.
case *chainrpc.ConfEvent_Conf:
tx, err := decodeTx(c.Conf.RawTx)
if err != nil {
errChan <- err
return
}
var block *wire.MsgBlock
if opts.includeBlock {
block, err = decodeBlock(
c.Conf.RawBlock,
)
if err != nil {
errChan <- err
return
}
}
blockHash, err := chainhash.NewHash(
c.Conf.BlockHash,
)
if err != nil {
errChan <- err
return
}
confChan <- &chainntnfs.TxConfirmation{
BlockHeight: c.Conf.BlockHeight,
BlockHash: blockHash,
Tx: tx,
TxIndex: c.Conf.TxIndex,
Block: block,
}
// If we're running in re-org aware mode, then
// we don't return here, since we might want to
// be informed about the new block we got
// confirmed in after a re-org.
if opts.reOrgChan == nil {
return
}
// On a re-org, we just need to signal, we don't have
// any additional information. But we only signal if the
// caller requested to be notified about re-orgs.
case *chainrpc.ConfEvent_Reorg:
if opts.reOrgChan != nil {
select {
case opts.reOrgChan <- struct{}{}:
case <-ctx.Done():
return
}
}
continue
// Nil event, should never happen.
case nil:
errChan <- fmt.Errorf("conf event empty")
return
// Unexpected type.
default:
errChan <- fmt.Errorf("conf event has " +
"unexpected type")
return
}
}
}()
return confChan, errChan, nil
}
func (s *chainNotifierClient) RegisterBlockEpochNtfn(ctx context.Context) (
chan int32, chan error, error) {
blockEpochClient, err := s.client.RegisterBlockEpochNtfn(
s.chainMac.WithMacaroonAuth(ctx), &chainrpc.BlockEpoch{},
)
if err != nil {
return nil, nil, err
}
blockErrorChan := make(chan error, 1)
blockEpochChan := make(chan int32)
// Start block epoch goroutine.
s.wg.Add(1)
go func() {
defer s.wg.Done()
for {
epoch, err := blockEpochClient.Recv()
if err != nil {
blockErrorChan <- err
return
}
select {
case blockEpochChan <- int32(epoch.Height):
case <-ctx.Done():
return
}
}
}()
return blockEpochChan, blockErrorChan, nil
}