github.com/koko1123/flow-go-1@v0.29.6/engine/protocol/api.go (about) 1 package protocol 2 3 import ( 4 "context" 5 6 "github.com/koko1123/flow-go-1/access" 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 NetworkAPI interface { 14 GetNetworkParameters(ctx context.Context) access.NetworkParameters 15 GetLatestProtocolStateSnapshot(ctx context.Context) ([]byte, error) 16 } 17 18 type API interface { 19 NetworkAPI 20 GetLatestBlockHeader(ctx context.Context, isSealed bool) (*flow.Header, error) 21 GetBlockHeaderByID(ctx context.Context, id flow.Identifier) (*flow.Header, error) 22 GetBlockHeaderByHeight(ctx context.Context, height uint64) (*flow.Header, error) 23 GetLatestBlock(ctx context.Context, isSealed bool) (*flow.Block, error) 24 GetBlockByID(ctx context.Context, id flow.Identifier) (*flow.Block, error) 25 GetBlockByHeight(ctx context.Context, height uint64) (*flow.Block, error) 26 } 27 28 type backend struct { 29 NetworkAPI 30 blocks storage.Blocks 31 headers storage.Headers 32 state protocol.State 33 } 34 35 func New( 36 state protocol.State, 37 blocks storage.Blocks, 38 headers storage.Headers, 39 network NetworkAPI, 40 ) API { 41 return &backend{ 42 NetworkAPI: network, 43 headers: headers, 44 blocks: blocks, 45 state: state, 46 } 47 } 48 49 func (b *backend) GetLatestBlock(_ context.Context, isSealed bool) (*flow.Block, error) { 50 header, err := b.getLatestHeader(isSealed) 51 if err != nil { 52 err = rpc.ConvertStorageError(err) 53 return nil, err 54 } 55 56 block, err := b.blocks.ByID(header.ID()) 57 if err != nil { 58 err = rpc.ConvertStorageError(err) 59 return nil, err 60 } 61 62 return block, nil 63 } 64 65 func (b *backend) GetBlockByID(_ context.Context, id flow.Identifier) (*flow.Block, error) { 66 block, err := b.blocks.ByID(id) 67 if err != nil { 68 err = rpc.ConvertStorageError(err) 69 return nil, err 70 } 71 72 return block, nil 73 } 74 75 func (b *backend) GetBlockByHeight(_ context.Context, height uint64) (*flow.Block, error) { 76 block, err := b.blocks.ByHeight(height) 77 if err != nil { 78 err = rpc.ConvertStorageError(err) 79 return nil, err 80 } 81 82 return block, nil 83 } 84 85 func (b *backend) GetLatestBlockHeader(_ context.Context, isSealed bool) (*flow.Header, error) { 86 header, err := b.getLatestHeader(isSealed) 87 if err != nil { 88 err = rpc.ConvertStorageError(err) 89 return nil, err 90 } 91 92 return header, nil 93 } 94 95 func (b *backend) GetBlockHeaderByID(_ context.Context, id flow.Identifier) (*flow.Header, error) { 96 header, err := b.headers.ByBlockID(id) 97 if err != nil { 98 err = rpc.ConvertStorageError(err) 99 return nil, err 100 } 101 102 return header, nil 103 } 104 105 func (b *backend) GetBlockHeaderByHeight(_ context.Context, height uint64) (*flow.Header, error) { 106 header, err := b.headers.ByHeight(height) 107 if err != nil { 108 err = rpc.ConvertStorageError(err) 109 return nil, err 110 } 111 112 return header, nil 113 } 114 115 func (b *backend) getLatestHeader(isSealed bool) (*flow.Header, error) { 116 var header *flow.Header 117 var err error 118 119 if isSealed { 120 // get the latest seal header from storage 121 header, err = b.state.Sealed().Head() 122 return header, err 123 } else { 124 // get the finalized header from state 125 header, err = b.state.Final().Head() 126 return header, err 127 } 128 }