|
| 1 | +package blob_client |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/sha256" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "net/url" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/scroll-tech/go-ethereum/common" |
| 13 | + "github.com/scroll-tech/go-ethereum/common/hexutil" |
| 14 | + "github.com/scroll-tech/go-ethereum/crypto/kzg4844" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + AwsS3DefaultTimeout = 15 * time.Second |
| 19 | +) |
| 20 | + |
| 21 | +type AwsS3Client struct { |
| 22 | + client *http.Client |
| 23 | + apiEndpoint string |
| 24 | +} |
| 25 | + |
| 26 | +func NewAwsS3Client(apiEndpoint string) *AwsS3Client { |
| 27 | + return &AwsS3Client{ |
| 28 | + apiEndpoint: apiEndpoint, |
| 29 | + client: &http.Client{Timeout: AwsS3DefaultTimeout}, |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func (c *AwsS3Client) GetBlobByVersionedHashAndBlockTime(ctx context.Context, versionedHash common.Hash, blockTime uint64) (*kzg4844.Blob, error) { |
| 34 | + // Scroll mainnet blob data AWS S3 endpoint: https://scroll-mainnet-blob-data.s3.us-west-2.amazonaws.com/ |
| 35 | + // Scroll sepolia blob data AWS S3 endpoint: https://scroll-sepolia-blob-data.s3.us-west-2.amazonaws.com/ |
| 36 | + path, err := url.JoinPath(c.apiEndpoint, versionedHash.String()) |
| 37 | + if err != nil { |
| 38 | + return nil, fmt.Errorf("failed to join path, err: %w", err) |
| 39 | + } |
| 40 | + req, err := http.NewRequestWithContext(ctx, "GET", path, nil) |
| 41 | + if err != nil { |
| 42 | + return nil, fmt.Errorf("cannot create request, err: %w", err) |
| 43 | + } |
| 44 | + resp, err := c.client.Do(req) |
| 45 | + if err != nil { |
| 46 | + return nil, fmt.Errorf("cannot do request, err: %w", err) |
| 47 | + } |
| 48 | + defer resp.Body.Close() |
| 49 | + |
| 50 | + if resp.StatusCode != http.StatusOK { |
| 51 | + body, err := io.ReadAll(resp.Body) |
| 52 | + if err != nil { |
| 53 | + return nil, fmt.Errorf("aws s3 request failed with status: %s: could not read response body: %w", resp.Status, err) |
| 54 | + } |
| 55 | + bodyStr := string(body) |
| 56 | + return nil, fmt.Errorf("aws s3 request failed, status: %s, body: %s", resp.Status, bodyStr) |
| 57 | + } |
| 58 | + |
| 59 | + var blob kzg4844.Blob |
| 60 | + buf := blob[:] |
| 61 | + if n, err := io.ReadFull(resp.Body, buf); err != nil { |
| 62 | + if err == io.ErrUnexpectedEOF || err == io.EOF { |
| 63 | + return nil, fmt.Errorf("blob data too short: got %d bytes", n) |
| 64 | + } |
| 65 | + return nil, fmt.Errorf("failed to read blob data: %w", err) |
| 66 | + } |
| 67 | + |
| 68 | + // sanity check that retrieved blob matches versioned hash |
| 69 | + commitment, err := kzg4844.BlobToCommitment(&blob) |
| 70 | + if err != nil { |
| 71 | + return nil, fmt.Errorf("failed to convert blob to commitment, err: %w", err) |
| 72 | + } |
| 73 | + |
| 74 | + blobVersionedHash := kzg4844.CalcBlobHashV1(sha256.New(), &commitment) |
| 75 | + if blobVersionedHash != versionedHash { |
| 76 | + return nil, fmt.Errorf("blob versioned hash mismatch, expected: %s, got: %s", versionedHash.String(), hexutil.Encode(blobVersionedHash[:])) |
| 77 | + } |
| 78 | + |
| 79 | + return &blob, nil |
| 80 | +} |
0 commit comments