@@ -71,8 +71,9 @@ type ChainNotifierClient interface {
71
71
chan error , error )
72
72
73
73
RegisterSpendNtfn (ctx context.Context ,
74
- outpoint * wire.OutPoint , pkScript []byte , heightHint int32 ) (
75
- chan * chainntnfs.SpendDetail , chan error , error )
74
+ outpoint * wire.OutPoint , pkScript []byte , heightHint int32 ,
75
+ optFuncs ... NotifierOption ) (chan * chainntnfs.SpendDetail ,
76
+ chan error , error )
76
77
}
77
78
78
79
type chainNotifierClient struct {
@@ -111,8 +112,18 @@ func (s *chainNotifierClient) RawClientWithMacAuth(
111
112
}
112
113
113
114
func (s * chainNotifierClient ) RegisterSpendNtfn (ctx context.Context ,
114
- outpoint * wire.OutPoint , pkScript []byte , heightHint int32 ) (
115
- chan * chainntnfs.SpendDetail , chan error , error ) {
115
+ outpoint * wire.OutPoint , pkScript []byte , heightHint int32 ,
116
+ optFuncs ... NotifierOption ) (chan * chainntnfs.SpendDetail , chan error ,
117
+ error ) {
118
+
119
+ opts := DefaultNotifierOptions ()
120
+ for _ , optFunc := range optFuncs {
121
+ optFunc (opts )
122
+ }
123
+ if opts .IncludeBlock {
124
+ return nil , nil , fmt .Errorf ("option IncludeBlock is not " +
125
+ "supported by RegisterSpendNtfn" )
126
+ }
116
127
117
128
var rpcOutpoint * chainrpc.Outpoint
118
129
if outpoint != nil {
@@ -148,7 +159,7 @@ func (s *chainNotifierClient) RegisterSpendNtfn(ctx context.Context,
148
159
if err != nil {
149
160
return err
150
161
}
151
- spendChan <- & chainntnfs.SpendDetail {
162
+ spend := & chainntnfs.SpendDetail {
152
163
SpentOutPoint : & wire.OutPoint {
153
164
Hash : * outpointHash ,
154
165
Index : d .SpendingOutpoint .Index ,
@@ -159,7 +170,24 @@ func (s *chainNotifierClient) RegisterSpendNtfn(ctx context.Context,
159
170
SpendingHeight : int32 (d .SpendingHeight ),
160
171
}
161
172
162
- return nil
173
+ select {
174
+ case spendChan <- spend :
175
+ return nil
176
+ case <- ctx .Done ():
177
+ return ctx .Err ()
178
+ }
179
+ }
180
+
181
+ processReorg := func () {
182
+ if opts .ReOrgChan == nil {
183
+ return
184
+ }
185
+
186
+ select {
187
+ case opts .ReOrgChan <- struct {}{}:
188
+ case <- ctx .Done ():
189
+ return
190
+ }
163
191
}
164
192
165
193
s .wg .Add (1 )
@@ -172,12 +200,35 @@ func (s *chainNotifierClient) RegisterSpendNtfn(ctx context.Context,
172
200
return
173
201
}
174
202
175
- c , ok := spendEvent .Event .(* chainrpc. SpendEvent_Spend )
176
- if ok {
203
+ switch c := spendEvent .Event .(type ) {
204
+ case * chainrpc. SpendEvent_Spend :
177
205
err := processSpendDetail (c .Spend )
178
206
if err != nil {
179
207
errChan <- err
208
+
209
+ return
180
210
}
211
+
212
+ // If we're running in re-org aware mode, then
213
+ // we don't return here, since we might want to
214
+ // be informed about the new block we got
215
+ // confirmed in after a re-org.
216
+ if opts .ReOrgChan == nil {
217
+ return
218
+ }
219
+
220
+ case * chainrpc.SpendEvent_Reorg :
221
+ processReorg ()
222
+
223
+ // Nil event, should never happen.
224
+ case nil :
225
+ errChan <- fmt .Errorf ("spend event empty" )
226
+ return
227
+
228
+ // Unexpected type.
229
+ default :
230
+ errChan <- fmt .Errorf ("spend event has " +
231
+ "unexpected type" )
181
232
return
182
233
}
183
234
}
@@ -256,14 +307,20 @@ func (s *chainNotifierClient) RegisterConfirmationsNtfn(ctx context.Context,
256
307
return
257
308
}
258
309
259
- confChan <- & chainntnfs.TxConfirmation {
310
+ conf := & chainntnfs.TxConfirmation {
260
311
BlockHeight : c .Conf .BlockHeight ,
261
312
BlockHash : blockHash ,
262
313
Tx : tx ,
263
314
TxIndex : c .Conf .TxIndex ,
264
315
Block : block ,
265
316
}
266
317
318
+ select {
319
+ case confChan <- conf :
320
+ case <- ctx .Done ():
321
+ return
322
+ }
323
+
267
324
// If we're running in re-org aware mode, then
268
325
// we don't return here, since we might want to
269
326
// be informed about the new block we got
0 commit comments