From e4c01c1658344403bc968ac2fccb6335c685f884 Mon Sep 17 00:00:00 2001 From: Guanqun Lu Date: Thu, 13 Sep 2018 23:48:55 +0800 Subject: [PATCH 01/21] sr-support: add SystemOrigin(Signed(who)) --- srml/support/src/dispatch.rs | 95 ++++++++++++++++++++++++++++++------ srml/treasury/src/lib.rs | 7 +-- 2 files changed, 83 insertions(+), 19 deletions(-) diff --git a/srml/support/src/dispatch.rs b/srml/support/src/dispatch.rs index 47ba7785cda48..f052dbce0c890 100644 --- a/srml/support/src/dispatch.rs +++ b/srml/support/src/dispatch.rs @@ -92,6 +92,7 @@ macro_rules! decl_module { ); }; + // fn $fn_name(origin, $param_name: $param) (@normalize $(#[$attr:meta])* pub struct $mod_type:ident<$trait_instance:ident: $trait_name:ident> @@ -105,7 +106,43 @@ macro_rules! decl_module { $(#[$attr])* pub struct $mod_type<$trait_instance: $trait_name> for enum $call_type where origin: $origin_type where system = $system - [ $($t)* $(#[doc = $doc_attr])* fn $fn_name(origin $( , $param_name : $param )* ) -> $result; ] + [ $($t)* $(#[doc = $doc_attr])* fn $fn_name((origin origin origin) $( , $param_name : $param )* ) -> $result; ] + $($rest)* + ); + }; + // fn $fn_name(SystemOrigin(Signed(who)), $param_name: $param) + (@normalize + $(#[$attr:meta])* + pub struct $mod_type:ident<$trait_instance:ident: $trait_name:ident> + for enum $call_type:ident where origin: $origin_type:ty where system = $system:ident + [ $($t:tt)* ] + $(#[doc = $doc_attr:tt])* + fn $fn_name:ident(SystemOrigin(Signed($who:ident)) $(, $param_name:ident : $param:ty)* ) -> $result:ty ; + $($rest:tt)* + ) => { + decl_module!(@normalize + $(#[$attr])* + pub struct $mod_type<$trait_instance: $trait_name> + for enum $call_type where origin: $origin_type where system = $system + [ $($t)* $(#[doc = $doc_attr])* fn $fn_name((system signed $who) $( , $param_name : $param )* ) -> $result; ] + $($rest)* + ); + }; + // fn $fn_name(CouncilOrigin(Members(n)), $param_name: $param) + (@normalize + $(#[$attr:meta])* + pub struct $mod_type:ident<$trait_instance:ident: $trait_name:ident> + for enum $call_type:ident where origin: $origin_type:ty where system = $system:ident + [ $($t:tt)* ] + $(#[doc = $doc_attr:tt])* + fn $fn_name:ident(CouncilOrigin(Members($n:ident)) $(, $param_name:ident : $param:ty)* ) -> $result:ty ; + $($rest:tt)* + ) => { + decl_module!(@normalize + $(#[$attr])* + pub struct $mod_type<$trait_instance: $trait_name> + for enum $call_type where origin: $origin_type where system = $system + [ $($t)* $(#[doc = $doc_attr])* fn $fn_name((council members $n) $( , $param_name : $param )* ) -> $result; ] $($rest)* ); }; @@ -122,7 +159,7 @@ macro_rules! decl_module { $(#[$attr])* pub struct $mod_type<$trait_instance: $trait_name> for enum $call_type where origin: $origin_type where system = $system - [ $($t)* $(#[doc = $doc_attr])* fn $fn_name(root $( , $param_name : $param )* ) -> $result; ] + [ $($t)* $(#[doc = $doc_attr])* fn $fn_name((root root root) $( , $param_name : $param )* ) -> $result; ] $($rest)* ); }; @@ -142,13 +179,13 @@ macro_rules! decl_module { }; (@call - origin + (origin origin origin) $mod_type:ident $trait_instance:ident $fn_name:ident $origin:ident $system:ident [ $( $param_name:ident),* ] ) => { <$mod_type<$trait_instance>>::$fn_name( $origin $(, $param_name )* ) }; (@call - root + (root root root) $mod_type:ident $trait_instance:ident $fn_name:ident $origin:ident $system:ident [ $( $param_name:ident),* ] ) => { { @@ -156,6 +193,15 @@ macro_rules! decl_module { <$mod_type<$trait_instance>>::$fn_name( $( $param_name ),* ) } }; + (@call + (system signed $who:ident) + $mod_type:ident $trait_instance:ident $fn_name:ident $origin:ident $system:ident [ $( $param_name:ident),* ] + ) => { + { + let $who = $system::ensure_signed($origin)?; + <$mod_type<$trait_instance>>::$fn_name( $who $(, $param_name )* ) + } + }; (@imp $(#[$attr:meta])* @@ -163,7 +209,7 @@ macro_rules! decl_module { for enum $call_type:ident where origin: $origin_type:ty where system = $system:ident { $( $(#[doc = $doc_attr:tt])* - fn $fn_name:ident($from:ident $( , $param_name:ident : $param:ty)*) -> $result:ty; + fn $fn_name:ident(($top_origin:ident $sub_origin:ident $origin_param:ident) $( , $param_name:ident : $param:ty)*) -> $result:ty; )*} ) => { // Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted. @@ -288,7 +334,7 @@ macro_rules! decl_module { match self { $( $call_type::$fn_name( $( $param_name ),* ) => { - decl_module!(@call $from $mod_type $trait_instance $fn_name _origin $system [ $( $param_name ),* ]) + decl_module!(@call ($top_origin $sub_origin $origin_param) $mod_type $trait_instance $fn_name _origin $system [ $( $param_name ),* ]) }, )* _ => { panic!("__PhantomItem should never be used.") }, @@ -306,10 +352,10 @@ macro_rules! decl_module { d.dispatch(origin) } } - __dispatch_impl_json_metadata! { - $mod_type $trait_instance $trait_name $call_type $origin_type - {$( $(#[doc = $doc_attr])* fn $fn_name($from $(, $param_name : $param )*) -> $result; )*} - } +// __dispatch_impl_json_metadata! { +// $mod_type $trait_instance $trait_name $call_type $origin_type +// {$( $(#[doc = $doc_attr])* fn $fn_name($from $(, $param_name : $param )*) -> $result; )*} +// } } } @@ -631,14 +677,20 @@ mod tests { pub trait Trait { type Origin; + type AccountId; } pub mod system { + use super::result; use super::Result; pub fn ensure_root(_: R) -> Result { Ok(()) } + + pub fn ensure_signed(_: O) -> result::Result { + Err("unreachable") + } } decl_module! { @@ -649,6 +701,12 @@ mod tests { fn aux_2(origin, data: i32, data2: String) -> Result; fn aux_3() -> Result; fn aux_4(data: i32) -> Result; + fn aux_5(SystemOrigin(Signed(who))) -> Result; + fn aux_6(SystemOrigin(Signed(who)), data: i32) -> Result; +// fn aux_7(CouncilOrigin(Members(n))) -> Result; +// fn aux_8(CouncilOrigin(Members(n)), data: i32) -> Result; +// fn aux_9(CouncilOrigin(Members(n)) if n > 2) -> Result; +// fn aux_10(CouncilOrigin(Members(n)) if n > 2, data: i32) -> Result; } } @@ -700,19 +758,28 @@ mod tests { fn aux_4(_: i32) -> Result { unreachable!() } + + fn aux_5(_: &T::AccountId) -> Result { + unreachable!() + } + + fn aux_6(_: &T::AccountId, _: i32) -> Result { + unreachable!() + } } struct TraitImpl {} impl Trait for TraitImpl { type Origin = u32; + type AccountId = u64; } #[test] fn module_json_metadata() { - let metadata = Module::::json_metadata(); - assert_eq!(EXPECTED_METADATA, metadata); - let _: serde::de::IgnoredAny = - serde_json::from_str(metadata).expect("Is valid json syntax"); +// let metadata = Module::::json_metadata(); +// assert_eq!(EXPECTED_METADATA, metadata); +// let _: serde::de::IgnoredAny = +// serde_json::from_str(metadata).expect("Is valid json syntax"); } } diff --git a/srml/treasury/src/lib.rs b/srml/treasury/src/lib.rs index 6026917a26eb4..bfbfd51ca804f 100644 --- a/srml/treasury/src/lib.rs +++ b/srml/treasury/src/lib.rs @@ -46,7 +46,6 @@ use runtime_support::{StorageValue, StorageMap}; use runtime_support::dispatch::Result; use runtime_primitives::{Permill, traits::{OnFinalise, Zero, EnsureOrigin}}; use balances::OnDilution; -use system::ensure_signed; /// Our module's configuration trait. All our types and consts go in here. If the /// module is dependent on specific other modules, then their configuration traits @@ -74,7 +73,7 @@ decl_module! { // Put forward a suggestion for spending. A deposit proportional to the value // is reserved and slashed if the proposal is rejected. It is returned once the // proposal is awarded. - fn propose_spend(origin, value: T::Balance, beneficiary: T::AccountId) -> Result; + fn propose_spend(SystemOrigin(Signed(proposer)), value: T::Balance, beneficiary: T::AccountId) -> Result; // Set the balance of funds available to spend. fn set_pot(new_pot: T::Balance) -> Result; @@ -158,9 +157,7 @@ impl Module { // Implement Calls and add public immutables and private mutables. - fn propose_spend(origin: T::Origin, value: T::Balance, beneficiary: T::AccountId) -> Result { - let proposer = ensure_signed(origin)?; - + fn propose_spend(proposer: T::AccountId, value: T::Balance, beneficiary: T::AccountId) -> Result { let bond = Self::calculate_bond(value); >::reserve(&proposer, bond) .map_err(|_| "Proposer's balance too low")?; From 5bdd1ae45a6f1d566715c030e49045b18cf52f46 Mon Sep 17 00:00:00 2001 From: Guanqun Lu Date: Thu, 13 Sep 2018 23:55:06 +0800 Subject: [PATCH 02/21] sr-support: add back json generation --- srml/support/src/dispatch.rs | 45 ++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/srml/support/src/dispatch.rs b/srml/support/src/dispatch.rs index f052dbce0c890..28644e533e942 100644 --- a/srml/support/src/dispatch.rs +++ b/srml/support/src/dispatch.rs @@ -352,10 +352,10 @@ macro_rules! decl_module { d.dispatch(origin) } } -// __dispatch_impl_json_metadata! { -// $mod_type $trait_instance $trait_name $call_type $origin_type -// {$( $(#[doc = $doc_attr])* fn $fn_name($from $(, $param_name : $param )*) -> $result; )*} -// } + __dispatch_impl_json_metadata! { + $mod_type $trait_instance $trait_name $call_type $origin_type + {$( $(#[doc = $doc_attr])* fn $fn_name(($top_origin $sub_origin $origin_param) $(, $param_name : $param )*) -> $result; )*} + } } } @@ -529,7 +529,7 @@ macro_rules! __call_to_json { $call_type:ident $origin_type:ty {$( $(#[doc = $doc_attr:tt])* - fn $fn_name:ident($from:ident + fn $fn_name:ident(($top_origin:ident $sub_origin:ident $origin_param:ident) $( , $param_name:ident : $param:ty )* @@ -540,7 +540,7 @@ macro_rules! __call_to_json { r#"{ "name": ""#, stringify!($call_type), r#"", "functions": {"#, __functions_to_json!(""; 0; $origin_type; $( - fn $fn_name($from $(, $param_name: $param )* ) -> $result; + fn $fn_name(($top_origin $sub_origin $origin_param) $(, $param_name: $param )* ) -> $result; __function_doc_to_json!(""; $($doc_attr)*); )*), " } }" ) @@ -556,7 +556,8 @@ macro_rules! __functions_to_json { $prefix_str:tt; $fn_id:expr; $origin_type:ty; - fn $fn_name:ident(root + fn $fn_name:ident( + (root root root) $( , $param_name:ident : $param:ty )* @@ -574,12 +575,13 @@ macro_rules! __functions_to_json { ), __functions_to_json!(","; $fn_id + 1; $origin_type; $($rest)*) ) }; - // NON ROOT + // ORIGIN ( $prefix_str:tt; $fn_id:expr; $origin_type:ty; - fn $fn_name:ident(origin + fn $fn_name:ident( + (origin origin origin) $( , $param_name:ident : $param:ty )* @@ -598,6 +600,31 @@ macro_rules! __functions_to_json { ), __functions_to_json!(","; $fn_id + 1; $origin_type; $($rest)*) ) }; + // system signed who + ( + $prefix_str:tt; + $fn_id:expr; + $origin_type:ty; + fn $fn_name:ident( + (system signed $who:ident) + $( + , $param_name:ident : $param:ty + )* + ) -> $result:ty; + $fn_doc:expr; + $($rest:tt)* + ) => { + concat!($prefix_str, " ", + __function_to_json!( + fn $fn_name( + $who: $origin_type // TODO: fix this. + $( , $param_name : $param )* + ) -> $result; + $fn_doc; + $fn_id; + ), __functions_to_json!(","; $fn_id + 1; $origin_type; $($rest)*) + ) + }; // BASE CASE ( $prefix_str:tt; From 32dd71e50dab38d14d019a363f07850a0bdcb5ae Mon Sep 17 00:00:00 2001 From: Guanqun Lu Date: Fri, 14 Sep 2018 00:02:29 +0800 Subject: [PATCH 03/21] sr-support: fix the unit test --- srml/support/src/dispatch.rs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/srml/support/src/dispatch.rs b/srml/support/src/dispatch.rs index 28644e533e942..adc79e1e3e5a0 100644 --- a/srml/support/src/dispatch.rs +++ b/srml/support/src/dispatch.rs @@ -617,7 +617,7 @@ macro_rules! __functions_to_json { concat!($prefix_str, " ", __function_to_json!( fn $fn_name( - $who: $origin_type // TODO: fix this. + $who: T::AccountId // TODO: fix this. $( , $param_name : $param )* ) -> $result; $fn_doc; @@ -760,7 +760,17 @@ mod tests { r#""0 + 1 + 1 + 1 + 1": { "name": "aux_4", "params": [ "#, r#"{ "name": "data", "type": "i32" }"#, + r#" ], "description": [ ] }, "#, + + r#""0 + 1 + 1 + 1 + 1 + 1": { "name": "aux_5", "params": [ "#, + r#"{ "name": "who", "type": "T::AccountId" }"#, + r#" ], "description": [ ] }, "#, + + r#""0 + 1 + 1 + 1 + 1 + 1 + 1": { "name": "aux_6", "params": [ "#, + r#"{ "name": "who", "type": "T::AccountId" }, "#, + r#"{ "name": "data", "type": "i32" }"#, r#" ], "description": [ ] }"#, + r#" } }"#, r#" }"#, ); @@ -804,9 +814,9 @@ mod tests { #[test] fn module_json_metadata() { -// let metadata = Module::::json_metadata(); -// assert_eq!(EXPECTED_METADATA, metadata); -// let _: serde::de::IgnoredAny = -// serde_json::from_str(metadata).expect("Is valid json syntax"); + let metadata = Module::::json_metadata(); + assert_eq!(EXPECTED_METADATA, metadata); + let _: serde::de::IgnoredAny = + serde_json::from_str(metadata).expect("Is valid json syntax"); } } From 59ac5aa71c25d61ade0c56200936d93a86fcf8eb Mon Sep 17 00:00:00 2001 From: Guanqun Lu Date: Fri, 14 Sep 2018 00:08:46 +0800 Subject: [PATCH 04/21] sr-support: pass the trait instance parameter down --- srml/support/src/dispatch.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/srml/support/src/dispatch.rs b/srml/support/src/dispatch.rs index adc79e1e3e5a0..09c27306aedc2 100644 --- a/srml/support/src/dispatch.rs +++ b/srml/support/src/dispatch.rs @@ -515,7 +515,7 @@ macro_rules! __dispatch_impl_json_metadata { impl<$trait_instance: $trait_name> $mod_type<$trait_instance> { pub fn json_metadata() -> &'static str { concat!(r#"{ "name": ""#, stringify!($mod_type), r#"", "call": "#, - __call_to_json!($($rest)*), " }") + __call_to_json!($trait_instance $($rest)*), " }") } } } @@ -526,6 +526,7 @@ macro_rules! __dispatch_impl_json_metadata { #[doc(hidden)] macro_rules! __call_to_json { ( + $trait_instance:ident $call_type:ident $origin_type:ty {$( $(#[doc = $doc_attr:tt])* @@ -539,7 +540,7 @@ macro_rules! __call_to_json { concat!( r#"{ "name": ""#, stringify!($call_type), r#"", "functions": {"#, - __functions_to_json!(""; 0; $origin_type; $( + __functions_to_json!(""; 0; $origin_type; $trait_instance; $( fn $fn_name(($top_origin $sub_origin $origin_param) $(, $param_name: $param )* ) -> $result; __function_doc_to_json!(""; $($doc_attr)*); )*), " } }" @@ -556,6 +557,7 @@ macro_rules! __functions_to_json { $prefix_str:tt; $fn_id:expr; $origin_type:ty; + $trait_instance:ident; fn $fn_name:ident( (root root root) $( @@ -572,7 +574,7 @@ macro_rules! __functions_to_json { ) -> $result; $fn_doc; $fn_id; - ), __functions_to_json!(","; $fn_id + 1; $origin_type; $($rest)*) + ), __functions_to_json!(","; $fn_id + 1; $origin_type; $trait_instance; $($rest)*) ) }; // ORIGIN @@ -580,6 +582,7 @@ macro_rules! __functions_to_json { $prefix_str:tt; $fn_id:expr; $origin_type:ty; + $trait_instance:ident; fn $fn_name:ident( (origin origin origin) $( @@ -597,7 +600,7 @@ macro_rules! __functions_to_json { ) -> $result; $fn_doc; $fn_id; - ), __functions_to_json!(","; $fn_id + 1; $origin_type; $($rest)*) + ), __functions_to_json!(","; $fn_id + 1; $origin_type; $trait_instance; $($rest)*) ) }; // system signed who @@ -605,6 +608,7 @@ macro_rules! __functions_to_json { $prefix_str:tt; $fn_id:expr; $origin_type:ty; + $trait_instance:ident; fn $fn_name:ident( (system signed $who:ident) $( @@ -617,12 +621,12 @@ macro_rules! __functions_to_json { concat!($prefix_str, " ", __function_to_json!( fn $fn_name( - $who: T::AccountId // TODO: fix this. + $who: $trait_instance::AccountId $( , $param_name : $param )* ) -> $result; $fn_doc; $fn_id; - ), __functions_to_json!(","; $fn_id + 1; $origin_type; $($rest)*) + ), __functions_to_json!(","; $fn_id + 1; $origin_type; $trait_instance; $($rest)*) ) }; // BASE CASE From 9972a5db562b28ab692461b47d092b7979539aab Mon Sep 17 00:00:00 2001 From: Guanqun Lu Date: Fri, 14 Sep 2018 00:18:37 +0800 Subject: [PATCH 05/21] sr-support: revert back the changes in runtime for now --- srml/treasury/src/lib.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/srml/treasury/src/lib.rs b/srml/treasury/src/lib.rs index bfbfd51ca804f..6026917a26eb4 100644 --- a/srml/treasury/src/lib.rs +++ b/srml/treasury/src/lib.rs @@ -46,6 +46,7 @@ use runtime_support::{StorageValue, StorageMap}; use runtime_support::dispatch::Result; use runtime_primitives::{Permill, traits::{OnFinalise, Zero, EnsureOrigin}}; use balances::OnDilution; +use system::ensure_signed; /// Our module's configuration trait. All our types and consts go in here. If the /// module is dependent on specific other modules, then their configuration traits @@ -73,7 +74,7 @@ decl_module! { // Put forward a suggestion for spending. A deposit proportional to the value // is reserved and slashed if the proposal is rejected. It is returned once the // proposal is awarded. - fn propose_spend(SystemOrigin(Signed(proposer)), value: T::Balance, beneficiary: T::AccountId) -> Result; + fn propose_spend(origin, value: T::Balance, beneficiary: T::AccountId) -> Result; // Set the balance of funds available to spend. fn set_pot(new_pot: T::Balance) -> Result; @@ -157,7 +158,9 @@ impl Module { // Implement Calls and add public immutables and private mutables. - fn propose_spend(proposer: T::AccountId, value: T::Balance, beneficiary: T::AccountId) -> Result { + fn propose_spend(origin: T::Origin, value: T::Balance, beneficiary: T::AccountId) -> Result { + let proposer = ensure_signed(origin)?; + let bond = Self::calculate_bond(value); >::reserve(&proposer, bond) .map_err(|_| "Proposer's balance too low")?; From b62fa392054aef05ffb888513fa04170fcd052e9 Mon Sep 17 00:00:00 2001 From: Guanqun Lu Date: Sat, 15 Sep 2018 19:38:37 +0800 Subject: [PATCH 06/21] sr-support: add CouncilOrigin --- srml/support/src/dispatch.rs | 126 ++++++++++++++++++++++------------- 1 file changed, 81 insertions(+), 45 deletions(-) diff --git a/srml/support/src/dispatch.rs b/srml/support/src/dispatch.rs index 09c27306aedc2..4c5d03bfd32e6 100644 --- a/srml/support/src/dispatch.rs +++ b/srml/support/src/dispatch.rs @@ -71,7 +71,7 @@ macro_rules! decl_module { decl_module!(@normalize $(#[$attr])* pub struct $mod_type<$trait_instance: $trait_name> - for enum $call_type where origin: $origin_type where system = system + for enum $call_type where origin: $origin_type where system = system where council = council [] $($t)* ); @@ -79,24 +79,23 @@ macro_rules! decl_module { ( $(#[$attr:meta])* pub struct $mod_type:ident<$trait_instance:ident: $trait_name:ident> - for enum $call_type:ident where origin: $origin_type:ty where system = $system:ident { + for enum $call_type:ident where origin: $origin_type:ty where system = $system:ident where council = $council:ident { $($t:tt)* } ) => { decl_module!(@normalize $(#[$attr])* pub struct $mod_type<$trait_instance: $trait_name> - for enum $call_type where origin: $origin_type where system = $system + for enum $call_type where origin: $origin_type where system = $system where council = $council [] $($t)* ); }; - // fn $fn_name(origin, $param_name: $param) (@normalize $(#[$attr:meta])* pub struct $mod_type:ident<$trait_instance:ident: $trait_name:ident> - for enum $call_type:ident where origin: $origin_type:ty where system = $system:ident + for enum $call_type:ident where origin: $origin_type:ty where system = $system:ident where council = $council:ident [ $($t:tt)* ] $(#[doc = $doc_attr:tt])* fn $fn_name:ident(origin $(, $param_name:ident : $param:ty)* ) -> $result:ty ; @@ -105,51 +104,32 @@ macro_rules! decl_module { decl_module!(@normalize $(#[$attr])* pub struct $mod_type<$trait_instance: $trait_name> - for enum $call_type where origin: $origin_type where system = $system + for enum $call_type where origin: $origin_type where system = $system where council = $council [ $($t)* $(#[doc = $doc_attr])* fn $fn_name((origin origin origin) $( , $param_name : $param )* ) -> $result; ] $($rest)* ); }; - // fn $fn_name(SystemOrigin(Signed(who)), $param_name: $param) (@normalize $(#[$attr:meta])* pub struct $mod_type:ident<$trait_instance:ident: $trait_name:ident> - for enum $call_type:ident where origin: $origin_type:ty where system = $system:ident + for enum $call_type:ident where origin: $origin_type:ty where system = $system:ident where council = $council:ident [ $($t:tt)* ] $(#[doc = $doc_attr:tt])* - fn $fn_name:ident(SystemOrigin(Signed($who:ident)) $(, $param_name:ident : $param:ty)* ) -> $result:ty ; + fn $fn_name:ident($top_origin:ident($sub_origin:ident($origin_param:ident)) $(, $param_name:ident : $param:ty)* ) -> $result:ty ; $($rest:tt)* ) => { decl_module!(@normalize $(#[$attr])* pub struct $mod_type<$trait_instance: $trait_name> - for enum $call_type where origin: $origin_type where system = $system - [ $($t)* $(#[doc = $doc_attr])* fn $fn_name((system signed $who) $( , $param_name : $param )* ) -> $result; ] + for enum $call_type where origin: $origin_type where system = $system where council = $council + [ $($t)* $(#[doc = $doc_attr])* fn $fn_name(($top_origin $sub_origin $origin_param) $( , $param_name : $param )* ) -> $result; ] $($rest)* ); }; - // fn $fn_name(CouncilOrigin(Members(n)), $param_name: $param) (@normalize $(#[$attr:meta])* pub struct $mod_type:ident<$trait_instance:ident: $trait_name:ident> - for enum $call_type:ident where origin: $origin_type:ty where system = $system:ident - [ $($t:tt)* ] - $(#[doc = $doc_attr:tt])* - fn $fn_name:ident(CouncilOrigin(Members($n:ident)) $(, $param_name:ident : $param:ty)* ) -> $result:ty ; - $($rest:tt)* - ) => { - decl_module!(@normalize - $(#[$attr])* - pub struct $mod_type<$trait_instance: $trait_name> - for enum $call_type where origin: $origin_type where system = $system - [ $($t)* $(#[doc = $doc_attr])* fn $fn_name((council members $n) $( , $param_name : $param )* ) -> $result; ] - $($rest)* - ); - }; - (@normalize - $(#[$attr:meta])* - pub struct $mod_type:ident<$trait_instance:ident: $trait_name:ident> - for enum $call_type:ident where origin: $origin_type:ty where system = $system:ident + for enum $call_type:ident where origin: $origin_type:ty where system = $system:ident where council = $council:ident [ $($t:tt)* ] $(#[doc = $doc_attr:tt])* fn $fn_name:ident($( $param_name:ident : $param:ty),* ) -> $result:ty ; @@ -158,7 +138,7 @@ macro_rules! decl_module { decl_module!(@normalize $(#[$attr])* pub struct $mod_type<$trait_instance: $trait_name> - for enum $call_type where origin: $origin_type where system = $system + for enum $call_type where origin: $origin_type where system = $system where council = $council [ $($t)* $(#[doc = $doc_attr])* fn $fn_name((root root root) $( , $param_name : $param )* ) -> $result; ] $($rest)* ); @@ -166,13 +146,13 @@ macro_rules! decl_module { (@normalize $(#[$attr:meta])* pub struct $mod_type:ident<$trait_instance:ident: $trait_name:ident> - for enum $call_type:ident where origin: $origin_type:ty where system = $system:ident + for enum $call_type:ident where origin: $origin_type:ty where system = $system:ident where council = $council:ident [ $($t:tt)* ] ) => { decl_module!(@imp $(#[$attr])* pub struct $mod_type<$trait_instance: $trait_name> - for enum $call_type where origin: $origin_type where system = $system { + for enum $call_type where origin: $origin_type where system = $system where council = $council { $($t)* } ); @@ -180,13 +160,13 @@ macro_rules! decl_module { (@call (origin origin origin) - $mod_type:ident $trait_instance:ident $fn_name:ident $origin:ident $system:ident [ $( $param_name:ident),* ] + $mod_type:ident $trait_instance:ident $fn_name:ident $origin:ident $system:ident $council:ident [ $( $param_name:ident),* ] ) => { <$mod_type<$trait_instance>>::$fn_name( $origin $(, $param_name )* ) }; (@call (root root root) - $mod_type:ident $trait_instance:ident $fn_name:ident $origin:ident $system:ident [ $( $param_name:ident),* ] + $mod_type:ident $trait_instance:ident $fn_name:ident $origin:ident $system:ident $council:ident [ $( $param_name:ident),* ] ) => { { $system::ensure_root($origin)?; @@ -194,19 +174,28 @@ macro_rules! decl_module { } }; (@call - (system signed $who:ident) - $mod_type:ident $trait_instance:ident $fn_name:ident $origin:ident $system:ident [ $( $param_name:ident),* ] + (SystemOrigin Signed $who:ident) + $mod_type:ident $trait_instance:ident $fn_name:ident $origin:ident $system:ident $council:ident [ $( $param_name:ident),* ] ) => { { let $who = $system::ensure_signed($origin)?; <$mod_type<$trait_instance>>::$fn_name( $who $(, $param_name )* ) } }; + (@call + (CouncilOrigin Members $n:ident) + $mod_type:ident $trait_instance:ident $fn_name:ident $origin:ident $system:ident $council:ident [ $( $param_name:ident),* ] + ) => { + { + $council::ensure_council_members($n::VALUE)?; + <$mod_type<$trait_instance>>::$fn_name( $( $param_name ),* ) + } + }; (@imp $(#[$attr:meta])* pub struct $mod_type:ident<$trait_instance:ident: $trait_name:ident> - for enum $call_type:ident where origin: $origin_type:ty where system = $system:ident { + for enum $call_type:ident where origin: $origin_type:ty where system = $system:ident where council = $council:ident { $( $(#[doc = $doc_attr:tt])* fn $fn_name:ident(($top_origin:ident $sub_origin:ident $origin_param:ident) $( , $param_name:ident : $param:ty)*) -> $result:ty; @@ -334,7 +323,7 @@ macro_rules! decl_module { match self { $( $call_type::$fn_name( $( $param_name ),* ) => { - decl_module!(@call ($top_origin $sub_origin $origin_param) $mod_type $trait_instance $fn_name _origin $system [ $( $param_name ),* ]) + decl_module!(@call ($top_origin $sub_origin $origin_param) $mod_type $trait_instance $fn_name _origin $system $council [ $( $param_name ),* ]) }, )* _ => { panic!("__PhantomItem should never be used.") }, @@ -603,14 +592,14 @@ macro_rules! __functions_to_json { ), __functions_to_json!(","; $fn_id + 1; $origin_type; $trait_instance; $($rest)*) ) }; - // system signed who + // system signed $who ( $prefix_str:tt; $fn_id:expr; $origin_type:ty; $trait_instance:ident; fn $fn_name:ident( - (system signed $who:ident) + (SystemOrigin Signed $who:ident) $( , $param_name:ident : $param:ty )* @@ -629,6 +618,31 @@ macro_rules! __functions_to_json { ), __functions_to_json!(","; $fn_id + 1; $origin_type; $trait_instance; $($rest)*) ) }; + // council members $n + ( + $prefix_str:tt; + $fn_id:expr; + $origin_type:ty; + $trait_instance:ident; + fn $fn_name:ident( + (CouncilOrigin Members $n:ident) + $( + , $param_name:ident : $param:ty + )* + ) -> $result:ty; + $fn_doc:expr; + $($rest:tt)* + ) => { + concat!($prefix_str, " ", + __function_to_json!( + fn $fn_name( + $( $param_name : $param ),* + ) -> $result; + $fn_doc; + $fn_id; + ), __functions_to_json!(","; $fn_id + 1; $origin_type; $trait_instance; $($rest)*) + ) + }; // BASE CASE ( $prefix_str:tt; @@ -705,6 +719,7 @@ mod tests { use super::*; use serde; use serde_json; + use primitives::u32_trait::{_2, Value}; pub trait Trait { type Origin; @@ -724,6 +739,14 @@ mod tests { } } + pub mod council { + use super::Result; + + pub fn ensure_council_members(_: N) -> Result { + Ok(()) + } + } + decl_module! { pub struct Module for enum Call where origin: T::Origin { /// Hi, this is a comment. @@ -734,10 +757,8 @@ mod tests { fn aux_4(data: i32) -> Result; fn aux_5(SystemOrigin(Signed(who))) -> Result; fn aux_6(SystemOrigin(Signed(who)), data: i32) -> Result; -// fn aux_7(CouncilOrigin(Members(n))) -> Result; -// fn aux_8(CouncilOrigin(Members(n)), data: i32) -> Result; -// fn aux_9(CouncilOrigin(Members(n)) if n > 2) -> Result; -// fn aux_10(CouncilOrigin(Members(n)) if n > 2, data: i32) -> Result; + fn aux_7(CouncilOrigin(Members(_2))) -> Result; + fn aux_8(CouncilOrigin(Members(_2)), data: i32) -> Result; } } @@ -773,6 +794,13 @@ mod tests { r#""0 + 1 + 1 + 1 + 1 + 1 + 1": { "name": "aux_6", "params": [ "#, r#"{ "name": "who", "type": "T::AccountId" }, "#, r#"{ "name": "data", "type": "i32" }"#, + r#" ], "description": [ ] }, "#, + + r#""0 + 1 + 1 + 1 + 1 + 1 + 1 + 1": { "name": "aux_7", "params": [ "#, + r#" ], "description": [ ] }, "#, + + r#""0 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1": { "name": "aux_8", "params": [ "#, + r#"{ "name": "data", "type": "i32" }"#, r#" ], "description": [ ] }"#, r#" } }"#, @@ -807,6 +835,14 @@ mod tests { fn aux_6(_: &T::AccountId, _: i32) -> Result { unreachable!() } + + fn aux_7() -> Result { + unreachable!() + } + + fn aux_8(_: i32) -> Result { + unreachable!() + } } struct TraitImpl {} From 52fcb1e3120308d326598fec8a0fb9d0aff272e8 Mon Sep 17 00:00:00 2001 From: Guanqun Lu Date: Sat, 15 Sep 2018 19:49:58 +0800 Subject: [PATCH 07/21] democracy: update this module's origin declaration --- srml/democracy/src/lib.rs | 72 ++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 38 deletions(-) diff --git a/srml/democracy/src/lib.rs b/srml/democracy/src/lib.rs index 68db72fa4667b..95b68fd163c67 100644 --- a/srml/democracy/src/lib.rs +++ b/srml/democracy/src/lib.rs @@ -46,7 +46,6 @@ use rstd::result; use primitives::traits::{Zero, OnFinalise, As, MaybeSerializeDebug}; use srml_support::{StorageValue, StorageMap, Parameter, Dispatchable, IsSubType}; use srml_support::dispatch::Result; -use system::ensure_signed; #[cfg(any(feature = "std", test))] use std::collections::HashMap; @@ -67,9 +66,9 @@ pub trait Trait: balances::Trait + Sized { decl_module! { pub struct Module for enum Call where origin: T::Origin { - fn propose(origin, proposal: Box, value: T::Balance) -> Result; - fn second(origin, proposal: PropIndex) -> Result; - fn vote(origin, ref_index: ReferendumIndex, approve_proposal: bool) -> Result; + fn propose(SystemOrigin(Signed(who)), proposal: Box, value: T::Balance) -> Result; + fn second(SystemOrigin(Signed(who)), proposal: PropIndex) -> Result; + fn vote(SystemOrigin(Signed(who)), ref_index: ReferendumIndex, approve_proposal: bool) -> Result; fn start_referendum(proposal: Box, vote_threshold: VoteThreshold) -> Result; fn cancel_referendum(ref_index: ReferendumIndex) -> Result; @@ -170,8 +169,7 @@ impl Module { // dispatching. /// Propose a sensitive action to be taken. - fn propose(origin: T::Origin, proposal: Box, value: T::Balance) -> Result { - let who = ensure_signed(origin)?; + fn propose(who: T::AccountId, proposal: Box, value: T::Balance) -> Result { ensure!(value >= Self::minimum_deposit(), "value too low"); >::reserve(&who, value) .map_err(|_| "proposer's balance too low")?; @@ -187,8 +185,7 @@ impl Module { } /// Propose a sensitive action to be taken. - fn second(origin: T::Origin, proposal: PropIndex) -> Result { - let who = ensure_signed(origin)?; + fn second(who: T::AccountId, proposal: PropIndex) -> Result { let mut deposit = Self::deposit_of(proposal) .ok_or("can only second an existing proposal")?; >::reserve(&who, deposit.0) @@ -200,8 +197,7 @@ impl Module { /// Vote in a referendum. If `approve_proposal` is true, the vote is to enact the proposal; /// false would be a vote to keep the status quo. - fn vote(origin: T::Origin, ref_index: ReferendumIndex, approve_proposal: bool) -> Result { - let who = ensure_signed(origin)?; + fn vote(who: T::AccountId, ref_index: ReferendumIndex, approve_proposal: bool) -> Result { ensure!(Self::is_active_referendum(ref_index), "vote given for invalid referendum."); ensure!(!>::total_balance(&who).is_zero(), "transactor must have balance to signal approval."); @@ -452,7 +448,7 @@ mod tests { } fn propose_set_balance(who: u64, value: u64, locked: u64) -> super::Result { - Democracy::propose(Origin::signed(who), Box::new(set_balance_proposal(value)), locked) + Democracy::propose(who, Box::new(set_balance_proposal(value)), locked) } #[test] @@ -477,7 +473,7 @@ mod tests { System::set_block_number(2); let r = 0; - assert_ok!(Democracy::vote(Origin::signed(1), r, true)); + assert_ok!(Democracy::vote(1, r, true)); assert_eq!(Democracy::referendum_count(), 1); assert_eq!(Democracy::voters_for(r), vec![1]); @@ -495,10 +491,10 @@ mod tests { with_externalities(&mut new_test_ext(), || { System::set_block_number(1); assert_ok!(propose_set_balance(1, 2, 5)); - assert_ok!(Democracy::second(Origin::signed(2), 0)); - assert_ok!(Democracy::second(Origin::signed(5), 0)); - assert_ok!(Democracy::second(Origin::signed(5), 0)); - assert_ok!(Democracy::second(Origin::signed(5), 0)); + assert_ok!(Democracy::second(2, 0)); + assert_ok!(Democracy::second(5, 0)); + assert_ok!(Democracy::second(5, 0)); + assert_ok!(Democracy::second(5, 0)); assert_eq!(Balances::free_balance(&1), 5); assert_eq!(Balances::free_balance(&2), 15); assert_eq!(Balances::free_balance(&5), 35); @@ -510,10 +506,10 @@ mod tests { with_externalities(&mut new_test_ext(), || { System::set_block_number(1); assert_ok!(propose_set_balance(1, 2, 5)); - assert_ok!(Democracy::second(Origin::signed(2), 0)); - assert_ok!(Democracy::second(Origin::signed(5), 0)); - assert_ok!(Democracy::second(Origin::signed(5), 0)); - assert_ok!(Democracy::second(Origin::signed(5), 0)); + assert_ok!(Democracy::second(2, 0)); + assert_ok!(Democracy::second(5, 0)); + assert_ok!(Democracy::second(5, 0)); + assert_ok!(Democracy::second(5, 0)); assert_eq!(Democracy::end_block(System::block_number()), Ok(())); assert_eq!(Balances::free_balance(&1), 10); assert_eq!(Balances::free_balance(&2), 20); @@ -542,7 +538,7 @@ mod tests { with_externalities(&mut new_test_ext(), || { System::set_block_number(1); assert_ok!(propose_set_balance(2, 2, 11)); - assert_noop!(Democracy::second(Origin::signed(1), 0), "seconder\'s balance too low"); + assert_noop!(Democracy::second(1, 0), "seconder\'s balance too low"); }); } @@ -556,17 +552,17 @@ mod tests { assert_eq!(Democracy::end_block(System::block_number()), Ok(())); System::set_block_number(1); - assert_ok!(Democracy::vote(Origin::signed(1), 0, true)); + assert_ok!(Democracy::vote(1, 0, true)); assert_eq!(Democracy::end_block(System::block_number()), Ok(())); assert_eq!(Balances::free_balance(&42), 4); System::set_block_number(2); - assert_ok!(Democracy::vote(Origin::signed(1), 1, true)); + assert_ok!(Democracy::vote(1, 1, true)); assert_eq!(Democracy::end_block(System::block_number()), Ok(())); assert_eq!(Balances::free_balance(&42), 3); System::set_block_number(3); - assert_ok!(Democracy::vote(Origin::signed(1), 2, true)); + assert_ok!(Democracy::vote(1, 2, true)); assert_eq!(Democracy::end_block(System::block_number()), Ok(())); }); } @@ -576,7 +572,7 @@ mod tests { with_externalities(&mut new_test_ext(), || { System::set_block_number(1); let r = Democracy::inject_referendum(1, set_balance_proposal(2), VoteThreshold::SuperMajorityApprove).unwrap(); - assert_ok!(Democracy::vote(Origin::signed(1), r, true)); + assert_ok!(Democracy::vote(1, r, true)); assert_eq!(Democracy::voters_for(r), vec![1]); assert_eq!(Democracy::vote_of((r, 1)), Some(true)); @@ -593,7 +589,7 @@ mod tests { with_externalities(&mut new_test_ext(), || { System::set_block_number(1); let r = Democracy::inject_referendum(1, set_balance_proposal(2), VoteThreshold::SuperMajorityApprove).unwrap(); - assert_ok!(Democracy::vote(Origin::signed(1), r, true)); + assert_ok!(Democracy::vote(1, r, true)); assert_ok!(Democracy::cancel_referendum(r)); assert_eq!(Democracy::end_block(System::block_number()), Ok(())); @@ -607,7 +603,7 @@ mod tests { with_externalities(&mut new_test_ext(), || { System::set_block_number(1); let r = Democracy::inject_referendum(1, set_balance_proposal(2), VoteThreshold::SuperMajorityApprove).unwrap(); - assert_ok!(Democracy::vote(Origin::signed(1), r, false)); + assert_ok!(Democracy::vote(1, r, false)); assert_eq!(Democracy::voters_for(r), vec![1]); assert_eq!(Democracy::vote_of((r, 1)), Some(false)); @@ -624,12 +620,12 @@ mod tests { with_externalities(&mut new_test_ext(), || { System::set_block_number(1); let r = Democracy::inject_referendum(1, set_balance_proposal(2), VoteThreshold::SuperMajorityApprove).unwrap(); - assert_ok!(Democracy::vote(Origin::signed(1), r, true)); - assert_ok!(Democracy::vote(Origin::signed(2), r, false)); - assert_ok!(Democracy::vote(Origin::signed(3), r, false)); - assert_ok!(Democracy::vote(Origin::signed(4), r, true)); - assert_ok!(Democracy::vote(Origin::signed(5), r, false)); - assert_ok!(Democracy::vote(Origin::signed(6), r, true)); + assert_ok!(Democracy::vote(1, r, true)); + assert_ok!(Democracy::vote(2, r, false)); + assert_ok!(Democracy::vote(3, r, false)); + assert_ok!(Democracy::vote(4, r, true)); + assert_ok!(Democracy::vote(5, r, false)); + assert_ok!(Democracy::vote(6, r, true)); assert_eq!(Democracy::tally(r), (110, 100)); @@ -644,8 +640,8 @@ mod tests { with_externalities(&mut new_test_ext(), || { System::set_block_number(1); let r = Democracy::inject_referendum(1, set_balance_proposal(2), VoteThreshold::SuperMajorityApprove).unwrap(); - assert_ok!(Democracy::vote(Origin::signed(5), r, false)); - assert_ok!(Democracy::vote(Origin::signed(6), r, true)); + assert_ok!(Democracy::vote(5, r, false)); + assert_ok!(Democracy::vote(6, r, true)); assert_eq!(Democracy::tally(r), (60, 50)); @@ -663,9 +659,9 @@ mod tests { System::set_block_number(1); let r = Democracy::inject_referendum(1, set_balance_proposal(2), VoteThreshold::SuperMajorityApprove).unwrap(); - assert_ok!(Democracy::vote(Origin::signed(4), r, true)); - assert_ok!(Democracy::vote(Origin::signed(5), r, false)); - assert_ok!(Democracy::vote(Origin::signed(6), r, true)); + assert_ok!(Democracy::vote(4, r, true)); + assert_ok!(Democracy::vote(5, r, false)); + assert_ok!(Democracy::vote(6, r, true)); assert_eq!(Democracy::tally(r), (100, 50)); From ac5bbacb5af9c8d3260c63dc97f3d6cb9fe0fa32 Mon Sep 17 00:00:00 2001 From: Guanqun Lu Date: Sat, 15 Sep 2018 22:44:46 +0800 Subject: [PATCH 08/21] update balance module --- srml/balances/src/lib.rs | 6 ++---- srml/balances/src/tests.rs | 34 +++++++++++++++++----------------- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/srml/balances/src/lib.rs b/srml/balances/src/lib.rs index c56b8d949af62..3b1b7f3f73d97 100644 --- a/srml/balances/src/lib.rs +++ b/srml/balances/src/lib.rs @@ -48,7 +48,6 @@ use runtime_support::dispatch::Result; use primitives::traits::{Zero, One, SimpleArithmetic, OnFinalise, MakePayment, As, Lookup, Member, CheckedAdd, CheckedSub}; use address::Address as RawAddress; -use system::ensure_signed; mod mock; @@ -130,7 +129,7 @@ pub trait Trait: system::Trait { decl_module! { pub struct Module for enum Call where origin: T::Origin { - fn transfer(origin, dest: RawAddress, value: T::Balance) -> Result; + fn transfer(SystemOrigin(Signed(who)), dest: RawAddress, value: T::Balance) -> Result; fn set_balance(who: RawAddress, free: T::Balance, reserved: T::Balance) -> Result; } } @@ -279,8 +278,7 @@ impl Module { // PUBLIC DISPATCH /// Transfer some liquid free balance to another staker. - pub fn transfer(origin: T::Origin, dest: Address, value: T::Balance) -> Result { - let transactor = ensure_signed(origin)?; + pub fn transfer(transactor: T::AccountId, dest: Address, value: T::Balance) -> Result { let dest = Self::lookup(dest)?; let from_balance = Self::free_balance(&transactor); diff --git a/srml/balances/src/tests.rs b/srml/balances/src/tests.rs index 475c3d70ff7db..0acc8bfcb755a 100644 --- a/srml/balances/src/tests.rs +++ b/srml/balances/src/tests.rs @@ -47,7 +47,7 @@ fn indexing_lookup_should_work() { fn default_indexing_on_new_accounts_should_work() { with_externalities(&mut new_test_ext(10, true), || { assert_eq!(Balances::lookup_index(4), None); - assert_ok!(Balances::transfer(Some(1).into(), 5.into(), 10)); + assert_ok!(Balances::transfer(1, 5.into(), 10)); assert_eq!(Balances::lookup_index(4), Some(5)); }); } @@ -57,7 +57,7 @@ fn default_indexing_on_new_accounts_should_work2() { with_externalities(&mut new_test_ext2(10, true), || { assert_eq!(Balances::lookup_index(4), None); // account 1 has 256 * 10 = 2560, account 5 is not exist, ext_deposit is 10, value is 10 - assert_ok!(Balances::transfer(Some(1).into(), 5.into(), 10)); + assert_ok!(Balances::transfer(1, 5.into(), 10)); assert_eq!(Balances::lookup_index(4), Some(5)); assert_eq!(Balances::free_balance(&1), 256 * 10 - 10 - 50); // 10 is value, 50 is creation_free @@ -69,7 +69,7 @@ fn default_indexing_on_new_accounts_should_not_work2() { with_externalities(&mut new_test_ext2(10, true), || { assert_eq!(Balances::lookup_index(4), None); // account 1 has 256 * 10 = 2560, account 5 is not exist, ext_deposit is 10, value is 9, not satisfies for ext_deposit - assert_noop!(Balances::transfer(Some(1).into(), 5.into(), 9), "value too low to create account"); + assert_noop!(Balances::transfer(1, 5.into(), 9), "value too low to create account"); assert_eq!(Balances::lookup_index(4), None); // account 5 should not exist assert_eq!(Balances::free_balance(&1), 256 * 10); }); @@ -82,7 +82,7 @@ fn dust_account_removal_should_work() { assert_eq!(System::account_nonce(&2), 1); assert_eq!(Balances::total_balance(&2), 256 * 20); - assert_ok!(Balances::transfer(Some(2).into(), 5.into(), 256 * 10 + 1)); // index 1 (account 2) becomes zombie + assert_ok!(Balances::transfer(2, 5.into(), 256 * 10 + 1)); // index 1 (account 2) becomes zombie assert_eq!(Balances::total_balance(&2), 0); assert_eq!(Balances::total_balance(&5), 256 * 10 + 1); assert_eq!(System::account_nonce(&2), 0); @@ -95,7 +95,7 @@ fn dust_account_removal_should_work2() { System::inc_account_nonce(&2); assert_eq!(System::account_nonce(&2), 1); assert_eq!(Balances::total_balance(&2), 256 * 20); - assert_ok!(Balances::transfer(Some(2).into(), 5.into(), 256 * 10)); // index 1 (account 2) becomes zombie for 256*10 + 50(fee) < 256 * 10 (ext_deposit) + assert_ok!(Balances::transfer(2, 5.into(), 256 * 10)); // index 1 (account 2) becomes zombie for 256*10 + 50(fee) < 256 * 10 (ext_deposit) assert_eq!(Balances::total_balance(&2), 0); assert_eq!(Balances::total_balance(&5), 256 * 10); assert_eq!(System::account_nonce(&2), 0); @@ -109,10 +109,10 @@ fn reclaim_indexing_on_new_accounts_should_work() { assert_eq!(Balances::lookup_index(4), None); assert_eq!(Balances::total_balance(&2), 256 * 20); - assert_ok!(Balances::transfer(Some(2).into(), 5.into(), 256 * 20)); // account 2 becomes zombie freeing index 1 for reclaim) + assert_ok!(Balances::transfer(2, 5.into(), 256 * 20)); // account 2 becomes zombie freeing index 1 for reclaim) assert_eq!(Balances::total_balance(&2), 0); - assert_ok!(Balances::transfer(Some(5).into(), 6.into(), 256 * 1 + 0x69)); // account 6 takes index 1. + assert_ok!(Balances::transfer(5, 6.into(), 256 * 1 + 0x69)); // account 6 takes index 1. assert_eq!(Balances::total_balance(&6), 256 * 1 + 0x69); assert_eq!(Balances::lookup_index(1), Some(6)); }); @@ -125,10 +125,10 @@ fn reclaim_indexing_on_new_accounts_should_work2() { assert_eq!(Balances::lookup_index(4), None); assert_eq!(Balances::total_balance(&2), 256 * 20); - assert_ok!(Balances::transfer(Some(2).into(), 5.into(), 256 * 20 - 50)); // account 2 becomes zombie freeing index 1 for reclaim) 50 is creation fee + assert_ok!(Balances::transfer(2, 5.into(), 256 * 20 - 50)); // account 2 becomes zombie freeing index 1 for reclaim) 50 is creation fee assert_eq!(Balances::total_balance(&2), 0); - assert_ok!(Balances::transfer(Some(5).into(), 6.into(), 256 * 1 + 0x69)); // account 6 takes index 1. + assert_ok!(Balances::transfer(5, 6.into(), 256 * 1 + 0x69)); // account 6 takes index 1. assert_eq!(Balances::total_balance(&6), 256 * 1 + 0x69); assert_eq!(Balances::lookup_index(1), Some(6)); }); @@ -147,7 +147,7 @@ fn reserved_balance_should_prevent_reclaim_count() { assert_eq!(Balances::total_balance(&2), 256 * 19 + 1); // reserve still exists. assert_eq!(System::account_nonce(&2), 1); - assert_ok!(Balances::transfer(Some(4).into(), 5.into(), 256 * 1 + 0x69)); // account 4 tries to take index 1 for account 5. + assert_ok!(Balances::transfer(4, 5.into(), 256 * 1 + 0x69)); // account 4 tries to take index 1 for account 5. assert_eq!(Balances::total_balance(&5), 256 * 1 + 0x69); assert_eq!(Balances::lookup_index(1), Some(2)); // but fails. assert_eq!(System::account_nonce(&2), 1); @@ -156,7 +156,7 @@ fn reserved_balance_should_prevent_reclaim_count() { assert_eq!(Balances::total_balance(&2), 0); // "free" account deleted." assert_eq!(System::account_nonce(&2), 0); - assert_ok!(Balances::transfer(Some(4).into(), 6.into(), 256 * 1 + 0x69)); // account 4 tries to take index 1 again for account 6. + assert_ok!(Balances::transfer(4, 6.into(), 256 * 1 + 0x69)); // account 4 tries to take index 1 again for account 6. assert_eq!(Balances::total_balance(&6), 256 * 1 + 0x69); assert_eq!(Balances::lookup_index(1), Some(6)); // and succeeds. }); @@ -175,7 +175,7 @@ fn reserved_balance_should_prevent_reclaim_count2() { assert_eq!(Balances::total_balance(&2), 256 * 19 + 1); // reserve still exists. assert_eq!(System::account_nonce(&2), 1); - assert_ok!(Balances::transfer(Some(4).into(), 5.into(), 256 * 1 + 0x69)); // account 4 tries to take index 1 for account 5. + assert_ok!(Balances::transfer(4, 5.into(), 256 * 1 + 0x69)); // account 4 tries to take index 1 for account 5. assert_eq!(Balances::total_balance(&5), 256 * 1 + 0x69); assert_eq!(Balances::lookup_index(1), Some(2)); // but fails. assert_eq!(System::account_nonce(&2), 1); @@ -184,7 +184,7 @@ fn reserved_balance_should_prevent_reclaim_count2() { assert_eq!(Balances::total_balance(&2), 0); // "free" account deleted." assert_eq!(System::account_nonce(&2), 0); - assert_ok!(Balances::transfer(Some(4).into(), 6.into(), 256 * 1 + 0x69)); // account 4 tries to take index 1 again for account 6. + assert_ok!(Balances::transfer(4, 6.into(), 256 * 1 + 0x69)); // account 4 tries to take index 1 again for account 6. assert_eq!(Balances::total_balance(&6), 256 * 1 + 0x69); assert_eq!(Balances::lookup_index(1), Some(6)); // and succeeds. }); @@ -208,7 +208,7 @@ fn balance_transfer_works() { with_externalities(&mut new_test_ext(0, false), || { Balances::set_free_balance(&1, 111); Balances::increase_total_stake_by(111); - assert_ok!(Balances::transfer(Some(1).into(), 2.into(), 69)); + assert_ok!(Balances::transfer(1, 2.into(), 69)); assert_eq!(Balances::total_balance(&1), 42); assert_eq!(Balances::total_balance(&2), 69); }); @@ -236,7 +236,7 @@ fn balance_transfer_when_reserved_should_not_work() { with_externalities(&mut new_test_ext(0, false), || { Balances::set_free_balance(&1, 111); assert_ok!(Balances::reserve(&1, 69)); - assert_noop!(Balances::transfer(Some(1).into(), 2.into(), 69), "balance too low to send value"); + assert_noop!(Balances::transfer(1, 2.into(), 69), "balance too low to send value"); }); } @@ -367,7 +367,7 @@ fn transferring_too_high_value_should_not_panic() { >::insert(2, 1); assert_err!( - Balances::transfer(Some(1).into(), 2.into(), u64::max_value()), + Balances::transfer(1, 2.into(), u64::max_value()), "destination balance too high to receive value" ); @@ -393,7 +393,7 @@ fn account_removal_on_free_too_low() { // Transfer funds from account 1 of such amount that after this transfer // the balance of account 1 will be below the exsistential threshold. // This should lead to the removal of all balance of this account. - assert_ok!(Balances::transfer(Some(1).into(), 2.into(), 20)); + assert_ok!(Balances::transfer(1, 2.into(), 20)); // Verify free balance removal of account 1. assert_eq!(Balances::free_balance(&1), 0); From 507ddcdd51bdeba90422bef3ddc5d5686e4f13bb Mon Sep 17 00:00:00 2001 From: Guanqun Lu Date: Sat, 15 Sep 2018 22:45:04 +0800 Subject: [PATCH 09/21] update consensus module --- srml/consensus/src/lib.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/srml/consensus/src/lib.rs b/srml/consensus/src/lib.rs index 522515d29b617..dabad4a082f7d 100644 --- a/srml/consensus/src/lib.rs +++ b/srml/consensus/src/lib.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Substrate. If not, see . -//! Conensus module for runtime; manages the authority set ready for the native code. +//! Consensus module for runtime; manages the authority set ready for the native code. #![cfg_attr(not(feature = "std"), no_std)] @@ -48,7 +48,7 @@ use runtime_support::storage::StorageValue; use runtime_support::storage::unhashed::StorageVec; use primitives::traits::{MaybeSerializeDebug, OnFinalise, Member, DigestItem}; use primitives::bft::MisbehaviorReport; -use system::{ensure_signed, ensure_inherent}; +use system::ensure_inherent; #[cfg(any(feature = "std", test))] use substrate_primitives::Blake2Hasher; @@ -129,9 +129,9 @@ decl_storage! { decl_module! { pub struct Module for enum Call where origin: T::Origin { - fn report_misbehavior(origin, report: MisbehaviorReport) -> Result; + fn report_misbehavior(SystemOrigin(Signed(who)), report: MisbehaviorReport) -> Result; fn note_offline(origin, offline_val_indices: Vec) -> Result; - fn remark(origin, remark: Vec) -> Result; + fn remark(SystemOrigin(Signed(who)), remark: Vec) -> Result; fn set_code(new: Vec) -> Result; fn set_storage(items: Vec) -> Result; } @@ -158,8 +158,7 @@ impl Module { } /// Report some misbehaviour. - fn report_misbehavior(origin: T::Origin, _report: MisbehaviorReport) -> Result { - ensure_signed(origin)?; + fn report_misbehavior(_who: T::AccountId, _report: MisbehaviorReport) -> Result { // TODO. Ok(()) } @@ -183,8 +182,7 @@ impl Module { } /// Make some on-chain remark. - fn remark(origin: T::Origin, _remark: Vec) -> Result { - ensure_signed(origin)?; + fn remark(_who: T::AccountId, _remark: Vec) -> Result { Ok(()) } From f04f2b4dfe60442439ecf237dfea3a0540c4549d Mon Sep 17 00:00:00 2001 From: Guanqun Lu Date: Sat, 15 Sep 2018 22:45:24 +0800 Subject: [PATCH 10/21] update concil module --- srml/council/src/seats.rs | 411 +++++++++++++++++++------------------- 1 file changed, 203 insertions(+), 208 deletions(-) diff --git a/srml/council/src/seats.rs b/srml/council/src/seats.rs index 97d2f75c92d74..25b68815b1d93 100644 --- a/srml/council/src/seats.rs +++ b/srml/council/src/seats.rs @@ -22,7 +22,7 @@ use runtime_io::print; use srml_support::{StorageValue, StorageMap, dispatch::Result}; use democracy; use balances::{self, address::Address}; -use system::{self, ensure_signed}; +use system; // no polynomial attacks: // @@ -86,11 +86,11 @@ pub trait Trait: democracy::Trait { decl_module! { pub struct Module for enum Call where origin: T::Origin { - fn set_approvals(origin, votes: Vec, index: VoteIndex) -> Result; - fn reap_inactive_voter(origin, reporter_index: u32, who: Address, who_index: u32, assumed_vote_index: VoteIndex) -> Result; - fn retract_voter(origin, index: u32) -> Result; - fn submit_candidacy(origin, slot: u32) -> Result; - fn present_winner(origin, candidate: Address, total: T::Balance, index: VoteIndex) -> Result; + fn set_approvals(SystemOrigin(Signed(who)), votes: Vec, index: VoteIndex) -> Result; + fn reap_inactive_voter(SystemOrigin(Signed(reporter)), reporter_index: u32, who: Address, who_index: u32, assumed_vote_index: VoteIndex) -> Result; + fn retract_voter(SystemOrigin(Signed(who)), index: u32) -> Result; + fn submit_candidacy(SystemOrigin(Signed(who)), slot: u32) -> Result; + fn present_winner(SystemOrigin(Signed(who)), candidate: Address, total: T::Balance, index: VoteIndex) -> Result; fn set_desired_seats(count: u32) -> Result; fn remove_member(who: Address) -> Result; @@ -227,8 +227,7 @@ impl Module { /// Set candidate approvals. Approval slots stay valid as long as candidates in those slots /// are registered. - fn set_approvals(origin: T::Origin, votes: Vec, index: VoteIndex) -> Result { - let who = ensure_signed(origin)?; + fn set_approvals(who: T::AccountId, votes: Vec, index: VoteIndex) -> Result { ensure!(!Self::presentation_active(), "no approval changes during presentation period"); ensure!(index == Self::vote_index(), "incorrect vote index"); @@ -254,13 +253,12 @@ impl Module { /// /// May be called by anyone. Returns the voter deposit to `signed`. fn reap_inactive_voter( - origin: T::Origin, + reporter: T::AccountId, reporter_index: u32, who: Address, who_index: u32, assumed_vote_index: VoteIndex ) -> Result { - let reporter = ensure_signed(origin)?; let who = >::lookup(who)?; ensure!(!Self::presentation_active(), "cannot reap during presentation period"); @@ -302,8 +300,7 @@ impl Module { } /// Remove a voter. All votes are cancelled and the voter deposit is returned. - fn retract_voter(origin: T::Origin, index: u32) -> Result { - let who = ensure_signed(origin)?; + fn retract_voter(who: T::AccountId, index: u32) -> Result { ensure!(!Self::presentation_active(), "cannot retract when presenting"); ensure!(>::exists(&who), "cannot retract non-voter"); @@ -320,8 +317,7 @@ impl Module { /// Submit oneself for candidacy. /// /// Account must have enough transferrable funds in it to pay the bond. - fn submit_candidacy(origin: T::Origin, slot: u32) -> Result { - let who = ensure_signed(origin)?; + fn submit_candidacy(who: T::AccountId, slot: u32) -> Result { ensure!(!Self::is_a_candidate(&who), "duplicate candidate submission"); let slot = slot as usize; @@ -352,12 +348,11 @@ impl Module { /// Only works if the `block_number >= current_vote().0` and `< current_vote().0 + presentation_duration()`` /// `signed` should have at least fn present_winner( - origin: T::Origin, + who: T::AccountId, candidate: Address, total: T::Balance, index: VoteIndex ) -> Result { - let who = ensure_signed(origin)?; let candidate = >::lookup(candidate)?; ensure!(index == Self::vote_index(), "index not current"); @@ -606,14 +601,14 @@ mod tests { assert_eq!(Council::is_a_candidate(&1), false); assert_eq!(Council::is_a_candidate(&2), false); - assert_ok!(Council::submit_candidacy(Origin::signed(1), 0)); + assert_ok!(Council::submit_candidacy(1, 0)); assert_eq!(Council::candidates(), vec![1]); assert_eq!(Council::candidate_reg_info(1), Some((0, 0))); assert_eq!(Council::candidate_reg_info(2), None); assert_eq!(Council::is_a_candidate(&1), true); assert_eq!(Council::is_a_candidate(&2), false); - assert_ok!(Council::submit_candidacy(Origin::signed(2), 1)); + assert_ok!(Council::submit_candidacy(2, 1)); assert_eq!(Council::candidates(), vec![1, 2]); assert_eq!(Council::candidate_reg_info(1), Some((0, 0))); assert_eq!(Council::candidate_reg_info(2), Some((0, 1))); @@ -640,10 +635,10 @@ mod tests { System::set_block_number(1); assert_eq!(Council::candidates(), vec![0, 0, 1]); - assert_ok!(Council::submit_candidacy(Origin::signed(2), 1)); + assert_ok!(Council::submit_candidacy(2, 1)); assert_eq!(Council::candidates(), vec![0, 2, 1]); - assert_ok!(Council::submit_candidacy(Origin::signed(3), 0)); + assert_ok!(Council::submit_candidacy(3, 0)); assert_eq!(Council::candidates(), vec![3, 2, 1]); }); } @@ -656,10 +651,10 @@ mod tests { System::set_block_number(1); assert_eq!(Council::candidates(), vec![0, 0, 1]); - assert_ok!(Council::submit_candidacy(Origin::signed(2), 0)); + assert_ok!(Council::submit_candidacy(2, 0)); assert_eq!(Council::candidates(), vec![2, 0, 1]); - assert_ok!(Council::submit_candidacy(Origin::signed(3), 1)); + assert_ok!(Council::submit_candidacy(3, 1)); assert_eq!(Council::candidates(), vec![2, 3, 1]); }); } @@ -668,7 +663,7 @@ mod tests { fn candidate_submission_not_using_free_slot_should_not_work() { with_externalities(&mut new_test_ext_with_candidate_holes(), || { System::set_block_number(1); - assert_noop!(Council::submit_candidacy(Origin::signed(4), 3), "invalid candidate slot"); + assert_noop!(Council::submit_candidacy(4, 3), "invalid candidate slot"); }); } @@ -677,7 +672,7 @@ mod tests { with_externalities(&mut new_test_ext(false), || { System::set_block_number(1); assert_eq!(Council::candidates(), Vec::::new()); - assert_noop!(Council::submit_candidacy(Origin::signed(1), 1), "invalid candidate slot"); + assert_noop!(Council::submit_candidacy(1, 1), "invalid candidate slot"); }); } @@ -686,9 +681,9 @@ mod tests { with_externalities(&mut new_test_ext(false), || { System::set_block_number(1); assert_eq!(Council::candidates(), Vec::::new()); - assert_ok!(Council::submit_candidacy(Origin::signed(1), 0)); + assert_ok!(Council::submit_candidacy(1, 0)); assert_eq!(Council::candidates(), vec![1]); - assert_noop!(Council::submit_candidacy(Origin::signed(2), 0), "invalid candidate slot"); + assert_noop!(Council::submit_candidacy(2, 0), "invalid candidate slot"); }); } @@ -697,9 +692,9 @@ mod tests { with_externalities(&mut new_test_ext(false), || { System::set_block_number(1); assert_eq!(Council::candidates(), Vec::::new()); - assert_ok!(Council::submit_candidacy(Origin::signed(1), 0)); + assert_ok!(Council::submit_candidacy(1, 0)); assert_eq!(Council::candidates(), vec![1]); - assert_noop!(Council::submit_candidacy(Origin::signed(1), 1), "duplicate candidate submission"); + assert_noop!(Council::submit_candidacy(1, 1), "duplicate candidate submission"); }); } @@ -708,7 +703,7 @@ mod tests { with_externalities(&mut new_test_ext(false), || { System::set_block_number(1); assert_eq!(Council::candidates(), Vec::::new()); - assert_noop!(Council::submit_candidacy(Origin::signed(7), 0), "candidate has not enough funds"); + assert_noop!(Council::submit_candidacy(7, 0), "candidate has not enough funds"); }); } @@ -717,20 +712,20 @@ mod tests { with_externalities(&mut new_test_ext(false), || { System::set_block_number(1); - assert_ok!(Council::submit_candidacy(Origin::signed(5), 0)); + assert_ok!(Council::submit_candidacy(5, 0)); - assert_ok!(Council::set_approvals(Origin::signed(1), vec![true], 0)); - assert_ok!(Council::set_approvals(Origin::signed(4), vec![true], 0)); + assert_ok!(Council::set_approvals(1, vec![true], 0)); + assert_ok!(Council::set_approvals(4, vec![true], 0)); assert_eq!(Council::approvals_of(1), vec![true]); assert_eq!(Council::approvals_of(4), vec![true]); assert_eq!(Council::voters(), vec![1, 4]); - assert_ok!(Council::submit_candidacy(Origin::signed(2), 1)); - assert_ok!(Council::submit_candidacy(Origin::signed(3), 2)); + assert_ok!(Council::submit_candidacy(2, 1)); + assert_ok!(Council::submit_candidacy(3, 2)); - assert_ok!(Council::set_approvals(Origin::signed(2), vec![false, true, true], 0)); - assert_ok!(Council::set_approvals(Origin::signed(3), vec![false, true, true], 0)); + assert_ok!(Council::set_approvals(2, vec![false, true, true], 0)); + assert_ok!(Council::set_approvals(3, vec![false, true, true], 0)); assert_eq!(Council::approvals_of(1), vec![true]); assert_eq!(Council::approvals_of(4), vec![true]); @@ -746,14 +741,14 @@ mod tests { with_externalities(&mut new_test_ext(false), || { System::set_block_number(1); - assert_ok!(Council::submit_candidacy(Origin::signed(5), 0)); - assert_ok!(Council::set_approvals(Origin::signed(4), vec![true], 0)); + assert_ok!(Council::submit_candidacy(5, 0)); + assert_ok!(Council::set_approvals(4, vec![true], 0)); assert_eq!(Council::approvals_of(4), vec![true]); - assert_ok!(Council::submit_candidacy(Origin::signed(2), 1)); - assert_ok!(Council::submit_candidacy(Origin::signed(3), 2)); - assert_ok!(Council::set_approvals(Origin::signed(4), vec![true, false, true], 0)); + assert_ok!(Council::submit_candidacy(2, 1)); + assert_ok!(Council::submit_candidacy(3, 2)); + assert_ok!(Council::set_approvals(4, vec![true, false, true], 0)); assert_eq!(Council::approvals_of(4), vec![true, false, true]); }); @@ -764,14 +759,14 @@ mod tests { with_externalities(&mut new_test_ext(false), || { System::set_block_number(1); - assert_ok!(Council::submit_candidacy(Origin::signed(5), 0)); - assert_ok!(Council::submit_candidacy(Origin::signed(2), 1)); - assert_ok!(Council::submit_candidacy(Origin::signed(3), 2)); + assert_ok!(Council::submit_candidacy(5, 0)); + assert_ok!(Council::submit_candidacy(2, 1)); + assert_ok!(Council::submit_candidacy(3, 2)); - assert_ok!(Council::set_approvals(Origin::signed(1), vec![true], 0)); - assert_ok!(Council::set_approvals(Origin::signed(2), vec![false, true, true], 0)); - assert_ok!(Council::set_approvals(Origin::signed(3), vec![false, true, true], 0)); - assert_ok!(Council::set_approvals(Origin::signed(4), vec![true, false, true], 0)); + assert_ok!(Council::set_approvals(1, vec![true], 0)); + assert_ok!(Council::set_approvals(2, vec![false, true, true], 0)); + assert_ok!(Council::set_approvals(3, vec![false, true, true], 0)); + assert_ok!(Council::set_approvals(4, vec![true, false, true], 0)); assert_eq!(Council::voters(), vec![1, 2, 3, 4]); assert_eq!(Council::approvals_of(1), vec![true]); @@ -779,7 +774,7 @@ mod tests { assert_eq!(Council::approvals_of(3), vec![false, true, true]); assert_eq!(Council::approvals_of(4), vec![true, false, true]); - assert_ok!(Council::retract_voter(Origin::signed(1), 0)); + assert_ok!(Council::retract_voter(1, 0)); assert_eq!(Council::voters(), vec![4, 2, 3]); assert_eq!(Council::approvals_of(1), Vec::::new()); @@ -787,7 +782,7 @@ mod tests { assert_eq!(Council::approvals_of(3), vec![false, true, true]); assert_eq!(Council::approvals_of(4), vec![true, false, true]); - assert_ok!(Council::retract_voter(Origin::signed(2), 1)); + assert_ok!(Council::retract_voter(2, 1)); assert_eq!(Council::voters(), vec![4, 3]); assert_eq!(Council::approvals_of(1), Vec::::new()); @@ -795,7 +790,7 @@ mod tests { assert_eq!(Council::approvals_of(3), vec![false, true, true]); assert_eq!(Council::approvals_of(4), vec![true, false, true]); - assert_ok!(Council::retract_voter(Origin::signed(3), 1)); + assert_ok!(Council::retract_voter(3, 1)); assert_eq!(Council::voters(), vec![4]); assert_eq!(Council::approvals_of(1), Vec::::new()); @@ -809,11 +804,11 @@ mod tests { fn invalid_retraction_index_should_not_work() { with_externalities(&mut new_test_ext(false), || { System::set_block_number(1); - assert_ok!(Council::submit_candidacy(Origin::signed(3), 0)); - assert_ok!(Council::set_approvals(Origin::signed(1), vec![true], 0)); - assert_ok!(Council::set_approvals(Origin::signed(2), vec![true], 0)); + assert_ok!(Council::submit_candidacy(3, 0)); + assert_ok!(Council::set_approvals(1, vec![true], 0)); + assert_ok!(Council::set_approvals(2, vec![true], 0)); assert_eq!(Council::voters(), vec![1, 2]); - assert_noop!(Council::retract_voter(Origin::signed(1), 1), "retraction index mismatch"); + assert_noop!(Council::retract_voter(1, 1), "retraction index mismatch"); }); } @@ -821,9 +816,9 @@ mod tests { fn overflow_retraction_index_should_not_work() { with_externalities(&mut new_test_ext(false), || { System::set_block_number(1); - assert_ok!(Council::submit_candidacy(Origin::signed(3), 0)); - assert_ok!(Council::set_approvals(Origin::signed(1), vec![true], 0)); - assert_noop!(Council::retract_voter(Origin::signed(1), 1), "retraction index invalid"); + assert_ok!(Council::submit_candidacy(3, 0)); + assert_ok!(Council::set_approvals(1, vec![true], 0)); + assert_noop!(Council::retract_voter(1, 1), "retraction index invalid"); }); } @@ -831,9 +826,9 @@ mod tests { fn non_voter_retraction_should_not_work() { with_externalities(&mut new_test_ext(false), || { System::set_block_number(1); - assert_ok!(Council::submit_candidacy(Origin::signed(3), 0)); - assert_ok!(Council::set_approvals(Origin::signed(1), vec![true], 0)); - assert_noop!(Council::retract_voter(Origin::signed(2), 0), "cannot retract non-voter"); + assert_ok!(Council::submit_candidacy(3, 0)); + assert_ok!(Council::set_approvals(1, vec![true], 0)); + assert_noop!(Council::retract_voter(2, 0), "cannot retract non-voter"); }); } @@ -843,10 +838,10 @@ mod tests { System::set_block_number(4); assert!(!Council::presentation_active()); - assert_ok!(Council::submit_candidacy(Origin::signed(2), 0)); - assert_ok!(Council::submit_candidacy(Origin::signed(5), 1)); - assert_ok!(Council::set_approvals(Origin::signed(2), vec![true, false], 0)); - assert_ok!(Council::set_approvals(Origin::signed(5), vec![false, true], 0)); + assert_ok!(Council::submit_candidacy(2, 0)); + assert_ok!(Council::submit_candidacy(5, 1)); + assert_ok!(Council::set_approvals(2, vec![true, false], 0)); + assert_ok!(Council::set_approvals(5, vec![false, true], 0)); assert_eq!(Council::voters(), vec![2, 5]); assert_eq!(Council::approvals_of(2), vec![true, false]); assert_eq!(Council::approvals_of(5), vec![false, true]); @@ -854,8 +849,8 @@ mod tests { System::set_block_number(6); assert!(Council::presentation_active()); - assert_eq!(Council::present_winner(Origin::signed(4), 2.into(), 20, 0), Ok(())); - assert_eq!(Council::present_winner(Origin::signed(4), 5.into(), 50, 0), Ok(())); + assert_eq!(Council::present_winner(4, 2.into(), 20, 0), Ok(())); + assert_eq!(Council::present_winner(4, 5.into(), 50, 0), Ok(())); assert_eq!(Council::leaderboard(), Some(vec![(0, 0), (0, 0), (20, 2), (50, 5)])); assert_ok!(Council::end_block(System::block_number())); @@ -877,16 +872,16 @@ mod tests { assert!(Balances::can_slash(&4, 10)); System::set_block_number(4); - assert_ok!(Council::submit_candidacy(Origin::signed(2), 0)); - assert_ok!(Council::submit_candidacy(Origin::signed(5), 1)); - assert_ok!(Council::set_approvals(Origin::signed(2), vec![true, false], 0)); - assert_ok!(Council::set_approvals(Origin::signed(5), vec![false, true], 0)); + assert_ok!(Council::submit_candidacy(2, 0)); + assert_ok!(Council::submit_candidacy(5, 1)); + assert_ok!(Council::set_approvals(2, vec![true, false], 0)); + assert_ok!(Council::set_approvals(5, vec![false, true], 0)); assert_ok!(Council::end_block(System::block_number())); System::set_block_number(6); - assert_ok!(Council::present_winner(Origin::signed(4), 2.into(), 20, 0)); - assert_ok!(Council::present_winner(Origin::signed(4), 5.into(), 50, 0)); - assert_eq!(Council::present_winner(Origin::signed(4), 5.into(), 50, 0), Err("duplicate presentation")); + assert_ok!(Council::present_winner(4, 2.into(), 20, 0)); + assert_ok!(Council::present_winner(4, 5.into(), 50, 0)); + assert_eq!(Council::present_winner(4, 5.into(), 50, 0), Err("duplicate presentation")); assert_ok!(Council::end_block(System::block_number())); assert_eq!(Council::active_council(), vec![(5, 11), (2, 11)]); @@ -898,24 +893,24 @@ mod tests { fn retracting_inactive_voter_should_work() { with_externalities(&mut new_test_ext(false), || { System::set_block_number(4); - assert_ok!(Council::submit_candidacy(Origin::signed(2), 0)); - assert_ok!(Council::set_approvals(Origin::signed(2), vec![true], 0)); + assert_ok!(Council::submit_candidacy(2, 0)); + assert_ok!(Council::set_approvals(2, vec![true], 0)); assert_ok!(Council::end_block(System::block_number())); System::set_block_number(6); - assert_ok!(Council::present_winner(Origin::signed(4), 2.into(), 20, 0)); + assert_ok!(Council::present_winner(4, 2.into(), 20, 0)); assert_ok!(Council::end_block(System::block_number())); System::set_block_number(8); - assert_ok!(Council::submit_candidacy(Origin::signed(5), 0)); - assert_ok!(Council::set_approvals(Origin::signed(5), vec![true], 1)); + assert_ok!(Council::submit_candidacy(5, 0)); + assert_ok!(Council::set_approvals(5, vec![true], 1)); assert_ok!(Council::end_block(System::block_number())); System::set_block_number(10); - assert_ok!(Council::present_winner(Origin::signed(4), 5.into(), 50, 1)); + assert_ok!(Council::present_winner(4, 5.into(), 50, 1)); assert_ok!(Council::end_block(System::block_number())); - assert_ok!(Council::reap_inactive_voter(Origin::signed(5), + assert_ok!(Council::reap_inactive_voter(5, Council::voters().iter().position(|&i| i == 5).unwrap() as u32, 2.into(), Council::voters().iter().position(|&i| i == 2).unwrap() as u32, 2 @@ -932,21 +927,21 @@ mod tests { fn presenting_for_double_election_should_not_work() { with_externalities(&mut new_test_ext(false), || { System::set_block_number(4); - assert_eq!(Council::submit_candidacy(Origin::signed(2), 0), Ok(())); - assert_ok!(Council::set_approvals(Origin::signed(2), vec![true], 0)); + assert_eq!(Council::submit_candidacy(2, 0), Ok(())); + assert_ok!(Council::set_approvals(2, vec![true], 0)); assert_ok!(Council::end_block(System::block_number())); System::set_block_number(6); - assert_ok!(Council::present_winner(Origin::signed(4), 2.into(), 20, 0)); + assert_ok!(Council::present_winner(4, 2.into(), 20, 0)); assert_ok!(Council::end_block(System::block_number())); System::set_block_number(8); - assert_eq!(Council::submit_candidacy(Origin::signed(2), 0), Ok(())); - assert_ok!(Council::set_approvals(Origin::signed(2), vec![true], 1)); + assert_eq!(Council::submit_candidacy(2, 0), Ok(())); + assert_ok!(Council::set_approvals(2, vec![true], 1)); assert_ok!(Council::end_block(System::block_number())); System::set_block_number(10); - assert_noop!(Council::present_winner(Origin::signed(4), 2.into(), 20, 1), "candidate must not form a duplicated member if elected"); + assert_noop!(Council::present_winner(4, 2.into(), 20, 1), "candidate must not form a duplicated member if elected"); }); } @@ -954,27 +949,27 @@ mod tests { fn retracting_inactive_voter_with_other_candidates_in_slots_should_work() { with_externalities(&mut new_test_ext(false), || { System::set_block_number(4); - assert_ok!(Council::submit_candidacy(Origin::signed(2), 0)); - assert_ok!(Council::set_approvals(Origin::signed(2), vec![true], 0)); + assert_ok!(Council::submit_candidacy(2, 0)); + assert_ok!(Council::set_approvals(2, vec![true], 0)); assert_ok!(Council::end_block(System::block_number())); System::set_block_number(6); - assert_ok!(Council::present_winner(Origin::signed(4), 2.into(), 20, 0)); + assert_ok!(Council::present_winner(4, 2.into(), 20, 0)); assert_ok!(Council::end_block(System::block_number())); System::set_block_number(8); - assert_ok!(Council::submit_candidacy(Origin::signed(5), 0)); - assert_ok!(Council::set_approvals(Origin::signed(5), vec![true], 1)); + assert_ok!(Council::submit_candidacy(5, 0)); + assert_ok!(Council::set_approvals(5, vec![true], 1)); assert_ok!(Council::end_block(System::block_number())); System::set_block_number(10); - assert_ok!(Council::present_winner(Origin::signed(4), 5.into(), 50, 1)); + assert_ok!(Council::present_winner(4, 5.into(), 50, 1)); assert_ok!(Council::end_block(System::block_number())); System::set_block_number(11); - assert_ok!(Council::submit_candidacy(Origin::signed(1), 0)); + assert_ok!(Council::submit_candidacy(1, 0)); - assert_ok!(Council::reap_inactive_voter(Origin::signed(5), + assert_ok!(Council::reap_inactive_voter(5, Council::voters().iter().position(|&i| i == 5).unwrap() as u32, 2.into(), Council::voters().iter().position(|&i| i == 2).unwrap() as u32, 2 @@ -991,24 +986,24 @@ mod tests { fn retracting_inactive_voter_with_bad_reporter_index_should_not_work() { with_externalities(&mut new_test_ext(false), || { System::set_block_number(4); - assert_ok!(Council::submit_candidacy(Origin::signed(2), 0)); - assert_ok!(Council::set_approvals(Origin::signed(2), vec![true], 0)); + assert_ok!(Council::submit_candidacy(2, 0)); + assert_ok!(Council::set_approvals(2, vec![true], 0)); assert_ok!(Council::end_block(System::block_number())); System::set_block_number(6); - assert_ok!(Council::present_winner(Origin::signed(4), 2.into(), 20, 0)); + assert_ok!(Council::present_winner(4, 2.into(), 20, 0)); assert_ok!(Council::end_block(System::block_number())); System::set_block_number(8); - assert_ok!(Council::submit_candidacy(Origin::signed(5), 0)); - assert_ok!(Council::set_approvals(Origin::signed(5), vec![true], 1)); + assert_ok!(Council::submit_candidacy(5, 0)); + assert_ok!(Council::set_approvals(5, vec![true], 1)); assert_ok!(Council::end_block(System::block_number())); System::set_block_number(10); - assert_ok!(Council::present_winner(Origin::signed(4), 5.into(), 50, 1)); + assert_ok!(Council::present_winner(4, 5.into(), 50, 1)); assert_ok!(Council::end_block(System::block_number())); - assert_noop!(Council::reap_inactive_voter(Origin::signed(2), + assert_noop!(Council::reap_inactive_voter(2, 42, 2.into(), Council::voters().iter().position(|&i| i == 2).unwrap() as u32, 2 @@ -1020,24 +1015,24 @@ mod tests { fn retracting_inactive_voter_with_bad_target_index_should_not_work() { with_externalities(&mut new_test_ext(false), || { System::set_block_number(4); - assert_ok!(Council::submit_candidacy(Origin::signed(2), 0)); - assert_ok!(Council::set_approvals(Origin::signed(2), vec![true], 0)); + assert_ok!(Council::submit_candidacy(2, 0)); + assert_ok!(Council::set_approvals(2, vec![true], 0)); assert_ok!(Council::end_block(System::block_number())); System::set_block_number(6); - assert_ok!(Council::present_winner(Origin::signed(4), 2.into(), 20, 0)); + assert_ok!(Council::present_winner(4, 2.into(), 20, 0)); assert_ok!(Council::end_block(System::block_number())); System::set_block_number(8); - assert_ok!(Council::submit_candidacy(Origin::signed(5), 0)); - assert_ok!(Council::set_approvals(Origin::signed(5), vec![true], 1)); + assert_ok!(Council::submit_candidacy(5, 0)); + assert_ok!(Council::set_approvals(5, vec![true], 1)); assert_ok!(Council::end_block(System::block_number())); System::set_block_number(10); - assert_ok!(Council::present_winner(Origin::signed(4), 5.into(), 50, 1)); + assert_ok!(Council::present_winner(4, 5.into(), 50, 1)); assert_ok!(Council::end_block(System::block_number())); - assert_noop!(Council::reap_inactive_voter(Origin::signed(2), + assert_noop!(Council::reap_inactive_voter(2, Council::voters().iter().position(|&i| i == 2).unwrap() as u32, 2.into(), 42, 2 @@ -1049,21 +1044,21 @@ mod tests { fn attempting_to_retract_active_voter_should_slash_reporter() { with_externalities(&mut new_test_ext(false), || { System::set_block_number(4); - assert_ok!(Council::submit_candidacy(Origin::signed(2), 0)); - assert_ok!(Council::submit_candidacy(Origin::signed(3), 1)); - assert_ok!(Council::submit_candidacy(Origin::signed(4), 2)); - assert_ok!(Council::submit_candidacy(Origin::signed(5), 3)); - assert_ok!(Council::set_approvals(Origin::signed(2), vec![true, false, false, false], 0)); - assert_ok!(Council::set_approvals(Origin::signed(3), vec![false, true, false, false], 0)); - assert_ok!(Council::set_approvals(Origin::signed(4), vec![false, false, true, false], 0)); - assert_ok!(Council::set_approvals(Origin::signed(5), vec![false, false, false, true], 0)); + assert_ok!(Council::submit_candidacy(2, 0)); + assert_ok!(Council::submit_candidacy(3, 1)); + assert_ok!(Council::submit_candidacy(4, 2)); + assert_ok!(Council::submit_candidacy(5, 3)); + assert_ok!(Council::set_approvals(2, vec![true, false, false, false], 0)); + assert_ok!(Council::set_approvals(3, vec![false, true, false, false], 0)); + assert_ok!(Council::set_approvals(4, vec![false, false, true, false], 0)); + assert_ok!(Council::set_approvals(5, vec![false, false, false, true], 0)); assert_ok!(Council::end_block(System::block_number())); System::set_block_number(6); - assert_ok!(Council::present_winner(Origin::signed(4), 2.into(), 20, 0)); - assert_ok!(Council::present_winner(Origin::signed(4), 3.into(), 30, 0)); - assert_ok!(Council::present_winner(Origin::signed(4), 4.into(), 40, 0)); - assert_ok!(Council::present_winner(Origin::signed(4), 5.into(), 50, 0)); + assert_ok!(Council::present_winner(4, 2.into(), 20, 0)); + assert_ok!(Council::present_winner(4, 3.into(), 30, 0)); + assert_ok!(Council::present_winner(4, 4.into(), 40, 0)); + assert_ok!(Council::present_winner(4, 5.into(), 50, 0)); assert_ok!(Council::end_block(System::block_number())); System::set_block_number(8); @@ -1071,11 +1066,11 @@ mod tests { assert_ok!(Council::end_block(System::block_number())); System::set_block_number(10); - assert_ok!(Council::present_winner(Origin::signed(4), 2.into(), 20, 1)); - assert_ok!(Council::present_winner(Origin::signed(4), 3.into(), 30, 1)); + assert_ok!(Council::present_winner(4, 2.into(), 20, 1)); + assert_ok!(Council::present_winner(4, 3.into(), 30, 1)); assert_ok!(Council::end_block(System::block_number())); - assert_ok!(Council::reap_inactive_voter(Origin::signed(4), + assert_ok!(Council::reap_inactive_voter(4, Council::voters().iter().position(|&i| i == 4).unwrap() as u32, 2.into(), Council::voters().iter().position(|&i| i == 2).unwrap() as u32, 2 @@ -1091,24 +1086,24 @@ mod tests { fn attempting_to_retract_inactive_voter_by_nonvoter_should_not_work() { with_externalities(&mut new_test_ext(false), || { System::set_block_number(4); - assert_ok!(Council::submit_candidacy(Origin::signed(2), 0)); - assert_ok!(Council::set_approvals(Origin::signed(2), vec![true], 0)); + assert_ok!(Council::submit_candidacy(2, 0)); + assert_ok!(Council::set_approvals(2, vec![true], 0)); assert_ok!(Council::end_block(System::block_number())); System::set_block_number(6); - assert_ok!(Council::present_winner(Origin::signed(4), 2.into(), 20, 0)); + assert_ok!(Council::present_winner(4, 2.into(), 20, 0)); assert_ok!(Council::end_block(System::block_number())); System::set_block_number(8); - assert_ok!(Council::submit_candidacy(Origin::signed(5), 0)); - assert_ok!(Council::set_approvals(Origin::signed(5), vec![true], 1)); + assert_ok!(Council::submit_candidacy(5, 0)); + assert_ok!(Council::set_approvals(5, vec![true], 1)); assert_ok!(Council::end_block(System::block_number())); System::set_block_number(10); - assert_ok!(Council::present_winner(Origin::signed(4), 5.into(), 50, 1)); + assert_ok!(Council::present_winner(4, 5.into(), 50, 1)); assert_ok!(Council::end_block(System::block_number())); - assert_noop!(Council::reap_inactive_voter(Origin::signed(4), + assert_noop!(Council::reap_inactive_voter(4, 0, 2.into(), Council::voters().iter().position(|&i| i == 2).unwrap() as u32, 2 @@ -1120,24 +1115,24 @@ mod tests { fn presenting_loser_should_not_work() { with_externalities(&mut new_test_ext(false), || { System::set_block_number(4); - assert_ok!(Council::submit_candidacy(Origin::signed(1), 0)); - assert_ok!(Council::set_approvals(Origin::signed(6), vec![true], 0)); - assert_ok!(Council::submit_candidacy(Origin::signed(2), 1)); - assert_ok!(Council::set_approvals(Origin::signed(2), vec![false, true], 0)); - assert_ok!(Council::submit_candidacy(Origin::signed(3), 2)); - assert_ok!(Council::set_approvals(Origin::signed(3), vec![false, false, true], 0)); - assert_ok!(Council::submit_candidacy(Origin::signed(4), 3)); - assert_ok!(Council::set_approvals(Origin::signed(4), vec![false, false, false, true], 0)); - assert_ok!(Council::submit_candidacy(Origin::signed(5), 4)); - assert_ok!(Council::set_approvals(Origin::signed(5), vec![false, false, false, false, true], 0)); + assert_ok!(Council::submit_candidacy(1, 0)); + assert_ok!(Council::set_approvals(6, vec![true], 0)); + assert_ok!(Council::submit_candidacy(2, 1)); + assert_ok!(Council::set_approvals(2, vec![false, true], 0)); + assert_ok!(Council::submit_candidacy(3, 2)); + assert_ok!(Council::set_approvals(3, vec![false, false, true], 0)); + assert_ok!(Council::submit_candidacy(4, 3)); + assert_ok!(Council::set_approvals(4, vec![false, false, false, true], 0)); + assert_ok!(Council::submit_candidacy(5, 4)); + assert_ok!(Council::set_approvals(5, vec![false, false, false, false, true], 0)); assert_ok!(Council::end_block(System::block_number())); System::set_block_number(6); - assert_ok!(Council::present_winner(Origin::signed(4), 1.into(), 60, 0)); - assert_ok!(Council::present_winner(Origin::signed(4), 3.into(), 30, 0)); - assert_ok!(Council::present_winner(Origin::signed(4), 4.into(), 40, 0)); - assert_ok!(Council::present_winner(Origin::signed(4), 5.into(), 50, 0)); - assert_noop!(Council::present_winner(Origin::signed(4), 2.into(), 20, 0), "candidate not worthy of leaderboard"); + assert_ok!(Council::present_winner(4, 1.into(), 60, 0)); + assert_ok!(Council::present_winner(4, 3.into(), 30, 0)); + assert_ok!(Council::present_winner(4, 4.into(), 40, 0)); + assert_ok!(Council::present_winner(4, 5.into(), 50, 0)); + assert_noop!(Council::present_winner(4, 2.into(), 20, 0), "candidate not worthy of leaderboard"); }); } @@ -1145,24 +1140,24 @@ mod tests { fn presenting_loser_first_should_not_matter() { with_externalities(&mut new_test_ext(false), || { System::set_block_number(4); - assert_ok!(Council::submit_candidacy(Origin::signed(1), 0)); - assert_ok!(Council::set_approvals(Origin::signed(6), vec![true], 0)); - assert_ok!(Council::submit_candidacy(Origin::signed(2), 1)); - assert_ok!(Council::set_approvals(Origin::signed(2), vec![false, true], 0)); - assert_ok!(Council::submit_candidacy(Origin::signed(3), 2)); - assert_ok!(Council::set_approvals(Origin::signed(3), vec![false, false, true], 0)); - assert_ok!(Council::submit_candidacy(Origin::signed(4), 3)); - assert_ok!(Council::set_approvals(Origin::signed(4), vec![false, false, false, true], 0)); - assert_ok!(Council::submit_candidacy(Origin::signed(5), 4)); - assert_ok!(Council::set_approvals(Origin::signed(5), vec![false, false, false, false, true], 0)); + assert_ok!(Council::submit_candidacy(1, 0)); + assert_ok!(Council::set_approvals(6, vec![true], 0)); + assert_ok!(Council::submit_candidacy(2, 1)); + assert_ok!(Council::set_approvals(2, vec![false, true], 0)); + assert_ok!(Council::submit_candidacy(3, 2)); + assert_ok!(Council::set_approvals(3, vec![false, false, true], 0)); + assert_ok!(Council::submit_candidacy(4, 3)); + assert_ok!(Council::set_approvals(4, vec![false, false, false, true], 0)); + assert_ok!(Council::submit_candidacy(5, 4)); + assert_ok!(Council::set_approvals(5, vec![false, false, false, false, true], 0)); assert_ok!(Council::end_block(System::block_number())); System::set_block_number(6); - assert_ok!(Council::present_winner(Origin::signed(4), 2.into(), 20, 0)); - assert_ok!(Council::present_winner(Origin::signed(4), 1.into(), 60, 0)); - assert_ok!(Council::present_winner(Origin::signed(4), 3.into(), 30, 0)); - assert_ok!(Council::present_winner(Origin::signed(4), 4.into(), 40, 0)); - assert_ok!(Council::present_winner(Origin::signed(4), 5.into(), 50, 0)); + assert_ok!(Council::present_winner(4, 2.into(), 20, 0)); + assert_ok!(Council::present_winner(4, 1.into(), 60, 0)); + assert_ok!(Council::present_winner(4, 3.into(), 30, 0)); + assert_ok!(Council::present_winner(4, 4.into(), 40, 0)); + assert_ok!(Council::present_winner(4, 5.into(), 50, 0)); assert_eq!(Council::leaderboard(), Some(vec![ (30, 3), @@ -1178,7 +1173,7 @@ mod tests { with_externalities(&mut new_test_ext(false), || { System::set_block_number(4); assert!(!Council::presentation_active()); - assert_noop!(Council::present_winner(Origin::signed(5), 5.into(), 1, 0), "cannot present outside of presentation period"); + assert_noop!(Council::present_winner(5, 5.into(), 1, 0), "cannot present outside of presentation period"); }); } @@ -1186,14 +1181,14 @@ mod tests { fn present_with_invalid_vote_index_should_not_work() { with_externalities(&mut new_test_ext(false), || { System::set_block_number(4); - assert_ok!(Council::submit_candidacy(Origin::signed(2), 0)); - assert_ok!(Council::submit_candidacy(Origin::signed(5), 1)); - assert_ok!(Council::set_approvals(Origin::signed(2), vec![true, false], 0)); - assert_ok!(Council::set_approvals(Origin::signed(5), vec![false, true], 0)); + assert_ok!(Council::submit_candidacy(2, 0)); + assert_ok!(Council::submit_candidacy(5, 1)); + assert_ok!(Council::set_approvals(2, vec![true, false], 0)); + assert_ok!(Council::set_approvals(5, vec![false, true], 0)); assert_ok!(Council::end_block(System::block_number())); System::set_block_number(6); - assert_noop!(Council::present_winner(Origin::signed(4), 2.into(), 20, 1), "index not current"); + assert_noop!(Council::present_winner(4, 2.into(), 20, 1), "index not current"); }); } @@ -1203,16 +1198,16 @@ mod tests { System::set_block_number(4); assert!(!Council::presentation_active()); - assert_ok!(Council::submit_candidacy(Origin::signed(1), 0)); - assert_ok!(Council::submit_candidacy(Origin::signed(5), 1)); - assert_ok!(Council::set_approvals(Origin::signed(2), vec![true, false], 0)); - assert_ok!(Council::set_approvals(Origin::signed(5), vec![false, true], 0)); + assert_ok!(Council::submit_candidacy(1, 0)); + assert_ok!(Council::submit_candidacy(5, 1)); + assert_ok!(Council::set_approvals(2, vec![true, false], 0)); + assert_ok!(Council::set_approvals(5, vec![false, true], 0)); assert_ok!(Council::end_block(System::block_number())); System::set_block_number(6); assert_eq!(Balances::free_balance(&1), 1); assert_eq!(Balances::reserved_balance(&1), 9); - assert_noop!(Council::present_winner(Origin::signed(1), 1.into(), 20, 0), "presenter must have sufficient slashable funds"); + assert_noop!(Council::present_winner(1, 1.into(), 20, 0), "presenter must have sufficient slashable funds"); }); } @@ -1223,14 +1218,14 @@ mod tests { assert!(!Council::presentation_active()); assert_eq!(Balances::total_balance(&4), 40); - assert_ok!(Council::submit_candidacy(Origin::signed(2), 0)); - assert_ok!(Council::submit_candidacy(Origin::signed(5), 1)); - assert_ok!(Council::set_approvals(Origin::signed(2), vec![true, false], 0)); - assert_ok!(Council::set_approvals(Origin::signed(5), vec![false, true], 0)); + assert_ok!(Council::submit_candidacy(2, 0)); + assert_ok!(Council::submit_candidacy(5, 1)); + assert_ok!(Council::set_approvals(2, vec![true, false], 0)); + assert_ok!(Council::set_approvals(5, vec![false, true], 0)); assert_ok!(Council::end_block(System::block_number())); System::set_block_number(6); - assert_err!(Council::present_winner(Origin::signed(4), 2.into(), 80, 0), "incorrect total"); + assert_err!(Council::present_winner(4, 2.into(), 80, 0), "incorrect total"); assert_eq!(Balances::total_balance(&4), 38); }); @@ -1242,31 +1237,31 @@ mod tests { System::set_block_number(4); assert!(!Council::presentation_active()); - assert_ok!(Council::submit_candidacy(Origin::signed(1), 0)); - assert_ok!(Council::set_approvals(Origin::signed(6), vec![true], 0)); - assert_ok!(Council::submit_candidacy(Origin::signed(2), 1)); - assert_ok!(Council::set_approvals(Origin::signed(2), vec![false, true], 0)); - assert_ok!(Council::submit_candidacy(Origin::signed(3), 2)); - assert_ok!(Council::set_approvals(Origin::signed(3), vec![false, false, true], 0)); - assert_ok!(Council::submit_candidacy(Origin::signed(4), 3)); - assert_ok!(Council::set_approvals(Origin::signed(4), vec![false, false, false, true], 0)); - assert_ok!(Council::submit_candidacy(Origin::signed(5), 4)); - assert_ok!(Council::set_approvals(Origin::signed(5), vec![false, false, false, false, true], 0)); + assert_ok!(Council::submit_candidacy(1, 0)); + assert_ok!(Council::set_approvals(6, vec![true], 0)); + assert_ok!(Council::submit_candidacy(2, 1)); + assert_ok!(Council::set_approvals(2, vec![false, true], 0)); + assert_ok!(Council::submit_candidacy(3, 2)); + assert_ok!(Council::set_approvals(3, vec![false, false, true], 0)); + assert_ok!(Council::submit_candidacy(4, 3)); + assert_ok!(Council::set_approvals(4, vec![false, false, false, true], 0)); + assert_ok!(Council::submit_candidacy(5, 4)); + assert_ok!(Council::set_approvals(5, vec![false, false, false, false, true], 0)); assert_ok!(Council::end_block(System::block_number())); System::set_block_number(6); assert!(Council::presentation_active()); - assert_ok!(Council::present_winner(Origin::signed(4), 1.into(), 60, 0)); + assert_ok!(Council::present_winner(4, 1.into(), 60, 0)); assert_eq!(Council::leaderboard(), Some(vec![ (0, 0), (0, 0), (0, 0), (60, 1) ])); - assert_ok!(Council::present_winner(Origin::signed(4), 3.into(), 30, 0)); - assert_ok!(Council::present_winner(Origin::signed(4), 4.into(), 40, 0)); - assert_ok!(Council::present_winner(Origin::signed(4), 5.into(), 50, 0)); + assert_ok!(Council::present_winner(4, 3.into(), 30, 0)); + assert_ok!(Council::present_winner(4, 4.into(), 40, 0)); + assert_ok!(Council::present_winner(4, 5.into(), 50, 0)); assert_eq!(Council::leaderboard(), Some(vec![ (30, 3), (40, 4), @@ -1299,33 +1294,33 @@ mod tests { fn second_tally_should_use_runners_up() { with_externalities(&mut new_test_ext(false), || { System::set_block_number(4); - assert_ok!(Council::submit_candidacy(Origin::signed(1), 0)); - assert_ok!(Council::set_approvals(Origin::signed(6), vec![true], 0)); - assert_ok!(Council::submit_candidacy(Origin::signed(2), 1)); - assert_ok!(Council::set_approvals(Origin::signed(2), vec![false, true], 0)); - assert_ok!(Council::submit_candidacy(Origin::signed(3), 2)); - assert_ok!(Council::set_approvals(Origin::signed(3), vec![false, false, true], 0)); - assert_ok!(Council::submit_candidacy(Origin::signed(4), 3)); - assert_ok!(Council::set_approvals(Origin::signed(4), vec![false, false, false, true], 0)); - assert_ok!(Council::submit_candidacy(Origin::signed(5), 4)); - assert_ok!(Council::set_approvals(Origin::signed(5), vec![false, false, false, false, true], 0)); + assert_ok!(Council::submit_candidacy(1, 0)); + assert_ok!(Council::set_approvals(6, vec![true], 0)); + assert_ok!(Council::submit_candidacy(2, 1)); + assert_ok!(Council::set_approvals(2, vec![false, true], 0)); + assert_ok!(Council::submit_candidacy(3, 2)); + assert_ok!(Council::set_approvals(3, vec![false, false, true], 0)); + assert_ok!(Council::submit_candidacy(4, 3)); + assert_ok!(Council::set_approvals(4, vec![false, false, false, true], 0)); + assert_ok!(Council::submit_candidacy(5, 4)); + assert_ok!(Council::set_approvals(5, vec![false, false, false, false, true], 0)); assert_ok!(Council::end_block(System::block_number())); System::set_block_number(6); - assert_ok!(Council::present_winner(Origin::signed(4), 1.into(), 60, 0)); - assert_ok!(Council::present_winner(Origin::signed(4), 3.into(), 30, 0)); - assert_ok!(Council::present_winner(Origin::signed(4), 4.into(), 40, 0)); - assert_ok!(Council::present_winner(Origin::signed(4), 5.into(), 50, 0)); + assert_ok!(Council::present_winner(4, 1.into(), 60, 0)); + assert_ok!(Council::present_winner(4, 3.into(), 30, 0)); + assert_ok!(Council::present_winner(4, 4.into(), 40, 0)); + assert_ok!(Council::present_winner(4, 5.into(), 50, 0)); assert_ok!(Council::end_block(System::block_number())); System::set_block_number(8); - assert_ok!(Council::set_approvals(Origin::signed(6), vec![false, false, true, false], 1)); + assert_ok!(Council::set_approvals(6, vec![false, false, true, false], 1)); assert_ok!(Council::set_desired_seats(3)); assert_ok!(Council::end_block(System::block_number())); System::set_block_number(10); - assert_ok!(Council::present_winner(Origin::signed(4), 3.into(), 90, 1)); - assert_ok!(Council::present_winner(Origin::signed(4), 4.into(), 40, 1)); + assert_ok!(Council::present_winner(4, 3.into(), 90, 1)); + assert_ok!(Council::present_winner(4, 4.into(), 40, 1)); assert_ok!(Council::end_block(System::block_number())); assert!(!Council::presentation_active()); From 087a0e92b4f19b6fa690a5a4e203efce5941eae4 Mon Sep 17 00:00:00 2001 From: Guanqun Lu Date: Sat, 15 Sep 2018 23:12:55 +0800 Subject: [PATCH 11/21] update treasury module --- srml/treasury/src/lib.rs | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/srml/treasury/src/lib.rs b/srml/treasury/src/lib.rs index 6026917a26eb4..a2a2270e8b69a 100644 --- a/srml/treasury/src/lib.rs +++ b/srml/treasury/src/lib.rs @@ -46,7 +46,6 @@ use runtime_support::{StorageValue, StorageMap}; use runtime_support::dispatch::Result; use runtime_primitives::{Permill, traits::{OnFinalise, Zero, EnsureOrigin}}; use balances::OnDilution; -use system::ensure_signed; /// Our module's configuration trait. All our types and consts go in here. If the /// module is dependent on specific other modules, then their configuration traits @@ -74,7 +73,7 @@ decl_module! { // Put forward a suggestion for spending. A deposit proportional to the value // is reserved and slashed if the proposal is rejected. It is returned once the // proposal is awarded. - fn propose_spend(origin, value: T::Balance, beneficiary: T::AccountId) -> Result; + fn propose_spend(SystemOrigin(Signed(proposer)), value: T::Balance, beneficiary: T::AccountId) -> Result; // Set the balance of funds available to spend. fn set_pot(new_pot: T::Balance) -> Result; @@ -158,8 +157,7 @@ impl Module { // Implement Calls and add public immutables and private mutables. - fn propose_spend(origin: T::Origin, value: T::Balance, beneficiary: T::AccountId) -> Result { - let proposer = ensure_signed(origin)?; + fn propose_spend(proposer: T::AccountId, value: T::Balance, beneficiary: T::AccountId) -> Result { let bond = Self::calculate_bond(value); >::reserve(&proposer, bond) @@ -412,7 +410,7 @@ mod tests { #[test] fn spend_proposal_takes_min_deposit() { with_externalities(&mut new_test_ext(), || { - assert_ok!(Treasury::propose_spend(Origin::signed(0), 1, 3)); + assert_ok!(Treasury::propose_spend(0, 1, 3)); assert_eq!(Balances::free_balance(&0), 99); assert_eq!(Balances::reserved_balance(&0), 1); }); @@ -421,7 +419,7 @@ mod tests { #[test] fn spend_proposal_takes_proportional_deposit() { with_externalities(&mut new_test_ext(), || { - assert_ok!(Treasury::propose_spend(Origin::signed(0), 100, 3)); + assert_ok!(Treasury::propose_spend(0, 100, 3)); assert_eq!(Balances::free_balance(&0), 95); assert_eq!(Balances::reserved_balance(&0), 5); }); @@ -430,7 +428,7 @@ mod tests { #[test] fn spend_proposal_fails_when_proposer_poor() { with_externalities(&mut new_test_ext(), || { - assert_noop!(Treasury::propose_spend(Origin::signed(2), 100, 3), "Proposer's balance too low"); + assert_noop!(Treasury::propose_spend(2, 100, 3), "Proposer's balance too low"); }); } @@ -439,7 +437,7 @@ mod tests { with_externalities(&mut new_test_ext(), || { Treasury::on_dilution(100, 100); - assert_ok!(Treasury::propose_spend(Origin::signed(0), 100, 3)); + assert_ok!(Treasury::propose_spend(0, 100, 3)); assert_ok!(Treasury::approve_proposal(Origin::ROOT, 0)); >::on_finalise(1); @@ -463,7 +461,7 @@ mod tests { with_externalities(&mut new_test_ext(), || { Treasury::on_dilution(100, 100); - assert_ok!(Treasury::propose_spend(Origin::signed(0), 100, 3)); + assert_ok!(Treasury::propose_spend(0, 100, 3)); assert_ok!(Treasury::reject_proposal(Origin::ROOT, 0)); >::on_finalise(2); @@ -477,7 +475,7 @@ mod tests { with_externalities(&mut new_test_ext(), || { Treasury::on_dilution(100, 100); - assert_ok!(Treasury::propose_spend(Origin::signed(0), 100, 3)); + assert_ok!(Treasury::propose_spend(0, 100, 3)); assert_ok!(Treasury::reject_proposal(Origin::ROOT, 0)); assert_noop!(Treasury::reject_proposal(Origin::ROOT, 0), "No proposal at that index"); }); @@ -502,7 +500,7 @@ mod tests { with_externalities(&mut new_test_ext(), || { Treasury::on_dilution(100, 100); - assert_ok!(Treasury::propose_spend(Origin::signed(0), 100, 3)); + assert_ok!(Treasury::propose_spend(0, 100, 3)); assert_ok!(Treasury::reject_proposal(Origin::ROOT, 0)); assert_noop!(Treasury::approve_proposal(Origin::ROOT, 0), "No proposal at that index"); }); @@ -513,7 +511,7 @@ mod tests { with_externalities(&mut new_test_ext(), || { Treasury::on_dilution(100, 100); - assert_ok!(Treasury::propose_spend(Origin::signed(0), 100, 3)); + assert_ok!(Treasury::propose_spend(0, 100, 3)); assert_ok!(Treasury::approve_proposal(Origin::ROOT, 0)); >::on_finalise(2); @@ -527,7 +525,7 @@ mod tests { with_externalities(&mut new_test_ext(), || { Treasury::on_dilution(100, 100); - assert_ok!(Treasury::propose_spend(Origin::signed(0), 150, 3)); + assert_ok!(Treasury::propose_spend(0, 150, 3)); assert_ok!(Treasury::approve_proposal(Origin::ROOT, 0)); >::on_finalise(2); From bed938e4db72f21d4afa5c3550c6d9fdb9b880db Mon Sep 17 00:00:00 2001 From: Guanqun Lu Date: Sat, 15 Sep 2018 23:15:36 +0800 Subject: [PATCH 12/21] update example module --- srml/example/src/lib.rs | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/srml/example/src/lib.rs b/srml/example/src/lib.rs index a32f599b8f19f..1699fc5aca491 100644 --- a/srml/example/src/lib.rs +++ b/srml/example/src/lib.rs @@ -60,7 +60,6 @@ extern crate srml_balances as balances; use runtime_primitives::traits::OnFinalise; use runtime_support::{StorageValue, dispatch::Result}; -use system::ensure_signed; /// Our module's configuration trait. All our types and consts go in here. If the /// module is dependent on specific other modules, then their configuration traits @@ -108,9 +107,9 @@ decl_module! { /// This is your public interface. Be extremely careful. /// This is just a simple example of how to interact with the module from the external /// world. - fn accumulate_dummy(origin, increase_by: T::Balance) -> Result; + fn accumulate_dummy(SystemOrigin(Signed(sender)), increase_by: T::Balance) -> Result; - fn accumulate_foo(origin, increase_by: T::Balance) -> Result; + fn accumulate_foo(SystemOrigin(Signed(sender)), increase_by: T::Balance) -> Result; /// A privileged call; in this case it resets our dummy value to something new. fn set_dummy(new_dummy: T::Balance) -> Result; @@ -217,9 +216,7 @@ impl Module { // no progress. // // If you don't respect these rules, it is likely that your chain will be attackable. - fn accumulate_dummy(origin: T::Origin, increase_by: T::Balance) -> Result { - // This is a public call, so we ensure that the origin is some signed account. - let _sender = ensure_signed(origin)?; + fn accumulate_dummy(_sender: T::AccountId, increase_by: T::Balance) -> Result { // Read the value of dummy from storage. // let dummy = Self::dummy(); @@ -247,8 +244,7 @@ impl Module { Ok(()) } - fn accumulate_foo(origin: T::Origin, increase_by: T::Balance) -> Result { - let _sender = ensure_signed(origin)?; + fn accumulate_foo(_sender: T::AccountId, increase_by: T::Balance) -> Result { // Because Foo has 'default', the type of 'foo' in closure is the raw type instead of an Option<> type. >::mutate(|foo| *foo = *foo + increase_by); @@ -386,7 +382,7 @@ mod tests { assert_eq!(Example::dummy(), Some(42)); // Check that accumulate works when we have Some value in Dummy already. - assert_ok!(Example::accumulate_dummy(Origin::signed(1), 27)); + assert_ok!(Example::accumulate_dummy(1, 27)); assert_eq!(Example::dummy(), Some(69)); // Check that finalising the block removes Dummy from storage. @@ -394,7 +390,7 @@ mod tests { assert_eq!(Example::dummy(), None); // Check that accumulate works when we Dummy has None in it. - assert_ok!(Example::accumulate_dummy(Origin::signed(1), 42)); + assert_ok!(Example::accumulate_dummy(1, 42)); assert_eq!(Example::dummy(), Some(42)); }); } @@ -403,7 +399,7 @@ mod tests { fn it_works_for_default_value() { with_externalities(&mut new_test_ext(), || { assert_eq!(Example::foo(), 24); - assert_ok!(Example::accumulate_foo(Origin::signed(1), 1)); + assert_ok!(Example::accumulate_foo(1, 1)); assert_eq!(Example::foo(), 25); }); } From f8793315d3569c6b57796160227293c138a5cdd4 Mon Sep 17 00:00:00 2001 From: Guanqun Lu Date: Sat, 15 Sep 2018 23:22:27 +0800 Subject: [PATCH 13/21] update staking module --- srml/staking/src/lib.rs | 26 ++++++--------- srml/staking/src/tests.rs | 68 +++++++++++++++++++-------------------- 2 files changed, 44 insertions(+), 50 deletions(-) diff --git a/srml/staking/src/lib.rs b/srml/staking/src/lib.rs index cd5abdc6af49f..452aa33d3eb4f 100644 --- a/srml/staking/src/lib.rs +++ b/srml/staking/src/lib.rs @@ -54,7 +54,6 @@ use session::OnSessionChange; use primitives::traits::{Zero, One, Bounded, OnFinalise, As, Lookup}; use balances::{address::Address, OnDilution}; -use system::ensure_signed; mod mock; @@ -104,11 +103,11 @@ pub trait Trait: balances::Trait + session::Trait { decl_module! { #[cfg_attr(feature = "std", serde(bound(deserialize = "T::Balance: ::serde::de::DeserializeOwned")))] pub struct Module for enum Call where origin: T::Origin { - fn stake(origin) -> Result; - fn unstake(origin, intentions_index: u32) -> Result; - fn nominate(origin, target: Address) -> Result; - fn unnominate(origin, target_index: u32) -> Result; - fn register_preferences(origin, intentions_index: u32, prefs: ValidatorPrefs) -> Result; + fn stake(SystemOrigin(Signed(who))) -> Result; + fn unstake(SystemOrigin(Signed(who)), intentions_index: u32) -> Result; + fn nominate(SystemOrigin(Signed(who)), target: Address) -> Result; + fn unnominate(SystemOrigin(Signed(source)), target_index: u32) -> Result; + fn register_preferences(SystemOrigin(Signed(who)), intentions_index: u32, prefs: ValidatorPrefs) -> Result; fn set_sessions_per_era(new: T::BlockNumber) -> Result; fn set_bonding_duration(new: T::BlockNumber) -> Result; @@ -228,8 +227,7 @@ impl Module { /// Declare the desire to stake for the transactor. /// /// Effects will be felt at the beginning of the next era. - fn stake(origin: T::Origin) -> Result { - let who = ensure_signed(origin)?; + fn stake(who: T::AccountId) -> Result { ensure!(Self::nominating(&who).is_none(), "Cannot stake if already nominating."); let mut intentions = >::get(); // can't be in the list twice. @@ -244,8 +242,7 @@ impl Module { /// Retract the desire to stake for the transactor. /// /// Effects will be felt at the beginning of the next era. - fn unstake(origin: T::Origin, intentions_index: u32) -> Result { - let who = ensure_signed(origin)?; + fn unstake(who: T::AccountId, intentions_index: u32) -> Result { // unstake fails in degenerate case of having too few existing staked parties if Self::intentions().len() <= Self::minimum_validator_count() { return Err("cannot unstake when there are too few staked participants") @@ -253,8 +250,7 @@ impl Module { Self::apply_unstake(&who, intentions_index as usize) } - fn nominate(origin: T::Origin, target: Address) -> Result { - let who = ensure_signed(origin)?; + fn nominate(who: T::AccountId, target: Address) -> Result { let target = >::lookup(target)?; ensure!(Self::nominating(&who).is_none(), "Cannot nominate if already nominating."); @@ -276,8 +272,7 @@ impl Module { /// Will panic if called when source isn't currently nominating target. /// Updates Nominating, NominatorsFor and NominationBalance. - fn unnominate(origin: T::Origin, target_index: u32) -> Result { - let source = ensure_signed(origin)?; + fn unnominate(source: T::AccountId, target_index: u32) -> Result { let target_index = target_index as usize; let target = >::get(&source).ok_or("Account must be nominating")?; @@ -305,11 +300,10 @@ impl Module { /// /// An error (no-op) if `Self::intentions()[intentions_index] != origin`. fn register_preferences( - origin: T::Origin, + who: T::AccountId, intentions_index: u32, prefs: ValidatorPrefs ) -> Result { - let who = ensure_signed(origin)?; if Self::intentions().get(intentions_index as usize) != Some(&who) { return Err("Invalid index") diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index 0834598a6110a..8ab8928dd6f2b 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -21,7 +21,7 @@ use super::*; use consensus::OnOfflineValidator; use runtime_io::with_externalities; -use mock::{Balances, Session, Staking, System, Timestamp, Test, new_test_ext, Origin}; +use mock::{Balances, Session, Staking, System, Timestamp, Test, new_test_ext}; #[test] fn note_null_offline_should_work() { @@ -104,7 +104,7 @@ fn note_offline_force_unstake_session_change_should_work() { with_externalities(&mut new_test_ext(0, 3, 3, 0, true, 10), || { Balances::set_free_balance(&10, 70); Balances::set_free_balance(&20, 70); - assert_ok!(Staking::stake(Origin::signed(1))); + assert_ok!(Staking::stake(1)); assert_eq!(Staking::slash_count(&10), 0); assert_eq!(Balances::free_balance(&10), 70); @@ -130,7 +130,7 @@ fn note_offline_auto_unstake_session_change_should_work() { with_externalities(&mut new_test_ext(0, 3, 3, 0, true, 10), || { Balances::set_free_balance(&10, 7000); Balances::set_free_balance(&20, 7000); - assert_ok!(Staking::register_preferences(Origin::signed(10), 0, ValidatorPrefs { unstake_threshold: 1, validator_payment: 0 })); + assert_ok!(Staking::register_preferences(10, 0, ValidatorPrefs { unstake_threshold: 1, validator_payment: 0 })); assert_eq!(Staking::intentions(), vec![10, 20]); @@ -241,9 +241,9 @@ fn staking_should_work() { // Block 1: Add three validators. No obvious change. System::set_block_number(1); - assert_ok!(Staking::stake(Origin::signed(1))); - assert_ok!(Staking::stake(Origin::signed(2))); - assert_ok!(Staking::stake(Origin::signed(4))); + assert_ok!(Staking::stake(1)); + assert_ok!(Staking::stake(2)); + assert_ok!(Staking::stake(4)); Session::check_rotate_session(System::block_number()); assert_eq!(Staking::current_era(), 0); assert_eq!(Session::validators(), vec![10, 20]); @@ -256,8 +256,8 @@ fn staking_should_work() { // Block 3: Unstake highest, introduce another staker. No change yet. System::set_block_number(3); - assert_ok!(Staking::stake(Origin::signed(3))); - assert_ok!(Staking::unstake(Origin::signed(4), Staking::intentions().iter().position(|&x| x == 4).unwrap() as u32)); + assert_ok!(Staking::stake(3)); + assert_ok!(Staking::unstake(4, Staking::intentions().iter().position(|&x| x == 4).unwrap() as u32)); assert_eq!(Staking::current_era(), 1); Session::check_rotate_session(System::block_number()); @@ -269,7 +269,7 @@ fn staking_should_work() { // Block 5: Transfer stake from highest to lowest. No change yet. System::set_block_number(5); - assert_ok!(Balances::transfer(Origin::signed(4), 1.into(), 40)); + assert_ok!(Balances::transfer(4, 1.into(), 40)); Session::check_rotate_session(System::block_number()); // Block 6: Lowest now validator. @@ -279,7 +279,7 @@ fn staking_should_work() { // Block 7: Unstake three. No change yet. System::set_block_number(7); - assert_ok!(Staking::unstake(Origin::signed(3), Staking::intentions().iter().position(|&x| x == 3).unwrap() as u32)); + assert_ok!(Staking::unstake(3, Staking::intentions().iter().position(|&x| x == 3).unwrap() as u32)); Session::check_rotate_session(System::block_number()); assert_eq!(Session::validators(), vec![1, 3]); @@ -299,10 +299,10 @@ fn nominating_and_rewards_should_work() { assert_eq!(Session::validators(), vec![10, 20]); System::set_block_number(1); - assert_ok!(Staking::stake(Origin::signed(1))); - assert_ok!(Staking::stake(Origin::signed(2))); - assert_ok!(Staking::stake(Origin::signed(3))); - assert_ok!(Staking::nominate(Origin::signed(4), 1.into())); + assert_ok!(Staking::stake(1)); + assert_ok!(Staking::stake(2)); + assert_ok!(Staking::stake(3)); + assert_ok!(Staking::nominate(4, 1.into())); Session::check_rotate_session(System::block_number()); assert_eq!(Staking::current_era(), 1); assert_eq!(Session::validators(), vec![1, 3]); // 4 + 1, 3 @@ -312,7 +312,7 @@ fn nominating_and_rewards_should_work() { assert_eq!(Balances::total_balance(&4), 40); System::set_block_number(2); - assert_ok!(Staking::unnominate(Origin::signed(4), 0)); + assert_ok!(Staking::unnominate(4, 0)); Session::check_rotate_session(System::block_number()); assert_eq!(Staking::current_era(), 2); assert_eq!(Session::validators(), vec![3, 2]); @@ -322,9 +322,9 @@ fn nominating_and_rewards_should_work() { assert_eq!(Balances::total_balance(&4), 48); System::set_block_number(3); - assert_ok!(Staking::stake(Origin::signed(4))); - assert_ok!(Staking::unstake(Origin::signed(3), Staking::intentions().iter().position(|&x| x == 3).unwrap() as u32)); - assert_ok!(Staking::nominate(Origin::signed(3), 1.into())); + assert_ok!(Staking::stake(4)); + assert_ok!(Staking::unstake(3, Staking::intentions().iter().position(|&x| x == 3).unwrap() as u32)); + assert_ok!(Staking::nominate(3, 1.into())); Session::check_rotate_session(System::block_number()); assert_eq!(Session::validators(), vec![1, 4]); assert_eq!(Balances::total_balance(&1), 12); @@ -345,9 +345,9 @@ fn nominating_and_rewards_should_work() { fn rewards_with_off_the_table_should_work() { with_externalities(&mut new_test_ext(0, 1, 1, 0, true, 10), || { System::set_block_number(1); - assert_ok!(Staking::stake(Origin::signed(1))); - assert_ok!(Staking::nominate(Origin::signed(2), 1.into())); - assert_ok!(Staking::stake(Origin::signed(3))); + assert_ok!(Staking::stake(1)); + assert_ok!(Staking::nominate(2, 1.into())); + assert_ok!(Staking::stake(3)); Session::check_rotate_session(System::block_number()); assert_eq!(Session::validators(), vec![1, 3]); // 1 + 2, 3 assert_eq!(Balances::total_balance(&1), 10); @@ -355,7 +355,7 @@ fn rewards_with_off_the_table_should_work() { assert_eq!(Balances::total_balance(&3), 30); System::set_block_number(2); - assert_ok!(Staking::register_preferences(Origin::signed(1), Staking::intentions().into_iter().position(|i| i == 1).unwrap() as u32, ValidatorPrefs { unstake_threshold: 3, validator_payment: 4 })); + assert_ok!(Staking::register_preferences(1, Staking::intentions().into_iter().position(|i| i == 1).unwrap() as u32, ValidatorPrefs { unstake_threshold: 3, validator_payment: 4 })); Session::check_rotate_session(System::block_number()); assert_eq!(Balances::total_balance(&1), 16); assert_eq!(Balances::total_balance(&2), 24); @@ -376,10 +376,10 @@ fn nominating_slashes_should_work() { Timestamp::set_timestamp(15); System::set_block_number(4); - assert_ok!(Staking::stake(Origin::signed(1))); - assert_ok!(Staking::stake(Origin::signed(3))); - assert_ok!(Staking::nominate(Origin::signed(2), 3.into())); - assert_ok!(Staking::nominate(Origin::signed(4), 1.into())); + assert_ok!(Staking::stake(1)); + assert_ok!(Staking::stake(3)); + assert_ok!(Staking::nominate(2, 3.into())); + assert_ok!(Staking::nominate(4, 1.into())); Session::check_rotate_session(System::block_number()); assert_eq!(Staking::current_era(), 1); @@ -404,12 +404,12 @@ fn nominating_slashes_should_work() { fn double_staking_should_fail() { with_externalities(&mut new_test_ext(0, 1, 2, 0, true, 0), || { System::set_block_number(1); - assert_ok!(Staking::stake(Origin::signed(1))); - assert_noop!(Staking::stake(Origin::signed(1)), "Cannot stake if already staked."); - assert_noop!(Staking::nominate(Origin::signed(1), 1.into()), "Cannot nominate if already staked."); - assert_ok!(Staking::nominate(Origin::signed(2), 1.into())); - assert_noop!(Staking::stake(Origin::signed(2)), "Cannot stake if already nominating."); - assert_noop!(Staking::nominate(Origin::signed(2), 1.into()), "Cannot nominate if already nominating."); + assert_ok!(Staking::stake(1)); + assert_noop!(Staking::stake(1), "Cannot stake if already staked."); + assert_noop!(Staking::nominate(1, 1.into()), "Cannot nominate if already staked."); + assert_ok!(Staking::nominate(2, 1.into())); + assert_noop!(Staking::stake(2), "Cannot stake if already nominating."); + assert_noop!(Staking::nominate(2, 1.into()), "Cannot nominate if already nominating."); }); } @@ -485,8 +485,8 @@ fn staking_eras_work() { fn staking_balance_transfer_when_bonded_should_not_work() { with_externalities(&mut new_test_ext(0, 1, 3, 1, false, 0), || { Balances::set_free_balance(&1, 111); - assert_ok!(Staking::stake(Origin::signed(1))); - assert_noop!(Balances::transfer(Origin::signed(1), 2.into(), 69), "cannot transfer illiquid funds"); + assert_ok!(Staking::stake(1)); + assert_noop!(Balances::transfer(1, 2.into(), 69), "cannot transfer illiquid funds"); }); } From aeb9c9686208b0c5d4f2f4f7b0343b5df82b6d9d Mon Sep 17 00:00:00 2001 From: Guanqun Lu Date: Sat, 15 Sep 2018 23:25:18 +0800 Subject: [PATCH 14/21] update council voting module --- srml/council/src/voting.rs | 92 ++++++++++++++++++-------------------- 1 file changed, 44 insertions(+), 48 deletions(-) diff --git a/srml/council/src/voting.rs b/srml/council/src/voting.rs index fa03bc5e36f7f..3d21b87884404 100644 --- a/srml/council/src/voting.rs +++ b/srml/council/src/voting.rs @@ -24,7 +24,6 @@ use srml_support::dispatch::Result; use srml_support::{StorageValue, StorageMap, IsSubType}; use {system, democracy}; use super::{Trait as CouncilTrait, Module as Council}; -use system::ensure_signed; pub trait Trait: CouncilTrait { type Event: From> + Into<::Event>; @@ -32,9 +31,9 @@ pub trait Trait: CouncilTrait { decl_module! { pub struct Module for enum Call where origin: T::Origin { - fn propose(origin, proposal: Box) -> Result; - fn vote(origin, proposal: T::Hash, approve: bool) -> Result; - fn veto(origin, proposal_hash: T::Hash) -> Result; + fn propose(SystemOrigin(Signed(who)), proposal: Box) -> Result; + fn vote(SystemOrigin(Signed(who)), proposal: T::Hash, approve: bool) -> Result; + fn veto(SystemOrigin(Signed(who)), proposal_hash: T::Hash) -> Result; fn set_cooloff_period(blocks: T::BlockNumber) -> Result; fn set_voting_period(blocks: T::BlockNumber) -> Result; @@ -95,8 +94,7 @@ impl Module { } // Dispatch - fn propose(origin: T::Origin, proposal: Box) -> Result { - let who = ensure_signed(origin)?; + fn propose(who: T::AccountId, proposal: Box) -> Result { let expiry = >::block_number() + Self::voting_period(); ensure!(Self::will_still_be_councillor_at(&who, expiry), "proposer would not be on council"); @@ -118,8 +116,7 @@ impl Module { Ok(()) } - fn vote(origin: T::Origin, proposal: T::Hash, approve: bool) -> Result { - let who = ensure_signed(origin)?; + fn vote(who: T::AccountId, proposal: T::Hash, approve: bool) -> Result { ensure!(Self::is_councillor(&who), "only councillors may vote on council proposals"); @@ -130,8 +127,7 @@ impl Module { Ok(()) } - fn veto(origin: T::Origin, proposal_hash: T::Hash) -> Result { - let who = ensure_signed(origin)?; + fn veto(who: T::AccountId, proposal_hash: T::Hash) -> Result { ensure!(Self::is_councillor(&who), "only councillors may veto council proposals"); ensure!(>::exists(&proposal_hash), "proposal must exist to be vetoed"); @@ -239,7 +235,7 @@ impl OnFinalise for Module { mod tests { use super::*; use ::tests::*; - use ::tests::{Call, Origin}; + use ::tests::Call; use srml_support::Hashable; use democracy::VoteThreshold; @@ -281,9 +277,9 @@ mod tests { let cancellation = cancel_referendum_proposal(0); let hash = cancellation.blake2_256().into(); - assert_ok!(CouncilVoting::propose(Origin::signed(1), Box::new(cancellation))); - assert_ok!(CouncilVoting::vote(Origin::signed(2), hash, true)); - assert_ok!(CouncilVoting::vote(Origin::signed(3), hash, true)); + assert_ok!(CouncilVoting::propose(1, Box::new(cancellation))); + assert_ok!(CouncilVoting::vote(2, hash, true)); + assert_ok!(CouncilVoting::vote(3, hash, true)); assert_eq!(CouncilVoting::proposals(), vec![(2, hash)]); assert_ok!(CouncilVoting::end_block(System::block_number())); @@ -303,9 +299,9 @@ mod tests { let cancellation = cancel_referendum_proposal(0); let hash = cancellation.blake2_256().into(); - assert_ok!(CouncilVoting::propose(Origin::signed(1), Box::new(cancellation))); - assert_ok!(CouncilVoting::vote(Origin::signed(2), hash, true)); - assert_ok!(CouncilVoting::vote(Origin::signed(3), hash, false)); + assert_ok!(CouncilVoting::propose(1, Box::new(cancellation))); + assert_ok!(CouncilVoting::vote(2, hash, true)); + assert_ok!(CouncilVoting::vote(3, hash, false)); assert_ok!(CouncilVoting::end_block(System::block_number())); System::set_block_number(2); @@ -323,8 +319,8 @@ mod tests { let cancellation = cancel_referendum_proposal(0); let hash = cancellation.blake2_256().into(); - assert_ok!(CouncilVoting::propose(Origin::signed(1), Box::new(cancellation))); - assert_ok!(CouncilVoting::vote(Origin::signed(2), hash, true)); + assert_ok!(CouncilVoting::propose(1, Box::new(cancellation))); + assert_ok!(CouncilVoting::vote(2, hash, true)); assert_ok!(CouncilVoting::end_block(System::block_number())); System::set_block_number(2); @@ -339,8 +335,8 @@ mod tests { System::set_block_number(1); let proposal = set_balance_proposal(42); let hash = proposal.blake2_256().into(); - assert_ok!(CouncilVoting::propose(Origin::signed(1), Box::new(proposal.clone()))); - assert_ok!(CouncilVoting::veto(Origin::signed(2), hash)); + assert_ok!(CouncilVoting::propose(1, Box::new(proposal.clone()))); + assert_ok!(CouncilVoting::veto(2, hash)); assert_eq!(CouncilVoting::proposals().len(), 0); assert_eq!(Democracy::active_referendums().len(), 0); }); @@ -352,12 +348,12 @@ mod tests { System::set_block_number(1); let proposal = set_balance_proposal(42); let hash = proposal.blake2_256().into(); - assert_ok!(CouncilVoting::propose(Origin::signed(1), Box::new(proposal.clone()))); - assert_ok!(CouncilVoting::veto(Origin::signed(2), hash)); + assert_ok!(CouncilVoting::propose(1, Box::new(proposal.clone()))); + assert_ok!(CouncilVoting::veto(2, hash)); System::set_block_number(3); - assert_ok!(CouncilVoting::propose(Origin::signed(1), Box::new(proposal.clone()))); - assert_noop!(CouncilVoting::veto(Origin::signed(2), hash), "a councillor may not veto a proposal twice"); + assert_ok!(CouncilVoting::propose(1, Box::new(proposal.clone()))); + assert_noop!(CouncilVoting::veto(2, hash), "a councillor may not veto a proposal twice"); }); } @@ -367,11 +363,11 @@ mod tests { System::set_block_number(1); let proposal = set_balance_proposal(42); let hash = proposal.blake2_256().into(); - assert_ok!(CouncilVoting::propose(Origin::signed(1), Box::new(proposal.clone()))); - assert_ok!(CouncilVoting::veto(Origin::signed(2), hash)); + assert_ok!(CouncilVoting::propose(1, Box::new(proposal.clone()))); + assert_ok!(CouncilVoting::veto(2, hash)); System::set_block_number(2); - assert_noop!(CouncilVoting::propose(Origin::signed(1), Box::new(proposal.clone())), "proposal is vetoed"); + assert_noop!(CouncilVoting::propose(1, Box::new(proposal.clone())), "proposal is vetoed"); }); } @@ -381,13 +377,13 @@ mod tests { System::set_block_number(1); let proposal = set_balance_proposal(42); let hash = proposal.blake2_256().into(); - assert_ok!(CouncilVoting::propose(Origin::signed(1), Box::new(proposal.clone()))); - assert_ok!(CouncilVoting::veto(Origin::signed(2), hash)); + assert_ok!(CouncilVoting::propose(1, Box::new(proposal.clone()))); + assert_ok!(CouncilVoting::veto(2, hash)); System::set_block_number(3); - assert_ok!(CouncilVoting::propose(Origin::signed(1), Box::new(proposal.clone()))); - assert_ok!(CouncilVoting::vote(Origin::signed(2), hash, false)); - assert_ok!(CouncilVoting::vote(Origin::signed(3), hash, true)); + assert_ok!(CouncilVoting::propose(1, Box::new(proposal.clone()))); + assert_ok!(CouncilVoting::vote(2, hash, false)); + assert_ok!(CouncilVoting::vote(3, hash, true)); assert_ok!(CouncilVoting::end_block(System::block_number())); System::set_block_number(4); @@ -403,12 +399,12 @@ mod tests { System::set_block_number(1); let proposal = set_balance_proposal(42); let hash = proposal.blake2_256().into(); - assert_ok!(CouncilVoting::propose(Origin::signed(1), Box::new(proposal.clone()))); - assert_ok!(CouncilVoting::veto(Origin::signed(2), hash)); + assert_ok!(CouncilVoting::propose(1, Box::new(proposal.clone()))); + assert_ok!(CouncilVoting::veto(2, hash)); System::set_block_number(3); - assert_ok!(CouncilVoting::propose(Origin::signed(1), Box::new(proposal.clone()))); - assert_ok!(CouncilVoting::veto(Origin::signed(3), hash)); + assert_ok!(CouncilVoting::propose(1, Box::new(proposal.clone()))); + assert_ok!(CouncilVoting::veto(3, hash)); assert_eq!(CouncilVoting::proposals().len(), 0); assert_eq!(Democracy::active_referendums().len(), 0); }); @@ -420,7 +416,7 @@ mod tests { System::set_block_number(1); let proposal = set_balance_proposal(42); let hash = proposal.blake2_256().into(); - assert_ok!(CouncilVoting::propose(Origin::signed(1), Box::new(proposal.clone()))); + assert_ok!(CouncilVoting::propose(1, Box::new(proposal.clone()))); assert_eq!(CouncilVoting::proposals().len(), 1); assert_eq!(CouncilVoting::proposal_voters(&hash), vec![1]); assert_eq!(CouncilVoting::vote_of((hash, 1)), Some(true)); @@ -433,7 +429,7 @@ mod tests { with_externalities(&mut new_test_ext(true), || { System::set_block_number(1); let proposal = set_balance_proposal(42); - assert_ok!(CouncilVoting::propose(Origin::signed(1), Box::new(proposal.clone()))); + assert_ok!(CouncilVoting::propose(1, Box::new(proposal.clone()))); assert_eq!(CouncilVoting::tally(&proposal.blake2_256().into()), (1, 0, 2)); assert_ok!(CouncilVoting::end_block(System::block_number())); @@ -449,9 +445,9 @@ mod tests { with_externalities(&mut new_test_ext(true), || { System::set_block_number(1); let proposal = set_balance_proposal(42); - assert_ok!(CouncilVoting::propose(Origin::signed(1), Box::new(proposal.clone()))); - assert_ok!(CouncilVoting::vote(Origin::signed(2), proposal.blake2_256().into(), true)); - assert_ok!(CouncilVoting::vote(Origin::signed(3), proposal.blake2_256().into(), true)); + assert_ok!(CouncilVoting::propose(1, Box::new(proposal.clone()))); + assert_ok!(CouncilVoting::vote(2, proposal.blake2_256().into(), true)); + assert_ok!(CouncilVoting::vote(3, proposal.blake2_256().into(), true)); assert_eq!(CouncilVoting::tally(&proposal.blake2_256().into()), (3, 0, 0)); assert_ok!(CouncilVoting::end_block(System::block_number())); @@ -467,9 +463,9 @@ mod tests { with_externalities(&mut new_test_ext(true), || { System::set_block_number(1); let proposal = set_balance_proposal(42); - assert_ok!(CouncilVoting::propose(Origin::signed(1), Box::new(proposal.clone()))); - assert_ok!(CouncilVoting::vote(Origin::signed(2), proposal.blake2_256().into(), true)); - assert_ok!(CouncilVoting::vote(Origin::signed(3), proposal.blake2_256().into(), false)); + assert_ok!(CouncilVoting::propose(1, Box::new(proposal.clone()))); + assert_ok!(CouncilVoting::vote(2, proposal.blake2_256().into(), true)); + assert_ok!(CouncilVoting::vote(3, proposal.blake2_256().into(), false)); assert_eq!(CouncilVoting::tally(&proposal.blake2_256().into()), (2, 1, 0)); assert_ok!(CouncilVoting::end_block(System::block_number())); @@ -485,7 +481,7 @@ mod tests { with_externalities(&mut new_test_ext(true), || { System::set_block_number(1); let proposal = set_balance_proposal(42); - assert_noop!(CouncilVoting::propose(Origin::signed(4), Box::new(proposal)), "proposer would not be on council"); + assert_noop!(CouncilVoting::propose(4, Box::new(proposal)), "proposer would not be on council"); }); } @@ -494,8 +490,8 @@ mod tests { with_externalities(&mut new_test_ext(true), || { System::set_block_number(1); let proposal = set_balance_proposal(42); - assert_ok!(CouncilVoting::propose(Origin::signed(1), Box::new(proposal.clone()))); - assert_noop!(CouncilVoting::vote(Origin::signed(4), proposal.blake2_256().into(), true), "only councillors may vote on council proposals"); + assert_ok!(CouncilVoting::propose(1, Box::new(proposal.clone()))); + assert_noop!(CouncilVoting::vote(4, proposal.blake2_256().into(), true), "only councillors may vote on council proposals"); }); } } From b3e6b51c2634d93d1c0b9cb79e6bbef2db6b72e6 Mon Sep 17 00:00:00 2001 From: Guanqun Lu Date: Sat, 15 Sep 2018 23:26:43 +0800 Subject: [PATCH 15/21] update session module --- srml/session/src/lib.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/srml/session/src/lib.rs b/srml/session/src/lib.rs index 36b0b9891e0fa..82b7566d25835 100644 --- a/srml/session/src/lib.rs +++ b/srml/session/src/lib.rs @@ -52,7 +52,6 @@ use rstd::prelude::*; use primitives::traits::{Zero, One, OnFinalise, Convert, As}; use runtime_support::{StorageValue, StorageMap}; use runtime_support::dispatch::Result; -use system::ensure_signed; #[cfg(any(feature = "std", test))] use std::collections::HashMap; @@ -75,7 +74,7 @@ pub trait Trait: timestamp::Trait { decl_module! { pub struct Module for enum Call where origin: T::Origin { - fn set_key(origin, key: T::SessionKey) -> Result; + fn set_key(SystemOrigin(Signed(who)), key: T::SessionKey) -> Result; fn set_length(new: T::BlockNumber) -> Result; fn force_new_session(apply_rewards: bool) -> Result; @@ -134,8 +133,7 @@ impl Module { /// Sets the session key of `_validator` to `_key`. This doesn't take effect until the next /// session. - fn set_key(origin: T::Origin, key: T::SessionKey) -> Result { - let who = ensure_signed(origin)?; + fn set_key(who: T::AccountId, key: T::SessionKey) -> Result { // set new value for next session >::insert(who, key); Ok(()) @@ -448,7 +446,7 @@ mod tests { // Block 3: Set new key for validator 2; no visible change. System::set_block_number(3); - assert_ok!(Session::set_key(Origin::signed(2), 5)); + assert_ok!(Session::set_key(2, 5)); assert_eq!(Consensus::authorities(), vec![1, 2, 3]); Session::check_rotate_session(3); From 2ff8f26fd73e9c8a20aec9370f05f98651d943e7 Mon Sep 17 00:00:00 2001 From: Guanqun Lu Date: Sat, 15 Sep 2018 23:38:31 +0800 Subject: [PATCH 16/21] update council motion module --- srml/council/src/motions.rs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/srml/council/src/motions.rs b/srml/council/src/motions.rs index 4e34cd1518df2..13cb32f933100 100644 --- a/srml/council/src/motions.rs +++ b/srml/council/src/motions.rs @@ -23,7 +23,7 @@ use primitives::traits::{Hash, EnsureOrigin, MaybeSerializeDebug, OnFinalise}; use srml_support::dispatch::{Result, Dispatchable, Parameter}; use srml_support::{StorageValue, StorageMap}; use super::{Trait as CouncilTrait, Module as Council}; -use system::{self, ensure_signed}; +use system; /// Simple index type for proposal counting. pub type ProposalIndex = u32; @@ -67,8 +67,8 @@ decl_event!( decl_module! { #[cfg_attr(feature = "std", serde(bound(deserialize = "::Proposal: ::serde::de::DeserializeOwned")))] pub struct Module for enum Call where origin: ::Origin { - fn propose(origin, threshold: u32, proposal: Box<::Proposal>) -> Result; - fn vote(origin, proposal: T::Hash, index: ProposalIndex, approve: bool) -> Result; + fn propose(SystemOrigin(Signed(who)), threshold: u32, proposal: Box<::Proposal>) -> Result; + fn vote(SystemOrigin(Signed(who)), proposal: T::Hash, index: ProposalIndex, approve: bool) -> Result; } } @@ -98,8 +98,7 @@ impl Module { } // Dispatch - fn propose(origin: ::Origin, threshold: u32, proposal: Box<::Proposal>) -> Result { - let who = ensure_signed(origin)?; + fn propose(who: T::AccountId, threshold: u32, proposal: Box<::Proposal>) -> Result { ensure!(Self::is_councillor(&who), "proposer not on council"); @@ -122,8 +121,7 @@ impl Module { Ok(()) } - fn vote(origin: ::Origin, proposal: T::Hash, index: ProposalIndex, approve: bool) -> Result { - let who = ensure_signed(origin)?; + fn vote(who: T::AccountId, proposal: T::Hash, index: ProposalIndex, approve: bool) -> Result { ensure!(Self::is_councillor(&who), "voter not on council"); @@ -241,7 +239,7 @@ mod tests { System::set_block_number(1); let proposal = set_balance_proposal(42); let hash = proposal.blake2_256().into(); - assert_ok!(CouncilMotions::propose(Origin::signed(1), 3, Box::new(proposal.clone()))); + assert_ok!(CouncilMotions::propose(1, 3, Box::new(proposal.clone()))); assert_eq!(CouncilMotions::proposals(), vec![hash]); assert_eq!(CouncilMotions::proposal_of(&hash), Some(proposal)); assert_eq!(CouncilMotions::voting(&hash), Some((0, 3, vec![1], Vec::::new()))); @@ -260,7 +258,7 @@ mod tests { with_externalities(&mut new_test_ext(true), || { System::set_block_number(1); let proposal = set_balance_proposal(42); - assert_noop!(CouncilMotions::propose(Origin::signed(42), 3, Box::new(proposal.clone())), "proposer not on council"); + assert_noop!(CouncilMotions::propose(42, 3, Box::new(proposal.clone())), "proposer not on council"); }); } From 433a56984e6489c9c64fe1d5ee1b8508af5c5da4 Mon Sep 17 00:00:00 2001 From: Guanqun Lu Date: Sat, 15 Sep 2018 23:43:00 +0800 Subject: [PATCH 17/21] update left modules --- srml/contract/src/lib.rs | 11 ++++------- srml/contract/src/tests.rs | 34 +++++++++++++++++----------------- srml/council/src/motions.rs | 24 ++++++++++++------------ 3 files changed, 33 insertions(+), 36 deletions(-) diff --git a/srml/contract/src/lib.rs b/srml/contract/src/lib.rs index a332fa6f9bd3b..24e34f6196bd9 100644 --- a/srml/contract/src/lib.rs +++ b/srml/contract/src/lib.rs @@ -108,7 +108,6 @@ use codec::Codec; use runtime_primitives::traits::{Hash, As, SimpleArithmetic, OnFinalise}; use runtime_support::dispatch::Result; use runtime_support::{Parameter, StorageMap, StorageValue}; -use system::ensure_signed; pub trait Trait: balances::Trait { /// Function type to get the contract address given the creator. @@ -152,7 +151,7 @@ decl_module! { pub struct Module for enum Call where origin: T::Origin { // TODO: Change AccountId to staking::Address fn call( - origin, + SystemOrigin(Signed(origin)), dest: T::AccountId, value: T::Balance, gas_limit: T::Gas, @@ -160,7 +159,7 @@ decl_module! { ) -> Result; fn create( - origin, + SystemOrigin(Signed(origin)), value: T::Balance, gas_limit: T::Gas, init_code: Vec, @@ -208,13 +207,12 @@ impl double_map::StorageDoubleMap for StorageOf { impl Module { /// Make a call to a specified account, optionally transferring some balance. fn call( - origin: ::Origin, + origin: T::AccountId, dest: T::AccountId, value: T::Balance, gas_limit: T::Gas, data: Vec, ) -> Result { - let origin = ensure_signed(origin)?; // Pay for the gas upfront. // @@ -255,13 +253,12 @@ impl Module { /// after the execution is saved as the `code` of the account. That code will be invoked /// upon any message received by this account. fn create( - origin: ::Origin, + origin: T::AccountId, endowment: T::Balance, gas_limit: T::Gas, ctor_code: Vec, data: Vec, ) -> Result { - let origin = ensure_signed(origin)?; // Pay for the gas upfront. // diff --git a/srml/contract/src/tests.rs b/srml/contract/src/tests.rs index f61c0bab7a67e..3e4f7499b3df0 100644 --- a/srml/contract/src/tests.rs +++ b/srml/contract/src/tests.rs @@ -186,7 +186,7 @@ fn contract_transfer() { Balances::set_free_balance(&1, 11); Balances::increase_total_stake_by(11); - assert_ok!(Contract::call(Origin::signed(0), 1, 3, 100_000, Vec::new())); + assert_ok!(Contract::call(0, 1, 3, 100_000, Vec::new())); assert_eq!( Balances::free_balance(&0), @@ -222,7 +222,7 @@ fn contract_transfer_takes_creation_fee() { Balances::set_free_balance(&1, 11); Balances::increase_total_stake_by(11); - assert_ok!(Contract::call(Origin::signed(0), 1, 3, 100_000, Vec::new())); + assert_ok!(Contract::call(0, 1, 3, 100_000, Vec::new())); assert_eq!( Balances::free_balance(&0), @@ -263,7 +263,7 @@ fn contract_transfer_takes_transfer_fee() { // is charged (and creation fee is not). Balances::set_free_balance(&CONTRACT_SHOULD_TRANSFER_TO, 25); - assert_ok!(Contract::call(Origin::signed(0), 1, 3, 100_000, Vec::new())); + assert_ok!(Contract::call(0, 1, 3, 100_000, Vec::new())); assert_eq!( Balances::free_balance(&0), @@ -300,7 +300,7 @@ fn contract_transfer_oog() { Balances::set_free_balance(&1, 11); Balances::increase_total_stake_by(11); - assert_ok!(Contract::call(Origin::signed(0), 1, 3, 135 + 135 + 7, Vec::new())); + assert_ok!(Contract::call(0, 1, 3, 135 + 135 + 7, Vec::new())); assert_eq!( Balances::free_balance(&0), @@ -332,7 +332,7 @@ fn contract_transfer_max_depth() { Balances::set_free_balance(&CONTRACT_SHOULD_TRANSFER_TO, 11); Balances::increase_total_stake_by(11); - assert_ok!(Contract::call(Origin::signed(0), CONTRACT_SHOULD_TRANSFER_TO, 3, 100_000, Vec::new())); + assert_ok!(Contract::call(0, CONTRACT_SHOULD_TRANSFER_TO, 3, 100_000, Vec::new())); assert_eq!( Balances::free_balance(&0), @@ -444,7 +444,7 @@ fn contract_create() { >::insert(1, code_create.to_vec()); // When invoked, the contract at address `1` must create a contract with 'transfer' code. - assert_ok!(Contract::call(Origin::signed(0), 1, 11, 100_000, Vec::new())); + assert_ok!(Contract::call(0, 1, 11, 100_000, Vec::new())); let derived_address = ::DetermineContractAddress::contract_address_for( &code_ctor_transfer, @@ -464,7 +464,7 @@ fn contract_create() { assert_eq!(Balances::free_balance(&derived_address), 3); // Initiate transfer to the newly created contract. - assert_ok!(Contract::call(Origin::signed(0), derived_address, 22, 100_000, Vec::new())); + assert_ok!(Contract::call(0, derived_address, 22, 100_000, Vec::new())); assert_eq!( Balances::free_balance(&0), @@ -497,7 +497,7 @@ fn top_level_create() { Balances::increase_total_stake_by(30); assert_ok!(Contract::create( - Origin::signed(0), + 0, 11, 100_000, code_ctor_transfer.clone(), @@ -536,7 +536,7 @@ fn refunds_unused_gas() { Balances::set_free_balance(&0, 100_000_000); Balances::increase_total_stake_by(100_000_000); - assert_ok!(Contract::call(Origin::signed(0), 1, 0, 100_000, Vec::new())); + assert_ok!(Contract::call(0, 1, 0, 100_000, Vec::new())); assert_eq!(Balances::free_balance(&0), 100_000_000 - 4 - (2 * 135)); }); @@ -550,7 +550,7 @@ fn call_with_zero_value() { Balances::set_free_balance(&0, 100_000_000); Balances::increase_total_stake_by(100_000_000); - assert_ok!(Contract::call(Origin::signed(0), 1, 0, 100_000, Vec::new())); + assert_ok!(Contract::call(0, 1, 0, 100_000, Vec::new())); assert_eq!(Balances::free_balance(&0), 100_000_000 - (2 * 135)); }); @@ -564,7 +564,7 @@ fn create_with_zero_endowment() { Balances::set_free_balance(&0, 100_000_000); Balances::increase_total_stake_by(100_000_000); - assert_ok!(Contract::create(Origin::signed(0), 0, 100_000, code_nop, Vec::new())); + assert_ok!(Contract::create(0, 0, 100_000, code_nop, Vec::new())); assert_eq!( Balances::free_balance(&0), @@ -597,7 +597,7 @@ fn account_removal_removes_storage() { // the balance of account 1 is will be below than exsistential threshold. // // This should lead to the removal of all storage associated with this account. - assert_ok!(Balances::transfer(Origin::signed(1), 2.into(), 20)); + assert_ok!(Balances::transfer(1, 2.into(), 20)); // Verify that all entries from account 1 is removed, while // entries from account 2 is in place. @@ -637,7 +637,7 @@ fn top_level_call_refunds_even_if_fails() { Balances::increase_total_stake_by(100_000_000); assert_err!( - Contract::call(Origin::signed(0), 1, 0, 100_000, Vec::new()), + Contract::call(0, 1, 0, 100_000, Vec::new()), "vm execute returned error while call" ); @@ -668,19 +668,19 @@ fn block_gas_limit() { // Spend 50_000 units of gas (OOG). assert_err!( - Contract::call(Origin::signed(0), 1, 0, 50_000, Vec::new()), + Contract::call(0, 1, 0, 50_000, Vec::new()), "vm execute returned error while call" ); // Ensure we can't spend more gas than available in block gas limit. assert_err!( - Contract::call(Origin::signed(0), 1, 0, 50_001, Vec::new()), + Contract::call(0, 1, 0, 50_001, Vec::new()), "block gas limit is reached" ); // However, we can spend another 50_000 assert_err!( - Contract::call(Origin::signed(0), 1, 0, 50_000, Vec::new()), + Contract::call(0, 1, 0, 50_000, Vec::new()), "vm execute returned error while call" ); }, @@ -753,7 +753,7 @@ fn input_data() { Balances::set_free_balance(&0, 100_000_000); Balances::increase_total_stake_by(100_000_000); - assert_ok!(Contract::call(Origin::signed(0), 1, 0, 50_000, vec![0, 1, 2, 3])); + assert_ok!(Contract::call(0, 1, 0, 50_000, vec![0, 1, 2, 3])); // all asserts are made within contract code itself. }, diff --git a/srml/council/src/motions.rs b/srml/council/src/motions.rs index 13cb32f933100..ef76de87c0573 100644 --- a/srml/council/src/motions.rs +++ b/srml/council/src/motions.rs @@ -268,8 +268,8 @@ mod tests { System::set_block_number(1); let proposal = set_balance_proposal(42); let hash: H256 = proposal.blake2_256().into(); - assert_ok!(CouncilMotions::propose(Origin::signed(1), 3, Box::new(proposal.clone()))); - assert_noop!(CouncilMotions::vote(Origin::signed(42), hash.clone(), 0, true), "voter not on council"); + assert_ok!(CouncilMotions::propose(1, 3, Box::new(proposal.clone()))); + assert_noop!(CouncilMotions::vote(42, hash.clone(), 0, true), "voter not on council"); }); } @@ -279,8 +279,8 @@ mod tests { System::set_block_number(3); let proposal = set_balance_proposal(42); let hash: H256 = proposal.blake2_256().into(); - assert_ok!(CouncilMotions::propose(Origin::signed(1), 3, Box::new(proposal.clone()))); - assert_noop!(CouncilMotions::vote(Origin::signed(2), hash.clone(), 1, true), "mismatched index"); + assert_ok!(CouncilMotions::propose(1, 3, Box::new(proposal.clone()))); + assert_noop!(CouncilMotions::vote(2, hash.clone(), 1, true), "mismatched index"); }); } @@ -290,12 +290,12 @@ mod tests { System::set_block_number(1); let proposal = set_balance_proposal(42); let hash: H256 = proposal.blake2_256().into(); - assert_ok!(CouncilMotions::propose(Origin::signed(1), 2, Box::new(proposal.clone()))); + assert_ok!(CouncilMotions::propose(1, 2, Box::new(proposal.clone()))); assert_eq!(CouncilMotions::voting(&hash), Some((0, 2, vec![1], Vec::::new()))); - assert_noop!(CouncilMotions::vote(Origin::signed(1), hash.clone(), 0, true), "duplicate vote ignored"); - assert_ok!(CouncilMotions::vote(Origin::signed(1), hash.clone(), 0, false)); + assert_noop!(CouncilMotions::vote(1, hash.clone(), 0, true), "duplicate vote ignored"); + assert_ok!(CouncilMotions::vote(1, hash.clone(), 0, false)); assert_eq!(CouncilMotions::voting(&hash), Some((0, 2, Vec::::new(), vec![1]))); - assert_noop!(CouncilMotions::vote(Origin::signed(1), hash.clone(), 0, false), "duplicate vote ignored"); + assert_noop!(CouncilMotions::vote(1, hash.clone(), 0, false), "duplicate vote ignored"); assert_eq!(System::events(), vec![ EventRecord { @@ -316,8 +316,8 @@ mod tests { System::set_block_number(1); let proposal = set_balance_proposal(42); let hash: H256 = proposal.blake2_256().into(); - assert_ok!(CouncilMotions::propose(Origin::signed(1), 3, Box::new(proposal.clone()))); - assert_ok!(CouncilMotions::vote(Origin::signed(2), hash.clone(), 0, false)); + assert_ok!(CouncilMotions::propose(1, 3, Box::new(proposal.clone()))); + assert_ok!(CouncilMotions::vote(2, hash.clone(), 0, false)); assert_eq!(System::events(), vec![ EventRecord { @@ -342,8 +342,8 @@ mod tests { System::set_block_number(1); let proposal = set_balance_proposal(42); let hash: H256 = proposal.blake2_256().into(); - assert_ok!(CouncilMotions::propose(Origin::signed(1), 2, Box::new(proposal.clone()))); - assert_ok!(CouncilMotions::vote(Origin::signed(2), hash.clone(), 0, true)); + assert_ok!(CouncilMotions::propose(1, 2, Box::new(proposal.clone()))); + assert_ok!(CouncilMotions::vote(2, hash.clone(), 0, true)); assert_eq!(System::events(), vec![ EventRecord { From b40006d6421bfffb4f8c2a8018ec14ade58e2f4b Mon Sep 17 00:00:00 2001 From: Guanqun Lu Date: Sun, 16 Sep 2018 13:07:50 +0800 Subject: [PATCH 18/21] sr-support: add inherent check --- srml/support/src/dispatch.rs | 76 +++++++++++++++++++++++++++++++++++- 1 file changed, 74 insertions(+), 2 deletions(-) diff --git a/srml/support/src/dispatch.rs b/srml/support/src/dispatch.rs index 4c5d03bfd32e6..477efa9570c7e 100644 --- a/srml/support/src/dispatch.rs +++ b/srml/support/src/dispatch.rs @@ -126,6 +126,23 @@ macro_rules! decl_module { $($rest)* ); }; + (@normalize + $(#[$attr:meta])* + pub struct $mod_type:ident<$trait_instance:ident: $trait_name:ident> + for enum $call_type:ident where origin: $origin_type:ty where system = $system:ident where council = $council:ident + [ $($t:tt)* ] + $(#[doc = $doc_attr:tt])* + fn $fn_name:ident($top_origin:ident($sub_origin:ident) $(, $param_name:ident : $param:ty)* ) -> $result:ty ; + $($rest:tt)* + ) => { + decl_module!(@normalize + $(#[$attr])* + pub struct $mod_type<$trait_instance: $trait_name> + for enum $call_type where origin: $origin_type where system = $system where council = $council + [ $($t)* $(#[doc = $doc_attr])* fn $fn_name(($top_origin $sub_origin $sub_origin) $( , $param_name : $param )* ) -> $result; ] + $($rest)* + ); + }; (@normalize $(#[$attr:meta])* pub struct $mod_type:ident<$trait_instance:ident: $trait_name:ident> @@ -182,6 +199,15 @@ macro_rules! decl_module { <$mod_type<$trait_instance>>::$fn_name( $who $(, $param_name )* ) } }; + (@call + (SystemOrigin Inherent Inherent) + $mod_type:ident $trait_instance:ident $fn_name:ident $origin:ident $system:ident $council:ident [ $( $param_name:ident),* ] + ) => { + { + $system::ensure_inherent($origin)?; + <$mod_type<$trait_instance>>::$fn_name( $( $param_name ),* ) + } + }; (@call (CouncilOrigin Members $n:ident) $mod_type:ident $trait_instance:ident $fn_name:ident $origin:ident $system:ident $council:ident [ $( $param_name:ident),* ] @@ -618,6 +644,31 @@ macro_rules! __functions_to_json { ), __functions_to_json!(","; $fn_id + 1; $origin_type; $trait_instance; $($rest)*) ) }; + // system inherent inherent + ( + $prefix_str:tt; + $fn_id:expr; + $origin_type:ty; + $trait_instance:ident; + fn $fn_name:ident( + (SystemOrigin Inherent Inherent) + $( + , $param_name:ident : $param:ty + )* + ) -> $result:ty; + $fn_doc:expr; + $($rest:tt)* + ) => { + concat!($prefix_str, " ", + __function_to_json!( + fn $fn_name( + $( $param_name : $param ),* + ) -> $result; + $fn_doc; + $fn_id; + ), __functions_to_json!(","; $fn_id + 1; $origin_type; $trait_instance; $($rest)*) + ) + }; // council members $n ( $prefix_str:tt; @@ -734,6 +785,10 @@ mod tests { Ok(()) } + pub fn ensure_inherent(_: R) -> Result { + Ok(()) + } + pub fn ensure_signed(_: O) -> result::Result { Err("unreachable") } @@ -757,8 +812,10 @@ mod tests { fn aux_4(data: i32) -> Result; fn aux_5(SystemOrigin(Signed(who))) -> Result; fn aux_6(SystemOrigin(Signed(who)), data: i32) -> Result; - fn aux_7(CouncilOrigin(Members(_2))) -> Result; - fn aux_8(CouncilOrigin(Members(_2)), data: i32) -> Result; + fn aux_7(SystemOrigin(Inherent)) -> Result; + fn aux_8(SystemOrigin(Inherent), data: i32) -> Result; + fn aux_9(CouncilOrigin(Members(_2))) -> Result; + fn aux_10(CouncilOrigin(Members(_2)), data: i32) -> Result; } } @@ -801,6 +858,13 @@ mod tests { r#""0 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1": { "name": "aux_8", "params": [ "#, r#"{ "name": "data", "type": "i32" }"#, + r#" ], "description": [ ] }, "#, + + r#""0 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1": { "name": "aux_9", "params": [ "#, + r#" ], "description": [ ] }, "#, + + r#""0 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1": { "name": "aux_10", "params": [ "#, + r#"{ "name": "data", "type": "i32" }"#, r#" ], "description": [ ] }"#, r#" } }"#, @@ -843,6 +907,14 @@ mod tests { fn aux_8(_: i32) -> Result { unreachable!() } + + fn aux_9() -> Result { + unreachable!() + } + + fn aux_10(_: i32) -> Result { + unreachable!() + } } struct TraitImpl {} From a16c7257bcf760b4687898998a279e17ccb4a9ae Mon Sep 17 00:00:00 2001 From: Guanqun Lu Date: Sun, 16 Sep 2018 13:08:49 +0800 Subject: [PATCH 19/21] update timestamp to replace the inherent check --- srml/timestamp/src/lib.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/srml/timestamp/src/lib.rs b/srml/timestamp/src/lib.rs index 2556b3211c741..8919282476624 100644 --- a/srml/timestamp/src/lib.rs +++ b/srml/timestamp/src/lib.rs @@ -56,7 +56,6 @@ extern crate parity_codec as codec; use runtime_support::{StorageValue, Parameter}; use runtime_support::dispatch::Result; use runtime_primitives::traits::{OnFinalise, SimpleArithmetic, As, Zero}; -use system::ensure_inherent; pub trait Trait: consensus::Trait + system::Trait { /// The position of the required timestamp-set extrinsic. @@ -68,7 +67,7 @@ pub trait Trait: consensus::Trait + system::Trait { decl_module! { pub struct Module for enum Call where origin: T::Origin { - fn set(origin, now: T::Moment) -> Result; + fn set(SystemOrigin(Inherent), now: T::Moment) -> Result; } } @@ -102,8 +101,7 @@ impl Module { /// if this call hasn't been invoked by that time. /// /// The timestamp should be greater than the previous one by the amount specified by `block_period`. - fn set(origin: T::Origin, now: T::Moment) -> Result { - ensure_inherent(origin)?; + fn set(now: T::Moment) -> Result { assert!(!::DidUpdate::exists(), "Timestamp must be updated only once in the block"); assert!( >::extrinsic_index() == Some(T::TIMESTAMP_SET_POSITION), From a909ce4e0e527ee47cd906496dae72f6947d3b9f Mon Sep 17 00:00:00 2001 From: Guanqun Lu Date: Sun, 16 Sep 2018 13:09:42 +0800 Subject: [PATCH 20/21] update consensus's inherent check --- srml/consensus/src/lib.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/srml/consensus/src/lib.rs b/srml/consensus/src/lib.rs index dabad4a082f7d..4d69c3f61ba0f 100644 --- a/srml/consensus/src/lib.rs +++ b/srml/consensus/src/lib.rs @@ -130,7 +130,7 @@ decl_storage! { decl_module! { pub struct Module for enum Call where origin: T::Origin { fn report_misbehavior(SystemOrigin(Signed(who)), report: MisbehaviorReport) -> Result; - fn note_offline(origin, offline_val_indices: Vec) -> Result; + fn note_offline(SystemOrigin(Inherent), offline_val_indices: Vec) -> Result; fn remark(SystemOrigin(Signed(who)), remark: Vec) -> Result; fn set_code(new: Vec) -> Result; fn set_storage(items: Vec) -> Result; @@ -166,8 +166,7 @@ impl Module { /// Note the previous block's validator missed their opportunity to propose a block. This only comes in /// if 2/3+1 of the validators agree that no proposal was submitted. It's only relevant /// for the previous block. - fn note_offline(origin: T::Origin, offline_val_indices: Vec) -> Result { - ensure_inherent(origin)?; + fn note_offline(offline_val_indices: Vec) -> Result { assert!( >::extrinsic_index() == Some(T::NOTE_OFFLINE_POSITION), "note_offline extrinsic must be at position {} in the block", From 00b6f45714539bbbb42044692ca0625b41a03eb3 Mon Sep 17 00:00:00 2001 From: Guanqun Lu Date: Sun, 16 Sep 2018 13:15:19 +0800 Subject: [PATCH 21/21] fix the test warning --- srml/consensus/src/lib.rs | 1 - srml/council/src/motions.rs | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/srml/consensus/src/lib.rs b/srml/consensus/src/lib.rs index 4d69c3f61ba0f..a76f13accdb68 100644 --- a/srml/consensus/src/lib.rs +++ b/srml/consensus/src/lib.rs @@ -48,7 +48,6 @@ use runtime_support::storage::StorageValue; use runtime_support::storage::unhashed::StorageVec; use primitives::traits::{MaybeSerializeDebug, OnFinalise, Member, DigestItem}; use primitives::bft::MisbehaviorReport; -use system::ensure_inherent; #[cfg(any(feature = "std", test))] use substrate_primitives::Blake2Hasher; diff --git a/srml/council/src/motions.rs b/srml/council/src/motions.rs index ef76de87c0573..e4ff8ff890995 100644 --- a/srml/council/src/motions.rs +++ b/srml/council/src/motions.rs @@ -216,7 +216,7 @@ mod tests { use super::*; use super::RawEvent; use ::tests::*; - use ::tests::{Call, Origin, Event as OuterEvent}; + use ::tests::{Call, Event as OuterEvent}; use srml_support::Hashable; use system::{EventRecord, Phase};