@@ -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 {
@@ -162,6 +173,18 @@ func (s *chainNotifierClient) RegisterSpendNtfn(ctx context.Context,
162
173
return nil
163
174
}
164
175
176
+ processReorg := func () {
177
+ if opts .ReOrgChan == nil {
178
+ return
179
+ }
180
+
181
+ select {
182
+ case opts .ReOrgChan <- struct {}{}:
183
+ case <- ctx .Done ():
184
+ return
185
+ }
186
+ }
187
+
165
188
s .wg .Add (1 )
166
189
go func () {
167
190
defer s .wg .Done ()
@@ -172,12 +195,35 @@ func (s *chainNotifierClient) RegisterSpendNtfn(ctx context.Context,
172
195
return
173
196
}
174
197
175
- c , ok := spendEvent .Event .(* chainrpc. SpendEvent_Spend )
176
- if ok {
198
+ switch c := spendEvent .Event .(type ) {
199
+ case * chainrpc. SpendEvent_Spend :
177
200
err := processSpendDetail (c .Spend )
178
201
if err != nil {
179
202
errChan <- err
203
+
204
+ return
180
205
}
206
+
207
+ // If we're running in re-org aware mode, then
208
+ // we don't return here, since we might want to
209
+ // be informed about the new block we got
210
+ // confirmed in after a re-org.
211
+ if opts .ReOrgChan == nil {
212
+ return
213
+ }
214
+
215
+ case * chainrpc.SpendEvent_Reorg :
216
+ processReorg ()
217
+
218
+ // Nil event, should never happen.
219
+ case nil :
220
+ errChan <- fmt .Errorf ("spend event empty" )
221
+ return
222
+
223
+ // Unexpected type.
224
+ default :
225
+ errChan <- fmt .Errorf ("spend event has " +
226
+ "unexpected type" )
181
227
return
182
228
}
183
229
}
0 commit comments