referrerpolicy=no-referrer-when-downgrade
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// 	http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Generic implementation of an extrinsic that has passed the verification
//! stage.

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;

/// Default version of the [Extension](TransactionExtension) used to construct the inherited
/// implication for legacy transactions.
const DEFAULT_EXTENSION_VERSION: ExtensionVersion = 0;

/// The kind of extrinsic this is, including any fields required of that kind. This is basically
/// the full extrinsic except the `Call`.
#[derive(PartialEq, Eq, Clone, sp_core::RuntimeDebug)]
pub enum ExtrinsicFormat<AccountId, Extension> {
	/// Extrinsic is bare; it must pass either the bare forms of `TransactionExtension` or
	/// `ValidateUnsigned`, both deprecated, or alternatively a `ProvideInherent`.
	Bare,
	/// Extrinsic has a default `Origin` of `Signed(AccountId)` and must pass all
	/// `TransactionExtension`s regular checks and includes all extension data.
	Signed(AccountId, Extension),
	/// Extrinsic has a default `Origin` of `None` and must pass all `TransactionExtension`s.
	/// regular checks and includes all extension data.
	General(ExtensionVersion, Extension),
}

/// Definition of something that the external world might want to say; its existence implies that it
/// has been checked and is good, particularly with regards to the signature.
///
/// This is typically passed into [`traits::Applyable::apply`], which should execute
/// [`CheckedExtrinsic::function`], alongside all other bits and bobs.
#[derive(PartialEq, Eq, Clone, sp_core::RuntimeDebug)]
pub struct CheckedExtrinsic<AccountId, Call, Extension> {
	/// Who this purports to be from and the number of extrinsics have come before
	/// from the same signer, if anyone (note this is not a signature).
	pub format: ExtrinsicFormat<AccountId, Extension>,

	/// The function that should be called.
	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)?;
				// TODO: Separate logic from `TransactionExtension` into a new `InherentExtension`
				// interface.
				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);
				// TODO: Separate logic from `TransactionExtension` into a new `InherentExtension`
				// interface.
				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>
{
	/// Returns the weight of the extension of this transaction, if present. If the transaction
	/// doesn't use any extension, the weight returned is equal to zero.
	pub fn extension_weight(&self) -> Weight {
		match &self.format {
			ExtrinsicFormat::Bare => Weight::zero(),
			ExtrinsicFormat::Signed(_, ext) | ExtrinsicFormat::General(_, ext) =>
				ext.weight(&self.function),
		}
	}
}