Emit error when construct_runtime imports a non-existent pallet part (#8949)

* Emit error when construct_runtime imports a non-existent Call part

* Reword and display pallet name when emitting part not found error

* Migrate decl_outer_dispatch to a proc macro

* Rename calls.rs to call.rs

* Create new construct_runtime_v2 macro

* Add UI test for importing non-existent call part in construct_runtime

* Emit error when construct_runtime imports a non-existent Config part

* Emit error when construct_runtime imports a non-existent Event part

* Migrate decl_outer_inherent to a proc macro

* Emit error when construct_runtime imports a non-existent Inherent part

* Migrate decl_outer_validate_unsigned to a proc macro

* Emit error when construct_runtime imports a non-existent ValidateUnsigned part

* impl for old macro

* fix line width

* add doc

* hide macroes and use unique counter everywhere

* Remove construct_runtime_v2

* Encapsulate pallet part check macros in a module

* Fix macro definitions in dummy part checker

* Tag ProvideInherent impl with #[pallet::inherent] properly for authorship pallet

* Remove Call part from pallets that do not define it

* Add Call part unit tests

* Remove undefined Call part import from offences pallet

* Add tests for expand_outer_inherent

* Remove Call part from pallets that do not define them

* Remove Call part imports from pallets that do not have it defined

* Remove Call part import of the offences pallet from grandpa pallet mocks

* Update frame/support/test/tests/pallet.rs

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>

* Remove Call part imports for pallets that do not define them

* Move inherent tests to inherent_expand

* Add unit tests for expand_outer_validate_unsigned

* Add newline at the end of file

* fix ui test

* Small prayer to RNGsus for fixing CI

* Remove Call part from construct_runtime for randomness collective flip pallet

* Remove Call part import for randomness collective flip pallet

* Summon Laplace's demon instead of praying to RNGsus

* Update test expectations

* fix ui test and make sure it's flaky

* Revert "fix ui test and make sure it's flaky"

This reverts commit 362b6881389c911ef8d9ef85d71c9463f5694b20.

* Comment out test instead of putting it in conditional compilation

* Update UI test expectations

* Update UI test expectations

* Emit error when construct_runtime imports a non-existent Origin part

Co-authored-by: thiolliere <gui.thiolliere@gmail.com>
Co-authored-by: Denis P <denis.pisarev@parity.io>
This commit is contained in:
Keith Yeung
2021-06-15 20:44:22 -07:00
committed by GitHub
parent 7dd38e3aec
commit 58e837fcd3
47 changed files with 1934 additions and 197 deletions
+175 -5
View File
@@ -304,10 +304,13 @@ pub mod pallet {
type Call = Call<T>;
fn validate_unsigned(
_source: TransactionSource,
_call: &Self::Call
call: &Self::Call
) -> TransactionValidity {
T::AccountId::from(SomeType1); // Test for where clause
T::AccountId::from(SomeType5); // Test for where clause
if matches!(call, Call::foo_transactional(_)) {
return Ok(ValidTransaction::default());
}
Err(TransactionValidityError::Invalid(InvalidTransaction::Call))
}
}
@@ -324,22 +327,40 @@ pub mod pallet {
fn create_inherent(_data: &InherentData) -> Option<Self::Call> {
T::AccountId::from(SomeType1); // Test for where clause
T::AccountId::from(SomeType6); // Test for where clause
unimplemented!();
Some(Call::foo_no_post_info())
}
fn is_inherent(_call: &Self::Call) -> bool {
unimplemented!();
fn is_inherent(call: &Self::Call) -> bool {
matches!(call, Call::foo_no_post_info() | Call::foo(..))
}
fn check_inherent(call: &Self::Call, _: &InherentData) -> Result<(), Self::Error> {
match call {
Call::foo_no_post_info() => Ok(()),
Call::foo(0, 0) => Err(InherentError::Fatal),
Call::foo(..) => Ok(()),
_ => unreachable!("other calls are not inherents"),
}
}
fn is_inherent_required(d: &InherentData) -> Result<Option<Self::Error>, Self::Error> {
match d.get_data::<bool>(b"required") {
Ok(Some(true)) => Ok(Some(InherentError::Fatal)),
Ok(Some(false)) | Ok(None) => Ok(None),
Err(_) => unreachable!("should not happen in tests"),
}
}
}
#[derive(codec::Encode, sp_runtime::RuntimeDebug)]
#[cfg_attr(feature = "std", derive(codec::Decode))]
pub enum InherentError {
Fatal,
}
impl frame_support::inherent::IsFatalError for InherentError {
fn is_fatal_error(&self) -> bool {
unimplemented!();
matches!(self, InherentError::Fatal)
}
}
@@ -538,6 +559,155 @@ fn instance_expand() {
let _: pallet::__InherentHiddenInstance = ();
}
#[test]
fn inherent_expand() {
use frame_support::{
inherent::{BlockT, InherentData},
traits::EnsureInherentsAreFirst,
};
use sp_core::Hasher;
use sp_runtime::{traits::{BlakeTwo256, Header}, Digest};
let inherents = InherentData::new().create_extrinsics();
let expected = vec![
UncheckedExtrinsic { function: Call::Example(pallet::Call::foo_no_post_info()), signature: None },
];
assert_eq!(expected, inherents);
let block = Block::new(
Header::new(
1,
BlakeTwo256::hash(b"test"),
BlakeTwo256::hash(b"test"),
BlakeTwo256::hash(b"test"),
Digest::default(),
),
vec![
UncheckedExtrinsic { function: Call::Example(pallet::Call::foo_no_post_info()), signature: None },
UncheckedExtrinsic { function: Call::Example(pallet::Call::foo(1, 0)), signature: None },
],
);
assert!(InherentData::new().check_extrinsics(&block).ok());
let block = Block::new(
Header::new(
1,
BlakeTwo256::hash(b"test"),
BlakeTwo256::hash(b"test"),
BlakeTwo256::hash(b"test"),
Digest::default(),
),
vec![
UncheckedExtrinsic { function: Call::Example(pallet::Call::foo_no_post_info()), signature: None },
UncheckedExtrinsic { function: Call::Example(pallet::Call::foo(0, 0)), signature: None },
],
);
assert!(InherentData::new().check_extrinsics(&block).fatal_error());
let block = Block::new(
Header::new(
1,
BlakeTwo256::hash(b"test"),
BlakeTwo256::hash(b"test"),
BlakeTwo256::hash(b"test"),
Digest::default(),
),
vec![
UncheckedExtrinsic { function: Call::Example(pallet::Call::foo_transactional(0)), signature: None },
],
);
let mut inherent = InherentData::new();
inherent.put_data(*b"required", &true).unwrap();
assert!(inherent.check_extrinsics(&block).fatal_error());
let block = Block::new(
Header::new(
1,
BlakeTwo256::hash(b"test"),
BlakeTwo256::hash(b"test"),
BlakeTwo256::hash(b"test"),
Digest::default(),
),
vec![
UncheckedExtrinsic { function: Call::Example(pallet::Call::foo_no_post_info()), signature: Some((1, (), ())) },
],
);
let mut inherent = InherentData::new();
inherent.put_data(*b"required", &true).unwrap();
assert!(inherent.check_extrinsics(&block).fatal_error());
let block = Block::new(
Header::new(
1,
BlakeTwo256::hash(b"test"),
BlakeTwo256::hash(b"test"),
BlakeTwo256::hash(b"test"),
Digest::default(),
),
vec![
UncheckedExtrinsic { function: Call::Example(pallet::Call::foo(1, 1)), signature: None },
UncheckedExtrinsic { function: Call::Example(pallet::Call::foo_transactional(0)), signature: None },
],
);
assert!(Runtime::ensure_inherents_are_first(&block).is_ok());
let block = Block::new(
Header::new(
1,
BlakeTwo256::hash(b"test"),
BlakeTwo256::hash(b"test"),
BlakeTwo256::hash(b"test"),
Digest::default(),
),
vec![
UncheckedExtrinsic { function: Call::Example(pallet::Call::foo(1, 1)), signature: None },
UncheckedExtrinsic { function: Call::Example(pallet::Call::foo_transactional(0)), signature: None },
UncheckedExtrinsic { function: Call::Example(pallet::Call::foo_no_post_info()), signature: None },
],
);
assert_eq!(Runtime::ensure_inherents_are_first(&block).err().unwrap(), 2);
let block = Block::new(
Header::new(
1,
BlakeTwo256::hash(b"test"),
BlakeTwo256::hash(b"test"),
BlakeTwo256::hash(b"test"),
Digest::default(),
),
vec![
UncheckedExtrinsic { function: Call::Example(pallet::Call::foo(1, 1)), signature: None },
UncheckedExtrinsic { function: Call::Example(pallet::Call::foo(1, 0)), signature: Some((1, (), ())) },
UncheckedExtrinsic { function: Call::Example(pallet::Call::foo_no_post_info()), signature: None },
],
);
assert_eq!(Runtime::ensure_inherents_are_first(&block).err().unwrap(), 2);
}
#[test]
fn validate_unsigned_expand() {
use frame_support::pallet_prelude::{
InvalidTransaction, TransactionSource, TransactionValidityError, ValidTransaction, ValidateUnsigned,
};
let call = pallet::Call::<Runtime>::foo_no_post_info();
let validity = pallet::Pallet::validate_unsigned(TransactionSource::Local, &call).unwrap_err();
assert_eq!(validity, TransactionValidityError::Invalid(InvalidTransaction::Call));
let call = pallet::Call::<Runtime>::foo_transactional(0);
let validity = pallet::Pallet::validate_unsigned(TransactionSource::External, &call).unwrap();
assert_eq!(validity, ValidTransaction::default());
}
#[test]
fn trait_store_expand() {
TestExternalities::default().execute_with(|| {