Benchmark Utility Pallet (#5384)

* Move tests

* Move around feature cfgs

* Start benchmarks

* as_multi bench

* finish

* fix tests

* Update frame/utility/src/benchmarking.rs

Co-Authored-By: Marcio Diaz <marcio.diaz@gmail.com>

* Update Cargo.toml

* fix test

Co-authored-by: Marcio Diaz <marcio.diaz@gmail.com>
This commit is contained in:
Shawn Tabrizi
2020-03-31 22:53:21 +02:00
committed by GitHub
parent 63e6f81776
commit 74b008a729
23 changed files with 653 additions and 451 deletions
+1
View File
@@ -4658,6 +4658,7 @@ dependencies = [
name = "pallet-utility"
version = "2.0.0-alpha.5"
dependencies = [
"frame-benchmarking",
"frame-support",
"frame-system",
"pallet-balances",
+5 -5
View File
@@ -138,18 +138,18 @@ runtime-benchmarks = [
"sp-runtime/runtime-benchmarks",
"pallet-balances/runtime-benchmarks",
"pallet-collective/runtime-benchmarks",
"pallet-democracy/runtime-benchmarks",
"pallet-elections-phragmen/runtime-benchmarks",
"pallet-identity/runtime-benchmarks",
"pallet-im-online/runtime-benchmarks",
"pallet-society/runtime-benchmarks",
"pallet-staking/runtime-benchmarks",
"pallet-timestamp/runtime-benchmarks",
"pallet-treasury/runtime-benchmarks",
"pallet-staking/runtime-benchmarks",
"pallet-utility/runtime-benchmarks",
"pallet-vesting/runtime-benchmarks",
"pallet-collective/runtime-benchmarks",
"pallet-session-benchmarking",
"pallet-staking/runtime-benchmarks",
"pallet-society/runtime-benchmarks",
"pallet-im-online/runtime-benchmarks",
"pallet-democracy/runtime-benchmarks",
]
[package.metadata.docs.rs]
+4 -3
View File
@@ -878,15 +878,16 @@ impl_runtime_apis! {
let mut batches = Vec::<BenchmarkBatch>::new();
let params = (&pallet, &benchmark, &lowest_range_values, &highest_range_values, &steps, repeat);
add_benchmark!(params, batches, b"balances", Balances);
add_benchmark!(params, batches, b"im-online", ImOnline);
add_benchmark!(params, batches, b"collective", Council);
add_benchmark!(params, batches, b"democracy", Democracy);
add_benchmark!(params, batches, b"identity", Identity);
add_benchmark!(params, batches, b"im-online", ImOnline);
add_benchmark!(params, batches, b"session", SessionBench::<Runtime>);
add_benchmark!(params, batches, b"staking", Staking);
add_benchmark!(params, batches, b"timestamp", Timestamp);
add_benchmark!(params, batches, b"treasury", Treasury);
add_benchmark!(params, batches, b"utility", Utility);
add_benchmark!(params, batches, b"vesting", Vesting);
add_benchmark!(params, batches, b"democracy", Democracy);
add_benchmark!(params, batches, b"collective", Council);
if batches.is_empty() { return Err("Benchmark not found for this pallet.".into()) }
Ok(batches)
}
@@ -16,6 +16,8 @@
//! Balances pallet benchmarking.
#![cfg(feature = "runtime-benchmarks")]
use super::*;
use frame_system::RawOrigin;
+2 -6
View File
@@ -148,14 +148,10 @@
#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(test)]
mod tests_local;
#[cfg(test)]
mod tests_composite;
#[cfg(test)]
#[macro_use]
mod tests;
#[cfg(feature = "runtime-benchmarks")]
mod tests_local;
mod tests_composite;
mod benchmarking;
use sp_std::prelude::*;
+2
View File
@@ -16,6 +16,8 @@
//! Macro for creating the tests for the module.
#![cfg(test)]
#[macro_export]
macro_rules! decl_tests {
($test:ty, $ext_builder:ty, $existential_deposit:expr) => {
@@ -16,6 +16,8 @@
//! Test utilities
#![cfg(test)]
use sp_runtime::{Perbill, traits::{ConvertInto, IdentityLookup}, testing::Header};
use sp_core::H256;
use sp_io;
@@ -16,6 +16,8 @@
//! Test utilities
#![cfg(test)]
use sp_runtime::{Perbill, traits::{ConvertInto, IdentityLookup}, testing::Header};
use sp_core::H256;
use sp_io;
@@ -16,6 +16,8 @@
//! Benchmarks for common FRAME Pallet operations.
#![cfg(feature = "runtime-benchmarks")]
use super::*;
use frame_system::RawOrigin;
+1 -2
View File
@@ -26,8 +26,7 @@ use frame_system::{self as system, ensure_signed};
use codec::{Encode, Decode};
use sp_std::prelude::Vec;
#[cfg(feature = "runtime-benchmarks")]
pub mod benchmarking;
mod benchmarking;
/// Type alias for currency balance.
pub type BalanceOf<T> = <<T as Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::Balance;
@@ -16,6 +16,8 @@
//! Identity pallet benchmarking.
#![cfg(feature = "runtime-benchmarks")]
use super::*;
use frame_system::RawOrigin;
+1 -2
View File
@@ -78,8 +78,7 @@ use frame_support::{
};
use frame_system::{self as system, ensure_signed, ensure_root};
#[cfg(feature = "runtime-benchmarks")]
pub mod benchmarking;
mod benchmarking;
type BalanceOf<T> = <<T as Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::Balance;
type NegativeImbalanceOf<T> = <<T as Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::NegativeImbalance;
@@ -16,10 +16,10 @@
//! Timestamp pallet benchmarking.
#![cfg(feature = "runtime-benchmarks")]
use super::*;
use sp_std::prelude::*;
use frame_system::RawOrigin;
use frame_benchmarking::benchmarks;
-1
View File
@@ -91,7 +91,6 @@
#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
use sp_std::{result, cmp};
@@ -16,6 +16,8 @@
//! Treasury pallet benchmarking.
#![cfg(feature = "runtime-benchmarks")]
use super::*;
use frame_system::RawOrigin;
-2
View File
@@ -102,9 +102,7 @@ use frame_support::{weights::{Weight, WeighData, SimpleDispatchInfo}, traits::Co
use codec::{Encode, Decode};
use frame_system::{self as system, ensure_signed, ensure_root};
#[cfg(test)]
mod tests;
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
type BalanceOf<T> = <<T as Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::Balance;
+20
View File
@@ -1,3 +1,23 @@
// Copyright 2020 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Treasury pallet tests.
#![cfg(test)]
use super::*;
use frame_support::{
+6
View File
@@ -18,6 +18,8 @@ sp-runtime = { version = "2.0.0-alpha.5", default-features = false, path = "../.
sp-std = { version = "2.0.0-alpha.5", default-features = false, path = "../../primitives/std" }
sp-io = { version = "2.0.0-alpha.5", default-features = false, path = "../../primitives/io" }
frame-benchmarking = { version = "2.0.0-alpha.5", default-features = false, path = "../benchmarking", optional = true }
[dev-dependencies]
sp-core = { version = "2.0.0-alpha.5", path = "../../primitives/core" }
pallet-balances = { version = "2.0.0-alpha.5", path = "../balances" }
@@ -33,6 +35,10 @@ std = [
"sp-io/std",
"sp-std/std"
]
runtime-benchmarks = [
"frame-benchmarking",
"frame-support/runtime-benchmarks",
]
[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
+148
View File
@@ -0,0 +1,148 @@
// Copyright 2019-2020 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
// Benchmarks for Utility Pallet
#![cfg(feature = "runtime-benchmarks")]
use super::*;
use frame_system::RawOrigin;
use frame_benchmarking::{benchmarks, account};
use sp_runtime::traits::Saturating;
use crate::Module as Utility;
const SEED: u32 = 0;
fn setup_multi<T: Trait>(s: u32, z: u32) -> Result<(Vec<T::AccountId>, Box<<T as Trait>::Call>), &'static str>{
let mut signatories: Vec<T::AccountId> = Vec::new();
for i in 0 .. s {
let signatory = account("signatory", i, SEED);
// Give them some balance for a possible deposit
let deposit = T::MultisigDepositBase::get() + T::MultisigDepositFactor::get() * s.into();
let balance = T::Currency::minimum_balance().saturating_mul(100.into()) + deposit;
T::Currency::make_free_balance_be(&signatory, balance);
signatories.push(signatory);
}
signatories.sort();
let call: Box<<T as Trait>::Call> = Box::new(frame_system::Call::remark(vec![0; z as usize]).into());
return Ok((signatories, call))
}
benchmarks! {
_ { }
batch {
let c in 0 .. 1000;
let mut calls: Vec<<T as Trait>::Call> = Vec::new();
for i in 0 .. c {
let call = frame_system::Call::remark(vec![]).into();
calls.push(call);
}
let caller = account("caller", 0, SEED);
}: _(RawOrigin::Signed(caller), calls)
as_sub {
let u in 0 .. 1000;
let caller = account("caller", u, SEED);
let call = Box::new(frame_system::Call::remark(vec![]).into());
}: _(RawOrigin::Signed(caller), u as u16, call)
as_multi_create {
// Signatories, need at least 2 total people
let s in 2 .. T::MaxSignatories::get() as u32;
// Transaction Length
let z in 0 .. 10_000;
let (mut signatories, call) = setup_multi::<T>(s, z)?;
let caller = signatories.pop().ok_or("signatories should have len 2 or more")?;
}: as_multi(RawOrigin::Signed(caller), s as u16, signatories, None, call)
as_multi_approve {
// Signatories, need at least 2 people
let s in 2 .. T::MaxSignatories::get() as u32;
// Transaction Length
let z in 0 .. 10_000;
let (mut signatories, call) = setup_multi::<T>(s, z)?;
let mut signatories2 = signatories.clone();
let caller = signatories.pop().ok_or("signatories should have len 2 or more")?;
// before the call, get the timepoint
let timepoint = Utility::<T>::timepoint();
// Create the multi
Utility::<T>::as_multi(RawOrigin::Signed(caller).into(), s as u16, signatories, None, call.clone())?;
let caller2 = signatories2.remove(0);
}: as_multi(RawOrigin::Signed(caller2), s as u16, signatories2, Some(timepoint), call)
as_multi_complete {
// Signatories, need at least 2 people
let s in 2 .. T::MaxSignatories::get() as u32;
// Transaction Length
let z in 0 .. 10_000;
let (mut signatories, call) = setup_multi::<T>(s, z)?;
let mut signatories2 = signatories.clone();
let caller = signatories.pop().ok_or("signatories should have len 2 or more")?;
// before the call, get the timepoint
let timepoint = Utility::<T>::timepoint();
// Create the multi
Utility::<T>::as_multi(RawOrigin::Signed(caller).into(), s as u16, signatories, None, call.clone())?;
// Everyone except the first person approves
for i in 1 .. s - 1 {
let mut signatories_loop = signatories2.clone();
let caller_loop = signatories_loop.remove(i as usize);
Utility::<T>::as_multi(RawOrigin::Signed(caller_loop).into(), s as u16, signatories_loop, Some(timepoint), call.clone())?;
}
let caller2 = signatories2.remove(0);
}: as_multi(RawOrigin::Signed(caller2), s as u16, signatories2, Some(timepoint), call)
approve_as_multi_create {
// Signatories, need at least 2 people
let s in 2 .. T::MaxSignatories::get() as u32;
// Transaction Length
let z in 0 .. 10_000;
let (mut signatories, call) = setup_multi::<T>(s, z)?;
let caller = signatories.pop().ok_or("signatories should have len 2 or more")?;
let call_hash = call.using_encoded(blake2_256);
// Create the multi
}: approve_as_multi(RawOrigin::Signed(caller), s as u16, signatories, None, call_hash)
approve_as_multi_approve {
// Signatories, need at least 2 people
let s in 2 .. T::MaxSignatories::get() as u32;
// Transaction Length
let z in 0 .. 10_000;
let (mut signatories, call) = setup_multi::<T>(s, z)?;
let mut signatories2 = signatories.clone();
let caller = signatories.pop().ok_or("signatories should have len 2 or more")?;
let call_hash = call.using_encoded(blake2_256);
// before the call, get the timepoint
let timepoint = Utility::<T>::timepoint();
// Create the multi
Utility::<T>::as_multi(RawOrigin::Signed(caller).into(), s as u16, signatories, None, call.clone())?;
let caller2 = signatories2.remove(0);
}: approve_as_multi(RawOrigin::Signed(caller2), s as u16, signatories2, Some(timepoint), call_hash)
cancel_as_multi {
// Signatories, need at least 2 people
let s in 2 .. T::MaxSignatories::get() as u32;
// Transaction Length
let z in 0 .. 10_000;
let (mut signatories, call) = setup_multi::<T>(s, z)?;
let caller = signatories.pop().ok_or("signatories should have len 2 or more")?;
let call_hash = call.using_encoded(blake2_256);
let timepoint = Utility::<T>::timepoint();
// Create the multi
Utility::<T>::as_multi(RawOrigin::Signed(caller.clone()).into(), s as u16, signatories.clone(), None, call.clone())?;
}: _(RawOrigin::Signed(caller), s as u16, signatories, timepoint, call_hash)
}
+4 -426
View File
@@ -72,6 +72,9 @@ use frame_support::{traits::{Get, ReservableCurrency, Currency},
use frame_system::{self as system, ensure_signed};
use sp_runtime::{DispatchError, DispatchResult, traits::Dispatchable};
mod tests;
mod benchmarking;
type BalanceOf<T> = <<T as Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::Balance;
/// Configuration trait.
@@ -80,7 +83,7 @@ pub trait Trait: frame_system::Trait {
type Event: From<Event<Self>> + Into<<Self as frame_system::Trait>::Event>;
/// The overarching call type.
type Call: Parameter + Dispatchable<Origin=Self::Origin> + GetDispatchInfo;
type Call: Parameter + Dispatchable<Origin=Self::Origin> + GetDispatchInfo + From<frame_system::Call<Self>>;
/// The currency mechanism.
type Currency: ReservableCurrency<Self::AccountId>;
@@ -563,428 +566,3 @@ impl<T: Trait> Module<T> {
Ok(signatories)
}
}
#[cfg(test)]
mod tests {
use super::*;
use frame_support::{
assert_ok, assert_noop, impl_outer_origin, parameter_types, impl_outer_dispatch,
weights::Weight, impl_outer_event
};
use sp_core::H256;
use sp_runtime::{Perbill, traits::{BlakeTwo256, IdentityLookup}, testing::Header};
use crate as utility;
impl_outer_origin! {
pub enum Origin for Test where system = frame_system {}
}
impl_outer_event! {
pub enum TestEvent for Test {
system<T>,
pallet_balances<T>,
utility<T>,
}
}
impl_outer_dispatch! {
pub enum Call for Test where origin: Origin {
pallet_balances::Balances,
utility::Utility,
}
}
// For testing the pallet, we construct most of a mock runtime. This means
// first constructing a configuration type (`Test`) which `impl`s each of the
// configuration traits of pallets we want to use.
#[derive(Clone, Eq, PartialEq)]
pub struct Test;
parameter_types! {
pub const BlockHashCount: u64 = 250;
pub const MaximumBlockWeight: Weight = 1024;
pub const MaximumBlockLength: u32 = 2 * 1024;
pub const AvailableBlockRatio: Perbill = Perbill::one();
}
impl frame_system::Trait for Test {
type Origin = Origin;
type Index = u64;
type BlockNumber = u64;
type Hash = H256;
type Call = Call;
type Hashing = BlakeTwo256;
type AccountId = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Event = TestEvent;
type BlockHashCount = BlockHashCount;
type MaximumBlockWeight = MaximumBlockWeight;
type MaximumBlockLength = MaximumBlockLength;
type AvailableBlockRatio = AvailableBlockRatio;
type Version = ();
type ModuleToIndex = ();
type AccountData = pallet_balances::AccountData<u64>;
type OnNewAccount = ();
type OnKilledAccount = ();
}
parameter_types! {
pub const ExistentialDeposit: u64 = 1;
}
impl pallet_balances::Trait for Test {
type Balance = u64;
type Event = TestEvent;
type DustRemoval = ();
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = System;
}
parameter_types! {
pub const MultisigDepositBase: u64 = 1;
pub const MultisigDepositFactor: u64 = 1;
pub const MaxSignatories: u16 = 3;
}
impl Trait for Test {
type Event = TestEvent;
type Call = Call;
type Currency = Balances;
type MultisigDepositBase = MultisigDepositBase;
type MultisigDepositFactor = MultisigDepositFactor;
type MaxSignatories = MaxSignatories;
}
type System = frame_system::Module<Test>;
type Balances = pallet_balances::Module<Test>;
type Utility = Module<Test>;
use pallet_balances::Call as BalancesCall;
use pallet_balances::Error as BalancesError;
fn new_test_ext() -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::default().build_storage::<Test>().unwrap();
pallet_balances::GenesisConfig::<Test> {
balances: vec![(1, 10), (2, 10), (3, 10), (4, 10), (5, 10)],
}.assimilate_storage(&mut t).unwrap();
let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| System::set_block_number(1));
ext
}
fn last_event() -> TestEvent {
system::Module::<Test>::events().pop().map(|e| e.event).expect("Event expected")
}
fn expect_event<E: Into<TestEvent>>(e: E) {
assert_eq!(last_event(), e.into());
}
fn now() -> Timepoint<u64> {
Utility::timepoint()
}
#[test]
fn multisig_deposit_is_taken_and_returned() {
new_test_ext().execute_with(|| {
let multi = Utility::multi_account_id(&[1, 2, 3][..], 2);
assert_ok!(Balances::transfer(Origin::signed(1), multi, 5));
assert_ok!(Balances::transfer(Origin::signed(2), multi, 5));
assert_ok!(Balances::transfer(Origin::signed(3), multi, 5));
let call = Box::new(Call::Balances(BalancesCall::transfer(6, 15)));
assert_ok!(Utility::as_multi(Origin::signed(1), 2, vec![2, 3], None, call.clone()));
assert_eq!(Balances::free_balance(1), 2);
assert_eq!(Balances::reserved_balance(1), 3);
assert_ok!(Utility::as_multi(Origin::signed(2), 2, vec![1, 3], Some(now()), call));
assert_eq!(Balances::free_balance(1), 5);
assert_eq!(Balances::reserved_balance(1), 0);
});
}
#[test]
fn cancel_multisig_returns_deposit() {
new_test_ext().execute_with(|| {
let call = Box::new(Call::Balances(BalancesCall::transfer(6, 15)));
let hash = call.using_encoded(blake2_256);
assert_ok!(Utility::approve_as_multi(Origin::signed(1), 3, vec![2, 3], None, hash.clone()));
assert_ok!(Utility::approve_as_multi(Origin::signed(2), 3, vec![1, 3], Some(now()), hash.clone()));
assert_eq!(Balances::free_balance(1), 6);
assert_eq!(Balances::reserved_balance(1), 4);
assert_ok!(
Utility::cancel_as_multi(Origin::signed(1), 3, vec![2, 3], now(), hash.clone()),
);
assert_eq!(Balances::free_balance(1), 10);
assert_eq!(Balances::reserved_balance(1), 0);
});
}
#[test]
fn timepoint_checking_works() {
new_test_ext().execute_with(|| {
let multi = Utility::multi_account_id(&[1, 2, 3][..], 2);
assert_ok!(Balances::transfer(Origin::signed(1), multi, 5));
assert_ok!(Balances::transfer(Origin::signed(2), multi, 5));
assert_ok!(Balances::transfer(Origin::signed(3), multi, 5));
let call = Box::new(Call::Balances(BalancesCall::transfer(6, 15)));
let hash = call.using_encoded(blake2_256);
assert_noop!(
Utility::approve_as_multi(Origin::signed(2), 2, vec![1, 3], Some(now()), hash.clone()),
Error::<Test>::UnexpectedTimepoint,
);
assert_ok!(Utility::approve_as_multi(Origin::signed(1), 2, vec![2, 3], None, hash));
assert_noop!(
Utility::as_multi(Origin::signed(2), 2, vec![1, 3], None, call.clone()),
Error::<Test>::NoTimepoint,
);
let later = Timepoint { index: 1, .. now() };
assert_noop!(
Utility::as_multi(Origin::signed(2), 2, vec![1, 3], Some(later), call.clone()),
Error::<Test>::WrongTimepoint,
);
});
}
#[test]
fn multisig_2_of_3_works() {
new_test_ext().execute_with(|| {
let multi = Utility::multi_account_id(&[1, 2, 3][..], 2);
assert_ok!(Balances::transfer(Origin::signed(1), multi, 5));
assert_ok!(Balances::transfer(Origin::signed(2), multi, 5));
assert_ok!(Balances::transfer(Origin::signed(3), multi, 5));
let call = Box::new(Call::Balances(BalancesCall::transfer(6, 15)));
let hash = call.using_encoded(blake2_256);
assert_ok!(Utility::approve_as_multi(Origin::signed(1), 2, vec![2, 3], None, hash));
assert_eq!(Balances::free_balance(6), 0);
assert_ok!(Utility::as_multi(Origin::signed(2), 2, vec![1, 3], Some(now()), call));
assert_eq!(Balances::free_balance(6), 15);
});
}
#[test]
fn multisig_3_of_3_works() {
new_test_ext().execute_with(|| {
let multi = Utility::multi_account_id(&[1, 2, 3][..], 3);
assert_ok!(Balances::transfer(Origin::signed(1), multi, 5));
assert_ok!(Balances::transfer(Origin::signed(2), multi, 5));
assert_ok!(Balances::transfer(Origin::signed(3), multi, 5));
let call = Box::new(Call::Balances(BalancesCall::transfer(6, 15)));
let hash = call.using_encoded(blake2_256);
assert_ok!(Utility::approve_as_multi(Origin::signed(1), 3, vec![2, 3], None, hash.clone()));
assert_ok!(Utility::approve_as_multi(Origin::signed(2), 3, vec![1, 3], Some(now()), hash.clone()));
assert_eq!(Balances::free_balance(6), 0);
assert_ok!(Utility::as_multi(Origin::signed(3), 3, vec![1, 2], Some(now()), call));
assert_eq!(Balances::free_balance(6), 15);
});
}
#[test]
fn cancel_multisig_works() {
new_test_ext().execute_with(|| {
let call = Box::new(Call::Balances(BalancesCall::transfer(6, 15)));
let hash = call.using_encoded(blake2_256);
assert_ok!(Utility::approve_as_multi(Origin::signed(1), 3, vec![2, 3], None, hash.clone()));
assert_ok!(Utility::approve_as_multi(Origin::signed(2), 3, vec![1, 3], Some(now()), hash.clone()));
assert_noop!(
Utility::cancel_as_multi(Origin::signed(2), 3, vec![1, 3], now(), hash.clone()),
Error::<Test>::NotOwner,
);
assert_ok!(
Utility::cancel_as_multi(Origin::signed(1), 3, vec![2, 3], now(), hash.clone()),
);
});
}
#[test]
fn multisig_2_of_3_as_multi_works() {
new_test_ext().execute_with(|| {
let multi = Utility::multi_account_id(&[1, 2, 3][..], 2);
assert_ok!(Balances::transfer(Origin::signed(1), multi, 5));
assert_ok!(Balances::transfer(Origin::signed(2), multi, 5));
assert_ok!(Balances::transfer(Origin::signed(3), multi, 5));
let call = Box::new(Call::Balances(BalancesCall::transfer(6, 15)));
assert_ok!(Utility::as_multi(Origin::signed(1), 2, vec![2, 3], None, call.clone()));
assert_eq!(Balances::free_balance(6), 0);
assert_ok!(Utility::as_multi(Origin::signed(2), 2, vec![1, 3], Some(now()), call));
assert_eq!(Balances::free_balance(6), 15);
});
}
#[test]
fn multisig_2_of_3_as_multi_with_many_calls_works() {
new_test_ext().execute_with(|| {
let multi = Utility::multi_account_id(&[1, 2, 3][..], 2);
assert_ok!(Balances::transfer(Origin::signed(1), multi, 5));
assert_ok!(Balances::transfer(Origin::signed(2), multi, 5));
assert_ok!(Balances::transfer(Origin::signed(3), multi, 5));
let call1 = Box::new(Call::Balances(BalancesCall::transfer(6, 10)));
let call2 = Box::new(Call::Balances(BalancesCall::transfer(7, 5)));
assert_ok!(Utility::as_multi(Origin::signed(1), 2, vec![2, 3], None, call1.clone()));
assert_ok!(Utility::as_multi(Origin::signed(2), 2, vec![1, 3], None, call2.clone()));
assert_ok!(Utility::as_multi(Origin::signed(3), 2, vec![1, 2], Some(now()), call2));
assert_ok!(Utility::as_multi(Origin::signed(3), 2, vec![1, 2], Some(now()), call1));
assert_eq!(Balances::free_balance(6), 10);
assert_eq!(Balances::free_balance(7), 5);
});
}
#[test]
fn multisig_2_of_3_cannot_reissue_same_call() {
new_test_ext().execute_with(|| {
let multi = Utility::multi_account_id(&[1, 2, 3][..], 2);
assert_ok!(Balances::transfer(Origin::signed(1), multi, 5));
assert_ok!(Balances::transfer(Origin::signed(2), multi, 5));
assert_ok!(Balances::transfer(Origin::signed(3), multi, 5));
let call = Box::new(Call::Balances(BalancesCall::transfer(6, 10)));
assert_ok!(Utility::as_multi(Origin::signed(1), 2, vec![2, 3], None, call.clone()));
assert_ok!(Utility::as_multi(Origin::signed(2), 2, vec![1, 3], Some(now()), call.clone()));
assert_eq!(Balances::free_balance(multi), 5);
assert_ok!(Utility::as_multi(Origin::signed(1), 2, vec![2, 3], None, call.clone()));
assert_ok!(Utility::as_multi(Origin::signed(3), 2, vec![1, 2], Some(now()), call));
let err = DispatchError::from(BalancesError::<Test, _>::InsufficientBalance).stripped();
expect_event(RawEvent::MultisigExecuted(3, now(), multi, Err(err)));
});
}
#[test]
fn zero_threshold_fails() {
new_test_ext().execute_with(|| {
let call = Box::new(Call::Balances(BalancesCall::transfer(6, 15)));
assert_noop!(
Utility::as_multi(Origin::signed(1), 0, vec![2], None, call),
Error::<Test>::ZeroThreshold,
);
});
}
#[test]
fn too_many_signatories_fails() {
new_test_ext().execute_with(|| {
let call = Box::new(Call::Balances(BalancesCall::transfer(6, 15)));
assert_noop!(
Utility::as_multi(Origin::signed(1), 2, vec![2, 3, 4], None, call.clone()),
Error::<Test>::TooManySignatories,
);
});
}
#[test]
fn duplicate_approvals_are_ignored() {
new_test_ext().execute_with(|| {
let call = Box::new(Call::Balances(BalancesCall::transfer(6, 15)));
let hash = call.using_encoded(blake2_256);
assert_ok!(Utility::approve_as_multi(Origin::signed(1), 2, vec![2, 3], None, hash.clone()));
assert_noop!(
Utility::approve_as_multi(Origin::signed(1), 2, vec![2, 3], Some(now()), hash.clone()),
Error::<Test>::AlreadyApproved,
);
assert_ok!(Utility::approve_as_multi(Origin::signed(2), 2, vec![1, 3], Some(now()), hash.clone()));
assert_noop!(
Utility::approve_as_multi(Origin::signed(3), 2, vec![1, 2], Some(now()), hash.clone()),
Error::<Test>::NoApprovalsNeeded,
);
});
}
#[test]
fn multisig_1_of_3_works() {
new_test_ext().execute_with(|| {
let multi = Utility::multi_account_id(&[1, 2, 3][..], 1);
assert_ok!(Balances::transfer(Origin::signed(1), multi, 5));
assert_ok!(Balances::transfer(Origin::signed(2), multi, 5));
assert_ok!(Balances::transfer(Origin::signed(3), multi, 5));
let call = Box::new(Call::Balances(BalancesCall::transfer(6, 15)));
let hash = call.using_encoded(blake2_256);
assert_noop!(
Utility::approve_as_multi(Origin::signed(1), 1, vec![2, 3], None, hash.clone()),
Error::<Test>::NoApprovalsNeeded,
);
assert_noop!(
Utility::as_multi(Origin::signed(4), 1, vec![2, 3], None, call.clone()),
BalancesError::<Test, _>::InsufficientBalance,
);
assert_ok!(Utility::as_multi(Origin::signed(1), 1, vec![2, 3], None, call));
assert_eq!(Balances::free_balance(6), 15);
});
}
#[test]
fn as_sub_works() {
new_test_ext().execute_with(|| {
let sub_1_0 = Utility::sub_account_id(1, 0);
assert_ok!(Balances::transfer(Origin::signed(1), sub_1_0, 5));
assert_noop!(Utility::as_sub(
Origin::signed(1),
1,
Box::new(Call::Balances(BalancesCall::transfer(6, 3))),
), BalancesError::<Test, _>::InsufficientBalance);
assert_ok!(Utility::as_sub(
Origin::signed(1),
0,
Box::new(Call::Balances(BalancesCall::transfer(2, 3))),
));
assert_eq!(Balances::free_balance(sub_1_0), 2);
assert_eq!(Balances::free_balance(2), 13);
});
}
#[test]
fn batch_with_root_works() {
new_test_ext().execute_with(|| {
assert_eq!(Balances::free_balance(1), 10);
assert_eq!(Balances::free_balance(2), 10);
assert_ok!(Utility::batch(Origin::ROOT, vec![
Call::Balances(BalancesCall::force_transfer(1, 2, 5)),
Call::Balances(BalancesCall::force_transfer(1, 2, 5))
]));
assert_eq!(Balances::free_balance(1), 0);
assert_eq!(Balances::free_balance(2), 20);
});
}
#[test]
fn batch_with_signed_works() {
new_test_ext().execute_with(|| {
assert_eq!(Balances::free_balance(1), 10);
assert_eq!(Balances::free_balance(2), 10);
assert_ok!(
Utility::batch(Origin::signed(1), vec![
Call::Balances(BalancesCall::transfer(2, 5)),
Call::Balances(BalancesCall::transfer(2, 5))
]),
);
assert_eq!(Balances::free_balance(1), 0);
assert_eq!(Balances::free_balance(2), 20);
});
}
#[test]
fn batch_early_exit_works() {
new_test_ext().execute_with(|| {
assert_eq!(Balances::free_balance(1), 10);
assert_eq!(Balances::free_balance(2), 10);
assert_ok!(
Utility::batch(Origin::signed(1), vec![
Call::Balances(BalancesCall::transfer(2, 5)),
Call::Balances(BalancesCall::transfer(2, 10)),
Call::Balances(BalancesCall::transfer(2, 5)),
]),
);
assert_eq!(Balances::free_balance(1), 5);
assert_eq!(Balances::free_balance(2), 15);
});
}
}
+442
View File
@@ -0,0 +1,442 @@
// Copyright 2019-2020 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
// Tests for Utility Pallet
#![cfg(test)]
use super::*;
use frame_support::{
assert_ok, assert_noop, impl_outer_origin, parameter_types, impl_outer_dispatch,
weights::Weight, impl_outer_event
};
use sp_core::H256;
use sp_runtime::{Perbill, traits::{BlakeTwo256, IdentityLookup}, testing::Header};
use crate as utility;
impl_outer_origin! {
pub enum Origin for Test where system = frame_system {}
}
impl_outer_event! {
pub enum TestEvent for Test {
system<T>,
pallet_balances<T>,
utility<T>,
}
}
impl_outer_dispatch! {
pub enum Call for Test where origin: Origin {
frame_system::System,
pallet_balances::Balances,
utility::Utility,
}
}
// For testing the pallet, we construct most of a mock runtime. This means
// first constructing a configuration type (`Test`) which `impl`s each of the
// configuration traits of pallets we want to use.
#[derive(Clone, Eq, PartialEq)]
pub struct Test;
parameter_types! {
pub const BlockHashCount: u64 = 250;
pub const MaximumBlockWeight: Weight = 1024;
pub const MaximumBlockLength: u32 = 2 * 1024;
pub const AvailableBlockRatio: Perbill = Perbill::one();
}
impl frame_system::Trait for Test {
type Origin = Origin;
type Index = u64;
type BlockNumber = u64;
type Hash = H256;
type Call = Call;
type Hashing = BlakeTwo256;
type AccountId = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Event = TestEvent;
type BlockHashCount = BlockHashCount;
type MaximumBlockWeight = MaximumBlockWeight;
type MaximumBlockLength = MaximumBlockLength;
type AvailableBlockRatio = AvailableBlockRatio;
type Version = ();
type ModuleToIndex = ();
type AccountData = pallet_balances::AccountData<u64>;
type OnNewAccount = ();
type OnKilledAccount = ();
}
parameter_types! {
pub const ExistentialDeposit: u64 = 1;
}
impl pallet_balances::Trait for Test {
type Balance = u64;
type Event = TestEvent;
type DustRemoval = ();
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = System;
}
parameter_types! {
pub const MultisigDepositBase: u64 = 1;
pub const MultisigDepositFactor: u64 = 1;
pub const MaxSignatories: u16 = 3;
}
impl Trait for Test {
type Event = TestEvent;
type Call = Call;
type Currency = Balances;
type MultisigDepositBase = MultisigDepositBase;
type MultisigDepositFactor = MultisigDepositFactor;
type MaxSignatories = MaxSignatories;
}
type System = frame_system::Module<Test>;
type Balances = pallet_balances::Module<Test>;
type Utility = Module<Test>;
use pallet_balances::Call as BalancesCall;
use pallet_balances::Error as BalancesError;
fn new_test_ext() -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::default().build_storage::<Test>().unwrap();
pallet_balances::GenesisConfig::<Test> {
balances: vec![(1, 10), (2, 10), (3, 10), (4, 10), (5, 10)],
}.assimilate_storage(&mut t).unwrap();
let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| System::set_block_number(1));
ext
}
fn last_event() -> TestEvent {
system::Module::<Test>::events().pop().map(|e| e.event).expect("Event expected")
}
fn expect_event<E: Into<TestEvent>>(e: E) {
assert_eq!(last_event(), e.into());
}
fn now() -> Timepoint<u64> {
Utility::timepoint()
}
#[test]
fn multisig_deposit_is_taken_and_returned() {
new_test_ext().execute_with(|| {
let multi = Utility::multi_account_id(&[1, 2, 3][..], 2);
assert_ok!(Balances::transfer(Origin::signed(1), multi, 5));
assert_ok!(Balances::transfer(Origin::signed(2), multi, 5));
assert_ok!(Balances::transfer(Origin::signed(3), multi, 5));
let call = Box::new(Call::Balances(BalancesCall::transfer(6, 15)));
assert_ok!(Utility::as_multi(Origin::signed(1), 2, vec![2, 3], None, call.clone()));
assert_eq!(Balances::free_balance(1), 2);
assert_eq!(Balances::reserved_balance(1), 3);
assert_ok!(Utility::as_multi(Origin::signed(2), 2, vec![1, 3], Some(now()), call));
assert_eq!(Balances::free_balance(1), 5);
assert_eq!(Balances::reserved_balance(1), 0);
});
}
#[test]
fn cancel_multisig_returns_deposit() {
new_test_ext().execute_with(|| {
let call = Box::new(Call::Balances(BalancesCall::transfer(6, 15)));
let hash = call.using_encoded(blake2_256);
assert_ok!(Utility::approve_as_multi(Origin::signed(1), 3, vec![2, 3], None, hash.clone()));
assert_ok!(Utility::approve_as_multi(Origin::signed(2), 3, vec![1, 3], Some(now()), hash.clone()));
assert_eq!(Balances::free_balance(1), 6);
assert_eq!(Balances::reserved_balance(1), 4);
assert_ok!(
Utility::cancel_as_multi(Origin::signed(1), 3, vec![2, 3], now(), hash.clone()),
);
assert_eq!(Balances::free_balance(1), 10);
assert_eq!(Balances::reserved_balance(1), 0);
});
}
#[test]
fn timepoint_checking_works() {
new_test_ext().execute_with(|| {
let multi = Utility::multi_account_id(&[1, 2, 3][..], 2);
assert_ok!(Balances::transfer(Origin::signed(1), multi, 5));
assert_ok!(Balances::transfer(Origin::signed(2), multi, 5));
assert_ok!(Balances::transfer(Origin::signed(3), multi, 5));
let call = Box::new(Call::Balances(BalancesCall::transfer(6, 15)));
let hash = call.using_encoded(blake2_256);
assert_noop!(
Utility::approve_as_multi(Origin::signed(2), 2, vec![1, 3], Some(now()), hash.clone()),
Error::<Test>::UnexpectedTimepoint,
);
assert_ok!(Utility::approve_as_multi(Origin::signed(1), 2, vec![2, 3], None, hash));
assert_noop!(
Utility::as_multi(Origin::signed(2), 2, vec![1, 3], None, call.clone()),
Error::<Test>::NoTimepoint,
);
let later = Timepoint { index: 1, .. now() };
assert_noop!(
Utility::as_multi(Origin::signed(2), 2, vec![1, 3], Some(later), call.clone()),
Error::<Test>::WrongTimepoint,
);
});
}
#[test]
fn multisig_2_of_3_works() {
new_test_ext().execute_with(|| {
let multi = Utility::multi_account_id(&[1, 2, 3][..], 2);
assert_ok!(Balances::transfer(Origin::signed(1), multi, 5));
assert_ok!(Balances::transfer(Origin::signed(2), multi, 5));
assert_ok!(Balances::transfer(Origin::signed(3), multi, 5));
let call = Box::new(Call::Balances(BalancesCall::transfer(6, 15)));
let hash = call.using_encoded(blake2_256);
assert_ok!(Utility::approve_as_multi(Origin::signed(1), 2, vec![2, 3], None, hash));
assert_eq!(Balances::free_balance(6), 0);
assert_ok!(Utility::as_multi(Origin::signed(2), 2, vec![1, 3], Some(now()), call));
assert_eq!(Balances::free_balance(6), 15);
});
}
#[test]
fn multisig_3_of_3_works() {
new_test_ext().execute_with(|| {
let multi = Utility::multi_account_id(&[1, 2, 3][..], 3);
assert_ok!(Balances::transfer(Origin::signed(1), multi, 5));
assert_ok!(Balances::transfer(Origin::signed(2), multi, 5));
assert_ok!(Balances::transfer(Origin::signed(3), multi, 5));
let call = Box::new(Call::Balances(BalancesCall::transfer(6, 15)));
let hash = call.using_encoded(blake2_256);
assert_ok!(Utility::approve_as_multi(Origin::signed(1), 3, vec![2, 3], None, hash.clone()));
assert_ok!(Utility::approve_as_multi(Origin::signed(2), 3, vec![1, 3], Some(now()), hash.clone()));
assert_eq!(Balances::free_balance(6), 0);
assert_ok!(Utility::as_multi(Origin::signed(3), 3, vec![1, 2], Some(now()), call));
assert_eq!(Balances::free_balance(6), 15);
});
}
#[test]
fn cancel_multisig_works() {
new_test_ext().execute_with(|| {
let call = Box::new(Call::Balances(BalancesCall::transfer(6, 15)));
let hash = call.using_encoded(blake2_256);
assert_ok!(Utility::approve_as_multi(Origin::signed(1), 3, vec![2, 3], None, hash.clone()));
assert_ok!(Utility::approve_as_multi(Origin::signed(2), 3, vec![1, 3], Some(now()), hash.clone()));
assert_noop!(
Utility::cancel_as_multi(Origin::signed(2), 3, vec![1, 3], now(), hash.clone()),
Error::<Test>::NotOwner,
);
assert_ok!(
Utility::cancel_as_multi(Origin::signed(1), 3, vec![2, 3], now(), hash.clone()),
);
});
}
#[test]
fn multisig_2_of_3_as_multi_works() {
new_test_ext().execute_with(|| {
let multi = Utility::multi_account_id(&[1, 2, 3][..], 2);
assert_ok!(Balances::transfer(Origin::signed(1), multi, 5));
assert_ok!(Balances::transfer(Origin::signed(2), multi, 5));
assert_ok!(Balances::transfer(Origin::signed(3), multi, 5));
let call = Box::new(Call::Balances(BalancesCall::transfer(6, 15)));
assert_ok!(Utility::as_multi(Origin::signed(1), 2, vec![2, 3], None, call.clone()));
assert_eq!(Balances::free_balance(6), 0);
assert_ok!(Utility::as_multi(Origin::signed(2), 2, vec![1, 3], Some(now()), call));
assert_eq!(Balances::free_balance(6), 15);
});
}
#[test]
fn multisig_2_of_3_as_multi_with_many_calls_works() {
new_test_ext().execute_with(|| {
let multi = Utility::multi_account_id(&[1, 2, 3][..], 2);
assert_ok!(Balances::transfer(Origin::signed(1), multi, 5));
assert_ok!(Balances::transfer(Origin::signed(2), multi, 5));
assert_ok!(Balances::transfer(Origin::signed(3), multi, 5));
let call1 = Box::new(Call::Balances(BalancesCall::transfer(6, 10)));
let call2 = Box::new(Call::Balances(BalancesCall::transfer(7, 5)));
assert_ok!(Utility::as_multi(Origin::signed(1), 2, vec![2, 3], None, call1.clone()));
assert_ok!(Utility::as_multi(Origin::signed(2), 2, vec![1, 3], None, call2.clone()));
assert_ok!(Utility::as_multi(Origin::signed(3), 2, vec![1, 2], Some(now()), call2));
assert_ok!(Utility::as_multi(Origin::signed(3), 2, vec![1, 2], Some(now()), call1));
assert_eq!(Balances::free_balance(6), 10);
assert_eq!(Balances::free_balance(7), 5);
});
}
#[test]
fn multisig_2_of_3_cannot_reissue_same_call() {
new_test_ext().execute_with(|| {
let multi = Utility::multi_account_id(&[1, 2, 3][..], 2);
assert_ok!(Balances::transfer(Origin::signed(1), multi, 5));
assert_ok!(Balances::transfer(Origin::signed(2), multi, 5));
assert_ok!(Balances::transfer(Origin::signed(3), multi, 5));
let call = Box::new(Call::Balances(BalancesCall::transfer(6, 10)));
assert_ok!(Utility::as_multi(Origin::signed(1), 2, vec![2, 3], None, call.clone()));
assert_ok!(Utility::as_multi(Origin::signed(2), 2, vec![1, 3], Some(now()), call.clone()));
assert_eq!(Balances::free_balance(multi), 5);
assert_ok!(Utility::as_multi(Origin::signed(1), 2, vec![2, 3], None, call.clone()));
assert_ok!(Utility::as_multi(Origin::signed(3), 2, vec![1, 2], Some(now()), call));
let err = DispatchError::from(BalancesError::<Test, _>::InsufficientBalance).stripped();
expect_event(RawEvent::MultisigExecuted(3, now(), multi, Err(err)));
});
}
#[test]
fn zero_threshold_fails() {
new_test_ext().execute_with(|| {
let call = Box::new(Call::Balances(BalancesCall::transfer(6, 15)));
assert_noop!(
Utility::as_multi(Origin::signed(1), 0, vec![2], None, call),
Error::<Test>::ZeroThreshold,
);
});
}
#[test]
fn too_many_signatories_fails() {
new_test_ext().execute_with(|| {
let call = Box::new(Call::Balances(BalancesCall::transfer(6, 15)));
assert_noop!(
Utility::as_multi(Origin::signed(1), 2, vec![2, 3, 4], None, call.clone()),
Error::<Test>::TooManySignatories,
);
});
}
#[test]
fn duplicate_approvals_are_ignored() {
new_test_ext().execute_with(|| {
let call = Box::new(Call::Balances(BalancesCall::transfer(6, 15)));
let hash = call.using_encoded(blake2_256);
assert_ok!(Utility::approve_as_multi(Origin::signed(1), 2, vec![2, 3], None, hash.clone()));
assert_noop!(
Utility::approve_as_multi(Origin::signed(1), 2, vec![2, 3], Some(now()), hash.clone()),
Error::<Test>::AlreadyApproved,
);
assert_ok!(Utility::approve_as_multi(Origin::signed(2), 2, vec![1, 3], Some(now()), hash.clone()));
assert_noop!(
Utility::approve_as_multi(Origin::signed(3), 2, vec![1, 2], Some(now()), hash.clone()),
Error::<Test>::NoApprovalsNeeded,
);
});
}
#[test]
fn multisig_1_of_3_works() {
new_test_ext().execute_with(|| {
let multi = Utility::multi_account_id(&[1, 2, 3][..], 1);
assert_ok!(Balances::transfer(Origin::signed(1), multi, 5));
assert_ok!(Balances::transfer(Origin::signed(2), multi, 5));
assert_ok!(Balances::transfer(Origin::signed(3), multi, 5));
let call = Box::new(Call::Balances(BalancesCall::transfer(6, 15)));
let hash = call.using_encoded(blake2_256);
assert_noop!(
Utility::approve_as_multi(Origin::signed(1), 1, vec![2, 3], None, hash.clone()),
Error::<Test>::NoApprovalsNeeded,
);
assert_noop!(
Utility::as_multi(Origin::signed(4), 1, vec![2, 3], None, call.clone()),
BalancesError::<Test, _>::InsufficientBalance,
);
assert_ok!(Utility::as_multi(Origin::signed(1), 1, vec![2, 3], None, call));
assert_eq!(Balances::free_balance(6), 15);
});
}
#[test]
fn as_sub_works() {
new_test_ext().execute_with(|| {
let sub_1_0 = Utility::sub_account_id(1, 0);
assert_ok!(Balances::transfer(Origin::signed(1), sub_1_0, 5));
assert_noop!(Utility::as_sub(
Origin::signed(1),
1,
Box::new(Call::Balances(BalancesCall::transfer(6, 3))),
), BalancesError::<Test, _>::InsufficientBalance);
assert_ok!(Utility::as_sub(
Origin::signed(1),
0,
Box::new(Call::Balances(BalancesCall::transfer(2, 3))),
));
assert_eq!(Balances::free_balance(sub_1_0), 2);
assert_eq!(Balances::free_balance(2), 13);
});
}
#[test]
fn batch_with_root_works() {
new_test_ext().execute_with(|| {
assert_eq!(Balances::free_balance(1), 10);
assert_eq!(Balances::free_balance(2), 10);
assert_ok!(Utility::batch(Origin::ROOT, vec![
Call::Balances(BalancesCall::force_transfer(1, 2, 5)),
Call::Balances(BalancesCall::force_transfer(1, 2, 5))
]));
assert_eq!(Balances::free_balance(1), 0);
assert_eq!(Balances::free_balance(2), 20);
});
}
#[test]
fn batch_with_signed_works() {
new_test_ext().execute_with(|| {
assert_eq!(Balances::free_balance(1), 10);
assert_eq!(Balances::free_balance(2), 10);
assert_ok!(
Utility::batch(Origin::signed(1), vec![
Call::Balances(BalancesCall::transfer(2, 5)),
Call::Balances(BalancesCall::transfer(2, 5))
]),
);
assert_eq!(Balances::free_balance(1), 0);
assert_eq!(Balances::free_balance(2), 20);
});
}
#[test]
fn batch_early_exit_works() {
new_test_ext().execute_with(|| {
assert_eq!(Balances::free_balance(1), 10);
assert_eq!(Balances::free_balance(2), 10);
assert_ok!(
Utility::batch(Origin::signed(1), vec![
Call::Balances(BalancesCall::transfer(2, 5)),
Call::Balances(BalancesCall::transfer(2, 10)),
Call::Balances(BalancesCall::transfer(2, 5)),
]),
);
assert_eq!(Balances::free_balance(1), 5);
assert_eq!(Balances::free_balance(2), 15);
});
}
+3 -1
View File
@@ -16,6 +16,8 @@
//! Vesting pallet benchmarking.
#![cfg(feature = "runtime-benchmarks")]
use super::*;
use frame_system::{RawOrigin, Module as System};
@@ -119,6 +121,6 @@ benchmarks! {
};
let _ = T::Currency::make_free_balance_be(&from, transfer_amount * 10.into());
}: _(RawOrigin::Signed(from), to_lookup, vesting_schedule)
}
-1
View File
@@ -60,7 +60,6 @@ use frame_support::traits::{
use frame_support::weights::SimpleDispatchInfo;
use frame_system::{self as system, ensure_signed};
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
type BalanceOf<T> = <<T as Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::Balance;