Skip to content

Instantly share code, notes, and snippets.

@shawntabrizi
Created September 10, 2019 16:17
Show Gist options
  • Save shawntabrizi/4d66a7db8d3b665fd6a7787fbd5e77cd to your computer and use it in GitHub Desktop.
Save shawntabrizi/4d66a7db8d3b665fd6a7787fbd5e77cd to your computer and use it in GitHub Desktop.
use support::{decl_module, decl_storage, decl_event, StorageValue, dispatch::Result};
use system::ensure_signed;
use rstd::prelude::*;
use rstd::collections::btree_set::BTreeSet;
pub trait Trait: system::Trait {
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
}
decl_storage! {
trait Store for Module<T: Trait> as TemplateModule {
State: BTreeSet<Vec<u8>>;
}
}
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
fn deposit_event() = default;
pub fn add_bytes(origin, bytes: Vec<u8>) -> Result {
let who = ensure_signed(origin)?;
let mut state = <Self as Store>::State::get();
state.insert(bytes);
<Self as Store>::State::put(state);
Self::deposit_event(RawEvent::Stored(who));
Ok(())
}
}
}
decl_event!(
pub enum Event<T> where AccountId = <T as system::Trait>::AccountId {
Stored(AccountId),
}
);
impl<T: Trait> Module<T> {
pub fn state() -> BTreeSet<Vec<u8>> {
<Self as Store>::State::get()
}
}
/// tests for this module
#[cfg(test)]
mod tests {
use super::*;
use runtime_io::with_externalities;
use primitives::{H256, Blake2Hasher};
use support::{impl_outer_origin, assert_ok, parameter_types};
use sr_primitives::{traits::{BlakeTwo256, IdentityLookup}, testing::Header};
use sr_primitives::weights::Weight;
use sr_primitives::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 WeightMultiplierUpdate = ();
type Event = ();
type BlockHashCount = BlockHashCount;
type MaximumBlockWeight = MaximumBlockWeight;
type MaximumBlockLength = MaximumBlockLength;
type AvailableBlockRatio = AvailableBlockRatio;
type Version = ();
}
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() -> runtime_io::TestExternalities<Blake2Hasher> {
system::GenesisConfig::default().build_storage::<Test>().unwrap().into()
}
#[test]
fn it_works() {
with_externalities(&mut new_test_ext(), || {
assert_ok!(TemplateModule::add_bytes(Origin::signed(1), [1,2,3,4].to_vec()));
assert!(TemplateModule::state().contains(&[1,2,3,4].to_vec()));
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment