Skip to content
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

Free TX - council can update free tx per era, fixed benchmarks #177

Merged
merged 2 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions pallets/free-tx/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#![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;

use super::*;
Expand All @@ -12,7 +12,7 @@ use super::*;
benchmarks! {
try_free_call {
let caller: T::AccountId = whitelisted_caller();
FreeCallsRemaining::<T>::insert(&caller, FreeCallInfo { free_calls_remaining: 2 as FreeCallCount, era_index: 1 as EraIndex});
FreeCallsPerEra::<T>::set(Some(2));

let call: <T as Config>::Call = frame_system::Call::<T>::remark { remark: b"entropy rocks".to_vec() }.into();
}: _(RawOrigin::Signed(caller.clone()), Box::new(call))
Expand All @@ -21,9 +21,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!(
<FreeTx<T>>::set_free_calls_per_era(origin, free_calls)
);
}
verify {
assert_eq!(FreeCallsPerEra::<T>::get().unwrap(), free_calls as FreeCallCount);
}
Expand Down
22 changes: 8 additions & 14 deletions pallets/free-tx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
/// <https://docs.substrate.io/reference/frame-pallets/>
Expand All @@ -33,6 +21,7 @@ pub mod pallet {
dispatch::Dispatchable,
pallet_prelude::*,
traits::IsSubType,
transactional,
weights::{GetDispatchInfo, PostDispatchInfo},
};
use frame_system::{pallet_prelude::*, RawOrigin};
Expand All @@ -44,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]
Expand All @@ -57,6 +47,9 @@ pub mod pallet {
+ GetDispatchInfo
+ From<frame_system::Call<Self>>;

// Counsil (or another) can update the number of free transactions per era
type UpdateOrigin: EnsureOrigin<Self::Origin>;

// The weight information of this pallet.
type WeightInfo: WeightInfo;
}
Expand Down Expand Up @@ -162,12 +155,13 @@ 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(<T as crate::Config>::WeightInfo::set_free_calls_per_era())]
#[transactional]
pub fn set_free_calls_per_era(
origin: OriginFor<T>,
free_calls_per_era: FreeCallCount,
) -> DispatchResult {
ensure_root(origin)?;
T::UpdateOrigin::ensure_origin(origin)?;
if free_calls_per_era == 0 {
// make sure that <FreeCallsPerEra<T>>::get() returns None instead of Some(0)
<FreeCallsPerEra<T>>::kill();
Expand Down
10 changes: 8 additions & 2 deletions pallets/free-tx/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<One, AccountId>;
type WeightInfo = ();
}

Expand Down Expand Up @@ -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 * <SessionsPerEra as Get<u32>>::get()));
start_session(era_index * <SessionsPerEra as Get<u32>>::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.
Expand Down
2 changes: 1 addition & 1 deletion pallets/free-tx/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions pallets/free-tx/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -44,6 +45,12 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
.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
Expand All @@ -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))
}
}
4 changes: 4 additions & 0 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AccountId>,
pallet_collective::EnsureProportionAtLeast<AccountId, CouncilCollective, 2, 3>,
>;
type WeightInfo = weights::pallet_free_tx::WeightInfo<Runtime>;
}

Expand Down
35 changes: 19 additions & 16 deletions runtime/src/weights/pallet_free_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -33,10 +27,19 @@ use sp_std::marker::PhantomData;
/// Weight functions for `pallet_free_tx`.
pub struct WeightInfo<T>(PhantomData<T>);
impl<T: frame_system::Config> pallet_free_tx::WeightInfo for WeightInfo<T> {
// 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))
}
}
1 change: 1 addition & 0 deletions scripts/benchmarks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pallets=(
pallet_staking_extension
pallet_constraints
pallet_transaction_pause
pallet_free_tx
)

for p in ${pallets[@]}
Expand Down