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 47dfbc2 commit ced7ab3
Showing 1 changed file with 32 additions and 12 deletions.
44 changes: 32 additions & 12 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,16 +122,16 @@ 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")
}

// TODO(hopeyen): uncomment this after the accountant is implemented
// if c.accountant == nil {
// return nil, [32]byte{}, api.NewErrorInternal("uninitialized accountant for paid dispersal; make sure to call PopulateAccountant after creating the client")
// }

// symbolLength := encoding.GetBlobLengthPowerOf2(uint(len(data)))
// payment, err := c.accountant.AccountBlob(ctx, uint64(symbolLength), quorums, salt)
// if err != nil {
Expand Down Expand Up @@ -273,7 +274,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 +290,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 ced7ab3

Please sign in to comment.