@@ -3,59 +3,28 @@ package controllers
33import (
44 "blob-service/dto"
55 "blob-service/internal"
6- pbbl "blob-service/pb/pinax/ethereum/blobs/v1 "
6+ "blob-service/services "
77 "context"
8- "encoding/binary"
9- "fmt"
108 "strconv"
119 "strings"
1210 "time"
1311
14- pbkv "github.com/streamingfast/substreams-sink-kv/pb/substreams/sink/kv/v1"
15-
16- "github.com/eosnationftw/eosn-base-api/helper"
1712 "github.com/eosnationftw/eosn-base-api/response"
1813 "github.com/gin-gonic/gin"
19- "github.com/golang/protobuf/proto"
2014 "google.golang.org/grpc/codes"
2115 "google.golang.org/grpc/status"
2216)
2317
24- const (
25- NOT_FOUND_SLOT = "slot_not_found" // no slot found
26- INVALID_SLOT = "invalid_slot" // invalid slot
27- )
28-
2918type BlobsController struct {
30- sinkClient pbkv. KvClient
19+ blobsService * services. BlobsService
3120}
3221
33- func NewBlobsController (sinkClient pbkv. KvClient ) * BlobsController {
34- return & BlobsController {sinkClient : sinkClient }
22+ func NewBlobsController (blobsService * services. BlobsService ) * BlobsController {
23+ return & BlobsController {blobsService : blobsService }
3524}
3625
3726type blobsBySlotRetType []* dto.Blob
3827
39- func (bc * BlobsController ) parseBlockId (ctx context.Context , block_id string ) (uint64 , error ) {
40- if block_id == "head" {
41- resp , err := bc .sinkClient .Get (ctx , & pbkv.GetRequest {Key : "head" })
42- if err != nil {
43- return 0 , err
44- }
45- return binary .BigEndian .Uint64 (resp .GetValue ()), nil
46- }
47-
48- if block_id [:2 ] == "0x" {
49- resp , err := bc .sinkClient .Get (ctx , & pbkv.GetRequest {Key : "block_root:" + block_id })
50- if err != nil {
51- return 0 , err
52- }
53- return binary .BigEndian .Uint64 (resp .GetValue ()), nil
54- }
55-
56- return strconv .ParseUint (block_id , 10 , 64 )
57- }
58-
5928// BlobsByBlockId
6029//
6130// @Summary Get Blobs by block id
@@ -64,61 +33,47 @@ func (bc *BlobsController) parseBlockId(ctx context.Context, block_id string) (u
6433// @Param block_id path string true "Block identifier. Can be one of: 'head', slot number, hex encoded blockRoot with 0x prefix"
6534// @Param indices query []string false "Array of indices for blob sidecars to request for in the specified block. Returns all blob sidecars in the block if not specified."
6635// @Success 200 {object} response.ApiDataResponse{data=blobsBySlotRetType} "Successful response"
67- // @Failure 400 {object} response.ApiErrorResponse "invalid_slot" "Invalid block id
36+ // @Failure 400 {object} response.ApiErrorResponse "invalid_slot" "Invalid block id"
6837// @Failure 404 {object} response.ApiErrorResponse "slot_not_found" "Slot not found"
6938// @Failure 500 {object} response.ApiErrorResponse
7039// @Router /eth/v1/beacon/blob_sidecars/{block_id} [get]
7140func (bc * BlobsController ) BlobsByBlockId (c * gin.Context ) {
7241
7342 blockId := c .Param ("block_id" )
74- indices := strings .Split (c .Query ("indices" ), "," )
75- if len (indices ) == 1 && indices [0 ] == "" {
76- indices = []string {}
77- }
78-
79- ctx , cancel := context .WithTimeout (c .Request .Context (), 5 * time .Second )
80- defer cancel ()
81-
82- slotNum , err := bc .parseBlockId (ctx , blockId )
83- if err != nil {
84- if ctx .Err () == context .DeadlineExceeded {
85- helper .ReportPublicErrorAndAbort (c , response .GatewayTimeout , err )
86- return
43+ indices := []uint32 {}
44+ for _ , str := range strings .Split (c .Query ("indices" ), "," ) {
45+ if str == "" {
46+ continue
8747 }
88- st , ok := status . FromError ( err )
89- if ok && st . Code () == codes . NotFound {
90- helper . ReportPublicErrorAndAbort (c , response . NewApiErrorNotFound ( NOT_FOUND_SLOT ), err )
48+ i , err := strconv . ParseUint ( str , 10 , 32 )
49+ if err != nil {
50+ internal . WriteErrorResponse (c , internal . ErrInvalidIndex )
9151 return
9252 }
93- helper .ReportPublicErrorAndAbort (c , response .BadGateway , err )
94- return
53+ indices = append (indices , uint32 (i ))
9554 }
9655
97- resp , err := bc .sinkClient .Get (ctx , & pbkv.GetRequest {Key : fmt .Sprintf ("slot:%d" , slotNum )})
56+ ctx , cancel := context .WithTimeout (c .Request .Context (), 5 * time .Second )
57+ defer cancel ()
58+
59+ slot , err := bc .blobsService .GetSlotByBlockId (ctx , blockId )
9860 if err != nil {
9961 if ctx .Err () == context .DeadlineExceeded {
100- helper . ReportPublicErrorAndAbort (c , response . GatewayTimeout , err )
62+ internal . WriteErrorResponse (c , internal . ErrSinkTimeout )
10163 return
10264 }
10365 st , ok := status .FromError (err )
10466 if ok && st .Code () == codes .NotFound {
105- helper . ReportPublicErrorAndAbort (c , response . NewApiErrorNotFound ( NOT_FOUND_SLOT ), err )
67+ internal . WriteErrorResponse (c , internal . ErrSlotNotFound )
10668 return
10769 }
108- helper .ReportPublicErrorAndAbort (c , response .BadGateway , err )
109- return
110- }
111-
112- slot := & pbbl.Slot {}
113- err = proto .Unmarshal (resp .GetValue (), slot )
114- if err != nil {
115- helper .ReportPublicErrorAndAbort (c , response .InternalServerError , err )
70+ internal .WriteErrorResponse (c , err )
11671 return
11772 }
11873
11974 resBlobs := []* dto.Blob {}
12075 for _ , blob := range slot .Blobs {
121- if len (indices ) == 0 || internal .Contains (indices , fmt . Sprintf ( "%d" , blob .Index ) ) {
76+ if len (indices ) == 0 || internal .Contains (indices , blob .Index ) {
12277 resBlobs = append (resBlobs , dto .NewBlob (blob , slot ))
12378 }
12479 }
0 commit comments