Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: properly set fields without onchain correspondance #1014

Merged
merged 5 commits into from
Dec 17, 2024

Conversation

hopeyen
Copy link
Contributor

@hopeyen hopeyen commented Dec 17, 2024

Why are these changes needed?

  • do not let server return API errors when some users did not have payment set on-chain
  • for the client's accoutant, use default values instead of failing when one of the payments info is missing

Checks

  • I've made sure the tests are passing. Note that there might be a few flaky tests, in that case, please comment that they are not relevant.
  • I've checked the new test coverage and the coverage percentage didn't drop.
  • Testing Strategy
    • Unit tests
    • Integration tests
    • This PR is not tested :(

@hopeyen hopeyen requested a review from ian-shim December 17, 2024 03:50
@hopeyen hopeyen force-pushed the hope/payment-state-nil-handling branch from 1bb83ad to 0767072 Compare December 17, 2024 19:01
Index: record.Index,
Usage: record.Usage,
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we document this behavior (which fields are optional and what the consequences are)?

binRecords := make([]BinRecord, len(paymentState.GetBinRecords()))
for i, record := range paymentState.GetBinRecords() {
if record == nil {
binRecords[i] = *DummyBinRecord()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

binRecords[i] = BinRecord{
	Index: 0,
	Usage: 0,
}

?

}
// 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)
reservation = core.DummyReservedPayment()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why don't we leave them as nil?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way we build reply struct assumes that there is a valid nonnil object. Would it make more sense to set the inner fields here and use those when we construct the reply?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the reply need to return zero valued object? Could we return nil instead?
Accountant handles nil reservation/ondemand payment here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could return nil, it is just that we will add a separate step to convert cached struct to protobuf structs outside of the reply construction.

This seems like a cleaner behavior, so I will update to do that

}
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think it would be helpful to log accountIDs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated to include accountIDs and use labels

@@ -254,20 +255,44 @@ 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use labels:

s.logger.Debug("failed to get reservation records, use placeholders", "err ", err, "accountID", accountID)

}
// on-Chain account state
pbReservation := &pb.Reservation{}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we declare it like var pbReservation *pb.Reservation, we don't have to set pbReservation = nil in L269

@hopeyen hopeyen requested a review from ian-shim December 17, 2024 21:05
core/data.go Outdated
type OnDemandPayment struct {
// Total amount deposited by the user
CumulativePayment *big.Int
}

func DummyOnDemandPayment() *OnDemandPayment {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this used anywhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

}

if paymentState.GetReservation() == nil {
a.reservation = core.DummyReservedPayment()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: could we explicitly create empty object

a.reservation = &ReservedPayment{
	SymbolsPerSecond: 0,
	StartTimestamp:   0,
	EndTimestamp:     0,
	QuorumNumbers:    []uint8{},
	QuorumSplits:     []byte{},
}

and remove DummyReservedPayment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, updated

@hopeyen hopeyen merged commit 7022ed2 into master Dec 17, 2024
9 checks passed
@hopeyen hopeyen deleted the hope/payment-state-nil-handling branch December 17, 2024 23:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants