Skip to content
This repository has been archived by the owner on Nov 15, 2024. It is now read-only.

cleanup function for unused storage in AH contract #436

Merged
merged 9 commits into from
Jul 12, 2024
7 changes: 2 additions & 5 deletions bindings/src/account_history/msg/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@ pub enum ExecuteMsg {
delete_old_data_enabled: Option<bool>,
delete_epoch: Option<u64>,
},
UpdateAccount {},
CleanHistory {
limit: u64,
},
CleanOldHistory {
CleanStorage {
cryptokage1996 marked this conversation as resolved.
Show resolved Hide resolved
limit: u64,
},
CleanStorageBulk {},
}
2 changes: 2 additions & 0 deletions bindings/src/account_history/msg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub use sudo::SudoMsg;
pub mod query_resp {
mod get_all_resp;
mod get_rewards_resp;
mod get_storage_size;
mod liquid_asset;
mod membership_tier_response;
mod params_resp;
Expand All @@ -21,6 +22,7 @@ pub mod query_resp {

pub use get_all_resp::GetAllResp;
pub use get_rewards_resp::GetRewardsResp;
pub use get_storage_size::StorageSizeResp;
pub use liquid_asset::LiquidAsset;
pub use membership_tier_response::MembershipTierResponse;
pub use params_resp::ParamsResp;
Expand Down
4 changes: 2 additions & 2 deletions bindings/src/account_history/msg/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ pub enum QueryMsg {
IncentiveAprs {},

#[cfg(feature = "debug")]
#[returns(Uint128)]
AddressQueueSize {},
#[returns(StorageSizeResp)]
StorageSize {},

#[cfg(feature = "debug")]
#[returns(ContractVersion)]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use cosmwasm_schema::cw_serde;

#[cw_serde]
pub struct StorageSizeResp {
pub user_address_queue_data_size: u128,
pub history_data_size: u128,
pub old_history_2_data_size: u128,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use cosmwasm_std::{DepsMut, Response, StdResult};

use crate::states::{HISTORY, OLD_HISTORY_2, USER_ADDRESS_QUEUE};
use elys_bindings::{ElysMsg, ElysQuery};

pub fn clean_up_storage(deps: &mut DepsMut<ElysQuery>, limit: u64) -> StdResult<Response<ElysMsg>> {
// Delete history values
for _ in 0..limit {
if let Some(val) = HISTORY.first(deps.storage)? {
HISTORY.remove(deps.storage, &val.0);
}
if let Some(val) = OLD_HISTORY_2.first(deps.storage)? {
OLD_HISTORY_2.remove(deps.storage, &val.0);
}
if !USER_ADDRESS_QUEUE.is_empty(deps.storage).unwrap() {
let _ = USER_ADDRESS_QUEUE.pop_front(deps.storage);
}
}
Ok(Response::default())
}
9 changes: 2 additions & 7 deletions contracts/account-history-contract/src/action/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
pub mod sudo {
mod update_account_chain;
pub use update_account_chain::{
clean_old_history, clean_up_history, update_account, update_account_chain,
};
}

pub mod query {
mod get_liquid_assets;
use crate::error::ContractError;
Expand Down Expand Up @@ -70,5 +63,7 @@ pub mod query {

pub mod execute {
mod add_user_address_to_queue;
mod clean_up_storage;
pub use add_user_address_to_queue::add_user_address_to_queue;
pub use clean_up_storage::clean_up_storage;
}
3 changes: 0 additions & 3 deletions contracts/account-history-contract/src/action/sudo/mod.rs

This file was deleted.

This file was deleted.

29 changes: 10 additions & 19 deletions contracts/account-history-contract/src/entry_point/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@ use cosmwasm_std::{entry_point, DepsMut, Env, MessageInfo, Response, StdError, S
use elys_bindings::{account_history::msg::ExecuteMsg, ElysMsg, ElysQuery};

use crate::{
action::{
execute::add_user_address_to_queue,
sudo::{clean_old_history, clean_up_history, update_account_chain},
},
action::execute::{add_user_address_to_queue, clean_up_storage},
states::{
DELETE_EPOCH, DELETE_OLD_DATA_ENABLED, PARAMS_ADMIN, PROCESSED_ACCOUNT_PER_BLOCK,
TRADE_SHIELD_ADDRESS, UPDATE_ACCOUNT_ENABLED,
DELETE_EPOCH, DELETE_OLD_DATA_ENABLED, HISTORY, OLD_HISTORY_2, PARAMS_ADMIN,
PROCESSED_ACCOUNT_PER_BLOCK, TRADE_SHIELD_ADDRESS, UPDATE_ACCOUNT_ENABLED,
},
};

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn execute(
mut deps: DepsMut<ElysQuery>,
env: Env,
_env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> StdResult<Response<ElysMsg>> {
Expand Down Expand Up @@ -60,26 +57,20 @@ pub fn execute(
}
Ok(Response::new())
}
ExecuteMsg::UpdateAccount {} => {
if info.sender != PARAMS_ADMIN.load(deps.storage)? {
return Err(StdError::generic_err("Unauthorized"));
}
let resp = update_account_chain(deps, env)?;
Ok(resp)
}
ExecuteMsg::CleanHistory { limit } => {
ExecuteMsg::CleanStorage { limit } => {
if info.sender != PARAMS_ADMIN.load(deps.storage)? {
return Err(StdError::generic_err("Unauthorized"));
}
let resp = clean_up_history(&mut deps, env, limit)?;
let resp = clean_up_storage(&mut deps, limit)?;
Ok(resp)
}
ExecuteMsg::CleanOldHistory { limit } => {
ExecuteMsg::CleanStorageBulk {} => {
if info.sender != PARAMS_ADMIN.load(deps.storage)? {
return Err(StdError::generic_err("Unauthorized"));
}
let resp = clean_old_history(&mut deps, limit)?;
Ok(resp)
HISTORY.clear(deps.storage);
OLD_HISTORY_2.clear(deps.storage);
Ok(Response::new())
}
}
}
20 changes: 16 additions & 4 deletions contracts/account-history-contract/src/entry_point/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
get_membership_tier, get_perpetuals_assets, get_pool_balances, get_rewards,
get_staked_assets, get_usdc_earn_program_details,
},
states::USER_ADDRESS_QUEUE,
states::{HISTORY, OLD_HISTORY_2, USER_ADDRESS_QUEUE},
types::AccountSnapshotGenerator,
};

Expand All @@ -19,7 +19,9 @@ use crate::action::query::{
use cosmwasm_std::{entry_point, to_json_binary, Binary, Deps, Env, StdResult};
use cw2::CONTRACT;
use elys_bindings::{
account_history::types::ElysDenom, query_resp::QueryAprResponse, ElysQuerier, ElysQuery,
account_history::{msg::query_resp::StorageSizeResp, types::ElysDenom},
query_resp::QueryAprResponse,
ElysQuerier, ElysQuery,
};

use crate::msg::QueryMsg;
Expand Down Expand Up @@ -254,10 +256,20 @@ pub fn query(deps: Deps<ElysQuery>, env: Env, msg: QueryMsg) -> StdResult<Binary

to_json_binary(&response)
}
AddressQueueSize {} => {
StorageSize {} => {
let user_address_queue_data_size = USER_ADDRESS_QUEUE.len(deps.storage)? as u128;
let history_data_size = HISTORY
.keys(deps.storage, None, None, cosmwasm_std::Order::Descending)
.count() as u128;
let old_history_2_data_size = OLD_HISTORY_2
.keys(deps.storage, None, None, cosmwasm_std::Order::Descending)
.count() as u128;

to_json_binary(&user_address_queue_data_size)
to_json_binary(&StorageSizeResp {
user_address_queue_data_size,
history_data_size,
old_history_2_data_size,
})
}
Version {} => to_json_binary(&CONTRACT.load(deps.storage)?),
}
Expand Down
26 changes: 8 additions & 18 deletions contracts/account-history-contract/src/entry_point/sudo.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
use crate::msg::SudoMsg;
use crate::states::{DELETE_EPOCH, DELETE_OLD_DATA_ENABLED};
use crate::{
action::sudo::{clean_old_history, clean_up_history, update_account},
states::UPDATE_ACCOUNT_ENABLED,
};
use cosmwasm_std::{entry_point, DepsMut, Env, Response, StdError, StdResult};
use crate::action::execute::clean_up_storage;
use crate::states::DELETE_OLD_DATA_ENABLED;
use crate::{msg::SudoMsg, states::DELETE_EPOCH};
use cosmwasm_std::{entry_point, DepsMut, Env, Response, StdResult};
use elys_bindings::{ElysMsg, ElysQuery};

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn sudo(mut deps: DepsMut<ElysQuery>, env: Env, msg: SudoMsg) -> StdResult<Response<ElysMsg>> {
pub fn sudo(mut deps: DepsMut<ElysQuery>, _env: Env, msg: SudoMsg) -> StdResult<Response<ElysMsg>> {
match msg {
SudoMsg::ClockEndBlock {} => {
if UPDATE_ACCOUNT_ENABLED.load(deps.storage)? == false {
return Err(StdError::generic_err("Update account is disabled"));
}

let epoch = DELETE_EPOCH.load(deps.storage)?;
if DELETE_OLD_DATA_ENABLED.load(deps.storage)? == true && env.block.height % epoch == 0
{
clean_old_history(&mut deps, epoch)?;
clean_up_history(&mut deps, env.clone(), epoch)?;
if DELETE_OLD_DATA_ENABLED.load(deps.storage)? == true {
clean_up_storage(&mut deps, epoch)?;
}

update_account(deps, env)
return Ok(Response::new());
}
}
}
5 changes: 3 additions & 2 deletions contracts/account-history-contract/src/tests/get_all_pools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,9 @@ fn get_all_pools() {
)
.unwrap();

app.wasm_sudo(addr.clone(), &SudoMsg::ClockEndBlock {})
.unwrap();
let msg = app.wasm_sudo(addr.clone(), &SudoMsg::ClockEndBlock {});

println!("{:?}", msg);

let resp: QueryEarnPoolResponse = app
.wrap()
Expand Down
2 changes: 0 additions & 2 deletions contracts/account-history-contract/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@ mod get_liquid_assets;
mod get_rewards;
mod get_staked_assets;
mod get_usdc_earn_program_details;
mod update_accounts;
mod week_snapshots;
Loading
Loading