-
Notifications
You must be signed in to change notification settings - Fork 0
Multiple proxies added #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
5b4fa92
61e3c2f
add5677
26cc5a2
960a59b
c4944cb
82f8b48
85beb0a
56ead68
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,11 +40,36 @@ mod benchmarking; | |
| pub mod weights; | ||
| pub use weights::WeightInfo; | ||
|
|
||
| /// The parameters under which a particular account has a proxy relationship with some other | ||
| /// account. | ||
| #[derive( | ||
| Encode, | ||
| Decode, | ||
| Clone, | ||
| Copy, | ||
| Eq, | ||
| PartialEq, | ||
| Ord, | ||
| PartialOrd, | ||
| MaxEncodedLen, | ||
| TypeInfo, | ||
| )] | ||
| pub struct ProxyDefinition<AccountId, CallFilter> { | ||
| /// The account which may act on behalf of another. | ||
| pub proxy: AccountId, | ||
| /// A value defining the subset of calls that it is allowed to make. | ||
| pub filter: CallFilter, | ||
| } | ||
|
|
||
| #[frame_support::pallet] | ||
| pub mod pallet { | ||
| use super::*; | ||
|
|
||
| /// The current storage version. | ||
| pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); | ||
|
|
||
| #[pallet::pallet] | ||
| #[pallet::storage_version(STORAGE_VERSION)] | ||
| pub struct Pallet<T>(_); | ||
|
|
||
| // TODO: The pallet is intentionally very basic. It could be improved to handle more origins, more aliases, etc. | ||
|
|
@@ -68,11 +93,19 @@ pub mod pallet { | |
| /// Origin that can act on behalf of the collective. | ||
| type CollectiveProxy: EnsureOrigin<<Self as frame_system::Config>::RuntimeOrigin>; | ||
|
|
||
| /// Account representing the collective treasury. | ||
| type ProxyAccountId: Get<Self::AccountId>; | ||
| /// Origin with permissions to add and remove proxies for the collective. | ||
| type ProxyAdmin: EnsureOrigin<<Self as frame_system::Config>::RuntimeOrigin>; | ||
|
|
||
| /// Filter to determine whether a call can be executed or not. | ||
| type CallFilter: InstanceFilter<<Self as Config>::RuntimeCall> + Default; | ||
| type CallFilter: InstanceFilter<<Self as Config>::RuntimeCall> | ||
| + Parameter | ||
| + Member | ||
| + Clone | ||
| + Encode | ||
| + Decode | ||
| + MaxEncodedLen | ||
| + TypeInfo | ||
| + Default; | ||
|
|
||
| /// Weight info | ||
| type WeightInfo: WeightInfo; | ||
|
|
@@ -85,6 +118,22 @@ pub mod pallet { | |
| CollectiveProxyExecuted { result: DispatchResult }, | ||
| } | ||
|
|
||
| #[pallet::error] | ||
| pub enum Error<T> { | ||
| /// Proxy registration not found. | ||
| NotFound, | ||
| } | ||
|
|
||
| /// The set of account proxies | ||
| #[pallet::storage] | ||
| pub type Proxies<T: Config> = StorageMap< | ||
| _, | ||
| Twox64Concat, | ||
| T::AccountId, | ||
| T::CallFilter, | ||
| OptionQuery, | ||
| >; | ||
|
|
||
| #[pallet::call] | ||
| impl<T: Config> Pallet<T> { | ||
| /// Executes the call on a behalf of an aliased account. | ||
|
|
@@ -98,19 +147,22 @@ pub mod pallet { | |
| })] | ||
| pub fn execute_call( | ||
| origin: OriginFor<T>, | ||
| proxy: T::AccountId, | ||
| call: Box<<T as Config>::RuntimeCall>, | ||
| ) -> DispatchResult { | ||
| // Ensure origin is valid. | ||
| T::CollectiveProxy::ensure_origin(origin)?; | ||
|
|
||
| let def = Self::find_proxy(proxy)?; | ||
|
|
||
| // Account authentication is ensured by the `CollectiveProxy` origin check. | ||
| let mut origin: T::RuntimeOrigin = | ||
| frame_system::RawOrigin::Signed(T::ProxyAccountId::get()).into(); | ||
| frame_system::RawOrigin::Signed(def.proxy).into(); | ||
|
|
||
| // Ensure custom filter is applied. | ||
| origin.add_filter(move |c: &<T as frame_system::Config>::RuntimeCall| { | ||
| let c = <T as Config>::RuntimeCall::from_ref(c); | ||
| T::CallFilter::default().filter(c) | ||
| def.filter.filter(c) | ||
| }); | ||
|
|
||
| // Dispatch the call. | ||
|
|
@@ -121,5 +173,52 @@ pub mod pallet { | |
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| /// Register a proxy account for the sender that is able to make calls on its behalf. | ||
| /// | ||
| /// The dispatch origin for this call must be _Signed_. | ||
| /// | ||
| /// Parameters: | ||
| /// - `proxy`: The account that the `caller` would like to make a proxy. | ||
| /// - `filter`: Call filter used for the proxy | ||
| #[pallet::call_index(1)] | ||
| #[pallet::weight(T::WeightInfo::add_proxy())] | ||
| pub fn add_proxy( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens when the ProxyAdmin tries to overwrite an existing proxy with a stricter filter? (e.g., temporary restriction during maintenance/emergency)
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reworked to map |
||
| origin: OriginFor<T>, | ||
| proxy: T::AccountId, | ||
| filter: T::CallFilter, | ||
| ) -> DispatchResult { | ||
| T::ProxyAdmin::ensure_origin(origin)?; | ||
| let proxy = proxy.clone(); | ||
| let filter = filter.clone(); | ||
| Proxies::<T>::insert(proxy, filter); | ||
| Ok(()) | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if there are multiple Proxies for the same account Id?
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They differ by calling filter and the first found with the suitable filter will be used. I think, I relied this logic on the base proxy pallet There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I understand it's implemented like that. But since If one privilege is superset of the other, why would proxy ever use the less privileged one? To follow-up on my previous comment - one thing to consider here is the data struct used to store the values. Like it is now, we can have vector full of essentially the same values. Is this good for the functionality? Would you suggest a change here?
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aa, I got your concern. I think, it can be easily addressed with superset validation that would prevent the case you're describing (the change added). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That would help, yes, but you can also replace the existing vector with a Map-like collection. E.g. if key is the account Id, then it's not possible to have duplicates.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah. I did experiments with such structures (ordering leftovers were caused by them) but decided that storage costs are not worth it and used simple vec eventually. Added code will prevent any redundant duplication. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see! Out of curiosity, how much less performant was the Back to my suggestion above, performance wise, this should be equivalent or faster than the vec approach: #[pallet::storage]
pub type Proxies<T: Config> = StorageMap<
_,
(T::CollectiveProxy, T::AccountId),
T::CallFilter,
ValueQuery,
>;The This way you can support more than 1 collective proxy type, each proxy can delegate to multiple accounts, but only one delegation type is possible. Anyways, just something of top of my hat, haven't tried or implemented it 🙂 Thank you for all the replies & the effort.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was considering standard BoundedBTreeMap. I don't think, there is any significant difference in performance for such small sets of data (adding to equation codec's complexity makes things even more complicated). My concern was mostly about the additional storage costs for TreeMap. I don't remember already but there was a general optimization hint to use compact storage structures as much as possible. Thank you for your comments and questions! It helped me a lot to understand better the use case. |
||
|
|
||
| /// Unregister a proxy account for the sender. | ||
| /// | ||
| /// The dispatch origin for this call must be _Signed_. | ||
| /// | ||
| /// Parameters: | ||
| /// - `proxy`: The account that the `caller` would like to remove as a proxy. | ||
| /// - `filter`: Call filter used for the proxy | ||
| #[pallet::call_index(2)] | ||
| #[pallet::weight(T::WeightInfo::remove_proxy())] | ||
| pub fn remove_proxy( | ||
| origin: OriginFor<T>, | ||
| proxy: T::AccountId, | ||
| ) -> DispatchResult { | ||
| T::ProxyAdmin::ensure_origin(origin)?; | ||
| Proxies::<T>::remove(proxy); | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| impl<T: Config> Pallet<T> { | ||
| pub fn find_proxy( | ||
| proxy: T::AccountId, | ||
| ) -> Result<ProxyDefinition<T::AccountId, T::CallFilter>, DispatchError> { | ||
| Ok(Proxies::<T>::get(&proxy).map(|filter| ProxyDefinition { proxy, filter} ).ok_or(Error::<T>::NotFound)?) | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.