From 8e8f8c54d70483186b88404cd2d2501f0d5f3347 Mon Sep 17 00:00:00 2001 From: Ganesh Vanahalli Date: Fri, 1 Mar 2024 14:31:03 -0600 Subject: [PATCH] Save Blob data as fetched from beaconURL to disk --- util/headerreader/blob_client.go | 12 ++++++++---- util/headerreader/blob_client_test.go | 4 +++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/util/headerreader/blob_client.go b/util/headerreader/blob_client.go index 93d6c903a2..8989a321c7 100644 --- a/util/headerreader/blob_client.go +++ b/util/headerreader/blob_client.go @@ -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)) @@ -203,7 +207,7 @@ 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 } } @@ -211,13 +215,13 @@ func (b *BlobClient) blobSidecars(ctx context.Context, slot uint64, versionedHas 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") diff --git a/util/headerreader/blob_client_test.go b/util/headerreader/blob_client_test.go index 4c19d16fc8..9735899daa 100644 --- a/util/headerreader/blob_client_test.go +++ b/util/headerreader/blob_client_test.go @@ -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")