referrerpolicy=no-referrer-when-downgrade

sp_runtime/traits/transaction_extension/
as_transaction_extension.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! The [AsTransactionExtension] adapter struct for adapting [SignedExtension]s to
19//! [TransactionExtension]s.
20
21#![allow(deprecated)]
22
23use scale_info::TypeInfo;
24use sp_core::RuntimeDebug;
25
26use crate::{
27	traits::{AsSystemOriginSigner, SignedExtension, ValidateResult},
28	transaction_validity::{InvalidTransaction, TransactionSource},
29};
30
31use super::*;
32
33/// Adapter to use a `SignedExtension` in the place of a `TransactionExtension`.
34#[derive(TypeInfo, Encode, Decode, DecodeWithMemTracking, Clone, PartialEq, Eq, RuntimeDebug)]
35#[deprecated = "Convert your SignedExtension to a TransactionExtension."]
36pub struct AsTransactionExtension<SE: SignedExtension>(pub SE);
37
38impl<SE: SignedExtension + Default> Default for AsTransactionExtension<SE> {
39	fn default() -> Self {
40		Self(SE::default())
41	}
42}
43
44impl<SE: SignedExtension> From<SE> for AsTransactionExtension<SE> {
45	fn from(value: SE) -> Self {
46		Self(value)
47	}
48}
49
50impl<SE: SignedExtension> TransactionExtension<SE::Call> for AsTransactionExtension<SE>
51where
52	<SE::Call as Dispatchable>::RuntimeOrigin: AsSystemOriginSigner<SE::AccountId> + Clone,
53{
54	const IDENTIFIER: &'static str = SE::IDENTIFIER;
55	type Implicit = SE::AdditionalSigned;
56
57	fn implicit(&self) -> Result<Self::Implicit, TransactionValidityError> {
58		self.0.additional_signed()
59	}
60	fn metadata() -> Vec<TransactionExtensionMetadata> {
61		SE::metadata()
62	}
63	fn weight(&self, _call: &SE::Call) -> Weight {
64		Weight::zero()
65	}
66	type Val = ();
67	type Pre = SE::Pre;
68
69	fn validate(
70		&self,
71		origin: <SE::Call as Dispatchable>::RuntimeOrigin,
72		call: &SE::Call,
73		info: &DispatchInfoOf<SE::Call>,
74		len: usize,
75		_self_implicit: Self::Implicit,
76		_inherited_implication: &impl Encode,
77		_source: TransactionSource,
78	) -> ValidateResult<Self::Val, SE::Call> {
79		let who = origin.as_system_origin_signer().ok_or(InvalidTransaction::BadSigner)?;
80		let r = self.0.validate(who, call, info, len)?;
81		Ok((r, (), origin))
82	}
83
84	fn prepare(
85		self,
86		_: (),
87		origin: &<SE::Call as Dispatchable>::RuntimeOrigin,
88		call: &SE::Call,
89		info: &DispatchInfoOf<SE::Call>,
90		len: usize,
91	) -> Result<Self::Pre, TransactionValidityError> {
92		let who = origin.as_system_origin_signer().ok_or(InvalidTransaction::BadSigner)?;
93		self.0.pre_dispatch(who, call, info, len)
94	}
95
96	fn post_dispatch_details(
97		pre: Self::Pre,
98		info: &DispatchInfoOf<SE::Call>,
99		post_info: &PostDispatchInfoOf<SE::Call>,
100		len: usize,
101		result: &DispatchResult,
102	) -> Result<Weight, TransactionValidityError> {
103		SE::post_dispatch(Some(pre), info, post_info, len, result)?;
104		Ok(Weight::zero())
105	}
106
107	fn bare_validate(
108		call: &SE::Call,
109		info: &DispatchInfoOf<SE::Call>,
110		len: usize,
111	) -> TransactionValidity {
112		SE::validate_unsigned(call, info, len)
113	}
114
115	fn bare_validate_and_prepare(
116		call: &SE::Call,
117		info: &DispatchInfoOf<SE::Call>,
118		len: usize,
119	) -> Result<(), TransactionValidityError> {
120		SE::pre_dispatch_unsigned(call, info, len)
121	}
122
123	fn bare_post_dispatch(
124		info: &DispatchInfoOf<SE::Call>,
125		post_info: &mut PostDispatchInfoOf<SE::Call>,
126		len: usize,
127		result: &DispatchResult,
128	) -> Result<(), TransactionValidityError> {
129		SE::post_dispatch(None, info, post_info, len, result)
130	}
131}