Skip to content

Commit

Permalink
test-runtime: Return hashed call as provides in unsigned validation (p…
Browse files Browse the repository at this point in the history
…aritytech#14180)

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.
  • Loading branch information
bkchr authored and nathanwhit committed Jul 19, 2023
1 parent 679ebc0 commit d8dd817
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 60 deletions.
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

0 comments on commit d8dd817

Please sign in to comment.