diff --git a/api/clients/v2/disperser_client.go b/api/clients/v2/disperser_client.go index ff945227f..84d9ab92f 100644 --- a/api/clients/v2/disperser_client.go +++ b/api/clients/v2/disperser_client.go @@ -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{} @@ -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") @@ -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...) @@ -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 +}