b-harvest / Canto

GNU Lesser General Public License v3.0
3 stars 4 forks source link

feat: gas optimization #33

Closed zsystm closed 1 year ago

zsystm commented 1 year ago

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

Need to optimize gas consumption.

zsystm commented 1 year ago

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
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 commented 1 year ago

Dependent on Utilization Ratio

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

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

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

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

For each unbonding chunks, it consumes about 2,047

zsystm commented 1 year ago

@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

zsystm commented 1 year ago

@dongsam Optimization - LiquidUnstake

commit: https://github.com/b-harvest/Canto/commit/28a62769d64c00f236239d1d1ae5bb147481fe00

Effects: gas consumption reduced

dongsam commented 1 year ago

@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 commented 1 year ago

@dongsam Optimization - GetNetAmountStateEssentials

Effects: a huge decrease of gas consumption

commit: https://github.com/b-harvest/Canto/commit/6ec0f107a752a83d4183256d2bb74807c191324c