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

[BREAKING] Feat: handle stopping of oracle submissions better #1119

Merged
merged 3 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion crates/btc-relay/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ impl pallet_timestamp::Config for Test {
impl security::Config for Test {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
type MaxErrors = ConstU32<1>;
}

pub type TestEvent = RuntimeEvent;
Expand Down
1 change: 0 additions & 1 deletion crates/fee/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ impl pallet_timestamp::Config for Test {
impl security::Config for Test {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
type MaxErrors = ConstU32<1>;
}

pub struct CurrencyConvert;
Expand Down
1 change: 0 additions & 1 deletion crates/issue/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ impl btc_relay::Config for Test {
impl security::Config for Test {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
type MaxErrors = ConstU32<1>;
}

parameter_types! {
Expand Down
4 changes: 1 addition & 3 deletions crates/loans/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use primitives::{
KSM as KSM_CURRENCY,
};
use rate_model::{InterestRateModel, JumpModel};
use security::{Pallet as Security, StatusCode};
use security::Pallet as Security;
use sp_std::prelude::*;

const SEED: u32 = 0;
Expand Down Expand Up @@ -622,7 +622,6 @@ pub mod benchmarks {
initialize::<T>();

Security::<T>::set_active_block_number(1u32.into());
Security::<T>::set_status(StatusCode::Running);
let alice: T::AccountId = account("Sample", 100, SEED);
let bob: T::AccountId = account("Sample", 101, SEED);
transfer_initial_balance::<T>(alice.clone());
Expand Down Expand Up @@ -815,6 +814,5 @@ fn initialize<
+ currency::Config<UnsignedFixedPoint = UnsignedFixedPoint>,
>() {
Security::<T>::set_active_block_number(1u32.into());
Security::<T>::set_status(StatusCode::Running);
assert_ok!(Oracle::<T>::_set_exchange_rate(KBTC, UnsignedFixedPoint::one()));
}
1 change: 0 additions & 1 deletion crates/loans/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ impl oracle::Config for Test {
impl security::Config for Test {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
type MaxErrors = ConstU32<1>;
}

parameter_types! {
Expand Down
2 changes: 0 additions & 2 deletions crates/nomination/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use vault_registry::{
use crate::Pallet as Nomination;
use fee::Pallet as Fee;
use oracle::Pallet as Oracle;
use security::{Pallet as Security, StatusCode};
use vault_registry::Pallet as VaultRegistry;

fn deposit_tokens<T: crate::Config>(currency_id: CurrencyId, account_id: &T::AccountId, amount: BalanceOf<T>) {
Expand Down Expand Up @@ -74,7 +73,6 @@ pub mod benchmarks {

#[benchmark]
pub fn set_nomination_limit() {
Security::<T>::set_status(StatusCode::Running);
let vault_id = activate_lending_and_get_vault_id::<T>();
let amount = 100u32.into();
#[extrinsic_call]
Expand Down
9 changes: 0 additions & 9 deletions crates/nomination/src/ext.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
#[cfg(test)]
use mocktopus::macros::mockable;

#[cfg_attr(test, mockable)]
pub(crate) mod security {
use frame_support::dispatch::DispatchResult;

pub fn ensure_parachain_status_running<T: crate::Config>() -> DispatchResult {
<security::Pallet<T>>::ensure_parachain_status_running()
}
}

#[cfg_attr(test, mockable)]
pub(crate) mod vault_registry {
use currency::Amount;
Expand Down
6 changes: 0 additions & 6 deletions crates/nomination/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ pub mod pallet {
origin: OriginFor<T>,
currency_pair: DefaultVaultCurrencyPair<T>,
) -> DispatchResultWithPostInfo {
ext::security::ensure_parachain_status_running::<T>()?;
let account_id = ensure_signed(origin)?;
let vault_id = VaultId::new(account_id, currency_pair.collateral, currency_pair.wrapped);

Expand All @@ -173,8 +172,6 @@ pub mod pallet {
origin: OriginFor<T>,
currency_pair: DefaultVaultCurrencyPair<T>,
) -> DispatchResultWithPostInfo {
ext::security::ensure_parachain_status_running::<T>()?;

let account_id = ensure_signed(origin)?;
let vault_id = VaultId::new(account_id, currency_pair.collateral, currency_pair.wrapped);
Self::_opt_out_of_nomination(&vault_id)?;
Expand All @@ -190,7 +187,6 @@ pub mod pallet {
amount: BalanceOf<T>,
) -> DispatchResultWithPostInfo {
let nominator_id = ensure_signed(origin)?;
ext::security::ensure_parachain_status_running::<T>()?;
Self::_deposit_collateral(&vault_id, &nominator_id, amount)?;
Ok(().into())
}
Expand All @@ -205,7 +201,6 @@ pub mod pallet {
index: Option<T::Index>,
) -> DispatchResultWithPostInfo {
let nominator_id = ensure_signed(origin)?;
ext::security::ensure_parachain_status_running::<T>()?;
Self::_withdraw_collateral(&vault_id, &nominator_id, amount, index.unwrap_or_default())?;
Ok(().into())
}
Expand All @@ -218,7 +213,6 @@ pub mod pallet {
currency_pair: DefaultVaultCurrencyPair<T>,
limit: BalanceOf<T>,
) -> DispatchResultWithPostInfo {
ext::security::ensure_parachain_status_running::<T>()?;
let account_id = ensure_signed(origin)?;
let vault_id = VaultId::new(account_id, currency_pair.collateral, currency_pair.wrapped);

Expand Down
1 change: 0 additions & 1 deletion crates/nomination/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ impl currency::Config for Test {
impl security::Config for Test {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
type MaxErrors = ConstU32<1>;
}

parameter_types! {
Expand Down
1 change: 1 addition & 0 deletions crates/oracle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,4 @@ runtime-benchmarks = [
"pallet-timestamp/runtime-benchmarks",
]
try-runtime = [ "frame-support/try-runtime" ]
testing-utils = []
28 changes: 0 additions & 28 deletions crates/oracle/src/ext.rs

This file was deleted.

53 changes: 18 additions & 35 deletions crates/oracle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#![cfg_attr(test, feature(proc_macro_hygiene))]
#![cfg_attr(not(feature = "std"), no_std)]

mod ext;

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;

Expand Down Expand Up @@ -40,7 +38,6 @@ use frame_support::{
};
use frame_system::{ensure_root, ensure_signed};
use scale_info::TypeInfo;
use security::{ErrorCode, StatusCode};
use sp_runtime::traits::*;
use sp_std::{convert::TryInto, vec::Vec};
use traits::OracleApi;
Expand Down Expand Up @@ -278,20 +275,6 @@ impl<T: Config> Pallet<T> {
Self::deposit_event(Event::<T>::AggregateUpdated { values: updated_items });
}

let current_status_is_online = Self::is_oracle_online();
let new_status_is_online = raw_values_updated.len() > 0
&& raw_values_updated
.iter()
.all(|(key, _)| Aggregate::<T>::get(key).is_some());

if current_status_is_online != new_status_is_online {
if new_status_is_online {
Self::recover_from_oracle_offline();
} else {
Self::report_oracle_offline();
}
}

raw_values_updated.len().saturated_into()
}

Expand All @@ -316,8 +299,6 @@ impl<T: Config> Pallet<T> {

/// Get the exchange rate in planck per satoshi
pub fn get_price(key: OracleKey) -> Result<UnsignedFixedPoint<T>, DispatchError> {
ext::security::ensure_parachain_status_running::<T>()?;

Aggregate::<T>::get(key).ok_or(Error::<T>::MissingExchangeRate.into())
}

Expand All @@ -344,7 +325,7 @@ impl<T: Config> Pallet<T> {
let mut raw_values: Vec<_> = RawValues::<T>::iter_prefix(key).map(|(_, value)| value).collect();
let min_timestamp = Self::get_current_time().saturating_sub(Self::get_max_delay());
raw_values.retain(|value| value.timestamp >= min_timestamp);
if raw_values.len() == 0 {
let ret = if raw_values.len() == 0 {
Aggregate::<T>::remove(key);
ValidUntil::<T>::remove(key);
None
Expand All @@ -360,12 +341,15 @@ impl<T: Config> Pallet<T> {

Aggregate::<T>::insert(key, value);
ValidUntil::<T>::insert(key, valid_until);
if let OracleKey::ExchangeRate(currency_id) = key {
T::OnExchangeRateChange::on_exchange_rate_change(currency_id);
}

Some(value)
};

if let OracleKey::ExchangeRate(currency_id) = key {
T::OnExchangeRateChange::on_exchange_rate_change(currency_id);
}

ret
}

fn median(mut raw_values: Vec<UnsignedFixedPoint<T>>) -> Option<UnsignedFixedPoint<T>> {
Expand Down Expand Up @@ -406,23 +390,22 @@ impl<T: Config> Pallet<T> {
Aggregate::<T>::insert(&OracleKey::ExchangeRate(currency_id), exchange_rate);
T::OnExchangeRateChange::on_exchange_rate_change(&currency_id);

// this is useful for benchmark tests
Self::recover_from_oracle_offline();
Ok(())
}

fn is_oracle_online() -> bool {
!ext::security::get_errors::<T>().contains(&ErrorCode::OracleOffline)
}

fn report_oracle_offline() {
ext::security::set_status::<T>(StatusCode::Error);
// this is unlikely to fail since there is only one possible error
let _ = ext::security::insert_error::<T>(ErrorCode::OracleOffline);
#[cfg(feature = "testing-utils")]
pub fn expire_price(currency_id: CurrencyId) {
Aggregate::<T>::remove(&OracleKey::ExchangeRate(currency_id.clone()));
T::OnExchangeRateChange::on_exchange_rate_change(&currency_id);
}

fn recover_from_oracle_offline() {
ext::security::recover_from_oracle_offline::<T>()
#[cfg(feature = "testing-utils")]
pub fn expire_all() {
for (key, _old_rate) in Aggregate::<T>::drain() {
if let OracleKey::ExchangeRate(currency_id) = key {
T::OnExchangeRateChange::on_exchange_rate_change(&currency_id);
}
}
}

/// Returns the current timestamp
Expand Down
1 change: 0 additions & 1 deletion crates/oracle/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ impl pallet_timestamp::Config for Test {
impl security::Config for Test {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
type MaxErrors = ConstU32<1>;
}

impl staking::Config for Test {
Expand Down
Loading