Skip to content

Commit

Permalink
fix: properly set fields without onchain correspondance
Browse files Browse the repository at this point in the history
  • Loading branch information
hopeyen committed Dec 17, 2024
1 parent b679a14 commit 1bb83ad
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 47 deletions.
95 changes: 53 additions & 42 deletions api/clients/v2/accountant.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ type BinRecord struct {
Usage uint64
}

func DummyBinRecord() *BinRecord {
return &BinRecord{
Index: 0,
Usage: 0,
}
}

func NewAccountant(accountID string, reservation *core.ReservedPayment, onDemand *core.OnDemandPayment, reservationWindow uint32, pricePerSymbol uint32, minNumSymbols uint32, numBins uint32) *Accountant {
//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
Expand Down Expand Up @@ -159,51 +166,55 @@ 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.PaymentGlobalParams.MinNumSymbols)
a.onDemand.CumulativePayment = new(big.Int).SetBytes(paymentState.OnchainCumulativePayment)
a.cumulativePayment = new(big.Int).SetBytes(paymentState.CumulativePayment)
a.pricePerSymbol = uint32(paymentState.PaymentGlobalParams.PricePerSymbol)

a.reservation.SymbolsPerSecond = uint64(paymentState.PaymentGlobalParams.GlobalSymbolsPerSecond)
a.reservation.StartTimestamp = uint64(paymentState.Reservation.StartTimestamp)
a.reservation.EndTimestamp = uint64(paymentState.Reservation.EndTimestamp)
a.reservationWindow = uint32(paymentState.PaymentGlobalParams.ReservationWindow)

quorumNumbers := make([]uint8, len(paymentState.Reservation.QuorumNumbers))
for i, quorum := range paymentState.Reservation.QuorumNumbers {
quorumNumbers[i] = uint8(quorum)
}
a.reservation.QuorumNumbers = quorumNumbers

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

binRecords := make([]BinRecord, len(paymentState.BinRecords))
for i, record := range paymentState.BinRecords {
binRecords[i] = BinRecord{
Index: record.Index,
Usage: record.Usage,
}

a.minNumSymbols = uint32(paymentState.GetPaymentGlobalParams().GetMinNumSymbols())
a.pricePerSymbol = uint32(paymentState.GetPaymentGlobalParams().GetPricePerSymbol())
a.reservationWindow = uint32(paymentState.GetPaymentGlobalParams().GetReservationWindow())

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 = core.DummyReservedPayment()
} 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)
}
a.reservation.QuorumSplits = quorumSplits
}
a.binRecords = binRecords

binRecords := make([]BinRecord, len(paymentState.GetBinRecords()))
for i, record := range paymentState.GetBinRecords() {
if record == nil {
binRecords[i] = *DummyBinRecord()
} else {
binRecords[i] = BinRecord{
Index: record.Index,
Usage: record.Usage,
}
}
}
a.binRecords = binRecords
return nil
}

Expand Down
10 changes: 10 additions & 0 deletions core/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,16 @@ type ReservedPayment struct {
QuorumSplits []byte
}

func DummyReservedPayment() *ReservedPayment {
return &ReservedPayment{
SymbolsPerSecond: 0,
StartTimestamp: 0,
EndTimestamp: 0,
QuorumNumbers: []uint8{},
QuorumSplits: []byte{},
}
}

type OnDemandPayment struct {
// Total amount deposited by the user
CumulativePayment *big.Int
Expand Down
2 changes: 1 addition & 1 deletion core/meterer/offchain_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func (s *OffchainStore) GetLargestCumulativePayment(ctx context.Context, account
}

if len(payments) == 0 {
return nil, nil
return big.NewInt(0), nil
}

var payment *big.Int
Expand Down
8 changes: 4 additions & 4 deletions disperser/apiserver/server_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,20 +254,20 @@ func (s *DispersalServerV2) GetPaymentState(ctx context.Context, req *pb.GetPaym
currentReservationPeriod := meterer.GetReservationPeriod(now, reservationWindow)
binRecords, err := s.meterer.OffchainStore.GetBinRecords(ctx, req.AccountId, currentReservationPeriod)
if err != nil {
return nil, api.NewErrorNotFound("failed to get active reservation")
s.logger.Debug("failed to get reservation records, use placeholders", err, accountID)
}
largestCumulativePayment, err := s.meterer.OffchainStore.GetLargestCumulativePayment(ctx, req.AccountId)
if err != nil {
return nil, api.NewErrorNotFound("failed to get largest cumulative payment")
s.logger.Debug("failed to get largest cumulative payment, use zero value", err)
}
// on-Chain account state
reservation, err := s.meterer.ChainPaymentState.GetReservedPaymentByAccount(ctx, accountID)
if err != nil {
return nil, api.NewErrorNotFound("failed to get active reservation")
s.logger.Debug("failed to get onchain reservation, use zero values", err)
}
onDemandPayment, err := s.meterer.ChainPaymentState.GetOnDemandPaymentByAccount(ctx, accountID)
if err != nil {
return nil, api.NewErrorNotFound("failed to get on-demand payment")
s.logger.Debug("failed to get ondemand payment, use zero value", err)
}

paymentGlobalParams := pb.PaymentGlobalParams{
Expand Down

0 comments on commit 1bb83ad

Please sign in to comment.