2019-03-06 20:13:50 +00:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
2020-04-06 08:58:50 +00:00
|
|
|
"bytes"
|
2019-03-06 20:13:50 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
2023-01-09 15:36:52 +00:00
|
|
|
"github.com/lightninglabs/lndclient"
|
2019-03-06 20:13:50 +00:00
|
|
|
"github.com/lightningnetwork/lnd/chainntnfs"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
type mockChainNotifier struct {
|
2020-04-07 08:57:21 +00:00
|
|
|
sync.Mutex
|
|
|
|
lnd *LndMockServices
|
|
|
|
confRegistrations []*ConfRegistration
|
|
|
|
wg sync.WaitGroup
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SpendRegistration contains registration details.
|
|
|
|
type SpendRegistration struct {
|
|
|
|
Outpoint *wire.OutPoint
|
|
|
|
PkScript []byte
|
|
|
|
HeightHint int32
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConfRegistration contains registration details.
|
|
|
|
type ConfRegistration struct {
|
|
|
|
TxID *chainhash.Hash
|
|
|
|
PkScript []byte
|
|
|
|
HeightHint int32
|
|
|
|
NumConfs int32
|
2020-04-07 08:57:21 +00:00
|
|
|
ConfChan chan *chainntnfs.TxConfirmation
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *mockChainNotifier) RegisterSpendNtfn(ctx context.Context,
|
|
|
|
outpoint *wire.OutPoint, pkScript []byte, heightHint int32) (
|
|
|
|
chan *chainntnfs.SpendDetail, chan error, error) {
|
|
|
|
|
|
|
|
c.lnd.RegisterSpendChannel <- &SpendRegistration{
|
|
|
|
HeightHint: heightHint,
|
|
|
|
Outpoint: outpoint,
|
|
|
|
PkScript: pkScript,
|
|
|
|
}
|
|
|
|
|
|
|
|
spendChan := make(chan *chainntnfs.SpendDetail, 1)
|
|
|
|
errChan := make(chan error, 1)
|
|
|
|
|
|
|
|
c.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer c.wg.Done()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case m := <-c.lnd.SpendChannel:
|
|
|
|
select {
|
|
|
|
case spendChan <- m:
|
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return spendChan, errChan, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *mockChainNotifier) WaitForFinished() {
|
|
|
|
c.wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *mockChainNotifier) RegisterBlockEpochNtfn(ctx context.Context) (
|
|
|
|
chan int32, chan error, error) {
|
|
|
|
|
|
|
|
blockErrorChan := make(chan error, 1)
|
2024-01-15 16:07:25 +00:00
|
|
|
blockEpochChan := make(chan int32, 1)
|
|
|
|
|
|
|
|
c.lnd.lock.Lock()
|
|
|
|
c.lnd.blockHeightListeners = append(
|
|
|
|
c.lnd.blockHeightListeners, blockEpochChan,
|
|
|
|
)
|
|
|
|
c.lnd.lock.Unlock()
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
c.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer c.wg.Done()
|
2024-01-15 16:07:25 +00:00
|
|
|
defer func() {
|
|
|
|
c.lnd.lock.Lock()
|
|
|
|
defer c.lnd.lock.Unlock()
|
|
|
|
for i := 0; i < len(c.lnd.blockHeightListeners); i++ {
|
|
|
|
if c.lnd.blockHeightListeners[i] == blockEpochChan {
|
|
|
|
c.lnd.blockHeightListeners = append(
|
|
|
|
c.lnd.blockHeightListeners[:i],
|
|
|
|
c.lnd.blockHeightListeners[i+1:]...,
|
|
|
|
)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
// Send initial block height
|
2024-01-15 16:07:25 +00:00
|
|
|
c.lnd.lock.Lock()
|
2019-03-06 20:13:50 +00:00
|
|
|
select {
|
|
|
|
case blockEpochChan <- c.lnd.Height:
|
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
2024-01-15 16:07:25 +00:00
|
|
|
c.lnd.lock.Unlock()
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2024-01-15 16:07:25 +00:00
|
|
|
<-ctx.Done()
|
2019-03-06 20:13:50 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
return blockEpochChan, blockErrorChan, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *mockChainNotifier) RegisterConfirmationsNtfn(ctx context.Context,
|
2023-01-09 15:36:52 +00:00
|
|
|
txid *chainhash.Hash, pkScript []byte, numConfs, heightHint int32,
|
|
|
|
opts ...lndclient.NotifierOption) (chan *chainntnfs.TxConfirmation,
|
|
|
|
chan error, error) {
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2020-04-07 08:57:21 +00:00
|
|
|
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()
|
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
errChan := make(chan error, 1)
|
|
|
|
|
|
|
|
c.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer c.wg.Done()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case m := <-c.lnd.ConfChannel:
|
2020-04-07 08:57:21 +00:00
|
|
|
c.Lock()
|
|
|
|
for i := 0; i < len(c.confRegistrations); i++ {
|
|
|
|
r := c.confRegistrations[i]
|
|
|
|
|
|
|
|
// Whichever conf notifier catches the confirmation
|
2024-01-12 12:56:44 +00:00
|
|
|
// will forward it to all matching subscribers.
|
2020-04-07 08:57:21 +00:00
|
|
|
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():
|
|
|
|
}
|
2020-04-06 08:58:50 +00:00
|
|
|
}
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
2020-04-07 08:57:21 +00:00
|
|
|
c.Unlock()
|
2019-03-06 20:13:50 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
2020-04-07 08:57:21 +00:00
|
|
|
case c.lnd.RegisterConfChannel <- reg:
|
2019-03-06 20:13:50 +00:00
|
|
|
case <-time.After(Timeout):
|
|
|
|
return nil, nil, ErrTimeout
|
|
|
|
}
|
|
|
|
|
2020-04-07 08:57:21 +00:00
|
|
|
return reg.ConfChan, errChan, nil
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|