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

audit fixes (2) #775

Merged
merged 5 commits into from
Sep 26, 2023
Merged

Conversation

psorinionut
Copy link
Contributor

No description provided.

@github-actions
Copy link

github-actions bot commented Sep 20, 2023

Contract comparison - from 8ac9c19 to aba2e4e

Path                                                                                             size                  has-allocator                     has-format
mx-exchange-sc
- pause-all/pause-all.wasm 5438 No No
mx-exchange-sc/dex
- farm/farm.wasm 38630 ➡️ 38696 🔴 No No
- farm-with-locked-rewards/farm-with-locked-rewards.wasm 38202 ➡️ 38299 🔴 No No
- governance/governance.wasm 17283 No No
- pair
- - pair.wasm 30520 No No
- - pair-full.wasm 31970 No No
- - safe-price-view.wasm 7862 No No
- pair-mock/pair-mock.wasm 5474 No No
- price-discovery/price-discovery.wasm 16254 No No
- proxy-deployer/proxy-deployer.wasm 4457 No No
- router/router.wasm 25991 No No
mx-exchange-sc/energy-integration
- energy-factory-mock/energy-factory-mock.wasm 4233 No No
- energy-update/energy-update.wasm 1510 No No
- fees-collector/fees-collector.wasm 18531 No No
- governance-v2/governance-v2.wasm 16822 No No
mx-exchange-sc/farm-staking
- farm-staking/farm-staking.wasm 42797 ➡️ 42863 🔴 No No
- farm-staking-proxy/farm-staking-proxy.wasm 18631 No No
- metabonding-staking/metabonding-staking.wasm 9593 No No
mx-exchange-sc/locked-asset
- distribution/distribution.wasm 14358 No No
- energy-factory/energy-factory.wasm 34193 No No
- factory/factory.wasm 30375 No No
- lkmex-transfer/lkmex-transfer.wasm 11214 No No
- locked-token-wrapper/locked-token-wrapper.wasm 14291 No No
- proxy_dex/proxy_dex.wasm 34017 No No
- simple-lock/simple-lock.wasm 24111 No No
- simple-lock-whitelist/simple-lock-whitelist.wasm 25759 No No
- token-unstake/token-unstake.wasm 13625 No No

original_owner: self.blockchain().get_sc_address(),
};

let migration_farm_token_nonce = if farm_token_mapper.get_token_state().is_set() {
Copy link
Contributor

Choose a reason for hiding this comment

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

no need to create new token. Use VM endpoint GetCurrentESDTNFTNonce / implement it to Rust Framework if it does not exist.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated.

}
}

if migrated_amount > 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

why do we have duplicated code ? Can't you add this into base function ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The farm staking contract does not import the base_functions module as there are some variables that are Farm only specific. Maybe we could rewrite this to be more generic.

Copy link
Contributor

Choose a reason for hiding this comment

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

We cannot do that due to Token Attributes. I think it's difficult to rewrite the code write now. The code changes will be huge and we would have to re-audit the whole solution.

original_owner: self.blockchain().get_sc_address(),
};

let migration_farm_token_nonce = if farm_token_mapper.get_token_state().is_set() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above - use GetCurrentESDTNFTNonce. Why do you have duplicated code ? Base function should resolv this - as code is exactly the same.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated.

Copy link
Contributor

Choose a reason for hiding this comment

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

The issue for the code duplication was farm_token. It has different attributes for farm and farm-staking.
I update the code: moved this to common module and provide the token_mapper as a parameter.

if sc.is_old_farm_position(farm_position.token_nonce) {
continue;
}

farm_token_mapper.require_same_token(&farm_position.token_identifier);

let token_attributes: FarmTokenAttributes<<Self::FarmSc as ContractBase>::Api> =
farm_token_mapper.get_token_attributes(farm_position.token_nonce);

Copy link
Contributor

Choose a reason for hiding this comment

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

Can we optimize here the storage access for user_farm_position? After the for, settle with increase/decrease of user_farm_position.

if sc.is_old_farm_position(farm_position.token_nonce) {
continue;
}

farm_token_mapper.require_same_token(&farm_position.token_identifier);

let token_attributes: FarmTokenAttributes<<Self::FarmSc as ContractBase>::Api> =
farm_token_mapper.get_token_attributes(farm_position.token_nonce);

Copy link
Contributor

Choose a reason for hiding this comment

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

Can we extract the increase/decrease of the user_farm_position outside of the for, to improve the gas consumption?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll add it in the new PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, it can be used only for increase position, as decrease position function needs the entire looped payment to check the attributes and decrease for the original owner. I'll see if there is any improvement if we send the original_owner as a parameter, instead of the payment.


if migrated_amount > 0 {
let mut user_total_farm_position = self.get_user_total_farm_position(caller);
user_total_farm_position.total_farm_position += &migrated_amount;
Copy link
Contributor

Choose a reason for hiding this comment

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

A gas improvement here could be an update instead of a get + set. (see the decrease_old_farm_positions function below).

self.get_user_total_farm_position() also verifies if position is empty, so a new function could do that (verification + update)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know if this optimizes the gas cost as much, as we still need to check if the storage is empty before the update function. And also, this would mean another function, which would increase the contract size (even if by a small margin).

CostinCarabas and others added 2 commits September 21, 2023 12:53
Moved try_set_farm_position_migration_nonce to common modules to
remove code duplication.
@psorinionut psorinionut merged commit 1d92432 into farm-position-fixes Sep 26, 2023
4 checks passed
@psorinionut psorinionut deleted the farm-position-audit-fixes-2 branch September 26, 2023 05:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants