lib.rs 64.5 KiB
Newer Older
		ExecutionPhase::put(Phase::Finalization);
	}

	/// To be called immediately after finishing the initialization of the block
	/// (e.g., called `on_initialize` for all modules).
	pub fn note_finished_initialize() {
		ExecutionPhase::put(Phase::ApplyExtrinsic(0))
	/// Remove all extrinsic data and save the extrinsics trie root.
	pub fn derive_extrinsics() {
		let extrinsics = (0..ExtrinsicCount::get().unwrap_or_default())
			.map(ExtrinsicData::take).collect();
		let xts_root = extrinsics_data_root::<T::Hashing>(extrinsics);
		<ExtrinsicsRoot<T>>::put(xts_root);
	}
Gavin Wood's avatar
Gavin Wood committed

	/// An account is being created.
	pub fn on_created_account(who: T::AccountId) {
		T::OnNewAccount::on_new_account(&who);
		Self::deposit_event(RawEvent::NewAccount(who));
	}

	/// Do anything that needs to be done after an account has been killed.
	fn on_killed_account(who: T::AccountId) {
Gavin Wood's avatar
Gavin Wood committed
		T::OnKilledAccount::on_killed_account(&who);
		Self::deposit_event(RawEvent::KilledAccount(who));
	}

	/// Remove an account from storage. This should only be done when its refs are zero or you'll
	/// get storage leaks in other modules. Nonetheless we assume that the calling logic knows best.
	///
	/// This is a no-op if the account doesn't already exist. If it does then it will ensure
	/// cleanups (those in `on_killed_account`) take place.
	fn kill_account(who: &T::AccountId) {
		if Account::<T>::contains_key(who) {
			let account = Account::<T>::take(who);
			if account.refcount > 0 {
				debug::debug!(
					target: "system",
					"WARNING: Referenced account deleted. This is probably a bug."
				);
			}
			Module::<T>::on_killed_account(who.clone());
		}

	/// Determine whether or not it is possible to update the code.
	///
	/// This function has no side effects and is idempotent, but is fairly
	/// heavy. It is automatically called by `set_code`; in most cases,
	/// a direct call to `set_code` is preferable. It is useful to call
	/// `can_set_code` when it is desirable to perform the appropriate
	/// runtime checks without actually changing the code yet.
	pub fn can_set_code(origin: T::Origin, code: &[u8]) -> Result<(), sp_runtime::DispatchError> {
		ensure_root(origin)?;

		let current_version = T::Version::get();
		let new_version = sp_io::misc::runtime_version(&code)
			.and_then(|v| RuntimeVersion::decode(&mut &v[..]).ok())
			.ok_or_else(|| Error::<T>::FailedToExtractRuntimeVersion)?;

		if new_version.spec_name != current_version.spec_name {
			Err(Error::<T>::InvalidSpecName)?
		}

		if new_version.spec_version <= current_version.spec_version {
			Err(Error::<T>::SpecVersionNeedsToIncrease)?
		}

		Ok(())
	}
Gavin Wood's avatar
Gavin Wood committed
}

/// Event handler which calls on_created_account when it happens.
pub struct CallOnCreatedAccount<T>(PhantomData<T>);
impl<T: Trait> Happened<T::AccountId> for CallOnCreatedAccount<T> {
	fn happened(who: &T::AccountId) {
		Module::<T>::on_created_account(who.clone());
	}
}

/// Event handler which calls kill_account when it happens.
pub struct CallKillAccount<T>(PhantomData<T>);
impl<T: Trait> Happened<T::AccountId> for CallKillAccount<T> {
	fn happened(who: &T::AccountId) {
Gavin Wood's avatar
Gavin Wood committed
		Module::<T>::kill_account(who)
Gavin Wood's avatar
Gavin Wood committed
	}
}

// Implement StoredMap for a simple single-item, kill-account-on-remove system. This works fine for
// storing a single item which is required to not be empty/default for the account to exist.
// Anything more complex will need more sophisticated logic.
impl<T: Trait> StoredMap<T::AccountId, T::AccountData> for Module<T> {
	fn get(k: &T::AccountId) -> T::AccountData {
Gavin Wood's avatar
Gavin Wood committed
		Account::<T>::get(k).data
Gavin Wood's avatar
Gavin Wood committed
	}
	fn is_explicit(k: &T::AccountId) -> bool {
		Account::<T>::contains_key(k)
	}
Gavin Wood's avatar
Gavin Wood committed
	fn insert(k: &T::AccountId, data: T::AccountData) {
Gavin Wood's avatar
Gavin Wood committed
		let existed = Account::<T>::contains_key(k);
Gavin Wood's avatar
Gavin Wood committed
		Account::<T>::mutate(k, |a| a.data = data);
Gavin Wood's avatar
Gavin Wood committed
		if !existed {
			Self::on_created_account(k.clone());
		}
	}
	fn remove(k: &T::AccountId) {
Gavin Wood's avatar
Gavin Wood committed
		Self::kill_account(k)
Gavin Wood's avatar
Gavin Wood committed
	}
	fn mutate<R>(k: &T::AccountId, f: impl FnOnce(&mut T::AccountData) -> R) -> R {
		let existed = Account::<T>::contains_key(k);
Gavin Wood's avatar
Gavin Wood committed
		let r = Account::<T>::mutate(k, |a| f(&mut a.data));
Gavin Wood's avatar
Gavin Wood committed
		if !existed {
			Self::on_created_account(k.clone());
		}
		r
	}
	fn mutate_exists<R>(k: &T::AccountId, f: impl FnOnce(&mut Option<T::AccountData>) -> R) -> R {
Gavin Wood's avatar
Gavin Wood committed
		Self::try_mutate_exists(k, |x| -> Result<R, Infallible> { Ok(f(x)) }).expect("Infallible; qed")
Gavin Wood's avatar
Gavin Wood committed
	}
	fn try_mutate_exists<R, E>(k: &T::AccountId, f: impl FnOnce(&mut Option<T::AccountData>) -> Result<R, E>) -> Result<R, E> {
		Account::<T>::try_mutate_exists(k, |maybe_value| {
			let existed = maybe_value.is_some();
Gavin Wood's avatar
Gavin Wood committed
			let (maybe_prefix, mut maybe_data) = split_inner(
				maybe_value.take(),
				|account| ((account.nonce, account.refcount), account.data)
			);
			f(&mut maybe_data).map(|result| {
				*maybe_value = maybe_data.map(|data| {
					let (nonce, refcount) = maybe_prefix.unwrap_or_default();
					AccountInfo { nonce, refcount, data }
				});
				(existed, maybe_value.is_some(), result)
Gavin Wood's avatar
Gavin Wood committed
			})
		}).map(|(existed, exists, v)| {
			if !existed && exists {
				Self::on_created_account(k.clone());
			} else if existed && !exists {
				Self::on_killed_account(k.clone());
			}
			v
		})
	}
Gavin Wood's avatar
Gavin Wood committed
/// Split an `option` into two constituent options, as defined by a `splitter` function.
pub fn split_inner<T, R, S>(option: Option<T>, splitter: impl FnOnce(T) -> (R, S))
	-> (Option<R>, Option<S>)
{
	match option {
		Some(inner) => {
			let (r, s) = splitter(inner);
			(Some(r), Some(s))
		}
		None => (None, None),
/// resource limit check.
#[derive(Encode, Decode, Clone, Eq, PartialEq)]
pub struct CheckWeight<T: Trait + Send + Sync>(PhantomData<T>);

impl<T: Trait + Send + Sync> CheckWeight<T> {
Kian Peymani's avatar
Kian Peymani committed
	/// Get the quota ratio of each dispatch class type. This indicates that all operational
	/// dispatches can use the full capacity of any resource, while user-triggered ones can consume
Kian Peymani's avatar
Kian Peymani committed
	/// a portion.
	fn get_dispatch_limit_ratio(class: DispatchClass) -> Perbill {
		match class {
Kian Paimani's avatar
Kian Paimani committed
			DispatchClass::Operational => <Perbill as sp_runtime::PerThing>::one(),
Kian Peymani's avatar
Kian Peymani committed
			DispatchClass::Normal => T::AvailableBlockRatio::get(),
	/// Checks if the current extrinsic can fit into the block with respect to block weight limits.
	///
	/// Upon successes, it returns the new block weight as a `Result`.
	fn check_weight(
		info: <Self as SignedExtension>::DispatchInfo,
	) -> Result<Weight, TransactionValidityError> {
		let current_weight = Module::<T>::all_extrinsics_weight();
		let maximum_weight = T::MaximumBlockWeight::get();
Kian Peymani's avatar
Kian Peymani committed
		let limit = Self::get_dispatch_limit_ratio(info.class) * maximum_weight;
		let added_weight = info.weight.min(limit);
		let next_weight = current_weight.saturating_add(added_weight);
		if next_weight > limit {
			Err(InvalidTransaction::ExhaustsResources.into())
		} else {
			Ok(next_weight)
		}
	}

	/// Checks if the current extrinsic can fit into the block with respect to block length limits.
	///
	/// Upon successes, it returns the new block length as a `Result`.
	fn check_block_length(
		info: <Self as SignedExtension>::DispatchInfo,
		len: usize,
	) -> Result<u32, TransactionValidityError> {
		let current_len = Module::<T>::all_extrinsics_len();
		let maximum_len = T::MaximumBlockLength::get();
Kian Peymani's avatar
Kian Peymani committed
		let limit = Self::get_dispatch_limit_ratio(info.class) * maximum_len;
		let added_len = len as u32;
		let next_len = current_len.saturating_add(added_len);
		if next_len > limit {
			Err(InvalidTransaction::ExhaustsResources.into())
		} else {
			Ok(next_len)
		}
	}

	/// get the priority of an extrinsic denoted by `info`.
	fn get_priority(info: <Self as SignedExtension>::DispatchInfo) -> TransactionPriority {
		match info.class {
			DispatchClass::Normal => info.weight.into(),
			DispatchClass::Operational => Bounded::max_value()
		}
	}

	/// Creates new `SignedExtension` to check weight of the extrinsic.
		Self(PhantomData)
	}

	/// Do the pre-dispatch checks. This can be applied to both signed and unsigned.
	///
	/// It checks and notes the new weight and length.
	fn do_pre_dispatch(
		info: <Self as SignedExtension>::DispatchInfo,
		len: usize,
	) -> Result<(), TransactionValidityError> {
		let next_len = Self::check_block_length(info, len)?;
		let next_weight = Self::check_weight(info)?;
		AllExtrinsicsLen::put(next_len);
		AllExtrinsicsWeight::put(next_weight);
		Ok(())
	}

	/// Do the validate checks. This can be applied to both signed and unsigned.
	///
	/// It only checks that the block weight and length limit will not exceed.
	fn do_validate(
		info: <Self as SignedExtension>::DispatchInfo,
		len: usize,
	) -> TransactionValidity {
		// ignore the next weight and length. If they return `Ok`, then it is below the limit.
		let _ = Self::check_block_length(info, len)?;
		let _ = Self::check_weight(info)?;

		Ok(ValidTransaction { priority: Self::get_priority(info), ..Default::default() })
	}
}

impl<T: Trait + Send + Sync> SignedExtension for CheckWeight<T> {
	type AccountId = T::AccountId;
	type Call = T::Call;
	type AdditionalSigned = ();
	type DispatchInfo = DispatchInfo;
Xiliang Chen's avatar
Xiliang Chen committed
	type Pre = ();
	const IDENTIFIER: &'static str = "CheckWeight";
	fn additional_signed(&self) -> sp_std::result::Result<(), TransactionValidityError> { Ok(()) }

	fn pre_dispatch(
		self,
		_who: &Self::AccountId,
		_call: &Self::Call,
	) -> Result<(), TransactionValidityError> {
		Self::do_pre_dispatch(info, len)
	}

	fn validate(
		&self,
		_who: &Self::AccountId,
		_call: &Self::Call,
	) -> TransactionValidity {
		Self::do_validate(info, len)
	}
	fn pre_dispatch_unsigned(
		_call: &Self::Call,
		info: Self::DispatchInfo,
		len: usize,
	) -> Result<(), TransactionValidityError> {
		Self::do_pre_dispatch(info, len)
	}
	fn validate_unsigned(
		_call: &Self::Call,
		info: Self::DispatchInfo,
		len: usize,
	) -> TransactionValidity {
		Self::do_validate(info, len)
impl<T: Trait + Send + Sync> Debug for CheckWeight<T> {
	#[cfg(feature = "std")]
	fn fmt(&self, f: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result {
		write!(f, "CheckWeight")
	}

	#[cfg(not(feature = "std"))]
	fn fmt(&self, _: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result {
		Ok(())
	}
}

/// Nonce check and increment to give replay protection for transactions.
#[derive(Encode, Decode, Clone, Eq, PartialEq)]
pub struct CheckNonce<T: Trait>(#[codec(compact)] T::Index);

impl<T: Trait> CheckNonce<T> {
	/// utility constructor. Used only in client/factory code.
	pub fn from(nonce: T::Index) -> Self {
		Self(nonce)
	}
}

impl<T: Trait> Debug for CheckNonce<T> {
	#[cfg(feature = "std")]
	fn fmt(&self, f: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result {
		write!(f, "CheckNonce({})", self.0)

	#[cfg(not(feature = "std"))]
	fn fmt(&self, _: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result {
}

impl<T: Trait> SignedExtension for CheckNonce<T> {
	type AccountId = T::AccountId;
	type Call = T::Call;
	type AdditionalSigned = ();
	type DispatchInfo = DispatchInfo;
Xiliang Chen's avatar
Xiliang Chen committed
	type Pre = ();
	const IDENTIFIER: &'static str = "CheckNonce";
	fn additional_signed(&self) -> sp_std::result::Result<(), TransactionValidityError> { Ok(()) }

	fn pre_dispatch(
		self,
		who: &Self::AccountId,
		_call: &Self::Call,
		_info: Self::DispatchInfo,
	) -> Result<(), TransactionValidityError> {
Gavin Wood's avatar
Gavin Wood committed
		let mut account = Account::<T>::get(who);
		if self.0 != account.nonce {
Gavin Wood's avatar
Gavin Wood committed
				if self.0 < account.nonce {
					InvalidTransaction::Stale
				} else {
					InvalidTransaction::Future
				}.into()
Gavin Wood's avatar
Gavin Wood committed
		account.nonce += T::Index::one();
		Account::<T>::insert(who, account);
		Ok(())
	}

	fn validate(
		&self,
		who: &Self::AccountId,
		_call: &Self::Call,
	) -> TransactionValidity {
		// check index
Gavin Wood's avatar
Gavin Wood committed
		let account = Account::<T>::get(who);
		if self.0 < account.nonce {
			return InvalidTransaction::Stale.into()
		}

		let provides = vec![Encode::encode(&(who, self.0))];
Gavin Wood's avatar
Gavin Wood committed
		let requires = if account.nonce < self.0 {
			vec![Encode::encode(&(who, self.0 - One::one()))]
		} else {
			vec![]
		};

		Ok(ValidTransaction {
			priority: info.weight as TransactionPriority,
			requires,
			provides,
			longevity: TransactionLongevity::max_value(),
			propagate: true,
		})
	}
}

Gavin Wood's avatar
Gavin Wood committed
impl<T: Trait> IsDeadAccount<T::AccountId> for Module<T> {
	fn is_dead_account(who: &T::AccountId) -> bool {
		!Account::<T>::contains_key(who)
	}
}

/// Check for transaction mortality.
#[derive(Encode, Decode, Clone, Eq, PartialEq)]
pub struct CheckEra<T: Trait + Send + Sync>(Era, sp_std::marker::PhantomData<T>);

impl<T: Trait + Send + Sync> CheckEra<T> {
	/// utility constructor. Used only in client/factory code.
	pub fn from(era: Era) -> Self {
		Self(era, sp_std::marker::PhantomData)
impl<T: Trait + Send + Sync> Debug for CheckEra<T> {
	#[cfg(feature = "std")]
	fn fmt(&self, f: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result {
		write!(f, "CheckEra({:?})", self.0)

	#[cfg(not(feature = "std"))]
	fn fmt(&self, _: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result {
}

impl<T: Trait + Send + Sync> SignedExtension for CheckEra<T> {
	type AccountId = T::AccountId;
	type Call = T::Call;
	type AdditionalSigned = T::Hash;
	type DispatchInfo = DispatchInfo;
Xiliang Chen's avatar
Xiliang Chen committed
	type Pre = ();
	const IDENTIFIER: &'static str = "CheckEra";
	fn validate(
		&self,
		_who: &Self::AccountId,
		_call: &Self::Call,
		_info: Self::DispatchInfo,
	) -> TransactionValidity {
		let current_u64 = <Module<T>>::block_number().saturated_into::<u64>();
		let valid_till = self.0.death(current_u64);
		Ok(ValidTransaction {
			longevity: valid_till.saturating_sub(current_u64),
			..Default::default()
		})
	}

	fn additional_signed(&self) -> Result<Self::AdditionalSigned, TransactionValidityError> {
		let current_u64 = <Module<T>>::block_number().saturated_into::<u64>();
		let n = self.0.birth(current_u64).saturated_into::<T::BlockNumber>();
		if !<BlockHash<T>>::contains_key(n) {
			Err(InvalidTransaction::AncientBirthBlock.into())
		} else {
			Ok(<Module<T>>::block_hash(n))
		}
/// Nonce check and increment to give replay protection for transactions.
#[derive(Encode, Decode, Clone, Eq, PartialEq)]
pub struct CheckGenesis<T: Trait + Send + Sync>(sp_std::marker::PhantomData<T>);
impl<T: Trait + Send + Sync> Debug for CheckGenesis<T> {
	#[cfg(feature = "std")]
	fn fmt(&self, f: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result {
		write!(f, "CheckGenesis")
	}

	#[cfg(not(feature = "std"))]
	fn fmt(&self, _: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result {
		Ok(())
	}
}

impl<T: Trait + Send + Sync> CheckGenesis<T> {
	/// Creates new `SignedExtension` to check genesis hash.
		Self(sp_std::marker::PhantomData)
	}
}

impl<T: Trait + Send + Sync> SignedExtension for CheckGenesis<T> {
	type AccountId = T::AccountId;
	type Call = <T as Trait>::Call;
	type AdditionalSigned = T::Hash;
	type DispatchInfo = DispatchInfo;
Xiliang Chen's avatar
Xiliang Chen committed
	type Pre = ();
	const IDENTIFIER: &'static str = "CheckGenesis";
	fn additional_signed(&self) -> Result<Self::AdditionalSigned, TransactionValidityError> {
		Ok(<Module<T>>::block_hash(T::BlockNumber::zero()))
	}
}

/// Ensure the runtime version registered in the transaction is the same as at present.
#[derive(Encode, Decode, Clone, Eq, PartialEq)]
pub struct CheckVersion<T: Trait + Send + Sync>(sp_std::marker::PhantomData<T>);
impl<T: Trait + Send + Sync> Debug for CheckVersion<T> {
	#[cfg(feature = "std")]
	fn fmt(&self, f: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result {
		write!(f, "CheckVersion")
	}

	#[cfg(not(feature = "std"))]
	fn fmt(&self, _: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result {
		Ok(())
	}
}

impl<T: Trait + Send + Sync> CheckVersion<T> {
	/// Create new `SignedExtension` to check runtime version.
		Self(sp_std::marker::PhantomData)
	}
}

impl<T: Trait + Send + Sync> SignedExtension for CheckVersion<T> {
	type AccountId = T::AccountId;
	type Call = <T as Trait>::Call;
	type AdditionalSigned = u32;
	type DispatchInfo = DispatchInfo;
	const IDENTIFIER: &'static str = "CheckVersion";
	fn additional_signed(&self) -> Result<Self::AdditionalSigned, TransactionValidityError> {
		Ok(<Module<T>>::runtime_version().spec_version)
	}
}

pub struct ChainContext<T>(sp_std::marker::PhantomData<T>);
impl<T> Default for ChainContext<T> {
	fn default() -> Self {
		ChainContext(sp_std::marker::PhantomData)
	}
}

impl<T: Trait> Lookup for ChainContext<T> {
	type Source = <T::Lookup as StaticLookup>::Source;
	type Target = <T::Lookup as StaticLookup>::Target;

	fn lookup(&self, s: Self::Source) -> Result<Self::Target, LookupError> {
		<T::Lookup as StaticLookup>::lookup(s)
#[cfg(test)]
mod tests {
	use super::*;
Gavin Wood's avatar
Gavin Wood committed
	use sp_std::cell::RefCell;
	use sp_runtime::{traits::{BlakeTwo256, IdentityLookup}, testing::Header, DispatchError};
	use frame_support::{impl_outer_origin, parameter_types};
	impl_outer_origin! {
		pub enum Origin for Test where system = super {}
	}

	#[derive(Clone, Eq, PartialEq)]
	pub struct Test;

	parameter_types! {
		pub const BlockHashCount: u64 = 10;
		pub const MaximumBlockWeight: Weight = 1024;
Kian Peymani's avatar
Kian Peymani committed
		pub const AvailableBlockRatio: Perbill = Perbill::from_percent(75);
		pub const MaximumBlockLength: u32 = 1024;
		pub const Version: RuntimeVersion = RuntimeVersion {
			spec_name: sp_version::create_runtime_str!("test"),
			impl_name: sp_version::create_runtime_str!("system-test"),
			authoring_version: 1,
			spec_version: 1,
			impl_version: 1,
			apis: sp_version::create_apis_vec!([]),
		};
Gavin Wood's avatar
Gavin Wood committed
	thread_local!{
		pub static KILLED: RefCell<Vec<u64>> = RefCell::new(vec![]);
	}

	pub struct RecordKilled;
	impl OnKilledAccount<u64> for RecordKilled {
		fn on_killed_account(who: &u64) { KILLED.with(|r| r.borrow_mut().push(*who)) }
	}

	impl Trait for Test {
		type Origin = Origin;
		type Call = ();
		type Index = u64;
		type BlockNumber = u64;
		type Hash = H256;
		type Hashing = BlakeTwo256;
		type AccountId = u64;
		type Lookup = IdentityLookup<Self::AccountId>;
		type Header = Header;
		type Event = u16;
		type BlockHashCount = BlockHashCount;
		type MaximumBlockWeight = MaximumBlockWeight;
Kian Peymani's avatar
Kian Peymani committed
		type AvailableBlockRatio = AvailableBlockRatio;
		type MaximumBlockLength = MaximumBlockLength;
		type ModuleToIndex = ();
Gavin Wood's avatar
Gavin Wood committed
		type AccountData = u32;
		type OnNewAccount = ();
Gavin Wood's avatar
Gavin Wood committed
		type OnKilledAccount = RecordKilled;
Gavin Wood's avatar
Gavin Wood committed
	impl From<Event<Test>> for u16 {
		fn from(e: Event<Test>) -> u16 {
Gavin Wood's avatar
Gavin Wood committed
				Event::<Test>::ExtrinsicSuccess(..) => 100,
				Event::<Test>::ExtrinsicFailed(..) => 101,
				Event::<Test>::CodeUpdated => 102,
				_ => 103,
	type System = Module<Test>;

	const CALL: &<Test as Trait>::Call = &();

	fn new_test_ext() -> sp_io::TestExternalities {
		GenesisConfig::default().build_storage::<Test>().unwrap().into()
Kian Peymani's avatar
Kian Peymani committed
	fn normal_weight_limit() -> Weight {
		<Test as Trait>::AvailableBlockRatio::get() * <Test as Trait>::MaximumBlockWeight::get()
	}

	fn normal_length_limit() -> u32 {
		<Test as Trait>::AvailableBlockRatio::get() * <Test as Trait>::MaximumBlockLength::get()
	}

	#[test]
	fn origin_works() {
		let o = Origin::from(RawOrigin::<u64>::Signed(1u64));
		let x: Result<RawOrigin<u64>, Origin> = o.into();
		assert_eq!(x, Ok(RawOrigin::<u64>::Signed(1u64)));
	}

Gavin Wood's avatar
Gavin Wood committed
	#[test]
	fn stored_map_works() {
		new_test_ext().execute_with(|| {
			System::insert(&0, 42);
			assert!(System::allow_death(&0));

			System::inc_ref(&0);
			assert!(!System::allow_death(&0));

			System::insert(&0, 69);
			assert!(!System::allow_death(&0));

			System::dec_ref(&0);
			assert!(System::allow_death(&0));

			assert!(KILLED.with(|r| r.borrow().is_empty()));
			System::kill_account(&0);
			assert_eq!(KILLED.with(|r| r.borrow().clone()), vec![0u64]);
		});
	}

	#[test]
	fn deposit_event_should_work() {
		new_test_ext().execute_with(|| {
			System::initialize(
				&1,
				&[0u8; 32].into(),
				&[0u8; 32].into(),
				&Default::default(),
				InitKind::Full,
			);
			System::note_finished_extrinsics();
			System::deposit_event(1u16);
			System::finalize();
				vec![
					EventRecord {
						phase: Phase::Finalization,
						event: 1u16,
						topics: vec![],
					}
				]
			System::initialize(
				&2,
				&[0u8; 32].into(),
				&[0u8; 32].into(),
				&Default::default(),
				InitKind::Full,
			);
			System::deposit_event(32u16);
			System::note_finished_initialize();
			System::deposit_event(42u16);
			System::note_applied_extrinsic(&Ok(()), 0, Default::default());
			System::note_applied_extrinsic(&Err(DispatchError::BadOrigin), 0, Default::default());
			System::note_finished_extrinsics();
			System::deposit_event(3u16);
			System::finalize();
			assert_eq!(
				System::events(),
				vec![
					EventRecord { phase: Phase::Initialization, event: 32u16, topics: vec![] },
					EventRecord { phase: Phase::ApplyExtrinsic(0), event: 42u16, topics: vec![] },
					EventRecord { phase: Phase::ApplyExtrinsic(0), event: 100u16, topics: vec![] },
					EventRecord { phase: Phase::ApplyExtrinsic(1), event: 101u16, topics: vec![] },
					EventRecord { phase: Phase::Finalization, event: 3u16, topics: vec![] }
				]
			);

	#[test]
	fn deposit_event_topics() {
		new_test_ext().execute_with(|| {
			const BLOCK_NUMBER: u64 = 1;

			System::initialize(
				&BLOCK_NUMBER,
				&[0u8; 32].into(),
				&[0u8; 32].into(),
				&Default::default(),
			System::note_finished_extrinsics();

			let topics = vec![
				H256::repeat_byte(1),
				H256::repeat_byte(2),
				H256::repeat_byte(3),
			];

			// We deposit a few events with different sets of topics.
			System::deposit_event_indexed(&topics[0..3], 1u16);
			System::deposit_event_indexed(&topics[0..1], 2u16);
			System::deposit_event_indexed(&topics[1..2], 3u16);

			System::finalize();

			// Check that topics are reflected in the event record.
			assert_eq!(
				System::events(),
				vec![
					EventRecord {
						phase: Phase::Finalization,
						event: 1u16,
						topics: topics[0..3].to_vec(),
					},
					EventRecord {
						phase: Phase::Finalization,
						event: 2u16,
						topics: topics[0..1].to_vec(),
					},
					EventRecord {
						phase: Phase::Finalization,
						event: 3u16,
						topics: topics[1..2].to_vec(),
					}
				]
			);

			// Check that the topic-events mapping reflects the deposited topics.
			// Note that these are indexes of the events.
			assert_eq!(
				System::event_topics(&topics[0]),
				vec![(BLOCK_NUMBER, 0), (BLOCK_NUMBER, 1)],
			);
			assert_eq!(
				System::event_topics(&topics[1]),
				vec![(BLOCK_NUMBER, 0), (BLOCK_NUMBER, 2)],
			);
			assert_eq!(
				System::event_topics(&topics[2]),
				vec![(BLOCK_NUMBER, 0)],
			);
		});
	}

	#[test]
	fn prunes_block_hash_mappings() {
		new_test_ext().execute_with(|| {
			// simulate import of 15 blocks
			for n in 1..=15 {
				System::initialize(
					&n,
					&[n as u8 - 1; 32].into(),
					&[0u8; 32].into(),
					&Default::default(),
				);

				System::finalize();
			}

			// first 5 block hashes are pruned
			for n in 0..5 {
				assert_eq!(
					System::block_hash(n),
					H256::zero(),
				);
			}

			// the remaining 10 are kept
			for n in 5..15 {
				assert_eq!(
					System::block_hash(n),
					[n as u8; 32].into(),
				);
			}
		})
	}

	#[test]
	fn signed_ext_check_nonce_works() {
		new_test_ext().execute_with(|| {
Gavin Wood's avatar
Gavin Wood committed
			Account::<Test>::insert(1, AccountInfo { nonce: 1, refcount: 0, data: 0 });
			let info = DispatchInfo::default();
			let len = 0_usize;
			// stale
			assert!(CheckNonce::<Test>(0).validate(&1, CALL, info, len).is_err());
			assert!(CheckNonce::<Test>(0).pre_dispatch(&1, CALL, info, len).is_err());
			assert!(CheckNonce::<Test>(1).validate(&1, CALL, info, len).is_ok());
			assert!(CheckNonce::<Test>(1).pre_dispatch(&1, CALL, info, len).is_ok());
			assert!(CheckNonce::<Test>(5).validate(&1, CALL, info, len).is_ok());
			assert!(CheckNonce::<Test>(5).pre_dispatch(&1, CALL, info, len).is_err());
Kian Peymani's avatar
Kian Peymani committed
	fn signed_ext_check_weight_works_normal_tx() {
		new_test_ext().execute_with(|| {
Kian Peymani's avatar
Kian Peymani committed
			let normal_limit = normal_weight_limit();
			let small = DispatchInfo { weight: 100, ..Default::default() };
			let medium = DispatchInfo {
Kian Peymani's avatar
Kian Peymani committed
				weight: normal_limit - 1,
				..Default::default()
			};
			let big = DispatchInfo {
Kian Peymani's avatar
Kian Peymani committed
				weight: normal_limit + 1,
				..Default::default()
			};
			let len = 0_usize;

			let reset_check_weight = |i, f, s| {
				AllExtrinsicsWeight::put(s);
				let r = CheckWeight::<Test>(PhantomData).pre_dispatch(&1, CALL, i, len);
				if f { assert!(r.is_err()) } else { assert!(r.is_ok()) }
			};

			reset_check_weight(small, false, 0);
			reset_check_weight(medium, false, 0);
			reset_check_weight(big, true, 1);
		})
	}

	#[test]
	fn signed_ext_check_weight_fee_works() {
		new_test_ext().execute_with(|| {
			let free = DispatchInfo { weight: 0, ..Default::default() };
			let len = 0_usize;

			assert_eq!(System::all_extrinsics_weight(), 0);
			let r = CheckWeight::<Test>(PhantomData).pre_dispatch(&1, CALL, free, len);
			assert!(r.is_ok());
			assert_eq!(System::all_extrinsics_weight(), 0);
		})
	}

	#[test]
	fn signed_ext_check_weight_max_works() {
		new_test_ext().execute_with(|| {
			let max = DispatchInfo { weight: Weight::max_value(), ..Default::default() };
			let len = 0_usize;
Kian Peymani's avatar
Kian Peymani committed
			let normal_limit = normal_weight_limit();

			assert_eq!(System::all_extrinsics_weight(), 0);
			let r = CheckWeight::<Test>(PhantomData).pre_dispatch(&1, CALL, max, len);
			assert!(r.is_ok());
Kian Peymani's avatar
Kian Peymani committed
			assert_eq!(System::all_extrinsics_weight(), normal_limit);
		})
	}

	#[test]
	fn signed_ext_check_weight_works_operational_tx() {
		new_test_ext().execute_with(|| {
			let normal = DispatchInfo { weight: 100, ..Default::default() };
			let op = DispatchInfo { weight: 100, class: DispatchClass::Operational, pays_fee: true };
			let len = 0_usize;
Kian Peymani's avatar
Kian Peymani committed
			let normal_limit = normal_weight_limit();

			// given almost full block
Kian Peymani's avatar
Kian Peymani committed
			AllExtrinsicsWeight::put(normal_limit);
			// will not fit.
			assert!(CheckWeight::<Test>(PhantomData).pre_dispatch(&1, CALL, normal, len).is_err());
			assert!(CheckWeight::<Test>(PhantomData).pre_dispatch(&1, CALL, op, len).is_ok());

			// likewise for length limit.
			let len = 100_usize;
Kian Peymani's avatar
Kian Peymani committed
			AllExtrinsicsLen::put(normal_length_limit());
			assert!(CheckWeight::<Test>(PhantomData).pre_dispatch(&1, CALL, normal, len).is_err());
			assert!(CheckWeight::<Test>(PhantomData).pre_dispatch(&1, CALL, op, len).is_ok());
		})
	}

	#[test]
	fn signed_ext_check_weight_priority_works() {
		new_test_ext().execute_with(|| {
			let normal = DispatchInfo { weight: 100, class: DispatchClass::Normal, pays_fee: true };
			let op = DispatchInfo { weight: 100, class: DispatchClass::Operational, pays_fee: true };
			let len = 0_usize;

			let priority = CheckWeight::<Test>(PhantomData)
				.validate(&1, CALL, normal, len)
				.unwrap()
				.priority;
			assert_eq!(priority, 100);

			let priority = CheckWeight::<Test>(PhantomData)
				.validate(&1, CALL, op, len)
				.unwrap()
				.priority;
			assert_eq!(priority, u64::max_value());
		})
	}

	#[test]
	fn signed_ext_check_weight_block_size_works() {
		new_test_ext().execute_with(|| {
Kian Peymani's avatar
Kian Peymani committed
			let normal = DispatchInfo::default();
			let normal_limit = normal_weight_limit() as usize;
			let reset_check_weight = |tx, s, f| {
				AllExtrinsicsLen::put(0);
				let r = CheckWeight::<Test>(PhantomData).pre_dispatch(&1, CALL, tx, s);
				if f { assert!(r.is_err()) } else { assert!(r.is_ok()) }
			};

Kian Peymani's avatar
Kian Peymani committed
			reset_check_weight(normal, normal_limit - 1, false);
			reset_check_weight(normal, normal_limit, false);
			reset_check_weight(normal, normal_limit + 1, true);

			// Operational ones don't have this limit.
			let op = DispatchInfo { weight: 0, class: DispatchClass::Operational, pays_fee: true };
Kian Peymani's avatar
Kian Peymani committed
			reset_check_weight(op, normal_limit, false);
			reset_check_weight(op, normal_limit + 100, false);
			reset_check_weight(op, 1024, false);
			reset_check_weight(op, 1025, true);
		})
	}

	#[test]
	fn signed_ext_check_era_should_work() {
		new_test_ext().execute_with(|| {
			// future
			assert_eq!(
				CheckEra::<Test>::from(Era::mortal(4, 2)).additional_signed().err().unwrap(),
				InvalidTransaction::AncientBirthBlock.into(),
			);

			// correct
			System::set_block_number(13);
			<BlockHash<Test>>::insert(12, H256::repeat_byte(1));
			assert!(CheckEra::<Test>::from(Era::mortal(4, 12)).additional_signed().is_ok());
		})
	}

	#[test]
	fn signed_ext_check_era_should_change_longevity() {
		new_test_ext().execute_with(|| {
			let normal = DispatchInfo { weight: 100, class: DispatchClass::Normal, pays_fee: true };
			let len = 0_usize;
			let ext = (
				CheckWeight::<Test>(PhantomData),
				CheckEra::<Test>::from(Era::mortal(16, 256)),
			);
			System::set_block_number(17);
			<BlockHash<Test>>::insert(16, H256::repeat_byte(1));

			assert_eq!(ext.validate(&1, CALL, normal, len).unwrap().longevity, 15);


	#[test]
	fn set_code_checks_works() {
		struct CallInWasm(Vec<u8>);

		impl sp_core::traits::CallInWasm for CallInWasm {
			fn call_in_wasm(
				&self,
				_: &[u8],