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

feat: gas optimization #33

Closed
zsystm opened this issue Aug 11, 2023 · 6 comments · Fixed by #39
Closed

feat: gas optimization #33

zsystm opened this issue Aug 11, 2023 · 6 comments · Fixed by #39
Assignees
Labels
enhancement New feature or request

Comments

@zsystm
Copy link

zsystm commented Aug 11, 2023

While fuzzing tests liquidstaking module, it often exceeded 20,000,000.

Need to optimize gas consumption.

@zsystm zsystm added the enhancement New feature or request label Aug 11, 2023
@zsystm zsystm self-assigned this Aug 11, 2023
@zsystm zsystm moved this to In Progress in liquidstaking-module Aug 14, 2023
@zsystm
Copy link
Author

zsystm commented Aug 14, 2023

Analyze gas consumptions by running simulation testing

Gas-guzzling parts

GetNetAmountStateEssentials (not msg, but heavy gas consumer)

paired chunks unbonding chunks rest chunks consumed
265 65 0 7128932
229 101 0 6615871
  • unit paired chunk consumption: 27K
  • unit unbonding chunk consumption: 2K

LiquidStake

method(or logic) consumed notes
GetNetAmountStateEssentials 8293 pairedchunks=0, unbondingchunks=0, restchunks=0
GetPairingInsurances 669548 pairinginsurances=76
Escrow canto and mint lscanto 132411

LiquidUnstake

method(or logic) consumed notes
IterateAllChunks 591277 pairedchunks=100, unbondingchunks=82, restchunks=0
GetNetAmountStateEssentials 2971939 pairedchunks=100, unbondingchunks=82, restchunks=0
Queue liquid unstake including escrow lscanto 18662

ClaimDiscountedReward

method(or logic) consumed notes
GetNetAmountStateEssentials 2507286 pairedchunks=84, unbondingchunks=82, restchunks=0
Rest of logics 37176

Normal parts (< 40K)

msg consumed
ProvideInsurance 37780
WithdrawInsurance 3732
WithdrawInsuranceCommission 22383
DepositInsurance 14926
CancelProvideInsurance 16500

@zsystm
Copy link
Author

zsystm commented Aug 21, 2023

Dependent on Utilization Ratio

LiquidStake (Total Gas Usage at SoftCap: 3,677,506 | at HardCap: 9,584,748)

  • GetPairingInsurances:
    • 99,043 (iterate 215 insurances, 2 pairing insurances)
    • 577,585 (iterate 824 insurances, 110 pairing insurances)
  • GetNetAmountStateEssentials
    • 3,448,895 (SoftCap, u: 0.050359255514767015)
    • 8,877,595 (HardCap, u: 0.099419919986008629)
  • Get tokens and mint ls tokens: 129,568 (if liquid stake n chunks, then should multiply n)

LiquidUnstake (Total Gas Usage at SoftCap: 4,300,690 | at HardCap: 10,971,065)

  • IterateAllChunks:
    • 729,389 (SoftCap, u: 0.051311339313346141)
    • 2,036,620 (HardCap, u: 0.099896375855056327)
  • GetNetAmountStateEssentials
    • 3,552,699 (SoftCap, u: 0.051311339313346141)
    • 8,915,843 (HardCap, u: 0.099896375855056327)
  • Escrow tokens and queue: 18,602

ClaimDiscountedReward (Total Gas Usage at SoftCap: 3,486,035 | at HardCap: 8,830,348)

  • GetNetAmountStateEssentials
    • 3,448,895 (SoftCap, u: 0.050380883554559393)
    • 8,793,208 (HardCap, u: 0.098482544159051878)
  • Burn ls tokens and send rewards: 37,140

Not Dependent on Utilization Ratio

Msg Total Gas Usage
ProvideInsurance 37,624
WithdrawInsurance 3,768
WithdrawInsuranceCommission 22,431
DepositInsurance 14,899
CancelProvideInsurance 34,497

GetNetAmountStateEssentials

This function is most gas-guzzling function in lsm.
It iterates all chunks to create NetAmountStateEssentials.

For each paired chunks, it consumes about 22,850

  • GetDelegation: 2,963
  • IncrementValidatorPeriod: 16,044
  • CalculateDelegationRewardsIncrementValidatorPeriod: 3,843

For each unbonding chunks, it consumes about 2,047

  • IterateDelegatorUnbondingDelegations: 2047

@zsystm
Copy link
Author

zsystm commented Aug 21, 2023

@dongsam
I analyzed gas consumption for all msgs in lsm at above comment.

As we can said earlier, I'll try optimize gas consumption for

LiquidUnstake

  • instead of IterateAllChunks, get unstakable chunks when calc NetAmountStateEssentials

@zsystm
Copy link
Author

zsystm commented Aug 21, 2023

@dongsam
Optimization - LiquidUnstake

  • CalcNetAmountStateEssentials return an additional map and slices.
  • So we can skip iterate chunks when queue liquid unstake that reduces gas usage.

commit: 28a6276

Effects: gas consumption reduced

  • 10,971,065(u: 0.099896375855056327) --> 7,617,535 (u: 0.100624023240769217)

@dongsam
Copy link
Member

dongsam commented Aug 21, 2023

@zsystm good,

  1. After applying the gas refund logic for the cachedCtx logic on GetNetAmountStateEssentials also, let's find the final gas consumption one more time as did in this comment
  2. After that, please remove the debug logic and create it as PR for code review

@zsystm
Copy link
Author

zsystm commented Aug 21, 2023

@dongsam
Optimization - GetNetAmountStateEssentials

  • Refund gas used by cachedCtx

Effects: a huge decrease of gas consumption

  • Liquid Unstake
    • before: 7,617,535 (u: 0.100624023240769217)
    • after: 2,628,223 (u: 0.100462988764324908)
  • Liquid Stake:
    • before: 9,584,748 (u: 0.099419919986008629)
    • after: 2,964,523 (u: 0.099681660514577146)
  • ClaimDiscountedReward:
    • before: 8,793,208 (u: 0.098482544159051878)
    • after: 2,246,126 (u: 0.098412449172946314)

commit: 6ec0f10

@zsystm zsystm mentioned this issue Aug 21, 2023
10 tasks
@github-project-automation github-project-automation bot moved this from In Progress to Done in liquidstaking-module Aug 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Development

Successfully merging a pull request may close this issue.

2 participants