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: cumulative payment dynamo db unit conversion #979

Merged
merged 3 commits into from
Dec 12, 2024

Conversation

hopeyen
Copy link
Contributor

@hopeyen hopeyen commented Dec 11, 2024

Why are these changes needed?

Quick PR to move away from using dynamoDB cumulativePayment value to uint64 using strconv.ParseUint; directly convert to big.Int

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 force-pushed the hope/payments-db-fix branch from 5eab313 to 1a58cb6 Compare December 11, 2024 01:39
Copy link
Contributor

@ian-shim ian-shim left a comment

Choose a reason for hiding this comment

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

does this PR have the changes made on dynamodb storage?

func (m *Meterer) PaymentCharged(numSymbols uint) uint64 {
return uint64(m.SymbolsCharged(numSymbols)) * uint64(m.ChainPaymentState.GetPricePerSymbol())
func (m *Meterer) PaymentCharged(numSymbols uint) *big.Int {
return big.NewInt(int64(m.SymbolsCharged(numSymbols) * m.ChainPaymentState.GetPricePerSymbol()))
Copy link
Contributor

Choose a reason for hiding this comment

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

can m.SymbolsCharged(numSymbols) * m.ChainPaymentState.GetPricePerSymbol() ever be greater than max int64?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Highly unlikely 😬 If we induct backwards to find what's allowed by uint64 with our expected price per symbol 447,000,000 then the number of symbols uint64 can charge is 2^64/477,000,000=41,267,883,833.8. With 1 symbol being 32 bytes, we are going to allow 1,320,572,282,681.7bytes=1.3TB per blob

The reason I updated this is for consistency in the cumulativePayment calculations, but I'm happy to revert these changes and just convert the numbers to big.Int if you think that's better?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah converting each values to big int and using num0.Mul(num1) seems more safe?

@hopeyen
Copy link
Contributor Author

hopeyen commented Dec 11, 2024

does this PR have the changes made on dynamodb storage?

no, I think the changes are only how we read from dynamodb. Writing to the db is still the same as before

prevPayment, err = strconv.ParseUint(smallerResult[0]["CumulativePayments"].(*types.AttributeValueMemberN).Value, 10, 64)
if err != nil {
return 0, 0, 0, fmt.Errorf("failed to parse previous payment: %w", err)
setPrevPayment, success := prevPayment.SetString(smallerResult[0]["CumulativePayments"].(*types.AttributeValueMemberN).Value, 10)
Copy link
Contributor

Choose a reason for hiding this comment

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

I know these values are all expected in the happy path, but this chain seems dangerous
It's missing at least a couple validations: checking if "CumulativePayments" exists in the map and the result of the type assertion

Here and below as well

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added explicit checking for them!

func (m *Meterer) PaymentCharged(numSymbols uint) uint64 {
return uint64(m.SymbolsCharged(numSymbols)) * uint64(m.ChainPaymentState.GetPricePerSymbol())
func (m *Meterer) PaymentCharged(numSymbols uint) *big.Int {
return big.NewInt(int64(m.SymbolsCharged(numSymbols) * m.ChainPaymentState.GetPricePerSymbol()))
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah converting each values to big int and using num0.Mul(num1) seems more safe?

@hopeyen hopeyen force-pushed the hope/payments-db-fix branch from 1a58cb6 to f53dae8 Compare December 11, 2024 21:33
@hopeyen hopeyen requested a review from ian-shim December 11, 2024 22:49
@hopeyen hopeyen merged commit be47a6c into master Dec 12, 2024
9 checks passed
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