Skip to content

Commit

Permalink
fix: handle zero value empty slices
Browse files Browse the repository at this point in the history
  • Loading branch information
hopeyen committed Dec 16, 2024
1 parent ee89671 commit 65f65fe
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 30 deletions.
57 changes: 32 additions & 25 deletions api/clients/v2/accountant.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,41 +182,48 @@ func (a *Accountant) SetPaymentState(paymentState *disperser_rpc.GetPaymentState
return fmt.Errorf("payment state cannot be nil")
} else if paymentState.GetPaymentGlobalParams() == nil {
return fmt.Errorf("payment global params cannot be nil")
} else if paymentState.GetOnchainCumulativePayment() == nil {
return fmt.Errorf("onchain cumulative payment cannot be nil")
} else if paymentState.GetCumulativePayment() == nil {
return fmt.Errorf("cumulative payment cannot be nil")
} else if paymentState.GetReservation() == nil {
return fmt.Errorf("reservation cannot be nil")
} else if paymentState.GetReservation().GetQuorumNumbers() == nil {
return fmt.Errorf("reservation quorum numbers cannot be nil")
} else if paymentState.GetReservation().GetQuorumSplits() == nil {
return fmt.Errorf("reservation quorum split cannot be nil")
} else if paymentState.GetBinRecords() == nil {
return fmt.Errorf("bin records cannot be nil")
}

a.minNumSymbols = uint32(paymentState.GetPaymentGlobalParams().GetMinNumSymbols())
a.onDemand.CumulativePayment = new(big.Int).SetBytes(paymentState.GetOnchainCumulativePayment())
a.cumulativePayment = new(big.Int).SetBytes(paymentState.GetCumulativePayment())
a.pricePerSymbol = uint32(paymentState.GetPaymentGlobalParams().GetPricePerSymbol())

a.reservation.SymbolsPerSecond = uint64(paymentState.GetPaymentGlobalParams().GetGlobalSymbolsPerSecond())
a.reservation.StartTimestamp = uint64(paymentState.GetReservation().GetStartTimestamp())
a.reservation.EndTimestamp = uint64(paymentState.GetReservation().GetEndTimestamp())
a.reservationWindow = uint32(paymentState.GetPaymentGlobalParams().GetReservationWindow())

quorumNumbers := make([]uint8, len(paymentState.GetReservation().GetQuorumNumbers()))
for i, quorum := range paymentState.GetReservation().GetQuorumNumbers() {
quorumNumbers[i] = uint8(quorum)
}
a.reservation.QuorumNumbers = quorumNumbers
if paymentState.GetOnchainCumulativePayment() == nil {
a.onDemand.CumulativePayment = big.NewInt(0)
} else {
a.onDemand.CumulativePayment = new(big.Int).SetBytes(paymentState.GetOnchainCumulativePayment())
}

if paymentState.GetCumulativePayment() == nil {
a.cumulativePayment = big.NewInt(0)
} else {
a.cumulativePayment = new(big.Int).SetBytes(paymentState.GetCumulativePayment())
}

if paymentState.GetReservation() == nil {
a.reservation.SymbolsPerSecond = uint64(0)
a.reservation.StartTimestamp = uint64(0)
a.reservation.EndTimestamp = uint64(0)
a.reservation.QuorumNumbers = make([]uint8, 0)
a.reservation.QuorumSplits = make([]uint8, 0)
} else {
a.reservation.SymbolsPerSecond = uint64(paymentState.GetReservation().GetSymbolsPerSecond())
a.reservation.StartTimestamp = uint64(paymentState.GetReservation().GetStartTimestamp())
a.reservation.EndTimestamp = uint64(paymentState.GetReservation().GetEndTimestamp())
quorumNumbers := make([]uint8, len(paymentState.GetReservation().GetQuorumNumbers()))
for i, quorum := range paymentState.GetReservation().GetQuorumNumbers() {
quorumNumbers[i] = uint8(quorum)
}
a.reservation.QuorumNumbers = quorumNumbers

quorumSplits := make([]uint8, len(paymentState.GetReservation().GetQuorumSplits()))
for i, quorum := range paymentState.GetReservation().GetQuorumSplits() {
quorumSplits[i] = uint8(quorum)
quorumSplits := make([]uint8, len(paymentState.GetReservation().GetQuorumSplits()))
for i, quorum := range paymentState.GetReservation().GetQuorumSplits() {
quorumSplits[i] = uint8(quorum)
}
a.reservation.QuorumSplits = quorumSplits
}
a.reservation.QuorumSplits = quorumSplits

binRecords := make([]BinRecord, len(paymentState.GetBinRecords()))
for i, record := range paymentState.GetBinRecords() {
Expand Down
24 changes: 23 additions & 1 deletion api/clients/v2/disperser_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package clients
import (
"context"
"fmt"
"math/big"
"sync"

"github.com/Layr-Labs/eigenda/api"
disperser_rpc "github.com/Layr-Labs/eigenda/api/grpc/disperser/v2"
"github.com/Layr-Labs/eigenda/core"
"github.com/Layr-Labs/eigenda/core/meterer"
corev2 "github.com/Layr-Labs/eigenda/core/v2"
dispv2 "github.com/Layr-Labs/eigenda/disperser/common/v2"
"github.com/Layr-Labs/eigenda/encoding"
Expand Down Expand Up @@ -80,7 +82,27 @@ func NewDisperserClient(config *DisperserClientConfig, signer corev2.BlobRequest
if err != nil {
return nil, fmt.Errorf("error getting signer's account ID: %w", err)
}
accountant = DummyAccountant(accountID)
// accountant = DummyAccountant(accountID)
accountant = &Accountant{
accountID: accountID,
reservation: &core.ReservedPayment{
SymbolsPerSecond: 0,
StartTimestamp: 0,
EndTimestamp: 0,
QuorumNumbers: []uint8{},
QuorumSplits: []byte{},
},
onDemand: &core.OnDemandPayment{
CumulativePayment: big.NewInt(0),
},
reservationWindow: 0,
pricePerSymbol: 0,
minNumSymbols: 0,
binRecords: make([]BinRecord, 3),
usageLock: sync.Mutex{},
cumulativePayment: big.NewInt(0),
numBins: uint32(meterer.MinNumBins),
}
}

return &disperserClient{
Expand Down
1 change: 1 addition & 0 deletions core/meterer/offchain_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ func (s *OffchainStore) GetLargestCumulativePayment(ctx context.Context, account
return nil, fmt.Errorf("failed to query payments for account: %w", err)
}

fmt.Println("Get largest cumulative payment", payments)
if len(payments) == 0 {
return big.NewInt(0), nil
}
Expand Down
10 changes: 6 additions & 4 deletions disperser/apiserver/server_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,17 +303,19 @@ func (s *DispersalServerV2) GetPaymentState(ctx context.Context, req *pb.GetPaym
}
// build reply
reply := &pb.GetPaymentStateReply{
PaymentGlobalParams: &paymentGlobalParams,
BinRecords: binRecords[:],
PaymentGlobalParams: &paymentGlobalParams,
BinRecords: binRecords[:],
CumulativePayment: largestCumulativePayment.Bytes(),
OnchainCumulativePayment: onDemandPayment.CumulativePayment.Bytes(),
Reservation: &pb.Reservation{
SymbolsPerSecond: reservation.SymbolsPerSecond,
StartTimestamp: uint32(reservation.StartTimestamp),
EndTimestamp: uint32(reservation.EndTimestamp),
QuorumSplits: quorumSplits,
QuorumNumbers: quorumNumbers,
},
CumulativePayment: largestCumulativePayment.Bytes(),
OnchainCumulativePayment: onDemandPayment.CumulativePayment.Bytes(),
}

fmt.Println("GetPaymentState reply", reply, largestCumulativePayment, onDemandPayment.CumulativePayment)
return reply, nil
}

0 comments on commit 65f65fe

Please sign in to comment.