github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/storage/badger/operation/headers.go (about)

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