From 6571245a835292068207c81d4b8a3c6d28234dda Mon Sep 17 00:00:00 2001
From: Xiliang Chen <xlchen1291@gmail.com>
Date: Thu, 21 Nov 2019 21:57:54 +1300
Subject: [PATCH] add feed_values (#36)

* add feed_values

* fix tests

* workaround check wasm issue

* workaround build issue
---
 Makefile                           |  2 +-
 auction/Cargo.toml                 |  3 +-
 currencies/Cargo.toml              |  3 +-
 oracle/Cargo.toml                  |  1 +
 oracle/src/default_combine_data.rs |  2 +-
 oracle/src/lib.rs                  | 44 +++++++++++++---------
 oracle/src/tests.rs                | 60 ++++++++++++++++++++++++------
 prices/Cargo.toml                  |  3 +-
 tokens/Cargo.toml                  |  3 +-
 traits/Cargo.toml                  |  5 ++-
 utilities/Cargo.toml               |  1 +
 11 files changed, 91 insertions(+), 36 deletions(-)

diff --git a/Makefile b/Makefile
index 53578b4..0165665 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
 check: githooks
-	./scripts/run.sh check --no-default-features --target=wasm32-unknown-unknown
+	./scripts/run.sh check --no-default-features
 
 check-tests: githooks
 	./scripts/run.sh check --tests
diff --git a/auction/Cargo.toml b/auction/Cargo.toml
index 08ea8f1..54291ec 100644
--- a/auction/Cargo.toml
+++ b/auction/Cargo.toml
@@ -18,7 +18,8 @@ orml-traits = { path = "../traits", default-features = false }
 orml-utilities = { path = "../utilities", default-features = false }
 
 [dev-dependencies]
-primitives = { package = "substrate-primitives",  git = "https://github.com/paritytech/substrate.git", default-features = false }
+primitives = { package = "substrate-primitives",  git = "https://github.com/paritytech/substrate.git" }
+substrate-runtime-interface = { git = "https://github.com/paritytech/substrate.git", features = ['disable_target_static_assertions'] }
 
 [features]
 default = ["std"]
diff --git a/currencies/Cargo.toml b/currencies/Cargo.toml
index 6c954b3..9cdd561 100644
--- a/currencies/Cargo.toml
+++ b/currencies/Cargo.toml
@@ -17,9 +17,10 @@ palette-system = { git = "https://github.com/paritytech/substrate.git", default-
 orml-traits = { path = "../traits", default-features = false }
 
 [dev-dependencies]
-primitives = { package = "substrate-primitives",  git = "https://github.com/paritytech/substrate.git", default-features = false }
+primitives = { package = "substrate-primitives",  git = "https://github.com/paritytech/substrate.git" }
 pallet-balances = { package = "pallet-balances", git = "https://github.com/paritytech/substrate.git" }
 tokens = { package = "orml-tokens", path = "../tokens" }
+substrate-runtime-interface = { git = "https://github.com/paritytech/substrate.git", features = ['disable_target_static_assertions'] }
 
 [features]
 default = ["std"]
diff --git a/oracle/Cargo.toml b/oracle/Cargo.toml
index 7300bcb..2b8b9ca 100644
--- a/oracle/Cargo.toml
+++ b/oracle/Cargo.toml
@@ -20,6 +20,7 @@ orml-utilities = { path = "../utilities", default-features = false }
 
 [dev-dependencies]
 pallet-timestamp = { git = "https://github.com/paritytech/substrate.git" }
+substrate-runtime-interface = { git = "https://github.com/paritytech/substrate.git", features = ['disable_target_static_assertions'] }
 
 [features]
 default = ["std"]
diff --git a/oracle/src/default_combine_data.rs b/oracle/src/default_combine_data.rs
index 4e3b1e7..378124a 100644
--- a/oracle/src/default_combine_data.rs
+++ b/oracle/src/default_combine_data.rs
@@ -38,7 +38,7 @@ impl<T: Trait> CombineData<T::Key, TimestampedValueOf<T>> for DefaultCombineData
 		valid_values.sort_by(|a, b| a.value.cmp(&b.value));
 
 		let median_index = count / 2;
-		return Some(valid_values[median_index]);
+		return Some(valid_values[median_index].clone());
 	}
 }
 
diff --git a/oracle/src/lib.rs b/oracle/src/lib.rs
index 3374226..49d2ba8 100644
--- a/oracle/src/lib.rs
+++ b/oracle/src/lib.rs
@@ -11,7 +11,7 @@ pub use operator_provider::OperatorProvider;
 use palette_support::{
 	decl_error, decl_event, decl_module, decl_storage, dispatch::Result, ensure, traits::Time, Parameter,
 };
-use rstd::{prelude::Vec, result};
+use rstd::{prelude::*, result, vec};
 use sr_primitives::traits::Member;
 // FIXME: `pallet/palette-` prefix should be used for all pallet modules, but currently `palette_system`
 // would cause compiling error in `decl_module!` and `construct_runtime!`
@@ -29,8 +29,8 @@ pub trait Trait: palette_system::Trait {
 	type OperatorProvider: OperatorProvider<Self::AccountId>;
 	type CombineData: CombineData<Self::Key, TimestampedValueOf<Self>>;
 	type Time: Time;
-	type Key: Parameter + Member + Copy + Ord;
-	type Value: Parameter + Member + Copy + Ord;
+	type Key: Parameter + Member;
+	type Value: Parameter + Member + Ord;
 }
 
 decl_storage! {
@@ -52,9 +52,14 @@ decl_module! {
 	pub struct Module<T: Trait> for enum Call where origin: T::Origin {
 		fn deposit_event() = default;
 
-		pub fn feed_data(origin, key: T::Key, value: T::Value) -> Result {
+		pub fn feed_value(origin, key: T::Key, value: T::Value) -> Result {
 			let who = ensure_signed(origin)?;
-			Self::_feed_data(who, key, value).map_err(|e| e.into())
+			Self::_feed_values(who, vec![(key, value)]).map_err(|e| e.into())
+		}
+
+		pub fn feed_values(origin, values: Vec<(T::Key, T::Value)>) -> Result {
+			let who = ensure_signed(origin)?;
+			Self::_feed_values(who, values).map_err(|e| e.into())
 		}
 	}
 }
@@ -65,8 +70,8 @@ decl_event!(
 		<T as Trait>::Key,
 		<T as Trait>::Value,
 	{
-		/// New feed data is submitted (sender, key, value)
-		NewFeedData(AccountId, Key, Value),
+		/// New feed data is submitted (sender, values)
+		NewFeedData(AccountId, Vec<(Key, Value)>),
 	}
 );
 
@@ -82,7 +87,7 @@ impl<T: Trait> Module<T> {
 		if <HasUpdate<T>>::take(key) {
 			let values = Self::read_raw_values(key);
 			let timestamped = T::CombineData::combine_data(key, values, <Values<T>>::get(key))?;
-			<Values<T>>::insert(key, timestamped);
+			<Values<T>>::insert(key, timestamped.clone());
 			return Some(timestamped);
 		}
 		<Values<T>>::get(key)
@@ -90,19 +95,24 @@ impl<T: Trait> Module<T> {
 }
 
 impl<T: Trait> Module<T> {
-	fn _feed_data(who: T::AccountId, key: T::Key, value: T::Value) -> result::Result<(), Error> {
+	fn _feed_values(who: T::AccountId, values: Vec<(T::Key, T::Value)>) -> result::Result<(), Error> {
 		ensure!(T::OperatorProvider::can_feed_data(&who), Error::NoPermission);
 
-		let timestamp = TimestampedValue {
-			value,
-			timestamp: T::Time::now(),
-		};
-		<RawValues<T>>::insert(&key, &who, timestamp);
-		<HasUpdate<T>>::insert(&key, true);
+		let now = T::Time::now();
+
+		for (key, value) in &values {
+			let timestamped = TimestampedValue {
+				value: value.clone(),
+				timestamp: now,
+			};
+			<RawValues<T>>::insert(&key, &who, timestamped);
+			<HasUpdate<T>>::insert(&key, true);
+
+			T::OnNewData::on_new_data(&key, &value);
+		}
 
-		T::OnNewData::on_new_data(&key, &value);
+		Self::deposit_event(RawEvent::NewFeedData(who, values));
 
-		Self::deposit_event(RawEvent::NewFeedData(who, key, value));
 		Ok(())
 	}
 }
diff --git a/oracle/src/tests.rs b/oracle/src/tests.rs
index d45e31d..143167d 100644
--- a/oracle/src/tests.rs
+++ b/oracle/src/tests.rs
@@ -6,7 +6,7 @@ use crate::TimestampedValue;
 use palette_support::assert_ok;
 
 #[test]
-fn should_feed_data() {
+fn should_feed_value() {
 	new_test_ext().execute_with(|| {
 		let key: u32 = 1;
 		let account_id: u64 = 1;
@@ -18,19 +18,57 @@ fn should_feed_data() {
 			timestamp: 12345,
 		};
 
-		assert_ok!(ModuleOracle::feed_data(Origin::signed(account_id), key, 1000));
+		assert_ok!(ModuleOracle::feed_value(Origin::signed(account_id), key, 1000));
 
 		let feed_data = ModuleOracle::raw_values(&key, &account_id).unwrap();
 		assert_eq!(feed_data, expected);
 	});
 }
 
+#[test]
+fn should_feed_values() {
+	new_test_ext().execute_with(|| {
+		let account_id: u64 = 1;
+
+		Timestamp::set_timestamp(12345);
+
+		assert_ok!(ModuleOracle::feed_values(
+			Origin::signed(account_id),
+			vec![(1, 1000), (2, 900), (3, 800)]
+		));
+
+		assert_eq!(
+			ModuleOracle::raw_values(&1, &account_id),
+			Some(TimestampedValue {
+				value: 1000,
+				timestamp: 12345,
+			})
+		);
+
+		assert_eq!(
+			ModuleOracle::raw_values(&2, &account_id),
+			Some(TimestampedValue {
+				value: 900,
+				timestamp: 12345,
+			})
+		);
+
+		assert_eq!(
+			ModuleOracle::raw_values(&3, &account_id),
+			Some(TimestampedValue {
+				value: 800,
+				timestamp: 12345,
+			})
+		);
+	});
+}
+
 #[test]
 fn should_change_status_when_feeding() {
 	new_test_ext().execute_with(|| {
 		let key: u32 = 1;
 		assert_eq!(ModuleOracle::has_update(key), false);
-		assert_ok!(ModuleOracle::feed_data(Origin::signed(1), key, 1000));
+		assert_ok!(ModuleOracle::feed_value(Origin::signed(1), key, 1000));
 		assert_eq!(ModuleOracle::has_update(key), true);
 	});
 }
@@ -56,8 +94,8 @@ fn should_read_raw_values() {
 			},
 		];
 
-		assert_ok!(ModuleOracle::feed_data(Origin::signed(1), key, 1000));
-		assert_ok!(ModuleOracle::feed_data(Origin::signed(2), key, 1200));
+		assert_ok!(ModuleOracle::feed_value(Origin::signed(1), key, 1000));
+		assert_ok!(ModuleOracle::feed_value(Origin::signed(2), key, 1200));
 
 		let raw_values = ModuleOracle::read_raw_values(&key);
 		assert_eq!(raw_values, expected);
@@ -76,9 +114,9 @@ fn should_combined_data() {
 
 		let key: u32 = 1;
 
-		assert_ok!(ModuleOracle::feed_data(Origin::signed(1), key, 1300));
-		assert_ok!(ModuleOracle::feed_data(Origin::signed(2), key, 1000));
-		assert_ok!(ModuleOracle::feed_data(Origin::signed(3), key, 1200));
+		assert_ok!(ModuleOracle::feed_value(Origin::signed(1), key, 1300));
+		assert_ok!(ModuleOracle::feed_value(Origin::signed(2), key, 1000));
+		assert_ok!(ModuleOracle::feed_value(Origin::signed(3), key, 1200));
 		assert_eq!(ModuleOracle::get(&key), expected);
 	});
 }
@@ -95,9 +133,9 @@ fn should_return_prev_value() {
 
 		let key: u32 = 1;
 
-		assert_ok!(ModuleOracle::feed_data(Origin::signed(1), key, 1300));
-		assert_ok!(ModuleOracle::feed_data(Origin::signed(2), key, 1000));
-		assert_ok!(ModuleOracle::feed_data(Origin::signed(3), key, 1200));
+		assert_ok!(ModuleOracle::feed_value(Origin::signed(1), key, 1300));
+		assert_ok!(ModuleOracle::feed_value(Origin::signed(2), key, 1000));
+		assert_ok!(ModuleOracle::feed_value(Origin::signed(3), key, 1200));
 		assert_eq!(ModuleOracle::get(&key), expected);
 
 		Timestamp::set_timestamp(23456);
diff --git a/prices/Cargo.toml b/prices/Cargo.toml
index 3c6c0b0..078befd 100644
--- a/prices/Cargo.toml
+++ b/prices/Cargo.toml
@@ -17,7 +17,8 @@ orml-traits = { path = "../traits", default-features = false }
 orml-utilities = { path = "../utilities", default-features = false }
 
 [dev-dependencies]
-primitives = { package = "substrate-primitives",  git = "https://github.com/paritytech/substrate.git", default-features = false }
+primitives = { package = "substrate-primitives",  git = "https://github.com/paritytech/substrate.git" }
+substrate-runtime-interface = { git = "https://github.com/paritytech/substrate.git", features = ['disable_target_static_assertions'] }
 
 [features]
 default = ["std"]
diff --git a/tokens/Cargo.toml b/tokens/Cargo.toml
index 157cf55..f9e6260 100644
--- a/tokens/Cargo.toml
+++ b/tokens/Cargo.toml
@@ -18,7 +18,8 @@ orml-traits = { path = "../traits", default-features = false }
 orml-utilities = { path = "../utilities", default-features = false }
 
 [dev-dependencies]
-primitives = { package = "substrate-primitives",  git = "https://github.com/paritytech/substrate.git", default-features = false }
+primitives = { package = "substrate-primitives",  git = "https://github.com/paritytech/substrate.git" }
+substrate-runtime-interface = { git = "https://github.com/paritytech/substrate.git", features = ['disable_target_static_assertions'] }
 
 [features]
 default = ["std"]
diff --git a/traits/Cargo.toml b/traits/Cargo.toml
index 6df7a4b..46e46c4 100644
--- a/traits/Cargo.toml
+++ b/traits/Cargo.toml
@@ -13,8 +13,9 @@ rstd = { package = "sr-std", git = "https://github.com/paritytech/substrate.git"
 num-traits = { version = "0.2.8", default-features = false }
 impl-trait-for-tuples = "0.1.3"
 
-palette_support = { package = "palette-support", git = "https://github.com/paritytech/substrate.git", default-features = false }
-palette_system = { package = "palette-system", git = "https://github.com/paritytech/substrate.git", default-features = false }
+palette_support = { package = "palette-support", git = "https://github.com/paritytech/substrate.git" }
+palette_system = { package = "palette-system", git = "https://github.com/paritytech/substrate.git" }
+substrate-runtime-interface = { git = "https://github.com/paritytech/substrate.git", features = ['disable_target_static_assertions'] }
 
 [features]
 default = ["std"]
diff --git a/utilities/Cargo.toml b/utilities/Cargo.toml
index d7fb935..fbaaf1c 100644
--- a/utilities/Cargo.toml
+++ b/utilities/Cargo.toml
@@ -16,6 +16,7 @@ palette-support = { git = "https://github.com/paritytech/substrate.git", default
 
 [dev-dependencies]
 primitives = { package = "substrate-primitives",  git = "https://github.com/paritytech/substrate.git", default-features = false }
+substrate-runtime-interface = { git = "https://github.com/paritytech/substrate.git", features = ['disable_target_static_assertions'], default-features = false }
 
 [features]
 default = ["std"]
-- 
GitLab