This repository has been archived by the owner on Sep 22, 2022. It is now read-only.
forked from JoshOrndorff/substrate-node-template
-
Notifications
You must be signed in to change notification settings - Fork 23
Add tests to free-calls pallet #189
Open
TarekkMA
wants to merge
4
commits into
dappforce:feature/free-calls
Choose a base branch
from
TarekkMA:tarekkma/free-calls2-tests
base: feature/free-calls
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} | ||
|
@@ -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 = (); | ||
|
@@ -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; | ||
|
@@ -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); | ||
}; | ||
|
||
|
@@ -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, | ||
|
@@ -166,6 +173,7 @@ pub struct ExtBuilder { | |
windows_config: Vec<WindowConfig<BlockNumber>>, | ||
config_hash: ConfigHash, | ||
} | ||
|
||
impl Default for ExtBuilder { | ||
fn default() -> Self { | ||
Self { | ||
|
@@ -176,6 +184,7 @@ impl Default for ExtBuilder { | |
} | ||
} | ||
} | ||
|
||
impl ExtBuilder { | ||
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. Include a test scenario, where we'll use this function: |
||
pub fn call_filter(mut self, call_filter: CallFilterFn) -> Self { | ||
self.call_filter = call_filter; | ||
|
@@ -215,4 +224,4 @@ impl ExtBuilder { | |
|
||
ext | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
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. Seems unused |
||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like config validation function is not covered with tests:
https://github.com/dappforce/subsocial-node/blob/430fe055892bfdda1b328af81b5f9792a2ee559d/pallets/free-calls/src/config.rs#L72