Node template folders restructuring (#4811)

* Restructure node-template so it is clear that node, runtime, and pallets are separated
* Separating to mock and tests
* restructuring runtime to top-level
* updated release script
* updated Cargo.lock
This commit is contained in:
Jimmy Chu
2020-02-06 20:13:44 +08:00
committed by GitHub
parent 6148c7ce15
commit acb66f0699
17 changed files with 205 additions and 126 deletions
@@ -37,6 +37,9 @@ pub use frame_support::{
weights::Weight,
};
/// Importing a template pallet
pub use template;
/// An index to a block.
pub type BlockNumber = u32;
@@ -63,9 +66,6 @@ pub type Hash = sp_core::H256;
/// Digest item type.
pub type DigestItem = generic::DigestItem<Hash>;
/// Used for the module template in `./template.rs`
mod template;
/// Opaque types. These are used by the CLI to instantiate machinery that don't need to know
/// the specifics of the runtime. They can then be made to be agnostic over specific formats
/// of data like extrinsics, allowing for them to continue syncing the network through upgrades
@@ -1,176 +0,0 @@
/// A runtime module template with necessary imports
/// Feel free to remove or edit this file as needed.
/// If you change the name of this file, make sure to update its references in runtime/src/lib.rs
/// If you remove this file, you can remove those references
/// For more guidance on Substrate modules, see the example module
/// https://github.com/paritytech/substrate/blob/master/frame/example/src/lib.rs
use frame_support::{decl_module, decl_storage, decl_event, decl_error, dispatch};
use system::ensure_signed;
/// The pallet's configuration trait.
pub trait Trait: system::Trait {
// Add other types and constants required to configure this pallet.
/// The overarching event type.
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
}
// This pallet's storage items.
decl_storage! {
trait Store for Module<T: Trait> as TemplateModule {
// Just a dummy storage item.
// Here we are declaring a StorageValue, `Something` as a Option<u32>
// `get(fn something)` is the default getter which returns either the stored `u32` or `None` if nothing stored
Something get(fn something): Option<u32>;
}
}
// The pallet's events
decl_event!(
pub enum Event<T> where AccountId = <T as system::Trait>::AccountId {
/// Just a dummy event.
/// Event `Something` is declared with a parameter of the type `u32` and `AccountId`
/// To emit this event, we call the deposit funtion, from our runtime funtions
SomethingStored(u32, AccountId),
}
);
// The pallet's errors
decl_error! {
pub enum Error for Module<T: Trait> {
/// Value was None
NoneValue,
/// Value reached maximum and cannot be incremented further
StorageOverflow,
}
}
// The pallet's dispatchable functions.
decl_module! {
/// The module declaration.
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
// Initializing errors
// this includes information about your errors in the node's metadata.
// it is needed only if you are using errors in your pallet
type Error = Error<T>;
// Initializing events
// this is needed only if you are using events in your pallet
fn deposit_event() = default;
/// Just a dummy entry point.
/// function that can be called by the external world as an extrinsics call
/// takes a parameter of the type `AccountId`, stores it, and emits an event
pub fn do_something(origin, something: u32) -> dispatch::DispatchResult {
// Check it was signed and get the signer. See also: ensure_root and ensure_none
let who = ensure_signed(origin)?;
// Code to execute when something calls this.
// For example: the following line stores the passed in u32 in the storage
Something::put(something);
// Here we are raising the Something event
Self::deposit_event(RawEvent::SomethingStored(something, who));
Ok(())
}
/// Another dummy entry point.
/// takes no parameters, attempts to increment storage value, and possibly throws an error
pub fn cause_error(origin) -> dispatch::DispatchResult {
// Check it was signed and get the signer. See also: ensure_root and ensure_none
let _who = ensure_signed(origin)?;
match Something::get() {
None => Err(Error::<T>::NoneValue)?,
Some(old) => {
let new = old.checked_add(1).ok_or(Error::<T>::StorageOverflow)?;
Something::put(new);
Ok(())
},
}
}
}
}
/// Tests for this pallet
#[cfg(test)]
mod tests {
use super::*;
use sp_core::H256;
use frame_support::{impl_outer_origin, assert_ok, assert_noop, parameter_types, weights::Weight};
use sp_runtime::{
traits::{BlakeTwo256, IdentityLookup}, testing::Header, Perbill,
};
impl_outer_origin! {
pub enum Origin for Test {}
}
// For testing the module, we construct most of a mock runtime. This means
// first constructing a configuration type (`Test`) which `impl`s each of the
// configuration traits of modules 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::from_percent(75);
}
impl system::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 = ();
type BlockHashCount = BlockHashCount;
type MaximumBlockWeight = MaximumBlockWeight;
type MaximumBlockLength = MaximumBlockLength;
type AvailableBlockRatio = AvailableBlockRatio;
type Version = ();
type ModuleToIndex = ();
}
impl Trait for Test {
type Event = ();
}
type TemplateModule = Module<Test>;
// This function basically just builds a genesis storage key/value store according to
// our desired mockup.
fn new_test_ext() -> sp_io::TestExternalities {
system::GenesisConfig::default().build_storage::<Test>().unwrap().into()
}
#[test]
fn it_works_for_default_value() {
new_test_ext().execute_with(|| {
// Just a dummy test for the dummy funtion `do_something`
// calling the `do_something` function with a value 42
assert_ok!(TemplateModule::do_something(Origin::signed(1), 42));
// asserting that the stored value is equal to what we stored
assert_eq!(TemplateModule::something(), Some(42));
});
}
#[test]
fn correct_error_for_none_value() {
new_test_ext().execute_with(|| {
// Ensure the correct error is thrown on None value
assert_noop!(
TemplateModule::cause_error(Origin::signed(1)),
Error::<Test>::NoneValue
);
});
}
}