Skip to content

Commit 07cbafa

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 07cbafa

File tree

1 file changed

+48
-6
lines changed

1 file changed

+48
-6
lines changed

chainnotifier_client.go

Lines changed: 48 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,14 @@ 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+
opts.ReOrgChan <- struct{}{}
182+
}
183+
165184
s.wg.Add(1)
166185
go func() {
167186
defer s.wg.Done()
@@ -172,12 +191,35 @@ func (s *chainNotifierClient) RegisterSpendNtfn(ctx context.Context,
172191
return
173192
}
174193

175-
c, ok := spendEvent.Event.(*chainrpc.SpendEvent_Spend)
176-
if ok {
194+
switch c := spendEvent.Event.(type) {
195+
case *chainrpc.SpendEvent_Spend:
177196
err := processSpendDetail(c.Spend)
178197
if err != nil {
179198
errChan <- err
199+
200+
return
201+
}
202+
203+
// If we're running in re-org aware mode, then
204+
// we don't return here, since we might want to
205+
// be informed about the new block we got
206+
// confirmed in after a re-org.
207+
if opts.ReOrgChan == nil {
208+
return
180209
}
210+
211+
case *chainrpc.SpendEvent_Reorg:
212+
processReorg()
213+
214+
// Nil event, should never happen.
215+
case nil:
216+
errChan <- fmt.Errorf("spend event empty")
217+
return
218+
219+
// Unexpected type.
220+
default:
221+
errChan <- fmt.Errorf("spend event has " +
222+
"unexpected type")
181223
return
182224
}
183225
}

0 commit comments

Comments
 (0)