Skip to content

Commit

Permalink
[Communities] Call to dispatch_as_account (#354)
Browse files Browse the repository at this point in the history
* feat(pallet-communities): dispatch_as_account

* Benchmarking: Calculate weights for bda360c (#355)

* [ci] calculate weights

* change(pallet-communities): weight sanity tests

---------

Co-authored-by: pandres95 <[email protected]>
Co-authored-by: Pablo Andrés Dorado Suárez <[email protected]>

* chore: bump

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
pandres95 and github-actions[bot] authored Apr 8, 2024
1 parent c83e1fb commit c093eac
Show file tree
Hide file tree
Showing 15 changed files with 346 additions and 286 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion node/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "virto-node"
version = "0.8.1"
version = "0.9.0"
authors = ['Virto Team <[email protected]>']
license = "GPL-3.0-only"
homepage = 'https://github.com/virto-network/virto-node'
Expand Down
31 changes: 26 additions & 5 deletions pallets/communities/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use self::{
origin::DecisionMethod,
types::{
AccountIdOf, CommunityIdOf, DecisionMethodFor, MembershipIdOf, NativeBalanceOf, PalletsOriginOf, PollIndexOf,
Vote,
RuntimeCallFor, Vote,
},
Event, HoldReason, Pallet as Communities,
};
Expand All @@ -20,11 +20,11 @@ use frame_support::{
BoundedVec,
};
use frame_system::{
pallet_prelude::{BlockNumberFor, OriginFor, RuntimeCallFor},
pallet_prelude::{BlockNumberFor, OriginFor},
RawOrigin,
};
use sp_runtime::traits::StaticLookup;
use sp_std::{vec, vec::Vec};
use sp_runtime::traits::{Hash, StaticLookup};
use sp_std::{boxed::Box, vec, vec::Vec};

fn assert_has_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
frame_system::Pallet::<T>::assert_has_event(generic_event.into());
Expand Down Expand Up @@ -136,7 +136,7 @@ where
#[benchmarks(
where
T: frame_system::Config + crate::Config,
RuntimeCallFor<T>: From<crate::Call<T>>,
<T as Config>::RuntimeEvent: From<frame_system::Event<T>>,
MembershipIdOf<T>: From<u32>,
BlockNumberFor<T>: From<u32>
)]
Expand Down Expand Up @@ -407,6 +407,27 @@ mod benchmarks {
Ok(())
}

#[benchmark]
fn dispatch_as_account() -> Result<(), BenchmarkError> {
// setup code
let (id, origin) = create_community::<T>(RawOrigin::Root.into(), Some(DecisionMethod::NativeToken))?;
let remark = b"Hello, world".to_vec();

#[extrinsic_call]
_(
origin.into_caller(),
Box::new(frame_system::Call::<T>::remark_with_event { remark: remark.clone() }.into()),
);

// verification code
let sender = Communities::<T>::community_account(&id);
let hash = <T as frame_system::Config>::Hashing::hash(&remark);

assert_has_event::<T>(frame_system::Event::<T>::Remarked { sender, hash }.into());

Ok(())
}

impl_benchmark_test_suite!(
Communities,
sp_io::TestExternalities::new(Default::default()),
Expand Down
19 changes: 17 additions & 2 deletions pallets/communities/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@ use crate::{
origin::DecisionMethod,
types::{
AccountIdOf, CommunityIdOf, CommunityInfo, CommunityState, ConstSizedField, MembershipIdOf, PalletsOriginOf,
PollIndexOf, Tally, Vote, VoteOf, VoteWeight,
PollIndexOf, RuntimeCallFor, Tally, Vote, VoteOf, VoteWeight,
},
CommunityDecisionMethod, CommunityIdFor, CommunityVotes, Config, Error, HoldReason, Info, Metadata, Pallet,
};
use fc_traits_memberships::{GenericRank, Inspect, Rank};
use frame_support::{
dispatch::PostDispatchInfo,
fail,
pallet_prelude::*,
traits::{
fungible::MutateFreeze as FunMutateFreeze, fungibles::MutateHold as FunsMutateHold, tokens::Precision, Polling,
},
};
use sp_runtime::{traits::AccountIdConversion, TokenError};
use sp_runtime::{
traits::{AccountIdConversion, Dispatchable},
DispatchResultWithInfo, TokenError,
};
use sp_std::vec::Vec;

impl<T: Config> Pallet<T> {
Expand Down Expand Up @@ -189,6 +193,17 @@ impl<T: Config> Pallet<T> {
_ => Err(Error::<T>::NoLocksInPlace.into()),
}
}

pub(crate) fn do_dispatch_as_community_account(
community_id: &CommunityIdOf<T>,
call: RuntimeCallFor<T>,
) -> DispatchResultWithInfo<PostDispatchInfo> {
let community_account = Self::community_account(community_id);
let signer = frame_system::RawOrigin::Signed(community_account);

let post = call.dispatch(signer.into()).map_err(|e| e.error)?;
Ok(post)
}
}

impl<T: Config> Tally<T> {
Expand Down
31 changes: 28 additions & 3 deletions pallets/communities/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,15 @@ pub mod pallet {
use core::num::NonZeroU8;
use fc_traits_memberships::{self as membership, Inspect, Manager, Rank};
use frame_support::{
dispatch::{DispatchResultWithPostInfo, GetDispatchInfo, PostDispatchInfo},
pallet_prelude::*,
traits::{fungible, fungibles, EnsureOrigin, Polling},
traits::{fungible, fungibles, EnsureOrigin, IsSubType, Polling},
Blake2_128Concat, Parameter,
};
use frame_system::pallet_prelude::{OriginFor, *};
use sp_runtime::traits::StaticLookup;
use types::{PollIndexOf, *};
use sp_runtime::traits::{Dispatchable, StaticLookup};
use sp_std::prelude::Box;
use types::{PollIndexOf, RuntimeCallFor, *};
const ONE: NonZeroU8 = NonZeroU8::MIN;

#[pallet::pallet]
Expand Down Expand Up @@ -192,6 +194,15 @@ pub mod pallet {
+ fungible::freeze::Inspect<Self::AccountId, Id = Self::RuntimeHoldReason>
+ fungible::freeze::Mutate<Self::AccountId, Id = Self::RuntimeHoldReason>;

/// The overarching call type.
type RuntimeCall: Parameter
+ Dispatchable<RuntimeOrigin = Self::RuntimeOrigin, PostInfo = PostDispatchInfo>
+ GetDispatchInfo
+ From<Call<Self>>
+ From<frame_system::Call<Self>>
+ IsSubType<Call<Self>>
+ IsType<<Self as frame_system::Config>::RuntimeCall>;

/// The overarching hold reason.
type RuntimeHoldReason: From<HoldReason>;

Expand Down Expand Up @@ -499,5 +510,19 @@ pub mod pallet {

Self::do_unlock_for_vote(&who, &poll_index, &vote)
}

/// Dispatch a callable as the community account
#[pallet::call_index(11)]
#[pallet::weight({
let di = call.get_dispatch_info();
let weight = T::WeightInfo::dispatch_as_account()
.saturating_add(T::DbWeight::get().reads_writes(1, 1))
.saturating_add(di.weight);
(weight, di.class)
})]
pub fn dispatch_as_account(origin: OriginFor<T>, call: Box<RuntimeCallFor<T>>) -> DispatchResultWithPostInfo {
let community_id = T::MemberMgmtOrigin::ensure_origin(origin)?;
Self::do_dispatch_as_community_account(&community_id, *call)
}
}
}
1 change: 1 addition & 0 deletions pallets/communities/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ impl pallet_communities::Config for Test {
type CommunityMgmtOrigin = EnsureRoot<AccountId>;
type MemberMgmtOrigin = EnsureCommunity<Self>;

type RuntimeCall = RuntimeCall;
type RuntimeEvent = RuntimeEvent;
type RuntimeHoldReason = RuntimeHoldReason;
type WeightInfo = WeightInfo;
Expand Down
1 change: 1 addition & 0 deletions pallets/communities/src/tests/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ fn weights() {
("vote", SubstrateWeight::<Test>::vote()),
("remove_vote", SubstrateWeight::<Test>::remove_vote()),
("unlock", SubstrateWeight::<Test>::unlock()),
("dispatch_as_account", SubstrateWeight::<Test>::dispatch_as_account()),
] {
println!("{function}: {weight:?}",);
println!(
Expand Down
6 changes: 2 additions & 4 deletions pallets/communities/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub type AccountIdLookupOf<T> = <<T as frame_system::Config>::Lookup as StaticLo
pub type PalletsOriginOf<T> =
<<T as frame_system::Config>::RuntimeOrigin as frame_support::traits::OriginTrait>::PalletsOrigin;
pub type MembershipIdOf<T> = <T as Config>::MembershipId;
pub type RuntimeCallFor<T> = <T as Config>::RuntimeCall;

pub type SizedField<S> = BoundedVec<u8, S>;
pub type ConstSizedField<const S: u32> = SizedField<ConstU32<S>>;
Expand Down Expand Up @@ -148,10 +149,7 @@ impl<T: Config> Tally<T> {
}

#[cfg(feature = "runtime-benchmarks")]
use {
frame_benchmarking::BenchmarkError,
frame_system::pallet_prelude::{OriginFor, RuntimeCallFor},
};
use {frame_benchmarking::BenchmarkError, frame_system::pallet_prelude::OriginFor};

#[cfg(feature = "runtime-benchmarks")]
pub trait BenchmarkHelper<T: Config> {
Expand Down
Loading

0 comments on commit c093eac

Please sign in to comment.