Skip to content

Commit

Permalink
feat: init once populate accountant in disperseBlob
Browse files Browse the repository at this point in the history
  • Loading branch information
hopeyen committed Dec 17, 2024
1 parent bbe7226 commit efb9cf8
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions api/clients/v2/disperser_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ type DisperserClient interface {
}

type disperserClient struct {
config *DisperserClientConfig
signer corev2.BlobRequestSigner
initOnce sync.Once
conn *grpc.ClientConn
client disperser_rpc.DisperserClient
prover encoding.Prover
accountant *Accountant
config *DisperserClientConfig
signer corev2.BlobRequestSigner
initOnceGrpc sync.Once
initOnceAccountant sync.Once
conn *grpc.ClientConn
client disperser_rpc.DisperserClient
prover encoding.Prover
accountant *Accountant
}

var _ DisperserClient = &disperserClient{}
Expand Down Expand Up @@ -121,6 +122,10 @@ func (c *disperserClient) DisperseBlob(
if err != nil {
return nil, [32]byte{}, api.NewErrorFailover(err)
}
err = c.initOncePopulateAccountant(ctx)
if err != nil {
return nil, [32]byte{}, api.NewErrorFailover(err)
}

if c.signer == nil {
return nil, [32]byte{}, api.NewErrorInternal("uninitialized signer for authenticated dispersal")
Expand Down Expand Up @@ -273,7 +278,7 @@ func (c *disperserClient) GetBlobCommitment(ctx context.Context, data []byte) (*
// If initialization fails, it caches the error and will return it on every subsequent call.
func (c *disperserClient) initOnceGrpcConnection() error {
var initErr error
c.initOnce.Do(func() {
c.initOnceGrpc.Do(func() {
addr := fmt.Sprintf("%v:%v", c.config.Hostname, c.config.Port)
dialOptions := getGrpcDialOptions(c.config.UseSecureGrpcFlag)
conn, err := grpc.NewClient(addr, dialOptions...)
Expand All @@ -289,3 +294,22 @@ func (c *disperserClient) initOnceGrpcConnection() error {
}
return nil
}

// initOncePopulateAccountant initializes the accountant if it is not already initialized.
// If initialization fails, it caches the error and will return it on every subsequent call.
func (c *disperserClient) initOncePopulateAccountant(ctx context.Context) error {
var initErr error
c.initOnceAccountant.Do(func() {
if c.accountant == nil {
err := c.PopulateAccountant(ctx)
if err != nil {
initErr = err
return
}
}
})
if initErr != nil {
return fmt.Errorf("populating accountant: %w", initErr)
}
return nil
}

0 comments on commit efb9cf8

Please sign in to comment.