git.delta.rocks / unique-network / refs/commits / 22fc4951909c

difftreelog

source

substrate-node-template/runtime/src/template.rs4.4 KiBsourcehistory
1/// A runtime module template with necessary imports23/// Feel free to remove or edit this file as needed.4/// If you change the name of this file, make sure to update its references in runtime/src/lib.rs5/// If you remove this file, you can remove those references678/// For more guidance on Substrate modules, see the example module9/// https://github.com/paritytech/substrate/blob/master/srml/example/src/lib.rs1011use support::{decl_module, decl_storage, decl_event, StorageValue, dispatch::Result};12use system::ensure_signed;1314/// The module's configuration trait.15pub trait Trait: system::Trait {16	// TODO: Add other types and constants required configure this module.1718	/// The overarching event type.19	type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;20}2122// This module's storage items.23decl_storage! {24	trait Store for Module<T: Trait> as TemplateModule {25		// Just a dummy storage item.26		// Here we are declaring a StorageValue, `Something` as a Option<u32>27		// `get(something)` is the default getter which returns either the stored `u32` or `None` if nothing stored28		Something get(something): Option<u32>;29	}30}3132// The module's dispatchable functions.33decl_module! {34	/// The module declaration.35	pub struct Module<T: Trait> for enum Call where origin: T::Origin {36		// Initializing events37		// this is needed only if you are using events in your module38		fn deposit_event() = default;3940		// Just a dummy entry point.41		// function that can be called by the external world as an extrinsics call42		// takes a parameter of the type `AccountId`, stores it and emits an event43		pub fn do_something(origin, something: u32) -> Result {44			// TODO: You only need this if you want to check it was signed.45			let who = ensure_signed(origin)?;4647			// TODO: Code to execute when something calls this.48			// For example: the following line stores the passed in u32 in the storage49			Something::put(something);5051			// here we are raising the Something event52			Self::deposit_event(RawEvent::SomethingStored(something, who));53			Ok(())54		}55	}56}5758decl_event!(59	pub enum Event<T> where AccountId = <T as system::Trait>::AccountId {60		// Just a dummy event.61		// Event `Something` is declared with a parameter of the type `u32` and `AccountId`62		// To emit this event, we call the deposit funtion, from our runtime funtions63		SomethingStored(u32, AccountId),64	}65);6667/// tests for this module68#[cfg(test)]69mod tests {70	use super::*;7172	use runtime_io::with_externalities;73	use primitives::{H256, Blake2Hasher};74	use support::{impl_outer_origin, assert_ok, parameter_types};75	use sr_primitives::{traits::{BlakeTwo256, IdentityLookup}, testing::Header};76	use sr_primitives::weights::Weight;77	use sr_primitives::Perbill;7879	impl_outer_origin! {80		pub enum Origin for Test {}81	}8283	// For testing the module, we construct most of a mock runtime. This means84	// first constructing a configuration type (`Test`) which `impl`s each of the85	// configuration traits of modules we want to use.86	#[derive(Clone, Eq, PartialEq)]87	pub struct Test;88	parameter_types! {89		pub const BlockHashCount: u64 = 250;90		pub const MaximumBlockWeight: Weight = 1024;91		pub const MaximumBlockLength: u32 = 2 * 1024;92		pub const AvailableBlockRatio: Perbill = Perbill::from_percent(75);93	}94	impl system::Trait for Test {95		type Origin = Origin;96		type Call = ();97		type Index = u64;98		type BlockNumber = u64;99		type Hash = H256;100		type Hashing = BlakeTwo256;101		type AccountId = u64;102		type Lookup = IdentityLookup<Self::AccountId>;103		type Header = Header;104		type WeightMultiplierUpdate = ();105		type Event = ();106		type BlockHashCount = BlockHashCount;107		type MaximumBlockWeight = MaximumBlockWeight;108		type MaximumBlockLength = MaximumBlockLength;109		type AvailableBlockRatio = AvailableBlockRatio;110		type Version = ();111	}112	impl Trait for Test {113		type Event = ();114	}115	type TemplateModule = Module<Test>;116117	// This function basically just builds a genesis storage key/value store according to118	// our desired mockup.119	fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {120		system::GenesisConfig::default().build_storage::<Test>().unwrap().into()121	}122123	#[test]124	fn it_works_for_default_value() {125		with_externalities(&mut new_test_ext(), || {126			// Just a dummy test for the dummy funtion `do_something`127			// calling the `do_something` function with a value 42128			assert_ok!(TemplateModule::do_something(Origin::signed(1), 42));129			// asserting that the stored value is equal to what we stored130			assert_eq!(TemplateModule::something(), Some(42));131		});132	}133}