From c832fd1467aa3330f6e702132a2e101bc93b714e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Sat, 20 May 2023 00:18:37 +0200 Subject: [PATCH] test-runtime: Return hashed call as provides in unsigned validation This is required to make different unsigned extrinsics resolve to different transactions in the tx pool by having `provides` set to theh hash of the call. --- test-utils/runtime/src/lib.rs | 96 ++++++++----------- .../runtime/src/substrate_test_pallet.rs | 12 ++- 2 files changed, 48 insertions(+), 60 deletions(-) diff --git a/test-utils/runtime/src/lib.rs b/test-utils/runtime/src/lib.rs index e1d66e86002e7..c93d5c4e5694b 100644 --- a/test-utils/runtime/src/lib.rs +++ b/test-utils/runtime/src/lib.rs @@ -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, @@ -1103,63 +1106,42 @@ mod tests { fn validate_unsigned_works() { sp_tracing::try_init_simple(); new_test_ext().execute_with(|| { - assert_eq!( - ::validate_unsigned( - TransactionSource::External, - &substrate_test_pallet::Call::bench_call { transfer: Default::default() }, - ), - InvalidTransaction::Call.into(), - ); - - assert_eq!( - ::validate_unsigned( - TransactionSource::External, - &substrate_test_pallet::Call::include_data { data: vec![] }, - ), - InvalidTransaction::Call.into(), - ); - - assert_eq!( - ::validate_unsigned( - TransactionSource::External, - &substrate_test_pallet::Call::fill_block { ratio: Perbill::from_percent(50) }, - ), - InvalidTransaction::Call.into(), - ); - - assert_eq!( - ::validate_unsigned( - TransactionSource::External, - &substrate_test_pallet::Call::deposit_log_digest_item { - log: DigestItem::Other(vec![]) - }, - ), - Ok(Default::default()), - ); - - assert_eq!( - ::validate_unsigned( - TransactionSource::External, - &substrate_test_pallet::Call::storage_change { key: vec![], value: None }, - ), - Ok(Default::default()), - ); - - assert_eq!( - ::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!( + ::validate_unsigned( + TransactionSource::External, + &call, + ), + InvalidTransaction::Call.into(), + ); + } - assert_eq!( - ::validate_unsigned( - TransactionSource::External, - &substrate_test_pallet::Call::read_and_panic { count: 0 }, - ), - Ok(Default::default()), - ); + for call in succeeding_calls { + assert_eq!( + ::validate_unsigned( + TransactionSource::External, + &call, + ), + Ok(ValidTransaction { + provides: vec![BlakeTwo256::hash_of(&call).encode()], + ..Default::default() + }) + ); + } }); } diff --git a/test-utils/runtime/src/substrate_test_pallet.rs b/test-utils/runtime/src/substrate_test_pallet.rs index 40e7af0b43769..93a1a5efceeb9 100644 --- a/test-utils/runtime/src/substrate_test_pallet.rs +++ b/test-utils/runtime/src/substrate_test_pallet.rs @@ -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::*; @@ -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)), } }