Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Tx ext stage 2: 3/4] use #[pallet::authorize(...)] to migrate unsigned in system: tasks + apply_authorized_call #6325

Draft
wants to merge 7 commits into
base: gui-tx-ext-stage-2-part-2
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,7 @@ impl<T: frame_system::Config> frame_system::WeightInfo for WeightInfo<T> {
.saturating_add(T::DbWeight::get().reads(2))
.saturating_add(T::DbWeight::get().writes(3))
}
fn validate_apply_authorized_upgrade() -> Weight {
Weight::zero()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,7 @@ impl<T: frame_system::Config> frame_system::WeightInfo for WeightInfo<T> {
.saturating_add(T::DbWeight::get().reads(2))
.saturating_add(T::DbWeight::get().writes(3))
}
fn validate_apply_authorized_upgrade() -> Weight {
Weight::zero()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,7 @@ impl<T: frame_system::Config> frame_system::WeightInfo for WeightInfo<T> {
.saturating_add(T::DbWeight::get().reads(2))
.saturating_add(T::DbWeight::get().writes(3))
}
fn validate_apply_authorized_upgrade() -> Weight {
Weight::zero()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,7 @@ impl<T: frame_system::Config> frame_system::WeightInfo for WeightInfo<T> {
.saturating_add(T::DbWeight::get().reads(2))
.saturating_add(T::DbWeight::get().writes(3))
}
fn validate_apply_authorized_upgrade() -> Weight {
Weight::zero()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,7 @@ impl<T: frame_system::Config> frame_system::WeightInfo for WeightInfo<T> {
.saturating_add(T::DbWeight::get().reads(2))
.saturating_add(T::DbWeight::get().writes(3))
}
fn validate_apply_authorized_upgrade() -> Weight {
Weight::zero()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,7 @@ impl<T: frame_system::Config> frame_system::WeightInfo for WeightInfo<T> {
.saturating_add(T::DbWeight::get().reads(5))
.saturating_add(T::DbWeight::get().writes(4))
}
fn validate_apply_authorized_upgrade() -> Weight {
Weight::zero()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,7 @@ impl<T: frame_system::Config> frame_system::WeightInfo for WeightInfo<T> {
.saturating_add(T::DbWeight::get().reads(5))
.saturating_add(T::DbWeight::get().writes(4))
}
fn validate_apply_authorized_upgrade() -> Weight {
Weight::zero()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,7 @@ impl<T: frame_system::Config> frame_system::WeightInfo for WeightInfo<T> {
.saturating_add(T::DbWeight::get().reads(2))
.saturating_add(T::DbWeight::get().writes(3))
}
fn validate_apply_authorized_upgrade() -> Weight {
Weight::zero()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,7 @@ impl<T: frame_system::Config> frame_system::WeightInfo for WeightInfo<T> {
.saturating_add(T::DbWeight::get().reads(2))
.saturating_add(T::DbWeight::get().writes(3))
}
fn validate_apply_authorized_upgrade() -> Weight {
Weight::zero()
}
}
3 changes: 3 additions & 0 deletions polkadot/runtime/rococo/src/weights/frame_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,7 @@ impl<T: frame_system::Config> frame_system::WeightInfo for WeightInfo<T> {
.saturating_add(T::DbWeight::get().reads(2))
.saturating_add(T::DbWeight::get().writes(3))
}
fn validate_apply_authorized_upgrade() -> Weight {
Weight::zero()
}
}
3 changes: 3 additions & 0 deletions polkadot/runtime/westend/src/weights/frame_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,7 @@ impl<T: frame_system::Config> frame_system::WeightInfo for WeightInfo<T> {
.saturating_add(T::DbWeight::get().reads(2))
.saturating_add(T::DbWeight::get().writes(3))
}
fn validate_apply_authorized_upgrade() -> Weight {
Weight::zero()
}
}
8 changes: 5 additions & 3 deletions substrate/frame/examples/tasks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#![cfg_attr(not(feature = "std"), no_std)]

use frame_support::dispatch::DispatchResult;
use frame_system::offchain::CreateInherent;
use frame_system::offchain::CreateAuthorizedTransaction;
#[cfg(feature = "experimental")]
use frame_system::offchain::SubmitTransaction;
// Re-export pallet items so that they can be accessed from the crate namespace.
Expand Down Expand Up @@ -77,7 +77,7 @@ pub mod pallet {
let call = frame_system::Call::<T>::do_task { task: runtime_task.into() };

// Submit the task as an unsigned transaction
let xt = <T as CreateInherent<frame_system::Call<T>>>::create_inherent(call.into());
let xt = <T as CreateAuthorizedTransaction<frame_system::Call<T>>>::create_authorized_transaction(call.into());
let res = SubmitTransaction::<T, frame_system::Call<T>>::submit_transaction(xt);
match res {
Ok(_) => log::info!(target: LOG_TARGET, "Submitted the task."),
Expand All @@ -91,7 +91,9 @@ pub mod pallet {
}

#[pallet::config]
pub trait Config: CreateInherent<frame_system::Call<Self>> + frame_system::Config {
pub trait Config:
CreateAuthorizedTransaction<frame_system::Call<Self>> + frame_system::Config
{
type RuntimeTask: frame_support::traits::Task
+ IsType<<Self as frame_system::Config>::RuntimeTask>
+ From<Task<Self>>;
Expand Down
32 changes: 25 additions & 7 deletions substrate/frame/examples/tasks/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,28 @@

use crate::{self as pallet_example_tasks};
use frame_support::derive_impl;
use sp_runtime::testing::TestXt;
use sp_runtime::testing::UintAuthorityId;

pub type AccountId = u32;
pub type Balance = u32;

type Block = frame_system::mocking::MockBlock<Runtime>;
pub type TransactionExtension = (frame_system::AuthorizeCall<Runtime>,);
pub type Header = sp_runtime::generic::Header<u64, sp_runtime::traits::BlakeTwo256>;
pub type Block = sp_runtime::generic::Block<Header, Extrinsic>;
pub type Extrinsic = sp_runtime::generic::UncheckedExtrinsic<
u64,
RuntimeCall,
UintAuthorityId,
TransactionExtension,
>;

frame_support::construct_runtime!(
pub enum Runtime {
System: frame_system,
TasksExample: pallet_example_tasks,
}
);

pub type Extrinsic = TestXt<RuntimeCall, ()>;

#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
impl frame_system::Config for Runtime {
type Block = Block;
Expand All @@ -48,12 +55,23 @@ where
type Extrinsic = Extrinsic;
}

impl<LocalCall> frame_system::offchain::CreateInherent<LocalCall> for Runtime
impl<LocalCall> frame_system::offchain::CreateTransaction<LocalCall> for Runtime
where
RuntimeCall: From<LocalCall>,
{
type Extension = TransactionExtension;

fn create_transaction(call: RuntimeCall, extension: Self::Extension) -> Self::Extrinsic {
Extrinsic::new_transaction(call, extension)
}
}

impl<LocalCall> frame_system::offchain::CreateAuthorizedTransaction<LocalCall> for Runtime
where
RuntimeCall: From<LocalCall>,
{
fn create_inherent(call: Self::RuntimeCall) -> Self::Extrinsic {
Extrinsic::new_bare(call)
fn create_extension() -> Self::Extension {
(frame_system::AuthorizeCall::<Runtime>::new(),)
}
}

Expand Down
67 changes: 60 additions & 7 deletions substrate/frame/examples/tasks/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,23 @@
#![cfg(test)]

use crate::{mock::*, Numbers};
#[cfg(feature = "experimental")]
use codec::Decode;
use frame_support::traits::Task;
use sp_runtime::BuildStorage;
#[cfg(feature = "experimental")]
use codec::{Decode, Encode};
#[cfg(feature = "experimental")]
use frame_support::{
assert_noop, assert_ok, dispatch::GetDispatchInfo,
storage::{with_transaction_unchecked, TransactionOutcome},
};
#[cfg(feature = "experimental")]
use sp_core::offchain::{testing, OffchainWorkerExt, TransactionPoolExt};
use sp_runtime::BuildStorage;

#[cfg(feature = "experimental")]
use frame_support::{assert_noop, assert_ok};
use sp_runtime::{
traits::{Applyable, Checkable},
transaction_validity::TransactionSource,
};


// This function basically just builds a genesis storage key/value store according to
// our desired mockup.
Expand Down Expand Up @@ -157,7 +165,52 @@ fn task_with_offchain_worker() {
let tx = pool_state.write().transactions.pop().unwrap();
assert!(pool_state.read().transactions.is_empty());
let tx = Extrinsic::decode(&mut &*tx).unwrap();
use sp_runtime::traits::ExtrinsicLike;
assert!(tx.is_bare());
assert!(matches!(tx.preamble, sp_runtime::generic::Preamble::General(..)));

let info = tx.get_dispatch_info();
let len = tx.using_encoded(|e| e.len());

let checked = Checkable::check(tx, &frame_system::ChainContext::<Runtime>::default())
.expect("Transaction is general so signature is good");

with_transaction_unchecked(|| {
checked
.validate::<Runtime>(TransactionSource::External, &info, len)
.expect("call valid");
TransactionOutcome::Rollback(())
});

checked
.apply::<Runtime>(&info, len)
.expect("Transaction is valid")
.expect("Transaction is successful");
});
}

#[cfg(feature = "experimental")]
#[test]
fn task_with_bare_also_work() {
new_test_ext().execute_with(|| {
Numbers::<Runtime>::insert(0, 1);
let task = <Runtime as frame_system::Config>::RuntimeTask::iter()
.next()
.expect("There is one task");
let call = frame_system::Call::<Runtime>::do_task { task };
let tx = Extrinsic::new_bare(call.into());

let info = tx.get_dispatch_info();
let len = tx.using_encoded(|e| e.len());

let checked = Checkable::check(tx, &frame_system::ChainContext::<Runtime>::default())
.expect("Transaction is general so signature is good");

checked
.validate::<Runtime>(TransactionSource::External, &info, len)
.expect("call valid");

checked
.apply::<Runtime>(&info, len)
.expect("Transaction is valid")
.expect("Transaction is successful");
});
}
26 changes: 25 additions & 1 deletion substrate/frame/system/benchmarking/src/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@
use alloc::{vec, vec::Vec};
use codec::Encode;
use frame_benchmarking::v2::*;
use frame_support::{dispatch::DispatchClass, storage, traits::Get};
use frame_support::{
dispatch::DispatchClass,
pallet_prelude::{Authorize, TransactionSource},
storage,
traits::Get,
};
use frame_system::{Call, Pallet as System, RawOrigin};
use sp_core::storage::well_known_keys;
use sp_runtime::traits::Hash;
Expand Down Expand Up @@ -226,5 +231,24 @@ mod benchmarks {
Ok(())
}

#[benchmark]
fn authorize_apply_authorized_upgrade() -> Result<(), BenchmarkError> {
let runtime_blob = T::prepare_set_code_data();
T::setup_set_code_requirements(&runtime_blob)?;
let hash = T::Hashing::hash(&runtime_blob);
// Will be heavier when it needs to do verification (i.e. don't use `...without_checks`).
System::<T>::authorize_upgrade(RawOrigin::Root.into(), hash)?;

let call = Call::<T>::apply_authorized_upgrade { code: runtime_blob };

#[block]
{
call.authorize(TransactionSource::External)
.ok_or("Call must give some authorization")??;
}

Ok(())
}

impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Test);
}
Loading
Loading