Skip to content

Commit

Permalink
disable general extrinsics
Browse files Browse the repository at this point in the history
  • Loading branch information
vedhavyas committed Feb 19, 2025
1 parent 090c607 commit 5a4f65b
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ pub fn construct_signed_extrinsic(
frame_system::CheckWeight::<Runtime>::new(),
pallet_transaction_payment::ChargeTransactionPayment::<Runtime>::from(0u128),
DisablePallets,
subspace_runtime_primitives::extensions::DisableGeneralExtrinsics::<Runtime>::new(),
);
let raw_payload = generic::SignedPayload::<RuntimeCall, SignedExtra>::from_raw(
call.clone(),
Expand All @@ -430,6 +431,7 @@ pub fn construct_signed_extrinsic(
(),
(),
(),
(),
),
);

Expand Down
75 changes: 75 additions & 0 deletions crates/subspace-runtime-primitives/src/extensions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//! Extensions for Runtimes
use codec::{Decode, Encode};
use frame_support::pallet_prelude::{PhantomData, TypeInfo};
use frame_system::pallet_prelude::RuntimeCallFor;
use frame_system::Config;
use scale_info::prelude::fmt;
use sp_runtime::impl_tx_ext_default;
use sp_runtime::traits::{
AsSystemOriginSigner, DispatchInfoOf, DispatchOriginOf, Dispatchable, Implication,
TransactionExtension, ValidateResult,
};
use sp_runtime::transaction_validity::{InvalidTransaction, TransactionSource, ValidTransaction};

/// Disable General Extrinsics until we migrate from Bare to General.
// TODO: Should either adapt or remove use of this extension during and after migration to
// General Extrinsics from bare extrinsics
#[derive(Encode, Decode, Clone, Eq, PartialEq, TypeInfo)]
pub struct DisableGeneralExtrinsics<Runtime>(PhantomData<Runtime>);

impl<Runtime> DisableGeneralExtrinsics<Runtime> {
pub fn new() -> Self {
Self(PhantomData)
}
}

impl<Runtime> Default for DisableGeneralExtrinsics<Runtime> {
fn default() -> Self {
Self::new()
}
}

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

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

impl<Runtime> TransactionExtension<RuntimeCallFor<Runtime>> for DisableGeneralExtrinsics<Runtime>
where
Runtime: Config + scale_info::TypeInfo + fmt::Debug + Send + Sync,
<RuntimeCallFor<Runtime> as Dispatchable>::RuntimeOrigin:
AsSystemOriginSigner<<Runtime as Config>::AccountId> + Clone,
{
const IDENTIFIER: &'static str = "DisableGeneralExtrinsics";
type Implicit = ();
type Val = ();
type Pre = ();

fn validate(
&self,
origin: DispatchOriginOf<RuntimeCallFor<Runtime>>,
_call: &RuntimeCallFor<Runtime>,
_info: &DispatchInfoOf<RuntimeCallFor<Runtime>>,
_len: usize,
_self_implicit: Self::Implicit,
_inherited_implication: &impl Implication,
_source: TransactionSource,
) -> ValidateResult<Self::Val, RuntimeCallFor<Runtime>> {
if origin.as_system_origin_signer().is_none() {
Err(InvalidTransaction::Call.into())
} else {
Ok((ValidTransaction::default(), (), origin))
}
}

impl_tx_ext_default!(RuntimeCallFor<Runtime>; weight);
impl_tx_ext_default!(RuntimeCallFor<Runtime>; prepare);
}
1 change: 1 addition & 0 deletions crates/subspace-runtime-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#![cfg_attr(not(feature = "std"), no_std)]

pub mod extensions;
pub mod utility;

#[cfg(not(feature = "std"))]
Expand Down
2 changes: 2 additions & 0 deletions crates/subspace-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,8 @@ pub type SignedExtra = (
frame_system::CheckWeight<Runtime>,
pallet_transaction_payment::ChargeTransactionPayment<Runtime>,
DisablePallets,
// TODO: remove or adapt after or during migration to General extrinsic respectively
subspace_runtime_primitives::extensions::DisableGeneralExtrinsics<Runtime>,
);
/// Unchecked extrinsic type as expected by this runtime.
pub type UncheckedExtrinsic =
Expand Down
28 changes: 9 additions & 19 deletions crates/subspace-runtime/src/signed_extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use codec::{Decode, Encode};
use frame_support::pallet_prelude::Weight;
use frame_system::pallet_prelude::{OriginFor, RuntimeCallFor};
use scale_info::TypeInfo;
use sp_runtime::impl_tx_ext_default;
use sp_runtime::traits::{
AsSystemOriginSigner, DispatchInfoOf, DispatchOriginOf, TransactionExtension, ValidateResult,
AsSystemOriginSigner, DispatchInfoOf, TransactionExtension, ValidateResult,
};
use sp_runtime::transaction_validity::{
InvalidTransaction, TransactionSource, TransactionValidity, TransactionValidityError,
Expand Down Expand Up @@ -34,13 +35,6 @@ impl DisablePallets {
Ok(ValidTransaction::default())
}
}

fn do_validate(origin: &OriginFor<Runtime>, call: &RuntimeCall) -> TransactionValidity {
match origin.as_system_origin_signer() {
None => Self::do_validate_unsigned(call),
Some(_) => Self::do_validate_signed(call),
}
}
}

impl TransactionExtension<RuntimeCall> for DisablePallets {
Expand All @@ -65,20 +59,16 @@ impl TransactionExtension<RuntimeCall> for DisablePallets {
_inherited_implication: &impl Encode,
_source: TransactionSource,
) -> ValidateResult<Self::Val, RuntimeCallFor<Runtime>> {
let validity = Self::do_validate(&origin, call)?;
let validity = if origin.as_system_origin_signer().is_some() {
Self::do_validate_signed(call)?
} else {
ValidTransaction::default()
};

Ok((validity, (), origin))
}

fn prepare(
self,
_val: Self::Val,
_origin: &DispatchOriginOf<RuntimeCallFor<Runtime>>,
_call: &RuntimeCallFor<Runtime>,
_info: &DispatchInfoOf<RuntimeCallFor<Runtime>>,
_len: usize,
) -> Result<Self::Pre, TransactionValidityError> {
Ok(())
}
impl_tx_ext_default!(RuntimeCallFor<Runtime>; prepare);

fn bare_validate(
call: &RuntimeCallFor<Runtime>,
Expand Down
15 changes: 3 additions & 12 deletions domains/pallets/evm-tracker/src/create_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use frame_system::pallet_prelude::{OriginFor, RuntimeCallFor};
use pallet_ethereum::{Transaction as EthereumTransaction, TransactionAction};
use scale_info::prelude::fmt;
use sp_core::Get;
use sp_runtime::impl_tx_ext_default;
use sp_runtime::traits::{
AsSystemOriginSigner, DispatchInfoOf, DispatchOriginOf, Dispatchable, TransactionExtension,
ValidateResult,
AsSystemOriginSigner, DispatchInfoOf, Dispatchable, TransactionExtension, ValidateResult,
};
use sp_runtime::transaction_validity::{
InvalidTransaction, TransactionSource, TransactionValidity, TransactionValidityError,
Expand Down Expand Up @@ -229,16 +229,7 @@ where
Ok((validity, (), origin))
}

fn prepare(
self,
_val: Self::Val,
_origin: &DispatchOriginOf<RuntimeCallFor<Runtime>>,
_call: &RuntimeCallFor<Runtime>,
_info: &DispatchInfoOf<RuntimeCallFor<Runtime>>,
_len: usize,
) -> Result<Self::Pre, TransactionValidityError> {
Ok(())
}
impl_tx_ext_default!(RuntimeCallFor<Runtime>; prepare);

fn bare_validate(
call: &RuntimeCallFor<Runtime>,
Expand Down
2 changes: 2 additions & 0 deletions domains/runtime/auto-id/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ pub type SignedExtra = (
frame_system::CheckNonce<Runtime>,
domain_check_weight::CheckWeight<Runtime>,
pallet_transaction_payment::ChargeTransactionPayment<Runtime>,
// TODO: remove or adapt after or during migration to General extrinsic respectively
subspace_runtime_primitives::extensions::DisableGeneralExtrinsics<Runtime>,
);

/// Unchecked extrinsic type as expected by this runtime.
Expand Down
6 changes: 6 additions & 0 deletions domains/runtime/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ pub type SignedExtra = (
domain_check_weight::CheckWeight<Runtime>,
pallet_transaction_payment::ChargeTransactionPayment<Runtime>,
CheckContractCreation<Runtime>,
// TODO: remove or adapt after or during migration to General extrinsic respectively
subspace_runtime_primitives::extensions::DisableGeneralExtrinsics<Runtime>,
);

/// Custom signed extra for check_and_pre_dispatch.
Expand All @@ -135,6 +137,8 @@ type CustomSignedExtra = (
domain_check_weight::CheckWeight<Runtime>,
pallet_transaction_payment::ChargeTransactionPayment<Runtime>,
CheckContractCreation<Runtime>,
// TODO: remove or adapt after or during migration to General extrinsic respectively
subspace_runtime_primitives::extensions::DisableGeneralExtrinsics<Runtime>,
);

/// Unchecked extrinsic type as expected by this runtime.
Expand Down Expand Up @@ -1119,6 +1123,7 @@ fn check_transaction_and_do_pre_dispatch_inner(
extra.6,
extra.7.clone(),
extra.8,
extra.9,
);

let origin = RuntimeOrigin::none();
Expand All @@ -1143,6 +1148,7 @@ fn check_transaction_and_do_pre_dispatch_inner(
extra.6,
extra.7.clone(),
extra.8,
extra.9,
);

let origin = RuntimeOrigin::signed(account_id);
Expand Down
2 changes: 2 additions & 0 deletions domains/test/runtime/auto-id/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ pub type SignedExtra = (
frame_system::CheckNonce<Runtime>,
domain_check_weight::CheckWeight<Runtime>,
pallet_transaction_payment::ChargeTransactionPayment<Runtime>,
// TODO: remove or adapt after or during migration to General extrinsic respectively
subspace_runtime_primitives::extensions::DisableGeneralExtrinsics<Runtime>,
);

/// Unchecked extrinsic type as expected by this runtime.
Expand Down
8 changes: 8 additions & 0 deletions domains/test/runtime/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ pub type SignedExtra = (
domain_check_weight::CheckWeight<Runtime>,
pallet_transaction_payment::ChargeTransactionPayment<Runtime>,
pallet_evm_tracker::create_contract::CheckContractCreation<Runtime>,
// TODO: remove or adapt after or during migration to General extrinsic respectively
subspace_runtime_primitives::extensions::DisableGeneralExtrinsics<Runtime>,
);

/// Custom signed extra for check_and_pre_dispatch.
Expand All @@ -133,6 +135,8 @@ type CustomSignedExtra = (
domain_check_weight::CheckWeight<Runtime>,
pallet_transaction_payment::ChargeTransactionPayment<Runtime>,
pallet_evm_tracker::create_contract::CheckContractCreation<Runtime>,
// TODO: remove or adapt after or during migration to General extrinsic respectively
subspace_runtime_primitives::extensions::DisableGeneralExtrinsics<Runtime>,
);

/// Unchecked extrinsic type as expected by this runtime.
Expand Down Expand Up @@ -176,6 +180,7 @@ pub fn construct_extrinsic_raw_payload(
domain_check_weight::CheckWeight::<Runtime>::new(),
pallet_transaction_payment::ChargeTransactionPayment::<Runtime>::from(tip),
pallet_evm_tracker::create_contract::CheckContractCreation::<Runtime>::new(),
subspace_runtime_primitives::extensions::DisableGeneralExtrinsics::<Runtime>::new(),
);
(
generic::SignedPayload::<RuntimeCallFor<Runtime>, SignedExtra>::from_raw(
Expand All @@ -191,6 +196,7 @@ pub fn construct_extrinsic_raw_payload(
(),
(),
(),
(),
),
),
extra,
Expand Down Expand Up @@ -1145,6 +1151,7 @@ fn check_transaction_and_do_pre_dispatch_inner(
extra.6,
extra.7.clone(),
extra.8,
extra.9,
);

let origin = RuntimeOrigin::none();
Expand All @@ -1169,6 +1176,7 @@ fn check_transaction_and_do_pre_dispatch_inner(
extra.6,
extra.7.clone(),
extra.8,
extra.9,
);

let origin = RuntimeOrigin::signed(account_id);
Expand Down
2 changes: 2 additions & 0 deletions test/subspace-test-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,8 @@ pub type SignedExtra = (
frame_system::CheckNonce<Runtime>,
frame_system::CheckWeight<Runtime>,
pallet_transaction_payment::ChargeTransactionPayment<Runtime>,
// TODO: remove or adapt after or during migration to General extrinsic respectively
subspace_runtime_primitives::extensions::DisableGeneralExtrinsics<Runtime>,
);
/// Unchecked extrinsic type as expected by this runtime.
pub type UncheckedExtrinsic =
Expand Down
3 changes: 2 additions & 1 deletion test/subspace-test-service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1370,6 +1370,7 @@ where
frame_system::CheckNonce::<Runtime>::from(nonce),
frame_system::CheckWeight::<Runtime>::new(),
pallet_transaction_payment::ChargeTransactionPayment::<Runtime>::from(tip),
subspace_runtime_primitives::extensions::DisableGeneralExtrinsics::<Runtime>::new(),
);
(
generic::SignedPayload::<
Expand All @@ -1378,7 +1379,7 @@ where
>::from_raw(
function,
extra.clone(),
((), 100, 1, genesis_block, current_block_hash, (), (), ()),
((), 100, 1, genesis_block, current_block_hash, (), (), (), ()),
),
extra,
)
Expand Down

0 comments on commit 5a4f65b

Please sign in to comment.