Skip to content
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

feat: presign url using blob storage #82

Merged
merged 11 commits into from
Jan 25, 2024
3 changes: 3 additions & 0 deletions config/chainstorage/aptos/mainnet/base.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions config/chainstorage/arbitrum/mainnet/base.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions config/chainstorage/avacchain/mainnet/base.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions config/chainstorage/base/goerli/base.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions config/chainstorage/base/mainnet/base.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions config/chainstorage/bitcoin/mainnet/base.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions config/chainstorage/bsc/mainnet/base.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions config/chainstorage/dogecoin/mainnet/base.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions config/chainstorage/ethereum/goerli/base.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions config/chainstorage/ethereum/holesky/base.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions config/chainstorage/ethereum/mainnet/base.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions config/chainstorage/fantom/mainnet/base.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions config/chainstorage/optimism/mainnet/base.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions config/chainstorage/polygon/mainnet/base.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions config/chainstorage/polygon/testnet/base.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions config/chainstorage/solana/mainnet/base.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions config_templates/config/base.template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ aws:
region: us-east-1
storage:
data_compression: GZIP
gcp:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may want to change GcpConfig to a pointer so it becomes optional. Then I guess we don't need to add this section to the default template?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's already a pointer, so optional. However, since we marked the presigned_url_expiration required, we need a default value for it because gcp project is always required for firestore.

project: "development"
presigned_url_expiration: 30m
cadence:
address: ""
retention_period: 7
Expand Down
5 changes: 3 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,9 @@ type (
}

GcpConfig struct {
Project string `mapstructure:"project" validate:"required"`
Bucket string `mapstructure:"bucket"`
Project string `mapstructure:"project" validate:"required"`
Bucket string `mapstructure:"bucket"`
PresignedUrlExpiration time.Duration `mapstructure:"presigned_url_expiration" validate:"required"`
}

DynamoDBConfig struct {
Expand Down
13 changes: 2 additions & 11 deletions internal/server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import (
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
"google.golang.org/grpc/reflection"

"github.com/aws/aws-sdk-go/aws"
awss3 "github.com/aws/aws-sdk-go/service/s3"
"github.com/cenkalti/backoff"
"github.com/uber-go/tally/v4"
"go.uber.org/fx"
Expand Down Expand Up @@ -55,7 +53,6 @@ type (
metaStorage metastorage.MetaStorage
blobStorage blobstorage.BlobStorage
transactionStorage metastorage.TransactionStorage
s3Client s3.Client
blockchainClient client.Client
parser parser.Parser
metrics *serverMetrics
Expand Down Expand Up @@ -204,7 +201,6 @@ func NewServer(params ServerParams) *Server {
metaStorage: params.MetaStorage,
blobStorage: params.BlobStorage,
transactionStorage: params.TransactionStorage,
s3Client: params.S3Client,
blockchainClient: params.BlockchainClient,
parser: params.Parser,
metrics: newServerMetrics(params.Metrics),
Expand Down Expand Up @@ -717,14 +713,9 @@ func (s *Server) newBlockFile(block *api.BlockMetadata) (*api.BlockFile, error)

key := block.GetObjectKeyMain()
compression := storage_utils.GetCompressionType(key)
getObjectReq, _ := s.s3Client.GetObjectRequest(&awss3.GetObjectInput{
Bucket: aws.String(s.config.AWS.Bucket),
Key: aws.String(key),
})
fileUrl, err := getObjectReq.Presign(s.config.AWS.PresignedUrlExpiration)
fileUrl, err := s.blobStorage.PreSign(context.Background(), key)
if err != nil {
s.logger.Error("block file s3 presign error", zap.Reflect("block", block), zap.Error(err))
return nil, status.Errorf(codes.Internal, "internal block file url generation error: %+v", err)
return nil, xerrors.Errorf("failed to generate presigned url: %w", err)
}

return &api.BlockFile{
Expand Down
74 changes: 24 additions & 50 deletions internal/server/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ import (

"github.com/aws/aws-sdk-go/aws"
awsClient "github.com/aws/aws-sdk-go/aws/client"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/awstesting"
"github.com/aws/aws-sdk-go/awstesting/unit"
awss3 "github.com/aws/aws-sdk-go/service/s3"
geth "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -242,11 +240,10 @@ func (s *handlerTestSuite) TestGetBlockFile() {
}, nil
},
),
s.s3Client.EXPECT().GetObjectRequest(gomock.Any()).Times(1).DoAndReturn(
func(req *awss3.GetObjectInput) (*request.Request, *awss3.GetObjectOutput) {
require.Equal("example-chainstorage-ethereum-mainnet-dev", *req.Bucket)
require.Equal(objectKeyMain, *req.Key)
return s.newAwsPresignRequest("name", "GET", objectKeyMain), nil
s.blobStorage.EXPECT().PreSign(gomock.Any(), gomock.Any()).Times(1).DoAndReturn(
func(_ context.Context, key string) (string, error) {
require.Equal(objectKeyMain, key)
return "http://endpoint/foo/bar", nil
},
),
)
Expand Down Expand Up @@ -293,11 +290,10 @@ func (s *handlerTestSuite) TestGetBlockFile_Gzip() {
}, nil
},
),
s.s3Client.EXPECT().GetObjectRequest(gomock.Any()).Times(1).DoAndReturn(
func(req *awss3.GetObjectInput) (*request.Request, *awss3.GetObjectOutput) {
require.Equal("example-chainstorage-ethereum-mainnet-dev", *req.Bucket)
require.Equal(objectKeyMain, *req.Key)
return s.newAwsPresignRequest("name", "GET", objectKeyMain), nil
s.blobStorage.EXPECT().PreSign(gomock.Any(), gomock.Any()).Times(1).DoAndReturn(
func(_ context.Context, key string) (string, error) {
require.Equal(objectKeyMain, key)
return "http://endpoint/foo/bar.gzip", nil
},
),
)
Expand Down Expand Up @@ -434,22 +430,16 @@ func (s *handlerTestSuite) TestGetBlockFilesByRange_PresignErr() {
}, nil
},
),
s.s3Client.EXPECT().GetObjectRequest(gomock.Any()).Times(1).DoAndReturn(
func(req *awss3.GetObjectInput) (*request.Request, *awss3.GetObjectOutput) {
require.Equal("example-chainstorage-ethereum-mainnet-dev", *req.Bucket)
require.Equal("foo", *req.Key)
return s.awsClient.NewRequest(&request.Operation{
Name: "name",
HTTPMethod: "GET",
HTTPPath: "/foo",
}, &struct{}{}, &struct{}{}), nil
s.blobStorage.EXPECT().PreSign(gomock.Any(), gomock.Any()).Times(1).DoAndReturn(
func(_ context.Context, key string) (string, error) {
require.Equal("foo", key)
return "http://endpoint/foo", nil
},
),
s.s3Client.EXPECT().GetObjectRequest(gomock.Any()).Times(1).DoAndReturn(
func(req *awss3.GetObjectInput) (*request.Request, *awss3.GetObjectOutput) {
require.Equal("example-chainstorage-ethereum-mainnet-dev", *req.Bucket)
require.Equal("bar", *req.Key)
return s.newAwsPresignFailedRequest(), nil
s.blobStorage.EXPECT().PreSign(gomock.Any(), gomock.Any()).Times(1).DoAndReturn(
func(_ context.Context, key string) (string, error) {
require.Equal("bar", key)
return "", fmt.Errorf("failed")
},
),
)
Expand Down Expand Up @@ -489,18 +479,16 @@ func (s *handlerTestSuite) TestGetBlockFilesByRange() {
}, nil
},
),
s.s3Client.EXPECT().GetObjectRequest(gomock.Any()).Times(1).DoAndReturn(
func(req *awss3.GetObjectInput) (*request.Request, *awss3.GetObjectOutput) {
require.Equal("example-chainstorage-ethereum-mainnet-dev", *req.Bucket)
require.Equal("foo", *req.Key)
return s.newAwsPresignRequest("name", "GET", "/foo"), nil
s.blobStorage.EXPECT().PreSign(gomock.Any(), gomock.Any()).Times(1).DoAndReturn(
func(_ context.Context, key string) (string, error) {
require.Equal("foo", key)
return "http://endpoint/foo", nil
},
),
s.s3Client.EXPECT().GetObjectRequest(gomock.Any()).Times(1).DoAndReturn(
func(req *awss3.GetObjectInput) (*request.Request, *awss3.GetObjectOutput) {
require.Equal("example-chainstorage-ethereum-mainnet-dev", *req.Bucket)
require.Equal("bar", *req.Key)
return s.newAwsPresignRequest("name", "GET", "/bar"), nil
s.blobStorage.EXPECT().PreSign(gomock.Any(), gomock.Any()).Times(1).DoAndReturn(
func(_ context.Context, key string) (string, error) {
require.Equal("bar", key)
return "http://endpoint/bar", nil
},
),
)
Expand Down Expand Up @@ -1161,20 +1149,6 @@ func (s *handlerTestSuite) TestGetRosettaBlocksByRange_NotImplemented() {
s.verifyStatusCode(codes.Unimplemented, err)
}

func (s *handlerTestSuite) newAwsPresignFailedRequest() *request.Request {
return s.awsClient.NewRequest(&request.Operation{
BeforePresignFn: func(r *request.Request) error { return fmt.Errorf("fail") },
}, &struct{}{}, &struct{}{})
}

func (s *handlerTestSuite) newAwsPresignRequest(name, method, path string) *request.Request {
return s.awsClient.NewRequest(&request.Operation{
Name: name,
HTTPMethod: method,
HTTPPath: path,
}, &struct{}{}, &struct{}{})
}

func (s *handlerTestSuite) TestStreamChainEvents_WithSequence() {
require := testutil.Require(s.T())
const (
Expand Down
Loading
Loading