github.com/koko1123/flow-go-1@v0.29.6/engine/access/rpc/backend/backend_block_headers.go (about) 1 package backend 2 3 import ( 4 "context" 5 "fmt" 6 7 "github.com/koko1123/flow-go-1/engine/common/rpc" 8 "github.com/koko1123/flow-go-1/model/flow" 9 "github.com/koko1123/flow-go-1/state/protocol" 10 "github.com/koko1123/flow-go-1/storage" 11 ) 12 13 type backendBlockHeaders struct { 14 headers storage.Headers 15 state protocol.State 16 } 17 18 func (b *backendBlockHeaders) GetLatestBlockHeader(_ context.Context, isSealed bool) (*flow.Header, flow.BlockStatus, error) { 19 var header *flow.Header 20 var err error 21 22 if isSealed { 23 // get the latest seal header from storage 24 header, err = b.state.Sealed().Head() 25 } else { 26 // get the finalized header from state 27 header, err = b.state.Final().Head() 28 } 29 30 if err != nil { 31 err = rpc.ConvertStorageError(err) 32 return nil, flow.BlockStatusUnknown, err 33 } 34 35 status, err := b.getBlockStatus(header) 36 if err != nil { 37 return nil, status, err 38 } 39 return header, status, nil 40 } 41 42 func (b *backendBlockHeaders) GetBlockHeaderByID(_ context.Context, id flow.Identifier) (*flow.Header, flow.BlockStatus, error) { 43 header, err := b.headers.ByBlockID(id) 44 if err != nil { 45 err = rpc.ConvertStorageError(err) 46 return nil, flow.BlockStatusUnknown, err 47 } 48 49 status, err := b.getBlockStatus(header) 50 if err != nil { 51 return nil, status, err 52 } 53 return header, status, nil 54 } 55 56 func (b *backendBlockHeaders) GetBlockHeaderByHeight(_ context.Context, height uint64) (*flow.Header, flow.BlockStatus, error) { 57 header, err := b.headers.ByHeight(height) 58 if err != nil { 59 err = rpc.ConvertStorageError(err) 60 return nil, flow.BlockStatusUnknown, err 61 } 62 63 status, err := b.getBlockStatus(header) 64 if err != nil { 65 return nil, status, err 66 } 67 return header, status, nil 68 } 69 70 func (b *backendBlockHeaders) getBlockStatus(header *flow.Header) (flow.BlockStatus, error) { 71 sealed, err := b.state.Sealed().Head() 72 if err != nil { 73 return flow.BlockStatusUnknown, fmt.Errorf("failed to find latest sealed header: %w", err) 74 } 75 76 if header.Height > sealed.Height { 77 return flow.BlockStatusFinalized, nil 78 } 79 return flow.BlockStatusSealed, nil 80 }