Skip to content

Commit d225f94

Browse files
committed
refactor: convertLegacyPaymentStateToNew function
return early when no reservation exists, which makes the logic much simpler
1 parent 7db678c commit d225f94

File tree

1 file changed

+26
-30
lines changed

1 file changed

+26
-30
lines changed

api/clients/v2/disperser_client.go

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -478,41 +478,37 @@ func convertLegacyPaymentStateToNew(legacyReply *disperser_rpc.GetPaymentStateRe
478478
}
479479
}
480480

481-
// Convert aggregated period records to per-quorum format
482-
periodRecords := make(map[uint32]*disperser_rpc.PeriodRecords)
483-
// If no period records are available, return empty map
484-
// This is possible if no reservations exist for the account
485-
if len(legacyReply.PeriodRecords) > 0 {
486-
// Apply the same period records to all relevant quorums
487-
quorums := []uint32{0} // Default to quorum 0
488-
if legacyReply.Reservation != nil && len(legacyReply.Reservation.QuorumNumbers) > 0 {
489-
quorums = legacyReply.Reservation.QuorumNumbers
490-
}
481+
// If no reservation is available, return early with only payment vault params and cumulative payment info.
482+
if legacyReply.Reservation == nil {
483+
return &disperser_rpc.GetPaymentStateForAllQuorumsReply{
484+
PaymentVaultParams: paymentVaultParams,
485+
CumulativePayment: legacyReply.CumulativePayment,
486+
OnchainCumulativePayment: legacyReply.OnchainCumulativePayment,
487+
}, nil
488+
}
491489

492-
for _, quorumID := range quorums {
493-
periodRecords[quorumID] = &disperser_rpc.PeriodRecords{
494-
Records: legacyReply.PeriodRecords,
495-
}
496-
}
490+
// Otherwise there is a reservation available, so we need to convert it to the per-quorum format.
491+
492+
// We first make sure that the disperser returned valid data.
493+
if len(legacyReply.PeriodRecords) == 0 {
494+
return nil, fmt.Errorf("legacy payment state received from disperser does not contain period records")
495+
}
496+
if len(legacyReply.Reservation.QuorumNumbers) == 0 {
497+
return nil, fmt.Errorf("legacy payment state received from disperser does not contain reservation quorums")
497498
}
498499

499-
// Convert aggregated reservation to per-quorum format
500500
reservations := make(map[uint32]*disperser_rpc.QuorumReservation)
501-
// If no reservation is available, return empty map
502-
// This is possible if no reservations exist for the account
503-
if legacyReply.Reservation != nil {
504-
// Apply the reservation to all quorums mentioned in the reservation
505-
quorums := legacyReply.Reservation.QuorumNumbers
506-
if len(quorums) == 0 {
507-
return nil, fmt.Errorf("no quorums specified in legacy reservation received from disperser")
508-
}
501+
periodRecords := make(map[uint32]*disperser_rpc.PeriodRecords)
509502

510-
for _, quorumID := range quorums {
511-
reservations[quorumID] = &disperser_rpc.QuorumReservation{
512-
SymbolsPerSecond: legacyReply.Reservation.SymbolsPerSecond,
513-
StartTimestamp: legacyReply.Reservation.StartTimestamp,
514-
EndTimestamp: legacyReply.Reservation.EndTimestamp,
515-
}
503+
// Apply the reservation to all reservationQuorums mentioned in the reservation
504+
for _, quorumID := range legacyReply.Reservation.QuorumNumbers {
505+
reservations[quorumID] = &disperser_rpc.QuorumReservation{
506+
SymbolsPerSecond: legacyReply.Reservation.SymbolsPerSecond,
507+
StartTimestamp: legacyReply.Reservation.StartTimestamp,
508+
EndTimestamp: legacyReply.Reservation.EndTimestamp,
509+
}
510+
periodRecords[quorumID] = &disperser_rpc.PeriodRecords{
511+
Records: legacyReply.PeriodRecords,
516512
}
517513
}
518514

0 commit comments

Comments
 (0)