forked from ltcsuite/neutrino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock_store.go
83 lines (66 loc) · 2.12 KB
/
mock_store.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package neutrino
import (
"fmt"
"github.com/dcrlabs/neutrino-ltc/headerfs"
"github.com/ltcsuite/ltcd/blockchain"
"github.com/ltcsuite/ltcd/chaincfg/chainhash"
"github.com/ltcsuite/ltcd/wire"
)
// mockBlockHeaderStore is an implementation of the BlockHeaderStore backed by
// a simple map.
type mockBlockHeaderStore struct {
headers map[chainhash.Hash]wire.BlockHeader
heights map[uint32]wire.BlockHeader
}
// A compile-time check to ensure the mockBlockHeaderStore adheres to the
// BlockHeaderStore interface.
var _ headerfs.BlockHeaderStore = (*mockBlockHeaderStore)(nil)
// NewMockBlockHeaderStore returns a version of the BlockHeaderStore that's
// backed by an in-memory map. This instance is meant to be used by callers
// outside the package to unit test components that require a BlockHeaderStore
// interface.
func newMockBlockHeaderStore() *mockBlockHeaderStore {
return &mockBlockHeaderStore{
headers: make(map[chainhash.Hash]wire.BlockHeader),
heights: make(map[uint32]wire.BlockHeader),
}
}
func (m *mockBlockHeaderStore) ChainTip() (*wire.BlockHeader,
uint32, error) {
return nil, 0, nil
}
func (m *mockBlockHeaderStore) LatestBlockLocator() (
blockchain.BlockLocator, error) {
return nil, nil
}
func (m *mockBlockHeaderStore) FetchHeaderByHeight(height uint32) (
*wire.BlockHeader, error) {
if header, ok := m.heights[height]; ok {
return &header, nil
}
return nil, headerfs.ErrHeightNotFound
}
func (m *mockBlockHeaderStore) FetchHeaderAncestors(uint32,
*chainhash.Hash) ([]wire.BlockHeader, uint32, error) {
return nil, 0, nil
}
func (m *mockBlockHeaderStore) HeightFromHash(*chainhash.Hash) (uint32, error) {
return 0, nil
}
func (m *mockBlockHeaderStore) RollbackLastBlock() (*headerfs.BlockStamp,
error) {
return nil, nil
}
func (m *mockBlockHeaderStore) FetchHeader(h *chainhash.Hash) (
*wire.BlockHeader, uint32, error) {
if header, ok := m.headers[*h]; ok {
return &header, 0, nil
}
return nil, 0, fmt.Errorf("not found")
}
func (m *mockBlockHeaderStore) WriteHeaders(headers ...headerfs.BlockHeader) error {
for _, h := range headers {
m.headers[h.BlockHash()] = *h.BlockHeader
}
return nil
}