Skip to content

Commit

Permalink
fix(disperser-client): make constructor return err when config is wrong
Browse files Browse the repository at this point in the history
  • Loading branch information
samlaf committed Oct 31, 2024
1 parent 57d716f commit f2100c8
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 23 deletions.
32 changes: 24 additions & 8 deletions api/clients/disperser_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,36 @@ var _ DisperserClient = &disperserClient{}
//
// // Subsequent calls will use the existing connection
// status2, requestId2, err := client.DisperseBlob(ctx, otherData, otherQuorums)
func NewDisperserClient(config *Config, signer core.BlobRequestSigner) *disperserClient {
if config == nil {
config = &Config{}
}
if config.MaxRetrieveBlobSizeBytes == 0 {
// Set to 100MiB for forward compatibility.
// Check official documentation for current max blob size on mainnet.
config.MaxRetrieveBlobSizeBytes = 100 * 1024 * 1024
func NewDisperserClient(config *Config, signer core.BlobRequestSigner) (*disperserClient, error) {
if err := checkConfigAndSetDefaults(config); err != nil {
return nil, fmt.Errorf("invalid config: %w", err)
}
return &disperserClient{
config: config,
signer: signer,
// conn and client are initialized lazily
}, nil
}

func checkConfigAndSetDefaults(c *Config) error {
if c == nil {
return fmt.Errorf("config is nil")
}
if c.Hostname == "" {
return fmt.Errorf("config.Hostname is empty")
}
if c.Port == "" {
return fmt.Errorf("config.Port is empty")
}
if c.Timeout == 0 {
return fmt.Errorf("config.Timeout is 0")
}
if c.MaxRetrieveBlobSizeBytes == 0 {
// Set to 100MiB for forward compatibility.
// Check official documentation for current max blob size on mainnet.
c.MaxRetrieveBlobSizeBytes = 100 * 1024 * 1024
}
return nil
}

// Close closes the grpc connection to the disperser server.
Expand Down
5 changes: 3 additions & 2 deletions api/clients/disperser_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ import (

func TestPutBlobNoopSigner(t *testing.T) {
config := clients.NewConfig("nohost", "noport", time.Second, false)
disperserClient := clients.NewDisperserClient(config, auth.NewLocalNoopSigner())
disperserClient, err := clients.NewDisperserClient(config, auth.NewLocalNoopSigner())
assert.NoError(t, err)

test := []byte("test")
test[0] = 0x00 // make sure the first byte of the requst is always 0
quorums := []uint8{0}
_, _, err := disperserClient.DisperseBlobAuthenticated(context.Background(), test, quorums)
_, _, err = disperserClient.DisperseBlobAuthenticated(context.Background(), test, quorums)
st, isGRPCError := status.FromError(err)
assert.True(t, isGRPCError)
assert.Equal(t, codes.InvalidArgument.String(), st.Code().String())
Expand Down
13 changes: 8 additions & 5 deletions api/clients/eigenda_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,16 @@ func NewEigenDAClient(log log.Logger, config EigenDAClientConfig) (*EigenDAClien
var edasmCaller *edasm.ContractEigenDAServiceManagerCaller
ethClient, err = ethclient.Dial(config.EthRpcUrl)
if err != nil {
return nil, fmt.Errorf("failed to dial ETH RPC node: %w", err)
return nil, fmt.Errorf("dial ETH RPC node: %w", err)
}
edasmCaller, err = edasm.NewContractEigenDAServiceManagerCaller(common.HexToAddress(config.SvcManagerAddr), ethClient)
if err != nil {
return nil, fmt.Errorf("failed to create EigenDAServiceManagerCaller: %w", err)
return nil, fmt.Errorf("new EigenDAServiceManagerCaller: %w", err)
}

host, port, err := net.SplitHostPort(config.RPC)
if err != nil {
return nil, fmt.Errorf("failed to parse EigenDA RPC: %w", err)
return nil, fmt.Errorf("parse EigenDA RPC: %w", err)
}

var signer core.BlobRequestSigner
Expand All @@ -108,11 +108,14 @@ func NewEigenDAClient(log log.Logger, config EigenDAClientConfig) (*EigenDAClien
}

disperserConfig := NewConfig(host, port, config.ResponseTimeout, !config.DisableTLS)
disperserClient := NewDisperserClient(disperserConfig, signer)
disperserClient, err := NewDisperserClient(disperserConfig, signer)
if err != nil {
return nil, fmt.Errorf("new disperser-client: %w", err)
}

lowLevelCodec, err := codecs.BlobEncodingVersionToCodec(config.PutBlobEncodingVersion)
if err != nil {
return nil, fmt.Errorf("error initializing EigenDA client: %w", err)
return nil, fmt.Errorf("create low level codec: %w", err)
}

var codec codecs.BlobCodec
Expand Down
4 changes: 2 additions & 2 deletions inabox/tests/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ var _ = Describe("Inabox Integration", func() {
privateKeyHex := "0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcded"
signer := auth.NewLocalBlobRequestSigner(privateKeyHex)

disp := clients.NewDisperserClient(&clients.Config{
disp, err := clients.NewDisperserClient(&clients.Config{
Hostname: "localhost",
Port: "32003",
Timeout: 10 * time.Second,
}, signer)

Expect(err).To(BeNil())
Expect(disp).To(Not(BeNil()))

data := make([]byte, 1024)
Expand Down
5 changes: 3 additions & 2 deletions inabox/tests/ratelimit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,16 @@ func testRatelimit(t *testing.T, testConfig *deploy.Config, c ratelimitTestCase)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()

disp := clients.NewDisperserClient(&clients.Config{
disp, err := clients.NewDisperserClient(&clients.Config{
Hostname: "localhost",
Port: testConfig.Dispersers[0].DISPERSER_SERVER_GRPC_PORT,
Timeout: 10 * time.Second,
}, nil)
assert.NoError(t, err)
assert.NotNil(t, disp)

data := make([]byte, c.blobSize)
_, err := rand.Read(data)
_, err = rand.Read(data)
assert.NoError(t, err)

dispersalTicker := time.NewTicker(c.dispersalInterval)
Expand Down
9 changes: 7 additions & 2 deletions tools/traffic/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/rand"
"encoding/hex"
"fmt"
"os"
"os/signal"
"sync"
Expand All @@ -27,12 +28,16 @@ func NewTrafficGenerator(config *Config, signer core.BlobRequestSigner) (*Traffi
loggerConfig := common.DefaultLoggerConfig()
logger, err := common.NewLogger(loggerConfig)
if err != nil {
return nil, err
return nil, fmt.Errorf("new logger: %w", err)
}

dispserserClient, err := clients.NewDisperserClient(&config.Config, signer)
if err != nil {
return nil, fmt.Errorf("new disperser-client: %w", err)
}
return &TrafficGenerator{
Logger: logger,
DisperserClient: clients.NewDisperserClient(&config.Config, signer),
DisperserClient: dispserserClient,
Config: config,
}, nil
}
Expand Down
8 changes: 6 additions & 2 deletions tools/traffic/generator_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ func NewTrafficGeneratorV2(config *config.Config) (*Generator, error) {

unconfirmedKeyChannel := make(chan *workers.UnconfirmedKey, 100)

disperserClient := clients.NewDisperserClient(config.DisperserClientConfig, signer)
disperserClient, err := clients.NewDisperserClient(config.DisperserClientConfig, signer)
if err != nil {
cancel()
return nil, fmt.Errorf("new disperser-client: %w", err)
}
statusVerifier := workers.NewBlobStatusTracker(
&ctx,
&waitGroup,
Expand Down Expand Up @@ -134,7 +138,7 @@ func NewTrafficGeneratorV2(config *config.Config) (*Generator, error) {
waitGroup: &waitGroup,
generatorMetrics: generatorMetrics,
logger: &logger,
disperserClient: clients.NewDisperserClient(config.DisperserClientConfig, signer),
disperserClient: disperserClient,
eigenDAClient: client,
config: config,
writers: writers,
Expand Down

0 comments on commit f2100c8

Please sign in to comment.