Skip to content

A implementation for asset disabling #32

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

Merged
merged 2 commits into from
Jan 15, 2025

Conversation

celestialkylin
Copy link
Contributor

Hi!

I have provided an implementation for the asset disabling feature here. Please take a look. Thanks!
The main changes are:

  • Added a flag in "ReserveConfig" to indicate whether the reserve is enabled or disabled.
  • Added a "ResFlagsSummary" storage to help check reserve flags efficiently and quickly.
  • Added a "require_action_allowed" function in "Reserve" to check the reserve's status.
  • Added filtering logic in "do_gulp_emissions".

Looking forward to your reply

Copy link
Contributor

@mootz12 mootz12 left a comment

Choose a reason for hiding this comment

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

Functionality of the enabled flag looks good.

A few recommendations to clean up the implementation. Please let me know if you have questions about any of the feedback provided.

@@ -54,6 +63,7 @@ pub struct ReserveConfig {
pub r_three: u32, // the R3 value in the interest rate formula scaled expressed in 7 decimals
pub reactivity: u32, // the reactivity constant for the reserve scaled expressed in 7 decimals
pub collateral_cap: i128, // the total amount of underlying tokens that can be used as collateral
pub flags: u32, // the flag of the reserve
Copy link
Contributor

Choose a reason for hiding this comment

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

Curious if there was a reason to store this as a u32 instead of a boolean?

At the moment we have no additional flag types planned, and the implementation would be simpler if we opted to do:

pub enabled: bool, // if the reserve is enabled. If the reserve is not enabled, positions can only be removed for this reserve.

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 was just considering struct alignment and future extensibility. Yes, it might be unnecessary and over-designed. Using a bool would be fine

@@ -133,6 +134,7 @@ pub fn build_actions_from_request(
}
RequestType::Withdraw => {
let mut reserve = pool.load_reserve(e, &request.address, true);
reserve.require_action_allowed(e, request.request_type);
Copy link
Contributor

Choose a reason for hiding this comment

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

withdrawing assets is always allowed, regardless of any combination of pool or reserve status, thus this call is not needed

@@ -207,6 +212,7 @@ pub fn build_actions_from_request(
}
RequestType::Repay => {
let mut reserve = pool.load_reserve(e, &request.address, true);
reserve.require_action_allowed(e, request.request_type);
Copy link
Contributor

Choose a reason for hiding this comment

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

repaying assets is always allowed, regardless of any combination of pool or reserve status, thus this call is not needed.

Comment on lines 425 to 450
/// Fetch the reserve flags summary
pub fn get_res_flags_summary(e: &Env) -> Map<Address, u32> {
get_persistent_default(
e,
&Symbol::new(e, RES_FLAGS_SUMMARY),
|| map![e],
LEDGER_THRESHOLD_SHARED,
LEDGER_BUMP_SHARED,
)
}

/// Set the reserve flags summary
///
/// ### Arguments
/// * `summary` - The map of reserve's status summary by reserve address to a status u32
pub fn set_res_flags_summary(e: &Env, summary: &Map<Address, u32>) {
e.storage()
.persistent()
.set::<Symbol, Map<Address, u32>>(&Symbol::new(e, RES_FLAGS_SUMMARY), summary);
e.storage().persistent().extend_ttl(
&Symbol::new(e, RES_FLAGS_SUMMARY),
LEDGER_THRESHOLD_SHARED,
LEDGER_BUMP_SHARED,
);
}

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this optimization is necessary, as we need to load reserve_config for each asset when conduction gulp_emissions anyway. Please see comment on emissions/manager.rs for context.

Thus, don't need to maintain a second source of truth for the asset status, and can remove the res_flag_summary code.

.unwrap_optimized()
.fixed_mul_floor(new_emissions, SCALAR_7)
.unwrap_optimized();
update_reserve_emission_eps(e, &res_asset_address, res_token_id, new_reserve_emissions);
Copy link
Contributor

Choose a reason for hiding this comment

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

update_reserve_emissions_eps loads the reserve config for each asset anyway.

My suggestion:

  1. Use reserve_config to load the asset's enabled status
  2. Pass the reserve_config into update_reserve_emission_eps instead of the res_token_id to avoid loading it twice. reserve_config.id === res_token_id

Copy link
Contributor

Choose a reason for hiding this comment

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

This will likely require us to build a pool_emis_enabled: Vec<(reserve_config, res_eps_share)> vector during the first pass that determines the total_share, to also avoid loading the reserve_config twice.

remove "res_flag_summary" storage
"do_gulp_emissions" modified
some other code modified
@celestialkylin
Copy link
Contributor Author

Hello @mootz12 , I have modified the code based on your feedback. Please take a look. Thanks!

Copy link
Contributor

@mootz12 mootz12 left a comment

Choose a reason for hiding this comment

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

LG2M. Thanks for the contribution!

@mootz12 mootz12 merged commit cdd8341 into blend-capital:main Jan 15, 2025
3 checks passed
@mootz12 mootz12 linked an issue Jan 15, 2025 that may be closed by this pull request
@celestialkylin celestialkylin deleted the feat_asset_disabling branch January 21, 2025 05:28
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.

Safe asset disabling
2 participants