Skip to content

Optimise vested amounts computation using math/big#3456

Open
masih wants to merge 2 commits into
mainfrom
masih/math-perf-optimisation
Open

Optimise vested amounts computation using math/big#3456
masih wants to merge 2 commits into
mainfrom
masih/math-perf-optimisation

Conversation

@masih
Copy link
Copy Markdown
Collaborator

@masih masih commented May 18, 2026

Continuous-vesting proration is amount * elapsed / duration, which is an integer computation. Going through sdk.Dec allocates a scaled big.Int per coin only to truncate the extra precision back away. Use math/big.Int directly and skip the round trip.

While at it:

  • rename the loop-local "x" in PeriodicVestingAccount.GetVestedCoins to "elapsed" so it matches the continuous-vesting variable naming
  • correct the Validate() error string on continuous and periodic vesting accounts: the predicate fails when start-time >= end-time, so "cannot be before end-time" was reversed
  • cover two non-aligned timestamps in TestGetVestedCoinsContVestingAcc

Continuous-vesting proration is amount * elapsed / duration, which is
an integer computation. Going through sdk.Dec allocates a scaled
big.Int per coin only to truncate the extra precision back away. Use
math/big.Int directly and skip the round trip.

While at it:
* rename the loop-local "x" in PeriodicVestingAccount.GetVestedCoins
  to "elapsed" so it matches the continuous-vesting variable naming
* correct the Validate() error string on continuous and periodic
  vesting accounts: the predicate fails when start-time >= end-time,
  so "cannot be before end-time" was reversed
* cover two non-aligned timestamps in TestGetVestedCoinsContVestingAcc
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 18, 2026

The latest Buf updates on your PR. Results from workflow Buf / buf (pull_request).

BuildFormatLintBreakingUpdated (UTC)
✅ passed✅ passed✅ passed✅ passedMay 18, 2026, 2:17 PM

@codecov
Copy link
Copy Markdown

codecov Bot commented May 18, 2026

Codecov Report

❌ Patch coverage is 88.88889% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 59.27%. Comparing base (b671648) to head (09773ea).

Files with missing lines Patch % Lines
sei-cosmos/x/auth/vesting/types/vesting_account.go 88.88% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #3456      +/-   ##
==========================================
- Coverage   59.29%   59.27%   -0.03%     
==========================================
  Files        2126     2126              
  Lines      175702   175701       -1     
==========================================
- Hits       104183   104138      -45     
- Misses      62448    62481      +33     
- Partials     9071     9082      +11     
Flag Coverage Δ
sei-chain-pr 77.50% <88.88%> (?)
sei-db 70.41% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
sei-cosmos/x/auth/vesting/types/vesting_account.go 89.62% <88.88%> (ø)

... and 26 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@masih masih requested review from arajasek and sei-will May 18, 2026 13:04
@masih masih marked this pull request as ready for review May 18, 2026 13:04
@cursor
Copy link
Copy Markdown

cursor Bot commented May 18, 2026

PR Summary

Medium Risk
Touches vesting amount computation, which can affect spendable balances if the integer math changes rounding/truncation behavior. Test coverage is expanded, but any downstream expectations about decimal rounding should be rechecked.

Overview
Optimizes continuous vesting proration by replacing sdk.Dec-based scalar math with direct integer arithmetic via math/big in ContinuousVestingAccount.GetVestedCoins.

Also corrects the inverted validation error message for start/end time ordering in both continuous and periodic vesting accounts, renames a periodic loop-local variable for clarity, and adds test cases covering pro-rata vesting at non-aligned timestamps.

Reviewed by Cursor Bugbot for commit 09773ea. Bugbot is set up for automated code reviews on this repo. Configure here.

Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit cea1851. Configure here.

vestedAmt := ovc.Amount.ToDec().Mul(s).RoundInt()
vestedCoins = append(vestedCoins, sdk.NewCoin(ovc.Denom, vestedAmt))
vested := new(big.Int).Mul(ovc.Amount.BigInt(), elapsed)
vested.Quo(vested, duration)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Rounding behavior changed from bankers rounding to truncation

High Severity

The old code used sdk.Dec arithmetic with RoundInt(), which applies bankers rounding (round-half-to-even). The new big.Int code uses Quo which truncates toward zero. This changes vested amounts by up to 1 unit per denomination at non-aligned timestamps. For example, 1000 * 7/24 previously yielded 292 (rounded) but now yields 291 (truncated). In a blockchain context this is a consensus-breaking change — nodes running different versions will disagree on vesting state.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit cea1851. Configure here.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

yup and labeled as such. Reviewers, please advise if I can avoid this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants