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 the CallFilter logic. #1498

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 42 additions & 86 deletions runtime/bifrost-kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,130 +234,86 @@ parameter_types! {
pub struct CallFilter;
impl Contains<RuntimeCall> for CallFilter {
fn contains(call: &RuntimeCall) -> bool {
let is_core_call = matches!(
// Allow core system calls
if matches!(
call,
RuntimeCall::System(_) | RuntimeCall::Timestamp(_) | RuntimeCall::ParachainSystem(_)
);
if is_core_call {
// always allow core call
) {
return true;
}

// disable transfer
let is_transfer = matches!(call, RuntimeCall::Tokens(_) | RuntimeCall::Balances(_));
if is_transfer {
// Check for disabled transfer calls
if matches!(
call,
RuntimeCall::Tokens(_) | RuntimeCall::StablePool(_) | RuntimeCall::ZenlinkProtocol(_)
) {
let is_disabled = match call {
RuntimeCall::Tokens(orml_tokens::Call::transfer {
dest: _,
currency_id,
amount: _,
}) =>
VBNC == *currency_id ||
BLP_BNC_VBNC == *currency_id ||
LP_BNC_VBNC == *currency_id,
RuntimeCall::Tokens(orml_tokens::Call::transfer_all {
dest: _,
currency_id,
keep_alive: _,
}) =>
VBNC == *currency_id ||
BLP_BNC_VBNC == *currency_id ||
LP_BNC_VBNC == *currency_id,
// Disable specific `transfer`, `transfer_all`, and `transfer_keep_alive` calls for
// certain currencies
RuntimeCall::Tokens(orml_tokens::Call::transfer { currency_id, .. }) |
RuntimeCall::Tokens(orml_tokens::Call::transfer_all { currency_id, .. }) |
RuntimeCall::Tokens(orml_tokens::Call::transfer_keep_alive {
dest: _,
currency_id,
amount: _,
}) =>
VBNC == *currency_id ||
BLP_BNC_VBNC == *currency_id ||
LP_BNC_VBNC == *currency_id,
currency_id, ..
}) => [VBNC, BLP_BNC_VBNC, LP_BNC_VBNC].contains(currency_id),

// Disable StablePool calls with pool_id equal to 2
RuntimeCall::StablePool(bifrost_stable_pool::Call::add_liquidity {
pool_id,
amounts: _,
min_mint_amount: _,
}) => *pool_id == 2,
RuntimeCall::StablePool(bifrost_stable_pool::Call::swap {
pool_id,
i: _,
j: _,
dx: _,
min_dy: _,
}) => *pool_id == 2,
..
}) |
RuntimeCall::StablePool(bifrost_stable_pool::Call::swap { pool_id, .. }) |
RuntimeCall::StablePool(bifrost_stable_pool::Call::redeem_proportion {
pool_id,
amount: _,
min_redeem_amounts: _,
}) => *pool_id == 2,
..
}) |
RuntimeCall::StablePool(bifrost_stable_pool::Call::redeem_single {
pool_id,
amount: _,
i: _,
min_redeem_amount: _,
asset_length: _,
}) => *pool_id == 2,
..
}) |
RuntimeCall::StablePool(bifrost_stable_pool::Call::redeem_multi {
pool_id,
amounts: _,
max_redeem_amount: _,
..
}) => *pool_id == 2,

// Disable ZenlinkProtocol calls involving specific VBNC assets
RuntimeCall::ZenlinkProtocol(zenlink_protocol::Call::transfer {
asset_id,
recipient: _,
amount: _,
asset_id, ..
}) => *asset_id == KUSAMA_VBNC_ASSET_INDEX || *asset_id == KUSAMA_VBNC_LP_ASSET_INDEX,

RuntimeCall::ZenlinkProtocol(zenlink_protocol::Call::add_liquidity {
asset_0,
asset_1,
amount_0_desired: _,
amount_1_desired: _,
amount_0_min: _,
amount_1_min: _,
deadline: _,
}) => *asset_0 == KUSAMA_VBNC_ASSET_INDEX || *asset_1 == KUSAMA_VBNC_ASSET_INDEX,
..
}) |
RuntimeCall::ZenlinkProtocol(zenlink_protocol::Call::remove_liquidity {
asset_0,
asset_1,
liquidity: _,
amount_0_min: _,
amount_1_min: _,
recipient: _,
deadline: _,
}) => *asset_0 == KUSAMA_VBNC_ASSET_INDEX || *asset_1 == KUSAMA_VBNC_ASSET_INDEX,
RuntimeCall::ZenlinkProtocol(
zenlink_protocol::Call::swap_exact_assets_for_assets {
amount_in: _,
amount_out_min: _,
path,
recipient: _,
deadline: _,
},
) => path.contains(&KUSAMA_VBNC_ASSET_INDEX),
RuntimeCall::ZenlinkProtocol(
zenlink_protocol::Call::swap_assets_for_exact_assets {
amount_out: _,
amount_in_max: _,
path,
recipient: _,
deadline: _,
},
) => path.contains(&KUSAMA_VBNC_ASSET_INDEX),
..
}) |
RuntimeCall::ZenlinkProtocol(zenlink_protocol::Call::bootstrap_claim {
recipient: _,
asset_0,
asset_1,
deadline: _,
..
}) => *asset_0 == KUSAMA_VBNC_ASSET_INDEX || *asset_1 == KUSAMA_VBNC_ASSET_INDEX,

// Disable ZenlinkProtocol swap calls if the path contains VBNC assets
RuntimeCall::ZenlinkProtocol(
zenlink_protocol::Call::swap_exact_assets_for_assets { path, .. },
) |
RuntimeCall::ZenlinkProtocol(
zenlink_protocol::Call::swap_assets_for_exact_assets { path, .. },
) => path.contains(&KUSAMA_VBNC_ASSET_INDEX),

_ => false,
};

// If the call is disabled, log it and return false
if is_disabled {
// no switched off call
return false;
}
}

// Allow all other calls
true
}
}
Expand Down