Skip to content

Commit 5004cf9

Browse files
committed
feat: getPaymentState and init accountant
1 parent 9982bf1 commit 5004cf9

File tree

3 files changed

+74
-2
lines changed

3 files changed

+74
-2
lines changed

api/clients/accountant.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"sync"
99
"time"
1010

11+
disperser_rpc "github.com/Layr-Labs/eigenda/api/grpc/disperser/v2"
1112
"github.com/Layr-Labs/eigenda/core"
1213
"github.com/Layr-Labs/eigenda/core/meterer"
1314
)
@@ -158,6 +159,37 @@ func (a *accountant) GetRelativeBinRecord(index uint32) *BinRecord {
158159
return &a.binRecords[relativeIndex]
159160
}
160161

162+
func (a *accountant) SetPaymentState(paymentState *disperser_rpc.GetPaymentStateReply) {
163+
a.minNumSymbols = uint32(paymentState.PaymentGlobalParams.MinNumSymbols)
164+
165+
a.onDemand.CumulativePayment = new(big.Int).SetBytes(paymentState.OnchainCumulativePayment)
166+
a.cumulativePayment = new(big.Int).SetBytes(paymentState.CumulativePayment)
167+
a.pricePerSymbol = uint32(paymentState.PaymentGlobalParams.PricePerSymbol)
168+
169+
a.reservation.SymbolsPerSec = uint64(paymentState.PaymentGlobalParams.GlobalSymbolsPerSecond)
170+
a.reservation.StartTimestamp = uint64(paymentState.Reservation.StartTimestamp)
171+
a.reservation.EndTimestamp = uint64(paymentState.Reservation.EndTimestamp)
172+
a.reservationWindow = uint32(paymentState.PaymentGlobalParams.ReservationWindow)
173+
quorumNumbers := make([]uint8, len(paymentState.Reservation.QuorumNumbers))
174+
for i, quorum := range paymentState.Reservation.QuorumNumbers {
175+
quorumNumbers[i] = uint8(quorum)
176+
}
177+
a.reservation.QuorumNumbers = quorumNumbers
178+
quorumSplit := make([]uint8, len(paymentState.Reservation.QuorumSplit))
179+
for i, quorum := range paymentState.Reservation.QuorumSplit {
180+
quorumSplit[i] = uint8(quorum)
181+
}
182+
a.reservation.QuorumSplit = quorumSplit
183+
binRecords := make([]BinRecord, len(paymentState.BinRecords))
184+
for i, record := range paymentState.BinRecords {
185+
binRecords[i] = BinRecord{
186+
Index: record.Index,
187+
Usage: record.Usage,
188+
}
189+
}
190+
a.binRecords = binRecords
191+
}
192+
161193
// QuorumCheck eagerly returns error if the check finds a quorum number not an element of the allowed quorum numbers
162194
func QuorumCheck(quorumNumbers []uint8, allowedNumbers []uint8) error {
163195
if len(quorumNumbers) == 0 {

api/clients/disperser_client_v2.go

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type disperserClientV2 struct {
3535
conn *grpc.ClientConn
3636
client disperser_rpc.DisperserClient
3737
prover encoding.Prover
38-
accountant Accountant
38+
accountant *accountant
3939
}
4040

4141
var _ DisperserClientV2 = &disperserClientV2{}
@@ -60,7 +60,7 @@ var _ DisperserClientV2 = &disperserClientV2{}
6060
//
6161
// // Subsequent calls will use the existing connection
6262
// status2, blobKey2, err := client.DisperseBlob(ctx, data, blobHeader)
63-
func NewDisperserClientV2(config *DisperserClientV2Config, signer corev2.BlobRequestSigner, prover encoding.Prover, accountant Accountant) (*disperserClientV2, error) {
63+
func NewDisperserClientV2(config *DisperserClientV2Config, signer corev2.BlobRequestSigner, prover encoding.Prover, accountant *accountant) (*disperserClientV2, error) {
6464
if config == nil {
6565
return nil, api.NewErrorInvalidArg("config must be provided")
6666
}
@@ -83,6 +83,17 @@ func NewDisperserClientV2(config *DisperserClientV2Config, signer corev2.BlobReq
8383
}, nil
8484
}
8585

86+
// PopulateAccountant populates the accountant with the payment state from the disperser.
87+
// This function is required to be called before using the accountant. Perhaps rename to Start()?
88+
func (c *disperserClientV2) PopulateAccountant(ctx context.Context) error {
89+
paymentState, err := c.GetPaymentState(ctx)
90+
if err != nil {
91+
return fmt.Errorf("error getting payment state for initializing accountant: %w", err)
92+
}
93+
c.accountant.SetPaymentState(paymentState)
94+
return nil
95+
}
96+
8697
// Close closes the grpc connection to the disperser server.
8798
// It is thread safe and can be called multiple times.
8899
func (c *disperserClientV2) Close() error {
@@ -199,6 +210,30 @@ func (c *disperserClientV2) GetBlobStatus(ctx context.Context, blobKey corev2.Bl
199210
return c.client.GetBlobStatus(ctx, request)
200211
}
201212

213+
// GetPaymentState returns the payment state of the disperser client
214+
func (c *disperserClientV2) GetPaymentState(ctx context.Context) (*disperser_rpc.GetPaymentStateReply, error) {
215+
err := c.initOnceGrpcConnection()
216+
if err != nil {
217+
return nil, api.NewErrorInternal(err.Error())
218+
}
219+
220+
accountID, err := c.signer.GetAccountID()
221+
if err != nil {
222+
return nil, fmt.Errorf("error getting signer's account ID: %w", err)
223+
}
224+
225+
signature, err := c.signer.SignPaymentStateRequest()
226+
if err != nil {
227+
return nil, fmt.Errorf("error signing payment state request: %w", err)
228+
}
229+
230+
request := &disperser_rpc.GetPaymentStateRequest{
231+
AccountId: accountID,
232+
Signature: signature,
233+
}
234+
return c.client.GetPaymentState(ctx, request)
235+
}
236+
202237
// GetBlobCommitment is a utility method that calculates commitment for a blob payload.
203238
// While the blob commitment can be calculated by anyone, it requires SRS points to
204239
// be loaded. For service that does not have access to SRS points, this method can be

disperser/apiserver/server_v2.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,10 @@ func (s *DispersalServerV2) GetPaymentState(ctx context.Context, req *pb.GetPaym
255255
for i, v := range reservation.QuorumNumbers {
256256
quorumNumbers[i] = uint32(v)
257257
}
258+
quorumSplit := make([]uint32, len(reservation.QuorumSplit))
259+
for i, v := range reservation.QuorumSplit {
260+
quorumSplit[i] = uint32(v)
261+
}
258262
// build reply
259263
reply := &pb.GetPaymentStateReply{
260264
PaymentGlobalParams: &paymentGlobalParams,
@@ -264,6 +268,7 @@ func (s *DispersalServerV2) GetPaymentState(ctx context.Context, req *pb.GetPaym
264268
StartTimestamp: uint32(reservation.StartTimestamp),
265269
EndTimestamp: uint32(reservation.EndTimestamp),
266270
QuorumNumbers: quorumNumbers,
271+
QuorumSplit: quorumSplit,
267272
},
268273
CumulativePayment: largestCumulativePayment.Bytes(),
269274
OnchainCumulativePayment: onDemandPayment.CumulativePayment.Bytes(),

0 commit comments

Comments
 (0)