From 9ce89c1b46f52c69219cf84afd9c2dfa069cb0ce Mon Sep 17 00:00:00 2001 From: eugene Date: Tue, 18 Aug 2020 15:44:42 -0400 Subject: [PATCH] lnd: delete mock.go, move mockSpendNotifier to lntest/mock --- breacharbiter_test.go | 4 +- mock.go => lntest/mock/spendnotifier.go | 50 +++++++++++++------------ 2 files changed, 28 insertions(+), 26 deletions(-) rename mock.go => lntest/mock/spendnotifier.go (67%) diff --git a/breacharbiter_test.go b/breacharbiter_test.go index f38ca3b2..03c1cdb6 100644 --- a/breacharbiter_test.go +++ b/breacharbiter_test.go @@ -1434,7 +1434,7 @@ func testBreachSpends(t *testing.T, test breachTest) { // Notify that the breaching transaction is confirmed, to trigger the // retribution logic. - notifier := brar.cfg.Notifier.(*mockSpendNotifier) + notifier := brar.cfg.Notifier.(*mock.SpendNotifier) notifier.ConfChan <- &chainntnfs.TxConfirmation{} // 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} // Assemble our test arbiter. - notifier := makeMockSpendNotifier() + notifier := mock.MakeMockSpendNotifier() ba := newBreachArbiter(&BreachConfig{ CloseLink: func(_ *wire.OutPoint, _ htlcswitch.ChannelCloseType) {}, DB: db, diff --git a/mock.go b/lntest/mock/spendnotifier.go similarity index 67% rename from mock.go rename to lntest/mock/spendnotifier.go index 598b3d4a..7d51b458 100644 --- a/mock.go +++ b/lntest/mock/spendnotifier.go @@ -1,4 +1,4 @@ -package lnd +package mock import ( "sync" @@ -6,21 +6,21 @@ import ( "github.com/btcsuite/btcd/wire" "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. -type mockSpendNotifier struct { - *mock.ChainNotifier +type SpendNotifier struct { + *ChainNotifier spendMap map[wire.OutPoint][]chan *chainntnfs.SpendDetail spends map[wire.OutPoint]*chainntnfs.SpendDetail mtx sync.Mutex } -func makeMockSpendNotifier() *mockSpendNotifier { - return &mockSpendNotifier{ - ChainNotifier: &mock.ChainNotifier{ +// MakeMockSpendNotifier creates a SpendNotifier. +func MakeMockSpendNotifier() *SpendNotifier { + return &SpendNotifier{ + ChainNotifier: &ChainNotifier{ SpendChan: make(chan *chainntnfs.SpendDetail), EpochChan: make(chan *chainntnfs.BlockEpoch), 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) { - m.mtx.Lock() - defer m.mtx.Unlock() + + s.mtx.Lock() + defer s.mtx.Unlock() 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. spendChan <- &chainntnfs.SpendDetail{ SpentOutPoint: detail.SpentOutPoint, @@ -48,22 +50,22 @@ func (m *mockSpendNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint, } else { // Otherwise, queue the notification for delivery if the spend // is ever received. - m.spendMap[*outpoint] = append(m.spendMap[*outpoint], spendChan) + s.spendMap[*outpoint] = append(s.spendMap[*outpoint], spendChan) } return &chainntnfs.SpendEvent{ - Spend: spendChan, - Cancel: func() { - }, + Spend: spendChan, + Cancel: func() {}, }, nil } // Spend dispatches SpendDetails to all subscribers of the outpoint. The details -// will include the transaction and height provided by the caller. -func (m *mockSpendNotifier) Spend(outpoint *wire.OutPoint, height int32, +// will includethe transaction and height provided by the caller. +func (s *SpendNotifier) Spend(outpoint *wire.OutPoint, height int32, txn *wire.MsgTx) { - m.mtx.Lock() - defer m.mtx.Unlock() + + s.mtx.Lock() + defer s.mtx.Unlock() txnHash := txn.TxHash() details := &chainntnfs.SpendDetail{ @@ -75,13 +77,13 @@ func (m *mockSpendNotifier) Spend(outpoint *wire.OutPoint, height int32, } // Cache details in case of late registration. - if _, ok := m.spends[*outpoint]; !ok { - m.spends[*outpoint] = details + if _, ok := s.spends[*outpoint]; !ok { + s.spends[*outpoint] = details } // Deliver any backlogged spend notifications. - if spendChans, ok := m.spendMap[*outpoint]; ok { - delete(m.spendMap, *outpoint) + if spendChans, ok := s.spendMap[*outpoint]; ok { + delete(s.spendMap, *outpoint) for _, spendChan := range spendChans { spendChan <- &chainntnfs.SpendDetail{ SpentOutPoint: details.SpentOutPoint,