From 640175790e9c43fc26ac943a6a601399a3753d28 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Fri, 17 Jan 2020 18:15:03 +0100 Subject: [PATCH 1/2] Ensure all votes are removed after tally --- Cargo.lock | 1 - bin/node/cli/Cargo.toml | 1 - frame/society/src/lib.rs | 8 +++++++- frame/society/src/tests.rs | 15 +++++++++++++++ 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 78291bb8a4f2f..630e372dff82e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3040,7 +3040,6 @@ dependencies = [ "pallet-contracts 2.0.0", "pallet-im-online 2.0.0", "pallet-indices 2.0.0", - "pallet-society 2.0.0", "pallet-timestamp 2.0.0", "pallet-transaction-payment 2.0.0", "parity-scale-codec 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/bin/node/cli/Cargo.toml b/bin/node/cli/Cargo.toml index b0a2242f0e4a3..c81cf38a9c2ff 100644 --- a/bin/node/cli/Cargo.toml +++ b/bin/node/cli/Cargo.toml @@ -72,7 +72,6 @@ pallet-transaction-payment = { version = "2.0.0", path = "../../../frame/transac frame-support = { version = "2.0.0", default-features = false, path = "../../../frame/support" } pallet-im-online = { version = "2.0.0", default-features = false, path = "../../../frame/im-online" } pallet-authority-discovery = { version = "2.0.0", path = "../../../frame/authority-discovery" } -pallet-society = { version = "2.0.0", path = "../../../frame/society" } # node-specific dependencies node-runtime = { version = "2.0.0", path = "../runtime" } diff --git a/frame/society/src/lib.rs b/frame/society/src/lib.rs index 3bbd4705f5793..d59f0581869bf 100644 --- a/frame/society/src/lib.rs +++ b/frame/society/src/lib.rs @@ -1324,6 +1324,9 @@ impl, I: Instance> Module { } }).collect::>(); + // Clean up all votes. + >::remove_all(); + // Reward one of the voters who voted the right way. if !total_slash.is_zero() { if let Some(winner) = pick_item(&mut rng, &rewardees) { @@ -1472,7 +1475,7 @@ impl, I: Instance> Module { let mut rejection_count = 0; // Tallies total number of approve and reject votes for the defender. members.iter() - .filter_map(|m| >::get(m)) + .filter_map(|m| >::take(m)) .for_each(|v| { match v { Vote::Approve => approval_count += 1, @@ -1485,6 +1488,9 @@ impl, I: Instance> Module { Self::suspend_member(&defender); *members = Self::members(); } + + // Clean up the entire store item. + >::remove_all(); } // Start a new defender rotation diff --git a/frame/society/src/tests.rs b/frame/society/src/tests.rs index cf899fb4e250a..580edc36431b8 100644 --- a/frame/society/src/tests.rs +++ b/frame/society/src/tests.rs @@ -538,6 +538,11 @@ fn challenges_work() { assert_ok!(Society::add_member(&20)); assert_ok!(Society::add_member(&30)); assert_ok!(Society::add_member(&40)); + // Votes are empty + assert_eq!(>::get(10), None); + assert_eq!(>::get(20), None); + assert_eq!(>::get(30), None); + assert_eq!(>::get(40), None); // Check starting point assert_eq!(Society::members(), vec![10, 20, 30, 40]); assert_eq!(Society::defender(), None); @@ -561,6 +566,11 @@ fn challenges_work() { run_to_block(24); // 20 survives assert_eq!(Society::members(), vec![10, 20, 30, 40]); + // Votes are reset + assert_eq!(>::get(10), None); + assert_eq!(>::get(20), None); + assert_eq!(>::get(30), None); + assert_eq!(>::get(40), None); // One more time assert_eq!(Society::defender(), Some(20)); // 2 people say accept, 2 reject @@ -574,6 +584,11 @@ fn challenges_work() { assert_eq!(Society::suspended_member(20), true); // New defender is chosen assert_eq!(Society::defender(), Some(40)); + // Votes are reset + assert_eq!(>::get(10), None); + assert_eq!(>::get(20), None); + assert_eq!(>::get(30), None); + assert_eq!(>::get(40), None); }); } From 5d8159a3b1870f39ae8272b933cd9906d7181fb3 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Fri, 17 Jan 2020 18:19:34 +0100 Subject: [PATCH 2/2] Fix comment --- frame/society/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/society/src/lib.rs b/frame/society/src/lib.rs index d59f0581869bf..348607d196c86 100644 --- a/frame/society/src/lib.rs +++ b/frame/society/src/lib.rs @@ -1489,7 +1489,7 @@ impl, I: Instance> Module { *members = Self::members(); } - // Clean up the entire store item. + // Clean up all votes. >::remove_all(); }