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

Commit

Permalink
Use pallet-sudo extension CheckOnylSudoAccount (#1850)
Browse files Browse the repository at this point in the history
  • Loading branch information
koushiro committed Nov 19, 2022
1 parent 1d5566d commit 037397e
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 87 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ members = [
"pallets/parachain-system",
"pallets/parachain-system/proc-macro",
"pallets/session-benchmarking",
"pallets/solo-to-para",
"pallets/xcm",
"pallets/xcmp-queue",
"parachain-template/node",
Expand Down
88 changes: 2 additions & 86 deletions pallets/solo-to-para/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,12 @@

#![cfg_attr(not(feature = "std"), no_std)]

use codec::{Decode, Encode};
use cumulus_pallet_parachain_system as parachain_system;
use frame_support::{
dispatch::{DispatchInfo, DispatchResult},
pallet_prelude::*,
};
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
pub use pallet::*;
use polkadot_primitives::v2::PersistedValidationData;
use scale_info::TypeInfo;
use sp_runtime::{
traits::{DispatchInfoOf, Dispatchable, SignedExtension},
transaction_validity::{
InvalidTransaction, TransactionLongevity, TransactionPriority, TransactionValidity,
TransactionValidityError, ValidTransaction,
},
};
use sp_std::{prelude::*, vec::Vec};
use sp_std::vec::Vec;

#[frame_support::pallet]
pub mod pallet {
Expand Down Expand Up @@ -110,76 +98,4 @@ pub mod pallet {
crate::Pallet::<T>::set_pending_custom_validation_head_data();
}
}

/// Ensure that signed transactions are only valid if they are signed by root.
#[derive(Encode, Decode, Clone, Eq, PartialEq, TypeInfo, Default)]
#[scale_info(skip_type_params(T))]
pub struct CheckSudo<T: Config + Send + Sync>(sp_std::marker::PhantomData<T>);

impl<T: Config + Send + Sync> CheckSudo<T> {
pub fn new() -> Self {
Self(Default::default())
}
}

impl<T: Config + Send + Sync> sp_std::fmt::Debug for CheckSudo<T> {
#[cfg(feature = "std")]
fn fmt(&self, f: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result {
write!(f, "CheckSudo")
}

#[cfg(not(feature = "std"))]
fn fmt(&self, _: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result {
Ok(())
}
}

impl<T: Config + Send + Sync> SignedExtension for CheckSudo<T>
where
<T as frame_system::Config>::RuntimeCall: Dispatchable<Info = DispatchInfo>,
{
type AccountId = T::AccountId;
type Call = <T as frame_system::Config>::RuntimeCall;
type AdditionalSigned = ();
type Pre = ();
const IDENTIFIER: &'static str = "CheckSudo";

fn additional_signed(&self) -> sp_std::result::Result<(), TransactionValidityError> {
Ok(())
}

fn pre_dispatch(
self,
who: &Self::AccountId,
call: &Self::Call,
info: &DispatchInfoOf<Self::Call>,
len: usize,
) -> Result<Self::Pre, TransactionValidityError> {
Ok(self.validate(who, call, info, len).map(|_| ())?)
}

fn validate(
&self,
who: &Self::AccountId,
_call: &Self::Call,
info: &DispatchInfoOf<Self::Call>,
_len: usize,
) -> TransactionValidity {
let root_account = match pallet_sudo::Pallet::<T>::key() {
Some(account) => account,
None => return Err(InvalidTransaction::BadSigner.into()),
};

if *who == root_account {
Ok(ValidTransaction {
priority: info.weight.ref_time() as TransactionPriority,
longevity: TransactionLongevity::max_value(),
propagate: true,
..Default::default()
})
} else {
Err(InvalidTransaction::BadSigner.into())
}
}
}
}
2 changes: 1 addition & 1 deletion parachains/runtimes/starters/seedling/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ pub type SignedExtra = (
frame_system::CheckGenesis<Runtime>,
frame_system::CheckEra<Runtime>,
frame_system::CheckNonce<Runtime>,
cumulus_pallet_solo_to_para::CheckSudo<Runtime>,
pallet_sudo::CheckOnlySudoAccount<Runtime>,
);
/// Unchecked extrinsic type as expected by this runtime.
pub type UncheckedExtrinsic =
Expand Down

0 comments on commit 037397e

Please sign in to comment.