use codec::Encode;
use sp_weights::Weight;
use crate::{
traits::{
self, transaction_extension::TransactionExtension, AsTransactionAuthorizedOrigin,
DispatchInfoOf, DispatchTransaction, Dispatchable, MaybeDisplay, Member,
PostDispatchInfoOf, ValidateUnsigned,
},
transaction_validity::{TransactionSource, TransactionValidity},
};
use super::unchecked_extrinsic::ExtensionVersion;
const DEFAULT_EXTENSION_VERSION: ExtensionVersion = 0;
#[derive(PartialEq, Eq, Clone, sp_core::RuntimeDebug)]
pub enum ExtrinsicFormat<AccountId, Extension> {
Bare,
Signed(AccountId, Extension),
General(ExtensionVersion, Extension),
}
#[derive(PartialEq, Eq, Clone, sp_core::RuntimeDebug)]
pub struct CheckedExtrinsic<AccountId, Call, Extension> {
pub format: ExtrinsicFormat<AccountId, Extension>,
pub function: Call,
}
impl<AccountId, Call, Extension, RuntimeOrigin> traits::Applyable
for CheckedExtrinsic<AccountId, Call, Extension>
where
AccountId: Member + MaybeDisplay,
Call: Member + Dispatchable<RuntimeOrigin = RuntimeOrigin> + Encode,
Extension: TransactionExtension<Call>,
RuntimeOrigin: From<Option<AccountId>> + AsTransactionAuthorizedOrigin,
{
type Call = Call;
fn validate<I: ValidateUnsigned<Call = Self::Call>>(
&self,
source: TransactionSource,
info: &DispatchInfoOf<Self::Call>,
len: usize,
) -> TransactionValidity {
match self.format {
ExtrinsicFormat::Bare => {
let inherent_validation = I::validate_unsigned(source, &self.function)?;
#[allow(deprecated)]
let legacy_validation = Extension::bare_validate(&self.function, info, len)?;
Ok(legacy_validation.combine_with(inherent_validation))
},
ExtrinsicFormat::Signed(ref signer, ref extension) => {
let origin = Some(signer.clone()).into();
extension
.validate_only(
origin,
&self.function,
info,
len,
source,
DEFAULT_EXTENSION_VERSION,
)
.map(|x| x.0)
},
ExtrinsicFormat::General(extension_version, ref extension) => extension
.validate_only(None.into(), &self.function, info, len, source, extension_version)
.map(|x| x.0),
}
}
fn apply<I: ValidateUnsigned<Call = Self::Call>>(
self,
info: &DispatchInfoOf<Self::Call>,
len: usize,
) -> crate::ApplyExtrinsicResultWithInfo<PostDispatchInfoOf<Self::Call>> {
match self.format {
ExtrinsicFormat::Bare => {
I::pre_dispatch(&self.function)?;
Extension::bare_validate_and_prepare(&self.function, info, len)?;
let res = self.function.dispatch(None.into());
let mut post_info = res.unwrap_or_else(|err| err.post_info);
let pd_res = res.map(|_| ()).map_err(|e| e.error);
Extension::bare_post_dispatch(info, &mut post_info, len, &pd_res)?;
Ok(res)
},
ExtrinsicFormat::Signed(signer, extension) => extension.dispatch_transaction(
Some(signer).into(),
self.function,
info,
len,
DEFAULT_EXTENSION_VERSION,
),
ExtrinsicFormat::General(extension_version, extension) => extension
.dispatch_transaction(None.into(), self.function, info, len, extension_version),
}
}
}
impl<AccountId, Call: Dispatchable, Extension: TransactionExtension<Call>>
CheckedExtrinsic<AccountId, Call, Extension>
{
pub fn extension_weight(&self) -> Weight {
match &self.format {
ExtrinsicFormat::Bare => Weight::zero(),
ExtrinsicFormat::Signed(_, ext) | ExtrinsicFormat::General(_, ext) =>
ext.weight(&self.function),
}
}
}