github.com/koko1123/flow-go-1@v0.29.6/engine/protocol/handler.go (about) 1 package protocol 2 3 import ( 4 "context" 5 6 "github.com/onflow/flow/protobuf/go/flow/access" 7 "github.com/onflow/flow/protobuf/go/flow/entities" 8 9 "github.com/koko1123/flow-go-1/consensus/hotstuff" 10 "github.com/koko1123/flow-go-1/consensus/hotstuff/signature" 11 "github.com/koko1123/flow-go-1/engine/common/rpc/convert" 12 "github.com/koko1123/flow-go-1/model/flow" 13 ) 14 15 type Handler struct { 16 api API 17 signerIndicesDecoder hotstuff.BlockSignerDecoder 18 } 19 20 // HandlerOption is used to hand over optional constructor parameters 21 type HandlerOption func(*Handler) 22 23 func NewHandler(api API, options ...HandlerOption) *Handler { 24 h := &Handler{ 25 api: api, 26 signerIndicesDecoder: &signature.NoopBlockSignerDecoder{}, 27 } 28 for _, opt := range options { 29 opt(h) 30 } 31 return h 32 } 33 34 func WithBlockSignerDecoder(signerIndicesDecoder hotstuff.BlockSignerDecoder) func(*Handler) { 35 return func(handler *Handler) { 36 handler.signerIndicesDecoder = signerIndicesDecoder 37 } 38 } 39 40 func (h *Handler) GetNetworkParameters( 41 ctx context.Context, 42 _ *access.GetNetworkParametersRequest, 43 ) (*access.GetNetworkParametersResponse, error) { 44 params := h.api.GetNetworkParameters(ctx) 45 46 return &access.GetNetworkParametersResponse{ 47 ChainId: string(params.ChainID), 48 }, nil 49 } 50 51 // GetLatestProtocolStateSnapshot returns the latest serializable Snapshot 52 func (h *Handler) GetLatestProtocolStateSnapshot(ctx context.Context, req *access.GetLatestProtocolStateSnapshotRequest) (*access.ProtocolStateSnapshotResponse, error) { 53 snapshot, err := h.api.GetLatestProtocolStateSnapshot(ctx) 54 if err != nil { 55 return nil, err 56 } 57 58 return &access.ProtocolStateSnapshotResponse{ 59 SerializedSnapshot: snapshot, 60 }, nil 61 } 62 63 // GetLatestBlockHeader gets the latest sealed block header. 64 func (h *Handler) GetLatestBlockHeader( 65 ctx context.Context, 66 req *access.GetLatestBlockHeaderRequest, 67 ) (*access.BlockHeaderResponse, error) { 68 header, err := h.api.GetLatestBlockHeader(ctx, req.GetIsSealed()) 69 if err != nil { 70 return nil, err 71 } 72 return h.blockHeaderResponse(header) 73 } 74 75 // GetBlockHeaderByHeight gets a block header by height. 76 func (h *Handler) GetBlockHeaderByHeight( 77 ctx context.Context, 78 req *access.GetBlockHeaderByHeightRequest, 79 ) (*access.BlockHeaderResponse, error) { 80 header, err := h.api.GetBlockHeaderByHeight(ctx, req.GetHeight()) 81 if err != nil { 82 return nil, err 83 } 84 return h.blockHeaderResponse(header) 85 } 86 87 // GetBlockHeaderByID gets a block header by ID. 88 func (h *Handler) GetBlockHeaderByID( 89 ctx context.Context, 90 req *access.GetBlockHeaderByIDRequest, 91 ) (*access.BlockHeaderResponse, error) { 92 id, err := convert.BlockID(req.GetId()) 93 if err != nil { 94 return nil, err 95 } 96 header, err := h.api.GetBlockHeaderByID(ctx, id) 97 if err != nil { 98 return nil, err 99 } 100 return h.blockHeaderResponse(header) 101 } 102 103 // GetLatestBlock gets the latest sealed block. 104 func (h *Handler) GetLatestBlock( 105 ctx context.Context, 106 req *access.GetLatestBlockRequest, 107 ) (*access.BlockResponse, error) { 108 block, err := h.api.GetLatestBlock(ctx, req.GetIsSealed()) 109 if err != nil { 110 return nil, err 111 } 112 return h.blockResponse(block, req.GetFullBlockResponse()) 113 } 114 115 // GetBlockByHeight gets a block by height. 116 func (h *Handler) GetBlockByHeight( 117 ctx context.Context, 118 req *access.GetBlockByHeightRequest, 119 ) (*access.BlockResponse, error) { 120 block, err := h.api.GetBlockByHeight(ctx, req.GetHeight()) 121 if err != nil { 122 return nil, err 123 } 124 return h.blockResponse(block, req.GetFullBlockResponse()) 125 } 126 127 // GetBlockByID gets a block by ID. 128 func (h *Handler) GetBlockByID( 129 ctx context.Context, 130 req *access.GetBlockByIDRequest, 131 ) (*access.BlockResponse, error) { 132 id, err := convert.BlockID(req.GetId()) 133 if err != nil { 134 return nil, err 135 } 136 block, err := h.api.GetBlockByID(ctx, id) 137 if err != nil { 138 return nil, err 139 } 140 return h.blockResponse(block, req.GetFullBlockResponse()) 141 } 142 143 func (h *Handler) blockResponse(block *flow.Block, fullResponse bool) (*access.BlockResponse, error) { 144 signerIDs, err := h.signerIndicesDecoder.DecodeSignerIDs(block.Header) 145 if err != nil { 146 return nil, err 147 } 148 149 var msg *entities.Block 150 if fullResponse { 151 msg, err = convert.BlockToMessage(block, signerIDs) 152 if err != nil { 153 return nil, err 154 } 155 } else { 156 msg = convert.BlockToMessageLight(block) 157 } 158 return &access.BlockResponse{ 159 Block: msg, 160 }, nil 161 } 162 163 func (h *Handler) blockHeaderResponse(header *flow.Header) (*access.BlockHeaderResponse, error) { 164 signerIDs, err := h.signerIndicesDecoder.DecodeSignerIDs(header) 165 if err != nil { 166 return nil, err 167 } 168 169 msg, err := convert.BlockHeaderToMessage(header, signerIDs) 170 if err != nil { 171 return nil, err 172 } 173 174 return &access.BlockHeaderResponse{ 175 Block: msg, 176 }, nil 177 }