filterstore.go raw

   1  // Package filterstore holds BIP158 filters keyed by the block they describe.
   2  //
   3  // The key is the block hash and nothing else, because the filter of a block is
   4  // a property of the block: two chains that carry the same block carry the same
   5  // filter, so one copy serves both. That is the reason the filter store is not
   6  // per chain and the header stores are.
   7  //
   8  // The bytes are the BlobStore's: one data file per 512 MB with a length-prefixed
   9  // record, a per-file index, and a tail record that names the newest append that
  10  // was durable, so a crash mid-append is repaired and nothing else is. The store
  11  // does not check a filter against the block: a filter's own hash is committed by
  12  // the chain's filter-header fold, and the builder checks it there before it
  13  // writes one.
  14  package filterstore
  15  
  16  import (
  17  	"path/filepath"
  18  
  19  	"git.smesh.lol/nostrino/pkg/crypto/chainhash"
  20  
  21  	"git.smesh.lol/btcd-lite/internal/store"
  22  )
  23  
  24  // Dir is where a data directory keeps its filters.
  25  func Dir(data string) string { return filepath.Join(data, "filters", "by-block") }
  26  
  27  // Store is the filter store of one data directory.
  28  type Store struct {
  29  	*store.BlobStore
  30  }
  31  
  32  // Open opens or creates the store in dir.
  33  func Open(dir string) (*Store, error) {
  34  	b, err := store.NewBlobStore(dir, "flt", nil)
  35  	if err != nil {
  36  		return nil, err
  37  	}
  38  	return &Store{BlobStore: b}, nil
  39  }
  40  
  41  // Put files a filter under its block's hash.
  42  func (s *Store) Put(blockHash chainhash.Hash, filter []byte) error {
  43  	return s.BlobStore.Put(blockHash, filter)
  44  }
  45  
  46  // Get returns the filter for a block.
  47  func (s *Store) Get(blockHash chainhash.Hash) ([]byte, error) {
  48  	return s.BlobStore.Get(blockHash)
  49  }
  50