From 69ab3625f95786a1eb8f02da69c224823fc1a987 Mon Sep 17 00:00:00 2001 From: "Robert G. Jakabosky" Date: Thu, 7 Apr 2022 22:50:00 +0800 Subject: [PATCH 1/4] Use Circleci rust image to fix build issue. --- .circleci/config.yml | 14 +++++++------- scripts/coverage.sh | 5 +++-- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 630211077a..043e4074e2 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -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 @@ -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 diff --git a/scripts/coverage.sh b/scripts/coverage.sh index 9f588c6b04..bc9b599ff7 100755 --- a/scripts/coverage.sh +++ b/scripts/coverage.sh @@ -1,13 +1,14 @@ #!/bin/bash function run_tests() { + # TODO: fix build of sudo tests. + # --package pallet-sudo \ RUSTFLAGS="-Zinstrument-coverage -Clink-dead-code" \ LLVM_PROFILE_FILE="json5format-%m.profraw" \ - BUILD_DUMMY_WASM_BINARY=1 \ + SKIP_WASM_BUILD=1 RUST_BACKTRACE=1 \ cargo test --tests \ --package pallet-staking \ --package pallet-group \ - --package pallet-sudo \ --package polymesh-primitives \ --package node-rpc-runtime-api \ --package pallet-transaction-payment \ From 338200810f98af731a879d7762de6011b9c0d75b Mon Sep 17 00:00:00 2001 From: "Robert G. Jakabosky" Date: Fri, 8 Apr 2022 20:56:43 +0800 Subject: [PATCH 2/4] Fix sudo tests. --- pallets/sudo/src/mock.rs | 96 +++++++++++++-------------------------- pallets/sudo/src/tests.rs | 74 ++++++++++++++++++++++-------- scripts/coverage.sh | 3 +- 3 files changed, 86 insertions(+), 87 deletions(-) diff --git a/pallets/sudo/src/mock.rs b/pallets/sudo/src/mock.rs index 40c95ad6a5..eb8b39c2d2 100644 --- a/pallets/sudo/src/mock.rs +++ b/pallets/sudo/src/mock.rs @@ -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::{ @@ -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> + Into<::Event>; } decl_storage! { - trait Store for Module as Logger { + trait Store for Module as Logger { AccountLog get(fn account_log): Vec; I32Log get(fn i32_log): Vec; } @@ -55,7 +51,7 @@ pub mod logger { } decl_module! { - pub struct Module for enum Call where origin: ::Origin { + pub struct Module for enum Call where origin: ::Origin { fn deposit_event() = default; #[weight = *weight] @@ -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, - sudo, - logger, - } -} - -impl_outer_dispatch! { - pub enum Call for Test where origin: Origin { - sudo::Sudo, - logger::Logger, +type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; +type Block = frame_system::mocking::MockBlock; + +frame_support::construct_runtime!( + pub enum Test where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Pallet, Call, Config, Storage, Event}, + Sudo: sudo::{Pallet, Call, Config, Storage, Event}, + Logger: logger::{Pallet, Call, Storage, Event}, } -} - -// 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; @@ -114,15 +96,10 @@ parameter_types! { pub const AvailableBlockRatio: Perbill = Perbill::one(); } -pub struct BlockEverything; -impl Filter 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; @@ -132,38 +109,27 @@ impl frame_system::Config for Test { type AccountId = u64; type Lookup = IdentityLookup; 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; -pub type Logger = logger::Module; -pub type System = frame_system::Module; +impl logger::Config for Test { + type Event = Event; +} // New types for dispatchable functions. pub type SudoCall = sudo::Call; @@ -174,7 +140,7 @@ pub fn new_test_ext(root_key: u64) -> sp_io::TestExternalities { let mut t = frame_system::GenesisConfig::default() .build_storage::() .unwrap(); - GenesisConfig:: { key: root_key } + sudo::GenesisConfig:: { key: root_key } .assimilate_storage(&mut t) .unwrap(); t.into() diff --git a/pallets/sudo/src/tests.rs b/pallets/sudo/src/tests.rs index 451bce4b01..108b85b713 100644 --- a/pallets/sudo/src/tests.rs +++ b/pallets/sudo/src/tests.rs @@ -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() { @@ -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::::RequireSudo @@ -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)); }) } @@ -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::::RequireSudo, @@ -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); }); @@ -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)); }) } @@ -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)); }); } @@ -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::::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`. @@ -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)); }); } diff --git a/scripts/coverage.sh b/scripts/coverage.sh index bc9b599ff7..285327de2b 100755 --- a/scripts/coverage.sh +++ b/scripts/coverage.sh @@ -1,14 +1,13 @@ #!/bin/bash function run_tests() { - # TODO: fix build of sudo tests. - # --package pallet-sudo \ RUSTFLAGS="-Zinstrument-coverage -Clink-dead-code" \ LLVM_PROFILE_FILE="json5format-%m.profraw" \ SKIP_WASM_BUILD=1 RUST_BACKTRACE=1 \ cargo test --tests \ --package pallet-staking \ --package pallet-group \ + --package pallet-sudo \ --package polymesh-primitives \ --package node-rpc-runtime-api \ --package pallet-transaction-payment \ From 564fe5e26536464429d99392d736ca7b2efed89c Mon Sep 17 00:00:00 2001 From: "Robert G. Jakabosky" Date: Fri, 8 Apr 2022 21:20:00 +0800 Subject: [PATCH 3/4] Use integration mode. --- scripts/coverage.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/coverage.sh b/scripts/coverage.sh index 285327de2b..e82066eeed 100755 --- a/scripts/coverage.sh +++ b/scripts/coverage.sh @@ -4,6 +4,7 @@ function run_tests() { RUSTFLAGS="-Zinstrument-coverage -Clink-dead-code" \ LLVM_PROFILE_FILE="json5format-%m.profraw" \ SKIP_WASM_BUILD=1 RUST_BACKTRACE=1 \ + INTEGRATION_TEST=1 \ cargo test --tests \ --package pallet-staking \ --package pallet-group \ From ef0e79341bd48386f0a76dc19617d32141f96e32 Mon Sep 17 00:00:00 2001 From: "Robert G. Jakabosky" Date: Fri, 8 Apr 2022 23:55:32 +0800 Subject: [PATCH 4/4] Enable more tests for coverage. --- scripts/coverage.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scripts/coverage.sh b/scripts/coverage.sh index e82066eeed..533cb184a6 100755 --- a/scripts/coverage.sh +++ b/scripts/coverage.sh @@ -9,11 +9,13 @@ function run_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 $* }