Skip to content

Commit d848a75

Browse files
multi: interface filter header store
1 parent 6355419 commit d848a75

File tree

6 files changed

+123
-86
lines changed

6 files changed

+123
-86
lines changed

blockmanager.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ type blockManagerCfg struct {
8484

8585
// RegFilterHeaders is the store where filter headers for the regular
8686
// compact filters are persistently stored.
87-
RegFilterHeaders *headerfs.FilterHeaderStore
87+
RegFilterHeaders headerfs.FilterHeaderStore
8888

8989
// TimeSource is used to access a time estimate based on the clocks of
9090
// the connected peers.
@@ -702,7 +702,7 @@ waitForHeaders:
702702
// network, if it can, and resolves any conflicts between them. It then writes
703703
// any verified headers to the store.
704704
func (b *blockManager) getUncheckpointedCFHeaders(
705-
store *headerfs.FilterHeaderStore, fType wire.FilterType) error {
705+
store headerfs.FilterHeaderStore, fType wire.FilterType) error {
706706

707707
// Get the filter header store's chain tip.
708708
filterTip, filtHeight, err := store.ChainTip()
@@ -928,7 +928,7 @@ func (c *checkpointedCFHeadersQuery) handleResponse(req, resp wire.Message,
928928
// checkpoints we got from the network. It assumes that the filter header store
929929
// matches the checkpoints up to the tip of the store.
930930
func (b *blockManager) getCheckpointedCFHeaders(checkpoints []*chainhash.Hash,
931-
store *headerfs.FilterHeaderStore, fType wire.FilterType) {
931+
store headerfs.FilterHeaderStore, fType wire.FilterType) {
932932

933933
// We keep going until we've caught up the filter header store with the
934934
// latest known checkpoint.
@@ -1174,7 +1174,7 @@ func (b *blockManager) getCheckpointedCFHeaders(checkpoints []*chainhash.Hash,
11741174
// filter header field in the next message range before writing to disk, and
11751175
// the current height after writing the headers.
11761176
func (b *blockManager) writeCFHeadersMsg(msg *wire.MsgCFHeaders,
1177-
store *headerfs.FilterHeaderStore) (*chainhash.Hash, uint32, error) {
1177+
store headerfs.FilterHeaderStore) (*chainhash.Hash, uint32, error) {
11781178

11791179
// Check that the PrevFilterHeader is the same as the last stored so we
11801180
// can prevent misalignment.
@@ -1364,7 +1364,7 @@ func verifyCheckpoint(prevCheckpoint, nextCheckpoint *chainhash.Hash,
13641364
// information.
13651365
func (b *blockManager) resolveConflict(
13661366
checkpoints map[string][]*chainhash.Hash,
1367-
store *headerfs.FilterHeaderStore, fType wire.FilterType) (
1367+
store headerfs.FilterHeaderStore, fType wire.FilterType) (
13681368
[]*chainhash.Hash, error) {
13691369

13701370
// First check the served checkpoints against the hardcoded ones.
@@ -1913,7 +1913,7 @@ func (b *blockManager) getCheckpts(lastHash *chainhash.Hash,
19131913
// existing store up to the tip of the store. If all of the peers match but
19141914
// the store doesn't, the height at which the mismatch occurs is returned.
19151915
func checkCFCheckptSanity(cp map[string][]*chainhash.Hash,
1916-
headerStore *headerfs.FilterHeaderStore) (int, error) {
1916+
headerStore headerfs.FilterHeaderStore) (int, error) {
19171917

19181918
// Get the known best header to compare against checkpoints.
19191919
_, storeTip, err := headerStore.ChainTip()

blockmanager_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (m *mockDispatcher) Query(requests []*query.Request,
5555

5656
// setupBlockManager initialises a blockManager to be used in tests.
5757
func setupBlockManager(t *testing.T) (*blockManager, headerfs.BlockHeaderStore,
58-
*headerfs.FilterHeaderStore, error) {
58+
headerfs.FilterHeaderStore, error) {
5959

6060
// Set up the block and filter header stores.
6161
tempDir := t.TempDir()

headerfs/file.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func (h *blockHeaderStore) readHeader(height uint32) (wire.BlockHeader, error) {
140140

141141
// readHeader reads a single filter header at the specified height from the
142142
// flat files on disk.
143-
func (f *FilterHeaderStore) readHeader(height uint32) (*chainhash.Hash, error) {
143+
func (f *filterHeaderStore) readHeader(height uint32) (*chainhash.Hash, error) {
144144
seekDistance := uint64(height) * 32
145145

146146
rawHeader, err := f.readRaw(seekDistance)
@@ -158,7 +158,7 @@ func (f *FilterHeaderStore) readHeader(height uint32) (*chainhash.Hash, error) {
158158
//
159159
// NOTE: The end height is _inclusive_ so we'll fetch all headers from the
160160
// startHeight up to the end height, including the final header.
161-
func (f *FilterHeaderStore) readHeaderRange(startHeight uint32,
161+
func (f *filterHeaderStore) readHeaderRange(startHeight uint32,
162162
endHeight uint32) ([]chainhash.Hash, error) {
163163

164164
// Based on the defined header type, we'll determine the number of

headerfs/store.go

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -613,22 +613,52 @@ func (h *blockHeaderStore) ChainTip() (*wire.BlockHeader, uint32, error) {
613613
return &latestHeader, tipHeight, nil
614614
}
615615

616-
// FilterHeaderStore is an implementation of a fully fledged database for any
617-
// variant of filter headers. The FilterHeaderStore combines a flat file to
616+
// FilterHeaderStore defines the interface for storing and retrieving filter
617+
// headers.
618+
type FilterHeaderStore interface {
619+
// ChainTip returns the hash and height of the latest filter header.
620+
ChainTip() (*chainhash.Hash, uint32, error)
621+
622+
// FetchHeader fetches the filter header for a specific block hash.
623+
FetchHeader(hash *chainhash.Hash) (*chainhash.Hash, error)
624+
625+
// FetchHeaderAncestors fetches the given number of headers starting
626+
// from the specified stop hash and working backwards.
627+
FetchHeaderAncestors(numHeaders uint32,
628+
stopHash *chainhash.Hash) ([]chainhash.Hash, uint32, error)
629+
630+
// FetchHeaderByHeight fetches the filter header for a specific block
631+
// height.
632+
FetchHeaderByHeight(height uint32) (*chainhash.Hash, error)
633+
634+
// WriteHeaders writes a set of filter headers to the store.
635+
WriteHeaders(hdrs ...FilterHeader) error
636+
637+
// RollbackLastBlock rolls back the last block, returning the new tip
638+
// after rollback.
639+
RollbackLastBlock(newTip *chainhash.Hash) (*BlockStamp, error)
640+
}
641+
642+
// filterHeaderStore is an implementation of a fully fledged database for any
643+
// variant of filter headers. The filterHeaderStore combines a flat file to
618644
// store the block headers with a database instance for managing the index into
619645
// the set of flat files.
620-
type FilterHeaderStore struct {
646+
type filterHeaderStore struct {
621647
*headerStore
622648
}
623649

650+
// Compile-time assertion to ensure filterHeaderStore implements
651+
// FilterHeaderStore interface.
652+
var _ FilterHeaderStore = (*filterHeaderStore)(nil)
653+
624654
// NewFilterHeaderStore returns a new instance of the FilterHeaderStore based
625655
// on a target file path, filter type, and target net parameters. These
626656
// parameters are required as if this is the initial start up of the
627657
// FilterHeaderStore, then the initial genesis filter header will need to be
628658
// inserted.
629659
func NewFilterHeaderStore(filePath string, db walletdb.DB,
630660
filterType HeaderType, netParams *chaincfg.Params,
631-
headerStateAssertion *FilterHeader) (*FilterHeaderStore, error) {
661+
headerStateAssertion *FilterHeader) (FilterHeaderStore, error) {
632662

633663
fStore, err := newHeaderStore(db, filePath, filterType)
634664
if err != nil {
@@ -642,7 +672,7 @@ func NewFilterHeaderStore(filePath string, db walletdb.DB,
642672
return nil, err
643673
}
644674

645-
fhs := &FilterHeaderStore{
675+
fhs := &filterHeaderStore{
646676
fStore,
647677
}
648678

@@ -746,7 +776,7 @@ func NewFilterHeaderStore(filePath string, db walletdb.DB,
746776
// maybeResetHeaderState will reset the header state if the header assertion
747777
// fails, but only if the target height is found. The boolean returned indicates
748778
// that header state was reset.
749-
func (f *FilterHeaderStore) maybeResetHeaderState(
779+
func (f *filterHeaderStore) maybeResetHeaderState(
750780
headerStateAssertion *FilterHeader) (bool, error) {
751781

752782
// First, we'll attempt to locate the header at this height. If no such
@@ -781,7 +811,11 @@ func (f *FilterHeaderStore) maybeResetHeaderState(
781811

782812
// FetchHeader returns the filter header that corresponds to the passed block
783813
// height.
784-
func (f *FilterHeaderStore) FetchHeader(hash *chainhash.Hash) (*chainhash.Hash, error) {
814+
//
815+
// NOTE: Part of the FilterHeaderStore interface.
816+
func (f *filterHeaderStore) FetchHeader(
817+
hash *chainhash.Hash) (*chainhash.Hash, error) {
818+
785819
// Lock store for read.
786820
f.mtx.RLock()
787821
defer f.mtx.RUnlock()
@@ -795,7 +829,11 @@ func (f *FilterHeaderStore) FetchHeader(hash *chainhash.Hash) (*chainhash.Hash,
795829
}
796830

797831
// FetchHeaderByHeight returns the filter header for a particular block height.
798-
func (f *FilterHeaderStore) FetchHeaderByHeight(height uint32) (*chainhash.Hash, error) {
832+
//
833+
// NOTE: Part of the FilterHeaderStore interface.
834+
func (f *filterHeaderStore) FetchHeaderByHeight(
835+
height uint32) (*chainhash.Hash, error) {
836+
799837
// Lock store for read.
800838
f.mtx.RLock()
801839
defer f.mtx.RUnlock()
@@ -809,7 +847,9 @@ func (f *FilterHeaderStore) FetchHeaderByHeight(height uint32) (*chainhash.Hash,
809847
// then return the final header specified by the stop hash. We'll also return
810848
// the starting height of the header range as well so callers can compute the
811849
// height of each header without knowing the height of the stop hash.
812-
func (f *FilterHeaderStore) FetchHeaderAncestors(numHeaders uint32,
850+
//
851+
// NOTE: Part of the FilterHeaderStore interface.
852+
func (f *filterHeaderStore) FetchHeaderAncestors(numHeaders uint32,
813853
stopHash *chainhash.Hash) ([]chainhash.Hash, uint32, error) {
814854

815855
// First, we'll find the final header in the range, this will be the
@@ -855,7 +895,9 @@ func (f *FilterHeader) toIndexEntry() headerEntry {
855895
// WriteHeaders writes a batch of filter headers to persistent storage. The
856896
// headers themselves are appended to the flat file, and then the index updated
857897
// to reflect the new entries.
858-
func (f *FilterHeaderStore) WriteHeaders(hdrs ...FilterHeader) error {
898+
//
899+
// NOTE: Part of the FilterHeaderStore interface.
900+
func (f *filterHeaderStore) WriteHeaders(hdrs ...FilterHeader) error {
859901
// Lock store for write.
860902
f.mtx.Lock()
861903
defer f.mtx.Unlock()
@@ -896,7 +938,9 @@ func (f *FilterHeaderStore) WriteHeaders(hdrs ...FilterHeader) error {
896938

897939
// ChainTip returns the latest filter header and height known to the
898940
// FilterHeaderStore.
899-
func (f *FilterHeaderStore) ChainTip() (*chainhash.Hash, uint32, error) {
941+
//
942+
// NOTE: Part of the FilterHeaderStore interface.
943+
func (f *filterHeaderStore) ChainTip() (*chainhash.Hash, uint32, error) {
900944
// Lock store for read.
901945
f.mtx.RLock()
902946
defer f.mtx.RUnlock()
@@ -919,7 +963,11 @@ func (f *FilterHeaderStore) ChainTip() (*chainhash.Hash, uint32, error) {
919963
// re-org which disconnects the latest filter header from the end of the main
920964
// chain. The information about the latest header tip after truncation is
921965
// returned.
922-
func (f *FilterHeaderStore) RollbackLastBlock(newTip *chainhash.Hash) (*BlockStamp, error) {
966+
//
967+
// NOTE: Part of the FilterHeaderStore interface.
968+
func (f *filterHeaderStore) RollbackLastBlock(
969+
newTip *chainhash.Hash) (*BlockStamp, error) {
970+
923971
// Lock store for write.
924972
f.mtx.Lock()
925973
defer f.mtx.Unlock()

0 commit comments

Comments
 (0)