forked from lightningnetwork/lnd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock.go
59 lines (46 loc) · 1.11 KB
/
mock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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:
}
}