Skip to content

Commit

Permalink
fix merge residual compile errors
Browse files Browse the repository at this point in the history
setup initial mock structure

establish privelleged function test

save progress

first pass new_test_ext

test sudo to make sure it error when non-root

add set keys tests

fix unused result from set_key call

parent 5151bd7
author zeke <[email protected]> 1589076740 -0700
committer zeke <[email protected]> 1589350443 -0700

parent 5151bd7
author zeke <[email protected]> 1589076740 -0700
committer zeke <[email protected]> 1589350442 -0700

remove unused imports warnings

fix unused result from set_key call

remove unused imports warnings

pre master merge

Expose BlockHashCount on system metadata constants (paritytech#5960)

squash

fix whitespace

spelling and whitespace

a single pesky space

add logger module to mock

add logger dispatch to privlleged function

sub logger in for dummy functions

create first of several event tests

first pass at test coverage for events

comment house keeping

pre master merge

Expose BlockHashCount on system metadata constants (paritytech#5960)

Expose BlockHashCount on system metadata constants (paritytech#5960)

fix whitespace

spell check

implement last_seen_account storage item, event, and, logger function

create vec account log and use in tests

allow weight to be passed into account log

refactor all log dispatchables

save progress

complete initial transition to refactored logger

cleaning
  • Loading branch information
emostov committed May 13, 2020
1 parent e085300 commit f4b0afc
Show file tree
Hide file tree
Showing 11 changed files with 376 additions and 73 deletions.
34 changes: 0 additions & 34 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@
use sp_std::prelude::*;
use frame_support::{
construct_runtime, parameter_types, debug,
<<<<<<< HEAD
weights::Weight,
traits::{Currency, Randomness, OnUnbalanced, Imbalance, LockIdentifier},
=======
weights::{
Weight,
constants::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight, WEIGHT_PER_SECOND},
Expand All @@ -37,7 +33,6 @@ use sp_core::{
crypto::KeyTypeId,
u32_trait::{_1, _2, _3, _4},
OpaqueMetadata,
>>>>>>> upstream/master
};
pub use node_primitives::{AccountId, Signature};
use node_primitives::{AccountIndex, Balance, BlockNumber, Hash, Index, Moment};
Expand Down Expand Up @@ -397,21 +392,12 @@ parameter_types! {
pub const VotingBond: Balance = 1 * DOLLARS;
pub const TermDuration: BlockNumber = 7 * DAYS;
pub const DesiredMembers: u32 = 13;
<<<<<<< HEAD
pub const DesiredRunnersUp: u32 = 7;
pub const ElectionsPhragmenModuleId: LockIdentifier = *b"phrelect";
}

impl pallet_elections_phragmen::Trait for Runtime {
type ModuleId = ElectionsPhragmenModuleId;
=======
pub const DesiredRunnersUp: u32 = 7;
pub const ElectionsPhragmenModuleId: LockIdentifier = *b"phrelect";
}

impl pallet_elections_phragmen::Trait for Runtime {
type ModuleId = ElectionsPhragmenModuleId;
>>>>>>> upstream/master
type Event = Event;
type Currency = Balances;
type ChangeMembers = Council;
Expand Down Expand Up @@ -460,13 +446,8 @@ parameter_types! {
pub const TipCountdown: BlockNumber = 1 * DAYS;
pub const TipFindersFee: Percent = Percent::from_percent(20);
pub const TipReportDepositBase: Balance = 1 * DOLLARS;
<<<<<<< HEAD
pub const TipReportDepositPerByte: Balance = 1 * CENTS;
pub const TreasuryModuleId: ModuleId = ModuleId(*b"py/trsry");
=======
pub const TipReportDepositPerByte: Balance = 1 * CENTS;
pub const TreasuryModuleId: ModuleId = ModuleId(*b"py/trsry");
>>>>>>> upstream/master
}

impl pallet_treasury::Trait for Runtime {
Expand All @@ -483,13 +464,8 @@ impl pallet_treasury::Trait for Runtime {
type ProposalBond = ProposalBond;
type ProposalBondMinimum = ProposalBondMinimum;
type SpendPeriod = SpendPeriod;
<<<<<<< HEAD
type Burn = Burn;
type ModuleId = TreasuryModuleId;
=======
type Burn = Burn;
type ModuleId = TreasuryModuleId;
>>>>>>> upstream/master
}

parameter_types! {
Expand Down Expand Up @@ -679,13 +655,8 @@ parameter_types! {
pub const RotationPeriod: BlockNumber = 80 * HOURS;
pub const PeriodSpend: Balance = 500 * DOLLARS;
pub const MaxLockDuration: BlockNumber = 36 * 30 * DAYS;
<<<<<<< HEAD
pub const ChallengePeriod: BlockNumber = 7 * DAYS;
pub const SocietyModuleId: ModuleId = ModuleId(*b"py/socie");
=======
pub const ChallengePeriod: BlockNumber = 7 * DAYS;
pub const SocietyModuleId: ModuleId = ModuleId(*b"py/socie");
>>>>>>> upstream/master
}

impl pallet_society::Trait for Runtime {
Expand All @@ -701,13 +672,8 @@ impl pallet_society::Trait for Runtime {
type MaxLockDuration = MaxLockDuration;
type FounderSetOrigin = pallet_collective::EnsureProportionMoreThan<_1, _2, AccountId, CouncilCollective>;
type SuspensionJudgementOrigin = pallet_society::EnsureFounder<Runtime>;
<<<<<<< HEAD
type ChallengePeriod = ChallengePeriod;
type ModuleId = SocietyModuleId;
=======
type ChallengePeriod = ChallengePeriod;
type ModuleId = SocietyModuleId;
>>>>>>> upstream/master
}

parameter_types! {
Expand Down
12 changes: 1 addition & 11 deletions frame/elections-phragmen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ type NegativeImbalanceOf<T> =

pub trait Trait: frame_system::Trait {
/// The overarching event type.c
type Event: From<Event<Self>> + Into<<Self as frame_system::Trait>::Event>;

type ModuleId: Get<LockIdentifier>;
type Event: From<Event<Self>> + Into<<Self as frame_system::Trait>::Event>;

/// Identifier for the elections-phragmen pallet's lock
type ModuleId: Get<LockIdentifier>;
Expand Down Expand Up @@ -1022,14 +1020,6 @@ mod tests {
fn convert(x: u128) -> u64 {
x as u64
}
}

parameter_types!{
pub const ElectionsPhragmenModuleId: LockIdentifier = *b"phrelect";
}

parameter_types!{
pub const ElectionsPhragmenModuleId: LockIdentifier = *b"phrelect";
}

impl Trait for Test {
Expand Down
10 changes: 3 additions & 7 deletions frame/elections/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,6 @@ type ApprovalFlag = u32;
const APPROVAL_FLAG_LEN: usize = 32;

pub trait Trait: frame_system::Trait {
type ModuleId: Get<LockIdentifier>;

type Event: From<Event<Self>> + Into<<Self as frame_system::Trait>::Event>;

/// Identifier for the elections pallet's lock
Expand Down Expand Up @@ -380,11 +378,9 @@ decl_module! {
/// The chunk size of the voter vector.
const VOTER_SET_SIZE: u32 = VOTER_SET_SIZE as u32;
/// The chunk size of the approval vector.
const APPROVAL_SET_SIZE: u32 = APPROVAL_SET_SIZE as u32;

const MouduleId: LockIdentifier = T::ModuleId::get();
const APPROVAL_SET_SIZE: u32 = APPROVAL_SET_SIZE as u32;

const ModuleId: LockIdentifier = T::ModuleId::get();
const MouduleId: LockIdentifier = T::ModuleId::get();

fn deposit_event() = default;

Expand Down Expand Up @@ -903,7 +899,7 @@ impl<T: Trait> Module<T> {
&who,
locked_balance,
WithdrawReasons::all(),
);
);

<VoterInfoOf<T>>::insert(
&who,
Expand Down
6 changes: 2 additions & 4 deletions frame/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,9 @@ decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
type Error = Error<T>;

fn deposit_event() = default;

const MouduleId: ModuleId = T::ModuleId::get();
fn deposit_event() = default;

const ModuleId: ModuleId = T::ModuleId::get();
const MouduleId: ModuleId = T::ModuleId::get();

/// Deposit balance from currency/balances module into EVM.
#[weight = 0]
Expand Down
10 changes: 2 additions & 8 deletions frame/society/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,7 @@ type BalanceOf<T, I> = <<T as Trait<I>>::Currency as Currency<<T as system::Trai
/// The module's configuration trait.
pub trait Trait<I=DefaultInstance>: system::Trait {
/// The overarching event type.
type Event: From<Event<Self, I>> + Into<<Self as system::Trait>::Event>;

/// The societies's module id
type ModuleId: Get<ModuleId>;
type Event: From<Event<Self, I>> + Into<<Self as system::Trait>::Event>;

/// The societies's module id
type ModuleId: Get<ModuleId>;
Expand Down Expand Up @@ -493,10 +490,7 @@ decl_module! {
const RotationPeriod: T::BlockNumber = T::RotationPeriod::get();

/// The number of blocks between membership challenges.
const ChallengePeriod: T::BlockNumber = T::ChallengePeriod::get();

/// The societies's module id
const ModuleId: ModuleId = T::ModuleId::get();
const ChallengePeriod: T::BlockNumber = T::ChallengePeriod::get();

/// The societies's module id
const ModuleId: ModuleId = T::ModuleId::get();
Expand Down
2 changes: 1 addition & 1 deletion frame/society/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl frame_system::Trait for Test {
type ModuleToIndex = ();
type OnNewAccount = ();
type OnKilledAccount = ();
type AccountData = pallet_balances::AccountData<u64>;
type AccountData = pallet_balances::AccountData<u64>;
}

impl pallet_balances::Trait for Test {
Expand Down
5 changes: 5 additions & 0 deletions frame/sudo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ use frame_support::{
use frame_support::weights::{Weight, GetDispatchInfo, FunctionOf, Pays};
use frame_system::{self as system, ensure_signed};

#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;

pub trait Trait: frame_system::Trait {
/// The overarching event type.
type Event: From<Event<Self>> + Into<<Self as frame_system::Trait>::Event>;
Expand Down
185 changes: 185 additions & 0 deletions frame/sudo/src/mock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
// Copyright 2020 Parity Technologies (UK) Ltd.
// This file is part of Substrate.

// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

//! Test utilities
use super::*;
use frame_support::{
impl_outer_origin, impl_outer_dispatch, impl_outer_event, parameter_types,
weights::{Weight, DispatchClass}
};
use sp_core::H256;
// The testing primitives are very useful for avoiding having to work with signatures
// or public keys.
use sp_runtime::{Perbill, traits::{BlakeTwo256, IdentityLookup}, testing::Header};
use sp_io;
use crate as sudo;

// Logger module to track execution.
pub mod logger {
use super::*;
use frame_system::ensure_root;

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 Logger {
AccountLog get(fn account_log): Vec<T::AccountId>;
I32Log get(fn i32_log): Vec<i32>;
}
}

decl_event! {
pub enum Event<T> where AccountId = <T as frame_system::Trait>::AccountId {
AppendAccount(AccountId, i32, Weight),
AppendI32(i32, Weight),
}
}

decl_module! {
pub struct Module<T: Trait> for enum Call where origin: <T as system::Trait>::Origin {
fn deposit_event() = default;

#[weight = FunctionOf(
|args: (&i32, &Weight)| *args.1,
DispatchClass::Normal,
Pays::Yes,
)]
fn privileged_i32_log(origin, i: i32, weight: Weight){
// Ensure that the `origin` is `Root`.
ensure_root(origin)?;
<I32Log>::append(i);
Self::deposit_event(RawEvent::AppendI32(i, weight));
}

#[weight = FunctionOf(
|args: (&i32, &Weight)| *args.1,
DispatchClass::Normal,
Pays::Yes,
)]
fn non_privileged_i32_log(origin, i: i32, weight: Weight){
// Ensure that the `origin` is some signed account.
ensure_signed(origin)?;
<I32Log>::append(i);
Self::deposit_event(RawEvent::AppendI32(i, weight));
}

#[weight = FunctionOf(
|args: (&i32, &Weight)| *args.1,
DispatchClass::Normal,
Pays::Yes,
)]
fn non_privileged_account_log(origin, i: i32, weight: Weight) {
// Ensure that the `origin` is some signed account.
let sender = ensure_signed(origin)?;
<AccountLog<T>>::append(sender.clone());
Self::deposit_event(RawEvent::AppendAccount(sender, i, weight));
}
}
}
}

impl_outer_origin! {
pub enum Origin for Test where system = frame_system {}
}

mod test_events {
pub use crate::Event;
}

impl_outer_event! {
pub enum TestEvent for Test {
system<T>,
sudo<T>,
logger<T>,
}
}

impl_outer_dispatch! {
pub enum Call for Test where origin: Origin {
sudo::Sudo,
logger::Logger,
}
}

// For testing the pallet, we construct most of a mock runtime. This means
// first constructing a configuration type (`Test`) which `impl`s each of the
// configuration traits of pallets 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::one();
}

impl frame_system::Trait for Test {
type Origin = Origin;
type Call = 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 = TestEvent;
type BlockHashCount = BlockHashCount;
type MaximumBlockWeight = MaximumBlockWeight;
type DbWeight = ();
type BlockExecutionWeight = ();
type ExtrinsicBaseWeight = ();
type MaximumBlockLength = MaximumBlockLength;
type AvailableBlockRatio = AvailableBlockRatio;
type Version = ();
type ModuleToIndex = ();
type AccountData = ();
type OnNewAccount = ();
type OnKilledAccount = ();
}

// Implement the logger module's `Trait` on the Test runtime.
impl logger::Trait for Test {
type Event = TestEvent;
}

// Implement the sudo module's `Trait` on the Test runtime.
impl Trait for Test {
type Event = TestEvent;
type Call = Call;
}

// Assign back to type variables in order to make dispatched calls of these modules later.
pub type Sudo = Module<Test>;
pub type Logger = logger::Module<Test>;
pub type System = system::Module<Test>;

// New types for dispatchable functions.
pub type SudoCall = sudo::Call<Test>;
pub type LoggerCall = logger::Call<Test>;

// Build test environment by setting the root `key` for the Genesis.
pub fn new_test_ext(root_key: u64) -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::default().build_storage::<Test>().unwrap();
GenesisConfig::<Test>{
key: root_key,
}.assimilate_storage(&mut t).unwrap();
t.into()
}
Loading

0 comments on commit f4b0afc

Please sign in to comment.