Skip to content

Commit

Permalink
refactor: accountant sign over blob header fields
Browse files Browse the repository at this point in the history
  • Loading branch information
hopeyen committed Oct 3, 2024
1 parent 24977c1 commit ba7bb66
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 66 deletions.
44 changes: 40 additions & 4 deletions api/clients/accountant.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package clients

import (
"context"
"crypto/ecdsa"
"errors"
"fmt"
"math/big"
"time"

"github.com/Layr-Labs/eigenda/core"
"github.com/Layr-Labs/eigenda/disperser/meterer"
"github.com/ethereum/go-ethereum/common"
)

type IAccountant interface {
Expand All @@ -26,9 +30,16 @@ type Accountant struct {
binUsages []uint64
cumulativePayment uint64
stopRotation chan struct{}

domainSigner *meterer.EIP712Signer
accountantSigner *ecdsa.PrivateKey
}

func NewAccountant(reservation meterer.ActiveReservation, onDemand meterer.OnDemandPayment, reservationWindow uint32, pricePerChargeable uint32, minChargeableSize uint32) Accountant {
func NewAccountant(reservation meterer.ActiveReservation, onDemand meterer.OnDemandPayment, reservationWindow uint32, pricePerChargeable uint32, minChargeableSize uint32, accountantSigner *ecdsa.PrivateKey) Accountant {
//todo: refactor signing strategy; EIP712 -> serializer? private key -> Local blob request signer?
chainID := big.NewInt(17000)
verifyingContract := common.HexToAddress("0x1234000000000000000000000000000000000000")

a := Accountant{
reservation: reservation,
onDemand: onDemand,
Expand All @@ -38,6 +49,8 @@ func NewAccountant(reservation meterer.ActiveReservation, onDemand meterer.OnDem
binUsages: []uint64{0, 0, 0},
cumulativePayment: 0,
stopRotation: make(chan struct{}),
domainSigner: meterer.NewEIP712Signer(chainID, verifyingContract),
accountantSigner: accountantSigner,
}
go a.startBinRotation()
return a
Expand Down Expand Up @@ -68,8 +81,8 @@ func (a *Accountant) Stop() {
close(a.stopRotation)
}

// accountant provides and records payment information
func (a *Accountant) AccountBlob(ctx context.Context, dataLength uint64, quorums []uint8) (uint32, uint64, error) {
// accountant calculates and records payment information
func (a *Accountant) BlobPaymentInfo(ctx context.Context, dataLength uint64, quorums []uint8) (uint32, uint64, error) {
//TODO: do we need to lock the binUsages here in case the rotation happens in the middle of the function?
currentBinUsage := a.binUsages[0]
currentBinIndex := meterer.GetCurrentBinIndex(a.reservationWindow)
Expand All @@ -88,7 +101,6 @@ func (a *Accountant) AccountBlob(ctx context.Context, dataLength uint64, quorums
return currentBinIndex, 0, nil
}

fmt.Println("in ondemand:", currentBinUsage, dataLength, a.reservation.DataRate)
// reservation not available, attempt on-demand
//todo: rollback if disperser respond with some type of rejection?
incrementRequired := uint64(max(uint32(dataLength), a.minChargeableSize)) * uint64(a.pricePerChargeable) / uint64(a.minChargeableSize)
Expand All @@ -98,3 +110,27 @@ func (a *Accountant) AccountBlob(ctx context.Context, dataLength uint64, quorums
}
return 0, 0, errors.New("Accountant cannot approve payment for this blob")
}

// accountant provides and records payment information
func (a *Accountant) AccountBlob(ctx context.Context, dataLength uint64, quorums []uint8) (*meterer.BlobHeader, error) {
binIndex, cumulativePayment, err := a.BlobPaymentInfo(ctx, dataLength, quorums)
if err != nil {
return nil, err
}

// TODO: have client first generate a commitment, then pass it here
commitment := core.NewG1Point(big.NewInt(0), big.NewInt(1))
header, err := meterer.ConstructBlobHeader(a.domainSigner, binIndex, cumulativePayment, *commitment, uint32(dataLength), quorums, a.accountantSigner)
if err != nil {
return nil, err
}
return header, nil
}

func (a *Accountant) signBlobHeader(header *meterer.BlobHeader) (*meterer.BlobHeader, error) {
signedHeader, err := meterer.ConstructBlobHeader(a.domainSigner, header.BinIndex, header.CumulativePayment, header.Commitment, header.DataLength, header.QuorumNumbers, a.accountantSigner)
if err != nil {
return nil, err
}
return signedHeader, nil
}
Loading

0 comments on commit ba7bb66

Please sign in to comment.