Skip to content

Commit be48a19

Browse files
neutrino: mock block and filter header stores
1 parent d848a75 commit be48a19

File tree

4 files changed

+175
-100
lines changed

4 files changed

+175
-100
lines changed

blockmanager_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -798,8 +798,11 @@ func TestBlockManagerDetectBadPeers(t *testing.T) {
798798
for _, test := range testCases {
799799
// Create a mock block header store. We only need to be able to
800800
// serve a header for the target index.
801-
blockHeaders := newMockBlockHeaderStore()
802-
blockHeaders.heights[targetIndex] = blockHeader
801+
mBlockHeaderStore := &headerfs.MockBlockHeaderStore{}
802+
803+
mBlockHeaderStore.On("FetchHeaderByHeight", targetIndex).Return(
804+
&blockHeader, nil,
805+
)
803806

804807
// We set up the mock queryAllPeers to only respond according to
805808
// the active testcase.
@@ -851,7 +854,7 @@ func TestBlockManagerDetectBadPeers(t *testing.T) {
851854

852855
bm := &blockManager{
853856
cfg: &blockManagerCfg{
854-
BlockHeaders: blockHeaders,
857+
BlockHeaders: mBlockHeaderStore,
855858
queryAllPeers: queryAllPeers,
856859
},
857860
}

headerfs/store_mock.go

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package headerfs
2+
3+
import (
4+
"github.com/btcsuite/btcd/blockchain"
5+
"github.com/btcsuite/btcd/chaincfg/chainhash"
6+
"github.com/btcsuite/btcd/wire"
7+
"github.com/stretchr/testify/mock"
8+
)
9+
10+
// MockBlockHeaderStore is a mock implementation of the BlockHeaderStore.
11+
type MockBlockHeaderStore struct {
12+
mock.Mock
13+
}
14+
15+
// ChainTip returns the current chain tip for the mock block header store.
16+
func (m *MockBlockHeaderStore) ChainTip() (*wire.BlockHeader, uint32, error) {
17+
args := m.Called()
18+
return args.Get(0).(*wire.BlockHeader),
19+
args.Get(1).(uint32),
20+
args.Error(2)
21+
}
22+
23+
// LatestBlockLocator returns the latest block locator for the mock block header
24+
// store.
25+
//
26+
//nolint:lll
27+
func (m *MockBlockHeaderStore) LatestBlockLocator() (blockchain.BlockLocator, error) {
28+
args := m.Called()
29+
return args.Get(0).(blockchain.BlockLocator), args.Error(1)
30+
}
31+
32+
// FetchHeaderByHeight fetches a block header by height for the mock block
33+
// header store.
34+
func (m *MockBlockHeaderStore) FetchHeaderByHeight(
35+
height uint32) (*wire.BlockHeader, error) {
36+
37+
args := m.Called(height)
38+
if args.Get(0) == nil {
39+
return nil, args.Error(1)
40+
}
41+
return args.Get(0).(*wire.BlockHeader), args.Error(1)
42+
}
43+
44+
// FetchHeaderAncestors fetches block header ancestors for the mock block header
45+
// store.
46+
func (m *MockBlockHeaderStore) FetchHeaderAncestors(numHeaders uint32,
47+
stopHash *chainhash.Hash) ([]wire.BlockHeader, uint32, error) {
48+
49+
args := m.Called(numHeaders, stopHash)
50+
return args.Get(0).([]wire.BlockHeader),
51+
args.Get(1).(uint32),
52+
args.Error(2)
53+
}
54+
55+
// HeightFromHash returns the height from a hash for the mock block header
56+
// store.
57+
func (m *MockBlockHeaderStore) HeightFromHash(
58+
hash *chainhash.Hash) (uint32, error) {
59+
60+
args := m.Called(hash)
61+
return args.Get(0).(uint32), args.Error(1)
62+
}
63+
64+
// FetchHeader fetches a block header by hash for the mock block header store.
65+
func (m *MockBlockHeaderStore) FetchHeader(
66+
hash *chainhash.Hash) (*wire.BlockHeader, uint32, error) {
67+
68+
args := m.Called(hash)
69+
if args.Get(0) == nil {
70+
return nil, args.Get(1).(uint32), args.Error(2)
71+
}
72+
return args.Get(0).(*wire.BlockHeader),
73+
args.Get(1).(uint32),
74+
args.Error(2)
75+
}
76+
77+
// WriteHeaders writes block headers to the mock block header store.
78+
func (m *MockBlockHeaderStore) WriteHeaders(hdrs ...BlockHeader) error {
79+
args := m.Called(hdrs)
80+
return args.Error(0)
81+
}
82+
83+
// RollbackLastBlock rolls back the last block in the mock block header store.
84+
func (m *MockBlockHeaderStore) RollbackLastBlock() (*BlockStamp, error) {
85+
args := m.Called()
86+
if args.Get(0) == nil {
87+
return nil, args.Error(1)
88+
}
89+
return args.Get(0).(*BlockStamp), args.Error(1)
90+
}
91+
92+
// MockFilterHeaderStore is a mock implementation of the FilterHeaderStore.
93+
type MockFilterHeaderStore struct {
94+
mock.Mock
95+
}
96+
97+
// ChainTip returns the current chain tip for the mock filter header store.
98+
func (m *MockFilterHeaderStore) ChainTip() (*chainhash.Hash, uint32, error) {
99+
args := m.Called()
100+
return args.Get(0).(*chainhash.Hash),
101+
args.Get(1).(uint32),
102+
args.Error(2)
103+
}
104+
105+
// FetchHeader fetches a filter header by hash for the mock filter header store.
106+
func (m *MockFilterHeaderStore) FetchHeader(
107+
hash *chainhash.Hash) (*chainhash.Hash, error) {
108+
109+
args := m.Called(hash)
110+
if args.Get(0) == nil {
111+
return nil, args.Error(1)
112+
}
113+
return args.Get(0).(*chainhash.Hash), args.Error(1)
114+
}
115+
116+
// FetchHeaderAncestors fetches filter header ancestors for the mock filter
117+
// header store.
118+
func (m *MockFilterHeaderStore) FetchHeaderAncestors(numHeaders uint32,
119+
stopHash *chainhash.Hash) ([]chainhash.Hash, uint32, error) {
120+
121+
args := m.Called(numHeaders, stopHash)
122+
return args.Get(0).([]chainhash.Hash),
123+
args.Get(1).(uint32),
124+
args.Error(2)
125+
}
126+
127+
// FetchHeaderByHeight fetches a filter header by height for the mock filter
128+
// header store.
129+
func (m *MockFilterHeaderStore) FetchHeaderByHeight(
130+
height uint32) (*chainhash.Hash, error) {
131+
132+
args := m.Called(height)
133+
if args.Get(0) == nil {
134+
return nil, args.Error(1)
135+
}
136+
return args.Get(0).(*chainhash.Hash), args.Error(1)
137+
}
138+
139+
// WriteHeaders writes filter headers to the mock filter header store.
140+
func (m *MockFilterHeaderStore) WriteHeaders(headers ...FilterHeader) error {
141+
args := m.Called(headers)
142+
return args.Error(0)
143+
}
144+
145+
// RollbackLastBlock rolls back the last block in the mock filter header store.
146+
func (m *MockFilterHeaderStore) RollbackLastBlock(
147+
newTip *chainhash.Hash) (*BlockStamp, error) {
148+
149+
args := m.Called(newTip)
150+
if args.Get(0) == nil {
151+
return nil, args.Error(1)
152+
}
153+
return args.Get(0).(*BlockStamp), args.Error(1)
154+
}

mock_store.go

Lines changed: 0 additions & 84 deletions
This file was deleted.

query_test.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -254,23 +254,20 @@ func TestBlockCache(t *testing.T) {
254254

255255
// Load the first 255 blocks from disk.
256256
blocks, err := loadBlocks(t, blockDataFile, blockDataNet)
257-
if err != nil {
258-
t.Fatalf("loadBlocks: Unexpected error: %v", err)
259-
}
257+
require.NoError(t, err)
260258

261259
// We'll use a simple mock header store since the GetBlocks method
262260
// assumes we only query for blocks with an already known header.
263-
headers := newMockBlockHeaderStore()
261+
mBlockHeaderStore := &headerfs.MockBlockHeaderStore{}
264262

265263
// Iterate through the blocks, calculating the size of half of them,
266264
// and writing them to the header store.
267265
var size uint64
268266
for i, b := range blocks {
269-
header := headerfs.BlockHeader{
270-
BlockHeader: &b.MsgBlock().Header,
271-
Height: uint32(i),
272-
}
273-
headers.WriteHeaders(header)
267+
blkHeader := &b.MsgBlock().Header
268+
mBlockHeaderStore.On("FetchHeader", b.Hash()).Return(
269+
blkHeader, uint32(i), nil,
270+
)
274271

275272
sz, _ := (&CacheableBlock{Block: b}).Size()
276273
if i < len(blocks)/2 {
@@ -284,7 +281,7 @@ func TestBlockCache(t *testing.T) {
284281
BlockCache: lru.NewCache[wire.InvVect, *CacheableBlock](
285282
size,
286283
),
287-
BlockHeaders: headers,
284+
BlockHeaders: mBlockHeaderStore,
288285
chainParams: chaincfg.Params{
289286
PowLimit: maxPowLimit,
290287
},
@@ -317,7 +314,9 @@ func TestBlockCache(t *testing.T) {
317314
continue
318315
}
319316

320-
header, _, err := headers.FetchHeader(b.Hash())
317+
header, _, err := mBlockHeaderStore.FetchHeader(
318+
b.Hash(),
319+
)
321320
require.NoError(t, err)
322321

323322
resp := &wire.MsgBlock{
@@ -333,13 +332,16 @@ func TestBlockCache(t *testing.T) {
333332
select {
334333
case queries <- inv.Hash:
335334
case <-time.After(1 * time.Second):
336-
t.Fatalf("query was not handled")
335+
require.Fail(t, "query was not handled")
337336
}
338337

339338
return errChan
340339
}
341340

342-
t.Fatalf("queried for unknown block: %v", inv.Hash)
341+
require.Failf(
342+
t, "Test Failed Due to Unknown Block",
343+
"queried for unknown block: %v", inv.Hash,
344+
)
343345

344346
return errChan
345347
}

0 commit comments

Comments
 (0)