Skip to content

Commit

Permalink
cache: move filter+block cache impls into top level pkg
Browse files Browse the repository at this point in the history
In this commit, we move the filter+block cache implementations into a
top level package. This allows the package to has less dependencies,
which preps to make it a module in an upcoming commit.
  • Loading branch information
Roasbeef committed Feb 11, 2023
1 parent 88d8515 commit 1224cf6
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 26 deletions.
16 changes: 8 additions & 8 deletions blockmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -1625,14 +1625,14 @@ func (b *blockManager) detectBadPeers(headers map[string]*wire.MsgCFHeaders,
//
// We'll use a few strategies to figure out which peers we believe serve
// invalid filters:
// 1. If a peers' filter doesn't match on a script that must match, we know
// the filter is invalid.
// 2. If a peers' filter matches on a script that _should not_ match, it
// is potentially invalid. In this case we ban peers that matches more
// such scripts than other peers.
// 3. If we cannot detect which filters are invalid from the block
// contents, we ban peers serving filters different from the majority of
// peers.
// 1. If a peers' filter doesn't match on a script that must match, we know
// the filter is invalid.
// 2. If a peers' filter matches on a script that _should not_ match, it
// is potentially invalid. In this case we ban peers that matches more
// such scripts than other peers.
// 3. If we cannot detect which filters are invalid from the block
// contents, we ban peers serving filters different from the majority of
// peers.
func resolveFilterMismatchFromBlock(block *wire.MsgBlock,
fType wire.FilterType, filtersFromPeers map[string]*gcs.Filter,
threshold int) ([]string, error) {
Expand Down
14 changes: 7 additions & 7 deletions cache/cache_test.go → cache_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cache_test
package neutrino

import (
"crypto/rand"
Expand Down Expand Up @@ -55,9 +55,9 @@ func TestBlockFilterCaches(t *testing.T) {
filters = append(filters, filter)

// Put the generated filter in the filter caches.
cacheKey := cache.FilterCacheKey{blockHash, filterType}
cacheKey := FilterCacheKey{blockHash, filterType}
for _, c := range filterCaches {
_, _ = c.Put(cacheKey, &cache.CacheableFilter{Filter: filter})
_, _ = c.Put(cacheKey, &CacheableFilter{Filter: filter})
}

msgBlock := &wire.MsgBlock{}
Expand All @@ -70,7 +70,7 @@ func TestBlockFilterCaches(t *testing.T) {
wire.InvTypeWitnessBlock, &blockHash,
)
for _, c := range blockCaches {
_, _ = c.Put(*blockKey, &cache.CacheableBlock{block})
_, _ = c.Put(*blockKey, &CacheableBlock{block})
}
}

Expand All @@ -80,15 +80,15 @@ func TestBlockFilterCaches(t *testing.T) {
blockHash := blockHash

// Check filter caches.
cacheKey := cache.FilterCacheKey{blockHash, filterType}
cacheKey := FilterCacheKey{blockHash, filterType}
for _, c := range filterCaches {
e, err := c.Get(cacheKey)
if err != nil {
t.Fatalf("Unable to get filter: %v", err)
}

// Ensure we got the correct filter.
filter := e.(*cache.CacheableFilter).Filter
filter := e.(*CacheableFilter).Filter
if filter != filters[i] {
t.Fatalf("Filters not equal: %v vs %v ",
filter, filters[i])
Expand All @@ -106,7 +106,7 @@ func TestBlockFilterCaches(t *testing.T) {
}

// Ensure it is the same block.
block := b.(*cache.CacheableBlock).Block
block := b.(*CacheableBlock).Block
if block != blocks[i] {
t.Fatalf("Not equal: %v vs %v ",
block, blocks[i])
Expand Down
2 changes: 1 addition & 1 deletion cache/cacheable_block.go → cacheable_block.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cache
package neutrino

import "github.com/btcsuite/btcd/btcutil"

Expand Down
2 changes: 1 addition & 1 deletion cache/cacheable_filter.go → cacheable_filter.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cache
package neutrino

import (
"github.com/btcsuite/btcd/btcutil/gcs"
Expand Down
12 changes: 6 additions & 6 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ checkResponses:
func (s *ChainService) getFilterFromCache(blockHash *chainhash.Hash,
filterType filterdb.FilterType) (*gcs.Filter, error) {

cacheKey := cache.FilterCacheKey{
cacheKey := FilterCacheKey{
BlockHash: *blockHash,
FilterType: filterType,
}
Expand All @@ -557,18 +557,18 @@ func (s *ChainService) getFilterFromCache(blockHash *chainhash.Hash,
return nil, err
}

return filterValue.(*cache.CacheableFilter).Filter, nil
return filterValue.(*CacheableFilter).Filter, nil
}

// putFilterToCache inserts a given filter in ChainService's FilterCache.
func (s *ChainService) putFilterToCache(blockHash *chainhash.Hash,
filterType filterdb.FilterType, filter *gcs.Filter) (bool, error) { // nolint:unparam

cacheKey := cache.FilterCacheKey{
cacheKey := FilterCacheKey{
BlockHash: *blockHash,
FilterType: filterType,
}
return s.FilterCache.Put(cacheKey, &cache.CacheableFilter{Filter: filter})
return s.FilterCache.Put(cacheKey, &CacheableFilter{Filter: filter})
}

// cfiltersQuery is a struct that holds all the information necessary to
Expand Down Expand Up @@ -982,7 +982,7 @@ func (s *ChainService) GetBlock(blockHash chainhash.Hash,
// If the block is already in the cache, we can return it immediately.
blockValue, err := s.BlockCache.Get(*inv)
if err == nil && blockValue != nil {
return blockValue.(*cache.CacheableBlock).Block, err
return blockValue.(*CacheableBlock).Block, err
}
if err != nil && err != cache.ErrElementNotFound {
return nil, err
Expand Down Expand Up @@ -1066,7 +1066,7 @@ func (s *ChainService) GetBlock(blockHash chainhash.Hash,
}

// Add block to the cache before returning it.
_, err = s.BlockCache.Put(*inv, &cache.CacheableBlock{Block: foundBlock})
_, err = s.BlockCache.Put(*inv, &CacheableBlock{Block: foundBlock})
if err != nil {
log.Warnf("couldn't write block to cache: %v", err)
}
Expand Down
5 changes: 2 additions & 3 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/lightninglabs/neutrino/cache"
"github.com/lightninglabs/neutrino/cache/lru"
"github.com/lightninglabs/neutrino/filterdb"
"github.com/lightninglabs/neutrino/headerfs"
Expand Down Expand Up @@ -153,7 +152,7 @@ func genRandFilter(numElements uint32, t *testing.T) (
}

// Convert into CacheableFilter and compute Size.
c := &cache.CacheableFilter{Filter: filter}
c := &CacheableFilter{Filter: filter}
s, err := c.Size()
if err != nil {
t.Fatalf("unable to create random filter: %v", err)
Expand Down Expand Up @@ -267,7 +266,7 @@ func TestBlockCache(t *testing.T) {
}
headers.WriteHeaders(header)

sz, _ := (&cache.CacheableBlock{Block: b}).Size()
sz, _ := (&CacheableBlock{Block: b}).Size()
if i < len(blocks)/2 {
size += sz
}
Expand Down

0 comments on commit 1224cf6

Please sign in to comment.