github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/storage/badger/procedure/executed.go (about) 1 package procedure 2 3 import ( 4 "errors" 5 "fmt" 6 7 "github.com/dgraph-io/badger/v2" 8 9 "github.com/onflow/flow-go/model/flow" 10 "github.com/onflow/flow-go/storage" 11 "github.com/onflow/flow-go/storage/badger/operation" 12 ) 13 14 // UpdateHighestExecutedBlockIfHigher updates the latest executed block to be the input block 15 // if the input block has a greater height than the currently stored latest executed block. 16 // The executed block index must have been initialized before calling this function. 17 // Returns storage.ErrNotFound if the input block does not exist in storage. 18 func UpdateHighestExecutedBlockIfHigher(header *flow.Header) func(txn *badger.Txn) error { 19 return func(txn *badger.Txn) error { 20 var blockID flow.Identifier 21 err := operation.RetrieveExecutedBlock(&blockID)(txn) 22 if err != nil { 23 return fmt.Errorf("cannot lookup executed block: %w", err) 24 } 25 26 var highest flow.Header 27 err = operation.RetrieveHeader(blockID, &highest)(txn) 28 if err != nil { 29 return fmt.Errorf("cannot retrieve executed header: %w", err) 30 } 31 32 if header.Height <= highest.Height { 33 return nil 34 } 35 err = operation.UpdateExecutedBlock(header.ID())(txn) 36 if err != nil { 37 return fmt.Errorf("cannot update highest executed block: %w", err) 38 } 39 40 return nil 41 } 42 } 43 44 // GetHighestExecutedBlock retrieves the height and ID of the latest block executed by this node. 45 // Returns storage.ErrNotFound if no latest executed block has been stored. 46 func GetHighestExecutedBlock(height *uint64, blockID *flow.Identifier) func(tx *badger.Txn) error { 47 return func(tx *badger.Txn) error { 48 var highest flow.Header 49 err := operation.RetrieveExecutedBlock(blockID)(tx) 50 if err != nil { 51 return fmt.Errorf("could not lookup executed block %v: %w", blockID, err) 52 } 53 err = operation.RetrieveHeader(*blockID, &highest)(tx) 54 if err != nil { 55 if errors.Is(err, storage.ErrNotFound) { 56 return fmt.Errorf("unexpected: latest executed block does not exist in storage: %s", err.Error()) 57 } 58 return fmt.Errorf("could not retrieve executed header %v: %w", blockID, err) 59 } 60 *height = highest.Height 61 return nil 62 } 63 }