github.com/koko1123/flow-go-1@v0.29.6/access/legacy/handler.go (about) 1 package handler 2 3 import ( 4 "context" 5 6 accessproto "github.com/onflow/flow/protobuf/go/flow/legacy/access" 7 entitiesproto "github.com/onflow/flow/protobuf/go/flow/legacy/entities" 8 "google.golang.org/grpc/codes" 9 "google.golang.org/grpc/status" 10 11 "github.com/koko1123/flow-go-1/access" 12 "github.com/koko1123/flow-go-1/access/legacy/convert" 13 "github.com/koko1123/flow-go-1/model/flow" 14 ) 15 16 type Handler struct { 17 api access.API 18 chain flow.Chain 19 } 20 21 func NewHandler(api access.API, chain flow.Chain) *Handler { 22 return &Handler{ 23 api: api, 24 chain: chain, 25 } 26 } 27 28 // Ping the Access API server for a response. 29 func (h *Handler) Ping(context.Context, *accessproto.PingRequest) (*accessproto.PingResponse, error) { 30 return &accessproto.PingResponse{}, nil 31 } 32 33 func (h *Handler) GetNetworkParameters( 34 context.Context, 35 *accessproto.GetNetworkParametersRequest, 36 ) (*accessproto.GetNetworkParametersResponse, error) { 37 panic("implement me") 38 } 39 40 // SendTransaction submits a transaction to the network. 41 func (h *Handler) SendTransaction( 42 ctx context.Context, 43 req *accessproto.SendTransactionRequest, 44 ) (*accessproto.SendTransactionResponse, error) { 45 txMsg := req.GetTransaction() 46 47 tx, err := convert.MessageToTransaction(txMsg, h.chain) 48 if err != nil { 49 return nil, status.Error(codes.InvalidArgument, err.Error()) 50 } 51 52 err = h.api.SendTransaction(ctx, &tx) 53 if err != nil { 54 return nil, err 55 } 56 57 txID := tx.ID() 58 59 return &accessproto.SendTransactionResponse{ 60 Id: txID[:], 61 }, nil 62 } 63 64 // GetLatestBlockHeader gets the latest sealed block header. 65 func (h *Handler) GetLatestBlockHeader( 66 ctx context.Context, 67 req *accessproto.GetLatestBlockHeaderRequest, 68 ) (*accessproto.BlockHeaderResponse, error) { 69 header, _, err := h.api.GetLatestBlockHeader(ctx, req.GetIsSealed()) 70 if err != nil { 71 return nil, err 72 } 73 74 return blockHeaderResponse(header) 75 } 76 77 // GetBlockHeaderByHeight gets a block header by height. 78 func (h *Handler) GetBlockHeaderByHeight( 79 ctx context.Context, 80 req *accessproto.GetBlockHeaderByHeightRequest, 81 ) (*accessproto.BlockHeaderResponse, error) { 82 header, _, err := h.api.GetBlockHeaderByHeight(ctx, req.GetHeight()) 83 if err != nil { 84 return nil, err 85 } 86 87 return blockHeaderResponse(header) 88 } 89 90 // GetBlockHeaderByID gets a block header by ID. 91 func (h *Handler) GetBlockHeaderByID( 92 ctx context.Context, 93 req *accessproto.GetBlockHeaderByIDRequest, 94 ) (*accessproto.BlockHeaderResponse, error) { 95 blockID := convert.MessageToIdentifier(req.GetId()) 96 97 header, _, err := h.api.GetBlockHeaderByID(ctx, blockID) 98 if err != nil { 99 return nil, err 100 } 101 102 return blockHeaderResponse(header) 103 } 104 105 // GetLatestBlock gets the latest sealed block. 106 func (h *Handler) GetLatestBlock( 107 ctx context.Context, 108 req *accessproto.GetLatestBlockRequest, 109 ) (*accessproto.BlockResponse, error) { 110 block, _, err := h.api.GetLatestBlock(ctx, req.GetIsSealed()) 111 if err != nil { 112 return nil, err 113 } 114 115 return blockResponse(block) 116 } 117 118 // GetBlockByHeight gets a block by height. 119 func (h *Handler) GetBlockByHeight( 120 ctx context.Context, 121 req *accessproto.GetBlockByHeightRequest, 122 ) (*accessproto.BlockResponse, error) { 123 block, _, err := h.api.GetBlockByHeight(ctx, req.GetHeight()) 124 if err != nil { 125 return nil, err 126 } 127 128 return blockResponse(block) 129 } 130 131 // GetBlockByHeight gets a block by ID. 132 func (h *Handler) GetBlockByID( 133 ctx context.Context, 134 req *accessproto.GetBlockByIDRequest, 135 ) (*accessproto.BlockResponse, error) { 136 blockID := convert.MessageToIdentifier(req.GetId()) 137 138 block, _, err := h.api.GetBlockByID(ctx, blockID) 139 if err != nil { 140 return nil, err 141 } 142 143 return blockResponse(block) 144 } 145 146 // GetCollectionByID gets a collection by ID. 147 func (h *Handler) GetCollectionByID( 148 ctx context.Context, 149 req *accessproto.GetCollectionByIDRequest, 150 ) (*accessproto.CollectionResponse, error) { 151 id := convert.MessageToIdentifier(req.GetId()) 152 153 col, err := h.api.GetCollectionByID(ctx, id) 154 if err != nil { 155 return nil, err 156 } 157 158 colMsg, err := convert.LightCollectionToMessage(col) 159 if err != nil { 160 return nil, status.Error(codes.Internal, err.Error()) 161 } 162 163 return &accessproto.CollectionResponse{ 164 Collection: colMsg, 165 }, nil 166 } 167 168 // GetTransaction gets a transaction by ID. 169 func (h *Handler) GetTransaction( 170 ctx context.Context, 171 req *accessproto.GetTransactionRequest, 172 ) (*accessproto.TransactionResponse, error) { 173 id := convert.MessageToIdentifier(req.GetId()) 174 175 tx, err := h.api.GetTransaction(ctx, id) 176 if err != nil { 177 return nil, err 178 } 179 180 return &accessproto.TransactionResponse{ 181 Transaction: convert.TransactionToMessage(*tx), 182 }, nil 183 } 184 185 // GetTransactionResult gets a transaction by ID. 186 func (h *Handler) GetTransactionResult( 187 ctx context.Context, 188 req *accessproto.GetTransactionRequest, 189 ) (*accessproto.TransactionResultResponse, error) { 190 id := convert.MessageToIdentifier(req.GetId()) 191 192 result, err := h.api.GetTransactionResult(ctx, id) 193 if err != nil { 194 return nil, err 195 } 196 197 return convert.TransactionResultToMessage(*result), nil 198 } 199 200 // GetAccount returns an account by address at the latest sealed block. 201 func (h *Handler) GetAccount( 202 ctx context.Context, 203 req *accessproto.GetAccountRequest, 204 ) (*accessproto.GetAccountResponse, error) { 205 address := flow.BytesToAddress(req.GetAddress()) 206 207 account, err := h.api.GetAccount(ctx, address) 208 if err != nil { 209 return nil, err 210 } 211 212 accountMsg, err := convert.AccountToMessage(account) 213 if err != nil { 214 return nil, status.Error(codes.Internal, err.Error()) 215 } 216 217 return &accessproto.GetAccountResponse{ 218 Account: accountMsg, 219 }, nil 220 } 221 222 // GetAccountAtLatestBlock returns an account by address at the latest sealed block. 223 func (h *Handler) GetAccountAtLatestBlock( 224 ctx context.Context, 225 req *accessproto.GetAccountAtLatestBlockRequest, 226 ) (*accessproto.AccountResponse, error) { 227 address := flow.BytesToAddress(req.GetAddress()) 228 229 account, err := h.api.GetAccountAtLatestBlock(ctx, address) 230 if err != nil { 231 return nil, err 232 } 233 234 accountMsg, err := convert.AccountToMessage(account) 235 if err != nil { 236 return nil, status.Error(codes.Internal, err.Error()) 237 } 238 239 return &accessproto.AccountResponse{ 240 Account: accountMsg, 241 }, nil 242 } 243 244 func (h *Handler) GetAccountAtBlockHeight( 245 ctx context.Context, 246 request *accessproto.GetAccountAtBlockHeightRequest, 247 ) (*accessproto.AccountResponse, error) { 248 panic("implement me") 249 } 250 251 // ExecuteScriptAtLatestBlock executes a script at a the latest block 252 func (h *Handler) ExecuteScriptAtLatestBlock( 253 ctx context.Context, 254 req *accessproto.ExecuteScriptAtLatestBlockRequest, 255 ) (*accessproto.ExecuteScriptResponse, error) { 256 script := req.GetScript() 257 arguments := req.GetArguments() 258 259 value, err := h.api.ExecuteScriptAtLatestBlock(ctx, script, arguments) 260 if err != nil { 261 return nil, err 262 } 263 264 return &accessproto.ExecuteScriptResponse{ 265 Value: value, 266 }, nil 267 } 268 269 // ExecuteScriptAtBlockHeight executes a script at a specific block height 270 func (h *Handler) ExecuteScriptAtBlockHeight( 271 ctx context.Context, 272 req *accessproto.ExecuteScriptAtBlockHeightRequest, 273 ) (*accessproto.ExecuteScriptResponse, error) { 274 script := req.GetScript() 275 arguments := req.GetArguments() 276 blockHeight := req.GetBlockHeight() 277 278 value, err := h.api.ExecuteScriptAtBlockHeight(ctx, blockHeight, script, arguments) 279 if err != nil { 280 return nil, err 281 } 282 283 return &accessproto.ExecuteScriptResponse{ 284 Value: value, 285 }, nil 286 } 287 288 // ExecuteScriptAtBlockID executes a script at a specific block ID 289 func (h *Handler) ExecuteScriptAtBlockID( 290 ctx context.Context, 291 req *accessproto.ExecuteScriptAtBlockIDRequest, 292 ) (*accessproto.ExecuteScriptResponse, error) { 293 script := req.GetScript() 294 arguments := req.GetArguments() 295 blockID := convert.MessageToIdentifier(req.GetBlockId()) 296 297 value, err := h.api.ExecuteScriptAtBlockID(ctx, blockID, script, arguments) 298 if err != nil { 299 return nil, err 300 } 301 302 return &accessproto.ExecuteScriptResponse{ 303 Value: value, 304 }, nil 305 } 306 307 // GetEventsForHeightRange returns events matching a query. 308 func (h *Handler) GetEventsForHeightRange( 309 ctx context.Context, 310 req *accessproto.GetEventsForHeightRangeRequest, 311 ) (*accessproto.EventsResponse, error) { 312 eventType := req.GetType() 313 startHeight := req.GetStartHeight() 314 endHeight := req.GetEndHeight() 315 316 results, err := h.api.GetEventsForHeightRange(ctx, eventType, startHeight, endHeight) 317 if err != nil { 318 return nil, err 319 } 320 321 return &accessproto.EventsResponse{ 322 Results: blockEventsToMessages(results), 323 }, nil 324 } 325 326 // GetEventsForBlockIDs returns events matching a set of block IDs. 327 func (h *Handler) GetEventsForBlockIDs( 328 ctx context.Context, 329 req *accessproto.GetEventsForBlockIDsRequest, 330 ) (*accessproto.EventsResponse, error) { 331 eventType := req.GetType() 332 blockIDs := convert.MessagesToIdentifiers(req.GetBlockIds()) 333 334 results, err := h.api.GetEventsForBlockIDs(ctx, eventType, blockIDs) 335 if err != nil { 336 return nil, err 337 } 338 339 return &accessproto.EventsResponse{ 340 Results: blockEventsToMessages(results), 341 }, nil 342 } 343 344 func blockResponse(block *flow.Block) (*accessproto.BlockResponse, error) { 345 msg, err := convert.BlockToMessage(block) 346 if err != nil { 347 return nil, err 348 } 349 350 return &accessproto.BlockResponse{ 351 Block: msg, 352 }, nil 353 } 354 355 func blockHeaderResponse(header *flow.Header) (*accessproto.BlockHeaderResponse, error) { 356 msg, err := convert.BlockHeaderToMessage(header) 357 if err != nil { 358 return nil, err 359 } 360 361 return &accessproto.BlockHeaderResponse{ 362 Block: msg, 363 }, nil 364 } 365 366 func blockEventsToMessages(blocks []flow.BlockEvents) []*accessproto.EventsResponse_Result { 367 results := make([]*accessproto.EventsResponse_Result, len(blocks)) 368 369 for i, block := range blocks { 370 results[i] = blockEventsToMessage(block) 371 } 372 373 return results 374 } 375 376 func blockEventsToMessage(block flow.BlockEvents) *accessproto.EventsResponse_Result { 377 eventMessages := make([]*entitiesproto.Event, len(block.Events)) 378 for i, event := range block.Events { 379 eventMessages[i] = convert.EventToMessage(event) 380 } 381 382 return &accessproto.EventsResponse_Result{ 383 BlockId: block.BlockID[:], 384 BlockHeight: block.BlockHeight, 385 Events: eventMessages, 386 } 387 }