blockstore.go raw

   1  package store
   2  
   3  import (
   4  	"fmt"
   5  
   6  	"git.smesh.lol/nostrino/pkg/btcwire"
   7  	"git.smesh.lol/nostrino/pkg/crypto/chainhash"
   8  )
   9  
  10  // The block store's error names, which are the blob store's: a caller that asks
  11  // for a block reads the same errors it always did.
  12  var (
  13  	ErrBlockNotFound    = ErrNotFound
  14  	ErrBlockExists      = ErrExists
  15  	ErrCorruptBlockFile = ErrCorruptFile
  16  )
  17  
  18  // BlockStore is a BlobStore whose keys are block hashes: a body is filed under
  19  // the hash its own header commits to, and a body that does not commit to the
  20  // name it arrives under is refused at write time and again at open.
  21  //
  22  // It knows nothing about chains or heights. The same block has one height on
  23  // each chain that carries it, so a height belongs to a header store and a body
  24  // belongs to its hash.
  25  type BlockStore struct{ *BlobStore }
  26  
  27  // NewBlockStore opens or creates a block store in dir, verifying that every
  28  // block on disk commits to the hash it is filed under.
  29  func NewBlockStore(dir string) (*BlockStore, error) {
  30  	return newBlockStore(dir, false)
  31  }
  32  
  33  // NewBlockStoreTrusting is NewBlockStore without the open-time pass that reads
  34  // every block back. It is for an instance that is the store's only writer, on a
  35  // dataset big enough that the pass costs hours; a read still checks the length
  36  // it is handed.
  37  func NewBlockStoreTrusting(dir string) (*BlockStore, error) {
  38  	return newBlockStore(dir, true)
  39  }
  40  
  41  func newBlockStore(dir string, trustIndex bool) (*BlockStore, error) {
  42  	b, err := newBlobStore(dir, "blk", func(key chainhash.Hash, raw []byte) bool {
  43  		return len(raw) >= btcwire.BlockHeaderSize && blockHash(raw) == key
  44  	}, trustIndex)
  45  	if err != nil {
  46  		return nil, err
  47  	}
  48  	return &BlockStore{b}, nil
  49  }
  50  
  51  // PutBlock appends a raw block under the hash it commits to.
  52  func (s *BlockStore) PutBlock(hash chainhash.Hash, raw []byte) error {
  53  	if len(raw) < btcwire.BlockHeaderSize {
  54  		return fmt.Errorf("blockstore: block of %d bytes", len(raw))
  55  	}
  56  	return s.Put(hash, raw)
  57  }
  58  
  59  // GetBlock returns a stored block's bytes.
  60  func (s *BlockStore) GetBlock(hash chainhash.Hash) ([]byte, error) { return s.Get(hash) }
  61  
  62  // HasBlock reports whether a block is stored.
  63  func (s *BlockStore) HasBlock(hash chainhash.Hash) bool { return s.Has(hash) }
  64  
  65  // blockHash is the hash a block's own header commits to: double SHA-256 of the
  66  // 80-byte header, or the fork's construction when the header carries the v2
  67  // flag. It is what a body is filed under, so the store cannot be a store of
  68  // Bitcoin-form blocks only.
  69  func blockHash(raw []byte) chainhash.Hash {
  70  	if len(raw) >= btcwire.BlockHeaderV2Size && getUint32LE(raw[0:4])&btcwire.HeaderV2Flag != 0 {
  71  		var h btcwire.BlockHeader
  72  		if err := h.DeserializeV2(raw[:btcwire.BlockHeaderV2Size]); err == nil {
  73  			return h.BlockHash()
  74  		}
  75  	}
  76  	return chainhash.DoubleHashH(raw[:80])
  77  }
  78