From 6f59d9fc274e8cc6d1b3414698cf5fdab2ca0a0e Mon Sep 17 00:00:00 2001 From: roduquen Date: Tue, 12 Jul 2022 14:06:19 +0200 Subject: [PATCH 1/9] working --- rpc/runtime-api/Cargo.toml | 6 +- rpc/runtime-api/src/lib.rs | 3 +- src/lib.rs | 38 +++++- src/tests/rpc_calls.rs | 265 ++++++++++++++++++++++++------------- 4 files changed, 216 insertions(+), 96 deletions(-) diff --git a/rpc/runtime-api/Cargo.toml b/rpc/runtime-api/Cargo.toml index 2e856ed..849beb1 100644 --- a/rpc/runtime-api/Cargo.toml +++ b/rpc/runtime-api/Cargo.toml @@ -7,15 +7,15 @@ license = "Apache-2.0" [dependencies] -codec = { package = "parity-scale-codec", version = "3.0.0", features = [ +codec = { package = "parity-scale-codec", version = "3.0", features = [ "derive", ], default-features = false } scale-info = { version = "2.1.1", default-features = false, features = [ "derive", ] } -sp-api = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.24" } -sp-std = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.24" } +sp-api = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.24" , version = "4.0.0-dev"} +sp-std = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.24" , version = "4.0.0-dev"} [features] default = ["std"] diff --git a/rpc/runtime-api/src/lib.rs b/rpc/runtime-api/src/lib.rs index e3a33e0..cc0cbde 100644 --- a/rpc/runtime-api/src/lib.rs +++ b/rpc/runtime-api/src/lib.rs @@ -1,7 +1,8 @@ #![cfg_attr(not(feature = "std"), no_std)] use codec::Codec; -use sp_std::vec::Vec; +#[cfg(not(feature = "std"))] +use sp_std::prelude::Vec; sp_api::decl_runtime_apis! { pub trait SuperSigApi diff --git a/src/lib.rs b/src/lib.rs index 9616fb3..4d61517 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -755,7 +755,7 @@ pub mod pallet { #[allow(dead_code)] impl Pallet { - fn get_account_supersigs(who: T::AccountId) -> Vec { + pub fn get_account_supersigs(who: T::AccountId) -> Vec { Members::::iter() .filter_map(|(supersig_id, member_id, _)| { if member_id == who { @@ -767,7 +767,41 @@ impl Pallet { .collect() } - fn get_members_connected_to_supersig(which: SupersigId) -> Vec<(T::AccountId, Role)> { + pub fn get_members_connected(which: SupersigId) -> Vec<(T::AccountId, Role)> { Members::::iter_prefix(which).collect() } + + pub fn get_proposals(which: SupersigId) -> (Vec<((Vec, T::AccountId, BalanceOf), Vec)>, u32) { + let member_count = Self::total_members(which); + let proposal_state = Calls::::iter_prefix(which) + .map(|(call_id, call)| ( + (call.data, call.provider, call.deposit), + MembersVotes::::iter_prefix((which, call_id)) + .filter_map(|(account_id, vote)| { + if vote { + Some(account_id) + } else { + None + } + }) + .collect() + )).collect(); + (proposal_state, member_count) + } + + pub fn get_proposal_state(which: SupersigId, call_id: CallId) -> (bool, Vec, u32, u32) { + let member_count = Self::total_members(which); + let votes = Self::votes(which, call_id); + let exists = !Self::calls(which, call_id).is_none(); + let member_votes = MembersVotes::::iter_prefix((which, call_id)) + .filter_map(|(account_id, vote)| { + if vote { + Some(account_id) + } else { + None + } + }) + .collect(); + (exists, member_votes, member_count, votes) + } } diff --git a/src/tests/rpc_calls.rs b/src/tests/rpc_calls.rs index e74c6ef..aff0bac 100644 --- a/src/tests/rpc_calls.rs +++ b/src/tests/rpc_calls.rs @@ -3,128 +3,213 @@ use crate::{Role}; use frame_support::{ assert_ok }; pub use sp_std::{boxed::Box, mem::size_of}; +fn create_supersig(supersig_id : u128) -> sp_runtime::AccountId32 { + let creator = vec![(ALICE(), Role::Master)].try_into().unwrap(); + assert_ok!(Supersig::create_supersig(Origin::signed(ALICE()), creator,)); + let supersig_account = get_supersig_account(u64::try_from(supersig_id).unwrap()); + assert_ok!(Balances::transfer( + Origin::signed(ALICE()), + supersig_account.clone(), + 100_000 + )); + assert_ok!(Supersig::add_members( + Origin::signed(supersig_account.clone()), + vec!((BOB(), Role::Standard), (CHARLIE(), Role::Standard)).try_into().unwrap() + )); + assert_eq!(Supersig::members(supersig_id, ALICE()), Role::Master); + assert_eq!(Supersig::members(supersig_id, BOB()), Role::Standard); + assert_eq!(Supersig::members(supersig_id, CHARLIE()), Role::Standard); + assert_eq!(Supersig::total_members(supersig_id), 3); + supersig_account +} + #[test] fn get_account_supersigs() { ExtBuilder::default().balances(vec![]).build().execute_with(|| { - let members = vec![(ALICE(), Role::Standard), (BOB(), Role::Standard)].try_into().unwrap(); - assert_ok!(Supersig::create_supersig(Origin::signed(ALICE()), members,)); + let mut supersig_count : u128 = 0; - let supersig_account = get_supersig_account(0); - assert_ok!(Balances::transfer( - Origin::signed(ALICE()), - supersig_account.clone(), - 100_000 - )); - assert_ok!(Supersig::add_members( - Origin::signed(supersig_account.clone()), - vec!((BOB(), Role::Master), (CHARLIE(), Role::Standard)).try_into().unwrap() - )); + assert_eq!(Supersig::get_account_supersigs(ALICE()), vec![]); - assert_eq!(Supersig::members(0, ALICE()), Role::Standard); - assert_eq!(Supersig::members(0, BOB()), Role::Master); - assert_eq!(Supersig::members(0, CHARLIE()), Role::Standard); - assert_eq!(Supersig::total_members(0), 3); + create_supersig(supersig_count); assert_eq!(Supersig::get_account_supersigs(ALICE()), vec![0]); + supersig_count += 1; - let members2 = vec![(ALICE(), Role::Standard), (BOB(), Role::Standard)].try_into().unwrap(); - assert_ok!(Supersig::create_supersig(Origin::signed(ALICE()), members2,)); - let supersig_account2 = get_supersig_account(1); - assert_ok!(Balances::transfer( - Origin::signed(ALICE()), - supersig_account2.clone(), - 100_000 - )); - assert_ok!(Supersig::add_members( - Origin::signed(supersig_account2.clone()), - vec!((BOB(), Role::Master), (CHARLIE(), Role::Standard)).try_into().unwrap() - )); - - assert_eq!(Supersig::members(1, ALICE()), Role::Standard); - assert_eq!(Supersig::members(1, BOB()), Role::Master); - assert_eq!(Supersig::members(1, CHARLIE()), Role::Standard); - assert_eq!(Supersig::total_members(1), 3); + create_supersig(supersig_count); assert_eq!(Supersig::get_account_supersigs(ALICE()), vec![1, 0]); + supersig_count += 1; - let members3 = vec![(ALICE(), Role::Standard), (BOB(), Role::Standard)].try_into().unwrap(); - assert_ok!(Supersig::create_supersig(Origin::signed(BOB()), members3,)); - let supersig_account3 = get_supersig_account(2); - assert_ok!(Balances::transfer( - Origin::signed(ALICE()), - supersig_account3.clone(), - 100_000 - )); + create_supersig(supersig_count); + assert_eq!(Supersig::get_account_supersigs(ALICE()), vec![1, 0, 2]); + supersig_count += 1; + + create_supersig(supersig_count); + assert_eq!(Supersig::get_account_supersigs(ALICE()), vec![1, 0, 3, 2]); + supersig_count += 1; + + create_supersig(supersig_count); + assert_eq!(Supersig::get_account_supersigs(ALICE()), vec![1, 0, 3, 4, 2]); + }) +} + +#[test] +fn get_members_connected() { + ExtBuilder::default().balances(vec![]).build().execute_with(|| { + let mut supersig_count : u128 = 0; + let mut supersig_account : sp_runtime::AccountId32; + + create_supersig(supersig_count); + assert_eq!( + Supersig::get_members_connected(supersig_count), + vec![(ALICE(), Role::Master), (BOB(), Role::Standard), (CHARLIE(), Role::Standard)] + ); + supersig_count += 1; + + supersig_account = create_supersig(supersig_count); assert_ok!(Supersig::add_members( - Origin::signed(supersig_account3.clone()), + Origin::signed(supersig_account), vec!((BOB(), Role::Master), (CHARLIE(), Role::Standard)).try_into().unwrap() )); + assert_eq!(Supersig::total_members(supersig_count), 3); + assert_eq!( + Supersig::get_members_connected(supersig_count), + vec![(ALICE(), Role::Master), (BOB(), Role::Master), (CHARLIE(), Role::Standard)] + ); + supersig_count += 1; - assert_eq!(Supersig::members(2, ALICE()), Role::Standard); - assert_eq!(Supersig::members(2, BOB()), Role::Master); - assert_eq!(Supersig::members(2, CHARLIE()), Role::Standard); - assert_eq!(Supersig::total_members(2), 3); - assert_eq!(Supersig::get_account_supersigs(ALICE()), vec![1, 0, 2]); + supersig_account = create_supersig(supersig_count); + assert_ok!(Supersig::add_members( + Origin::signed(supersig_account), + vec!((ALICE(), Role::Standard), (CHARLIE(), Role::Standard)).try_into().unwrap() + )); + assert_eq!( + Supersig::get_members_connected(2), + vec![(ALICE(), Role::Standard), (BOB(), Role::Standard), (CHARLIE(), Role::Standard)] + ); }) } #[test] -fn get_members_connected_to_supersig() { +fn get_proposals() { ExtBuilder::default().balances(vec![]).build().execute_with(|| { - let members = vec![(ALICE(), Role::Standard), (BOB(), Role::Standard)].try_into().unwrap(); - assert_ok!(Supersig::create_supersig(Origin::signed(ALICE()), members,)); + let mut call_id = 0; + let supersig_id = 0; + let call = Call::Nothing( + NoCall::do_nothing { + nothing: "test".into() + }); + let data = vec![2, 0, 16, 116, 101, 115, 116]; + let supersig_account = create_supersig(supersig_id); - let supersig_account = get_supersig_account(0); - assert_ok!(Balances::transfer( + assert_ok!(Supersig::submit_call( Origin::signed(ALICE()), supersig_account.clone(), - 100_000 + Box::new(call.clone()) )); - assert_ok!(Supersig::add_members( - Origin::signed(supersig_account.clone()), - vec!((BOB(), Role::Master), (CHARLIE(), Role::Standard)).try_into().unwrap() + assert_eq!( + Supersig::get_proposals(supersig_id), + (vec![((data.clone(), ALICE(), 7000), vec![])], 3) + ); + assert_ok!(Supersig::approve_call( + Origin::signed(BOB()), + supersig_account.clone(), + call_id )); + let first_proposal = ((data.clone(), ALICE(), 7000), vec![BOB()]); + assert_eq!( + Supersig::get_proposals(supersig_id), + (vec![first_proposal.clone()], 3) + ); + call_id += 1; - assert_eq!(Supersig::members(0, ALICE()), Role::Standard); - assert_eq!(Supersig::members(0, BOB()), Role::Master); - assert_eq!(Supersig::members(0, CHARLIE()), Role::Standard); - assert_eq!(Supersig::total_members(0), 3); - assert_eq!(Supersig::get_members_connected_to_supersig(0), vec![(ALICE(), Role::Standard), (BOB(), Role::Master), (CHARLIE(), Role::Standard)]); - - let members2 = vec![(ALICE(), Role::Master), (BOB(), Role::Standard)].try_into().unwrap(); - assert_ok!(Supersig::create_supersig(Origin::signed(ALICE()), members2,)); - let supersig_account2 = get_supersig_account(1); - assert_ok!(Balances::transfer( + assert_ok!(Supersig::submit_call( Origin::signed(ALICE()), - supersig_account2.clone(), - 100_000 + supersig_account.clone(), + Box::new(call.clone()) )); - assert_ok!(Supersig::add_members( - Origin::signed(supersig_account2.clone()), - vec!((BOB(), Role::Master), (CHARLIE(), Role::Standard)).try_into().unwrap() + assert_eq!( + Supersig::get_proposals(supersig_id), + (vec![((data.clone(), ALICE(), 7000), vec![]), first_proposal.clone()], 3) + ); + assert_ok!(Supersig::approve_call( + Origin::signed(BOB()), + supersig_account.clone(), + call_id )); + let second_proposal = ((data.clone(), ALICE(), 7000), vec![BOB()]); + + assert_eq!( + Supersig::get_proposals(supersig_id), + (vec![second_proposal.clone(), first_proposal.clone()], 3) + ); + assert_ok!(Supersig::approve_call( + Origin::signed(ALICE()), + supersig_account.clone(), + call_id + )); + assert_eq!( + Supersig::get_proposals(0), + (vec![first_proposal.clone()], 3) + ); + }) +} - assert_eq!(Supersig::members(1, ALICE()), Role::Master); - assert_eq!(Supersig::members(1, BOB()), Role::Master); - assert_eq!(Supersig::members(1, CHARLIE()), Role::Standard); - assert_eq!(Supersig::total_members(1), 3); - assert_eq!(Supersig::get_members_connected_to_supersig(1), vec![(ALICE(), Role::Master), (BOB(), Role::Master), (CHARLIE(), Role::Standard)]); +#[test] +fn get_proposal_state() { + ExtBuilder::default().balances(vec![]).build().execute_with(|| { + let mut call_id = 0; + let supersig_id = 0; + let call = Call::Nothing( + NoCall::do_nothing { + nothing: "test".into() + }); + let supersig_account = create_supersig(supersig_id); - let members3 = vec![(ALICE(), Role::Standard), (BOB(), Role::Standard)].try_into().unwrap(); - assert_ok!(Supersig::create_supersig(Origin::signed(BOB()), members3,)); - let supersig_account3 = get_supersig_account(2); - assert_ok!(Balances::transfer( + assert_ok!(Supersig::submit_call( Origin::signed(ALICE()), - supersig_account3.clone(), - 100_000 + supersig_account.clone(), + Box::new(call.clone()) )); - assert_ok!(Supersig::add_members( - Origin::signed(supersig_account3.clone()), - vec!((BOB(), Role::Master), (CHARLIE(), Role::Standard)).try_into().unwrap() + assert_eq!( + Supersig::get_proposal_state(supersig_id, call_id), + (true, vec![], 3, 0) + ); + assert_ok!(Supersig::approve_call( + Origin::signed(BOB()), + supersig_account.clone(), + call_id )); + assert_eq!( + Supersig::get_proposal_state(supersig_id, call_id), + (true, vec![BOB()], 3, 1) + ); + call_id += 1; - assert_eq!(Supersig::members(2, ALICE()), Role::Standard); - assert_eq!(Supersig::members(2, BOB()), Role::Master); - assert_eq!(Supersig::members(2, CHARLIE()), Role::Standard); - assert_eq!(Supersig::total_members(2), 3); - assert_eq!(Supersig::get_members_connected_to_supersig(2), vec![(ALICE(), Role::Standard), (BOB(), Role::Master), (CHARLIE(), Role::Standard)]); + assert_ok!(Supersig::submit_call( + Origin::signed(ALICE()), + supersig_account.clone(), + Box::new(call.clone()) + )); + assert_eq!( + Supersig::get_proposal_state(supersig_id, call_id), + (true, vec![], 3, 0) + ); + assert_ok!(Supersig::approve_call( + Origin::signed(BOB()), + supersig_account.clone(), + call_id + )); + assert_eq!( + Supersig::get_proposal_state(supersig_id, call_id), + (true, vec![BOB()], 3, 1) + ); + assert_ok!(Supersig::approve_call( + Origin::signed(ALICE()), + supersig_account.clone(), + call_id + )); + assert_eq!( + Supersig::get_proposal_state(supersig_id, call_id), + (false, vec![], 3, 0) + ); }) } \ No newline at end of file From 5327f63910b86f444767c4017bc915ac5756db65 Mon Sep 17 00:00:00 2001 From: roduquen Date: Tue, 12 Jul 2022 19:01:56 +0200 Subject: [PATCH 2/9] adding some com --- src/lib.rs | 12 ++++++++++++ src/tests/rpc_calls.rs | 9 +++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4d61517..8468948 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -771,6 +771,12 @@ impl Pallet { Members::::iter_prefix(which).collect() } + // Return : + // Tuple : + // Vec<((Vec, T::AccountId, BalanceOf), Vec)> : + // The tuple inside the vec is just a Call that is unwrap. + // The vec inside the vec is all the account id that have voted. + // The second parameter of the tuple is the total amount of members into the supersig. pub fn get_proposals(which: SupersigId) -> (Vec<((Vec, T::AccountId, BalanceOf), Vec)>, u32) { let member_count = Self::total_members(which); let proposal_state = Calls::::iter_prefix(which) @@ -789,6 +795,12 @@ impl Pallet { (proposal_state, member_count) } + // Return : + // Tuple : + // The bool is to define if the Call that is asked for state still exists. + // The vec is all the account id that have voted. + // The first u32 is the total amount of members in the supersig + // The second u32 is the total number of votes (not necessary because == members_votes.len()) pub fn get_proposal_state(which: SupersigId, call_id: CallId) -> (bool, Vec, u32, u32) { let member_count = Self::total_members(which); let votes = Self::votes(which, call_id); diff --git a/src/tests/rpc_calls.rs b/src/tests/rpc_calls.rs index aff0bac..194a67f 100644 --- a/src/tests/rpc_calls.rs +++ b/src/tests/rpc_calls.rs @@ -82,7 +82,7 @@ fn get_members_connected() { vec!((ALICE(), Role::Standard), (CHARLIE(), Role::Standard)).try_into().unwrap() )); assert_eq!( - Supersig::get_members_connected(2), + Supersig::get_members_connected(supersig_count), vec![(ALICE(), Role::Standard), (BOB(), Role::Standard), (CHARLIE(), Role::Standard)] ); }) @@ -146,8 +146,10 @@ fn get_proposals() { supersig_account.clone(), call_id )); + // Here the expected behaviour is : + // Proposal has been deleted because 2 of 3 voted. assert_eq!( - Supersig::get_proposals(0), + Supersig::get_proposals(supersig_id), (vec![first_proposal.clone()], 3) ); }) @@ -202,11 +204,14 @@ fn get_proposal_state() { Supersig::get_proposal_state(supersig_id, call_id), (true, vec![BOB()], 3, 1) ); + assert_ok!(Supersig::approve_call( Origin::signed(ALICE()), supersig_account.clone(), call_id )); + // Here the expected behaviour is : + // Proposal has been deleted because 2 of 3 voted. assert_eq!( Supersig::get_proposal_state(supersig_id, call_id), (false, vec![], 3, 0) From d69f0c329b9dced3b56c8919cce8f5acfa7a0737 Mon Sep 17 00:00:00 2001 From: roduquen Date: Tue, 12 Jul 2022 19:10:39 +0200 Subject: [PATCH 3/9] format --- src/lib.rs | 57 ++++++++++++++++++++------------ src/tests/mod.rs | 2 +- src/tests/rpc_calls.rs | 75 ++++++++++++++++++++++++++---------------- 3 files changed, 84 insertions(+), 50 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 93501a1..8a3e826 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -777,21 +777,31 @@ impl Pallet { // The tuple inside the vec is just a Call that is unwrap. // The vec inside the vec is all the account id that have voted. // The second parameter of the tuple is the total amount of members into the supersig. - pub fn get_proposals(which: SupersigId) -> (Vec<((Vec, T::AccountId, BalanceOf), Vec)>, u32) { + pub fn get_proposals( + which: SupersigId, + ) -> ( + Vec<((Vec, T::AccountId, BalanceOf), Vec)>, + u32, + ) { let member_count = Self::total_members(which); let proposal_state = Calls::::iter_prefix(which) - .map(|(call_id, call)| ( - (call.data, call.provider, call.deposit), - MembersVotes::::iter_prefix((which, call_id)) - .filter_map(|(account_id, vote)| { - if vote { - Some(account_id) - } else { - None - } - }) - .collect() - )).collect(); + .map(|(call_id, call)| { + ( + (call.data, call.provider, call.deposit), + MembersVotes::::iter_prefix((which, call_id)) + .filter_map( + |(account_id, vote)| { + if vote { + Some(account_id) + } else { + None + } + }, + ) + .collect(), + ) + }) + .collect(); (proposal_state, member_count) } @@ -801,18 +811,23 @@ impl Pallet { // The vec is all the account id that have voted. // The first u32 is the total amount of members in the supersig // The second u32 is the total number of votes (not necessary because == members_votes.len()) - pub fn get_proposal_state(which: SupersigId, call_id: CallId) -> (bool, Vec, u32, u32) { + pub fn get_proposal_state( + which: SupersigId, + call_id: CallId, + ) -> (bool, Vec, u32, u32) { let member_count = Self::total_members(which); let votes = Self::votes(which, call_id); let exists = !Self::calls(which, call_id).is_none(); let member_votes = MembersVotes::::iter_prefix((which, call_id)) - .filter_map(|(account_id, vote)| { - if vote { - Some(account_id) - } else { - None - } - }) + .filter_map( + |(account_id, vote)| { + if vote { + Some(account_id) + } else { + None + } + }, + ) .collect(); (exists, member_votes, member_count, votes) } diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 4d33c44..5cb1ea7 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -5,8 +5,8 @@ mod delete_supersig; mod leave_supersig; mod remove_call; mod remove_members; -mod submit_call; mod rpc_calls; +mod submit_call; pub mod helper; pub mod mock; diff --git a/src/tests/rpc_calls.rs b/src/tests/rpc_calls.rs index 194a67f..bc8e758 100644 --- a/src/tests/rpc_calls.rs +++ b/src/tests/rpc_calls.rs @@ -1,9 +1,9 @@ use super::{helper::*, mock::*}; -use crate::{Role}; -use frame_support::{ assert_ok }; +use crate::Role; +use frame_support::assert_ok; pub use sp_std::{boxed::Box, mem::size_of}; -fn create_supersig(supersig_id : u128) -> sp_runtime::AccountId32 { +fn create_supersig(supersig_id: u128) -> sp_runtime::AccountId32 { let creator = vec![(ALICE(), Role::Master)].try_into().unwrap(); assert_ok!(Supersig::create_supersig(Origin::signed(ALICE()), creator,)); let supersig_account = get_supersig_account(u64::try_from(supersig_id).unwrap()); @@ -26,41 +26,48 @@ fn create_supersig(supersig_id : u128) -> sp_runtime::AccountId32 { #[test] fn get_account_supersigs() { ExtBuilder::default().balances(vec![]).build().execute_with(|| { - let mut supersig_count : u128 = 0; + let mut supersig_count: u128 = 0; - assert_eq!(Supersig::get_account_supersigs(ALICE()), vec![]); + assert_eq!(Supersig::get_account_supersigs(ALICE()), vec![]); create_supersig(supersig_count); - assert_eq!(Supersig::get_account_supersigs(ALICE()), vec![0]); + assert_eq!(Supersig::get_account_supersigs(ALICE()), vec![0]); supersig_count += 1; create_supersig(supersig_count); - assert_eq!(Supersig::get_account_supersigs(ALICE()), vec![1, 0]); + assert_eq!(Supersig::get_account_supersigs(ALICE()), vec![1, 0]); supersig_count += 1; create_supersig(supersig_count); - assert_eq!(Supersig::get_account_supersigs(ALICE()), vec![1, 0, 2]); + assert_eq!(Supersig::get_account_supersigs(ALICE()), vec![1, 0, 2]); supersig_count += 1; create_supersig(supersig_count); - assert_eq!(Supersig::get_account_supersigs(ALICE()), vec![1, 0, 3, 2]); + assert_eq!(Supersig::get_account_supersigs(ALICE()), vec![1, 0, 3, 2]); supersig_count += 1; create_supersig(supersig_count); - assert_eq!(Supersig::get_account_supersigs(ALICE()), vec![1, 0, 3, 4, 2]); + assert_eq!( + Supersig::get_account_supersigs(ALICE()), + vec![1, 0, 3, 4, 2] + ); }) } #[test] fn get_members_connected() { ExtBuilder::default().balances(vec![]).build().execute_with(|| { - let mut supersig_count : u128 = 0; - let mut supersig_account : sp_runtime::AccountId32; + let mut supersig_count: u128 = 0; + let mut supersig_account: sp_runtime::AccountId32; create_supersig(supersig_count); - assert_eq!( + assert_eq!( Supersig::get_members_connected(supersig_count), - vec![(ALICE(), Role::Master), (BOB(), Role::Standard), (CHARLIE(), Role::Standard)] + vec![ + (ALICE(), Role::Master), + (BOB(), Role::Standard), + (CHARLIE(), Role::Standard) + ] ); supersig_count += 1; @@ -70,9 +77,13 @@ fn get_members_connected() { vec!((BOB(), Role::Master), (CHARLIE(), Role::Standard)).try_into().unwrap() )); assert_eq!(Supersig::total_members(supersig_count), 3); - assert_eq!( + assert_eq!( Supersig::get_members_connected(supersig_count), - vec![(ALICE(), Role::Master), (BOB(), Role::Master), (CHARLIE(), Role::Standard)] + vec![ + (ALICE(), Role::Master), + (BOB(), Role::Master), + (CHARLIE(), Role::Standard) + ] ); supersig_count += 1; @@ -81,9 +92,13 @@ fn get_members_connected() { Origin::signed(supersig_account), vec!((ALICE(), Role::Standard), (CHARLIE(), Role::Standard)).try_into().unwrap() )); - assert_eq!( + assert_eq!( Supersig::get_members_connected(supersig_count), - vec![(ALICE(), Role::Standard), (BOB(), Role::Standard), (CHARLIE(), Role::Standard)] + vec![ + (ALICE(), Role::Standard), + (BOB(), Role::Standard), + (CHARLIE(), Role::Standard) + ] ); }) } @@ -93,10 +108,9 @@ fn get_proposals() { ExtBuilder::default().balances(vec![]).build().execute_with(|| { let mut call_id = 0; let supersig_id = 0; - let call = Call::Nothing( - NoCall::do_nothing { - nothing: "test".into() - }); + let call = Call::Nothing(NoCall::do_nothing { + nothing: "test".into(), + }); let data = vec![2, 0, 16, 116, 101, 115, 116]; let supersig_account = create_supersig(supersig_id); @@ -128,7 +142,13 @@ fn get_proposals() { )); assert_eq!( Supersig::get_proposals(supersig_id), - (vec![((data.clone(), ALICE(), 7000), vec![]), first_proposal.clone()], 3) + ( + vec![ + ((data.clone(), ALICE(), 7000), vec![]), + first_proposal.clone() + ], + 3 + ) ); assert_ok!(Supersig::approve_call( Origin::signed(BOB()), @@ -160,10 +180,9 @@ fn get_proposal_state() { ExtBuilder::default().balances(vec![]).build().execute_with(|| { let mut call_id = 0; let supersig_id = 0; - let call = Call::Nothing( - NoCall::do_nothing { - nothing: "test".into() - }); + let call = Call::Nothing(NoCall::do_nothing { + nothing: "test".into(), + }); let supersig_account = create_supersig(supersig_id); assert_ok!(Supersig::submit_call( @@ -217,4 +236,4 @@ fn get_proposal_state() { (false, vec![], 3, 0) ); }) -} \ No newline at end of file +} From 8ab55c5ce7148ccf9de9d68758eaa1eee65ad1f8 Mon Sep 17 00:00:00 2001 From: roduquen Date: Tue, 12 Jul 2022 19:15:25 +0200 Subject: [PATCH 4/9] test format --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 8a3e826..c42b195 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -250,7 +250,7 @@ pub mod pallet { // A supersig should at least have one member let member_length = members.len(); if member_length < 1 { - return Err(Error::::InvalidNumberOfMembers.into()); + return Err(Error::::InvalidNumberOfMembers.into()) } // Get it id and associated account From 8d9bd86bb67f520cde2e2725181f8dec5576492f Mon Sep 17 00:00:00 2001 From: roduquen Date: Tue, 12 Jul 2022 19:20:35 +0200 Subject: [PATCH 5/9] format --- src/lib.rs | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c42b195..14eeb8e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -352,10 +352,10 @@ pub mod pallet { let supersig_id = Self::get_supersig_id_from_account(&supersig_account)?; if Self::calls(supersig_id, call_id).is_none() { - return Err(Error::::CallNotFound.into()); + return Err(Error::::CallNotFound.into()) } if Self::members_votes((supersig_id, call_id, who.clone())) { - return Err(Error::::AlreadyVoted.into()); + return Err(Error::::AlreadyVoted.into()) } // Different roles have different voting weight @@ -427,7 +427,7 @@ pub mod pallet { // Either the supersig or the user that created the vote can remove a call if who != supersig_account && who != preimage.provider { - return Err(Error::::NotAllowed.into()); + return Err(Error::::NotAllowed.into()) } // Clean up storage and release reserved funds @@ -569,7 +569,7 @@ pub mod pallet { let supersig_id = Self::get_supersig_id_from_account(&supersig_account)?; if Self::members(supersig_id, &who) == Role::NotMember { - return Err(Error::::NotMember.into()); + return Err(Error::::NotMember.into()) } // Remeber the storage state before we remove the members from it @@ -586,7 +586,7 @@ pub mod pallet { // A supersig should at least have one member TotalMembers::::try_mutate(supersig_id, |nb| { if *nb == 1 { - return Err(Error::::InvalidNumberOfMembers); + return Err(Error::::InvalidNumberOfMembers) }; *nb -= 1; Ok(()) @@ -610,7 +610,7 @@ pub mod pallet { ) -> Result> { if let Some((account, supersig_id)) = PalletId::try_from_sub_account(supersig_account) { if account != T::PalletId::get() || Self::total_members(supersig_id) == 0 { - return Err(Error::::NotSupersig); + return Err(Error::::NotSupersig) } Ok(supersig_id) } else { @@ -679,7 +679,7 @@ pub mod pallet { let new_total_members = n.saturating_sub(removed.len().try_into().map_err(|_| Error::::Conversion)?); if new_total_members < 1 { - return Err(Error::::InvalidNumberOfMembers); + return Err(Error::::InvalidNumberOfMembers) } *n = new_total_members; @@ -791,11 +791,7 @@ impl Pallet { MembersVotes::::iter_prefix((which, call_id)) .filter_map( |(account_id, vote)| { - if vote { - Some(account_id) - } else { - None - } + if vote { Some(account_id) } else { None } }, ) .collect(), @@ -821,11 +817,7 @@ impl Pallet { let member_votes = MembersVotes::::iter_prefix((which, call_id)) .filter_map( |(account_id, vote)| { - if vote { - Some(account_id) - } else { - None - } + if vote { Some(account_id) } else { None } }, ) .collect(); From 224d4dbee7b6e41b0d3ff9a7d322ef4727c76d67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Delabrouille?= Date: Mon, 25 Jul 2022 23:01:34 +0200 Subject: [PATCH 6/9] feat: finish rpc calls --- Cargo.toml | 2 +- rpc/Cargo.toml | 1 + rpc/runtime-api/Cargo.toml | 6 +- rpc/runtime-api/src/lib.rs | 11 ++- rpc/src/lib.rs | 94 ++++++++++++++++--- src/lib.rs | 77 +-------------- src/rpc.rs | 120 ++++++++++++++++++++++++ src/tests/rpc_calls.rs | 187 +++++++++++-------------------------- src/types.rs | 13 ++- 9 files changed, 284 insertions(+), 227 deletions(-) create mode 100644 src/rpc.rs diff --git a/Cargo.toml b/Cargo.toml index 2698ebd..d8499f4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ edition = "2021" targets = ["x86_64-unknown-linux-gnu"] [dependencies] +serde = { version = "1.0.132", default-features = false, features = ["derive"] } codec = { package = "parity-scale-codec", version = "3.0.0", features = [ "derive", ], default-features = false } @@ -27,7 +28,6 @@ sp-std = { git = "https://github.com/paritytech/substrate", default-features = f sp-core = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.24" } [dev-dependencies] -serde = { version = "1.0.132" } sp-io = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.24" } pallet-balances = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.24" } diff --git a/rpc/Cargo.toml b/rpc/Cargo.toml index f33d154..902fc36 100644 --- a/rpc/Cargo.toml +++ b/rpc/Cargo.toml @@ -21,3 +21,4 @@ sp-std = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate sp-blockchain = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.24" } pallet-supersig-rpc-runtime-api = { path = "./runtime-api" } +pallet-supersig = { path = ".." } diff --git a/rpc/runtime-api/Cargo.toml b/rpc/runtime-api/Cargo.toml index 849beb1..9979f97 100644 --- a/rpc/runtime-api/Cargo.toml +++ b/rpc/runtime-api/Cargo.toml @@ -14,8 +14,10 @@ scale-info = { version = "2.1.1", default-features = false, features = [ "derive", ] } -sp-api = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.24" , version = "4.0.0-dev"} -sp-std = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.24" , version = "4.0.0-dev"} +sp-api = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.24", version = "4.0.0-dev" } +sp-std = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.24", version = "4.0.0-dev" } + +pallet-supersig = { path = "../.." } [features] default = ["std"] diff --git a/rpc/runtime-api/src/lib.rs b/rpc/runtime-api/src/lib.rs index cc0cbde..2cb8a6c 100644 --- a/rpc/runtime-api/src/lib.rs +++ b/rpc/runtime-api/src/lib.rs @@ -4,11 +4,18 @@ use codec::Codec; #[cfg(not(feature = "std"))] use sp_std::prelude::Vec; +use pallet_supersig::{rpc::ProposalState, CallId, Role, SupersigId}; + sp_api::decl_runtime_apis! { - pub trait SuperSigApi + pub trait SuperSigApi where AccountId: Codec, + Call: Codec, { - fn get_account_supersigs(origin: AccountId) -> Vec; + fn get_supersig_id(supersig_account: AccountId) -> Option; + fn get_user_supersigs(who: AccountId) -> Vec; + fn list_members(supersig_id: SupersigId) -> Vec<(AccountId, Role)>; + fn list_proposals(supersig_id: SupersigId) -> (Vec>, u32); + fn get_proposal_state(supersig_id: SupersigId, call_id: CallId) -> Option<(ProposalState, u32)>; } } diff --git a/rpc/src/lib.rs b/rpc/src/lib.rs index bb22fd8..b53553b 100644 --- a/rpc/src/lib.rs +++ b/rpc/src/lib.rs @@ -4,18 +4,47 @@ use jsonrpsee::{ proc_macros::rpc, types::error::{CallError, ErrorObject}, }; -use sp_api::BlockId; -use sp_api::BlockT; -use sp_api::ProvideRuntimeApi; +use sp_api::{BlockId, BlockT, ProvideRuntimeApi}; use sp_blockchain::HeaderBackend; use std::{marker::PhantomData, sync::Arc}; pub use pallet_supersig_rpc_runtime_api::SuperSigApi as SuperSigRuntimeApi; +use pallet_supersig::{rpc::ProposalState, CallId, Role, SupersigId}; + #[rpc(client, server)] -pub trait SuperSigApi { - #[method(name = "superSig_getAccountSupersigs")] - fn get_account_supersigs(&self, who: AccountId, at: Option) -> RpcResult>; +pub trait SuperSigApi { + #[method(name = "superSig_getSupersigId")] + fn get_supersig_id( + &self, + supersig_account: AccountId, + at: Option, + ) -> RpcResult>; + #[method(name = "superSig_getUserSupersigs")] + fn get_user_supersigs( + &self, + who: AccountId, + at: Option, + ) -> RpcResult>; + #[method(name = "superSig_listMembers")] + fn list_members( + &self, + supersig_id: SupersigId, + at: Option, + ) -> RpcResult>; + #[method(name = "superSig_listProposals")] + fn list_proposals( + &self, + supersig_id: SupersigId, + at: Option, + ) -> RpcResult<(Vec>, u32)>; + #[method(name = "superSig_getProposalState")] + fn get_proposal_state( + &self, + supersig_id: SupersigId, + call_id: CallId, + at: Option, + ) -> RpcResult, u32)>>; } /// SuperSig RPC methods. @@ -34,22 +63,65 @@ impl SuperSig { } } -impl SuperSigApiServer<::Hash, AccountId> +impl SuperSigApiServer<::Hash, AccountId, Call> for SuperSig where Block: BlockT, Client: Send + Sync + 'static + ProvideRuntimeApi + HeaderBackend, - Client::Api: SuperSigRuntimeApi, + Client::Api: SuperSigRuntimeApi, AccountId: Codec, + Call: Codec, { - fn get_account_supersigs( + fn get_supersig_id( + &self, + supersig_account: AccountId, + at: Option<::Hash>, + ) -> RpcResult> { + let api = self.client.runtime_api(); + let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); + api.get_supersig_id(&at, supersig_account).map_err(runtime_error_into_rpc_err) + } + + fn get_user_supersigs( &self, who: AccountId, at: Option<::Hash>, - ) -> RpcResult> { + ) -> RpcResult> { + let api = self.client.runtime_api(); + let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); + api.get_user_supersigs(&at, who).map_err(runtime_error_into_rpc_err) + } + + fn list_members( + &self, + supersig_id: SupersigId, + at: Option<::Hash>, + ) -> RpcResult> { + let api = self.client.runtime_api(); + let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); + api.list_members(&at, supersig_id).map_err(runtime_error_into_rpc_err) + } + + fn list_proposals( + &self, + supersig_id: SupersigId, + at: Option<::Hash>, + ) -> RpcResult<(Vec>, u32)> { + let api = self.client.runtime_api(); + let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); + api.list_proposals(&at, supersig_id).map_err(runtime_error_into_rpc_err) + } + + fn get_proposal_state( + &self, + supersig_id: SupersigId, + call_id: CallId, + at: Option<::Hash>, + ) -> RpcResult, u32)>> { let api = self.client.runtime_api(); let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); - api.get_account_supersigs(&at, who).map_err(runtime_error_into_rpc_err) + api.get_proposal_state(&at, supersig_id, call_id) + .map_err(runtime_error_into_rpc_err) } } diff --git a/src/lib.rs b/src/lib.rs index 14eeb8e..07a4171 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -75,6 +75,7 @@ pub use sp_runtime::traits::{ }; pub use sp_std::{boxed::Box, cmp::max, mem::size_of, prelude::Vec}; +pub mod rpc; pub mod types; pub mod weights; @@ -605,7 +606,7 @@ pub mod pallet { } impl Pallet { - fn get_supersig_id_from_account( + pub fn get_supersig_id_from_account( supersig_account: &T::AccountId, ) -> Result> { if let Some((account, supersig_id)) = PalletId::try_from_sub_account(supersig_account) { @@ -750,77 +751,3 @@ pub mod pallet { } } } - -// RPC calls - -#[allow(dead_code)] -impl Pallet { - pub fn get_account_supersigs(who: T::AccountId) -> Vec { - Members::::iter() - .filter_map(|(supersig_id, member_id, _)| { - if member_id == who { - Some(supersig_id) - } else { - None - } - }) - .collect() - } - - pub fn get_members_connected(which: SupersigId) -> Vec<(T::AccountId, Role)> { - Members::::iter_prefix(which).collect() - } - - // Return : - // Tuple : - // Vec<((Vec, T::AccountId, BalanceOf), Vec)> : - // The tuple inside the vec is just a Call that is unwrap. - // The vec inside the vec is all the account id that have voted. - // The second parameter of the tuple is the total amount of members into the supersig. - pub fn get_proposals( - which: SupersigId, - ) -> ( - Vec<((Vec, T::AccountId, BalanceOf), Vec)>, - u32, - ) { - let member_count = Self::total_members(which); - let proposal_state = Calls::::iter_prefix(which) - .map(|(call_id, call)| { - ( - (call.data, call.provider, call.deposit), - MembersVotes::::iter_prefix((which, call_id)) - .filter_map( - |(account_id, vote)| { - if vote { Some(account_id) } else { None } - }, - ) - .collect(), - ) - }) - .collect(); - (proposal_state, member_count) - } - - // Return : - // Tuple : - // The bool is to define if the Call that is asked for state still exists. - // The vec is all the account id that have voted. - // The first u32 is the total amount of members in the supersig - // The second u32 is the total number of votes (not necessary because == members_votes.len()) - pub fn get_proposal_state( - which: SupersigId, - call_id: CallId, - ) -> (bool, Vec, u32, u32) { - let member_count = Self::total_members(which); - let votes = Self::votes(which, call_id); - let exists = !Self::calls(which, call_id).is_none(); - let member_votes = MembersVotes::::iter_prefix((which, call_id)) - .filter_map( - |(account_id, vote)| { - if vote { Some(account_id) } else { None } - }, - ) - .collect(); - (exists, member_votes, member_count, votes) - } -} diff --git a/src/rpc.rs b/src/rpc.rs new file mode 100644 index 0000000..1fda0f5 --- /dev/null +++ b/src/rpc.rs @@ -0,0 +1,120 @@ +use codec::Decode; +use frame_support::Parameter; +pub use sp_std::{boxed::Box, cmp::max, mem::size_of, prelude::Vec}; + +use crate::{ + pallet, CallId, Calls, Config, Error, Members, MembersVotes, Pallet, Role, SupersigId, +}; + +#[derive(Debug, Clone, PartialEq, Eq, Decode, serde::Serialize, serde::Deserialize)] +pub struct ProposalState { + id: CallId, + call: Call, + provider: AccountId, + voters: Vec, +} + +impl ProposalState { + pub fn new(id: CallId, call: Call, provider: AccoutId, voters: Vec) -> Self { + Self { + id, + call, + provider, + voters, + } + } + + pub fn id(&self) -> &CallId { + &self.id + } + + pub fn call(&self) -> &Call { + &self.call + } + + pub fn provider(&self) -> &AccoutId { + &self.provider + } + + pub fn voters(&self) -> &Vec { + &self.voters + } +} + +impl Pallet { + pub fn get_supersig_id(supersig_account: &T::AccountId) -> Result> { + Self::get_supersig_id_from_account(supersig_account) + } + + pub fn get_user_supersigs(who: T::AccountId) -> Vec { + Members::::iter() + .filter_map(|(supersig_id, member_id, _)| { + if member_id == who { + Some(supersig_id) + } else { + None + } + }) + .collect() + } + + pub fn list_members(supersig_id: SupersigId) -> Vec<(T::AccountId, Role)> { + Members::::iter_prefix(supersig_id).collect() + } + + pub fn list_proposals( + supersig_id: SupersigId, + ) -> ( + Vec::Call>>, + u32, + ) { + let member_count = Self::total_members(supersig_id); + let proposal_state = Calls::::iter_prefix(supersig_id) + .map(|(call_id, call)| { + let voters = MembersVotes::::iter_prefix((supersig_id, call_id)) + .filter_map( + |(account_id, vote)| { + if vote { Some(account_id) } else { None } + }, + ) + .collect(); + + ProposalState::new( + call_id, + ::Call::decode(&mut &call.data[..]).unwrap(), + call.provider, + voters, + ) + }) + .collect(); + (proposal_state, member_count) + } + + pub fn get_proposal_state( + supersig_id: SupersigId, + call_id: CallId, + ) -> Option<( + ProposalState::Call>, + u32, + )> { + let call = Self::calls(supersig_id, call_id)?; + let member_count = Self::total_members(supersig_id); + let voters = MembersVotes::::iter_prefix((supersig_id, call_id)) + .filter_map( + |(account_id, vote)| { + if vote { Some(account_id) } else { None } + }, + ) + .collect(); + + Some(( + ProposalState::new( + call_id, + ::Call::decode(&mut &call.data[..]).unwrap(), + call.provider, + voters, + ), + member_count, + )) + } +} diff --git a/src/tests/rpc_calls.rs b/src/tests/rpc_calls.rs index bc8e758..1e34ba6 100644 --- a/src/tests/rpc_calls.rs +++ b/src/tests/rpc_calls.rs @@ -1,5 +1,5 @@ use super::{helper::*, mock::*}; -use crate::Role; +use crate::{rpc::ProposalState, Role}; use frame_support::assert_ok; pub use sp_std::{boxed::Box, mem::size_of}; @@ -28,149 +28,84 @@ fn get_account_supersigs() { ExtBuilder::default().balances(vec![]).build().execute_with(|| { let mut supersig_count: u128 = 0; - assert_eq!(Supersig::get_account_supersigs(ALICE()), vec![]); + assert_eq!(Supersig::get_user_supersigs(ALICE()), vec![]); create_supersig(supersig_count); - assert_eq!(Supersig::get_account_supersigs(ALICE()), vec![0]); + let alice_supersigs = Supersig::get_user_supersigs(ALICE()); + assert!(alice_supersigs.contains(&0)); supersig_count += 1; create_supersig(supersig_count); - assert_eq!(Supersig::get_account_supersigs(ALICE()), vec![1, 0]); + let alice_supersigs = Supersig::get_user_supersigs(ALICE()); + assert!(alice_supersigs.contains(&0)); + assert!(alice_supersigs.contains(&1)); supersig_count += 1; create_supersig(supersig_count); - assert_eq!(Supersig::get_account_supersigs(ALICE()), vec![1, 0, 2]); - supersig_count += 1; - - create_supersig(supersig_count); - assert_eq!(Supersig::get_account_supersigs(ALICE()), vec![1, 0, 3, 2]); - supersig_count += 1; - - create_supersig(supersig_count); - assert_eq!( - Supersig::get_account_supersigs(ALICE()), - vec![1, 0, 3, 4, 2] - ); + let alice_supersigs = Supersig::get_user_supersigs(ALICE()); + assert!(alice_supersigs.contains(&0)); + assert!(alice_supersigs.contains(&1)); + assert!(alice_supersigs.contains(&2)); }) } #[test] -fn get_members_connected() { +fn list_members() { ExtBuilder::default().balances(vec![]).build().execute_with(|| { - let mut supersig_count: u128 = 0; - let mut supersig_account: sp_runtime::AccountId32; - - create_supersig(supersig_count); - assert_eq!( - Supersig::get_members_connected(supersig_count), - vec![ - (ALICE(), Role::Master), - (BOB(), Role::Standard), - (CHARLIE(), Role::Standard) - ] - ); - supersig_count += 1; - - supersig_account = create_supersig(supersig_count); - assert_ok!(Supersig::add_members( - Origin::signed(supersig_account), - vec!((BOB(), Role::Master), (CHARLIE(), Role::Standard)).try_into().unwrap() - )); - assert_eq!(Supersig::total_members(supersig_count), 3); - assert_eq!( - Supersig::get_members_connected(supersig_count), - vec![ - (ALICE(), Role::Master), - (BOB(), Role::Master), - (CHARLIE(), Role::Standard) - ] - ); - supersig_count += 1; - - supersig_account = create_supersig(supersig_count); - assert_ok!(Supersig::add_members( - Origin::signed(supersig_account), - vec!((ALICE(), Role::Standard), (CHARLIE(), Role::Standard)).try_into().unwrap() - )); - assert_eq!( - Supersig::get_members_connected(supersig_count), - vec![ - (ALICE(), Role::Standard), - (BOB(), Role::Standard), - (CHARLIE(), Role::Standard) - ] - ); + create_supersig(0); + assert!(Supersig::list_members(0).contains(&(ALICE(), Role::Master))); + assert!(Supersig::list_members(0).contains(&(BOB(), Role::Standard))); + assert!(Supersig::list_members(0).contains(&(CHARLIE(), Role::Standard))); }) } #[test] fn get_proposals() { ExtBuilder::default().balances(vec![]).build().execute_with(|| { - let mut call_id = 0; - let supersig_id = 0; + let supersig_account = create_supersig(0); let call = Call::Nothing(NoCall::do_nothing { nothing: "test".into(), }); - let data = vec![2, 0, 16, 116, 101, 115, 116]; - let supersig_account = create_supersig(supersig_id); assert_ok!(Supersig::submit_call( Origin::signed(ALICE()), supersig_account.clone(), Box::new(call.clone()) )); - assert_eq!( - Supersig::get_proposals(supersig_id), - (vec![((data.clone(), ALICE(), 7000), vec![])], 3) - ); - assert_ok!(Supersig::approve_call( - Origin::signed(BOB()), - supersig_account.clone(), - call_id - )); - let first_proposal = ((data.clone(), ALICE(), 7000), vec![BOB()]); - assert_eq!( - Supersig::get_proposals(supersig_id), - (vec![first_proposal.clone()], 3) - ); - call_id += 1; assert_ok!(Supersig::submit_call( Origin::signed(ALICE()), supersig_account.clone(), Box::new(call.clone()) )); - assert_eq!( - Supersig::get_proposals(supersig_id), - ( - vec![ - ((data.clone(), ALICE(), 7000), vec![]), - first_proposal.clone() - ], - 3 - ) - ); + assert_ok!(Supersig::approve_call( Origin::signed(BOB()), supersig_account.clone(), - call_id + 0, )); - let second_proposal = ((data.clone(), ALICE(), 7000), vec![BOB()]); - assert_eq!( - Supersig::get_proposals(supersig_id), - (vec![second_proposal.clone(), first_proposal.clone()], 3) - ); + assert_ok!(Supersig::approve_call( + Origin::signed(BOB()), + supersig_account.clone(), + 1, + )); + + let list = Supersig::list_proposals(0); + assert_eq!(list.1, 3); + assert_eq!(list.0.len(), 2); + assert!(list.0.contains(&ProposalState::new(0, call.clone(), ALICE(), vec![BOB()]))); + assert!(list.0.contains(&ProposalState::new(1, call.clone(), ALICE(), vec![BOB()]))); + assert_ok!(Supersig::approve_call( Origin::signed(ALICE()), supersig_account.clone(), - call_id + 1, )); - // Here the expected behaviour is : - // Proposal has been deleted because 2 of 3 voted. + assert_eq!( - Supersig::get_proposals(supersig_id), - (vec![first_proposal.clone()], 3) + Supersig::list_proposals(0), + (vec![ProposalState::new(0, call, ALICE(), vec![BOB()])], 3) ); }) } @@ -178,62 +113,44 @@ fn get_proposals() { #[test] fn get_proposal_state() { ExtBuilder::default().balances(vec![]).build().execute_with(|| { - let mut call_id = 0; - let supersig_id = 0; + let supersig_account = create_supersig(0); let call = Call::Nothing(NoCall::do_nothing { nothing: "test".into(), }); - let supersig_account = create_supersig(supersig_id); + + assert_eq!(Supersig::get_proposal_state(0, 0), None); assert_ok!(Supersig::submit_call( Origin::signed(ALICE()), supersig_account.clone(), Box::new(call.clone()) )); + assert_eq!( - Supersig::get_proposal_state(supersig_id, call_id), - (true, vec![], 3, 0) - ); - assert_ok!(Supersig::approve_call( - Origin::signed(BOB()), - supersig_account.clone(), - call_id - )); - assert_eq!( - Supersig::get_proposal_state(supersig_id, call_id), - (true, vec![BOB()], 3, 1) + Supersig::get_proposal_state(0, 0), + Some((ProposalState::new(0, call.clone(), ALICE(), vec![]), 3)) ); - call_id += 1; - assert_ok!(Supersig::submit_call( + assert_ok!(Supersig::approve_call( Origin::signed(ALICE()), supersig_account.clone(), - Box::new(call.clone()) + 0, )); + assert_eq!( - Supersig::get_proposal_state(supersig_id, call_id), - (true, vec![], 3, 0) + Supersig::get_proposal_state(0, 0), + Some(( + ProposalState::new(0, call.clone(), ALICE(), vec![ALICE()]), + 3 + )) ); + assert_ok!(Supersig::approve_call( Origin::signed(BOB()), supersig_account.clone(), - call_id + 0, )); - assert_eq!( - Supersig::get_proposal_state(supersig_id, call_id), - (true, vec![BOB()], 3, 1) - ); - assert_ok!(Supersig::approve_call( - Origin::signed(ALICE()), - supersig_account.clone(), - call_id - )); - // Here the expected behaviour is : - // Proposal has been deleted because 2 of 3 voted. - assert_eq!( - Supersig::get_proposal_state(supersig_id, call_id), - (false, vec![], 3, 0) - ); + assert_eq!(Supersig::get_proposal_state(0, 0), None); }) } diff --git a/src/types.rs b/src/types.rs index e777f54..94d2d34 100644 --- a/src/types.rs +++ b/src/types.rs @@ -6,7 +6,18 @@ use scale_info::TypeInfo; pub type BalanceOf = <::Currency as Currency<::AccountId>>::Balance; -#[derive(Clone, Encode, Decode, TypeInfo, PartialEq, Eq, MaxEncodedLen, Debug)] +#[derive( + Clone, + Encode, + Decode, + TypeInfo, + PartialEq, + Eq, + MaxEncodedLen, + Debug, + serde::Serialize, + serde::Deserialize, +)] #[codec(mel_bound())] pub enum Role { Standard, From f908d2721676fc1027776253daf480d7bc6a61fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Delabrouille?= Date: Wed, 27 Jul 2022 19:05:11 +0200 Subject: [PATCH 7/9] CI: don't fail on clippy warning --- .github/workflows/rust.yml | 2 +- src/tests/rpc_calls.rs | 9 +++------ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 2bfd016..39b43c2 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -18,7 +18,7 @@ jobs: - name: Build run: cargo build --verbose - name: Clippy - run: cargo clippy --bins --tests --examples --all -- -D warnings + run: cargo clippy --bins --tests --examples -- - name: Run tests run: cargo test diff --git a/src/tests/rpc_calls.rs b/src/tests/rpc_calls.rs index 1e34ba6..8a914af 100644 --- a/src/tests/rpc_calls.rs +++ b/src/tests/rpc_calls.rs @@ -99,7 +99,7 @@ fn get_proposals() { assert_ok!(Supersig::approve_call( Origin::signed(ALICE()), - supersig_account.clone(), + supersig_account, 1, )); @@ -139,15 +139,12 @@ fn get_proposal_state() { assert_eq!( Supersig::get_proposal_state(0, 0), - Some(( - ProposalState::new(0, call.clone(), ALICE(), vec![ALICE()]), - 3 - )) + Some((ProposalState::new(0, call, ALICE(), vec![ALICE()]), 3)) ); assert_ok!(Supersig::approve_call( Origin::signed(BOB()), - supersig_account.clone(), + supersig_account, 0, )); From 7485da5357b756b6119d181c9543df4cd72bee6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Delabrouille?= Date: Tue, 2 Aug 2022 21:02:08 +0200 Subject: [PATCH 8/9] fix: remove the Call from signature as they are not serializables --- Cargo.toml | 5 +++- rpc/Cargo.toml | 4 +-- rpc/runtime-api/Cargo.toml | 4 +-- rpc/runtime-api/src/lib.rs | 7 ++--- rpc/src/lib.rs | 15 +++++----- src/rpc.rs | 61 +++++++++++--------------------------- src/tests/rpc_calls.rs | 36 +++++++++++----------- src/types.rs | 14 ++------- 8 files changed, 56 insertions(+), 90 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d8499f4..8760afc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,9 @@ edition = "2021" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -serde = { version = "1.0.132", default-features = false, features = ["derive"] } +serde = { version = "1.0.132", default-features = false, features = [ + "derive", +], optional = true } codec = { package = "parity-scale-codec", version = "3.0.0", features = [ "derive", ], default-features = false } @@ -35,6 +37,7 @@ pallet-balances = { git = "https://github.com/paritytech/substrate", default-fea default = ["std", "runtime-benchmarks"] runtime-benchmarks = ["frame-benchmarking/runtime-benchmarks"] std = [ + "serde", "codec/std", "sp-std/std", "sp-core/std", diff --git a/rpc/Cargo.toml b/rpc/Cargo.toml index 902fc36..1e2a4ef 100644 --- a/rpc/Cargo.toml +++ b/rpc/Cargo.toml @@ -14,11 +14,11 @@ scale-info = { version = "2.1.1", default-features = false, features = [ "derive", ] } -jsonrpsee = { version = "0.14.0", features = ["server", "macros"] } +jsonrpsee = { version = "0.13.0", features = ["server", "macros"] } sp-api = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.24" } sp-std = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.24" } sp-blockchain = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.24" } -pallet-supersig-rpc-runtime-api = { path = "./runtime-api" } pallet-supersig = { path = ".." } +pallet-supersig-rpc-runtime-api = { path = "./runtime-api" } diff --git a/rpc/runtime-api/Cargo.toml b/rpc/runtime-api/Cargo.toml index 9979f97..68dab30 100644 --- a/rpc/runtime-api/Cargo.toml +++ b/rpc/runtime-api/Cargo.toml @@ -17,8 +17,8 @@ scale-info = { version = "2.1.1", default-features = false, features = [ sp-api = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.24", version = "4.0.0-dev" } sp-std = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.24", version = "4.0.0-dev" } -pallet-supersig = { path = "../.." } +pallet-supersig = { path = "../..", default-features = false, features = [] } [features] default = ["std"] -std = ["codec/std", "sp-std/std", "sp-api/std"] +std = ["codec/std", "sp-std/std", "sp-api/std", "pallet-supersig/std"] diff --git a/rpc/runtime-api/src/lib.rs b/rpc/runtime-api/src/lib.rs index 2cb8a6c..4e5ee6f 100644 --- a/rpc/runtime-api/src/lib.rs +++ b/rpc/runtime-api/src/lib.rs @@ -7,15 +7,14 @@ use sp_std::prelude::Vec; use pallet_supersig::{rpc::ProposalState, CallId, Role, SupersigId}; sp_api::decl_runtime_apis! { - pub trait SuperSigApi + pub trait SuperSigApi where AccountId: Codec, - Call: Codec, { fn get_supersig_id(supersig_account: AccountId) -> Option; fn get_user_supersigs(who: AccountId) -> Vec; fn list_members(supersig_id: SupersigId) -> Vec<(AccountId, Role)>; - fn list_proposals(supersig_id: SupersigId) -> (Vec>, u32); - fn get_proposal_state(supersig_id: SupersigId, call_id: CallId) -> Option<(ProposalState, u32)>; + fn list_proposals(supersig_id: SupersigId) -> (Vec>, u32); + fn get_proposal_state(supersig_id: SupersigId, call_id: CallId) -> Option<(ProposalState, u32)>; } } diff --git a/rpc/src/lib.rs b/rpc/src/lib.rs index b53553b..5a78656 100644 --- a/rpc/src/lib.rs +++ b/rpc/src/lib.rs @@ -13,7 +13,7 @@ pub use pallet_supersig_rpc_runtime_api::SuperSigApi as SuperSigRuntimeApi; use pallet_supersig::{rpc::ProposalState, CallId, Role, SupersigId}; #[rpc(client, server)] -pub trait SuperSigApi { +pub trait SuperSigApi { #[method(name = "superSig_getSupersigId")] fn get_supersig_id( &self, @@ -37,14 +37,14 @@ pub trait SuperSigApi { &self, supersig_id: SupersigId, at: Option, - ) -> RpcResult<(Vec>, u32)>; + ) -> RpcResult<(Vec>, u32)>; #[method(name = "superSig_getProposalState")] fn get_proposal_state( &self, supersig_id: SupersigId, call_id: CallId, at: Option, - ) -> RpcResult, u32)>>; + ) -> RpcResult, u32)>>; } /// SuperSig RPC methods. @@ -63,14 +63,13 @@ impl SuperSig { } } -impl SuperSigApiServer<::Hash, AccountId, Call> +impl SuperSigApiServer<::Hash, AccountId> for SuperSig where Block: BlockT, Client: Send + Sync + 'static + ProvideRuntimeApi + HeaderBackend, - Client::Api: SuperSigRuntimeApi, + Client::Api: SuperSigRuntimeApi, AccountId: Codec, - Call: Codec, { fn get_supersig_id( &self, @@ -106,7 +105,7 @@ where &self, supersig_id: SupersigId, at: Option<::Hash>, - ) -> RpcResult<(Vec>, u32)> { + ) -> RpcResult<(Vec>, u32)> { let api = self.client.runtime_api(); let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); api.list_proposals(&at, supersig_id).map_err(runtime_error_into_rpc_err) @@ -117,7 +116,7 @@ where supersig_id: SupersigId, call_id: CallId, at: Option<::Hash>, - ) -> RpcResult, u32)>> { + ) -> RpcResult, u32)>> { let api = self.client.runtime_api(); let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); api.get_proposal_state(&at, supersig_id, call_id) diff --git a/src/rpc.rs b/src/rpc.rs index 1fda0f5..f6a8c98 100644 --- a/src/rpc.rs +++ b/src/rpc.rs @@ -1,24 +1,21 @@ -use codec::Decode; -use frame_support::Parameter; -pub use sp_std::{boxed::Box, cmp::max, mem::size_of, prelude::Vec}; +use crate::Vec; +use codec::{Decode, Encode}; +pub use sp_std::{boxed::Box, cmp::max, mem::size_of}; -use crate::{ - pallet, CallId, Calls, Config, Error, Members, MembersVotes, Pallet, Role, SupersigId, -}; +use crate::{CallId, Calls, Config, Error, Members, MembersVotes, Pallet, Role, SupersigId}; -#[derive(Debug, Clone, PartialEq, Eq, Decode, serde::Serialize, serde::Deserialize)] -pub struct ProposalState { +#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] +#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))] +pub struct ProposalState { id: CallId, - call: Call, provider: AccountId, voters: Vec, } -impl ProposalState { - pub fn new(id: CallId, call: Call, provider: AccoutId, voters: Vec) -> Self { +impl ProposalState { + pub fn new(id: CallId, provider: AccoutId, voters: Vec) -> Self { Self { id, - call, provider, voters, } @@ -28,10 +25,6 @@ impl ProposalState { &self.id } - pub fn call(&self) -> &Call { - &self.call - } - pub fn provider(&self) -> &AccoutId { &self.provider } @@ -46,10 +39,10 @@ impl Pallet { Self::get_supersig_id_from_account(supersig_account) } - pub fn get_user_supersigs(who: T::AccountId) -> Vec { + pub fn get_user_supersigs(who: &T::AccountId) -> Vec { Members::::iter() .filter_map(|(supersig_id, member_id, _)| { - if member_id == who { + if member_id == *who { Some(supersig_id) } else { None @@ -58,16 +51,11 @@ impl Pallet { .collect() } - pub fn list_members(supersig_id: SupersigId) -> Vec<(T::AccountId, Role)> { + pub fn list_members(supersig_id: &SupersigId) -> Vec<(T::AccountId, Role)> { Members::::iter_prefix(supersig_id).collect() } - pub fn list_proposals( - supersig_id: SupersigId, - ) -> ( - Vec::Call>>, - u32, - ) { + pub fn list_proposals(supersig_id: &SupersigId) -> (Vec>, u32) { let member_count = Self::total_members(supersig_id); let proposal_state = Calls::::iter_prefix(supersig_id) .map(|(call_id, call)| { @@ -79,24 +67,16 @@ impl Pallet { ) .collect(); - ProposalState::new( - call_id, - ::Call::decode(&mut &call.data[..]).unwrap(), - call.provider, - voters, - ) + ProposalState::new(call_id, call.provider, voters) }) .collect(); (proposal_state, member_count) } pub fn get_proposal_state( - supersig_id: SupersigId, - call_id: CallId, - ) -> Option<( - ProposalState::Call>, - u32, - )> { + supersig_id: &SupersigId, + call_id: &CallId, + ) -> Option<(ProposalState, u32)> { let call = Self::calls(supersig_id, call_id)?; let member_count = Self::total_members(supersig_id); let voters = MembersVotes::::iter_prefix((supersig_id, call_id)) @@ -108,12 +88,7 @@ impl Pallet { .collect(); Some(( - ProposalState::new( - call_id, - ::Call::decode(&mut &call.data[..]).unwrap(), - call.provider, - voters, - ), + ProposalState::new(*call_id, call.provider, voters), member_count, )) } diff --git a/src/tests/rpc_calls.rs b/src/tests/rpc_calls.rs index 8a914af..53d5ac2 100644 --- a/src/tests/rpc_calls.rs +++ b/src/tests/rpc_calls.rs @@ -28,21 +28,21 @@ fn get_account_supersigs() { ExtBuilder::default().balances(vec![]).build().execute_with(|| { let mut supersig_count: u128 = 0; - assert_eq!(Supersig::get_user_supersigs(ALICE()), vec![]); + assert_eq!(Supersig::get_user_supersigs(&ALICE()), vec![]); create_supersig(supersig_count); - let alice_supersigs = Supersig::get_user_supersigs(ALICE()); + let alice_supersigs = Supersig::get_user_supersigs(&ALICE()); assert!(alice_supersigs.contains(&0)); supersig_count += 1; create_supersig(supersig_count); - let alice_supersigs = Supersig::get_user_supersigs(ALICE()); + let alice_supersigs = Supersig::get_user_supersigs(&ALICE()); assert!(alice_supersigs.contains(&0)); assert!(alice_supersigs.contains(&1)); supersig_count += 1; create_supersig(supersig_count); - let alice_supersigs = Supersig::get_user_supersigs(ALICE()); + let alice_supersigs = Supersig::get_user_supersigs(&ALICE()); assert!(alice_supersigs.contains(&0)); assert!(alice_supersigs.contains(&1)); assert!(alice_supersigs.contains(&2)); @@ -53,9 +53,9 @@ fn get_account_supersigs() { fn list_members() { ExtBuilder::default().balances(vec![]).build().execute_with(|| { create_supersig(0); - assert!(Supersig::list_members(0).contains(&(ALICE(), Role::Master))); - assert!(Supersig::list_members(0).contains(&(BOB(), Role::Standard))); - assert!(Supersig::list_members(0).contains(&(CHARLIE(), Role::Standard))); + assert!(Supersig::list_members(&0).contains(&(ALICE(), Role::Master))); + assert!(Supersig::list_members(&0).contains(&(BOB(), Role::Standard))); + assert!(Supersig::list_members(&0).contains(&(CHARLIE(), Role::Standard))); }) } @@ -91,11 +91,11 @@ fn get_proposals() { 1, )); - let list = Supersig::list_proposals(0); + let list = Supersig::list_proposals(&0); assert_eq!(list.1, 3); assert_eq!(list.0.len(), 2); - assert!(list.0.contains(&ProposalState::new(0, call.clone(), ALICE(), vec![BOB()]))); - assert!(list.0.contains(&ProposalState::new(1, call.clone(), ALICE(), vec![BOB()]))); + assert!(list.0.contains(&ProposalState::new(0, ALICE(), vec![BOB()]))); + assert!(list.0.contains(&ProposalState::new(1, ALICE(), vec![BOB()]))); assert_ok!(Supersig::approve_call( Origin::signed(ALICE()), @@ -104,8 +104,8 @@ fn get_proposals() { )); assert_eq!( - Supersig::list_proposals(0), - (vec![ProposalState::new(0, call, ALICE(), vec![BOB()])], 3) + Supersig::list_proposals(&0), + (vec![ProposalState::new(0, ALICE(), vec![BOB()])], 3) ); }) } @@ -118,7 +118,7 @@ fn get_proposal_state() { nothing: "test".into(), }); - assert_eq!(Supersig::get_proposal_state(0, 0), None); + assert_eq!(Supersig::get_proposal_state(&0, &0), None); assert_ok!(Supersig::submit_call( Origin::signed(ALICE()), @@ -127,8 +127,8 @@ fn get_proposal_state() { )); assert_eq!( - Supersig::get_proposal_state(0, 0), - Some((ProposalState::new(0, call.clone(), ALICE(), vec![]), 3)) + Supersig::get_proposal_state(&0, &0), + Some((ProposalState::new(0, ALICE(), vec![]), 3)) ); assert_ok!(Supersig::approve_call( @@ -138,8 +138,8 @@ fn get_proposal_state() { )); assert_eq!( - Supersig::get_proposal_state(0, 0), - Some((ProposalState::new(0, call, ALICE(), vec![ALICE()]), 3)) + Supersig::get_proposal_state(&0, &0), + Some((ProposalState::new(0, ALICE(), vec![ALICE()]), 3)) ); assert_ok!(Supersig::approve_call( @@ -148,6 +148,6 @@ fn get_proposal_state() { 0, )); - assert_eq!(Supersig::get_proposal_state(0, 0), None); + assert_eq!(Supersig::get_proposal_state(&0, &0), None); }) } diff --git a/src/types.rs b/src/types.rs index 94d2d34..8410790 100644 --- a/src/types.rs +++ b/src/types.rs @@ -6,18 +6,8 @@ use scale_info::TypeInfo; pub type BalanceOf = <::Currency as Currency<::AccountId>>::Balance; -#[derive( - Clone, - Encode, - Decode, - TypeInfo, - PartialEq, - Eq, - MaxEncodedLen, - Debug, - serde::Serialize, - serde::Deserialize, -)] +#[derive(Clone, Encode, Decode, TypeInfo, PartialEq, Eq, MaxEncodedLen, Debug)] +#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))] #[codec(mel_bound())] pub enum Role { Standard, From b1d9132ec7aed93b2ef1591aa7c5af326b3b8e56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Delabrouille?= Date: Wed, 3 Aug 2022 21:50:06 +0200 Subject: [PATCH 9/9] feature: add encoded_call to ProposalState RPC object --- src/rpc.rs | 13 ++++++++++--- src/tests/rpc_calls.rs | 17 ++++++++++++----- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/rpc.rs b/src/rpc.rs index f6a8c98..3ebf0f1 100644 --- a/src/rpc.rs +++ b/src/rpc.rs @@ -8,14 +8,21 @@ use crate::{CallId, Calls, Config, Error, Members, MembersVotes, Pallet, Role, S #[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))] pub struct ProposalState { id: CallId, + encoded_call: Vec, provider: AccountId, voters: Vec, } impl ProposalState { - pub fn new(id: CallId, provider: AccoutId, voters: Vec) -> Self { + pub fn new( + id: CallId, + encoded_call: Vec, + provider: AccoutId, + voters: Vec, + ) -> Self { Self { id, + encoded_call, provider, voters, } @@ -67,7 +74,7 @@ impl Pallet { ) .collect(); - ProposalState::new(call_id, call.provider, voters) + ProposalState::new(call_id, call.data, call.provider, voters) }) .collect(); (proposal_state, member_count) @@ -88,7 +95,7 @@ impl Pallet { .collect(); Some(( - ProposalState::new(*call_id, call.provider, voters), + ProposalState::new(*call_id, call.data, call.provider, voters), member_count, )) } diff --git a/src/tests/rpc_calls.rs b/src/tests/rpc_calls.rs index 53d5ac2..d89389f 100644 --- a/src/tests/rpc_calls.rs +++ b/src/tests/rpc_calls.rs @@ -1,5 +1,6 @@ use super::{helper::*, mock::*}; use crate::{rpc::ProposalState, Role}; +use codec::Encode; use frame_support::assert_ok; pub use sp_std::{boxed::Box, mem::size_of}; @@ -94,8 +95,8 @@ fn get_proposals() { let list = Supersig::list_proposals(&0); assert_eq!(list.1, 3); assert_eq!(list.0.len(), 2); - assert!(list.0.contains(&ProposalState::new(0, ALICE(), vec![BOB()]))); - assert!(list.0.contains(&ProposalState::new(1, ALICE(), vec![BOB()]))); + assert!(list.0.contains(&ProposalState::new(0, call.encode(), ALICE(), vec![BOB()]))); + assert!(list.0.contains(&ProposalState::new(1, call.encode(), ALICE(), vec![BOB()]))); assert_ok!(Supersig::approve_call( Origin::signed(ALICE()), @@ -105,7 +106,10 @@ fn get_proposals() { assert_eq!( Supersig::list_proposals(&0), - (vec![ProposalState::new(0, ALICE(), vec![BOB()])], 3) + ( + vec![ProposalState::new(0, call.encode(), ALICE(), vec![BOB()])], + 3 + ) ); }) } @@ -128,7 +132,7 @@ fn get_proposal_state() { assert_eq!( Supersig::get_proposal_state(&0, &0), - Some((ProposalState::new(0, ALICE(), vec![]), 3)) + Some((ProposalState::new(0, call.encode(), ALICE(), vec![]), 3)) ); assert_ok!(Supersig::approve_call( @@ -139,7 +143,10 @@ fn get_proposal_state() { assert_eq!( Supersig::get_proposal_state(&0, &0), - Some((ProposalState::new(0, ALICE(), vec![ALICE()]), 3)) + Some(( + ProposalState::new(0, call.encode(), ALICE(), vec![ALICE()]), + 3 + )) ); assert_ok!(Supersig::approve_call(