Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Better testing for lock types in staking/democracy. (#3565)
Browse files Browse the repository at this point in the history
* Better testing for lock types in staking/democracy.

* Update docs.

* Update srml/executive/src/lib.rs

Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>

* Update srml/executive/src/lib.rs

Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>

* Fix test
  • Loading branch information
kianenigma authored Sep 6, 2019
1 parent 7fc21ce commit 43c4ef5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 29 deletions.
61 changes: 33 additions & 28 deletions srml/executive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,21 +293,24 @@ where
#[cfg(test)]
mod tests {
use super::*;
use balances::Call;
use runtime_io::with_externalities;
use primitives::{H256, Blake2Hasher};
use sr_primitives::{
generic::Era, Perbill, DispatchError, weights::Weight, testing::{Digest, Header, Block},
traits::{Header as HeaderT, BlakeTwo256, IdentityLookup, ConvertInto},
traits::{Bounded, Header as HeaderT, BlakeTwo256, IdentityLookup, ConvertInto},
transaction_validity::{InvalidTransaction, UnknownTransaction}, ApplyError,
};
use support::{
impl_outer_event, impl_outer_origin, parameter_types,
impl_outer_event, impl_outer_origin, parameter_types, impl_outer_dispatch,
traits::{Currency, LockIdentifier, LockableCurrency, WithdrawReasons, WithdrawReason},
};
use system;
use system::Call as SystemCall;
use balances::Call as BalancesCall;
use hex_literal::hex;

type System = system::Module<Runtime>;
type Balances = balances::Module<Runtime>;

impl_outer_origin! {
pub enum Origin for Runtime { }
}
Expand All @@ -317,6 +320,12 @@ mod tests {
balances<T>,
}
}
impl_outer_dispatch! {
pub enum Call for Runtime where origin: Origin {
system::System,
balances::Balances,
}
}

// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
#[derive(Clone, Eq, PartialEq)]
Expand All @@ -330,7 +339,7 @@ mod tests {
impl system::Trait for Runtime {
type Origin = Origin;
type Index = u64;
type Call = Call<Runtime>;
type Call = Call;
type BlockNumber = u64;
type Hash = primitives::H256;
type Hashing = BlakeTwo256;
Expand Down Expand Up @@ -369,11 +378,11 @@ mod tests {
}

impl ValidateUnsigned for Runtime {
type Call = Call<Runtime>;
type Call = Call;

fn validate_unsigned(call: &Self::Call) -> TransactionValidity {
match call {
Call::set_balance(_, _, _) => Ok(Default::default()),
Call::Balances(BalancesCall::set_balance(_, _, _)) => Ok(Default::default()),
_ => UnknownTransaction::NoUnsignedValidator.into(),
}
}
Expand All @@ -385,7 +394,7 @@ mod tests {
system::CheckWeight<Runtime>,
balances::TakeFees<Runtime>
);
type TestXt = sr_primitives::testing::TestXt<Call<Runtime>, SignedExtra>;
type TestXt = sr_primitives::testing::TestXt<Call, SignedExtra>;
type Executive = super::Executive<Runtime, Block<TestXt>, system::ChainContext<Runtime>, Runtime, ()>;

fn extra(nonce: u64, fee: u64) -> SignedExtra {
Expand All @@ -408,7 +417,7 @@ mod tests {
balances: vec![(1, 211)],
vesting: vec![],
}.assimilate_storage(&mut t).unwrap();
let xt = sr_primitives::testing::TestXt(sign_extra(1, 0, 0), Call::transfer(2, 69));
let xt = sr_primitives::testing::TestXt(sign_extra(1, 0, 0), Call::Balances(BalancesCall::transfer(2, 69)));
let weight = xt.get_dispatch_info().weight as u64;
let mut t = runtime_io::TestExternalities::<Blake2Hasher>::new(t);
with_externalities(&mut t, || {
Expand Down Expand Up @@ -489,7 +498,7 @@ mod tests {
fn bad_extrinsic_not_inserted() {
let mut t = new_test_ext(1);
// bad nonce check!
let xt = sr_primitives::testing::TestXt(sign_extra(1, 30, 0), Call::transfer(33, 69));
let xt = sr_primitives::testing::TestXt(sign_extra(1, 30, 0), Call::Balances(BalancesCall::transfer(33, 69)));
with_externalities(&mut t, || {
Executive::initialize_block(&Header::new(
1,
Expand All @@ -507,7 +516,7 @@ mod tests {
fn block_weight_limit_enforced() {
let mut t = new_test_ext(10000);
// given: TestXt uses the encoded len as fixed Len:
let xt = sr_primitives::testing::TestXt(sign_extra(1, 0, 0), Call::transfer::<Runtime>(33, 0));
let xt = sr_primitives::testing::TestXt(sign_extra(1, 0, 0), Call::Balances(BalancesCall::transfer(33, 0)));
let encoded = xt.encode();
let encoded_len = encoded.len() as Weight;
let limit = AvailableBlockRatio::get() * MaximumBlockWeight::get();
Expand All @@ -524,7 +533,7 @@ mod tests {

for nonce in 0..=num_to_exhaust_block {
let xt = sr_primitives::testing::TestXt(
sign_extra(1, nonce.into(), 0), Call::transfer::<Runtime>(33, 0),
sign_extra(1, nonce.into(), 0), Call::Balances(BalancesCall::transfer(33, 0)),
);
let res = Executive::apply_extrinsic(xt);
if nonce != num_to_exhaust_block {
Expand All @@ -543,9 +552,9 @@ mod tests {

#[test]
fn block_weight_and_size_is_stored_per_tx() {
let xt = sr_primitives::testing::TestXt(sign_extra(1, 0, 0), Call::transfer(33, 0));
let x1 = sr_primitives::testing::TestXt(sign_extra(1, 1, 0), Call::transfer(33, 0));
let x2 = sr_primitives::testing::TestXt(sign_extra(1, 2, 0), Call::transfer(33, 0));
let xt = sr_primitives::testing::TestXt(sign_extra(1, 0, 0), Call::Balances(BalancesCall::transfer(33, 0)));
let x1 = sr_primitives::testing::TestXt(sign_extra(1, 1, 0), Call::Balances(BalancesCall::transfer(33, 0)));
let x2 = sr_primitives::testing::TestXt(sign_extra(1, 2, 0), Call::Balances(BalancesCall::transfer(33, 0)));
let len = xt.clone().encode().len() as u32;
let mut t = new_test_ext(1);
with_externalities(&mut t, || {
Expand All @@ -569,7 +578,7 @@ mod tests {

#[test]
fn validate_unsigned() {
let xt = sr_primitives::testing::TestXt(None, Call::set_balance(33, 69, 69));
let xt = sr_primitives::testing::TestXt(None, Call::Balances(BalancesCall::set_balance(33, 69, 69)));
let mut t = new_test_ext(1);

with_externalities(&mut t, || {
Expand All @@ -578,7 +587,7 @@ mod tests {
Executive::apply_extrinsic(xt),
Ok(
Err(
DispatchError { module: None, error: 0, message: Some("RequireRootOrigin") }
DispatchError { module: Some(1), error: 0, message: Some("RequireRootOrigin") }
)
)
);
Expand All @@ -595,10 +604,13 @@ mod tests {
id,
&1,
110,
10,
Bounded::max_value(),
lock,
);
let xt = sr_primitives::testing::TestXt(sign_extra(1, 0, 0), Call::transfer(2, 10));
let xt = sr_primitives::testing::TestXt(
sign_extra(1, 0, 0),
Call::System(SystemCall::remark(vec![1u8])),
);
let weight = xt.get_dispatch_info().weight as u64;
Executive::initialize_block(&Header::new(
1,
Expand All @@ -609,15 +621,8 @@ mod tests {
));

if lock == WithdrawReasons::except(WithdrawReason::TransactionPayment) {
assert_eq!(
Executive::apply_extrinsic(xt).unwrap(),
Err(DispatchError {
module: None,
error: 0,
message: Some("account liquidity restrictions prevent withdrawal"),
}),
);
// but tx fee has been deducted. the transaction failed on transfer, not on fee.
assert!(Executive::apply_extrinsic(xt).unwrap().is_ok());
// tx fee has been deducted.
assert_eq!(<balances::Module<Runtime>>::total_balance(&1), 111 - 10 - weight);
} else {
assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion srml/staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1562,7 +1562,7 @@ fn bond_with_no_staked_value() {
.nominate(false)
.minimum_validator_count(1)
.build(), || {
// Can't bond with 1
// Can't bond with 1
assert_noop!(
Staking::bond(Origin::signed(1), 2, 1, RewardDestination::Controller),
"can not bond with value less than minimum balance"
Expand Down
10 changes: 10 additions & 0 deletions srml/support/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,16 @@ pub trait Time {

impl WithdrawReasons {
/// Choose all variants except for `one`.
///
/// ```rust
/// # use srml_support::traits::{WithdrawReason, WithdrawReasons};
/// # fn main() {
/// assert_eq!(
/// WithdrawReason::Fee | WithdrawReason::Transfer | WithdrawReason::Reserve,
/// WithdrawReasons::except(WithdrawReason::TransactionPayment),
/// );
/// # }
/// ```
pub fn except(one: WithdrawReason) -> WithdrawReasons {
let mut mask = Self::all();
mask.toggle(one);
Expand Down

0 comments on commit 43c4ef5

Please sign in to comment.