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

Optimize Stake 2.0 related code #5424

Closed
CarlChaoCarl opened this issue Aug 15, 2023 · 1 comment · Fixed by #5426
Closed

Optimize Stake 2.0 related code #5424

CarlChaoCarl opened this issue Aug 15, 2023 · 1 comment · Fixed by #5426
Assignees

Comments

@CarlChaoCarl
Copy link
Contributor

CarlChaoCarl commented Aug 15, 2023

Rationale

After the stake2.0 code was launched, some of the codes were found that could be optimized, and here these codes are proposed for discussion and optimization.

  1. call in the for loop: votesCapsule.addNewVotes

    • Solution: It is recommended to add to the list first, and then call addAll
  2. The code of estimateConsumeBandWidthSize is too complicated and needs to be simplified

    • Solution: It is recommended to remove redundant code and streamline the code.
  3. GetCanDelegatedMaxSize has many repeated codes.

    • Solution: It is recommended to encapsulate it into a function and reuse the code.
  4. The description of exception information in stake 2.0 is inaccurate.

    • Solution: It is recommended to describe the exception information accurately.

Implementation

  1. Located in: updateVote method of UnfreezeBalanceV2Actuator class

    • Add a List addVotes instead and do addAll once.
  2. Located in: estimateConsumeBandWidthSize method of TransactionUtil class

    • There are some redundant codes in the method of calculating the estimated transaction size, which can be simplified
  3. The getCanDelegatedMaxSize function in the Wallet are duplicated with other codes in the other place, and can be reused

    • It can be abstracted: getV2NetUsage, getV2EnergyUsage for code reuse.
  4. Optimize the description of Stake 2.0 exception messages
    - When delegateBalance < TRX_PRECISION in DelegateResourceActuator.java, description 'delegateBalance must be more than 1 TRX' is replaced with'delegateBalance must be greater than or equal to 1 TRX'
    - When ownerCapsule.getFrozenV2BalanceForBandwidth() - remainNetUsage < delegateBalance in DelegateResourceActuator.java, description 'delegateBalance must be less than available FreezeBandwidthV2 balance' is replaced with 'delegateBalance must be less than or equal to available FreezeBandwidthV2 balance'
    - When ownerCapsule.getFrozenV2BalanceForEnergy() - remainEnergyUsage < delegateBalance in DelegateResourceActuator.java, description 'delegateBalance must be less than available FreezeEnergyV2 balance' is replaced with 'delegateBalance must be less than or equal to available FreezeEnergyV2 balance'
    - When frozenBalance < TRX_PRECISION in FreezeBalanceActuator.java, description 'frozenBalance must be more than 1TRX' is replaced with 'frozenBalance must be greater than or equal to 1 TRX'
    - When frozenBalance > accountCapsule.getBalance() in FreezeBalanceActuator.java, description 'frozenBalance must be less than accountBalance' is replaced with 'frozenBalance must be less than or equal to accountBalance'
    - When frozenBalance < TRX_PRECISION in FreezeBalanceV2Actuator.java, description 'frozenBalance must be more than 1TRX' is replaced with 'frozenBalance must be greater than or equal to 1 TRX'
    - When frozenBalance > accountCapsule.getBalance() in FreezeBalanceV2Actuator.java, description 'frozenBalance must be less than accountBalance' is replaced with 'frozenBalance must be less than or equal to accountBalance'
    - When delegateBalance < TRX_PRECISION in DelegateResourceProcessor.java, description 'delegateBalance must be more than 1TRX' is replaced with 'delegateBalance must be greater than or equal to 1 TRX'
    - When ownerCapsule.getFrozenV2BalanceForBandwidth() - v2NetUsage < delegateBalance in DelegateResourceProcessor.java, description 'delegateBalance must be less than available FreezeBandwidthV2 balance' is replaced with 'delegateBalance must be less than or equal to available FreezeBandwidthV2 balance'
    - When ownerCapsule.getFrozenV2BalanceForEnergy() - v2EnergyUsage < delegateBalance in DelegateResourceProcessor.java, description 'delegateBalance must be less than available FreezeEnergyV2 balance' is replaced with 'delegateBalance must be less than or equal to available FreezeEnergyV2 balance'
    - When frozenBalance < TRX_PRECISION in FreezeBalanceProcessor.java, description 'FrozenBalance must be less than accountBalance' is replaced with 'FrozenBalance must be less than or equal to accountBalance'
    - When frozenBalance < TRX_PRECISION in FreezeBalanceV2Processor.java, description 'FrozenBalance must be more than 1TRX' is replaced with 'FrozenBalance must be greater than or equal to 1 TRX'
    - When frozenBalance > ownerCapsule.getBalance() in FreezeBalanceV2Processor.java, description 'FrozenBalance must be less than accountBalance' is replaced with 'FrozenBalance must be less than or equal to accountBalance'

Are you willing to implement this feature?

Yes, I have completed the code

@lxcmyf
Copy link
Contributor

lxcmyf commented Aug 17, 2023

@CarlChaoCarl
Making code simpler and easier to read is always a worthwhile direction forward.

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

Successfully merging a pull request may close this issue.

2 participants