Skip to content

Commit

Permalink
Tests for extrinsic
Browse files Browse the repository at this point in the history
  • Loading branch information
vayesy committed Mar 2, 2022
1 parent ff39da2 commit e0d0b18
Show file tree
Hide file tree
Showing 6 changed files with 305 additions and 325 deletions.
1 change: 1 addition & 0 deletions signal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkad
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.13" }
sp-io = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.13" }
pallet-randomness-collective-flip = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.13" }
frame-support-test = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.13" }
orml-currencies = { path = "../../orml/currencies", default-features = false }

[features]
Expand Down
57 changes: 31 additions & 26 deletions signal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ pub mod pallet {
use frame_support::{
dispatch::DispatchResult,
traits::{Randomness},
pallet_prelude::*
pallet_prelude::*,
transactional
};
use sp_std::vec::Vec;

Expand Down Expand Up @@ -187,7 +188,10 @@ pub mod pallet {
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
Proposal(T::AccountId, T::Hash),
Proposal {
sender_id: T::AccountId,
proposal_id: T::Hash
},
ProposalCreated(T::AccountId, T::Hash, T::Hash, Balance, T::BlockNumber),
ProposalVoted(T::AccountId, T::Hash, bool),
ProposalFinalized(T::Hash, u8),
Expand Down Expand Up @@ -244,13 +248,10 @@ pub mod pallet {
#[pallet::call]
impl<T: Config> Pallet<T> {

#[pallet::weight(10_000)]
pub fn simple_one(origin: OriginFor<T>) -> DispatchResult {
Ok(())
}

// TODO: general proposal for a DAO
#[pallet::weight(5_000_000)]
#[transactional]
pub fn general_proposal(
origin: OriginFor<T>,
context_id: T::Hash,
Expand Down Expand Up @@ -285,16 +286,22 @@ pub mod pallet {
let proposal_type = ProposalType::General;
let proposal_state = ProposalState::Active;
let voting_type = VotingType::Simple;
// let nonce = <Nonce<T>>::get();

// generate unique id
let phrase = b"just another proposal";
let proposal_id = <T::Randomness>::random(phrase).0;
// ensure!(!<Proposals<T>>::contains_key(&context_id), "Proposal id already exists");
ensure!(!<Proposals<T>>::contains_key(&context_id), Error::<T>::ProposalExists); // todo: was error generated manually on purpose?


// check add
let proposals_count = <ProposalsCount::<T>>::get();
let updated_proposals_count = proposals_count.checked_add(1).ok_or( Error::<T>::OverflowError)?;
let proposals_by_campaign_count = <ProposalsByContextCount::<T>>::get(&context_id);
let updated_proposals_by_campaign_count = proposals_by_campaign_count.checked_add(1).ok_or( Error::<T>::OverflowError )?;
let proposals_by_owner_count = <ProposalsByOwnerCount::<T>>::get(&sender);
let updated_proposals_by_owner_count = proposals_by_owner_count.checked_add(1).ok_or( Error::<T>::OverflowError )?;

// proposal

let nonce = Self::get_and_increment_nonce();
let (proposal_id, _) = <T::Randomness>::random(&nonce);
let new_proposal = Proposal {
proposal_id: proposal_id.clone(),
context_id: context_id.clone(),
Expand All @@ -316,14 +323,6 @@ pub mod pallet {
//
//

// check add
let proposals_count = <ProposalsCount::<T>>::get();
let updated_proposals_count = proposals_count.checked_add(1).ok_or( Error::<T>::OverflowError)?;
let proposals_by_campaign_count = <ProposalsByContextCount::<T>>::get(&context_id);
let updated_proposals_by_campaign_count = proposals_by_campaign_count.checked_add(1).ok_or( Error::<T>::OverflowError )?;
let proposals_by_owner_count = <ProposalsByOwnerCount::<T>>::get(&sender);
let updated_proposals_by_owner_count = proposals_by_owner_count.checked_add(1).ok_or( Error::<T>::OverflowError )?;

// insert proposals
<Proposals::<T>>::insert(proposal_id.clone(), new_proposal.clone());
<Metadata::<T>>::insert(proposal_id.clone(), metadata.clone());
Expand Down Expand Up @@ -352,10 +351,10 @@ pub mod pallet {
//

// nonce++
<Nonce::<T>>::mutate(|n| *n += 1); // todo: use safe addition
// <Nonce::<T>>::mutate(|n| *n += 1); // todo: use safe addition

// deposit event
Self::deposit_event(Event::<T>::Proposal(sender,proposal_id));
Self::deposit_event(Event::<T>::Proposal{sender_id: sender, proposal_id});
Ok(())
}

Expand All @@ -377,7 +376,7 @@ pub mod pallet {
// match action
// action
// deposit event
Self::deposit_event(Event::<T>::Proposal(sender, context));
Self::deposit_event(Event::<T>::Proposal{sender_id: sender, proposal_id: context});
Ok(())
}

Expand Down Expand Up @@ -430,10 +429,10 @@ pub mod pallet {

let proposal_type = ProposalType::Withdrawal; // treasury
let voting_type = VotingType::Simple; // votes
// let nonce = <Nonce<T>>::get();
let phrase = b"just another withdrawal";
let nonce = Self::get_and_increment_nonce();
// let phrase = b"just another withdrawal";

let proposal_id = <T as Config>::Randomness::random(phrase).0;
let (proposal_id, _) = <T as Config>::Randomness::random(&nonce.encode());
ensure!(!<Proposals<T>>::contains_key(&context_id), Error::<T>::HashCollision );

let proposal = Proposal {
Expand Down Expand Up @@ -481,7 +480,7 @@ pub mod pallet {

// ++

<Nonce<T>>::mutate(|n| *n += 1); // todo: safe nonce increase
// <Nonce<T>>::mutate(|n| *n += 1); // todo: safe nonce increase

// E V E N T

Expand Down Expand Up @@ -799,6 +798,12 @@ pub mod pallet {
Ok(())

}

fn get_and_increment_nonce() -> Vec<u8> {
let nonce = Nonce::<T>::get();
Nonce::<T>::put(nonce.wrapping_add(1));
nonce.encode()
}
}
}

Expand Down
60 changes: 47 additions & 13 deletions signal/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
use crate as pallet_signal;
use frame_support::parameter_types;
use frame_system as system;
use frame_support_test::TestRandomness;
use sp_std::cell::RefCell;
use sp_runtime::{
testing::Header,
traits::{BlakeTwo256, IdentityLookup},
Expand All @@ -17,22 +19,55 @@ type AccountId = u64;
pub const ACC1: AccountId = 1;
pub const ACC2: AccountId = 2;

pub struct ControlFixture {
pub body_controller: AccountId,
pub body_treasury: AccountId,
pub body_member_state: ControlMemberState,
pub body_state: ControlState
}
impl ControlFixture {
pub fn body_controller(&mut self, value: AccountId) {
self.body_controller = value;
}
pub fn body_state(&mut self, value: ControlState) {
self.body_state = value;
}
pub fn body_member_state(&mut self, value: ControlMemberState) {
self.body_member_state = value;
}
}
thread_local!(
pub static control_fixture: RefCell<ControlFixture> = RefCell::new(ControlFixture {
body_controller: ACC1,
body_treasury: ACC2,
body_member_state: ControlMemberState::Active,
body_state: ControlState::Active
});
);


pub struct ControlMock;
// impl<AccountId, Hash> ControlPalletStorage<AccountId, Hash> for ControlMock {
impl ControlPalletStorage<AccountId, Hash> for ControlMock {

fn body_controller(org: &Hash) -> AccountId { ACC1 }
fn body_treasury(org: &Hash) -> AccountId { ACC2 }
fn body_member_state(hash: &Hash, account_id: &AccountId) -> ControlMemberState { ControlMemberState::Active }
fn body_state(hash: &Hash) -> ControlState { ControlState::Active }
fn body_controller(_org: &Hash) -> AccountId { control_fixture.with(|v| v.borrow().body_controller.clone()) }
fn body_treasury(_org: &Hash) -> AccountId { control_fixture.with(|v| v.borrow().body_treasury.clone()) }
fn body_member_state(_hash: &Hash, _account_id: &AccountId) -> ControlMemberState { control_fixture.with(|v| v.borrow().body_member_state.clone()) }
fn body_state(_hash: &Hash) -> ControlState { control_fixture.with(|v| v.borrow().body_state.clone()) }
}
// impl ControlPalletStorage<AccountId, Hash> for ControlMock {

// fn body_controller(_org: &Hash) -> AccountId { ACC1 }
// fn body_treasury(_org: &Hash) -> AccountId { ACC2 }
// fn body_member_state(_hash: &Hash, _account_id: &AccountId) -> ControlMemberState { ControlMemberState::Active }
// fn body_state(_hash: &Hash) -> ControlState { ControlState::Active }
// }

pub struct FlowMock;
impl Flow<Hash, Balance> for FlowMock {
fn campaign_balance(hash: &Hash) -> Balance { Default::default() }
fn campaign_state(hash: &Hash) -> FlowState { FlowState::Active }
fn campaign_contributors_count(hash: &Hash) -> u64 { 0 }
fn campaign_org(hash: &Hash) -> Hash { Default::default() }
fn campaign_balance(_hash: &Hash) -> Balance { Default::default() }
fn campaign_state(_hash: &Hash) -> FlowState { FlowState::Active }
fn campaign_contributors_count(_hash: &Hash) -> u64 { 0 }
fn campaign_org(_hash: &Hash) -> Hash { Default::default() }
}

type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
Expand All @@ -47,7 +82,6 @@ frame_support::construct_runtime!(
{
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
Signal: pallet_signal::{Pallet, Call, Storage, Event<T>},
RandomnessCollectiveFlip: pallet_randomness_collective_flip::{Pallet, Storage},
}
);

Expand All @@ -58,12 +92,12 @@ parameter_types! {
frame_system::limits::BlockWeights::simple_max(1024);
pub static ExistentialDeposit: u64 = 0;

pub const MaxProposalsPerBlock: u32 = 1;
pub const MaxProposalsPerBlock: u32 = 2;
pub const MaxProposalDuration: u32 = 20;
pub const FundingCurrencyId: CurrencyId = TokenSymbol::GAME as u32;
}

impl pallet_randomness_collective_flip::Config for Test {}
// impl pallet_randomness_collective_flip::Config for Test {}

impl system::Config for Test {
type BaseCallFilter = frame_support::traits::Everything;
Expand Down Expand Up @@ -100,7 +134,7 @@ impl pallet_signal::Config for Test {
type MaxProposalsPerBlock = MaxProposalsPerBlock;
type MaxProposalDuration = MaxProposalDuration;
type FundingCurrencyId = FundingCurrencyId;
type Randomness = RandomnessCollectiveFlip;
type Randomness = TestRandomness<Self>;
// type Currency = Currency<Config, AccountId>;
}

Expand Down
Loading

0 comments on commit e0d0b18

Please sign in to comment.