Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

test-runtime: Return hashed call as provides in unsigned validation #14180

Merged
merged 1 commit into from
May 22, 2023
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
96 changes: 39 additions & 57 deletions test-utils/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,10 @@ mod tests {
use sp_consensus::BlockOrigin;
use sp_core::{storage::well_known_keys::HEAP_PAGES, ExecutionContext};
use sp_keyring::AccountKeyring;
use sp_runtime::{traits::SignedExtension, transaction_validity::InvalidTransaction};
use sp_runtime::{
traits::{Hash as _, SignedExtension},
transaction_validity::{InvalidTransaction, ValidTransaction},
};
use sp_state_machine::ExecutionStrategy;
use substrate_test_runtime_client::{
prelude::*, runtime::TestAPI, DefaultTestClientBuilderExt, TestClientBuilder,
Expand Down Expand Up @@ -1103,63 +1106,42 @@ mod tests {
fn validate_unsigned_works() {
sp_tracing::try_init_simple();
new_test_ext().execute_with(|| {
assert_eq!(
<SubstrateTest as sp_runtime::traits::ValidateUnsigned>::validate_unsigned(
TransactionSource::External,
&substrate_test_pallet::Call::bench_call { transfer: Default::default() },
),
InvalidTransaction::Call.into(),
);

assert_eq!(
<SubstrateTest as sp_runtime::traits::ValidateUnsigned>::validate_unsigned(
TransactionSource::External,
&substrate_test_pallet::Call::include_data { data: vec![] },
),
InvalidTransaction::Call.into(),
);

assert_eq!(
<SubstrateTest as sp_runtime::traits::ValidateUnsigned>::validate_unsigned(
TransactionSource::External,
&substrate_test_pallet::Call::fill_block { ratio: Perbill::from_percent(50) },
),
InvalidTransaction::Call.into(),
);

assert_eq!(
<SubstrateTest as sp_runtime::traits::ValidateUnsigned>::validate_unsigned(
TransactionSource::External,
&substrate_test_pallet::Call::deposit_log_digest_item {
log: DigestItem::Other(vec![])
},
),
Ok(Default::default()),
);

assert_eq!(
<SubstrateTest as sp_runtime::traits::ValidateUnsigned>::validate_unsigned(
TransactionSource::External,
&substrate_test_pallet::Call::storage_change { key: vec![], value: None },
),
Ok(Default::default()),
);

assert_eq!(
<SubstrateTest as sp_runtime::traits::ValidateUnsigned>::validate_unsigned(
TransactionSource::External,
&substrate_test_pallet::Call::read { count: 0 },
),
Ok(Default::default()),
);
let failing_calls = vec![
substrate_test_pallet::Call::bench_call { transfer: Default::default() },
substrate_test_pallet::Call::include_data { data: vec![] },
substrate_test_pallet::Call::fill_block { ratio: Perbill::from_percent(50) },
];
let succeeding_calls = vec![
substrate_test_pallet::Call::deposit_log_digest_item {
log: DigestItem::Other(vec![]),
},
substrate_test_pallet::Call::storage_change { key: vec![], value: None },
substrate_test_pallet::Call::read { count: 0 },
substrate_test_pallet::Call::read_and_panic { count: 0 },
];

for call in failing_calls {
assert_eq!(
<SubstrateTest as sp_runtime::traits::ValidateUnsigned>::validate_unsigned(
TransactionSource::External,
&call,
),
InvalidTransaction::Call.into(),
);
}

assert_eq!(
<SubstrateTest as sp_runtime::traits::ValidateUnsigned>::validate_unsigned(
TransactionSource::External,
&substrate_test_pallet::Call::read_and_panic { count: 0 },
),
Ok(Default::default()),
);
for call in succeeding_calls {
assert_eq!(
<SubstrateTest as sp_runtime::traits::ValidateUnsigned>::validate_unsigned(
TransactionSource::External,
&call,
),
Ok(ValidTransaction {
provides: vec![BlakeTwo256::hash_of(&call).encode()],
..Default::default()
})
);
}
});
}

Expand Down
12 changes: 9 additions & 3 deletions test-utils/runtime/src/substrate_test_pallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@

use frame_support::{pallet_prelude::*, storage};
use sp_core::sr25519::Public;
use sp_runtime::transaction_validity::{
InvalidTransaction, TransactionSource, TransactionValidity, ValidTransaction,
use sp_runtime::{
traits::{BlakeTwo256, Hash},
transaction_validity::{
InvalidTransaction, TransactionSource, TransactionValidity, ValidTransaction,
},
};
use sp_std::prelude::*;

Expand Down Expand Up @@ -225,7 +228,10 @@ pub mod pallet {
Call::deposit_log_digest_item { .. } |
Call::storage_change { .. } |
Call::read { .. } |
Call::read_and_panic { .. } => Ok(Default::default()),
Call::read_and_panic { .. } => Ok(ValidTransaction {
provides: vec![BlakeTwo256::hash_of(&call).encode()],
..Default::default()
}),
_ => Err(TransactionValidityError::Invalid(InvalidTransaction::Call)),
}
}
Expand Down