mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 13:27:57 +00:00
Time-delay proxies (#6770)
* Time-delay proxies. * Tests * Initial couple of benchmarks * Fix up runtime * Last couple of benchmarks * Tests * Docs * Migration * add tests to proxy benchmarks * generated benchmarks, not integrated * Fix weight trait * integrate weightinfo * default weight * Grumble * Deduplication, split proxy from announced_proxy and don't require reauthentication * Fix * Remoe superfluous * Typos * Indent * Fix * Fixes * rename 'proxy_announced' -> 'announced_proxy' * flip rename * comments and spacing * fix proxy_announced * remove unneeded `execute` marker * Avoid unneeded changes to extrinsic indices * Cleanup * Fixes * Update Benchmarks and Weights for Delayed Proxy (#6811) * update bechmarks to parameterize announcements * remove announcement param from proxy * Update pallet_proxy.rs * Update weights * Bump runtime * Fix benchmark Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
This commit is contained in:
@@ -20,13 +20,21 @@
|
||||
#![cfg(feature = "runtime-benchmarks")]
|
||||
|
||||
use super::*;
|
||||
use frame_system::RawOrigin;
|
||||
use frame_system::{RawOrigin, EventRecord};
|
||||
use frame_benchmarking::{benchmarks, account, whitelisted_caller};
|
||||
use sp_runtime::traits::Bounded;
|
||||
use crate::Module as Proxy;
|
||||
|
||||
const SEED: u32 = 0;
|
||||
|
||||
fn assert_last_event<T: Trait>(generic_event: <T as Trait>::Event) {
|
||||
let events = frame_system::Module::<T>::events();
|
||||
let system_event: <T as frame_system::Trait>::Event = generic_event.into();
|
||||
// compare to the last event record
|
||||
let EventRecord { event, .. } = &events[events.len() - 1];
|
||||
assert_eq!(event, &system_event);
|
||||
}
|
||||
|
||||
fn add_proxies<T: Trait>(n: u32, maybe_who: Option<T::AccountId>) -> Result<(), &'static str> {
|
||||
let caller = maybe_who.unwrap_or_else(|| whitelisted_caller());
|
||||
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
|
||||
@@ -34,7 +42,38 @@ fn add_proxies<T: Trait>(n: u32, maybe_who: Option<T::AccountId>) -> Result<(),
|
||||
Proxy::<T>::add_proxy(
|
||||
RawOrigin::Signed(caller.clone()).into(),
|
||||
account("target", i, SEED),
|
||||
T::ProxyType::default()
|
||||
T::ProxyType::default(),
|
||||
T::BlockNumber::zero(),
|
||||
)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn add_announcements<T: Trait>(
|
||||
n: u32,
|
||||
maybe_who: Option<T::AccountId>,
|
||||
maybe_real: Option<T::AccountId>
|
||||
) -> Result<(), &'static str> {
|
||||
let caller = maybe_who.unwrap_or_else(|| account("caller", 0, SEED));
|
||||
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
|
||||
let real = if let Some(real) = maybe_real {
|
||||
real
|
||||
} else {
|
||||
let real = account("real", 0, SEED);
|
||||
T::Currency::make_free_balance_be(&real, BalanceOf::<T>::max_value());
|
||||
Proxy::<T>::add_proxy(
|
||||
RawOrigin::Signed(real.clone()).into(),
|
||||
caller.clone(),
|
||||
T::ProxyType::default(),
|
||||
T::BlockNumber::zero(),
|
||||
)?;
|
||||
real
|
||||
};
|
||||
for _ in 0..n {
|
||||
Proxy::<T>::announce(
|
||||
RawOrigin::Signed(caller.clone()).into(),
|
||||
real.clone(),
|
||||
T::CallHasher::hash_of(&("add_announcement", n)),
|
||||
)?;
|
||||
}
|
||||
Ok(())
|
||||
@@ -49,43 +88,171 @@ benchmarks! {
|
||||
let p in ...;
|
||||
// In this case the caller is the "target" proxy
|
||||
let caller: T::AccountId = account("target", p - 1, SEED);
|
||||
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
|
||||
// ... and "real" is the traditional caller. This is not a typo.
|
||||
let real: T::AccountId = whitelisted_caller();
|
||||
let call: <T as Trait>::Call = frame_system::Call::<T>::remark(vec![]).into();
|
||||
}: _(RawOrigin::Signed(caller), real, Some(T::ProxyType::default()), Box::new(call))
|
||||
verify {
|
||||
assert_last_event::<T>(RawEvent::ProxyExecuted(Ok(())).into())
|
||||
}
|
||||
|
||||
proxy_announced {
|
||||
let a in 0 .. T::MaxPending::get() - 1;
|
||||
let p in ...;
|
||||
// In this case the caller is the "target" proxy
|
||||
let caller: T::AccountId = account("anonymous", 0, SEED);
|
||||
let delegate: T::AccountId = account("target", p - 1, SEED);
|
||||
T::Currency::make_free_balance_be(&delegate, BalanceOf::<T>::max_value());
|
||||
// ... and "real" is the traditional caller. This is not a typo.
|
||||
let real: T::AccountId = whitelisted_caller();
|
||||
let call: <T as Trait>::Call = frame_system::Call::<T>::remark(vec![]).into();
|
||||
Proxy::<T>::announce(
|
||||
RawOrigin::Signed(delegate.clone()).into(),
|
||||
real.clone(),
|
||||
T::CallHasher::hash_of(&call),
|
||||
)?;
|
||||
add_announcements::<T>(a, Some(delegate.clone()), None)?;
|
||||
}: _(RawOrigin::Signed(caller), delegate, real, Some(T::ProxyType::default()), Box::new(call))
|
||||
verify {
|
||||
assert_last_event::<T>(RawEvent::ProxyExecuted(Ok(())).into())
|
||||
}
|
||||
|
||||
remove_announcement {
|
||||
let a in 0 .. T::MaxPending::get() - 1;
|
||||
let p in ...;
|
||||
// In this case the caller is the "target" proxy
|
||||
let caller: T::AccountId = account("target", p - 1, SEED);
|
||||
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
|
||||
// ... and "real" is the traditional caller. This is not a typo.
|
||||
let real: T::AccountId = whitelisted_caller();
|
||||
let call: <T as Trait>::Call = frame_system::Call::<T>::remark(vec![]).into();
|
||||
Proxy::<T>::announce(
|
||||
RawOrigin::Signed(caller.clone()).into(),
|
||||
real.clone(),
|
||||
T::CallHasher::hash_of(&call),
|
||||
)?;
|
||||
add_announcements::<T>(a, Some(caller.clone()), None)?;
|
||||
}: _(RawOrigin::Signed(caller.clone()), real, T::CallHasher::hash_of(&call))
|
||||
verify {
|
||||
let (announcements, _) = Announcements::<T>::get(&caller);
|
||||
assert_eq!(announcements.len() as u32, a);
|
||||
}
|
||||
|
||||
reject_announcement {
|
||||
let a in 0 .. T::MaxPending::get() - 1;
|
||||
let p in ...;
|
||||
// In this case the caller is the "target" proxy
|
||||
let caller: T::AccountId = account("target", p - 1, SEED);
|
||||
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
|
||||
// ... and "real" is the traditional caller. This is not a typo.
|
||||
let real: T::AccountId = whitelisted_caller();
|
||||
let call: <T as Trait>::Call = frame_system::Call::<T>::remark(vec![]).into();
|
||||
Proxy::<T>::announce(
|
||||
RawOrigin::Signed(caller.clone()).into(),
|
||||
real.clone(),
|
||||
T::CallHasher::hash_of(&call),
|
||||
)?;
|
||||
add_announcements::<T>(a, Some(caller.clone()), None)?;
|
||||
}: _(RawOrigin::Signed(real), caller.clone(), T::CallHasher::hash_of(&call))
|
||||
verify {
|
||||
let (announcements, _) = Announcements::<T>::get(&caller);
|
||||
assert_eq!(announcements.len() as u32, a);
|
||||
}
|
||||
|
||||
announce {
|
||||
let a in 0 .. T::MaxPending::get() - 1;
|
||||
let p in ...;
|
||||
// In this case the caller is the "target" proxy
|
||||
let caller: T::AccountId = account("target", p - 1, SEED);
|
||||
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
|
||||
// ... and "real" is the traditional caller. This is not a typo.
|
||||
let real: T::AccountId = whitelisted_caller();
|
||||
add_announcements::<T>(a, Some(caller.clone()), None)?;
|
||||
let call: <T as Trait>::Call = frame_system::Call::<T>::remark(vec![]).into();
|
||||
let call_hash = T::CallHasher::hash_of(&call);
|
||||
}: _(RawOrigin::Signed(caller.clone()), real.clone(), call_hash)
|
||||
verify {
|
||||
assert_last_event::<T>(RawEvent::Announced(real, caller, call_hash).into());
|
||||
}
|
||||
|
||||
add_proxy {
|
||||
let p in ...;
|
||||
let caller: T::AccountId = whitelisted_caller();
|
||||
}: _(RawOrigin::Signed(caller), account("target", T::MaxProxies::get().into(), SEED), T::ProxyType::default())
|
||||
}: _(
|
||||
RawOrigin::Signed(caller.clone()),
|
||||
account("target", T::MaxProxies::get().into(), SEED),
|
||||
T::ProxyType::default(),
|
||||
T::BlockNumber::zero()
|
||||
)
|
||||
verify {
|
||||
let (proxies, _) = Proxies::<T>::get(caller);
|
||||
assert_eq!(proxies.len() as u32, p + 1);
|
||||
}
|
||||
|
||||
remove_proxy {
|
||||
let p in ...;
|
||||
let caller: T::AccountId = whitelisted_caller();
|
||||
}: _(RawOrigin::Signed(caller), account("target", 0, SEED), T::ProxyType::default())
|
||||
}: _(
|
||||
RawOrigin::Signed(caller.clone()),
|
||||
account("target", 0, SEED),
|
||||
T::ProxyType::default(),
|
||||
T::BlockNumber::zero()
|
||||
)
|
||||
verify {
|
||||
let (proxies, _) = Proxies::<T>::get(caller);
|
||||
assert_eq!(proxies.len() as u32, p - 1);
|
||||
}
|
||||
|
||||
remove_proxies {
|
||||
let p in ...;
|
||||
let caller: T::AccountId = whitelisted_caller();
|
||||
}: _(RawOrigin::Signed(caller))
|
||||
}: _(RawOrigin::Signed(caller.clone()))
|
||||
verify {
|
||||
let (proxies, _) = Proxies::<T>::get(caller);
|
||||
assert_eq!(proxies.len() as u32, 0);
|
||||
}
|
||||
|
||||
anonymous {
|
||||
let p in ...;
|
||||
}: _(RawOrigin::Signed(whitelisted_caller()), T::ProxyType::default(), 0)
|
||||
let caller: T::AccountId = whitelisted_caller();
|
||||
}: _(
|
||||
RawOrigin::Signed(caller.clone()),
|
||||
T::ProxyType::default(),
|
||||
T::BlockNumber::zero(),
|
||||
0
|
||||
)
|
||||
verify {
|
||||
let anon_account = Module::<T>::anonymous_account(&caller, &T::ProxyType::default(), 0, None);
|
||||
assert_last_event::<T>(RawEvent::AnonymousCreated(
|
||||
anon_account,
|
||||
caller,
|
||||
T::ProxyType::default(),
|
||||
0,
|
||||
).into());
|
||||
}
|
||||
|
||||
kill_anonymous {
|
||||
let p in 0 .. (T::MaxProxies::get() - 2).into();
|
||||
|
||||
let caller: T::AccountId = whitelisted_caller();
|
||||
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
|
||||
Module::<T>::anonymous(RawOrigin::Signed(whitelisted_caller()).into(), T::ProxyType::default(), 0)?;
|
||||
Module::<T>::anonymous(
|
||||
RawOrigin::Signed(whitelisted_caller()).into(),
|
||||
T::ProxyType::default(),
|
||||
T::BlockNumber::zero(),
|
||||
0
|
||||
)?;
|
||||
let height = system::Module::<T>::block_number();
|
||||
let ext_index = system::Module::<T>::extrinsic_index().unwrap_or(0);
|
||||
let anon = Module::<T>::anonymous_account(&caller, &T::ProxyType::default(), 0, None);
|
||||
|
||||
add_proxies::<T>(p, Some(anon.clone()))?;
|
||||
|
||||
}: _(RawOrigin::Signed(anon), caller, T::ProxyType::default(), 0, height, ext_index)
|
||||
ensure!(Proxies::<T>::contains_key(&anon), "anon proxy not created");
|
||||
}: _(RawOrigin::Signed(anon.clone()), caller.clone(), T::ProxyType::default(), 0, height, ext_index)
|
||||
verify {
|
||||
assert!(!Proxies::<T>::contains_key(&anon));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -98,6 +265,10 @@ mod tests {
|
||||
fn test_benchmarks() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_ok!(test_benchmark_proxy::<Test>());
|
||||
assert_ok!(test_benchmark_proxy_announced::<Test>());
|
||||
assert_ok!(test_benchmark_remove_announcement::<Test>());
|
||||
assert_ok!(test_benchmark_reject_announcement::<Test>());
|
||||
assert_ok!(test_benchmark_announce::<Test>());
|
||||
assert_ok!(test_benchmark_add_proxy::<Test>());
|
||||
assert_ok!(test_benchmark_remove_proxy::<Test>());
|
||||
assert_ok!(test_benchmark_remove_proxies::<Test>());
|
||||
|
||||
Reference in New Issue
Block a user