Skip to content
This repository has been archived by the owner on Sep 22, 2022. It is now read-only.

Add tests to free-calls pallet #189

Open
wants to merge 4 commits into
base: feature/free-calls
Choose a base branch
from
Open
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
3 changes: 1 addition & 2 deletions pallets/free-calls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ features = ['derive']
package = 'parity-scale-codec'
version = '2.0.0'


[dependencies]
impl-trait-for-tuples = '0.1.3'
serde = { features = ['derive'], optional = true, version = '1.0.119' }
Expand All @@ -67,4 +66,4 @@ sp-std = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v
sp-core = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.12', default-features = false }
sp-io = { git = 'https://github.com/paritytech/substrate', branch = 'polkadot-v0.9.12', default-features = false }
rand = "0.8.4"
rstest = "0.12.0"
rstest = "0.12.0"
6 changes: 6 additions & 0 deletions pallets/free-calls/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ pub use pallet::*;
#[cfg(test)]
mod mock;

#[cfg(test)]
mod test_pallet;

#[cfg(test)]
mod tests;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
mod weights;
Expand Down
57 changes: 33 additions & 24 deletions pallets/free-calls/src/mock.rs
Original file line number Diff line number Diff line change
@@ -1,55 +1,54 @@
use std::cell::RefCell;
use frame_support::{parameter_types, traits::Contains};
use frame_system as system;
use frame_system::EnsureRoot;
use sp_core::H256;
use sp_io::TestExternalities;
use sp_runtime::{
traits::{BlakeTwo256, IdentityLookup}, testing::Header
};
use sp_runtime::traits::{BlakeTwo256, IdentityLookup};
use sp_std::cell::RefCell;

pub use crate as pallet_free_calls;

use frame_support::{
parameter_types,
};
use frame_support::traits::{Contains};
use frame_system as system;
use frame_system::{EnsureRoot};
use pallet_locker_mirror::{BalanceOf, LockedInfoOf};

pub use crate as pallet_free_calls;
use crate::config::{ConfigHash, RateLimiterConfig, WindowConfig};
use crate::max_quota_percentage;
use crate::quota::NumberOfCalls;
use crate::test_pallet;

pub(crate) type AccountId = u64;
pub(crate) type BlockNumber = u64;

pub(crate) type AccountId = subsocial_primitives::AccountId;
pub(crate) type BlockNumber = subsocial_primitives::BlockNumber;
pub(crate) type Balance = subsocial_primitives::Balance;

type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;

frame_support::construct_runtime!(
pub enum Test where
Block = Block,
NodeBlock = Block,
Block = subsocial_primitives::Block,
NodeBlock = subsocial_primitives::Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: system::{Pallet, Call, Config, Storage, Event<T>},
FreeCalls: pallet_free_calls::{Pallet, Call, Storage, Event<T>},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
LockerMirror: pallet_locker_mirror::{Pallet, Call, Storage, Event<T>},
TestPallet: test_pallet::{Pallet, Call, Storage, Event<T>},
}
);

parameter_types! {
pub const BlockHashCount: u64 = 250;
pub const BlockHashCount: BlockNumber = 250;
pub const SS58Prefix: u8 = 28;
}

pub struct TestBaseCallFilter;

impl Contains<Call> for TestBaseCallFilter {
fn contains(c: &Call) -> bool {
match *c {
Call::FreeCalls(_) => true,
// For benchmarking, this acts as a noop call
Call::System(frame_system::Call::remark { .. }) => true,
// For tests
Call::TestPallet(_) => true,
_ => false,
}
}
Expand All @@ -67,13 +66,13 @@ impl system::Config for Test {
type Hashing = BlakeTwo256;
type AccountId = AccountId;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Header = subsocial_primitives::Header;
type Event = Event;
type BlockHashCount = BlockHashCount;
type DbWeight = ();
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<u64>;
type AccountData = pallet_balances::AccountData<Balance>;
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
Expand All @@ -86,7 +85,7 @@ parameter_types! {
}

impl pallet_balances::Config for Test {
type Balance = u64;
type Balance = Balance;
type DustRemoval = ();
type Event = Event;
type ExistentialDeposit = ExistentialDeposit;
Expand All @@ -105,14 +104,20 @@ impl pallet_locker_mirror::Config for Test {
type WeightInfo = ();
}

impl test_pallet::Config for Test {
type Event = Event;
}

////// Free Call Dependencies


type CallFilterFn = fn(&Call) -> bool;

static DEFAULT_CALL_FILTER_FN: CallFilterFn = |_| true;

type QuotaCalculationFn<T> = fn(<T as frame_system::Config>::BlockNumber, Option<LockedInfoOf<T>>) -> Option<NumberOfCalls>;
static DEFAULT_QUOTA_CALCULATION_FN: QuotaCalculationFn<Test> = |current_block, locked_info| {

static DEFAULT_QUOTA_CALCULATION_FN: QuotaCalculationFn<Test> = |_, _| {
return Some(10);
};

Expand All @@ -135,13 +140,15 @@ thread_local! {
}

pub struct TestCallFilter;

impl Contains<Call> for TestCallFilter {
fn contains(call: &Call) -> bool {
CALL_FILTER.with(|filter| filter.borrow()(call))
}
}

pub struct TestQuotaCalculation;

impl pallet_free_calls::quota_strategy::MaxQuotaCalculationStrategy<<Test as frame_system::Config>::BlockNumber, BalanceOf<Test>> for TestQuotaCalculation {
fn calculate(
current_block: <Test as frame_system::Config>::BlockNumber,
Expand All @@ -166,6 +173,7 @@ pub struct ExtBuilder {
windows_config: Vec<WindowConfig<BlockNumber>>,
config_hash: ConfigHash,
}

impl Default for ExtBuilder {
fn default() -> Self {
Self {
Expand All @@ -176,6 +184,7 @@ impl Default for ExtBuilder {
}
}
}

impl ExtBuilder {
Copy link
Member

@F3Joule F3Joule May 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pub fn call_filter(mut self, call_filter: CallFilterFn) -> Self {
self.call_filter = call_filter;
Expand Down Expand Up @@ -215,4 +224,4 @@ impl ExtBuilder {

ext
}
}
}
91 changes: 91 additions & 0 deletions pallets/free-calls/src/test_pallet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//! Just a dummy pallet to use during the tests
#![cfg_attr(not(feature = "std"), no_std)]

pub use pallet::*;

#[frame_support::pallet]
pub mod pallet {
use frame_support::pallet_prelude::*;
use frame_support::weights::PostDispatchInfo;
use frame_system::pallet_prelude::*;

#[pallet::config]
pub trait Config: frame_system::Config {
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
}

#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(_);


#[pallet::storage]
#[pallet::getter(fn something)]
pub type Something<T> = StorageValue<_, u32>;


#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
ValueStored(u32, T::AccountId),
}

#[pallet::error]
pub enum Error<T> {
DoNotCallMe,
DoNotSendZero,
}


#[pallet::call]
impl<T: Config> Pallet<T> {

#[pallet::weight(10_000)]
pub fn store_value(origin: OriginFor<T>, something: u32) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;

ensure!(something != 0, Error::<T>::DoNotSendZero);

// Update storage.
<Something<T>>::put(something);

// Emit an event.
Self::deposit_event(Event::ValueStored(something, who));

Ok(PostDispatchInfo {
actual_weight: Some(50_000),
pays_fee: Pays::Yes,
})
}

#[pallet::weight(10_000)]
pub fn cause_error(origin: OriginFor<T>) -> DispatchResult {
let _who = ensure_signed(origin)?;

ensure!(false, Error::<T>::DoNotCallMe);

Ok(())
}

#[pallet::weight(12_345)]
pub fn call_a(origin: OriginFor<T>) -> DispatchResult {
let _who = ensure_signed(origin)?;

Ok(())
}

#[pallet::weight(12_345)]
pub fn call_b(origin: OriginFor<T>) -> DispatchResult {
let _who = ensure_signed(origin)?;

Ok(())
}

#[pallet::weight(12_345)]
pub fn call_c(origin: OriginFor<T>) -> DispatchResult {
let _who = ensure_signed(origin)?;

Ok(())
}
Comment on lines +84 to +89
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems unused

}
}
Loading