From efb9cf8cc9f0b314f2e9342378f4f3b64d03efca Mon Sep 17 00:00:00 2001 From: hopeyen Date: Tue, 17 Dec 2024 13:34:00 -0800 Subject: [PATCH] feat: init once populate accountant in disperseBlob --- api/clients/v2/disperser_client.go | 40 ++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/api/clients/v2/disperser_client.go b/api/clients/v2/disperser_client.go index ff945227fb..84d9ab92f8 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 +}