From b1687ca5edd0b87be840a9e0444ba5c43723f9d7 Mon Sep 17 00:00:00 2001 From: Ian Shim Date: Tue, 17 Dec 2024 11:23:48 -0800 Subject: [PATCH] nil checks in disperser v2 server constructor --- disperser/apiserver/server_v2.go | 32 +++++++++++++++++++++++++-- disperser/apiserver/server_v2_test.go | 3 ++- disperser/cmd/apiserver/main.go | 5 ++++- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/disperser/apiserver/server_v2.go b/disperser/apiserver/server_v2.go index bd45248f1d..ed22b88954 100644 --- a/disperser/apiserver/server_v2.go +++ b/disperser/apiserver/server_v2.go @@ -74,7 +74,32 @@ func NewDispersalServerV2( onchainStateRefreshInterval time.Duration, _logger logging.Logger, registry *prometheus.Registry, -) *DispersalServerV2 { +) (*DispersalServerV2, error) { + if serverConfig.GrpcPort == "" { + return nil, errors.New("grpc port is required") + } + if blobStore == nil { + return nil, errors.New("blob store is required") + } + if blobMetadataStore == nil { + return nil, errors.New("blob metadata store is required") + } + if chainReader == nil { + return nil, errors.New("chain reader is required") + } + if authenticator == nil { + return nil, errors.New("authenticator is required") + } + if prover == nil { + return nil, errors.New("prover is required") + } + if maxNumSymbolsPerBlob == 0 { + return nil, errors.New("maxNumSymbolsPerBlob is required") + } + if _logger == nil { + return nil, errors.New("logger is required") + } + logger := _logger.With("component", "DispersalServerV2") return &DispersalServerV2{ @@ -92,7 +117,7 @@ func NewDispersalServerV2( onchainStateRefreshInterval: onchainStateRefreshInterval, metrics: newAPIServerV2Metrics(registry), - } + }, nil } func (s *DispersalServerV2) Start(ctx context.Context) error { @@ -232,6 +257,9 @@ func (s *DispersalServerV2) RefreshOnchainState(ctx context.Context) error { } func (s *DispersalServerV2) GetPaymentState(ctx context.Context, req *pb.GetPaymentStateRequest) (*pb.GetPaymentStateReply, error) { + if s.meterer == nil { + return nil, errors.New("payment meterer is not enabled") + } start := time.Now() defer func() { s.metrics.reportGetPaymentStateLatency(time.Since(start)) diff --git a/disperser/apiserver/server_v2_test.go b/disperser/apiserver/server_v2_test.go index 7f95a8f3bd..701c1c02cd 100644 --- a/disperser/apiserver/server_v2_test.go +++ b/disperser/apiserver/server_v2_test.go @@ -497,7 +497,7 @@ func newTestServerV2(t *testing.T) *testComponents { }, }, nil) - s := apiserver.NewDispersalServerV2( + s, err := apiserver.NewDispersalServerV2( disperser.ServerConfig{ GrpcPort: "51002", GrpcTimeout: 1 * time.Second, @@ -512,6 +512,7 @@ func newTestServerV2(t *testing.T) *testComponents { time.Hour, logger, prometheus.NewRegistry()) + assert.NoError(t, err) err = s.RefreshOnchainState(context.Background()) assert.NoError(t, err) diff --git a/disperser/cmd/apiserver/main.go b/disperser/cmd/apiserver/main.go index e28e3aeb79..1e5ad95ca7 100644 --- a/disperser/cmd/apiserver/main.go +++ b/disperser/cmd/apiserver/main.go @@ -170,7 +170,7 @@ func RunDisperserServer(ctx *cli.Context) error { blobMetadataStore := blobstorev2.NewBlobMetadataStore(dynamoClient, logger, config.BlobstoreConfig.TableName) blobStore := blobstorev2.NewBlobStore(bucketName, s3Client, logger) - server := apiserver.NewDispersalServerV2( + server, err := apiserver.NewDispersalServerV2( config.ServerConfig, blobStore, blobMetadataStore, @@ -183,6 +183,9 @@ func RunDisperserServer(ctx *cli.Context) error { logger, reg, ) + if err != nil { + return err + } return server.Start(context.Background()) }