@@ -71,8 +71,9 @@ type ChainNotifierClient interface {
7171 chan error , error )
7272
7373 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 )
7677}
7778
7879type chainNotifierClient struct {
@@ -111,8 +112,18 @@ func (s *chainNotifierClient) RawClientWithMacAuth(
111112}
112113
113114func (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+ }
116127
117128 var rpcOutpoint * chainrpc.Outpoint
118129 if outpoint != nil {
@@ -148,7 +159,7 @@ func (s *chainNotifierClient) RegisterSpendNtfn(ctx context.Context,
148159 if err != nil {
149160 return err
150161 }
151- spendChan <- & chainntnfs.SpendDetail {
162+ spend := & chainntnfs.SpendDetail {
152163 SpentOutPoint : & wire.OutPoint {
153164 Hash : * outpointHash ,
154165 Index : d .SpendingOutpoint .Index ,
@@ -159,7 +170,24 @@ func (s *chainNotifierClient) RegisterSpendNtfn(ctx context.Context,
159170 SpendingHeight : int32 (d .SpendingHeight ),
160171 }
161172
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+ }
163191 }
164192
165193 s .wg .Add (1 )
@@ -172,12 +200,35 @@ func (s *chainNotifierClient) RegisterSpendNtfn(ctx context.Context,
172200 return
173201 }
174202
175- c , ok := spendEvent .Event .(* chainrpc. SpendEvent_Spend )
176- if ok {
203+ switch c := spendEvent .Event .(type ) {
204+ case * chainrpc. SpendEvent_Spend :
177205 err := processSpendDetail (c .Spend )
178206 if err != nil {
179207 errChan <- err
208+
209+ return
180210 }
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" )
181232 return
182233 }
183234 }
@@ -256,14 +307,20 @@ func (s *chainNotifierClient) RegisterConfirmationsNtfn(ctx context.Context,
256307 return
257308 }
258309
259- confChan <- & chainntnfs.TxConfirmation {
310+ conf := & chainntnfs.TxConfirmation {
260311 BlockHeight : c .Conf .BlockHeight ,
261312 BlockHash : blockHash ,
262313 Tx : tx ,
263314 TxIndex : c .Conf .TxIndex ,
264315 Block : block ,
265316 }
266317
318+ select {
319+ case confChan <- conf :
320+ case <- ctx .Done ():
321+ return
322+ }
323+
267324 // If we're running in re-org aware mode, then
268325 // we don't return here, since we might want to
269326 // be informed about the new block we got
0 commit comments