Skip to content

Commit 692014f

Browse files
committed
RegisterSpendNtfn: support reorg channel
The corresponding API of LND already supports notifying the caller about a reorg. I reused NotifierOption's used for RegisterConfirmationsNtfn already to pass a channel notified upon a reorg affecting a reported confirmation.
1 parent a378c74 commit 692014f

File tree

1 file changed

+52
-6
lines changed

1 file changed

+52
-6
lines changed

chainnotifier_client.go

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

7879
type chainNotifierClient struct {
@@ -111,8 +112,18 @@ func (s *chainNotifierClient) RawClientWithMacAuth(
111112
}
112113

113114
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+
}
116127

117128
var rpcOutpoint *chainrpc.Outpoint
118129
if outpoint != nil {
@@ -162,6 +173,18 @@ func (s *chainNotifierClient) RegisterSpendNtfn(ctx context.Context,
162173
return nil
163174
}
164175

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+
165188
s.wg.Add(1)
166189
go func() {
167190
defer s.wg.Done()
@@ -172,12 +195,35 @@ func (s *chainNotifierClient) RegisterSpendNtfn(ctx context.Context,
172195
return
173196
}
174197

175-
c, ok := spendEvent.Event.(*chainrpc.SpendEvent_Spend)
176-
if ok {
198+
switch c := spendEvent.Event.(type) {
199+
case *chainrpc.SpendEvent_Spend:
177200
err := processSpendDetail(c.Spend)
178201
if err != nil {
179202
errChan <- err
203+
204+
return
180205
}
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")
181227
return
182228
}
183229
}

0 commit comments

Comments
 (0)