github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/storage/badger/operation/heights.go (about) 1 package operation 2 3 import ( 4 "github.com/dgraph-io/badger/v2" 5 ) 6 7 func InsertRootHeight(height uint64) func(*badger.Txn) error { 8 return insert(makePrefix(codeFinalizedRootHeight), height) 9 } 10 11 func RetrieveRootHeight(height *uint64) func(*badger.Txn) error { 12 return retrieve(makePrefix(codeFinalizedRootHeight), height) 13 } 14 15 func InsertSealedRootHeight(height uint64) func(*badger.Txn) error { 16 return insert(makePrefix(codeSealedRootHeight), height) 17 } 18 19 func RetrieveSealedRootHeight(height *uint64) func(*badger.Txn) error { 20 return retrieve(makePrefix(codeSealedRootHeight), height) 21 } 22 23 func InsertFinalizedHeight(height uint64) func(*badger.Txn) error { 24 return insert(makePrefix(codeFinalizedHeight), height) 25 } 26 27 func UpdateFinalizedHeight(height uint64) func(*badger.Txn) error { 28 return update(makePrefix(codeFinalizedHeight), height) 29 } 30 31 func RetrieveFinalizedHeight(height *uint64) func(*badger.Txn) error { 32 return retrieve(makePrefix(codeFinalizedHeight), height) 33 } 34 35 func InsertSealedHeight(height uint64) func(*badger.Txn) error { 36 return insert(makePrefix(codeSealedHeight), height) 37 } 38 39 func UpdateSealedHeight(height uint64) func(*badger.Txn) error { 40 return update(makePrefix(codeSealedHeight), height) 41 } 42 43 func RetrieveSealedHeight(height *uint64) func(*badger.Txn) error { 44 return retrieve(makePrefix(codeSealedHeight), height) 45 } 46 47 // InsertEpochFirstHeight inserts the height of the first block in the given epoch. 48 // The first block of an epoch E is the finalized block with view >= E.FirstView. 49 // Although we don't store the final height of an epoch, it can be inferred from this index. 50 // Returns storage.ErrAlreadyExists if the height has already been indexed. 51 func InsertEpochFirstHeight(epoch, height uint64) func(*badger.Txn) error { 52 return insert(makePrefix(codeEpochFirstHeight, epoch), height) 53 } 54 55 // RetrieveEpochFirstHeight retrieves the height of the first block in the given epoch. 56 // Returns storage.ErrNotFound if the first block of the epoch has not yet been finalized. 57 func RetrieveEpochFirstHeight(epoch uint64, height *uint64) func(*badger.Txn) error { 58 return retrieve(makePrefix(codeEpochFirstHeight, epoch), height) 59 } 60 61 // RetrieveEpochLastHeight retrieves the height of the last block in the given epoch. 62 // It's a more readable, but equivalent query to RetrieveEpochFirstHeight when interested in the last height of an epoch. 63 // Returns storage.ErrNotFound if the first block of the epoch has not yet been finalized. 64 func RetrieveEpochLastHeight(epoch uint64, height *uint64) func(*badger.Txn) error { 65 var nextEpochFirstHeight uint64 66 return func(tx *badger.Txn) error { 67 if err := retrieve(makePrefix(codeEpochFirstHeight, epoch+1), &nextEpochFirstHeight)(tx); err != nil { 68 return err 69 } 70 *height = nextEpochFirstHeight - 1 71 return nil 72 } 73 } 74 75 // InsertLastCompleteBlockHeightIfNotExists inserts the last full block height if it is not already set. 76 // Calling this function multiple times is a no-op and returns no expected errors. 77 func InsertLastCompleteBlockHeightIfNotExists(height uint64) func(*badger.Txn) error { 78 return SkipDuplicates(InsertLastCompleteBlockHeight(height)) 79 } 80 81 func InsertLastCompleteBlockHeight(height uint64) func(*badger.Txn) error { 82 return insert(makePrefix(codeLastCompleteBlockHeight), height) 83 } 84 85 func UpdateLastCompleteBlockHeight(height uint64) func(*badger.Txn) error { 86 return update(makePrefix(codeLastCompleteBlockHeight), height) 87 } 88 89 func RetrieveLastCompleteBlockHeight(height *uint64) func(*badger.Txn) error { 90 return retrieve(makePrefix(codeLastCompleteBlockHeight), height) 91 }