Skip to main content

sp_runtime/traits/vers_tx_ext/
invalid.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//! Implementation of versioned transaction extension pipeline that is always invalid.
19
20use crate::{
21	traits::{
22		DecodeWithVersion, DecodeWithVersionWithMemTracking, DispatchInfoOf, DispatchOriginOf,
23		Dispatchable, Pipeline, PipelineMetadataBuilder, PipelineVersion, PostDispatchInfoOf,
24	},
25	transaction_validity::{TransactionSource, TransactionValidityError, ValidTransaction},
26};
27use codec::Encode;
28use core::fmt::Debug;
29use scale_info::TypeInfo;
30use sp_weights::Weight;
31
32/// An implementation of [`Pipeline`] that consider any version invalid.
33///
34/// This is mostly used by [`crate::traits::MultiVersion`].
35///
36/// This type cannot be instantiated.
37#[derive(Encode, Debug, Clone, Eq, PartialEq, TypeInfo)]
38pub enum InvalidVersion {}
39
40impl DecodeWithVersion for InvalidVersion {
41	fn decode_with_version<I: codec::Input>(
42		_extension_version: u8,
43		_input: &mut I,
44	) -> Result<Self, codec::Error> {
45		Err(codec::Error::from("Invalid extension version"))
46	}
47}
48
49impl DecodeWithVersionWithMemTracking for InvalidVersion {}
50
51impl<Call: Dispatchable> Pipeline<Call> for InvalidVersion {
52	fn build_metadata(_builder: &mut PipelineMetadataBuilder) {
53		// Do nothing.
54	}
55	fn validate_only(
56		&self,
57		_origin: DispatchOriginOf<Call>,
58		_call: &Call,
59		_info: &DispatchInfoOf<Call>,
60		_len: usize,
61		_source: TransactionSource,
62	) -> Result<ValidTransaction, TransactionValidityError> {
63		// The type cannot be instantiated so this method is never called.
64		unreachable!()
65	}
66	fn dispatch_transaction(
67		self,
68		_origin: DispatchOriginOf<Call>,
69		_call: Call,
70		_info: &DispatchInfoOf<Call>,
71		_len: usize,
72	) -> crate::ApplyExtrinsicResultWithInfo<PostDispatchInfoOf<Call>> {
73		// The type cannot be instantiated so this method is never called.
74		unreachable!()
75	}
76	fn weight(&self, _call: &Call) -> Weight {
77		// The type cannot be instantiated so this method is never called.
78		unreachable!()
79	}
80}
81
82impl PipelineVersion for InvalidVersion {
83	fn version(&self) -> u8 {
84		// The type cannot be instantiated so this method is never called.
85		unreachable!()
86	}
87}
88
89#[cfg(test)]
90mod tests {
91	use super::*;
92
93	#[test]
94	fn invalid_version_cannot_be_decoded() {
95		let mut input = &b""[..];
96		assert_eq!(
97			InvalidVersion::decode_with_version(0, &mut input),
98			Err(codec::Error::from("Invalid extension version"))
99		);
100	}
101}