From 832d8669fe1899a7c1568159e806523d5d95e748 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 11 Sep 2023 10:26:47 +0200 Subject: [PATCH] fix: reanme + no default proposal --- api/lib/src/lib.rs | 2 +- .../pallets/cf-governance/src/benchmarking.rs | 8 +++--- state-chain/pallets/cf-governance/src/lib.rs | 27 +++++-------------- .../pallets/cf-governance/src/tests.rs | 16 +++++------ 4 files changed, 20 insertions(+), 33 deletions(-) diff --git a/api/lib/src/lib.rs b/api/lib/src/lib.rs index 53e3a5aac6..f0f2f49bdb 100644 --- a/api/lib/src/lib.rs +++ b/api/lib/src/lib.rs @@ -266,7 +266,7 @@ pub trait GovernanceApi: SignedExtrinsicApi { println!("Submitting governance proposal for rotation."); self.submit_signed_extrinsic(pallet_cf_governance::Call::propose_governance_extrinsic { call: Box::new(pallet_cf_validator::Call::force_rotation {}.into()), - execution: ExecutionMode::Scheduled, + execution: ExecutionMode::Automatic, }) .await .until_finalized() diff --git a/state-chain/pallets/cf-governance/src/benchmarking.rs b/state-chain/pallets/cf-governance/src/benchmarking.rs index 0b53db3d6d..625dbf9337 100644 --- a/state-chain/pallets/cf-governance/src/benchmarking.rs +++ b/state-chain/pallets/cf-governance/src/benchmarking.rs @@ -16,7 +16,7 @@ benchmarks! { let caller: T::AccountId = whitelisted_caller(); let call = Box::new(frame_system::Call::remark{remark: vec![]}.into()); >::put(BTreeSet::from([caller.clone()])); - }: _(RawOrigin::Signed(caller.clone()), call, ExecutionMode::Scheduled) + }: _(RawOrigin::Signed(caller.clone()), call, ExecutionMode::Automatic) verify { assert_eq!(ProposalIdCounter::::get(), 1); } @@ -24,7 +24,7 @@ benchmarks! { let call: ::RuntimeCall = frame_system::Call::remark{remark: vec![]}.into(); let caller: T::AccountId = whitelisted_caller(); >::put(BTreeSet::from([caller.clone()])); - Pallet::::push_proposal(Box::new(call), ExecutionMode::Scheduled); + Pallet::::push_proposal(Box::new(call), ExecutionMode::Automatic); }: _(RawOrigin::Signed(caller.clone()), 1) verify { assert_eq!(ProposalIdCounter::::get(), 1); @@ -48,7 +48,7 @@ benchmarks! { let b in 1 .. 100u32; for _n in 1 .. b { let call = Box::new(frame_system::Call::remark{remark: vec![]}.into()); - Pallet::::push_proposal(call, ExecutionMode::Scheduled); + Pallet::::push_proposal(call, ExecutionMode::Automatic); } }: { Pallet::::on_initialize(2u32.into()); @@ -61,7 +61,7 @@ benchmarks! { let b in 1 .. 100u32; for _n in 1 .. b { let call = Box::new(frame_system::Call::remark{remark: vec![]}.into()); - Pallet::::push_proposal(call, ExecutionMode::Scheduled); + Pallet::::push_proposal(call, ExecutionMode::Automatic); } } : { Pallet::::expire_proposals(>::get()); diff --git a/state-chain/pallets/cf-governance/src/lib.rs b/state-chain/pallets/cf-governance/src/lib.rs index 324dde9b78..d5c6c5fbef 100644 --- a/state-chain/pallets/cf-governance/src/lib.rs +++ b/state-chain/pallets/cf-governance/src/lib.rs @@ -52,18 +52,13 @@ pub mod pallet { use super::{GovCallHash, WeightInfo}; - #[derive(Encode, Decode, TypeInfo, Clone, RuntimeDebug, PartialEq, Eq)] + #[derive(Default, Encode, Decode, TypeInfo, Clone, RuntimeDebug, PartialEq, Eq)] pub enum ExecutionMode { - Scheduled, + #[default] + Automatic, Manual, } - impl Default for ExecutionMode { - fn default() -> Self { - Self::Scheduled - } - } - #[derive(Encode, Decode, TypeInfo, Clone, Copy, RuntimeDebug, PartialEq, Eq)] pub struct ActiveProposal { pub proposal_id: ProposalId, @@ -81,16 +76,6 @@ pub mod pallet { pub execution: ExecutionMode, } - impl Default for Proposal { - fn default() -> Self { - Self { - call: Default::default(), - approved: Default::default(), - execution: Default::default(), - } - } - } - type AccountId = ::AccountId; type OpaqueCall = Vec; type Timestamp = u64; @@ -133,7 +118,7 @@ pub mod pallet { #[pallet::storage] #[pallet::getter(fn proposals)] pub(super) type Proposals = - StorageMap<_, Blake2_128Concat, ProposalId, Proposal, ValueQuery>; + StorageMap<_, Blake2_128Concat, ProposalId, Proposal>; /// Active proposals. #[pallet::storage] @@ -538,7 +523,9 @@ impl Pallet { ensure!(Proposals::::contains_key(approved_id), Error::::ProposalNotFound); // Try to approve the proposal - let proposal = Proposals::::mutate(approved_id, |proposal| { + let proposal = Proposals::::try_mutate(approved_id, |proposal| { + let proposal = proposal.as_mut().ok_or(Error::::ProposalNotFound)?; + if !proposal.approved.insert(who) { return Err(Error::::AlreadyApproved) } diff --git a/state-chain/pallets/cf-governance/src/tests.rs b/state-chain/pallets/cf-governance/src/tests.rs index c9c8f73535..2b17f0d0e0 100644 --- a/state-chain/pallets/cf-governance/src/tests.rs +++ b/state-chain/pallets/cf-governance/src/tests.rs @@ -37,7 +37,7 @@ fn not_a_member() { Governance::propose_governance_extrinsic( RuntimeOrigin::signed(EVE), mock_extrinsic(), - ExecutionMode::Scheduled, + ExecutionMode::Automatic, ), >::NotMember ); @@ -52,7 +52,7 @@ fn propose_a_governance_extrinsic_and_expect_execution() { assert_ok!(Governance::propose_governance_extrinsic( RuntimeOrigin::signed(ALICE), mock_extrinsic(), - ExecutionMode::Scheduled, + ExecutionMode::Automatic, )); assert_eq!( last_event::(), @@ -84,7 +84,7 @@ fn already_executed() { assert_ok!(Governance::propose_governance_extrinsic( RuntimeOrigin::signed(ALICE), mock_extrinsic(), - ExecutionMode::Scheduled, + ExecutionMode::Automatic, )); // Assert the proposed event was fired assert_eq!( @@ -127,7 +127,7 @@ fn propose_a_governance_extrinsic_and_expect_it_to_expire() { assert_ok!(Governance::propose_governance_extrinsic( RuntimeOrigin::signed(ALICE), mock_extrinsic(), - ExecutionMode::Scheduled, + ExecutionMode::Automatic, )); }) .then_execute_at_next_block(|_| { @@ -151,7 +151,7 @@ fn can_not_vote_twice() { assert_ok!(Governance::propose_governance_extrinsic( RuntimeOrigin::signed(ALICE), mock_extrinsic(), - ExecutionMode::Scheduled, + ExecutionMode::Automatic, )); // Try to approve it again. Proposing implies approving. assert_noop!( @@ -167,7 +167,7 @@ fn several_open_proposals() { assert_ok!(Governance::propose_governance_extrinsic( RuntimeOrigin::signed(ALICE), mock_extrinsic(), - ExecutionMode::Scheduled, + ExecutionMode::Automatic, )); assert_eq!( last_event::(), @@ -176,7 +176,7 @@ fn several_open_proposals() { assert_ok!(Governance::propose_governance_extrinsic( RuntimeOrigin::signed(BOB), mock_extrinsic(), - ExecutionMode::Scheduled, + ExecutionMode::Automatic, )); assert_eq!( last_event::(), @@ -202,7 +202,7 @@ fn sudo_extrinsic() { assert_ok!(Governance::propose_governance_extrinsic( RuntimeOrigin::signed(ALICE), governance_extrinsic, - ExecutionMode::Scheduled, + ExecutionMode::Automatic, )); assert_eq!( last_event::(),