From 896853b073e4d03ec23019f0266600a4be03f7c2 Mon Sep 17 00:00:00 2001 From: jakehemmerle Date: Wed, 7 Sep 2022 13:04:47 -0400 Subject: [PATCH 1/2] feat: council can update number of free tx users get per era --- pallets/free-tx/src/benchmarking.rs | 9 +++++++-- pallets/free-tx/src/lib.rs | 17 ++++------------- pallets/free-tx/src/mock.rs | 10 ++++++++-- pallets/free-tx/src/tests.rs | 2 +- runtime/src/lib.rs | 4 ++++ scripts/benchmarks.sh | 1 + 6 files changed, 25 insertions(+), 18 deletions(-) diff --git a/pallets/free-tx/src/benchmarking.rs b/pallets/free-tx/src/benchmarking.rs index 1e99833c2..6c8600b18 100644 --- a/pallets/free-tx/src/benchmarking.rs +++ b/pallets/free-tx/src/benchmarking.rs @@ -2,6 +2,7 @@ #![cfg(feature = "runtime-benchmarks")] use frame_benchmarking::{benchmarks, whitelisted_caller}; +use frame_support::{assert_ok, traits::EnsureOrigin}; use frame_system::RawOrigin; use sp_staking::EraIndex; use sp_std::prelude::Box; @@ -21,9 +22,13 @@ benchmarks! { assert_eq!(free_calls_remaining, 1 as FreeCallCount); } set_free_calls_per_era { - let caller: T::AccountId = whitelisted_caller(); + let origin = T::UpdateOrigin::successful_origin(); let free_calls = 1 as FreeCallCount; - }: _(RawOrigin::Root, free_calls as FreeCallCount) + }: { + assert_ok!( + >::set_free_calls_per_era(origin, free_calls) + ); + } verify { assert_eq!(FreeCallsPerEra::::get().unwrap(), free_calls as FreeCallCount); } diff --git a/pallets/free-tx/src/lib.rs b/pallets/free-tx/src/lib.rs index a195a0f5d..a39e4a691 100644 --- a/pallets/free-tx/src/lib.rs +++ b/pallets/free-tx/src/lib.rs @@ -2,18 +2,6 @@ //! TODO JH: This is NOT SAFE for production yet. This is an MVP and likely DoS-able. -//! TODO JH: Free Transactions per Era -//! [x] FreeTxPerEra StorageValue - Enable pallet by setting it to Some(u16) -//! [x] FreeTxLeft StorageMap(AccountId, u16) - store the number of free transactions left for each -//! account -//! [x] try_free_tx modification -//! [x] SignedExtension modification -//! [] on_idle hook (optional/future) - prunes FreeCallsRemaining -//! [x] reset_free_tx - root function clears FreeTxLeft -//! -//! [] Remove GenesisConfig and fix tests - remove genesis config -//! [] new tests - /// Edit this file to define custom logic or remove it if it is not needed. /// Learn more about FRAME and the core library of Substrate FRAME pallets: /// @@ -57,6 +45,9 @@ pub mod pallet { + GetDispatchInfo + From>; + // Counsil (or another) can update the number of free transactions per era + type UpdateOrigin: EnsureOrigin; + // The weight information of this pallet. type WeightInfo: WeightInfo; } @@ -167,7 +158,7 @@ pub mod pallet { origin: OriginFor, free_calls_per_era: FreeCallCount, ) -> DispatchResult { - ensure_root(origin)?; + T::UpdateOrigin::ensure_origin(origin)?; if free_calls_per_era == 0 { // make sure that >::get() returns None instead of Some(0) >::kill(); diff --git a/pallets/free-tx/src/mock.rs b/pallets/free-tx/src/mock.rs index 7dff188d4..bf1eb77b5 100644 --- a/pallets/free-tx/src/mock.rs +++ b/pallets/free-tx/src/mock.rs @@ -2,10 +2,11 @@ use core::convert::{TryFrom, TryInto}; use frame_election_provider_support::{onchain, SequentialPhragmen, VoteWeight}; use frame_support::{ - parameter_types, + ord_parameter_types, parameter_types, traits::{ConstU32, GenesisBuild, Get, Hooks, OneSessionHandler}, }; use frame_system as system; +use frame_system::EnsureSignedBy; use pallet_session::historical as pallet_session_historical; use pallet_staking::StakerStatus; use sp_core::H256; @@ -258,9 +259,14 @@ impl pallet_staking_extension::Config for Test { type WeightInfo = (); } +ord_parameter_types! { + pub const One: AccountId = 1; +} + impl pallet_free_tx::Config for Test { type Call = Call; type Event = Event; + type UpdateOrigin = EnsureSignedBy; type WeightInfo = (); } @@ -462,7 +468,7 @@ pub(crate) fn current_era() -> EraIndex { FrameStaking::current_era().unwrap() } /// Progress until the given era. pub(crate) fn start_active_era(era_index: EraIndex) { - start_session((era_index * >::get())); + start_session(era_index * >::get()); assert_eq!(active_era(), era_index); // One way or another, current_era must have changed before the active era, so they must match // at this point. diff --git a/pallets/free-tx/src/tests.rs b/pallets/free-tx/src/tests.rs index ed78fc46c..d6b19ffca 100644 --- a/pallets/free-tx/src/tests.rs +++ b/pallets/free-tx/src/tests.rs @@ -105,7 +105,7 @@ fn free_calls_refresh_every_era() { start_active_era(1); // enable free calls via extrinsic - let _ = FreeTx::set_free_calls_per_era(Origin::root(), 5); + let _ = FreeTx::set_free_calls_per_era(Origin::signed(1), 5); assert_eq!(FreeTx::free_calls_remaining(&1u64), 5 as FreeCallCount); // make a call that works, check call is used diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index ad40d2939..de8d7fda6 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -1205,6 +1205,10 @@ impl pallet_bags_list::Config for Runtime { impl pallet_free_tx::Config for Runtime { type Call = Call; type Event = Event; + type UpdateOrigin = EnsureOneOf< + EnsureRoot, + pallet_collective::EnsureProportionAtLeast, + >; type WeightInfo = weights::pallet_free_tx::WeightInfo; } diff --git a/scripts/benchmarks.sh b/scripts/benchmarks.sh index bf0f62355..cee00ad58 100755 --- a/scripts/benchmarks.sh +++ b/scripts/benchmarks.sh @@ -9,6 +9,7 @@ pallets=( pallet_staking_extension pallet_constraints pallet_transaction_pause + pallet_free_tx ) for p in ${pallets[@]} From 75e1eab53090862e0459d5c50ab6fc795c95fd57 Mon Sep 17 00:00:00 2001 From: jakehemmerle Date: Wed, 7 Sep 2022 13:37:37 -0400 Subject: [PATCH 2/2] feat: free-tx fixed benches --- pallets/free-tx/src/benchmarking.rs | 3 +-- pallets/free-tx/src/lib.rs | 5 +++- pallets/free-tx/src/weights.rs | 13 ++++++++++ runtime/src/weights/pallet_free_tx.rs | 35 +++++++++++++++------------ 4 files changed, 37 insertions(+), 19 deletions(-) diff --git a/pallets/free-tx/src/benchmarking.rs b/pallets/free-tx/src/benchmarking.rs index 6c8600b18..71ba9ed0a 100644 --- a/pallets/free-tx/src/benchmarking.rs +++ b/pallets/free-tx/src/benchmarking.rs @@ -4,7 +4,6 @@ use frame_benchmarking::{benchmarks, whitelisted_caller}; use frame_support::{assert_ok, traits::EnsureOrigin}; use frame_system::RawOrigin; -use sp_staking::EraIndex; use sp_std::prelude::Box; use super::*; @@ -13,7 +12,7 @@ use super::*; benchmarks! { try_free_call { let caller: T::AccountId = whitelisted_caller(); - FreeCallsRemaining::::insert(&caller, FreeCallInfo { free_calls_remaining: 2 as FreeCallCount, era_index: 1 as EraIndex}); + FreeCallsPerEra::::set(Some(2)); let call: ::Call = frame_system::Call::::remark { remark: b"entropy rocks".to_vec() }.into(); }: _(RawOrigin::Signed(caller.clone()), Box::new(call)) diff --git a/pallets/free-tx/src/lib.rs b/pallets/free-tx/src/lib.rs index a39e4a691..7be77b805 100644 --- a/pallets/free-tx/src/lib.rs +++ b/pallets/free-tx/src/lib.rs @@ -21,6 +21,7 @@ pub mod pallet { dispatch::Dispatchable, pallet_prelude::*, traits::IsSubType, + transactional, weights::{GetDispatchInfo, PostDispatchInfo}, }; use frame_system::{pallet_prelude::*, RawOrigin}; @@ -32,6 +33,7 @@ pub mod pallet { use sp_staking::EraIndex; use sp_std::{fmt::Debug, prelude::*}; + // use super::*; pub use crate::weights::WeightInfo; #[pallet::config] @@ -153,7 +155,8 @@ pub mod pallet { /// Sets the number of free calls each account gets per era. /// To disable free calls, set this to `0`. /// TODO: weight - #[pallet::weight(10_000)] + #[pallet::weight(::WeightInfo::set_free_calls_per_era())] + #[transactional] pub fn set_free_calls_per_era( origin: OriginFor, free_calls_per_era: FreeCallCount, diff --git a/pallets/free-tx/src/weights.rs b/pallets/free-tx/src/weights.rs index d69e547af..afc5bc234 100644 --- a/pallets/free-tx/src/weights.rs +++ b/pallets/free-tx/src/weights.rs @@ -33,6 +33,7 @@ use sp_std::marker::PhantomData; pub trait WeightInfo { fn try_free_call() -> Weight; + fn set_free_calls_per_era() -> Weight; } /// Weight functions for `pallet_free_tx`. @@ -44,6 +45,12 @@ impl WeightInfo for SubstrateWeight { .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } + + fn set_free_calls_per_era() -> Weight { + (14_000_000 as Weight) + .saturating_add(T::DbWeight::get().reads(1 as Weight)) + .saturating_add(T::DbWeight::get().writes(1 as Weight)) + } } // For backwards compatibility and tests @@ -55,4 +62,10 @@ impl WeightInfo for () { .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } + + fn set_free_calls_per_era() -> Weight { + (14_000_000 as Weight) + .saturating_add(RocksDbWeight::get().reads(1 as Weight)) + .saturating_add(RocksDbWeight::get().writes(1 as Weight)) + } } \ No newline at end of file diff --git a/runtime/src/weights/pallet_free_tx.rs b/runtime/src/weights/pallet_free_tx.rs index 831db8623..07292acc0 100644 --- a/runtime/src/weights/pallet_free_tx.rs +++ b/runtime/src/weights/pallet_free_tx.rs @@ -2,26 +2,20 @@ //! Autogenerated weights for `pallet_free_tx` //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2022-08-12, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2022-09-07, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024 // Executed Command: // ./target/release/entropy // benchmark -// --chain -// dev +// --chain=dev // --execution=wasm // --wasm-execution=compiled -// --pallet -// pallet_free_tx -// --extrinsic -// * -// --steps -// 50 -// --repeat -// 20 -// --output -// ./weights.rs +// --pallet=pallet_free_tx +// --extrinsic=* +// --steps=50 +// --repeat=20 +// --output=./runtime/src/weights/ #![cfg_attr(rustfmt, rustfmt_skip)] #![allow(unused_parens)] @@ -33,10 +27,19 @@ use sp_std::marker::PhantomData; /// Weight functions for `pallet_free_tx`. pub struct WeightInfo(PhantomData); impl pallet_free_tx::WeightInfo for WeightInfo { - // Storage: FreeTx FreeCallsLeft (r:1 w:1) + // Storage: FreeTx FreeCallsPerEra (r:1 w:0) + // Storage: FreeTx FreeCallsRemaining (r:1 w:1) + // Storage: Staking CurrentEra (r:1 w:0) fn try_free_call() -> Weight { - (14_000_000 as Weight) - .saturating_add(T::DbWeight::get().reads(1 as Weight)) + (18_000_000 as Weight) + .saturating_add(T::DbWeight::get().reads(3 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } + // Storage: unknown [0x3a7472616e73616374696f6e5f6c6576656c3a] (r:1 w:1) + // Storage: FreeTx FreeCallsPerEra (r:0 w:1) + fn set_free_calls_per_era() -> Weight { + (4_000_000 as Weight) + .saturating_add(T::DbWeight::get().reads(1 as Weight)) + .saturating_add(T::DbWeight::get().writes(2 as Weight)) + } }