Skip to content

Commit

Permalink
fix: proto, contracts, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hopeyen committed Nov 1, 2024
1 parent 61a4c3b commit 3dfb7c9
Show file tree
Hide file tree
Showing 19 changed files with 98 additions and 704 deletions.
6 changes: 5 additions & 1 deletion api/clients/accountant.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,17 @@ func NewAccountant(reservation *core.ActiveReservation, onDemand *core.OnDemandP
//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
binRecords := make([]BinRecord, numBins)
for i := range binRecords {
binRecords[i] = BinRecord{Index: uint32(i), Usage: 0}
}
a := accountant{
reservation: reservation,
onDemand: onDemand,
reservationWindow: reservationWindow,
pricePerSymbol: pricePerSymbol,
minNumSymbols: minNumSymbols,
binRecords: []BinRecord{{Index: 0, Usage: 0}, {Index: 1, Usage: 0}, {Index: 2, Usage: 0}},
binRecords: binRecords,
cumulativePayment: big.NewInt(0),
paymentSigner: paymentSigner,
numBins: max(numBins, minNumBins),
Expand Down
91 changes: 49 additions & 42 deletions api/clients/accountant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package clients
import (
"context"
"encoding/hex"
"fmt"
"math/big"
"sync"
"testing"
Expand All @@ -19,14 +18,14 @@ import (
const numBins = uint32(3)

func TestNewAccountant(t *testing.T) {
reservation := core.ActiveReservation{
reservation := &core.ActiveReservation{
SymbolsPerSec: 100,
StartTimestamp: 100,
EndTimestamp: 200,
QuorumSplit: []byte{50, 50},
QuorumNumbers: []uint8{0, 1},
}
onDemand := core.OnDemandPayment{
onDemand := &core.OnDemandPayment{
CumulativePayment: big.NewInt(500),
}
reservationWindow := uint32(6)
Expand All @@ -35,8 +34,9 @@ func TestNewAccountant(t *testing.T) {

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

assert.NotNil(t, accountant)
assert.Equal(t, reservation, accountant.reservation)
Expand All @@ -49,14 +49,14 @@ func TestNewAccountant(t *testing.T) {
}

func TestAccountBlob_Reservation(t *testing.T) {
reservation := core.ActiveReservation{
reservation := &core.ActiveReservation{
SymbolsPerSec: 200,
StartTimestamp: 100,
EndTimestamp: 200,
QuorumSplit: []byte{50, 50},
QuorumNumbers: []uint8{0, 1},
}
onDemand := core.OnDemandPayment{
onDemand := &core.OnDemandPayment{
CumulativePayment: big.NewInt(500),
}
reservationWindow := uint32(5)
Expand All @@ -65,8 +65,9 @@ func TestAccountBlob_Reservation(t *testing.T) {

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

ctx := context.Background()
symbolLength := uint64(500)
Expand Down Expand Up @@ -100,14 +101,14 @@ func TestAccountBlob_Reservation(t *testing.T) {
}

func TestAccountBlob_OnDemand(t *testing.T) {
reservation := core.ActiveReservation{
reservation := &core.ActiveReservation{
SymbolsPerSec: 200,
StartTimestamp: 100,
EndTimestamp: 200,
QuorumSplit: []byte{50, 50},
QuorumNumbers: []uint8{0, 1},
}
onDemand := core.OnDemandPayment{
onDemand := &core.OnDemandPayment{
CumulativePayment: big.NewInt(1500),
}
reservationWindow := uint32(5)
Expand All @@ -116,8 +117,9 @@ func TestAccountBlob_OnDemand(t *testing.T) {

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

ctx := context.Background()
numSymbols := uint64(1500)
Expand All @@ -135,8 +137,8 @@ func TestAccountBlob_OnDemand(t *testing.T) {
}

func TestAccountBlob_InsufficientOnDemand(t *testing.T) {
reservation := core.ActiveReservation{}
onDemand := core.OnDemandPayment{
reservation := &core.ActiveReservation{}
onDemand := &core.OnDemandPayment{
CumulativePayment: big.NewInt(500),
}
reservationWindow := uint32(60)
Expand All @@ -145,8 +147,9 @@ func TestAccountBlob_InsufficientOnDemand(t *testing.T) {

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

ctx := context.Background()
numSymbols := uint64(2000)
Expand All @@ -157,14 +160,14 @@ func TestAccountBlob_InsufficientOnDemand(t *testing.T) {
}

func TestAccountBlobCallSeries(t *testing.T) {
reservation := core.ActiveReservation{
reservation := &core.ActiveReservation{
SymbolsPerSec: 200,
StartTimestamp: 100,
EndTimestamp: 200,
QuorumSplit: []byte{50, 50},
QuorumNumbers: []uint8{0, 1},
}
onDemand := core.OnDemandPayment{
onDemand := &core.OnDemandPayment{
CumulativePayment: big.NewInt(1000),
}
reservationWindow := uint32(5)
Expand All @@ -173,8 +176,9 @@ func TestAccountBlobCallSeries(t *testing.T) {

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

ctx := context.Background()
quorums := []uint8{0, 1}
Expand Down Expand Up @@ -208,56 +212,56 @@ func TestAccountBlobCallSeries(t *testing.T) {
}

func TestAccountBlob_BinRotation(t *testing.T) {
reservation := core.ActiveReservation{
reservation := &core.ActiveReservation{
SymbolsPerSec: 1000,
StartTimestamp: 100,
EndTimestamp: 200,
QuorumSplit: []byte{50, 50},
QuorumNumbers: []uint8{0, 1},
}
onDemand := core.OnDemandPayment{
onDemand := &core.OnDemandPayment{
CumulativePayment: big.NewInt(1000),
}
reservationWindow := uint32(1) // Set to 1 second for testing
pricePerSymbol := uint32(1)
minNumSymbols := uint32(100)
privateKey1, err := crypto.GenerateKey()
assert.NoError(t, err)
paymentSigner := auth.NewPaymentSigner(hex.EncodeToString(privateKey1.D.Bytes()))
accountant := NewAccountant(&reservation, &onDemand, reservationWindow, pricePerSymbol, minNumSymbols, paymentSigner, numBins)
paymentSigner, err := auth.NewPaymentSigner(hex.EncodeToString(privateKey1.D.Bytes()))
assert.NoError(t, err)
accountant := NewAccountant(reservation, onDemand, reservationWindow, pricePerSymbol, minNumSymbols, paymentSigner, numBins)

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

// First call
header, _, 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
header, _, err = accountant.AccountBlob(ctx, 300, quorums)
_, _, err = accountant.AccountBlob(ctx, 300, quorums)
assert.NoError(t, err)
fmt.Println("shifts ", header.BinIndex%3)
assert.Equal(t, isRotation([]uint64{800, 300, 0}, mapRecordUsage(accountant.binRecords)), true)

// Third call
header, _, 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)
}

func TestConcurrentBinRotationAndAccountBlob(t *testing.T) {
reservation := core.ActiveReservation{
reservation := &core.ActiveReservation{
SymbolsPerSec: 1000,
StartTimestamp: 100,
EndTimestamp: 200,
QuorumSplit: []byte{50, 50},
QuorumNumbers: []uint8{0, 1},
}
onDemand := core.OnDemandPayment{
onDemand := &core.OnDemandPayment{
CumulativePayment: big.NewInt(1000),
}
reservationWindow := uint32(1) // Set to 1 second for testing
Expand All @@ -266,8 +270,9 @@ func TestConcurrentBinRotationAndAccountBlob(t *testing.T) {

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

ctx := context.Background()
quorums := []uint8{0, 1}
Expand Down Expand Up @@ -296,14 +301,14 @@ func TestConcurrentBinRotationAndAccountBlob(t *testing.T) {
}

func TestAccountBlob_ReservationWithOneOverflow(t *testing.T) {
reservation := core.ActiveReservation{
reservation := &core.ActiveReservation{
SymbolsPerSec: 200,
StartTimestamp: 100,
EndTimestamp: 200,
QuorumSplit: []byte{50, 50},
QuorumNumbers: []uint8{0, 1},
}
onDemand := core.OnDemandPayment{
onDemand := &core.OnDemandPayment{
CumulativePayment: big.NewInt(1000),
}
reservationWindow := uint32(5)
Expand All @@ -312,8 +317,9 @@ func TestAccountBlob_ReservationWithOneOverflow(t *testing.T) {

privateKey1, err := crypto.GenerateKey()
assert.NoError(t, err)
paymentSigner := auth.NewPaymentSigner(hex.EncodeToString(privateKey1.D.Bytes()))
accountant := NewAccountant(&reservation, &onDemand, reservationWindow, pricePerSymbol, minNumSymbols, paymentSigner, numBins)
paymentSigner, err := auth.NewPaymentSigner(hex.EncodeToString(privateKey1.D.Bytes()))
assert.NoError(t, err)
accountant := NewAccountant(reservation, onDemand, reservationWindow, pricePerSymbol, minNumSymbols, paymentSigner, numBins)
ctx := context.Background()
quorums := []uint8{0, 1}
now := time.Now().Unix()
Expand Down Expand Up @@ -343,14 +349,14 @@ func TestAccountBlob_ReservationWithOneOverflow(t *testing.T) {
}

func TestAccountBlob_ReservationOverflowReset(t *testing.T) {
reservation := core.ActiveReservation{
reservation := &core.ActiveReservation{
SymbolsPerSec: 1000,
StartTimestamp: 100,
EndTimestamp: 200,
QuorumSplit: []byte{50, 50},
QuorumNumbers: []uint8{0, 1},
}
onDemand := core.OnDemandPayment{
onDemand := &core.OnDemandPayment{
CumulativePayment: big.NewInt(1000),
}
reservationWindow := uint32(1) // Set to 1 second for testing
Expand All @@ -359,8 +365,9 @@ func TestAccountBlob_ReservationOverflowReset(t *testing.T) {

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

ctx := context.Background()
quorums := []uint8{0, 1}
Expand All @@ -381,7 +388,7 @@ func TestAccountBlob_ReservationOverflowReset(t *testing.T) {
time.Sleep(time.Duration(reservationWindow) * time.Second)

// Third call: Should use new bin and allow overflow again
header, _, 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
10 changes: 0 additions & 10 deletions api/clients/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,6 @@ type EigenDAClientConfig struct {
// that can retrieve blobs but cannot disperse blobs.
SignerPrivateKeyHex string

// Payment signer private key in hex encoded format. This key connect to the wallet with payment registered on-chain
// if set to "", will result in a non-paying client and cannot disperse paid blobs.
PaymentSignerPrivateKeyHex string

// Payment number of bins indicate how many bins are kept at all times; the minimum is set to 3.
PaymentNumBins uint32

// Whether to disable TLS for an insecure connection when connecting to a local EigenDA disperser instance.
DisableTLS bool

Expand Down Expand Up @@ -119,9 +112,6 @@ func (c *EigenDAClientConfig) CheckAndSetDefaults() error {
if len(c.SignerPrivateKeyHex) > 0 && len(c.SignerPrivateKeyHex) != 64 {
return fmt.Errorf("a valid length SignerPrivateKeyHex needs to have 64 bytes")
}
if len(c.PaymentSignerPrivateKeyHex) > 0 && len(c.PaymentSignerPrivateKeyHex) != 64 {
return fmt.Errorf("a valid length PaymentSignerPrivateKeyHex needs to have 64 bytes")
}

if len(c.RPC) == 0 {
return fmt.Errorf("EigenDAClientConfig.RPC not set")
Expand Down
Loading

0 comments on commit 3dfb7c9

Please sign in to comment.