github.com/koko1123/flow-go-1@v0.29.6/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/v3" 7 8 "github.com/koko1123/flow-go-1/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 // InsertBlockValidity marks a block as valid or invalid, defined by the consensus algorithm. 31 func InsertBlockValidity(blockID flow.Identifier, valid bool) func(*badger.Txn) error { 32 return insert(makePrefix(codeBlockValidity, blockID), valid) 33 } 34 35 // RetrieveBlockValidity returns a block's validity wrt the consensus algorithm. 36 func RetrieveBlockValidity(blockID flow.Identifier, valid *bool) func(*badger.Txn) error { 37 return retrieve(makePrefix(codeBlockValidity, blockID), valid) 38 } 39 40 func InsertExecutedBlock(blockID flow.Identifier) func(*badger.Txn) error { 41 return insert(makePrefix(codeExecutedBlock), blockID) 42 } 43 44 func UpdateExecutedBlock(blockID flow.Identifier) func(*badger.Txn) error { 45 return update(makePrefix(codeExecutedBlock), blockID) 46 } 47 48 func RetrieveExecutedBlock(blockID *flow.Identifier) func(*badger.Txn) error { 49 return retrieve(makePrefix(codeExecutedBlock), blockID) 50 } 51 52 // IndexCollectionBlock indexes a block by a collection within that block. 53 func IndexCollectionBlock(collID flow.Identifier, blockID flow.Identifier) func(*badger.Txn) error { 54 return insert(makePrefix(codeCollectionBlock, collID), blockID) 55 } 56 57 func IndexBlockIDByChunkID(chunkID, blockID flow.Identifier) func(*badger.Txn) error { 58 return insert(makePrefix(codeIndexBlockByChunkID, chunkID), blockID) 59 } 60 61 // BatchIndexBlockByChunkID indexes blockID by chunkID into a batch 62 func BatchIndexBlockByChunkID(blockID, chunkID flow.Identifier) func(batch *badger.WriteBatch) error { 63 return batchWrite(makePrefix(codeIndexBlockByChunkID, chunkID), blockID) 64 } 65 66 // LookupCollectionBlock looks up a block by a collection within that block. 67 func LookupCollectionBlock(collID flow.Identifier, blockID *flow.Identifier) func(*badger.Txn) error { 68 return retrieve(makePrefix(codeCollectionBlock, collID), blockID) 69 } 70 71 // LookupBlockIDByChunkID looks up a block by a collection within that block. 72 func LookupBlockIDByChunkID(chunkID flow.Identifier, blockID *flow.Identifier) func(*badger.Txn) error { 73 return retrieve(makePrefix(codeIndexBlockByChunkID, chunkID), blockID) 74 } 75 76 // RemoveBlockIDByChunkID removes chunkID-blockID index by chunkID 77 func RemoveBlockIDByChunkID(chunkID flow.Identifier) func(*badger.Txn) error { 78 return remove(makePrefix(codeIndexBlockByChunkID, chunkID)) 79 } 80 81 // BatchRemoveBlockIDByChunkID removes chunkID-to-blockID index entries keyed by a chunkID in a provided batch. 82 // No errors are expected during normal operation, even if no entries are matched. 83 // If Badger unexpectedly fails to process the request, the error is wrapped in a generic error and returned. 84 func BatchRemoveBlockIDByChunkID(chunkID flow.Identifier) func(batch *badger.WriteBatch) error { 85 return batchRemove(makePrefix(codeIndexBlockByChunkID, chunkID)) 86 } 87 88 // FindHeaders iterates through all headers, calling `filter` on each, and adding 89 // them to the `found` slice if `filter` returned true 90 func FindHeaders(filter func(header *flow.Header) bool, found *[]flow.Header) func(*badger.Txn) error { 91 return traverse(makePrefix(codeHeader), func() (checkFunc, createFunc, handleFunc) { 92 check := func(key []byte) bool { 93 return true 94 } 95 var val flow.Header 96 create := func() interface{} { 97 return &val 98 } 99 handle := func() error { 100 if filter(&val) { 101 *found = append(*found, val) 102 } 103 return nil 104 } 105 return check, create, handle 106 }) 107 }