Skip to content
Snippets Groups Projects
Unverified Commit 4c914772 authored by Xiliang Chen's avatar Xiliang Chen Committed by GitHub
Browse files

make default combine data configurable (#58)

parent 5221fdbb
No related branches found
No related tags found
No related merge requests found
use frame_support::{
parameter_types,
traits::{Get, Time},
};
use frame_support::traits::{Get, Time};
use orml_traits::CombineData;
use rstd::prelude::Vec;
use rstd::prelude::*;
use crate::{MomentOf, TimestampedValueOf, Trait};
/// Sort by value and returns median timestamped value.
/// Returns prev_value if not enough valid values.
pub struct DefaultCombineData<T: Trait>(rstd::marker::PhantomData<T>);
impl<T: Trait> CombineData<T::OracleKey, TimestampedValueOf<T>> for DefaultCombineData<T> {
pub struct DefaultCombineData<T, MinimumCount, ExpiresIn>(rstd::marker::PhantomData<(T, MinimumCount, ExpiresIn)>);
impl<T, MinimumCount, ExpiresIn> CombineData<T::OracleKey, TimestampedValueOf<T>>
for DefaultCombineData<T, MinimumCount, ExpiresIn>
where
T: Trait,
MinimumCount: Get<u32>,
ExpiresIn: Get<MomentOf<T>>,
{
fn combine_data(
_key: &T::OracleKey,
values: Vec<TimestampedValueOf<T>>,
prev_value: Option<TimestampedValueOf<T>>,
) -> Option<TimestampedValueOf<T>> {
let expires_in: MomentOf<T> = <Self as Parameters<MomentOf<T>>>::expires_in::get().into();
let expires_in = ExpiresIn::get();
let now = T::Time::now();
let mut valid_values = values
.into_iter()
......@@ -29,8 +32,8 @@ impl<T: Trait> CombineData<T::OracleKey, TimestampedValueOf<T>> for DefaultCombi
})
.collect::<Vec<TimestampedValueOf<T>>>();
let count = valid_values.len();
let minimum_count = <Self as Parameters<MomentOf<T>>>::minimum_count::get();
let count = valid_values.len() as u32;
let minimum_count = MinimumCount::get();
if count < minimum_count {
return prev_value;
}
......@@ -38,21 +41,6 @@ impl<T: Trait> CombineData<T::OracleKey, TimestampedValueOf<T>> for DefaultCombi
valid_values.sort_by(|a, b| a.value.cmp(&b.value));
let median_index = count / 2;
return Some(valid_values[median_index].clone());
return Some(valid_values[median_index as usize].clone());
}
}
parameter_types! {
pub const MinimumCount: usize = 3;
pub const ExpiresIn: u32 = 600;
}
trait Parameters<Moment> {
type minimum_count: Get<usize>;
type expires_in: Get<Moment>;
}
impl<T: Trait> Parameters<MomentOf<T>> for DefaultCombineData<T> {
type minimum_count = MinimumCount;
type expires_in = ExpiresIn;
}
......@@ -71,11 +71,16 @@ impl OperatorProvider<AccountId> for MockOperatorProvider {
}
}
parameter_types! {
pub const MinimumCount: u32 = 3;
pub const ExpiresIn: u32 = 600;
}
impl Trait for Test {
type Event = ();
type OnNewData = ();
type OperatorProvider = MockOperatorProvider;
type CombineData = DefaultCombineData<Self>;
type CombineData = DefaultCombineData<Self, MinimumCount, ExpiresIn>;
type Time = pallet_timestamp::Module<Self>;
type OracleKey = Key;
type OracleValue = Value;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment