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

Improve pallet-oracle #8

Merged
merged 1 commit into from
Aug 3, 2021
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
28 changes: 16 additions & 12 deletions pallets/oracle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,12 @@ pub mod pallet {
AlreadySet,
UnsetController,
ControllerUsed,
SignerUsed,
AvoidPanic,
ExceedMaxAnswers,
InvalidMinAnswers
SignerUsed,
AvoidPanic,
ExceedMaxAnswers,
InvalidMinAnswers,
MaxAnswersLessThanMinAnswers,
ExceedThreshold,
}

#[pallet::hooks]
Expand Down Expand Up @@ -283,16 +285,18 @@ pub mod pallet {
max_answers: u64
) -> DispatchResultWithPostInfo {
T::AddOracle::ensure_origin(origin)?;
ensure!(max_answers <= T::MaxAnswerBound::get(), Error::<T>::ExceedMaxAnswers);
ensure!(min_answers > 0, Error::<T>::InvalidMinAnswers);
let asset_info = AssetInfo {
threshold,
min_answers,
max_answers
};
ensure!(min_answers > 0, Error::<T>::InvalidMinAnswers);
ensure!(max_answers >= min_answers, Error::<T>::MaxAnswersLessThanMinAnswers);
ensure!(threshold < Percent::from_percent(100), Error::<T>::ExceedThreshold);
ensure!(max_answers <= T::MaxAnswerBound::get(), Error::<T>::ExceedMaxAnswers);
let asset_info = AssetInfo {
threshold,
min_answers,
max_answers
};
AssetsInfo::<T>::insert(asset_id, asset_info);
AssetsCount::<T>::mutate(|a| *a += 1);
Self::deposit_event(Event::AssetInfoChange(asset_id, threshold, min_answers, max_answers));
Self::deposit_event(Event::AssetInfoChange(asset_id, threshold, min_answers, max_answers));
Ok(().into())
}

Expand Down
70 changes: 47 additions & 23 deletions pallets/oracle/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,45 +21,69 @@ use sp_runtime::traits::{BadOrigin};
#[test]
fn add_asset_and_info() {
new_test_ext().execute_with(|| {
const ASSET_ID: u64 = 1;
const MIN_ANSWERS: u64 = 3;
const MAX_ANSWERS: u64 = 5;
const THRESHOLD: Percent = Percent::from_percent(80);

// passes
let account_2 = get_account_2();
assert_ok!(Oracle::add_asset_and_info(
Origin::signed(account_2),
1,
Percent::from_percent(80),
3,
5,
ASSET_ID,
THRESHOLD,
MIN_ANSWERS,
MAX_ANSWERS,
));

let asset_info = AssetInfo {
threshold: Percent::from_percent(80),
min_answers: 3,
max_answers: 5
};
let asset_info = AssetInfo {
threshold: THRESHOLD,
min_answers: MIN_ANSWERS,
max_answers: MAX_ANSWERS
};
// id now activated and count incremented
assert_eq!(Oracle::accuracy_threshold(1), asset_info);
assert_eq!(Oracle::assets_count(), 1);
// fails with non permission
let account_1: AccountId = Default::default();
assert_noop!(
Oracle::add_asset_and_info(Origin::signed(account_1), 1, Percent::from_percent(80), 3, 5),
BadOrigin
);
assert_noop!(Oracle::add_asset_and_info(
Origin::signed(account_1),
ASSET_ID,
THRESHOLD,
MAX_ANSWERS,
MAX_ANSWERS
), BadOrigin);

assert_noop!(Oracle::add_asset_and_info(
assert_noop!(Oracle::add_asset_and_info(
Origin::signed(account_2),
1,
Percent::from_percent(80),
3,
6,
ASSET_ID,
THRESHOLD,
MAX_ANSWERS,
MIN_ANSWERS,
), Error::<Test>::MaxAnswersLessThanMinAnswers);

assert_noop!(Oracle::add_asset_and_info(
Origin::signed(account_2),
ASSET_ID,
Percent::from_percent(100),
MIN_ANSWERS,
MAX_ANSWERS,
), Error::<Test>::ExceedThreshold);

assert_noop!(Oracle::add_asset_and_info(
Origin::signed(account_2),
ASSET_ID,
THRESHOLD,
MIN_ANSWERS,
MAX_ANSWERS + 1,
), Error::<Test>::ExceedMaxAnswers);

assert_noop!(Oracle::add_asset_and_info(
assert_noop!(Oracle::add_asset_and_info(
Origin::signed(account_2),
1,
Percent::from_percent(80),
0,
5,
ASSET_ID,
THRESHOLD,
0,
MAX_ANSWERS,
), Error::<Test>::InvalidMinAnswers);
});
}
Expand Down