From 671be697435212b3194491ca9e0d35bcc5b4dd6d Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Wed, 31 Oct 2018 20:42:14 -0700 Subject: [PATCH] watchtower/lookout/mock: adds mock backend --- watchtower/lookout/mock.go | 61 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 watchtower/lookout/mock.go diff --git a/watchtower/lookout/mock.go b/watchtower/lookout/mock.go new file mode 100644 index 000000000..c5d8839a5 --- /dev/null +++ b/watchtower/lookout/mock.go @@ -0,0 +1,61 @@ +// +build dev + +package lookout + +import ( + "fmt" + "sync" + + "github.com/btcsuite/btcd/chaincfg/chainhash" + "github.com/btcsuite/btcd/wire" + "github.com/lightningnetwork/lnd/chainntnfs" +) + +type MockBackend struct { + mu sync.RWMutex + + blocks chan *chainntnfs.BlockEpoch + epochs map[chainhash.Hash]*wire.MsgBlock + quit chan struct{} +} + +func NewMockBackend() *MockBackend { + return &MockBackend{ + blocks: make(chan *chainntnfs.BlockEpoch), + epochs: make(map[chainhash.Hash]*wire.MsgBlock), + quit: make(chan struct{}), + } +} + +func (m *MockBackend) RegisterBlockEpochNtfn(*chainntnfs.BlockEpoch) ( + *chainntnfs.BlockEpochEvent, error) { + + return &chainntnfs.BlockEpochEvent{ + Epochs: m.blocks, + }, nil +} + +func (m *MockBackend) GetBlock(hash *chainhash.Hash) (*wire.MsgBlock, error) { + m.mu.RLock() + defer m.mu.RUnlock() + + block, ok := m.epochs[*hash] + if !ok { + return nil, fmt.Errorf("unknown block for hash %x", hash) + } + + return block, nil +} + +func (m *MockBackend) ConnectEpoch(epoch *chainntnfs.BlockEpoch, + block *wire.MsgBlock) { + + m.mu.Lock() + m.epochs[*epoch.Hash] = block + m.mu.Unlock() + + select { + case m.blocks <- epoch: + case <-m.quit: + } +}