Skip to content

Commit

Permalink
Merge pull request #39 from sander2/chore/remove-liquidation-error-state
Browse files Browse the repository at this point in the history
chore: remove unused liquidation error state
  • Loading branch information
gregdhill authored Mar 19, 2021
2 parents 0bdee77 + 61cbca5 commit ac9848c
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 86 deletions.
8 changes: 4 additions & 4 deletions crates/btc-relay/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1007,8 +1007,8 @@ fn test_flag_block_error_fails() {
let blockchain = get_empty_block_chain_from_chain_id_and_height(chain_ref, start_height, block_height);
BTCRelay::set_block_chain_from_id(chain_ref, &blockchain);

// not a valid error code
let error = ErrorCode::Liquidation;
// not a valid error code for a block
let error = ErrorCode::OracleOffline;

assert_err!(
BTCRelay::flag_block_error(rich_header.block_hash, error),
Expand Down Expand Up @@ -1094,8 +1094,8 @@ fn test_clear_block_error_fails() {
let blockchain = get_empty_block_chain_from_chain_id_and_height(chain_ref, start_height, block_height);
BTCRelay::set_block_chain_from_id(chain_ref, &blockchain);

// not a valid error code
let error = ErrorCode::Liquidation;
// not a valid error code for a block
let error = ErrorCode::OracleOffline;

assert_err!(
BTCRelay::clear_block_error(rich_header.block_hash, error),
Expand Down
10 changes: 3 additions & 7 deletions crates/redeem/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,15 @@ pub(crate) mod treasury {

#[cfg_attr(test, mockable)]
pub(crate) mod security {
use frame_support::dispatch::DispatchResult;
use frame_support::dispatch::DispatchError;
use primitive_types::H256;
use security::ErrorCode;
use sp_std::vec::Vec;

pub fn get_secure_id<T: security::Config>(id: &T::AccountId) -> H256 {
<security::Module<T>>::get_secure_id(id)
}

pub fn ensure_parachain_is_running_or_only_has_errors<T: security::Config>(
error_codes: Vec<ErrorCode>,
) -> DispatchResult {
<security::Module<T>>::ensure_parachain_is_running_or_only_has_errors(error_codes)
pub fn ensure_parachain_status_running<T: security::Config>() -> Result<(), DispatchError> {
<security::Module<T>>::ensure_parachain_status_running()
}
}

Expand Down
12 changes: 3 additions & 9 deletions crates/redeem/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ use frame_support::{
};
use frame_system::{ensure_root, ensure_signed};
use primitive_types::H256;
use security::ErrorCode;
use sp_runtime::{traits::*, ModuleId};
use sp_std::{convert::TryInto, vec::Vec};
use vault_registry::CurrencySource;
Expand Down Expand Up @@ -232,7 +231,7 @@ impl<T: Config> Module<T> {
btc_address: BtcAddress,
vault_id: T::AccountId,
) -> Result<H256, DispatchError> {
Self::ensure_parachain_running_or_error_liquidated()?;
ext::security::ensure_parachain_status_running::<T>()?;

let redeemer_balance = ext::treasury::get_balance::<T>(redeemer.clone());
ensure!(
Expand Down Expand Up @@ -309,7 +308,7 @@ impl<T: Config> Module<T> {
}

fn _liquidation_redeem(redeemer: T::AccountId, amount_polka_btc: PolkaBTC<T>) -> Result<(), DispatchError> {
Self::ensure_parachain_running_or_error_liquidated()?;
ext::security::ensure_parachain_status_running::<T>()?;

let redeemer_balance = ext::treasury::get_balance::<T>(redeemer.clone());
ensure!(
Expand All @@ -333,7 +332,7 @@ impl<T: Config> Module<T> {
merkle_proof: Vec<u8>,
raw_tx: Vec<u8>,
) -> Result<(), DispatchError> {
Self::ensure_parachain_running_or_error_liquidated()?;
ext::security::ensure_parachain_status_running::<T>()?;

let redeem = Self::get_open_redeem_request_from_id(&redeem_id)?;

Expand Down Expand Up @@ -498,11 +497,6 @@ impl<T: Config> Module<T> {
Ok(())
}

/// Ensure that the parachain is running or the system is in liquidation
fn ensure_parachain_running_or_error_liquidated() -> DispatchResult {
ext::security::ensure_parachain_is_running_or_only_has_errors::<T>([ErrorCode::Liquidation].to_vec())
}

/// Insert a new redeem request into state.
///
/// # Arguments
Expand Down
6 changes: 0 additions & 6 deletions crates/security/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,6 @@ impl<T: Config> Module<T> {
<ParachainStatus>::get() == StatusCode::Error && <Errors>::get().contains(&ErrorCode::OracleOffline)
}

/// Checks if the Parachain has a Liquidation Error state
pub fn is_parachain_error_liquidation() -> bool {
<ParachainStatus>::get() == StatusCode::Error && <Errors>::get().contains(&ErrorCode::Liquidation)
}

/// Gets the current `StatusCode`.
pub fn get_parachain_status() -> StatusCode {
<ParachainStatus>::get()
Expand Down Expand Up @@ -309,7 +304,6 @@ impl<T: Config> From<ErrorCode> for Error<T> {
ErrorCode::NoDataBTCRelay => Error::NoDataBTCRelay,
ErrorCode::InvalidBTCRelay => Error::InvalidBTCRelay,
ErrorCode::OracleOffline => Error::ParachainOracleOfflineError,
ErrorCode::Liquidation => Error::ParachainLiquidationError,
_ => Error::InvalidErrorCode,
}
}
Expand Down
9 changes: 0 additions & 9 deletions crates/security/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,6 @@ fn test_is_parachain_error_oracle_offline() {
})
}

#[test]
fn test_is_parachain_error_liquidation() {
run_test(|| {
Security::set_status(StatusCode::Error);
Security::insert_error(ErrorCode::Liquidation);
assert_eq!(Security::is_parachain_error_liquidation(), true);
})
}

fn test_recover_from_<F>(recover: F, error_codes: Vec<ErrorCode>)
where
F: FnOnce(),
Expand Down
6 changes: 3 additions & 3 deletions crates/staked-relayers/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -708,20 +708,20 @@ fn test_force_status_update_succeeds() {
assert_ok!(StakedRelayers::force_status_update(
Origin::signed(ALICE),
StatusCode::Shutdown,
Some(ErrorCode::Liquidation),
Some(ErrorCode::OracleOffline),
None
));

assert_eq!(ext::security::get_parachain_status::<Test>(), StatusCode::Shutdown);

assert_emitted!(Event::ForceStatusUpdate(
StatusCode::Shutdown,
Some(ErrorCode::Liquidation),
Some(ErrorCode::OracleOffline),
None
));

let errors = ext::security::get_errors::<Test>();
assert_eq!(errors.contains(&ErrorCode::Liquidation), true);
assert_eq!(errors.contains(&ErrorCode::OracleOffline), true);
})
}

Expand Down
56 changes: 8 additions & 48 deletions crates/vault-registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,12 +506,7 @@ impl<T: Config> Module<T> {
/// * `tokens` - the amount of tokens to be unreserved
pub fn decrease_to_be_issued_tokens(vault_id: &T::AccountId, tokens: PolkaBTC<T>) -> DispatchResult {
Self::check_parachain_not_shutdown_and_not_errors(
[
ErrorCode::InvalidBTCRelay,
ErrorCode::OracleOffline,
ErrorCode::Liquidation,
]
.to_vec(),
[ErrorCode::InvalidBTCRelay, ErrorCode::OracleOffline].to_vec(),
)?;

let mut vault = Self::get_rich_vault_from_id(vault_id)?;
Expand All @@ -534,12 +529,7 @@ impl<T: Config> Module<T> {
/// * `InsufficientTokensCommitted` - if the amount of tokens reserved is too low
pub fn issue_tokens(vault_id: &T::AccountId, tokens: PolkaBTC<T>) -> DispatchResult {
Self::check_parachain_not_shutdown_and_not_errors(
[
ErrorCode::InvalidBTCRelay,
ErrorCode::OracleOffline,
ErrorCode::Liquidation,
]
.to_vec(),
[ErrorCode::InvalidBTCRelay, ErrorCode::OracleOffline].to_vec(),
)?;
let mut vault = Self::get_rich_vault_from_id(&vault_id)?;
vault.issue_tokens(tokens)?;
Expand All @@ -563,12 +553,7 @@ impl<T: Config> Module<T> {
/// * `InsufficientTokensCommitted` - if the amount of redeemable tokens is too low
pub fn try_increase_to_be_redeemed_tokens(vault_id: &T::AccountId, tokens: PolkaBTC<T>) -> DispatchResult {
Self::check_parachain_not_shutdown_and_not_errors(
[
ErrorCode::InvalidBTCRelay,
ErrorCode::OracleOffline,
ErrorCode::Liquidation,
]
.to_vec(),
[ErrorCode::InvalidBTCRelay, ErrorCode::OracleOffline].to_vec(),
)?;
let mut vault = Self::get_active_rich_vault_from_id(&vault_id)?;
let redeemable = vault
Expand All @@ -595,12 +580,7 @@ impl<T: Config> Module<T> {
/// * `InsufficientTokensCommitted` - if the amount of to-be-redeemed tokens is too low
pub fn decrease_to_be_redeemed_tokens(vault_id: &T::AccountId, tokens: PolkaBTC<T>) -> DispatchResult {
Self::check_parachain_not_shutdown_and_not_errors(
[
ErrorCode::InvalidBTCRelay,
ErrorCode::OracleOffline,
ErrorCode::Liquidation,
]
.to_vec(),
[ErrorCode::InvalidBTCRelay, ErrorCode::OracleOffline].to_vec(),
)?;

let mut vault = Self::get_rich_vault_from_id(&vault_id)?;
Expand All @@ -621,12 +601,7 @@ impl<T: Config> Module<T> {
/// * `user_id` - the id of the user making the redeem request
pub fn decrease_tokens(vault_id: &T::AccountId, user_id: &T::AccountId, tokens: PolkaBTC<T>) -> DispatchResult {
Self::check_parachain_not_shutdown_and_not_errors(
[
ErrorCode::InvalidBTCRelay,
ErrorCode::OracleOffline,
ErrorCode::Liquidation,
]
.to_vec(),
[ErrorCode::InvalidBTCRelay, ErrorCode::OracleOffline].to_vec(),
)?;
// decrease to-be-redeemed and issued
let mut vault = Self::get_rich_vault_from_id(&vault_id)?;
Expand All @@ -650,12 +625,7 @@ impl<T: Config> Module<T> {
redeemer_id: &T::AccountId,
) -> DispatchResult {
Self::check_parachain_not_shutdown_and_not_errors(
[
ErrorCode::InvalidBTCRelay,
ErrorCode::OracleOffline,
ErrorCode::Liquidation,
]
.to_vec(),
[ErrorCode::InvalidBTCRelay, ErrorCode::OracleOffline].to_vec(),
)?;

let mut vault = Self::get_rich_vault_from_id(&vault_id)?;
Expand Down Expand Up @@ -769,12 +739,7 @@ impl<T: Config> Module<T> {
collateral: DOT<T>,
) -> DispatchResult {
Self::check_parachain_not_shutdown_and_not_errors(
[
ErrorCode::InvalidBTCRelay,
ErrorCode::OracleOffline,
ErrorCode::Liquidation,
]
.to_vec(),
[ErrorCode::InvalidBTCRelay, ErrorCode::OracleOffline].to_vec(),
)?;

let mut old_vault = Self::get_rich_vault_from_id(&old_vault_id)?;
Expand Down Expand Up @@ -815,12 +780,7 @@ impl<T: Config> Module<T> {
tokens: PolkaBTC<T>,
) -> DispatchResult {
Self::check_parachain_not_shutdown_and_not_errors(
[
ErrorCode::InvalidBTCRelay,
ErrorCode::OracleOffline,
ErrorCode::Liquidation,
]
.to_vec(),
[ErrorCode::InvalidBTCRelay, ErrorCode::OracleOffline].to_vec(),
)?;

let mut old_vault = Self::get_rich_vault_from_id(&old_vault_id)?;
Expand Down

0 comments on commit ac9848c

Please sign in to comment.