Skip to content

Commit 0b944cf

Browse files
committed
feat: client construct payment header
1 parent 1ae3e3c commit 0b944cf

File tree

3 files changed

+30
-46
lines changed

3 files changed

+30
-46
lines changed

api/clients/accountant.go

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

11-
commonpb "github.com/Layr-Labs/eigenda/api/grpc/common"
1211
"github.com/Layr-Labs/eigenda/core"
1312
"github.com/Layr-Labs/eigenda/core/meterer"
1413
)
@@ -17,7 +16,7 @@ var minNumBins uint32 = 3
1716
var requiredQuorums = []uint8{0, 1}
1817

1918
type Accountant interface {
20-
AccountBlob(ctx context.Context, numSymbols uint64, quorums []uint8) (*commonpb.PaymentHeader, error)
19+
AccountBlob(ctx context.Context, numSymbols uint64, quorums []uint8) (*core.PaymentMetadata, error)
2120
}
2221

2322
var _ Accountant = &accountant{}
@@ -116,7 +115,7 @@ func (a *accountant) BlobPaymentInfo(ctx context.Context, numSymbols uint64, quo
116115
}
117116

118117
// AccountBlob accountant provides and records payment information
119-
func (a *accountant) AccountBlob(ctx context.Context, numSymbols uint64, quorums []uint8) (*commonpb.PaymentHeader, error) {
118+
func (a *accountant) AccountBlob(ctx context.Context, numSymbols uint64, quorums []uint8) (*core.PaymentMetadata, error) {
120119
binIndex, cumulativePayment, err := a.BlobPaymentInfo(ctx, numSymbols, quorums)
121120
if err != nil {
122121
return nil, err
@@ -127,9 +126,8 @@ func (a *accountant) AccountBlob(ctx context.Context, numSymbols uint64, quorums
127126
BinIndex: binIndex,
128127
CumulativePayment: cumulativePayment,
129128
}
130-
protoPaymentHeader := pm.ConvertToProtoPaymentHeader()
131129

132-
return protoPaymentHeader, nil
130+
return pm, nil
133131
}
134132

135133
// TODO: PaymentCharged and SymbolsCharged copied from meterer, should be refactored

api/clients/accountant_test.go

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -71,30 +71,27 @@ func TestAccountBlob_Reservation(t *testing.T) {
7171
quorums := []uint8{0, 1}
7272

7373
header, err := accountant.AccountBlob(ctx, symbolLength, quorums)
74-
metadata := core.ConvertPaymentHeader(header)
7574

7675
assert.NoError(t, err)
7776
assert.Equal(t, meterer.GetBinIndex(uint64(time.Now().Unix()), reservationWindow), header.BinIndex)
78-
assert.Equal(t, big.NewInt(0), metadata.CumulativePayment)
77+
assert.Equal(t, big.NewInt(0), header.CumulativePayment)
7978
assert.Equal(t, isRotation([]uint64{500, 0, 0}, mapRecordUsage(accountant.binRecords)), true)
8079

8180
symbolLength = uint64(700)
8281

8382
header, err = accountant.AccountBlob(ctx, symbolLength, quorums)
84-
metadata = core.ConvertPaymentHeader(header)
8583

8684
assert.NoError(t, err)
8785
assert.NotEqual(t, 0, header.BinIndex)
88-
assert.Equal(t, big.NewInt(0), metadata.CumulativePayment)
86+
assert.Equal(t, big.NewInt(0), header.CumulativePayment)
8987
assert.Equal(t, isRotation([]uint64{1200, 0, 200}, mapRecordUsage(accountant.binRecords)), true)
9088

9189
// Second call should use on-demand payment
9290
header, err = accountant.AccountBlob(ctx, 300, quorums)
93-
metadata = core.ConvertPaymentHeader(header)
9491

9592
assert.NoError(t, err)
9693
assert.Equal(t, uint32(0), header.BinIndex)
97-
assert.Equal(t, big.NewInt(300), metadata.CumulativePayment)
94+
assert.Equal(t, big.NewInt(300), header.CumulativePayment)
9895
}
9996

10097
func TestAccountBlob_OnDemand(t *testing.T) {
@@ -124,10 +121,9 @@ func TestAccountBlob_OnDemand(t *testing.T) {
124121
header, err := accountant.AccountBlob(ctx, numSymbols, quorums)
125122
assert.NoError(t, err)
126123

127-
metadata := core.ConvertPaymentHeader(header)
128124
expectedPayment := big.NewInt(int64(numSymbols * uint64(pricePerSymbol)))
129125
assert.Equal(t, uint32(0), header.BinIndex)
130-
assert.Equal(t, expectedPayment, metadata.CumulativePayment)
126+
assert.Equal(t, expectedPayment, header.CumulativePayment)
131127
assert.Equal(t, isRotation([]uint64{0, 0, 0}, mapRecordUsage(accountant.binRecords)), true)
132128
assert.Equal(t, expectedPayment, accountant.cumulativePayment)
133129
}
@@ -180,24 +176,21 @@ func TestAccountBlobCallSeries(t *testing.T) {
180176

181177
// First call: Use reservation
182178
header, err := accountant.AccountBlob(ctx, 800, quorums)
183-
metadata := core.ConvertPaymentHeader(header)
184179
assert.NoError(t, err)
185180
assert.Equal(t, meterer.GetBinIndex(uint64(now), reservationWindow), header.BinIndex)
186-
assert.Equal(t, big.NewInt(0), metadata.CumulativePayment)
181+
assert.Equal(t, big.NewInt(0), header.CumulativePayment)
187182

188183
// Second call: Use remaining reservation + overflow
189184
header, err = accountant.AccountBlob(ctx, 300, quorums)
190-
metadata = core.ConvertPaymentHeader(header)
191185
assert.NoError(t, err)
192186
assert.Equal(t, meterer.GetBinIndex(uint64(now), reservationWindow), header.BinIndex)
193-
assert.Equal(t, big.NewInt(0), metadata.CumulativePayment)
187+
assert.Equal(t, big.NewInt(0), header.CumulativePayment)
194188

195189
// Third call: Use on-demand
196190
header, err = accountant.AccountBlob(ctx, 500, quorums)
197-
metadata = core.ConvertPaymentHeader(header)
198191
assert.NoError(t, err)
199192
assert.Equal(t, uint32(0), header.BinIndex)
200-
assert.Equal(t, big.NewInt(500), metadata.CumulativePayment)
193+
assert.Equal(t, big.NewInt(500), header.CumulativePayment)
201194

202195
// Fourth call: Insufficient on-demand
203196
_, err = accountant.AccountBlob(ctx, 600, quorums)
@@ -321,23 +314,20 @@ func TestAccountBlob_ReservationWithOneOverflow(t *testing.T) {
321314
header, err := accountant.AccountBlob(ctx, 800, quorums)
322315
assert.NoError(t, err)
323316
assert.Equal(t, meterer.GetBinIndex(uint64(now), reservationWindow), header.BinIndex)
324-
metadata := core.ConvertPaymentHeader(header)
325-
assert.Equal(t, big.NewInt(0), metadata.CumulativePayment)
317+
assert.Equal(t, big.NewInt(0), header.CumulativePayment)
326318
assert.Equal(t, isRotation([]uint64{800, 0, 0}, mapRecordUsage(accountant.binRecords)), true)
327319

328320
// Second call: Allow one overflow
329321
header, err = accountant.AccountBlob(ctx, 500, quorums)
330322
assert.NoError(t, err)
331-
metadata = core.ConvertPaymentHeader(header)
332-
assert.Equal(t, big.NewInt(0), metadata.CumulativePayment)
323+
assert.Equal(t, big.NewInt(0), header.CumulativePayment)
333324
assert.Equal(t, isRotation([]uint64{1300, 0, 300}, mapRecordUsage(accountant.binRecords)), true)
334325

335326
// Third call: Should use on-demand payment
336327
header, err = accountant.AccountBlob(ctx, 200, quorums)
337328
assert.NoError(t, err)
338329
assert.Equal(t, uint32(0), header.BinIndex)
339-
metadata = core.ConvertPaymentHeader(header)
340-
assert.Equal(t, big.NewInt(200), metadata.CumulativePayment)
330+
assert.Equal(t, big.NewInt(200), header.CumulativePayment)
341331
assert.Equal(t, isRotation([]uint64{1300, 0, 300}, mapRecordUsage(accountant.binRecords)), true)
342332
}
343333

@@ -373,8 +363,7 @@ func TestAccountBlob_ReservationOverflowReset(t *testing.T) {
373363
header, err := accountant.AccountBlob(ctx, 500, quorums)
374364
assert.NoError(t, err)
375365
assert.Equal(t, isRotation([]uint64{1000, 0, 0}, mapRecordUsage(accountant.binRecords)), true)
376-
metadata := core.ConvertPaymentHeader(header)
377-
assert.Equal(t, big.NewInt(500), metadata.CumulativePayment)
366+
assert.Equal(t, big.NewInt(500), header.CumulativePayment)
378367

379368
// Wait for next reservation duration
380369
time.Sleep(time.Duration(reservationWindow) * time.Second)

api/clients/disperser_client_v2.go

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package clients
33
import (
44
"context"
55
"fmt"
6-
"math/big"
76
"sync"
87

98
"github.com/Layr-Labs/eigenda/api"
@@ -30,12 +29,13 @@ type DisperserClientV2 interface {
3029
}
3130

3231
type disperserClientV2 struct {
33-
config *DisperserClientV2Config
34-
signer corev2.BlobRequestSigner
35-
initOnce sync.Once
36-
conn *grpc.ClientConn
37-
client disperser_rpc.DisperserClient
38-
prover encoding.Prover
32+
config *DisperserClientV2Config
33+
signer corev2.BlobRequestSigner
34+
initOnce sync.Once
35+
conn *grpc.ClientConn
36+
client disperser_rpc.DisperserClient
37+
prover encoding.Prover
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) (*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
}
@@ -75,9 +75,10 @@ func NewDisperserClientV2(config *DisperserClientV2Config, signer corev2.BlobReq
7575
}
7676

7777
return &disperserClientV2{
78-
config: config,
79-
signer: signer,
80-
prover: prover,
78+
config: config,
79+
signer: signer,
80+
prover: prover,
81+
accountant: accountant,
8182
// conn and client are initialized lazily
8283
}, nil
8384
}
@@ -109,15 +110,11 @@ func (c *disperserClientV2) DisperseBlob(
109110
return nil, [32]byte{}, api.NewErrorInternal("uninitialized signer for authenticated dispersal")
110111
}
111112

112-
var payment core.PaymentMetadata
113-
accountId, err := c.signer.GetAccountID()
113+
symbolLength := encoding.GetBlobLengthPowerOf2(uint(len(data)))
114+
payment, err := c.accountant.AccountBlob(ctx, uint64(symbolLength), quorums)
114115
if err != nil {
115-
return nil, [32]byte{}, api.NewErrorInvalidArg(fmt.Sprintf("please configure signer key if you want to use authenticated endpoint %v", err))
116+
return nil, [32]byte{}, fmt.Errorf("error accounting blob: %w", err)
116117
}
117-
payment.AccountID = accountId
118-
// TODO: add payment metadata
119-
payment.BinIndex = 0
120-
payment.CumulativePayment = big.NewInt(0)
121118

122119
if len(quorums) == 0 {
123120
return nil, [32]byte{}, api.NewErrorInvalidArg("quorum numbers must be provided")
@@ -160,7 +157,7 @@ func (c *disperserClientV2) DisperseBlob(
160157
BlobVersion: blobVersion,
161158
BlobCommitments: blobCommitments,
162159
QuorumNumbers: quorums,
163-
PaymentMetadata: payment,
160+
PaymentMetadata: *payment,
164161
}
165162
sig, err := c.signer.SignBlobRequest(blobHeader)
166163
if err != nil {

0 commit comments

Comments
 (0)