Skip to content
Merged
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
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 24 additions & 9 deletions crates/pallet-domains/src/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use crate::pallet::Call as DomainsCall;
use crate::weights::WeightInfo;
use crate::{Config, FraudProofFor, OpaqueBundleOf, Origin, Pallet as Domains, SingletonReceiptOf};
use frame_support::ensure;
use frame_support::pallet_prelude::{PhantomData, TypeInfo};
use frame_support::weights::Weight;
use frame_system::pallet_prelude::RuntimeCallFor;
Expand All @@ -28,6 +29,12 @@ where
fn maybe_domains_call(&self) -> Option<&DomainsCall<Runtime>>;
}

/// Trait to check if the Domains are enabled on Consensus.
pub trait DomainsCheck {
/// Check if the domains are enabled on Runtime.
fn is_domains_enabled() -> bool;
}

/// Extensions for pallet-domains unsigned extrinsics.
#[derive(Encode, Decode, Clone, Eq, PartialEq, TypeInfo)]
pub struct DomainsExtension<Runtime>(PhantomData<Runtime>);
Expand Down Expand Up @@ -165,7 +172,7 @@ where

impl<Runtime> TransactionExtension<RuntimeCallFor<Runtime>> for DomainsExtension<Runtime>
where
Runtime: Config + scale_info::TypeInfo + fmt::Debug + Send + Sync,
Runtime: Config + scale_info::TypeInfo + fmt::Debug + Send + Sync + DomainsCheck,
<RuntimeCallFor<Runtime> as Dispatchable>::RuntimeOrigin:
AsSystemOriginSigner<<Runtime as frame_system::Config>::AccountId> + From<Origin> + Clone,
RuntimeCallFor<Runtime>: MaybeDomainsCall<Runtime>,
Expand All @@ -177,19 +184,24 @@ where

fn weight(&self, call: &Runtime::RuntimeCall) -> Weight {
// This extension only apply to the following 3 calls thus only return weight for them.
match call.maybe_domains_call() {
let maybe_weight = match call.maybe_domains_call() {
Some(DomainsCall::submit_bundle { .. }) => {
<Runtime as Config>::WeightInfo::validate_submit_bundle()
Some(<Runtime as Config>::WeightInfo::validate_submit_bundle())
}
Some(DomainsCall::submit_fraud_proof { fraud_proof }) => {
Some(DomainsCall::submit_fraud_proof { fraud_proof }) => Some(
<Runtime as Config>::WeightInfo::fraud_proof_pre_check()
.saturating_add(fraud_proof_verification_weights::<_, _, _, _>(fraud_proof))
}
.saturating_add(fraud_proof_verification_weights::<_, _, _, _>(fraud_proof)),
),
Some(DomainsCall::submit_receipt { .. }) => {
<Runtime as Config>::WeightInfo::validate_singleton_receipt()
Some(<Runtime as Config>::WeightInfo::validate_singleton_receipt())
}
_ => Weight::zero(),
}
_ => None,
};

// There is one additional runtime read to check if the domains are enabled
maybe_weight
.and_then(|weight| weight.checked_add(&Runtime::DbWeight::get().reads(1)))
.unwrap_or(Runtime::DbWeight::get().reads(1))
}

fn validate(
Expand All @@ -212,6 +224,9 @@ where
None => return Ok((ValidTransaction::default(), (), origin)),
};

// ensure domains are enabled
ensure!(Runtime::is_domains_enabled(), InvalidTransaction::Call);

let validity = match domains_call {
DomainsCall::submit_bundle { opaque_bundle } => {
Self::do_validate_submit_bundle(opaque_bundle, source)?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ use sp_transaction_pool::runtime_api::TaggedTransactionQueue;
use std::error::Error;
use std::sync::Arc;
use subspace_core_primitives::pot::PotOutput;
use subspace_runtime::{DisablePallets, Runtime, RuntimeCall, SignedExtra, UncheckedExtrinsic};
use subspace_runtime::{Runtime, RuntimeCall, SignedExtra, UncheckedExtrinsic};
use subspace_runtime_primitives::extension::BalanceTransferCheckExtension;
use subspace_runtime_primitives::opaque::Block as CBlock;
use subspace_runtime_primitives::{AccountId, Balance, BlockHashFor, HeaderFor, Nonce};

Expand Down Expand Up @@ -414,7 +415,7 @@ fn get_singed_extra(best_number: u64, immortal: bool, nonce: Nonce) -> SignedExt
frame_system::CheckNonce::<Runtime>::from(nonce.into()),
frame_system::CheckWeight::<Runtime>::new(),
pallet_transaction_payment::ChargeTransactionPayment::<Runtime>::from(0u128),
DisablePallets,
BalanceTransferCheckExtension::<Runtime>::default(),
pallet_subspace::extensions::SubspaceExtension::<Runtime>::new(),
pallet_domains::extensions::DomainsExtension::<Runtime>::new(),
pallet_messenger::extensions::MessengerExtension::<Runtime>::new(),
Expand Down
10 changes: 10 additions & 0 deletions crates/subspace-runtime-primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
parity-scale-codec = { workspace = true, features = ["derive"] }
frame-benchmarking = { workspace = true, optional = true }
frame-support.workspace = true
frame-system.workspace = true
pallet-balances.workspace = true
pallet-multisig.workspace = true
pallet-transaction-payment.workspace = true
pallet-utility.workspace = true
Expand All @@ -35,6 +37,7 @@ std = [
"parity-scale-codec/std",
"frame-support/std",
"frame-system/std",
"pallet-balances/std",
"pallet-multisig/std",
"pallet-transaction-payment/std",
"pallet-utility/std",
Expand All @@ -47,3 +50,10 @@ std = [
testing = [
"sp-io"
]

runtime-benchmarks = [
"frame-benchmarking",
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
]
200 changes: 200 additions & 0 deletions crates/subspace-runtime-primitives/src/extension.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
#[cfg(feature = "runtime-benchmarks")]
pub mod benchmarking;
pub mod weights;

use crate::extension::weights::WeightInfo as SubstrateWeightInfo;
use crate::utility::{MaybeNestedCall, nested_call_iter};
use core::marker::PhantomData;
use frame_support::RuntimeDebugNoBound;
use frame_support::pallet_prelude::Weight;
use frame_system::Config;
use frame_system::pallet_prelude::{OriginFor, RuntimeCallFor};
use pallet_balances::Call as BalancesCall;
use parity_scale_codec::{Decode, Encode};
use scale_info::TypeInfo;
use scale_info::prelude::fmt;
use sp_runtime::DispatchResult;
use sp_runtime::traits::{
AsSystemOriginSigner, DispatchInfoOf, DispatchOriginOf, Dispatchable, PostDispatchInfoOf,
TransactionExtension, ValidateResult,
};
use sp_runtime::transaction_validity::{
InvalidTransaction, TransactionSource, TransactionValidityError, ValidTransaction,
};

/// Maximum number of calls we benchmarked for.
const MAXIMUM_NUMBER_OF_CALLS: u32 = 5_000;

/// Weights for the balance transfer check extension.
pub trait WeightInfo {
fn balance_transfer_check_multiple(c: u32) -> Weight;
fn balance_transfer_check_utility(c: u32) -> Weight;
fn balance_transfer_check_multisig(c: u32) -> Weight;
}

/// Trait to convert Runtime call to possible Balance call.
pub trait MaybeBalancesCall<Runtime>
where
Runtime: pallet_balances::Config,
{
fn maybe_balance_call(&self) -> Option<&BalancesCall<Runtime>>;
}

/// Trait to check if the Balance transfers are enabled.
pub trait BalanceTransferChecks {
fn is_balance_transferable() -> bool;
}

/// Disable balance transfers, if configured in the runtime.
#[derive(Debug, Encode, Decode, Clone, Eq, PartialEq, TypeInfo)]
pub struct BalanceTransferCheckExtension<Runtime>(PhantomData<Runtime>);

impl<Runtime> Default for BalanceTransferCheckExtension<Runtime>
where
Runtime: BalanceTransferChecks + pallet_balances::Config,
RuntimeCallFor<Runtime>: MaybeBalancesCall<Runtime> + MaybeNestedCall<Runtime>,
{
fn default() -> Self {
Self(PhantomData)
}
}

impl<Runtime> BalanceTransferCheckExtension<Runtime>
where
Runtime: BalanceTransferChecks + pallet_balances::Config,
RuntimeCallFor<Runtime>: MaybeBalancesCall<Runtime> + MaybeNestedCall<Runtime>,
{
fn do_validate_signed(
call: &RuntimeCallFor<Runtime>,
) -> Result<(ValidTransaction, u32), TransactionValidityError> {
if Runtime::is_balance_transferable() {
return Ok((ValidTransaction::default(), 0));
}

// Disable normal balance transfers.
let (contains_balance_call, calls) = Self::contains_balance_transfer(call);
if contains_balance_call {
Err(InvalidTransaction::Call.into())
} else {
Ok((ValidTransaction::default(), calls))
}
}

fn contains_balance_transfer(call: &RuntimeCallFor<Runtime>) -> (bool, u32) {
let mut calls = 0;
for call in nested_call_iter::<Runtime>(call) {
calls += 1;
// Any other calls might contain nested calls, so we can only return early if we find a
// balance transfer call.
if let Some(balance_call) = call.maybe_balance_call()
&& matches!(
balance_call,
BalancesCall::transfer_allow_death { .. }
| BalancesCall::transfer_keep_alive { .. }
| BalancesCall::transfer_all { .. }
)
{
return (true, calls);
}
}

(false, calls)
}

fn get_weights(n: u32) -> Weight {
SubstrateWeightInfo::<Runtime>::balance_transfer_check_multisig(n)
.max(SubstrateWeightInfo::<Runtime>::balance_transfer_check_multiple(n))
.max(SubstrateWeightInfo::<Runtime>::balance_transfer_check_utility(n))
}
}

/// Data passed from prepare to post_dispatch.
#[derive(RuntimeDebugNoBound)]
pub enum Pre {
Refund(Weight),
}

/// Data passed from validate to prepare.
#[derive(RuntimeDebugNoBound)]
pub enum Val {
FullRefund,
PartialRefund(Option<u32>),
}

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

fn weight(&self, _call: &RuntimeCallFor<Runtime>) -> Weight {
Self::get_weights(MAXIMUM_NUMBER_OF_CALLS)
}

fn validate(
&self,
origin: OriginFor<Runtime>,
call: &RuntimeCallFor<Runtime>,
_info: &DispatchInfoOf<RuntimeCallFor<Runtime>>,
_len: usize,
_self_implicit: Self::Implicit,
_inherited_implication: &impl Encode,
_source: TransactionSource,
) -> ValidateResult<Self::Val, RuntimeCallFor<Runtime>> {
let (validity, val) = if origin.as_system_origin_signer().is_some() {
let (valid, maybe_calls) =
Self::do_validate_signed(call).map(|(valid, calls)| (valid, Some(calls)))?;
(valid, Val::PartialRefund(maybe_calls))
} else {
(ValidTransaction::default(), Val::FullRefund)
};

Ok((validity, val, origin))
}

fn prepare(
self,
val: Self::Val,
_origin: &DispatchOriginOf<RuntimeCallFor<Runtime>>,
_call: &RuntimeCallFor<Runtime>,
_info: &DispatchInfoOf<RuntimeCallFor<Runtime>>,
_len: usize,
) -> Result<Self::Pre, TransactionValidityError> {
let total_weight = Self::get_weights(MAXIMUM_NUMBER_OF_CALLS);
match val {
// not a signed transaction, so return full refund.
Val::FullRefund => Ok(Pre::Refund(total_weight)),

// signed transaction with a minimum of one read weight,
// so refund any extra call weight
Val::PartialRefund(maybe_calls) => {
let actual_weights = Self::get_weights(maybe_calls.unwrap_or(0));
Ok(Pre::Refund(total_weight.saturating_sub(actual_weights)))
}
}
}

fn post_dispatch_details(
pre: Self::Pre,
_info: &DispatchInfoOf<RuntimeCallFor<Runtime>>,
_post_info: &PostDispatchInfoOf<RuntimeCallFor<Runtime>>,
_len: usize,
_result: &DispatchResult,
) -> Result<Weight, TransactionValidityError> {
let Pre::Refund(weight) = pre;
Ok(weight)
}
}
Loading
Loading