Skip to content

Commit

Permalink
refactor protobufs
Browse files Browse the repository at this point in the history
fixes #2
  • Loading branch information
YaroShkvorets committed Feb 28, 2024
1 parent 1028d19 commit 351aa56
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 214 deletions.
26 changes: 13 additions & 13 deletions controllers/blobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
)

const (
NOT_FOUND_BLOBS = "blobs_not_found" // no blobs found
INVALID_SLOT = "invalid_slot" // invalid slot
NOT_FOUND_SLOT = "slot_not_found" // no slot found
INVALID_SLOT = "invalid_slot" // invalid slot
)

type BlobsController struct {
Expand Down Expand Up @@ -64,13 +64,13 @@ func (bc *BlobsController) parseBlockId(ctx context.Context, block_id string) (u
// @Param block_id path string true "Block identifier. Can be one of: 'head', slot number, hex encoded blockRoot with 0x prefix"
// @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."
// @Success 200 {object} response.ApiDataResponse{data=blobsBySlotRetType} "Successful response"
// @Failure 400 {object} response.ApiErrorResponse "invalid_block_id" "Invalid block_id
// @Failure 404 {object} response.ApiErrorResponse "blobs_not_found" "No blobs found"
// @Failure 400 {object} response.ApiErrorResponse "invalid_slot" "Invalid block id
// @Failure 404 {object} response.ApiErrorResponse "slot_not_found" "Slot not found"
// @Failure 500 {object} response.ApiErrorResponse
// @Router /eth/v1/beacon/blob_sidecars/{block_id} [get]
func (bc *BlobsController) BlobsByBlockId(c *gin.Context) {

block_id := c.Param("block_id")
blockId := c.Param("block_id")
indices := strings.Split(c.Query("indices"), ",")
if len(indices) == 1 && indices[0] == "" {
indices = []string{}
Expand All @@ -79,47 +79,47 @@ func (bc *BlobsController) BlobsByBlockId(c *gin.Context) {
ctx, cancel := context.WithTimeout(c.Request.Context(), 5*time.Second)
defer cancel()

slot, err := bc.parseBlockId(ctx, block_id)
slotNum, err := bc.parseBlockId(ctx, blockId)
if err != nil {
if ctx.Err() == context.DeadlineExceeded {
helper.ReportPublicErrorAndAbort(c, response.GatewayTimeout, err)
return
}
st, ok := status.FromError(err)
if ok && st.Code() == codes.NotFound {
helper.ReportPublicErrorAndAbort(c, response.NewApiErrorNotFound(NOT_FOUND_BLOBS), err)
helper.ReportPublicErrorAndAbort(c, response.NewApiErrorNotFound(NOT_FOUND_SLOT), err)
return
}
helper.ReportPublicErrorAndAbort(c, response.BadGateway, err)
return
}

resp, err := bc.sinkClient.Get(ctx, &pbkv.GetRequest{Key: fmt.Sprintf("slot:%d", slot)})
resp, err := bc.sinkClient.Get(ctx, &pbkv.GetRequest{Key: fmt.Sprintf("slot:%d", slotNum)})
if err != nil {
if ctx.Err() == context.DeadlineExceeded {
helper.ReportPublicErrorAndAbort(c, response.GatewayTimeout, err)
return
}
st, ok := status.FromError(err)
if ok && st.Code() == codes.NotFound {
helper.ReportPublicErrorAndAbort(c, response.NewApiErrorNotFound(NOT_FOUND_BLOBS), err)
helper.ReportPublicErrorAndAbort(c, response.NewApiErrorNotFound(NOT_FOUND_SLOT), err)
return
}
helper.ReportPublicErrorAndAbort(c, response.BadGateway, err)
return
}

blobs := &pbbl.Blobs{}
err = proto.Unmarshal(resp.GetValue(), blobs)
slot := &pbbl.Slot{}
err = proto.Unmarshal(resp.GetValue(), slot)
if err != nil {
helper.ReportPublicErrorAndAbort(c, response.InternalServerError, err)
return
}

resBlobs := []*dto.Blob{}
for _, blob := range blobs.Blobs {
for _, blob := range slot.Blobs {
if len(indices) == 0 || internal.Contains(indices, fmt.Sprintf("%d", blob.Index)) {
resBlobs = append(resBlobs, dto.NewBlob(blob))
resBlobs = append(resBlobs, dto.NewBlob(blob, slot))
}
}

Expand Down
14 changes: 7 additions & 7 deletions dto/blobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,21 @@ func (s StrU64) MarshalJSON() ([]byte, error) {
return json.Marshal(strconv.FormatUint(uint64(s), 10))
}

func NewBlob(blob *pbbl.Blob) *Blob {
func NewBlob(blob *pbbl.Blob, slot *pbbl.Slot) *Blob {
return &Blob{
Index: StrU64(blob.Index),
Blob: blob.Blob,
KzgCommitment: blob.KzgCommitment,
KzgProof: blob.KzgProof,
SignedBlockHeader: SignedBlockHeader{
Message: &Message{
Slot: StrU64(blob.Slot),
ProposerIndex: StrU64(blob.ProposerIndex),
ParentRoot: blob.ParentRoot,
StateRoot: blob.StateRoot,
BodyRoot: blob.BodyRoot,
Slot: StrU64(slot.Slot),
ProposerIndex: StrU64(slot.ProposerIndex),
ParentRoot: slot.ParentRoot,
StateRoot: slot.StateRoot,
BodyRoot: slot.BodyRoot,
},
Signature: blob.Signature,
Signature: slot.Signature,
},
KzgCommitmentInclusionProof: convertToHexBytesSlice(blob.KzgCommitmentInclusionProof),
}
Expand Down
Loading

0 comments on commit 351aa56

Please sign in to comment.