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

@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 27, 2026, 2:30 PM

@codecov
Copy link
Copy Markdown

codecov Bot commented May 18, 2026

Codecov Report

❌ Patch coverage is 66.23377% with 26 lines in your changes missing coverage. Please review.
✅ Project coverage is 59.02%. Comparing base (3936ac9) to head (3798ebb).

Files with missing lines Patch % Lines
sei-cosmos/x/auth/vesting/types/msgs.go 0.00% 13 Missing ⚠️
sei-cosmos/x/bank/keeper/send.go 77.27% 2 Missing and 3 partials ⚠️
giga/deps/xbank/keeper/send.go 66.66% 1 Missing and 2 partials ⚠️
x/evm/keeper/abci.go 0.00% 1 Missing and 1 partial ⚠️
x/evm/keeper/balance.go 75.00% 1 Missing and 1 partial ⚠️
sei-cosmos/x/auth/vesting/types/vesting_account.go 94.11% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #3456      +/-   ##
==========================================
- Coverage   59.04%   59.02%   -0.02%     
==========================================
  Files        2199     2188      -11     
  Lines      182096   181356     -740     
==========================================
- Hits       107513   107049     -464     
+ Misses      64933    64687     -246     
+ Partials     9650     9620      -30     
Flag Coverage Δ
sei-chain-pr 70.26% <66.23%> (?)
sei-db 70.41% <ø> (ø)
sei-db-state-db ?

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

Files with missing lines Coverage Δ
giga/deps/xevm/keeper/balance.go 100.00% <100.00%> (ø)
sei-cosmos/x/auth/vesting/types/vesting_account.go 89.74% <94.11%> (+0.11%) ⬆️
x/evm/keeper/abci.go 58.33% <0.00%> (-0.99%) ⬇️
x/evm/keeper/balance.go 80.00% <75.00%> (-20.00%) ⬇️
giga/deps/xbank/keeper/send.go 84.46% <66.66%> (-0.82%) ⬇️
sei-cosmos/x/bank/keeper/send.go 91.07% <77.27%> (-1.38%) ⬇️
sei-cosmos/x/auth/vesting/types/msgs.go 21.62% <0.00%> (-1.24%) ⬇️

... and 105 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

High Risk
Changes vesting proration and bank/EVM spendable and SubWei locking behavior on fund paths; incorrect equivalence or clamping could block transfers or mis-report balances.

Overview
Continuous vesting now prorates vested amounts with math/big (scaled fraction × coin amount) instead of per-coin sdk.Dec math, with clearer edge-case handling and fixed start ≥ end validate messages. Periodic vesting renames a loop variable to elapsed; MsgCreateVestingAccount adds a max coin bit-length bound and shared address validation.

Bank / EVM funds: spendable checks clamp total − locked at zero in SubUnlockedCoins, EVM GetBalance, and coinbase surplus sweeps; SubWei rejects spends that would leave usei below locked vesting. Parallel updates land in giga deps and sei-cosmos bank keepers; bank precompile tests refresh expected gas refund/tip balances.

Vesting tests are refactored (shared fixtures, table-driven marshal) and add 7h / 17h continuous-vesting cases.

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

Comment thread sei-cosmos/x/auth/vesting/types/vesting_account.go Outdated
@masih masih requested a review from arajasek May 22, 2026 09:08
Comment thread sei-cosmos/x/bank/keeper/send.go Outdated
@masih masih force-pushed the masih/math-perf-optimisation branch 2 times, most recently from dedf4c6 to 2ba4418 Compare May 22, 2026 11:37
Comment thread sei-cosmos/x/bank/keeper/send.go Outdated
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
@masih masih force-pushed the masih/math-perf-optimisation branch from ed60a56 to c530ac3 Compare May 22, 2026 18:41
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 using default effort 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 c530ac3. Configure here.

newUsei, newWei := SplitUseiWeiAmount(postTotal)
lockedUsei := k.LockedCoins(ctx, addr).AmountOf(denom)
if newUsei.LT(lockedUsei) {
return sdkerrors.Wrapf(sdkerrors.ErrInsufficientFunds, "%suei is smaller than locked %suei", newUsei, lockedUsei)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Error message misspells denomination as "uei" instead of "usei"

Low Severity

The format string "%suei is smaller than locked %suei" produces output like 100uei instead of 100usei. The %s format verb consumes the numeric argument, and uei is the literal suffix — but the denomination is usei, not uei. The nearby wei error correctly uses "%swei". The fix is "%susei is smaller than locked %susei".

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit c530ac3. Configure here.

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