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 1fd0e11
Show file tree
Hide file tree
Showing 18 changed files with 66 additions and 668 deletions.
37 changes: 22 additions & 15 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 Down Expand Up @@ -35,7 +34,8 @@ func TestNewAccountant(t *testing.T) {

privateKey1, err := crypto.GenerateKey()
assert.NoError(t, err)
paymentSigner := auth.NewPaymentSigner(hex.EncodeToString(privateKey1.D.Bytes()))
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)
Expand Down Expand Up @@ -65,7 +65,8 @@ func TestAccountBlob_Reservation(t *testing.T) {

privateKey1, err := crypto.GenerateKey()
assert.NoError(t, err)
paymentSigner := auth.NewPaymentSigner(hex.EncodeToString(privateKey1.D.Bytes()))
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()
Expand Down Expand Up @@ -116,7 +117,8 @@ func TestAccountBlob_OnDemand(t *testing.T) {

privateKey1, err := crypto.GenerateKey()
assert.NoError(t, err)
paymentSigner := auth.NewPaymentSigner(hex.EncodeToString(privateKey1.D.Bytes()))
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()
Expand Down Expand Up @@ -145,7 +147,8 @@ func TestAccountBlob_InsufficientOnDemand(t *testing.T) {

privateKey1, err := crypto.GenerateKey()
assert.NoError(t, err)
paymentSigner := auth.NewPaymentSigner(hex.EncodeToString(privateKey1.D.Bytes()))
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()
Expand Down Expand Up @@ -173,7 +176,8 @@ func TestAccountBlobCallSeries(t *testing.T) {

privateKey1, err := crypto.GenerateKey()
assert.NoError(t, err)
paymentSigner := auth.NewPaymentSigner(hex.EncodeToString(privateKey1.D.Bytes()))
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()
Expand Down Expand Up @@ -223,28 +227,28 @@ func TestAccountBlob_BinRotation(t *testing.T) {
minNumSymbols := uint32(100)
privateKey1, err := crypto.GenerateKey()
assert.NoError(t, err)
paymentSigner := auth.NewPaymentSigner(hex.EncodeToString(privateKey1.D.Bytes()))
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)
}
Expand All @@ -266,7 +270,8 @@ func TestConcurrentBinRotationAndAccountBlob(t *testing.T) {

privateKey1, err := crypto.GenerateKey()
assert.NoError(t, err)
paymentSigner := auth.NewPaymentSigner(hex.EncodeToString(privateKey1.D.Bytes()))
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()
Expand Down Expand Up @@ -312,7 +317,8 @@ func TestAccountBlob_ReservationWithOneOverflow(t *testing.T) {

privateKey1, err := crypto.GenerateKey()
assert.NoError(t, err)
paymentSigner := auth.NewPaymentSigner(hex.EncodeToString(privateKey1.D.Bytes()))
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 @@ -359,7 +365,8 @@ func TestAccountBlob_ReservationOverflowReset(t *testing.T) {

privateKey1, err := crypto.GenerateKey()
assert.NoError(t, err)
paymentSigner := auth.NewPaymentSigner(hex.EncodeToString(privateKey1.D.Bytes()))
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()
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
55 changes: 0 additions & 55 deletions api/clients/disperser_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package clients
import (
"context"
"crypto/tls"
"errors"
"fmt"
"sync"
"time"
Expand Down Expand Up @@ -55,7 +54,6 @@ type DisperserClient interface {
// DisperseBlobAuthenticated disperses a blob with an authenticated request.
// The BlobStatus returned will always be PROCESSSING if error is nil.
DisperseBlobAuthenticated(ctx context.Context, data []byte, customQuorums []uint8) (*disperser.BlobStatus, []byte, error)
DispersePaidBlob(ctx context.Context, data []byte, customQuorums []uint8) (*disperser.BlobStatus, []byte, error)
GetBlobStatus(ctx context.Context, key []byte) (*disperser_rpc.BlobStatusReply, error)
RetrieveBlob(ctx context.Context, batchHeaderHash []byte, blobIndex uint32) ([]byte, error)
}
Expand Down Expand Up @@ -189,59 +187,6 @@ func (c *disperserClient) DisperseBlob(ctx context.Context, data []byte, quorums
return blobStatus, reply.GetRequestId(), nil
}

// DispersePaidBlob disperses a blob with a payment header and signature. Similar to DisperseBlob but with signed payment header.
func (c *disperserClient) DispersePaidBlob(ctx context.Context, data []byte, quorums []uint8) (*disperser.BlobStatus, []byte, error) {
if c.accountant == nil {
return nil, nil, api.NewErrorInternal("not implemented")
}

err := c.initOnceGrpcConnection()
if err != nil {
return nil, nil, fmt.Errorf("error initializing connection: %w", err)
}

ctxTimeout, cancel := context.WithTimeout(ctx, c.config.Timeout)
defer cancel()

quorumNumbers := make([]uint32, len(quorums))
for i, q := range quorums {
quorumNumbers[i] = uint32(q)
}

// check every 32 bytes of data are within the valid range for a bn254 field element
_, err = rs.ToFrArray(data)
if err != nil {
return nil, nil, fmt.Errorf("encountered an error to convert a 32-bytes into a valid field element, please use the correct format where every 32bytes(big-endian) is less than 21888242871839275222246405745257275088548364400416034343698204186575808495617 %w", err)
}

header, signature, err := c.accountant.AccountBlob(ctx, uint64(encoding.GetBlobLength(uint(len(data)))), quorums)
if header == nil {
return nil, nil, errors.New("accountant returned nil pointer to header")
}
if err != nil {
return nil, nil, err
}

request := &disperser_rpc.DispersePaidBlobRequest{
Data: data,
QuorumNumbers: quorumNumbers,
PaymentHeader: header,
PaymentSignature: signature,
}

reply, err := c.client.DispersePaidBlob(ctxTimeout, request)
if err != nil {
return nil, nil, err
}

blobStatus, err := disperser.FromBlobStatusProto(reply.GetResult())
if err != nil {
return nil, nil, err
}

return blobStatus, reply.GetRequestId(), nil
}

func (c *disperserClient) DisperseBlobAuthenticated(ctx context.Context, data []byte, quorums []uint8) (*disperser.BlobStatus, []byte, error) {
err := c.initOnceGrpcConnection()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion api/clients/disperser_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

func TestPutBlobNoopSigner(t *testing.T) {
config := clients.NewConfig("nohost", "noport", time.Second, false)
disperserClient, err := clients.NewDisperserClient(config, auth.NewLocalNoopSigner())
disperserClient, err := clients.NewDisperserClient(config, auth.NewLocalNoopSigner(), nil)
assert.NoError(t, err)

test := []byte("test")
Expand Down
23 changes: 2 additions & 21 deletions api/clients/eigenda_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,7 @@ func NewEigenDAClient(log log.Logger, config EigenDAClientConfig) (*EigenDAClien

disperserConfig := NewConfig(host, port, config.ResponseTimeout, !config.DisableTLS)

var paymentSigner core.PaymentSigner
if len(config.PaymentSignerPrivateKeyHex) == 64 {
paymentSigner, err = auth.NewPaymentSigner(config.PaymentSignerPrivateKeyHex)
if err != nil {
return nil, fmt.Errorf("new payment signer: %w", err)
}
} else if len(config.PaymentSignerPrivateKeyHex) == 0 {
paymentSigner = auth.NewNoopPaymentSigner()
} else {
return nil, fmt.Errorf("invalid length for signer private key")
}

// a subsequent PR contains updates to fill in payment state
accountant := NewAccountant(&core.ActiveReservation{}, &core.OnDemandPayment{}, 0, 0, 0, paymentSigner, config.PaymentNumBins)
disperserClient, err := NewDisperserClient(disperserConfig, signer, accountant)
disperserClient, err := NewDisperserClient(disperserConfig, signer, nil)
if err != nil {
return nil, fmt.Errorf("new disperser-client: %w", err)
}
Expand Down Expand Up @@ -253,13 +239,8 @@ func (m *EigenDAClient) putBlob(ctxFinality context.Context, rawData []byte, res
}
// disperse blob
// TODO: would be nice to add a trace-id key to the context, to be able to follow requests from batcher->proxy->eigenda
var requestID []byte
// clients with a payment signer setting can disperse paid blobs
if len(m.Config.PaymentSignerPrivateKeyHex) > 0 {
_, requestID, err = m.Client.DispersePaidBlob(ctxFinality, data, customQuorumNumbers)
} else {
_, requestID, err = m.Client.DisperseBlobAuthenticated(ctxFinality, data, customQuorumNumbers)
}
_, requestID, err := m.Client.DisperseBlobAuthenticated(ctxFinality, data, customQuorumNumbers)
if err != nil {
// DisperserClient returned error is already a grpc error which can be a 400 (eg rate limited) or 500,
// so we wrap the error such that clients can still use grpc's status.FromError() function to get the status code.
Expand Down
21 changes: 0 additions & 21 deletions api/clients/mock/disperser_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,27 +81,6 @@ func (c *MockDisperserClient) DisperseBlob(ctx context.Context, data []byte, quo
return status, key, err
}

func (c *MockDisperserClient) DispersePaidBlob(ctx context.Context, data []byte, quorums []uint8) (*disperser.BlobStatus, []byte, error) {
args := c.Called(data, quorums)
var status *disperser.BlobStatus
if args.Get(0) != nil {
status = (args.Get(0)).(*disperser.BlobStatus)
}
var key []byte
if args.Get(1) != nil {
key = (args.Get(1)).([]byte)
}
var err error
if args.Get(2) != nil {
err = (args.Get(2)).(error)
}

keyStr := base64.StdEncoding.EncodeToString(key)
c.mockRequestIDStore[keyStr] = data

return status, key, err
}

func (c *MockDisperserClient) GetBlobStatus(ctx context.Context, key []byte) (*disperser_rpc.BlobStatusReply, error) {
args := c.Called(key)
var reply *disperser_rpc.BlobStatusReply
Expand Down
Loading

0 comments on commit 1fd0e11

Please sign in to comment.