Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix coverage #1230

Merged
merged 4 commits into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -161,19 +161,21 @@ jobs:
- "./target"
coverage:
docker:
- image: polymathnet/rust:debian-nightly-2021-12-14
- image: cimg/rust:1.60.0
resource_class: large
environment:
- VERBOSE: "1"
- RUSTC_WORKSPACE_WRAPPER: ./scripts/rustc.sh
steps:
- run:
name: Install libclang
command: sudo apt-get update && sudo apt-get install libclang-dev
- checkout
- run:
name: Store rust version in a file for cache key
command: rustc --version > rust.version
command: rustc --version | tee rust.version
- restore_cache:
keys:
- v5-coverage-cache-{{ checksum "./rust.version" }}-{{ checksum "./Cargo.lock" }}
- v6-coverage-cache-{{ checksum "./rust.version" }}-{{ checksum "./Cargo.lock" }}
- run:
name: Install dependencies
command: rustup component add llvm-tools-preview && cargo install rustfilt cargo-binutils
Expand All @@ -182,11 +184,9 @@ jobs:
command: bash ./scripts/coverage.sh
no_output_timeout: 1h
- save_cache:
key: v5-coverage-cache-{{ checksum "./rust.version" }}-{{ checksum "./Cargo.lock" }}
key: v6-coverage-cache-{{ checksum "./rust.version" }}-{{ checksum "./Cargo.lock" }}
paths:
- "/usr/local/cargo"
- "~/.cargo"
- "./target"
cli:
docker:
- image: polymathnet/rust:debian-nightly-2021-12-14
Expand Down
96 changes: 31 additions & 65 deletions pallets/sudo/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@

//! Test utilities

use super::*;
use crate as sudo;
use frame_support::traits::Filter;
use frame_support::{
impl_outer_dispatch, impl_outer_event, impl_outer_origin, parameter_types, weights::Weight,
};
use frame_support::{parameter_types, weights::Weight};
use sp_core::H256;
use sp_io;
use sp_runtime::{
Expand All @@ -33,15 +29,15 @@ use sp_runtime::{

// Logger module to track execution.
pub mod logger {
use super::*;
use frame_system::ensure_root;
use frame_support::{decl_event, decl_module, decl_storage, weights::Weight};
use frame_system::{ensure_root, ensure_signed};

pub trait Trait: frame_system::Config {
pub trait Config: frame_system::Config {
type Event: From<Event<Self>> + Into<<Self as frame_system::Config>::Event>;
}

decl_storage! {
trait Store for Module<T: Trait> as Logger {
trait Store for Module<T: Config> as Logger {
AccountLog get(fn account_log): Vec<T::AccountId>;
I32Log get(fn i32_log): Vec<i32>;
}
Expand All @@ -55,7 +51,7 @@ pub mod logger {
}

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

#[weight = *weight]
Expand All @@ -78,34 +74,20 @@ pub mod logger {
}
}

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 {
frame_system<T>,
sudo<T>,
logger<T>,
}
}

impl_outer_dispatch! {
pub enum Call for Test where origin: Origin {
sudo::Sudo,
logger::Logger,
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;

frame_support::construct_runtime!(
pub enum Test where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
Sudo: sudo::{Pallet, Call, Config<T>, Storage, Event<T>},
Logger: logger::{Pallet, Call, Storage, Event<T>},
}
}

// 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;
Expand All @@ -114,15 +96,10 @@ parameter_types! {
pub const AvailableBlockRatio: Perbill = Perbill::one();
}

pub struct BlockEverything;
impl Filter<Call> for BlockEverything {
fn filter(_: &Call) -> bool {
false
}
}

impl frame_system::Config for Test {
type BaseCallFilter = BlockEverything;
type BaseCallFilter = frame_support::traits::Everything;
type BlockWeights = ();
type BlockLength = ();
type Origin = Origin;
type Call = Call;
type Index = u64;
Expand All @@ -132,38 +109,27 @@ impl frame_system::Config for Test {
type AccountId = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Event = TestEvent;
type Event = Event;
type BlockHashCount = BlockHashCount;
type MaximumBlockWeight = MaximumBlockWeight;
type DbWeight = ();
type BlockExecutionWeight = ();
type ExtrinsicBaseWeight = ();
type MaximumExtrinsicWeight = MaximumBlockWeight;
type MaximumBlockLength = MaximumBlockLength;
type AvailableBlockRatio = AvailableBlockRatio;
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = ();
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type OnSetCode = ();
type SS58Prefix = ();
}

// 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;
impl sudo::Config for Test {
type Event = Event;
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 = frame_system::Module<Test>;
impl logger::Config for Test {
type Event = Event;
}

// New types for dispatchable functions.
pub type SudoCall = sudo::Call<Test>;
Expand All @@ -174,7 +140,7 @@ 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 }
sudo::GenesisConfig::<Test> { key: root_key }
.assimilate_storage(&mut t)
.unwrap();
t.into()
Expand Down
74 changes: 54 additions & 20 deletions pallets/sudo/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@

use super::*;
use frame_support::{assert_noop, assert_ok};
use mock::{
new_test_ext, Call, Logger, LoggerCall, Origin, Sudo, SudoCall, System, Test, TestEvent,
};
use mock::{new_test_ext, Call, Event, Logger, LoggerCall, Origin, Sudo, SudoCall, System, Test};

#[test]
fn test_setup_works() {
Expand All @@ -38,12 +36,18 @@ fn sudo_basics() {
// Configure a default test environment and set the root `key` to 1.
new_test_ext(1).execute_with(|| {
// A privileged function should work when `sudo` is passed the root `key` as `origin`.
let call = Box::new(Call::Logger(LoggerCall::privileged_i32_log(42, 1_000)));
let call = Box::new(Call::Logger(LoggerCall::privileged_i32_log {
i: 42,
weight: 1_000,
}));
assert_ok!(Sudo::sudo(Origin::signed(1), call));
assert_eq!(Logger::i32_log(), vec![42i32]);

// A privileged function should not work when `sudo` is passed a non-root `key` as `origin`.
let call = Box::new(Call::Logger(LoggerCall::privileged_i32_log(42, 1_000)));
let call = Box::new(Call::Logger(LoggerCall::privileged_i32_log {
i: 42,
weight: 1_000,
}));
assert_noop!(
Sudo::sudo(Origin::signed(2), call),
Error::<Test>::RequireSudo
Expand All @@ -58,9 +62,12 @@ fn sudo_emits_events_correctly() {
System::set_block_number(1);

// Should emit event to indicate success when called with the root `key` and `call` is `Ok`.
let call = Box::new(Call::Logger(LoggerCall::privileged_i32_log(42, 1)));
let call = Box::new(Call::Logger(LoggerCall::privileged_i32_log {
i: 42,
weight: 1,
}));
assert_ok!(Sudo::sudo(Origin::signed(1), call));
let expected_event = TestEvent::sudo(RawEvent::Sudid(Ok(())));
let expected_event = Event::Sudo(RawEvent::Sudid(Ok(())));
assert!(System::events().iter().any(|a| a.event == expected_event));
})
}
Expand All @@ -69,12 +76,18 @@ fn sudo_emits_events_correctly() {
fn sudo_unchecked_weight_basics() {
new_test_ext(1).execute_with(|| {
// A privileged function should work when `sudo` is passed the root `key` as origin.
let call = Box::new(Call::Logger(LoggerCall::privileged_i32_log(42, 1_000)));
let call = Box::new(Call::Logger(LoggerCall::privileged_i32_log {
i: 42,
weight: 1_000,
}));
assert_ok!(Sudo::sudo_unchecked_weight(Origin::signed(1), call, 1_000));
assert_eq!(Logger::i32_log(), vec![42i32]);

// A privileged function should not work when called with a non-root `key`.
let call = Box::new(Call::Logger(LoggerCall::privileged_i32_log(42, 1_000)));
let call = Box::new(Call::Logger(LoggerCall::privileged_i32_log {
i: 42,
weight: 1_000,
}));
assert_noop!(
Sudo::sudo_unchecked_weight(Origin::signed(2), call, 1_000),
Error::<Test>::RequireSudo,
Expand All @@ -83,8 +96,14 @@ fn sudo_unchecked_weight_basics() {
assert_eq!(Logger::i32_log(), vec![42i32]);

// Controls the dispatched weight.
let call = Box::new(Call::Logger(LoggerCall::privileged_i32_log(42, 1)));
let sudo_unchecked_weight_call = SudoCall::sudo_unchecked_weight(call, 1_000);
let call = Box::new(Call::Logger(LoggerCall::privileged_i32_log {
i: 42,
weight: 1,
}));
let sudo_unchecked_weight_call = SudoCall::sudo_unchecked_weight {
call,
_weight: 1_000,
};
let info = sudo_unchecked_weight_call.get_dispatch_info();
assert_eq!(info.weight, 1_000);
});
Expand All @@ -97,9 +116,12 @@ fn sudo_unchecked_weight_emits_events_correctly() {
System::set_block_number(1);

// Should emit event to indicate success when called with the root `key` and `call` is `Ok`.
let call = Box::new(Call::Logger(LoggerCall::privileged_i32_log(42, 1)));
let call = Box::new(Call::Logger(LoggerCall::privileged_i32_log {
i: 42,
weight: 1,
}));
assert_ok!(Sudo::sudo_unchecked_weight(Origin::signed(1), call, 1_000));
let expected_event = TestEvent::sudo(RawEvent::Sudid(Ok(())));
let expected_event = Event::Sudo(RawEvent::Sudid(Ok(())));
assert!(System::events().iter().any(|a| a.event == expected_event));
})
}
Expand Down Expand Up @@ -129,11 +151,11 @@ fn set_key_emits_events_correctly() {

// A root `key` can change the root `key`.
assert_ok!(Sudo::set_key(Origin::signed(1), 2));
let expected_event = TestEvent::sudo(RawEvent::KeyChanged(1));
let expected_event = Event::Sudo(RawEvent::KeyChanged(1));
assert!(System::events().iter().any(|a| a.event == expected_event));
// Double check.
assert_ok!(Sudo::set_key(Origin::signed(2), 4));
let expected_event = TestEvent::sudo(RawEvent::KeyChanged(2));
let expected_event = Event::Sudo(RawEvent::KeyChanged(2));
assert!(System::events().iter().any(|a| a.event == expected_event));
});
}
Expand All @@ -142,20 +164,29 @@ fn set_key_emits_events_correctly() {
fn sudo_as_basics() {
new_test_ext(1).execute_with(|| {
// A privileged function will not work when passed to `sudo_as`.
let call = Box::new(Call::Logger(LoggerCall::privileged_i32_log(42, 1_000)));
let call = Box::new(Call::Logger(LoggerCall::privileged_i32_log {
i: 42,
weight: 1_000,
}));
assert_ok!(Sudo::sudo_as(Origin::signed(1), 2, call));
assert!(Logger::i32_log().is_empty());
assert!(Logger::account_log().is_empty());

// A non-privileged function should not work when called with a non-root `key`.
let call = Box::new(Call::Logger(LoggerCall::non_privileged_log(42, 1)));
let call = Box::new(Call::Logger(LoggerCall::non_privileged_log {
i: 42,
weight: 1,
}));
assert_noop!(
Sudo::sudo_as(Origin::signed(3), 2, call),
Error::<Test>::RequireSudo
);

// A non-privileged function will work when passed to `sudo_as` with the root `key`.
let call = Box::new(Call::Logger(LoggerCall::non_privileged_log(42, 1)));
let call = Box::new(Call::Logger(LoggerCall::non_privileged_log {
i: 42,
weight: 1,
}));
assert_ok!(Sudo::sudo_as(Origin::signed(1), 2, call));
assert_eq!(Logger::i32_log(), vec![42i32]);
// The correct user makes the call within `sudo_as`.
Expand All @@ -170,9 +201,12 @@ fn sudo_as_emits_events_correctly() {
System::set_block_number(1);

// A non-privileged function will work when passed to `sudo_as` with the root `key`.
let call = Box::new(Call::Logger(LoggerCall::non_privileged_log(42, 1)));
let call = Box::new(Call::Logger(LoggerCall::non_privileged_log {
i: 42,
weight: 1,
}));
assert_ok!(Sudo::sudo_as(Origin::signed(1), 2, call));
let expected_event = TestEvent::sudo(RawEvent::SudoAsDone(Ok(())));
let expected_event = Event::Sudo(RawEvent::SudoAsDone(Ok(())));
assert!(System::events().iter().any(|a| a.event == expected_event));
});
}
5 changes: 4 additions & 1 deletion scripts/coverage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
function run_tests() {
RUSTFLAGS="-Zinstrument-coverage -Clink-dead-code" \
LLVM_PROFILE_FILE="json5format-%m.profraw" \
BUILD_DUMMY_WASM_BINARY=1 \
SKIP_WASM_BUILD=1 RUST_BACKTRACE=1 \
INTEGRATION_TEST=1 \
cargo test --tests \
--package pallet-staking \
--package pallet-group \
--package pallet-sudo \
--package pallet-pips \
--package polymesh-primitives \
--package node-rpc-runtime-api \
--package pallet-transaction-payment \
--package polymesh-runtime-tests \
--package pallet-balances:0.1.0 \
--package asset-metadata \
--features default_identity $*
}

Expand Down