Skip to content
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 server payments api #902

Merged
merged 18 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 11 additions & 17 deletions api/clients/accountant.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ import (
"github.com/Layr-Labs/eigenda/core/meterer"
)

var minNumBins uint32 = 3
var requiredQuorums = []uint8{0, 1}

type Accountant interface {
AccountBlob(ctx context.Context, numSymbols uint64, quorums []uint8) (*commonpb.PaymentHeader, []byte, error)
AccountBlob(ctx context.Context, numSymbols uint64, quorums []uint8) (*commonpb.PaymentHeader, error)
}

var _ Accountant = &accountant{}

type accountant struct {
// on-chain states
accountID string
reservation *core.ActiveReservation
onDemand *core.OnDemandPayment
reservationWindow uint32
Expand All @@ -36,16 +36,16 @@ type accountant struct {
usageLock sync.Mutex
cumulativePayment *big.Int

paymentSigner core.PaymentSigner
numBins uint32
// number of bins in the circular accounting, restricted by minNumBins which is 3
numBins uint32
ian-shim marked this conversation as resolved.
Show resolved Hide resolved
}

type BinRecord struct {
Index uint32
Usage uint64
}

func NewAccountant(reservation *core.ActiveReservation, onDemand *core.OnDemandPayment, reservationWindow uint32, pricePerSymbol uint32, minNumSymbols uint32, paymentSigner core.PaymentSigner, numBins uint32) *accountant {
func NewAccountant(accountID string, reservation *core.ActiveReservation, onDemand *core.OnDemandPayment, reservationWindow uint32, pricePerSymbol uint32, minNumSymbols uint32, numBins uint32) *accountant {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are reservationWindow, pricePerSymbol, minNumSymbols, numBins different for each account?
Aren't these globally available somewhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reservationWindow, pricePerSymbol, minNumSymbols should be set through the contract. I linked a PR in the previous comment about this. numBins can be set by the accountants preferences, doesn't really matter but restricted by a minimum of 3

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in which case does it make sense to account for more than 3 bins?
I'm wondering if we should fix/hardcode this number everywhere vs. making it a variable

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a nice functionality considered for disperser clients who want to track more history in-memory. The data isn't really used and not critical

//TODO: client storage; currently every instance starts fresh but on-chain or a small store makes more sense
// Also client is currently responsible for supplying network params, we need to add RPC in order to be automatic
// There's a subsequent PR that handles populating the accountant with on-chain state from the disperser
Expand All @@ -54,15 +54,15 @@ func NewAccountant(reservation *core.ActiveReservation, onDemand *core.OnDemandP
binRecords[i] = BinRecord{Index: uint32(i), Usage: 0}
}
a := accountant{
accountID: accountID,
reservation: reservation,
onDemand: onDemand,
reservationWindow: reservationWindow,
ian-shim marked this conversation as resolved.
Show resolved Hide resolved
pricePerSymbol: pricePerSymbol,
minNumSymbols: minNumSymbols,
binRecords: binRecords,
cumulativePayment: big.NewInt(0),
paymentSigner: paymentSigner,
numBins: max(numBins, minNumBins),
numBins: max(numBins, uint32(meterer.MinNumBins)),
}
// TODO: add a routine to refresh the on-chain state occasionally?
ian-shim marked this conversation as resolved.
Show resolved Hide resolved
return &a
Expand Down Expand Up @@ -116,26 +116,20 @@ 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, []byte, error) {
func (a *accountant) AccountBlob(ctx context.Context, numSymbols uint64, quorums []uint8) (*commonpb.PaymentHeader, error) {
binIndex, cumulativePayment, err := a.BlobPaymentInfo(ctx, numSymbols, quorums)
if err != nil {
return nil, nil, err
return nil, err
}

accountID := a.paymentSigner.GetAccountID()
pm := &core.PaymentMetadata{
AccountID: accountID,
AccountID: a.accountID,
BinIndex: binIndex,
CumulativePayment: cumulativePayment,
}
protoPaymentHeader := pm.ConvertToProtoPaymentHeader()

signature, err := a.paymentSigner.SignBlobPayment(pm)
if err != nil {
return nil, nil, err
}

return protoPaymentHeader, signature, nil
return protoPaymentHeader, nil
}

// TODO: PaymentCharged and SymbolsCharged copied from meterer, should be refactored
Expand Down
86 changes: 39 additions & 47 deletions api/clients/accountant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"time"

"github.com/Layr-Labs/eigenda/core"
"github.com/Layr-Labs/eigenda/core/auth"
"github.com/Layr-Labs/eigenda/core/meterer"
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/assert"
Expand All @@ -34,9 +33,8 @@ func TestNewAccountant(t *testing.T) {

privateKey1, err := crypto.GenerateKey()
assert.NoError(t, err)
paymentSigner, err := auth.NewPaymentSigner(hex.EncodeToString(privateKey1.D.Bytes()))
assert.NoError(t, err)
accountant := NewAccountant(reservation, onDemand, reservationWindow, pricePerSymbol, minNumSymbols, paymentSigner, numBins)
accountId := hex.EncodeToString(privateKey1.D.Bytes())
accountant := NewAccountant(accountId, reservation, onDemand, reservationWindow, pricePerSymbol, minNumSymbols, numBins)

assert.NotNil(t, accountant)
assert.Equal(t, reservation, accountant.reservation)
Expand Down Expand Up @@ -65,15 +63,14 @@ func TestAccountBlob_Reservation(t *testing.T) {

privateKey1, err := crypto.GenerateKey()
assert.NoError(t, err)
paymentSigner, err := auth.NewPaymentSigner(hex.EncodeToString(privateKey1.D.Bytes()))
assert.NoError(t, err)
accountant := NewAccountant(reservation, onDemand, reservationWindow, pricePerSymbol, minNumSymbols, paymentSigner, numBins)
accountId := hex.EncodeToString(privateKey1.D.Bytes())
accountant := NewAccountant(accountId, reservation, onDemand, reservationWindow, pricePerSymbol, minNumSymbols, numBins)

ctx := context.Background()
symbolLength := uint64(500)
quorums := []uint8{0, 1}

header, _, err := accountant.AccountBlob(ctx, symbolLength, quorums)
header, err := accountant.AccountBlob(ctx, symbolLength, quorums)
metadata := core.ConvertPaymentHeader(header)

assert.NoError(t, err)
Expand All @@ -83,7 +80,7 @@ func TestAccountBlob_Reservation(t *testing.T) {

symbolLength = uint64(700)

header, _, err = accountant.AccountBlob(ctx, symbolLength, quorums)
header, err = accountant.AccountBlob(ctx, symbolLength, quorums)
metadata = core.ConvertPaymentHeader(header)

assert.NoError(t, err)
Expand All @@ -92,7 +89,7 @@ func TestAccountBlob_Reservation(t *testing.T) {
assert.Equal(t, isRotation([]uint64{1200, 0, 200}, mapRecordUsage(accountant.binRecords)), true)

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

assert.NoError(t, err)
Expand All @@ -117,15 +114,14 @@ func TestAccountBlob_OnDemand(t *testing.T) {

privateKey1, err := crypto.GenerateKey()
assert.NoError(t, err)
paymentSigner, err := auth.NewPaymentSigner(hex.EncodeToString(privateKey1.D.Bytes()))
assert.NoError(t, err)
accountant := NewAccountant(reservation, onDemand, reservationWindow, pricePerSymbol, minNumSymbols, paymentSigner, numBins)
accountId := hex.EncodeToString(privateKey1.D.Bytes())
accountant := NewAccountant(accountId, reservation, onDemand, reservationWindow, pricePerSymbol, minNumSymbols, numBins)

ctx := context.Background()
numSymbols := uint64(1500)
quorums := []uint8{0, 1}

header, _, err := accountant.AccountBlob(ctx, numSymbols, quorums)
header, err := accountant.AccountBlob(ctx, numSymbols, quorums)
assert.NoError(t, err)

metadata := core.ConvertPaymentHeader(header)
Expand All @@ -147,15 +143,14 @@ func TestAccountBlob_InsufficientOnDemand(t *testing.T) {

privateKey1, err := crypto.GenerateKey()
assert.NoError(t, err)
paymentSigner, err := auth.NewPaymentSigner(hex.EncodeToString(privateKey1.D.Bytes()))
assert.NoError(t, err)
accountant := NewAccountant(reservation, onDemand, reservationWindow, pricePerSymbol, minNumSymbols, paymentSigner, numBins)
accountId := hex.EncodeToString(privateKey1.D.Bytes())
accountant := NewAccountant(accountId, reservation, onDemand, reservationWindow, pricePerSymbol, minNumSymbols, numBins)

ctx := context.Background()
numSymbols := uint64(2000)
quorums := []uint8{0, 1}

_, _, err = accountant.AccountBlob(ctx, numSymbols, quorums)
_, err = accountant.AccountBlob(ctx, numSymbols, quorums)
assert.Contains(t, err.Error(), "neither reservation nor on-demand payment is available")
}

Expand All @@ -176,37 +171,36 @@ func TestAccountBlobCallSeries(t *testing.T) {

privateKey1, err := crypto.GenerateKey()
assert.NoError(t, err)
paymentSigner, err := auth.NewPaymentSigner(hex.EncodeToString(privateKey1.D.Bytes()))
assert.NoError(t, err)
accountant := NewAccountant(reservation, onDemand, reservationWindow, pricePerSymbol, minNumSymbols, paymentSigner, numBins)
accountId := hex.EncodeToString(privateKey1.D.Bytes())
accountant := NewAccountant(accountId, reservation, onDemand, reservationWindow, pricePerSymbol, minNumSymbols, numBins)

ctx := context.Background()
quorums := []uint8{0, 1}
now := time.Now().Unix()

// First call: Use reservation
header, _, err := accountant.AccountBlob(ctx, 800, quorums)
header, err := accountant.AccountBlob(ctx, 800, quorums)
metadata := core.ConvertPaymentHeader(header)
assert.NoError(t, err)
assert.Equal(t, meterer.GetBinIndex(uint64(now), reservationWindow), header.BinIndex)
assert.Equal(t, big.NewInt(0), metadata.CumulativePayment)

// Second call: Use remaining reservation + overflow
header, _, err = accountant.AccountBlob(ctx, 300, quorums)
header, err = accountant.AccountBlob(ctx, 300, quorums)
metadata = core.ConvertPaymentHeader(header)
assert.NoError(t, err)
assert.Equal(t, meterer.GetBinIndex(uint64(now), reservationWindow), header.BinIndex)
assert.Equal(t, big.NewInt(0), metadata.CumulativePayment)

// Third call: Use on-demand
header, _, err = accountant.AccountBlob(ctx, 500, quorums)
header, err = accountant.AccountBlob(ctx, 500, quorums)
metadata = core.ConvertPaymentHeader(header)
assert.NoError(t, err)
assert.Equal(t, uint32(0), header.BinIndex)
assert.Equal(t, big.NewInt(500), metadata.CumulativePayment)

// Fourth call: Insufficient on-demand
_, _, err = accountant.AccountBlob(ctx, 600, quorums)
_, err = accountant.AccountBlob(ctx, 600, quorums)
assert.Error(t, err)
assert.Contains(t, err.Error(), "neither reservation nor on-demand payment is available")
}
Expand All @@ -225,30 +219,30 @@ func TestAccountBlob_BinRotation(t *testing.T) {
reservationWindow := uint32(1) // Set to 1 second for testing
pricePerSymbol := uint32(1)
minNumSymbols := uint32(100)

privateKey1, err := crypto.GenerateKey()
assert.NoError(t, err)
paymentSigner, err := auth.NewPaymentSigner(hex.EncodeToString(privateKey1.D.Bytes()))
assert.NoError(t, err)
accountant := NewAccountant(reservation, onDemand, reservationWindow, pricePerSymbol, minNumSymbols, paymentSigner, numBins)
accountId := hex.EncodeToString(privateKey1.D.Bytes())
accountant := NewAccountant(accountId, reservation, onDemand, reservationWindow, pricePerSymbol, minNumSymbols, numBins)

ctx := context.Background()
quorums := []uint8{0, 1}

// First call
_, _, err = accountant.AccountBlob(ctx, 800, quorums)
_, err = accountant.AccountBlob(ctx, 800, quorums)
assert.NoError(t, err)
assert.Equal(t, isRotation([]uint64{800, 0, 0}, mapRecordUsage(accountant.binRecords)), true)

// next reservation duration
time.Sleep(1000 * time.Millisecond)

// Second call
_, _, err = accountant.AccountBlob(ctx, 300, quorums)
_, err = accountant.AccountBlob(ctx, 300, quorums)
assert.NoError(t, err)
assert.Equal(t, isRotation([]uint64{800, 300, 0}, mapRecordUsage(accountant.binRecords)), true)

// Third call
_, _, err = accountant.AccountBlob(ctx, 500, quorums)
_, err = accountant.AccountBlob(ctx, 500, quorums)
assert.NoError(t, err)
assert.Equal(t, isRotation([]uint64{800, 800, 0}, mapRecordUsage(accountant.binRecords)), true)
}
Expand All @@ -270,9 +264,8 @@ func TestConcurrentBinRotationAndAccountBlob(t *testing.T) {

privateKey1, err := crypto.GenerateKey()
assert.NoError(t, err)
paymentSigner, err := auth.NewPaymentSigner(hex.EncodeToString(privateKey1.D.Bytes()))
assert.NoError(t, err)
accountant := NewAccountant(reservation, onDemand, reservationWindow, pricePerSymbol, minNumSymbols, paymentSigner, numBins)
accountId := hex.EncodeToString(privateKey1.D.Bytes())
accountant := NewAccountant(accountId, reservation, onDemand, reservationWindow, pricePerSymbol, minNumSymbols, numBins)

ctx := context.Background()
quorums := []uint8{0, 1}
Expand All @@ -285,7 +278,7 @@ func TestConcurrentBinRotationAndAccountBlob(t *testing.T) {
defer wg.Done()
// for j := 0; j < 5; j++ {
// fmt.Println("request ", i)
_, _, err := accountant.AccountBlob(ctx, 100, quorums)
_, err := accountant.AccountBlob(ctx, 100, quorums)
assert.NoError(t, err)
time.Sleep(500 * time.Millisecond)
// }
Expand Down Expand Up @@ -317,30 +310,30 @@ func TestAccountBlob_ReservationWithOneOverflow(t *testing.T) {

privateKey1, err := crypto.GenerateKey()
assert.NoError(t, err)
paymentSigner, err := auth.NewPaymentSigner(hex.EncodeToString(privateKey1.D.Bytes()))
assert.NoError(t, err)
accountant := NewAccountant(reservation, onDemand, reservationWindow, pricePerSymbol, minNumSymbols, paymentSigner, numBins)
accountId := hex.EncodeToString(privateKey1.D.Bytes())
accountant := NewAccountant(accountId, reservation, onDemand, reservationWindow, pricePerSymbol, minNumSymbols, numBins)

ctx := context.Background()
quorums := []uint8{0, 1}
now := time.Now().Unix()

// Okay reservation
header, _, err := accountant.AccountBlob(ctx, 800, quorums)
header, err := accountant.AccountBlob(ctx, 800, quorums)
assert.NoError(t, err)
assert.Equal(t, meterer.GetBinIndex(uint64(now), reservationWindow), header.BinIndex)
metadata := core.ConvertPaymentHeader(header)
assert.Equal(t, big.NewInt(0), metadata.CumulativePayment)
assert.Equal(t, isRotation([]uint64{800, 0, 0}, mapRecordUsage(accountant.binRecords)), true)

// Second call: Allow one overflow
header, _, err = accountant.AccountBlob(ctx, 500, quorums)
header, err = accountant.AccountBlob(ctx, 500, quorums)
assert.NoError(t, err)
metadata = core.ConvertPaymentHeader(header)
assert.Equal(t, big.NewInt(0), metadata.CumulativePayment)
assert.Equal(t, isRotation([]uint64{1300, 0, 300}, mapRecordUsage(accountant.binRecords)), true)

// Third call: Should use on-demand payment
header, _, err = accountant.AccountBlob(ctx, 200, quorums)
header, err = accountant.AccountBlob(ctx, 200, quorums)
assert.NoError(t, err)
assert.Equal(t, uint32(0), header.BinIndex)
metadata = core.ConvertPaymentHeader(header)
Expand All @@ -365,20 +358,19 @@ func TestAccountBlob_ReservationOverflowReset(t *testing.T) {

privateKey1, err := crypto.GenerateKey()
assert.NoError(t, err)
paymentSigner, err := auth.NewPaymentSigner(hex.EncodeToString(privateKey1.D.Bytes()))
assert.NoError(t, err)
accountant := NewAccountant(reservation, onDemand, reservationWindow, pricePerSymbol, minNumSymbols, paymentSigner, numBins)
accountId := hex.EncodeToString(privateKey1.D.Bytes())
accountant := NewAccountant(accountId, reservation, onDemand, reservationWindow, pricePerSymbol, minNumSymbols, numBins)

ctx := context.Background()
quorums := []uint8{0, 1}

// full reservation
_, _, err = accountant.AccountBlob(ctx, 1000, quorums)
_, err = accountant.AccountBlob(ctx, 1000, quorums)
assert.NoError(t, err)
assert.Equal(t, isRotation([]uint64{1000, 0, 0}, mapRecordUsage(accountant.binRecords)), true)

// no overflow
header, _, err := accountant.AccountBlob(ctx, 500, quorums)
header, err := accountant.AccountBlob(ctx, 500, quorums)
assert.NoError(t, err)
assert.Equal(t, isRotation([]uint64{1000, 0, 0}, mapRecordUsage(accountant.binRecords)), true)
metadata := core.ConvertPaymentHeader(header)
Expand All @@ -388,7 +380,7 @@ func TestAccountBlob_ReservationOverflowReset(t *testing.T) {
time.Sleep(time.Duration(reservationWindow) * time.Second)

// Third call: Should use new bin and allow overflow again
_, _, err = accountant.AccountBlob(ctx, 500, quorums)
_, err = accountant.AccountBlob(ctx, 500, quorums)
assert.NoError(t, err)
assert.Equal(t, isRotation([]uint64{1000, 500, 0}, mapRecordUsage(accountant.binRecords)), true)
}
Expand Down
Loading
Loading