lnd: delete mock.go, move mockSpendNotifier to lntest/mock

This commit is contained in:
eugene 2020-08-18 15:44:42 -04:00
parent 4ff03afee0
commit 9ce89c1b46
2 changed files with 28 additions and 26 deletions

@ -1434,7 +1434,7 @@ func testBreachSpends(t *testing.T, test breachTest) {
// Notify that the breaching transaction is confirmed, to trigger the // Notify that the breaching transaction is confirmed, to trigger the
// retribution logic. // retribution logic.
notifier := brar.cfg.Notifier.(*mockSpendNotifier) notifier := brar.cfg.Notifier.(*mock.SpendNotifier)
notifier.ConfChan <- &chainntnfs.TxConfirmation{} notifier.ConfChan <- &chainntnfs.TxConfirmation{}
// The breach arbiter should attempt to sweep all outputs on the // The breach arbiter should attempt to sweep all outputs on the
@ -1674,7 +1674,7 @@ func createTestArbiter(t *testing.T, contractBreaches chan *ContractBreachEvent,
signer := &mock.SingleSigner{Privkey: aliceKeyPriv} signer := &mock.SingleSigner{Privkey: aliceKeyPriv}
// Assemble our test arbiter. // Assemble our test arbiter.
notifier := makeMockSpendNotifier() notifier := mock.MakeMockSpendNotifier()
ba := newBreachArbiter(&BreachConfig{ ba := newBreachArbiter(&BreachConfig{
CloseLink: func(_ *wire.OutPoint, _ htlcswitch.ChannelCloseType) {}, CloseLink: func(_ *wire.OutPoint, _ htlcswitch.ChannelCloseType) {},
DB: db, DB: db,

@ -1,4 +1,4 @@
package lnd package mock
import ( import (
"sync" "sync"
@ -6,21 +6,21 @@ import (
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/lntest/mock"
) )
// mockSpendNotifier extends the mock.ChainNotifier so that spend // SpendNotifier extends the mock.ChainNotifier so that spend
// notifications can be triggered and delivered to subscribers. // notifications can be triggered and delivered to subscribers.
type mockSpendNotifier struct { type SpendNotifier struct {
*mock.ChainNotifier *ChainNotifier
spendMap map[wire.OutPoint][]chan *chainntnfs.SpendDetail spendMap map[wire.OutPoint][]chan *chainntnfs.SpendDetail
spends map[wire.OutPoint]*chainntnfs.SpendDetail spends map[wire.OutPoint]*chainntnfs.SpendDetail
mtx sync.Mutex mtx sync.Mutex
} }
func makeMockSpendNotifier() *mockSpendNotifier { // MakeMockSpendNotifier creates a SpendNotifier.
return &mockSpendNotifier{ func MakeMockSpendNotifier() *SpendNotifier {
ChainNotifier: &mock.ChainNotifier{ return &SpendNotifier{
ChainNotifier: &ChainNotifier{
SpendChan: make(chan *chainntnfs.SpendDetail), SpendChan: make(chan *chainntnfs.SpendDetail),
EpochChan: make(chan *chainntnfs.BlockEpoch), EpochChan: make(chan *chainntnfs.BlockEpoch),
ConfChan: make(chan *chainntnfs.TxConfirmation), ConfChan: make(chan *chainntnfs.TxConfirmation),
@ -30,13 +30,15 @@ func makeMockSpendNotifier() *mockSpendNotifier {
} }
} }
func (m *mockSpendNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint, // RegisterSpendNtfn registers a spend notification for a specified outpoint.
func (s *SpendNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint,
_ []byte, heightHint uint32) (*chainntnfs.SpendEvent, error) { _ []byte, heightHint uint32) (*chainntnfs.SpendEvent, error) {
m.mtx.Lock()
defer m.mtx.Unlock() s.mtx.Lock()
defer s.mtx.Unlock()
spendChan := make(chan *chainntnfs.SpendDetail, 1) spendChan := make(chan *chainntnfs.SpendDetail, 1)
if detail, ok := m.spends[*outpoint]; ok { if detail, ok := s.spends[*outpoint]; ok {
// Deliver spend immediately if details are already known. // Deliver spend immediately if details are already known.
spendChan <- &chainntnfs.SpendDetail{ spendChan <- &chainntnfs.SpendDetail{
SpentOutPoint: detail.SpentOutPoint, SpentOutPoint: detail.SpentOutPoint,
@ -48,22 +50,22 @@ func (m *mockSpendNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint,
} else { } else {
// Otherwise, queue the notification for delivery if the spend // Otherwise, queue the notification for delivery if the spend
// is ever received. // is ever received.
m.spendMap[*outpoint] = append(m.spendMap[*outpoint], spendChan) s.spendMap[*outpoint] = append(s.spendMap[*outpoint], spendChan)
} }
return &chainntnfs.SpendEvent{ return &chainntnfs.SpendEvent{
Spend: spendChan, Spend: spendChan,
Cancel: func() { Cancel: func() {},
},
}, nil }, nil
} }
// Spend dispatches SpendDetails to all subscribers of the outpoint. The details // Spend dispatches SpendDetails to all subscribers of the outpoint. The details
// will include the transaction and height provided by the caller. // will includethe transaction and height provided by the caller.
func (m *mockSpendNotifier) Spend(outpoint *wire.OutPoint, height int32, func (s *SpendNotifier) Spend(outpoint *wire.OutPoint, height int32,
txn *wire.MsgTx) { txn *wire.MsgTx) {
m.mtx.Lock()
defer m.mtx.Unlock() s.mtx.Lock()
defer s.mtx.Unlock()
txnHash := txn.TxHash() txnHash := txn.TxHash()
details := &chainntnfs.SpendDetail{ details := &chainntnfs.SpendDetail{
@ -75,13 +77,13 @@ func (m *mockSpendNotifier) Spend(outpoint *wire.OutPoint, height int32,
} }
// Cache details in case of late registration. // Cache details in case of late registration.
if _, ok := m.spends[*outpoint]; !ok { if _, ok := s.spends[*outpoint]; !ok {
m.spends[*outpoint] = details s.spends[*outpoint] = details
} }
// Deliver any backlogged spend notifications. // Deliver any backlogged spend notifications.
if spendChans, ok := m.spendMap[*outpoint]; ok { if spendChans, ok := s.spendMap[*outpoint]; ok {
delete(m.spendMap, *outpoint) delete(s.spendMap, *outpoint)
for _, spendChan := range spendChans { for _, spendChan := range spendChans {
spendChan <- &chainntnfs.SpendDetail{ spendChan <- &chainntnfs.SpendDetail{
SpentOutPoint: details.SpentOutPoint, SpentOutPoint: details.SpentOutPoint,