-
Notifications
You must be signed in to change notification settings - Fork 239
[v2] disperser client payments api #928
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
Changes from 2 commits
9982bf1
5004cf9
dd00e32
0d59ee9
cd908e1
6643ca9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,15 +8,15 @@ import ( | |
"sync" | ||
"time" | ||
|
||
commonpb "github.com/Layr-Labs/eigenda/api/grpc/common" | ||
disperser_rpc "github.com/Layr-Labs/eigenda/api/grpc/disperser/v2" | ||
"github.com/Layr-Labs/eigenda/core" | ||
"github.com/Layr-Labs/eigenda/core/meterer" | ||
) | ||
|
||
var requiredQuorums = []uint8{0, 1} | ||
|
||
type Accountant interface { | ||
AccountBlob(ctx context.Context, numSymbols uint64, quorums []uint8) (*commonpb.PaymentHeader, error) | ||
AccountBlob(ctx context.Context, numSymbols uint64, quorums []uint8) (*core.PaymentMetadata, error) | ||
} | ||
samlaf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
var _ Accountant = &accountant{} | ||
|
@@ -116,7 +116,7 @@ func (a *accountant) BlobPaymentInfo(ctx context.Context, numSymbols uint64, quo | |
} | ||
|
||
// AccountBlob accountant provides and records payment information | ||
func (a *accountant) AccountBlob(ctx context.Context, numSymbols uint64, quorums []uint8) (*commonpb.PaymentHeader, error) { | ||
func (a *accountant) AccountBlob(ctx context.Context, numSymbols uint64, quorums []uint8) (*core.PaymentMetadata, error) { | ||
binIndex, cumulativePayment, err := a.BlobPaymentInfo(ctx, numSymbols, quorums) | ||
if err != nil { | ||
return nil, err | ||
|
@@ -127,9 +127,8 @@ func (a *accountant) AccountBlob(ctx context.Context, numSymbols uint64, quorums | |
BinIndex: binIndex, | ||
CumulativePayment: cumulativePayment, | ||
} | ||
protoPaymentHeader := pm.ConvertToProtoPaymentHeader() | ||
|
||
return protoPaymentHeader, nil | ||
return pm, nil | ||
} | ||
|
||
// TODO: PaymentCharged and SymbolsCharged copied from meterer, should be refactored | ||
|
@@ -160,6 +159,37 @@ func (a *accountant) GetRelativeBinRecord(index uint32) *BinRecord { | |
return &a.binRecords[relativeIndex] | ||
} | ||
|
||
func (a *accountant) SetPaymentState(paymentState *disperser_rpc.GetPaymentStateReply) { | ||
a.minNumSymbols = uint32(paymentState.PaymentGlobalParams.MinNumSymbols) | ||
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. these will panic if any of the attributes are nil? 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. yes.... added a check for all of them! 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. If you use getters (i.e. |
||
|
||
a.onDemand.CumulativePayment = new(big.Int).SetBytes(paymentState.OnchainCumulativePayment) | ||
a.cumulativePayment = new(big.Int).SetBytes(paymentState.CumulativePayment) | ||
a.pricePerSymbol = uint32(paymentState.PaymentGlobalParams.PricePerSymbol) | ||
|
||
a.reservation.SymbolsPerSec = uint64(paymentState.PaymentGlobalParams.GlobalSymbolsPerSecond) | ||
a.reservation.StartTimestamp = uint64(paymentState.Reservation.StartTimestamp) | ||
a.reservation.EndTimestamp = uint64(paymentState.Reservation.EndTimestamp) | ||
a.reservationWindow = uint32(paymentState.PaymentGlobalParams.ReservationWindow) | ||
quorumNumbers := make([]uint8, len(paymentState.Reservation.QuorumNumbers)) | ||
for i, quorum := range paymentState.Reservation.QuorumNumbers { | ||
quorumNumbers[i] = uint8(quorum) | ||
} | ||
a.reservation.QuorumNumbers = quorumNumbers | ||
quorumSplit := make([]uint8, len(paymentState.Reservation.QuorumSplit)) | ||
for i, quorum := range paymentState.Reservation.QuorumSplit { | ||
quorumSplit[i] = uint8(quorum) | ||
} | ||
a.reservation.QuorumSplit = quorumSplit | ||
binRecords := make([]BinRecord, len(paymentState.BinRecords)) | ||
for i, record := range paymentState.BinRecords { | ||
binRecords[i] = BinRecord{ | ||
Index: record.Index, | ||
Usage: record.Usage, | ||
} | ||
} | ||
a.binRecords = binRecords | ||
} | ||
|
||
// QuorumCheck eagerly returns error if the check finds a quorum number not an element of the allowed quorum numbers | ||
func QuorumCheck(quorumNumbers []uint8, allowedNumbers []uint8) error { | ||
if len(quorumNumbers) == 0 { | ||
|
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,25 @@ 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. | ||
// This function is required to be called before using the accountant. Perhaps rename to Start()? | ||
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. This function isn't required if a valid accountant is passed in the constructor, right? 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. Oh yeah, will update the comment |
||
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) | ||
} | ||
c.accountant.SetPaymentState(paymentState) | ||
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 { | ||
|
@@ -109,15 +121,11 @@ func (c *disperserClientV2) DisperseBlob( | |
return nil, [32]byte{}, api.NewErrorInternal("uninitialized signer for authenticated dispersal") | ||
} | ||
|
||
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 +168,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 +210,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 | ||
|
Uh oh!
There was an error while loading. Please reload this page.