forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 282
feat: add da blob client aws s3 #1209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
17e349e
feat: add blob client aws s3
yiweichi 46648be
go mod tidy
yiweichi 98bae0b
fix: typo
yiweichi 8bb672e
chore: auto version bump [bot]
yiweichi 7f96efa
Merge branch 'develop' into feat-add-blob-clinet-aws-s3
yiweichi 60d63ca
chore: auto version bump [bot]
yiweichi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package blob_client | ||
|
||
import ( | ||
"context" | ||
"crypto/sha256" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"time" | ||
|
||
"github.com/scroll-tech/go-ethereum/common" | ||
"github.com/scroll-tech/go-ethereum/common/hexutil" | ||
"github.com/scroll-tech/go-ethereum/crypto/kzg4844" | ||
) | ||
|
||
const ( | ||
AwsS3DefaultTimeout = 15 * time.Second | ||
) | ||
|
||
type AwsS3Client struct { | ||
client *http.Client | ||
apiEndpoint string | ||
} | ||
|
||
func NewAwsS3Client(apiEndpoint string) *AwsS3Client { | ||
return &AwsS3Client{ | ||
apiEndpoint: apiEndpoint, | ||
client: &http.Client{Timeout: AwsS3DefaultTimeout}, | ||
} | ||
} | ||
|
||
func (c *AwsS3Client) GetBlobByVersionedHashAndBlockTime(ctx context.Context, versionedHash common.Hash, blockTime uint64) (*kzg4844.Blob, error) { | ||
// Scroll mainnet blob data AWS S3 endpoint: https://scroll-mainnet-blob-data.s3.us-west-2.amazonaws.com/ | ||
// Scroll sepolia blob data AWS S3 endpoint: https://scroll-sepolia-blob-data.s3.us-west-2.amazonaws.com/ | ||
path, err := url.JoinPath(c.apiEndpoint, versionedHash.String()) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to join path, err: %w", err) | ||
} | ||
req, err := http.NewRequestWithContext(ctx, "GET", path, nil) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot create request, err: %w", err) | ||
} | ||
resp, err := c.client.Do(req) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot do request, err: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, fmt.Errorf("aws s3 request failed with status: %s: could not read response body: %w", resp.Status, err) | ||
} | ||
bodyStr := string(body) | ||
return nil, fmt.Errorf("aws s3 request failed, status: %s, body: %s", resp.Status, bodyStr) | ||
} | ||
|
||
var blob kzg4844.Blob | ||
buf := blob[:] | ||
if n, err := io.ReadFull(resp.Body, buf); err != nil { | ||
if err == io.ErrUnexpectedEOF || err == io.EOF { | ||
return nil, fmt.Errorf("blob data too short: got %d bytes", n) | ||
} | ||
return nil, fmt.Errorf("failed to read blob data: %w", err) | ||
} | ||
|
||
// sanity check that retrieved blob matches versioned hash | ||
commitment, err := kzg4844.BlobToCommitment(&blob) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to convert blob to commitment, err: %w", err) | ||
} | ||
|
||
blobVersionedHash := kzg4844.CalcBlobHashV1(sha256.New(), &commitment) | ||
if blobVersionedHash != versionedHash { | ||
return nil, fmt.Errorf("blob versioned hash mismatch, expected: %s, got: %s", versionedHash.String(), hexutil.Encode(blobVersionedHash[:])) | ||
} | ||
|
||
return &blob, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.