Skip to content

Commit

Permalink
feat(blobstorage): allow get_blob api to return blob data (#16629)
Browse files Browse the repository at this point in the history
Co-authored-by: jeff <[email protected]>
Co-authored-by: Jeffery Walsh <[email protected]>
  • Loading branch information
3 people authored Apr 4, 2024
1 parent 99177bb commit 2581772
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 23 deletions.
4 changes: 2 additions & 2 deletions packages/blobstorage/.default.indexer.env
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
RPC_URL=wss://l1ws.internal.taiko.xyz
BEACON_URL=https://l1beacon.internal.taiko.xyz
RPC_URL=wss://l1ws.hekla.taiko.xyz
BEACON_URL=https://l1beacon.hekla.taiko.xyz
TAIKO_L1_CONTRACT_ADDRESS=0xC069c3d2a9f2479F559AD34485698ad5199C555f
DATABASE_HOST=localhost
DATABASE_PORT=3306
Expand Down
2 changes: 0 additions & 2 deletions packages/blobstorage/blob_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package blobstorage
type BlobHash struct {
BlobHash string
KzgCommitment string
BlockTimestamp uint64
BlobData string
BlockID uint64
EmittedBlockID uint64
Expand All @@ -12,7 +11,6 @@ type BlobHash struct {
type SaveBlobHashOpts struct {
BlobHash string
KzgCommitment string
BlockTimestamp uint64
BlobData string
BlockID uint64
EmittedBlockID uint64
Expand Down
4 changes: 4 additions & 0 deletions packages/blobstorage/indexer/beaconclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"net/http"
"strconv"
"time"

"golang.org/x/exp/slog"
)

var (
Expand Down Expand Up @@ -64,6 +66,8 @@ func NewBeaconClient(cfg *Config, timeout time.Duration) (*BeaconClient, error)
return nil, err
}

slog.Info("beaconClientInfo", "secondsPerSlot", secondsPerSlotUint64, "genesisTime", genesisTime)

return &BeaconClient{
beaconURL: cfg.BeaconURL,
genesisTime: genesisTime,
Expand Down
24 changes: 11 additions & 13 deletions packages/blobstorage/indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,18 +284,23 @@ func (i *Indexer) checkReorg(ctx context.Context, event *taikol1.TaikoL1BlockPro
}

func (i *Indexer) storeBlob(ctx context.Context, event *taikol1.TaikoL1BlockProposed) error {
blockID, err := i.beaconClient.timeToSlot(event.Meta.Timestamp)
slot, err := i.beaconClient.timeToSlot(event.Meta.Timestamp)
if err != nil {
return err
}

slog.Info("blockProposed event found", "blockID", blockID, "emittedIn", event.Raw.BlockNumber, "blobUsed", event.Meta.BlobUsed)
slog.Info("blockProposed event found",
"slot", slot,
"emittedIn", event.Raw.BlockNumber,
"blobUsed", event.Meta.BlobUsed,
"timesStamp", event.Meta.Timestamp,
)

if !event.Meta.BlobUsed {
return nil
}

blobsResponse, err := i.beaconClient.getBlobs(ctx, blockID)
blobsResponse, err := i.beaconClient.getBlobs(ctx, slot)
if err != nil {
return err
}
Expand All @@ -306,15 +311,9 @@ func (i *Indexer) storeBlob(ctx context.Context, event *taikol1.TaikoL1BlockProp
metaBlobHash := common.BytesToHash(event.Meta.BlobHash[:])
// Comparing the hex strings of meta.blobHash (blobHash)
if calculateBlobHash(data.KzgCommitment) == metaBlobHash {
blockTs, err := i.getBlockTimestamp(i.cfg.RPCURL, new(big.Int).SetUint64(blockID))
if err != nil {
slog.Error("error getting block timestamp", "error", err)
return err
}

slog.Info("blockHash", "blobHash", metaBlobHash.String())
slog.Info("storing blobHash in db", "blobHash", metaBlobHash.String())

err = i.storeBlobInDB(metaBlobHash.String(), data.KzgCommitment, data.Blob, blockTs, event.BlockId.Uint64(), event.Raw.BlockNumber)
err = i.storeBlobInDB(metaBlobHash.String(), data.KzgCommitment, data.Blob, event.BlockId.Uint64(), event.Raw.BlockNumber)
if err != nil {
slog.Error("Error storing blob in DB", "error", err)
return err
Expand All @@ -327,13 +326,12 @@ func (i *Indexer) storeBlob(ctx context.Context, event *taikol1.TaikoL1BlockProp
return errors.New("BLOB not found")
}

func (i *Indexer) storeBlobInDB(blobHashInMeta, kzgCommitment, blob string, blockTs uint64, blockID uint64, emittedBlockID uint64) error {
func (i *Indexer) storeBlobInDB(blobHashInMeta, kzgCommitment, blob string, blockID uint64, emittedBlockID uint64) error {
return i.blobHashRepo.Save(blobstorage.SaveBlobHashOpts{
BlobHash: blobHashInMeta,
KzgCommitment: kzgCommitment,
BlockID: blockID,
BlobData: blob,
BlockTimestamp: blockTs,
EmittedBlockID: emittedBlockID,
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ CREATE TABLE IF NOT EXISTS blob_hashes (
emitted_block_id BIGINT NOT NULL,
blob_hash VARCHAR(100) NOT NULL,
kzg_commitment LONGTEXT NOT NULL,
block_timestamp BIGINT NOT NULL,
blob_data LONGTEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
Expand Down
13 changes: 9 additions & 4 deletions packages/blobstorage/pkg/http/get_blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ type getBlobResponse struct {
}

type blobData struct {
Blob string `bson:"blob_hash" json:"blob_hash"`
BlobHash string `bson:"blob_hash" json:"blob_hash"`
KzgCommitment string `bson:"kzg_commitment" json:"kzg_commitment"`
Blob string `bson:"blob" json:"blob"`
}

// GetBlob
Expand All @@ -37,6 +38,7 @@ func (srv *Server) GetBlob(c echo.Context) error {
}

data, err := srv.getBlobData(strings.Split(blobHashes, ","))

if err != nil {
return webutils.LogAndRenderErrors(c, http.StatusBadRequest, err)
}
Expand All @@ -48,8 +50,9 @@ func (srv *Server) GetBlob(c echo.Context) error {
// Convert data to the correct type
for _, d := range data {
response.Data = append(response.Data, blobData{
Blob: d.Blob,
BlobHash: d.BlobHash,
KzgCommitment: d.KzgCommitment,
Blob: d.Blob,
},
)
}
Expand All @@ -69,15 +72,17 @@ func (srv *Server) getBlobData(blobHashes []string) ([]blobData, error) {
if err != nil {
if err == gorm.ErrRecordNotFound {
// Handle case where blob hash is not found
result.Blob = "NOT_FOUND"
result.BlobHash = "NOT_FOUND"
result.KzgCommitment = "NOT_FOUND"
result.Blob = "NOT_FOUND"
} else {
// Return error for other types of errors
return nil, err
}
} else {
result.Blob = bh.BlobHash
result.BlobHash = bh.BlobHash
result.KzgCommitment = bh.KzgCommitment
result.Blob = bh.BlobData

results = append(results, result)
}
Expand Down
1 change: 0 additions & 1 deletion packages/blobstorage/pkg/repo/blob_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func (r *BlobHashRepository) Save(opts blobstorage.SaveBlobHashOpts) error {
b := &blobstorage.BlobHash{
BlobHash: opts.BlobHash,
KzgCommitment: opts.KzgCommitment,
BlockTimestamp: opts.BlockTimestamp,
BlobData: opts.BlobData,
BlockID: opts.BlockID,
EmittedBlockID: opts.EmittedBlockID,
Expand Down

0 comments on commit 2581772

Please sign in to comment.