Skip to content

Instantly share code, notes, and snippets.

@shawntabrizi
Created February 17, 2020 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shawntabrizi/a294c69c9cd12b34e1e0fece0cc7def7 to your computer and use it in GitHub Desktop.
Save shawntabrizi/a294c69c9cd12b34e1e0fece0cc7def7 to your computer and use it in GitHub Desktop.
/// 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, dispatch::DispatchResult, ensure};
use codec::{Encode, Decode};
use system::ensure_signed;
use sp_std::prelude::Vec;
/// The module's configuration trait.
pub trait Trait: system::Trait {
// TODO: Add other types and constants required configure this module.
/// The overarching event type.
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
}
#[derive(Encode, Decode, Debug, Clone, Default, PartialEq)]
struct Identity<AccountId> {
owner: AccountId,
key_index: u32,
}
// This module's storage items.
decl_storage! {
trait Store for Module<T: Trait> as TemplateModule {
Identities: map T::Hash => Identity<T::AccountId>;
}
}
// The module's dispatchable functions.
decl_module! {
/// The module declaration.
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
// Initializing events
// this is needed only if you are using events in your module
fn deposit_event() = default;
pub fn register_hash(origin, user_hash: T::Hash, _pkey: Vec<u8>) -> DispatchResult {
let sender = ensure_signed(origin)?;
ensure!(!<Identities<T>>::exists(user_hash), "This Identity already exists.");
let new_user = Identity {
owner: sender.clone(),
key_index: 0,
};
<Identities<T>>::insert(user_hash.clone(), new_user.clone());
Self::deposit_event(RawEvent::HashRegistered(sender, user_hash));
Ok(())
}
}
}
decl_event!(
pub enum Event<T> where
AccountId = <T as system::Trait>::AccountId,
Hash = <T as system::Trait>::Hash,
{
HashRegistered(AccountId, Hash),
}
);
/// tests for this module
#[cfg(test)]
mod tests {
use super::*;
use sp_core::H256;
use frame_support::{impl_outer_origin, assert_ok, 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() {
new_test_ext().execute_with(|| {
let hash: <Test as system::Trait>::Hash = Default::default();
assert_ok!(TemplateModule::register_hash(Origin::signed(1), hash, vec![]));
assert_eq!(Identities::<Test>::get(hash).owner, 1);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment