Skip to content

Commit 0f1ddcc

Browse files
committed
feat: getPaymentState and init accountant
1 parent 9982bf1 commit 0f1ddcc

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-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: 39 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) (*disperserClientV2, error) {
6464
if config == nil {
6565
return nil, api.NewErrorInvalidArg("config must be provided")
6666
}
@@ -74,6 +74,8 @@ func NewDisperserClientV2(config *DisperserClientV2Config, signer corev2.BlobReq
7474
return nil, api.NewErrorInvalidArg("signer must be provided")
7575
}
7676

77+
accountant := &accountant{}
78+
7779
return &disperserClientV2{
7880
config: config,
7981
signer: signer,
@@ -83,6 +85,17 @@ func NewDisperserClientV2(config *DisperserClientV2Config, signer corev2.BlobReq
8385
}, nil
8486
}
8587

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

215+
// GetPaymentState returns the payment state of the disperser client
216+
func (c *disperserClientV2) GetPaymentState(ctx context.Context) (*disperser_rpc.GetPaymentStateReply, error) {
217+
err := c.initOnceGrpcConnection()
218+
if err != nil {
219+
return nil, api.NewErrorInternal(err.Error())
220+
}
221+
222+
accountID, err := c.signer.GetAccountID()
223+
if err != nil {
224+
return nil, fmt.Errorf("error getting signer's account ID: %w", err)
225+
}
226+
227+
signature, err := c.signer.SignPaymentStateRequest()
228+
if err != nil {
229+
return nil, fmt.Errorf("error signing payment state request: %w", err)
230+
}
231+
232+
request := &disperser_rpc.GetPaymentStateRequest{
233+
AccountId: accountID,
234+
Signature: signature,
235+
}
236+
return c.client.GetPaymentState(ctx, request)
237+
}
238+
202239
// GetBlobCommitment is a utility method that calculates commitment for a blob payload.
203240
// While the blob commitment can be calculated by anyone, it requires SRS points to
204241
// 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)