2020-08-18 22:44:42 +03:00
|
|
|
package mock
|
2017-07-14 22:05:55 +03:00
|
|
|
|
|
|
|
import (
|
2018-01-17 07:56:51 +03:00
|
|
|
"sync"
|
2017-12-13 12:30:57 +03:00
|
|
|
|
2018-06-05 04:34:16 +03:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2019-01-16 17:47:43 +03:00
|
|
|
|
2018-07-18 05:23:47 +03:00
|
|
|
"github.com/lightningnetwork/lnd/chainntnfs"
|
2019-11-01 07:39:17 +03:00
|
|
|
)
|
|
|
|
|
2020-08-18 22:44:42 +03:00
|
|
|
// SpendNotifier extends the mock.ChainNotifier so that spend
|
2020-08-27 22:34:55 +03:00
|
|
|
// notifications can be triggered and delivered to subscribers.
|
2020-08-18 22:44:42 +03:00
|
|
|
type SpendNotifier struct {
|
|
|
|
*ChainNotifier
|
2017-12-13 12:30:57 +03:00
|
|
|
spendMap map[wire.OutPoint][]chan *chainntnfs.SpendDetail
|
2019-03-20 05:22:47 +03:00
|
|
|
spends map[wire.OutPoint]*chainntnfs.SpendDetail
|
2018-06-02 11:02:20 +03:00
|
|
|
mtx sync.Mutex
|
2017-12-13 12:30:57 +03:00
|
|
|
}
|
|
|
|
|
2020-08-18 22:44:42 +03:00
|
|
|
// MakeMockSpendNotifier creates a SpendNotifier.
|
|
|
|
func MakeMockSpendNotifier() *SpendNotifier {
|
|
|
|
return &SpendNotifier{
|
|
|
|
ChainNotifier: &ChainNotifier{
|
2020-08-27 22:34:55 +03:00
|
|
|
SpendChan: make(chan *chainntnfs.SpendDetail),
|
|
|
|
EpochChan: make(chan *chainntnfs.BlockEpoch),
|
|
|
|
ConfChan: make(chan *chainntnfs.TxConfirmation),
|
2017-12-13 12:30:57 +03:00
|
|
|
},
|
|
|
|
spendMap: make(map[wire.OutPoint][]chan *chainntnfs.SpendDetail),
|
2019-03-20 05:22:47 +03:00
|
|
|
spends: make(map[wire.OutPoint]*chainntnfs.SpendDetail),
|
2017-12-13 12:30:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-18 22:44:42 +03:00
|
|
|
// RegisterSpendNtfn registers a spend notification for a specified outpoint.
|
|
|
|
func (s *SpendNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint,
|
2018-07-18 05:23:47 +03:00
|
|
|
_ []byte, heightHint uint32) (*chainntnfs.SpendEvent, error) {
|
2020-08-18 22:44:42 +03:00
|
|
|
|
|
|
|
s.mtx.Lock()
|
|
|
|
defer s.mtx.Unlock()
|
2017-12-13 12:30:57 +03:00
|
|
|
|
2019-03-20 05:22:47 +03:00
|
|
|
spendChan := make(chan *chainntnfs.SpendDetail, 1)
|
2020-08-18 22:44:42 +03:00
|
|
|
if detail, ok := s.spends[*outpoint]; ok {
|
2019-03-20 05:22:47 +03:00
|
|
|
// Deliver spend immediately if details are already known.
|
|
|
|
spendChan <- &chainntnfs.SpendDetail{
|
|
|
|
SpentOutPoint: detail.SpentOutPoint,
|
|
|
|
SpendingHeight: detail.SpendingHeight,
|
|
|
|
SpendingTx: detail.SpendingTx,
|
|
|
|
SpenderTxHash: detail.SpenderTxHash,
|
|
|
|
SpenderInputIndex: detail.SpenderInputIndex,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Otherwise, queue the notification for delivery if the spend
|
|
|
|
// is ever received.
|
2020-08-18 22:44:42 +03:00
|
|
|
s.spendMap[*outpoint] = append(s.spendMap[*outpoint], spendChan)
|
2019-03-20 05:22:47 +03:00
|
|
|
}
|
|
|
|
|
2017-12-13 12:30:57 +03:00
|
|
|
return &chainntnfs.SpendEvent{
|
2020-08-18 22:44:42 +03:00
|
|
|
Spend: spendChan,
|
|
|
|
Cancel: func() {},
|
2017-12-13 12:30:57 +03:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Spend dispatches SpendDetails to all subscribers of the outpoint. The details
|
2020-08-18 22:44:42 +03:00
|
|
|
// will includethe transaction and height provided by the caller.
|
|
|
|
func (s *SpendNotifier) Spend(outpoint *wire.OutPoint, height int32,
|
2017-12-13 12:30:57 +03:00
|
|
|
txn *wire.MsgTx) {
|
2020-08-18 22:44:42 +03:00
|
|
|
|
|
|
|
s.mtx.Lock()
|
|
|
|
defer s.mtx.Unlock()
|
2017-12-13 12:30:57 +03:00
|
|
|
|
2021-04-20 09:50:55 +03:00
|
|
|
var inputIndex uint32
|
|
|
|
for i, in := range txn.TxIn {
|
|
|
|
if in.PreviousOutPoint == *outpoint {
|
|
|
|
inputIndex = uint32(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-20 05:22:47 +03:00
|
|
|
txnHash := txn.TxHash()
|
|
|
|
details := &chainntnfs.SpendDetail{
|
|
|
|
SpentOutPoint: outpoint,
|
|
|
|
SpendingHeight: height,
|
|
|
|
SpendingTx: txn,
|
|
|
|
SpenderTxHash: &txnHash,
|
2021-04-20 09:50:55 +03:00
|
|
|
SpenderInputIndex: inputIndex,
|
2019-03-20 05:22:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cache details in case of late registration.
|
2020-08-18 22:44:42 +03:00
|
|
|
if _, ok := s.spends[*outpoint]; !ok {
|
|
|
|
s.spends[*outpoint] = details
|
2019-03-20 05:22:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Deliver any backlogged spend notifications.
|
2020-08-18 22:44:42 +03:00
|
|
|
if spendChans, ok := s.spendMap[*outpoint]; ok {
|
|
|
|
delete(s.spendMap, *outpoint)
|
2017-12-13 12:30:57 +03:00
|
|
|
for _, spendChan := range spendChans {
|
|
|
|
spendChan <- &chainntnfs.SpendDetail{
|
2019-03-20 05:22:47 +03:00
|
|
|
SpentOutPoint: details.SpentOutPoint,
|
|
|
|
SpendingHeight: details.SpendingHeight,
|
|
|
|
SpendingTx: details.SpendingTx,
|
|
|
|
SpenderTxHash: details.SpenderTxHash,
|
|
|
|
SpenderInputIndex: details.SpenderInputIndex,
|
2017-12-13 12:30:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|