@@ -12,8 +12,10 @@ import (
12
12
)
13
13
14
14
type mockChainNotifier struct {
15
- lnd * LndMockServices
16
- wg sync.WaitGroup
15
+ sync.Mutex
16
+ lnd * LndMockServices
17
+ confRegistrations []* ConfRegistration
18
+ wg sync.WaitGroup
17
19
}
18
20
19
21
// SpendRegistration contains registration details.
@@ -29,6 +31,7 @@ type ConfRegistration struct {
29
31
PkScript []byte
30
32
HeightHint int32
31
33
NumConfs int32
34
+ ConfChan chan * chainntnfs.TxConfirmation
32
35
}
33
36
34
37
func (c * mockChainNotifier ) RegisterSpendNtfn (ctx context.Context ,
@@ -103,7 +106,18 @@ func (c *mockChainNotifier) RegisterConfirmationsNtfn(ctx context.Context,
103
106
txid * chainhash.Hash , pkScript []byte , numConfs , heightHint int32 ) (
104
107
chan * chainntnfs.TxConfirmation , chan error , error ) {
105
108
106
- confChan := make (chan * chainntnfs.TxConfirmation , 1 )
109
+ reg := & ConfRegistration {
110
+ PkScript : pkScript ,
111
+ TxID : txid ,
112
+ HeightHint : heightHint ,
113
+ NumConfs : numConfs ,
114
+ ConfChan : make (chan * chainntnfs.TxConfirmation , 1 ),
115
+ }
116
+
117
+ c .Lock ()
118
+ c .confRegistrations = append (c .confRegistrations , reg )
119
+ c .Unlock ()
120
+
107
121
errChan := make (chan error , 1 )
108
122
109
123
c .wg .Add (1 )
@@ -112,26 +126,35 @@ func (c *mockChainNotifier) RegisterConfirmationsNtfn(ctx context.Context,
112
126
113
127
select {
114
128
case m := <- c .lnd .ConfChannel :
115
- if bytes .Equal (m .Tx .TxOut [0 ].PkScript , pkScript ) {
116
- select {
117
- case confChan <- m :
118
- case <- ctx .Done ():
129
+ c .Lock ()
130
+ for i := 0 ; i < len (c .confRegistrations ); i ++ {
131
+ r := c .confRegistrations [i ]
132
+
133
+ // Whichever conf notifier catches the confirmation
134
+ // will forward it to all matching subscibers.
135
+ if bytes .Equal (m .Tx .TxOut [0 ].PkScript , r .PkScript ) {
136
+ // Unregister the "notifier".
137
+ c .confRegistrations = append (
138
+ c .confRegistrations [:i ], c .confRegistrations [i + 1 :]... ,
139
+ )
140
+ i --
141
+
142
+ select {
143
+ case r .ConfChan <- m :
144
+ case <- ctx .Done ():
145
+ }
119
146
}
120
147
}
148
+ c .Unlock ()
121
149
case <- ctx .Done ():
122
150
}
123
151
}()
124
152
125
153
select {
126
- case c .lnd .RegisterConfChannel <- & ConfRegistration {
127
- PkScript : pkScript ,
128
- TxID : txid ,
129
- HeightHint : heightHint ,
130
- NumConfs : numConfs ,
131
- }:
154
+ case c .lnd .RegisterConfChannel <- reg :
132
155
case <- time .After (Timeout ):
133
156
return nil , nil , ErrTimeout
134
157
}
135
158
136
- return confChan , errChan , nil
159
+ return reg . ConfChan , errChan , nil
137
160
}
0 commit comments