Skip to content

Commit

Permalink
Merge pull request OffchainLabs#2166 from OffchainLabs/update-saveblo…
Browse files Browse the repository at this point in the history
…btodisk-design
  • Loading branch information
ganeshvanahalli authored Mar 1, 2024
2 parents f10123b + 355a77f commit 60ff801
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
12 changes: 8 additions & 4 deletions util/headerreader/blob_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,14 @@ type blobResponseItem struct {
}

func (b *BlobClient) blobSidecars(ctx context.Context, slot uint64, versionedHashes []common.Hash) ([]kzg4844.Blob, error) {
response, err := beaconRequest[[]blobResponseItem](b, ctx, fmt.Sprintf("/eth/v1/beacon/blob_sidecars/%d", slot))
rawData, err := beaconRequest[json.RawMessage](b, ctx, fmt.Sprintf("/eth/v1/beacon/blob_sidecars/%d", slot))
if err != nil {
return nil, fmt.Errorf("error calling beacon client in blobSidecars: %w", err)
}
var response []blobResponseItem
if err := json.Unmarshal(rawData, &response); err != nil {
return nil, fmt.Errorf("error unmarshalling raw data into array of blobResponseItem in blobSidecars: %w", err)
}

if len(response) < len(versionedHashes) {
return nil, fmt.Errorf("expected at least %d blobs for slot %d but only got %d", len(versionedHashes), slot, len(response))
Expand Down Expand Up @@ -203,21 +207,21 @@ func (b *BlobClient) blobSidecars(ctx context.Context, slot uint64, versionedHas
}

if b.blobDirectory != "" {
if err := saveBlobDataToDisk(response, slot, b.blobDirectory); err != nil {
if err := saveBlobDataToDisk(rawData, slot, b.blobDirectory); err != nil {
return nil, err
}
}

return output, nil
}

func saveBlobDataToDisk(response []blobResponseItem, slot uint64, blobDirectory string) error {
func saveBlobDataToDisk(rawData json.RawMessage, slot uint64, blobDirectory string) error {
filePath := path.Join(blobDirectory, fmt.Sprint(slot))
file, err := os.Create(filePath)
if err != nil {
return fmt.Errorf("could not create file to store fetched blobs")
}
full := fullResult[[]blobResponseItem]{Data: response}
full := fullResult[json.RawMessage]{Data: rawData}
fullbytes, err := json.Marshal(full)
if err != nil {
return fmt.Errorf("unable to marshal data into bytes while attempting to store fetched blobs")
Expand Down
4 changes: 3 additions & 1 deletion util/headerreader/blob_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ func TestSaveBlobsToDisk(t *testing.T) {
KzgProof: []byte{2},
}}
testDir := t.TempDir()
err := saveBlobDataToDisk(response, 5, testDir)
rawData, err := json.Marshal(response)
Require(t, err)
err = saveBlobDataToDisk(rawData, 5, testDir)
Require(t, err)

filePath := path.Join(testDir, "5")
Expand Down

0 comments on commit 60ff801

Please sign in to comment.