test: followup update chainnotifier mock to fwd notifications for matching txs

This is the remainder of the previous commit which was left out from the
original PR.
pull/175/head
Andras Banki-Horvath 4 years ago
parent 95273c1cf8
commit 235e1bb9b8

@ -12,8 +12,10 @@ import (
) )
type mockChainNotifier struct { type mockChainNotifier struct {
lnd *LndMockServices sync.Mutex
wg sync.WaitGroup lnd *LndMockServices
confRegistrations []*ConfRegistration
wg sync.WaitGroup
} }
// SpendRegistration contains registration details. // SpendRegistration contains registration details.
@ -29,6 +31,7 @@ type ConfRegistration struct {
PkScript []byte PkScript []byte
HeightHint int32 HeightHint int32
NumConfs int32 NumConfs int32
ConfChan chan *chainntnfs.TxConfirmation
} }
func (c *mockChainNotifier) RegisterSpendNtfn(ctx context.Context, func (c *mockChainNotifier) RegisterSpendNtfn(ctx context.Context,
@ -103,7 +106,18 @@ func (c *mockChainNotifier) RegisterConfirmationsNtfn(ctx context.Context,
txid *chainhash.Hash, pkScript []byte, numConfs, heightHint int32) ( txid *chainhash.Hash, pkScript []byte, numConfs, heightHint int32) (
chan *chainntnfs.TxConfirmation, chan error, error) { chan *chainntnfs.TxConfirmation, chan error, error) {
confChan := make(chan *chainntnfs.TxConfirmation, 1) reg := &ConfRegistration{
PkScript: pkScript,
TxID: txid,
HeightHint: heightHint,
NumConfs: numConfs,
ConfChan: make(chan *chainntnfs.TxConfirmation, 1),
}
c.Lock()
c.confRegistrations = append(c.confRegistrations, reg)
c.Unlock()
errChan := make(chan error, 1) errChan := make(chan error, 1)
c.wg.Add(1) c.wg.Add(1)
@ -112,26 +126,35 @@ func (c *mockChainNotifier) RegisterConfirmationsNtfn(ctx context.Context,
select { select {
case m := <-c.lnd.ConfChannel: case m := <-c.lnd.ConfChannel:
if bytes.Equal(m.Tx.TxOut[0].PkScript, pkScript) { c.Lock()
select { for i := 0; i < len(c.confRegistrations); i++ {
case confChan <- m: r := c.confRegistrations[i]
case <-ctx.Done():
// Whichever conf notifier catches the confirmation
// will forward it to all matching subscibers.
if bytes.Equal(m.Tx.TxOut[0].PkScript, r.PkScript) {
// Unregister the "notifier".
c.confRegistrations = append(
c.confRegistrations[:i], c.confRegistrations[i+1:]...,
)
i--
select {
case r.ConfChan <- m:
case <-ctx.Done():
}
} }
} }
c.Unlock()
case <-ctx.Done(): case <-ctx.Done():
} }
}() }()
select { select {
case c.lnd.RegisterConfChannel <- &ConfRegistration{ case c.lnd.RegisterConfChannel <- reg:
PkScript: pkScript,
TxID: txid,
HeightHint: heightHint,
NumConfs: numConfs,
}:
case <-time.After(Timeout): case <-time.After(Timeout):
return nil, nil, ErrTimeout return nil, nil, ErrTimeout
} }
return confChan, errChan, nil return reg.ConfChan, errChan, nil
} }

Loading…
Cancel
Save