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

[Feature] Reduce gas cost #13

Open
cyraxred opened this issue Sep 14, 2022 · 3 comments
Open

[Feature] Reduce gas cost #13

cyraxred opened this issue Sep 14, 2022 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@cyraxred
Copy link

cyraxred commented Sep 14, 2022

https://github.com/windranger-io/windranger-mass-delegation-contracts/blob/main/contracts/MerkleDelegation.sol#L13

considering that DelegateeRecord.percentage is limited to 100_000, it can be changed to uint24. And as DelegateeRecord.delegate is address ( == uint160), then DelegateeRecord will take only 1 slot (24 + 160 < 256) instead of 2 slots, hence write and read gas will be 2 times less

@cyraxred cyraxred added the enhancement New feature or request label Sep 14, 2022
@jio-gl
Copy link
Contributor

jio-gl commented Sep 15, 2022

Thanks, I think we can use uint64 and leave the whole number as a fraction of 2**64 in case we are delegating to more than 100,000 addresses. Also, there is a thing with precision, because we are using BigNumber off-chain and fractions on-chain we have to double check there are no discrepancies due to the hashing. If we do both on-chain and off-chain (currently on-chain only fraction is checked, not net balance, but can be added).

On-chain calculation vs Off-chain calculation
floor(delegator_balance * total_weight * 2^64 / weight_fraction_uint64) == floor(delegator_balance * weight_BigNumber / delegator_total_weight

For example, the total_weight is 100, if the delegator is splitting into 60,28,12 between 3 delegatees.

@jio-gl
Copy link
Contributor

jio-gl commented Sep 15, 2022

Another solution is to store the total weight also on-chain and then we have the exact BigNumber data to compare with off-chain data.

@jio-gl jio-gl self-assigned this Sep 15, 2022
@jio-gl
Copy link
Contributor

jio-gl commented Sep 15, 2022

Also, the percentage or weight is only used in the verification that is a view function, so not sure it will impact gas costs.

https://github.com/windranger-io/windranger-mass-delegation-contracts/blob/jo-delegate2/contracts/MerkleDelegation.sol

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
None yet
Development

No branches or pull requests

2 participants