-
Notifications
You must be signed in to change notification settings - Fork 179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[v2] disperser client payments api #928
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9982bf1
feat: client construct payment header
hopeyen 5004cf9
feat: getPaymentState and init accountant
hopeyen dd00e32
fix: nil checks and remove bad comment
hopeyen 0d59ee9
fix: lint
hopeyen cd908e1
refactor: use protobuf getters and chain
hopeyen 6643ca9
refactor: rm Accountant interface and rename struct to Accountant
hopeyen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ package clients | |
import ( | ||
"context" | ||
"fmt" | ||
"math/big" | ||
"sync" | ||
|
||
"github.com/Layr-Labs/eigenda/api" | ||
|
@@ -30,12 +29,13 @@ type DisperserClientV2 interface { | |
} | ||
|
||
type disperserClientV2 struct { | ||
config *DisperserClientV2Config | ||
signer corev2.BlobRequestSigner | ||
initOnce sync.Once | ||
conn *grpc.ClientConn | ||
client disperser_rpc.DisperserClient | ||
prover encoding.Prover | ||
config *DisperserClientV2Config | ||
signer corev2.BlobRequestSigner | ||
initOnce sync.Once | ||
conn *grpc.ClientConn | ||
client disperser_rpc.DisperserClient | ||
prover encoding.Prover | ||
accountant *Accountant | ||
} | ||
|
||
var _ DisperserClientV2 = &disperserClientV2{} | ||
|
@@ -60,7 +60,7 @@ var _ DisperserClientV2 = &disperserClientV2{} | |
// | ||
// // Subsequent calls will use the existing connection | ||
// status2, blobKey2, err := client.DisperseBlob(ctx, data, blobHeader) | ||
func NewDisperserClientV2(config *DisperserClientV2Config, signer corev2.BlobRequestSigner, prover encoding.Prover) (*disperserClientV2, error) { | ||
func NewDisperserClientV2(config *DisperserClientV2Config, signer corev2.BlobRequestSigner, prover encoding.Prover, accountant *Accountant) (*disperserClientV2, error) { | ||
if config == nil { | ||
return nil, api.NewErrorInvalidArg("config must be provided") | ||
} | ||
|
@@ -75,13 +75,28 @@ func NewDisperserClientV2(config *DisperserClientV2Config, signer corev2.BlobReq | |
} | ||
|
||
return &disperserClientV2{ | ||
config: config, | ||
signer: signer, | ||
prover: prover, | ||
config: config, | ||
signer: signer, | ||
prover: prover, | ||
accountant: accountant, | ||
// conn and client are initialized lazily | ||
}, nil | ||
} | ||
|
||
// PopulateAccountant populates the accountant with the payment state from the disperser. | ||
func (c *disperserClientV2) PopulateAccountant(ctx context.Context) error { | ||
paymentState, err := c.GetPaymentState(ctx) | ||
if err != nil { | ||
return fmt.Errorf("error getting payment state for initializing accountant: %w", err) | ||
} | ||
|
||
err = c.accountant.SetPaymentState(paymentState) | ||
if err != nil { | ||
return fmt.Errorf("error setting payment state for accountant: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
// Close closes the grpc connection to the disperser server. | ||
// It is thread safe and can be called multiple times. | ||
func (c *disperserClientV2) Close() error { | ||
|
@@ -108,16 +123,15 @@ func (c *disperserClientV2) DisperseBlob( | |
if c.signer == nil { | ||
return nil, [32]byte{}, api.NewErrorInternal("uninitialized signer for authenticated dispersal") | ||
} | ||
if c.accountant == nil { | ||
return nil, [32]byte{}, api.NewErrorInternal("uninitialized accountant for paid dispersal; make sure to call PopulateAccountant after creating the client") | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. check if |
||
var payment core.PaymentMetadata | ||
accountId, err := c.signer.GetAccountID() | ||
symbolLength := encoding.GetBlobLengthPowerOf2(uint(len(data))) | ||
payment, err := c.accountant.AccountBlob(ctx, uint64(symbolLength), quorums) | ||
if err != nil { | ||
return nil, [32]byte{}, api.NewErrorInvalidArg(fmt.Sprintf("please configure signer key if you want to use authenticated endpoint %v", err)) | ||
return nil, [32]byte{}, fmt.Errorf("error accounting blob: %w", err) | ||
} | ||
payment.AccountID = accountId | ||
// TODO: add payment metadata | ||
payment.BinIndex = 0 | ||
payment.CumulativePayment = big.NewInt(0) | ||
|
||
if len(quorums) == 0 { | ||
return nil, [32]byte{}, api.NewErrorInvalidArg("quorum numbers must be provided") | ||
|
@@ -160,7 +174,7 @@ func (c *disperserClientV2) DisperseBlob( | |
BlobVersion: blobVersion, | ||
BlobCommitments: blobCommitments, | ||
QuorumNumbers: quorums, | ||
PaymentMetadata: payment, | ||
PaymentMetadata: *payment, | ||
} | ||
sig, err := c.signer.SignBlobRequest(blobHeader) | ||
if err != nil { | ||
|
@@ -202,6 +216,30 @@ func (c *disperserClientV2) GetBlobStatus(ctx context.Context, blobKey corev2.Bl | |
return c.client.GetBlobStatus(ctx, request) | ||
} | ||
|
||
// GetPaymentState returns the payment state of the disperser client | ||
func (c *disperserClientV2) GetPaymentState(ctx context.Context) (*disperser_rpc.GetPaymentStateReply, error) { | ||
err := c.initOnceGrpcConnection() | ||
if err != nil { | ||
return nil, api.NewErrorInternal(err.Error()) | ||
} | ||
|
||
accountID, err := c.signer.GetAccountID() | ||
if err != nil { | ||
return nil, fmt.Errorf("error getting signer's account ID: %w", err) | ||
} | ||
|
||
signature, err := c.signer.SignPaymentStateRequest() | ||
if err != nil { | ||
return nil, fmt.Errorf("error signing payment state request: %w", err) | ||
} | ||
|
||
request := &disperser_rpc.GetPaymentStateRequest{ | ||
AccountId: accountID, | ||
Signature: signature, | ||
} | ||
return c.client.GetPaymentState(ctx, request) | ||
} | ||
|
||
// GetBlobCommitment is a utility method that calculates commitment for a blob payload. | ||
// While the blob commitment can be calculated by anyone, it requires SRS points to | ||
// be loaded. For service that does not have access to SRS points, this method can be | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about we make the call to PopulateAccountant as part of the initOnce function? This way its hidden from the user.
Is there ever a need to recall PopulateAccountant? For eg if disperser went down, or whatever other weird case? To reset the accountant basically? If so, should it be called
ResetAccountantFromServer
or something like that?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case, maybe we also move signer check to the initOnce?
There's some cases a user might recall populate accountant, I don't think it hurts to be able to reset. Can update the function name for sure
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'll leave this as a future refactoring