From 1bb83ad04395ef4f5f06a972a4c47f33a4381cfc Mon Sep 17 00:00:00 2001 From: hopeyen <60078528+hopeyen@users.noreply.github.com> Date: Tue, 17 Dec 2024 00:33:51 +0000 Subject: [PATCH] fix: properly set fields without onchain correspondance --- api/clients/v2/accountant.go | 95 ++++++++++++++++++-------------- core/data.go | 10 ++++ core/meterer/offchain_store.go | 2 +- disperser/apiserver/server_v2.go | 8 +-- 4 files changed, 68 insertions(+), 47 deletions(-) diff --git a/api/clients/v2/accountant.go b/api/clients/v2/accountant.go index 6b923d51b4..a9a3d7b11f 100644 --- a/api/clients/v2/accountant.go +++ b/api/clients/v2/accountant.go @@ -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 @@ -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 } diff --git a/core/data.go b/core/data.go index 367faad32d..db97e96a92 100644 --- a/core/data.go +++ b/core/data.go @@ -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 diff --git a/core/meterer/offchain_store.go b/core/meterer/offchain_store.go index 6b213a495e..5899ca75e9 100644 --- a/core/meterer/offchain_store.go +++ b/core/meterer/offchain_store.go @@ -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 diff --git a/disperser/apiserver/server_v2.go b/disperser/apiserver/server_v2.go index bd45248f1d..ad8c9b9f73 100644 --- a/disperser/apiserver/server_v2.go +++ b/disperser/apiserver/server_v2.go @@ -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{