github.com/onflow/flow-go@v0.33.17/storage/badger/operation/headers.go (about)

     1  // (c) 2019 Dapper Labs - ALL RIGHTS RESERVED
     2  
     3  package operation
     4  
     5  import (
     6  	"github.com/dgraph-io/badger/v2"
     7  
     8  	"github.com/onflow/flow-go/model/flow"
     9  )
    10  
    11  func InsertHeader(headerID flow.Identifier, header *flow.Header) func(*badger.Txn) error {
    12  	return insert(makePrefix(codeHeader, headerID), header)
    13  }
    14  
    15  func RetrieveHeader(blockID flow.Identifier, header *flow.Header) func(*badger.Txn) error {
    16  	return retrieve(makePrefix(codeHeader, blockID), header)
    17  }
    18  
    19  // IndexBlockHeight indexes the height of a block. It should only be called on
    20  // finalized blocks.
    21  func IndexBlockHeight(height uint64, blockID flow.Identifier) func(*badger.Txn) error {
    22  	return insert(makePrefix(codeHeightToBlock, height), blockID)
    23  }
    24  
    25  // LookupBlockHeight retrieves finalized blocks by height.
    26  func LookupBlockHeight(height uint64, blockID *flow.Identifier) func(*badger.Txn) error {
    27  	return retrieve(makePrefix(codeHeightToBlock, height), blockID)
    28  }
    29  
    30  // BlockExists checks whether the block exists in the database.
    31  // No errors are expected during normal operation.
    32  func BlockExists(blockID flow.Identifier, blockExists *bool) func(*badger.Txn) error {
    33  	return exists(makePrefix(codeHeader, blockID), blockExists)
    34  }
    35  
    36  func InsertExecutedBlock(blockID flow.Identifier) func(*badger.Txn) error {
    37  	return insert(makePrefix(codeExecutedBlock), blockID)
    38  }
    39  
    40  func UpdateExecutedBlock(blockID flow.Identifier) func(*badger.Txn) error {
    41  	return update(makePrefix(codeExecutedBlock), blockID)
    42  }
    43  
    44  func RetrieveExecutedBlock(blockID *flow.Identifier) func(*badger.Txn) error {
    45  	return retrieve(makePrefix(codeExecutedBlock), blockID)
    46  }
    47  
    48  // IndexCollectionBlock indexes a block by a collection within that block.
    49  func IndexCollectionBlock(collID flow.Identifier, blockID flow.Identifier) func(*badger.Txn) error {
    50  	return insert(makePrefix(codeCollectionBlock, collID), blockID)
    51  }
    52  
    53  // LookupCollectionBlock looks up a block by a collection within that block.
    54  func LookupCollectionBlock(collID flow.Identifier, blockID *flow.Identifier) func(*badger.Txn) error {
    55  	return retrieve(makePrefix(codeCollectionBlock, collID), blockID)
    56  }
    57  
    58  // FindHeaders iterates through all headers, calling `filter` on each, and adding
    59  // them to the `found` slice if `filter` returned true
    60  func FindHeaders(filter func(header *flow.Header) bool, found *[]flow.Header) func(*badger.Txn) error {
    61  	return traverse(makePrefix(codeHeader), func() (checkFunc, createFunc, handleFunc) {
    62  		check := func(key []byte) bool {
    63  			return true
    64  		}
    65  		var val flow.Header
    66  		create := func() interface{} {
    67  			return &val
    68  		}
    69  		handle := func() error {
    70  			if filter(&val) {
    71  				*found = append(*found, val)
    72  			}
    73  			return nil
    74  		}
    75  		return check, create, handle
    76  	})
    77  }