diff --git a/srml/executive/src/lib.rs b/srml/executive/src/lib.rs index c6931af0a1b0b..5afd28ab2b5d2 100644 --- a/srml/executive/src/lib.rs +++ b/srml/executive/src/lib.rs @@ -354,19 +354,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; - use sr_primitives::Perbill; - use sr_primitives::weights::Weight; - use sr_primitives::traits::{Header as HeaderT, BlakeTwo256, IdentityLookup, ConvertInto}; - use sr_primitives::testing::{Digest, Header, Block}; - use support::{impl_outer_event, impl_outer_origin, parameter_types}; - use support::traits::{Currency, LockIdentifier, LockableCurrency, WithdrawReasons, WithdrawReason}; - use system; + use sr_primitives::{ + generic::Era, Perbill, weights::Weight, testing::{Digest, Header, Block}, + traits::{Bounded, Header as HeaderT, BlakeTwo256, IdentityLookup, ConvertInto}, + ApplyError, + }; + use support::{ + impl_outer_event, impl_outer_origin, parameter_types, impl_outer_dispatch, + traits::{Currency, LockIdentifier, LockableCurrency, WithdrawReasons, WithdrawReason}, + }; + use system::Call as SystemCall; + use balances::Call as BalancesCall; use hex_literal::hex; + type System = system::Module; + type Balances = balances::Module; + impl_outer_origin! { pub enum Origin for Runtime { } } @@ -376,6 +381,12 @@ mod tests { balances, } } + 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)] @@ -389,7 +400,7 @@ mod tests { impl system::Trait for Runtime { type Origin = Origin; type Index = u64; - type Call = Call; + type Call = Call; type BlockNumber = u64; type Hash = primitives::H256; type Hashing = BlakeTwo256; @@ -428,11 +439,11 @@ mod tests { } impl ValidateUnsigned for Runtime { - type Call = Call; + type Call = Call; fn validate_unsigned(call: &Self::Call) -> TransactionValidity { match call { - Call::set_balance(_, _, _) => TransactionValidity::Valid(Default::default()), + Call::Balances(BalancesCall::set_balance(_, _, _)) => TransactionValidity::Valid(Default::default()), _ => TransactionValidity::Invalid(0), } } @@ -444,7 +455,7 @@ mod tests { system::CheckWeight, balances::TakeFees ); - type TestXt = sr_primitives::testing::TestXt, SignedExtra>; + type TestXt = sr_primitives::testing::TestXt; type Executive = super::Executive, system::ChainContext, Runtime, ()>; fn extra(nonce: u64, fee: u64) -> SignedExtra { @@ -467,7 +478,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::::new(t); with_externalities(&mut t, || { @@ -548,7 +559,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, @@ -566,7 +577,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::(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(); @@ -582,7 +593,9 @@ mod tests { assert_eq!(>::all_extrinsics_weight(), 0); for nonce in 0..=num_to_exhaust_block { - let xt = sr_primitives::testing::TestXt(sign_extra(1, nonce.into(), 0), Call::transfer::(33, 0)); + let xt = sr_primitives::testing::TestXt( + sign_extra(1, nonce.into(), 0), Call::Balances(BalancesCall::transfer(33, 0)), + ); let res = Executive::apply_extrinsic(xt); if nonce != num_to_exhaust_block { assert_eq!(res.unwrap(), ApplyOutcome::Success); @@ -597,9 +610,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, || { @@ -623,12 +636,11 @@ mod tests { #[test] fn validate_unsigned() { - let xt = sr_primitives::testing::TestXt(None, Call::set_balance(33, 69, 69)); - let valid = TransactionValidity::Valid(Default::default()); + 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, || { - assert_eq!(Executive::validate_transaction(xt.clone()), valid); + assert_eq!(Executive::validate_transaction(xt.clone()), TransactionValidity::Valid(Default::default())); assert_eq!(Executive::apply_extrinsic(xt), Ok(ApplyOutcome::Fail)); }); } @@ -643,10 +655,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, @@ -657,8 +672,8 @@ mod tests { )); if lock == WithdrawReasons::except(WithdrawReason::TransactionPayment) { - assert_eq!(Executive::apply_extrinsic(xt).unwrap(), ApplyOutcome::Fail); - // but tx fee has been deducted. the transaction failed on transfer, not on fee. + assert_eq!(Executive::apply_extrinsic(xt).unwrap(), ApplyOutcome::Success); + // tx fee has been deducted. assert_eq!(>::total_balance(&1), 111 - 10 - weight); } else { assert_eq!(Executive::apply_extrinsic(xt), Err(ApplyError::CantPay)); diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index c3f39222994d6..684207322b1cc 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -1556,7 +1556,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" diff --git a/srml/support/src/traits.rs b/srml/support/src/traits.rs index 355f05992fce1..4ce94bb341ac4 100644 --- a/srml/support/src/traits.rs +++ b/srml/support/src/traits.rs @@ -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);