sp-runtime 46.0.0

Runtime Modules shared primitive types.
Documentation
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
// 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 unchecked (pre-verification) extrinsic.

use crate::{
	generic::{CheckedExtrinsic, ExtrinsicFormat},
	traits::{
		self, transaction_extension::TransactionExtension, Checkable, Dispatchable, ExtrinsicCall,
		ExtrinsicLike, ExtrinsicMetadata, IdentifyAccount, LazyExtrinsic, MaybeDisplay, Member,
		SignaturePayload,
	},
	transaction_validity::{InvalidTransaction, TransactionValidityError},
	OpaqueExtrinsic,
};
#[cfg(all(not(feature = "std"), feature = "serde"))]
use alloc::format;
use alloc::{vec, vec::Vec};
use codec::{
	Compact, CountedInput, Decode, DecodeWithMemLimit, DecodeWithMemTracking, Encode, EncodeLike,
	Input,
};
use core::fmt::{
	Debug, {self},
};
use scale_info::{build::Fields, meta_type, Path, StaticTypeInfo, Type, TypeInfo, TypeParameter};
use sp_io::hashing::blake2_256;
use sp_weights::Weight;

/// Type to represent the version of the [Extension](TransactionExtension) used in this extrinsic.
pub type ExtensionVersion = u8;
/// Type to represent the extrinsic format version which defines an [UncheckedExtrinsic].
pub type ExtrinsicVersion = u8;

/// Current version of the [`UncheckedExtrinsic`] encoded format.
///
/// This version needs to be bumped if the encoded representation changes.
/// It ensures that if the representation is changed and the format is not known,
/// the decoding fails.
pub const EXTRINSIC_FORMAT_VERSION: ExtrinsicVersion = 5;
/// Legacy version of the [`UncheckedExtrinsic`] encoded format.
///
/// This version was used in the signed/unsigned transaction model and is still supported for
/// compatibility reasons. It will be deprecated in favor of v5 extrinsics and an inherent/general
/// transaction model.
pub const LEGACY_EXTRINSIC_FORMAT_VERSION: ExtrinsicVersion = 4;
/// Current version of the [Extension](TransactionExtension) used in this
/// [extrinsic](UncheckedExtrinsic).
///
/// This version needs to be bumped if there are breaking changes to the extension used in the
/// [UncheckedExtrinsic] implementation.
const EXTENSION_VERSION: ExtensionVersion = 0;

/// Maximum decoded heap size for a runtime call (in bytes).
pub const DEFAULT_MAX_CALL_SIZE: usize = 16 * 1024 * 1024; // 16 MiB

/// The `SignaturePayload` of `UncheckedExtrinsic`.
pub type UncheckedSignaturePayload<Address, Signature, Extension> = (Address, Signature, Extension);

impl<Address: TypeInfo, Signature: TypeInfo, Extension: TypeInfo> SignaturePayload
	for UncheckedSignaturePayload<Address, Signature, Extension>
{
	type SignatureAddress = Address;
	type Signature = Signature;
	type SignatureExtra = Extension;
}

/// A "header" for extrinsics leading up to the call itself. Determines the type of extrinsic and
/// holds any necessary specialized data.
#[derive(DecodeWithMemTracking, Eq, PartialEq, Clone)]
pub enum Preamble<Address, Signature, Extension> {
	/// An extrinsic without a signature or any extension. This means it's either an inherent or
	/// an old-school "Unsigned" (we don't use that terminology any more since it's confusable with
	/// the general transaction which is without a signature but does have an extension).
	///
	/// NOTE: In the future, once we remove `ValidateUnsigned`, this will only serve Inherent
	/// extrinsics and thus can be renamed to `Inherent`.
	Bare(ExtrinsicVersion),
	/// An old-school transaction extrinsic which includes a signature of some hard-coded crypto.
	/// Available only on extrinsic version 4.
	Signed(Address, Signature, Extension),
	/// A new-school transaction extrinsic which does not include a signature by default. The
	/// origin authorization, through signatures or other means, is performed by the transaction
	/// extension in this extrinsic. Available starting with extrinsic version 5.
	General(ExtensionVersion, Extension),
}

const VERSION_MASK: u8 = 0b0011_1111;
const TYPE_MASK: u8 = 0b1100_0000;
const BARE_EXTRINSIC: u8 = 0b0000_0000;
const SIGNED_EXTRINSIC: u8 = 0b1000_0000;
const GENERAL_EXTRINSIC: u8 = 0b0100_0000;

impl<Address, Signature, Extension> Decode for Preamble<Address, Signature, Extension>
where
	Address: Decode,
	Signature: Decode,
	Extension: Decode,
{
	fn decode<I: Input>(input: &mut I) -> Result<Self, codec::Error> {
		let version_and_type = input.read_byte()?;

		let version = version_and_type & VERSION_MASK;
		let xt_type = version_and_type & TYPE_MASK;

		let preamble = match (version, xt_type) {
			(
				extrinsic_version @ LEGACY_EXTRINSIC_FORMAT_VERSION..=EXTRINSIC_FORMAT_VERSION,
				BARE_EXTRINSIC,
			) => Self::Bare(extrinsic_version),
			(LEGACY_EXTRINSIC_FORMAT_VERSION, SIGNED_EXTRINSIC) => {
				let address = Address::decode(input)?;
				let signature = Signature::decode(input)?;
				let ext = Extension::decode(input)?;
				Self::Signed(address, signature, ext)
			},
			(EXTRINSIC_FORMAT_VERSION, GENERAL_EXTRINSIC) => {
				let ext_version = ExtensionVersion::decode(input)?;
				let ext = Extension::decode(input)?;
				Self::General(ext_version, ext)
			},
			(_, _) => return Err("Invalid transaction version".into()),
		};

		Ok(preamble)
	}
}

impl<Address, Signature, Extension> Encode for Preamble<Address, Signature, Extension>
where
	Address: Encode,
	Signature: Encode,
	Extension: Encode,
{
	fn size_hint(&self) -> usize {
		match &self {
			Preamble::Bare(_) => EXTRINSIC_FORMAT_VERSION.size_hint(),
			Preamble::Signed(address, signature, ext) => LEGACY_EXTRINSIC_FORMAT_VERSION
				.size_hint()
				.saturating_add(address.size_hint())
				.saturating_add(signature.size_hint())
				.saturating_add(ext.size_hint()),
			Preamble::General(ext_version, ext) => EXTRINSIC_FORMAT_VERSION
				.size_hint()
				.saturating_add(ext_version.size_hint())
				.saturating_add(ext.size_hint()),
		}
	}

	fn encode_to<T: codec::Output + ?Sized>(&self, dest: &mut T) {
		match &self {
			Preamble::Bare(extrinsic_version) => {
				(extrinsic_version | BARE_EXTRINSIC).encode_to(dest);
			},
			Preamble::Signed(address, signature, ext) => {
				(LEGACY_EXTRINSIC_FORMAT_VERSION | SIGNED_EXTRINSIC).encode_to(dest);
				address.encode_to(dest);
				signature.encode_to(dest);
				ext.encode_to(dest);
			},
			Preamble::General(ext_version, ext) => {
				(EXTRINSIC_FORMAT_VERSION | GENERAL_EXTRINSIC).encode_to(dest);
				ext_version.encode_to(dest);
				ext.encode_to(dest);
			},
		}
	}
}

impl<Address, Signature, Extension> Preamble<Address, Signature, Extension> {
	/// Returns `Some` if this is a signed extrinsic, together with the relevant inner fields.
	pub fn to_signed(self) -> Option<(Address, Signature, Extension)> {
		match self {
			Self::Signed(a, s, e) => Some((a, s, e)),
			_ => None,
		}
	}
}

impl<Address, Signature, Extension> fmt::Debug for Preamble<Address, Signature, Extension>
where
	Address: fmt::Debug,
	Extension: fmt::Debug,
{
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		match self {
			Self::Bare(_) => write!(f, "Bare"),
			Self::Signed(address, _, tx_ext) => write!(f, "Signed({:?}, {:?})", address, tx_ext),
			Self::General(ext_version, tx_ext) => {
				write!(f, "General({:?}, {:?})", ext_version, tx_ext)
			},
		}
	}
}

/// An extrinsic right from the external world. This is unchecked and so can contain a signature.
///
/// An extrinsic is formally described as any external data that is originating from the outside of
/// the runtime and fed into the runtime as a part of the block-body.
///
/// Inherents are special types of extrinsics that are placed into the block by the block-builder.
/// They are unsigned because the assertion is that they are "inherently true" by virtue of getting
/// past all validators.
///
/// Transactions are all other statements provided by external entities that the chain deems values
/// and decided to include in the block. This value is typically in the form of fee payment, but it
/// could in principle be any other interaction. Transactions are either signed or unsigned. A
/// sensible transaction pool should ensure that only transactions that are worthwhile are
/// considered for block-building.
#[cfg_attr(all(feature = "std", not(windows)), doc = simple_mermaid::mermaid!("../../docs/mermaid/extrinsics.mmd"))]
/// This type is by no means enforced within Substrate, but given its genericness, it is highly
/// likely that for most use-cases it will suffice. Thus, the encoding of this type will dictate
/// exactly what bytes should be sent to a runtime to transact with it.
///
/// This can be checked using [`Checkable`], yielding a [`CheckedExtrinsic`], which is the
/// counterpart of this type after its signature (and other non-negotiable validity checks) have
/// passed.
#[derive(DecodeWithMemTracking, Eq, Clone)]
#[codec(decode_with_mem_tracking_bound(
	Address: DecodeWithMemTracking,
	Call: DecodeWithMemTracking,
	Signature: DecodeWithMemTracking,
	Extension: DecodeWithMemTracking)
)]
pub struct UncheckedExtrinsic<
	Address,
	Call,
	Signature,
	Extension,
	const MAX_CALL_SIZE: usize = DEFAULT_MAX_CALL_SIZE,
> {
	/// Information regarding the type of extrinsic this is (inherent or transaction) as well as
	/// associated extension (`Extension`) data if it's a transaction and a possible signature.
	pub preamble: Preamble<Address, Signature, Extension>,
	/// The function that should be called.
	pub function: Call,
	/// Stores the raw encoded call.
	///
	/// This is mainly interesting if this extrinsic was created by decoding it from bytes. In this
	/// case this field should be set to `Some` holding the original bytes used to decode the
	/// [`Self::function`]. This is done to protect against decode implementations of `Call` that
	/// are not bijective (encodes to the exact same bytes it was encoded from). If this `field`
	/// is set, it is being used when re-encoding this transaction.
	pub encoded_call: Option<Vec<u8>>,
}

impl<
		Address: Debug,
		Call: Debug,
		Signature: Debug,
		Extension: Debug,
		const MAX_CALL_SIZE: usize,
	> Debug for UncheckedExtrinsic<Address, Call, Signature, Extension, MAX_CALL_SIZE>
{
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		f.debug_struct("UncheckedExtrinsic")
			.field("preamble", &self.preamble)
			.field("function", &self.function)
			.finish()
	}
}

impl<
		Address: PartialEq,
		Call: PartialEq,
		Signature: PartialEq,
		Extension: PartialEq,
		const MAX_CALL_SIZE: usize,
	> PartialEq for UncheckedExtrinsic<Address, Call, Signature, Extension, MAX_CALL_SIZE>
{
	fn eq(&self, other: &Self) -> bool {
		self.preamble == other.preamble && self.function == other.function
	}
}

/// Manual [`TypeInfo`] implementation because of custom encoding. The data is a valid encoded
/// `Vec<u8>`, but requires some logic to extract the signature and payload.
///
/// See [`UncheckedExtrinsic::encode`] and [`UncheckedExtrinsic::decode`].
impl<Address, Call, Signature, Extension> TypeInfo
	for UncheckedExtrinsic<Address, Call, Signature, Extension>
where
	Address: StaticTypeInfo,
	Call: StaticTypeInfo,
	Signature: StaticTypeInfo,
	Extension: StaticTypeInfo,
{
	type Identity = UncheckedExtrinsic<Address, Call, Signature, Extension>;

	fn type_info() -> Type {
		Type::builder()
			.path(Path::new("UncheckedExtrinsic", module_path!()))
			// Include the type parameter types, even though they are not used directly in any of
			// the described fields. These type definitions can be used by downstream consumers
			// to help construct the custom decoding from the opaque bytes (see below).
			.type_params(vec![
				TypeParameter::new("Address", Some(meta_type::<Address>())),
				TypeParameter::new("Call", Some(meta_type::<Call>())),
				TypeParameter::new("Signature", Some(meta_type::<Signature>())),
				TypeParameter::new("Extra", Some(meta_type::<Extension>())),
			])
			.docs(&["UncheckedExtrinsic raw bytes, requires custom decoding routine"])
			// Because of the custom encoding, we can only accurately describe the encoding as an
			// opaque `Vec<u8>`. Downstream consumers will need to manually implement the codec to
			// encode/decode the `signature` and `function` fields.
			.composite(Fields::unnamed().field(|f| f.ty::<Vec<u8>>()))
	}
}

impl<Address, Call, Signature, Extension, const MAX_CALL_SIZE: usize>
	UncheckedExtrinsic<Address, Call, Signature, Extension, MAX_CALL_SIZE>
{
	/// New instance of a bare (ne unsigned) extrinsic. This could be used for an inherent or an
	/// old-school "unsigned transaction" (which are new being deprecated in favour of general
	/// transactions).
	#[deprecated = "Use new_bare instead"]
	pub fn new_unsigned(function: Call) -> Self {
		Self::new_bare(function)
	}

	/// Returns `true` if this extrinsic instance is an inherent, `false`` otherwise.
	pub fn is_inherent(&self) -> bool {
		matches!(self.preamble, Preamble::Bare(_))
	}

	/// Returns `true` if this extrinsic instance is an old-school signed transaction, `false`
	/// otherwise.
	pub fn is_signed(&self) -> bool {
		matches!(self.preamble, Preamble::Signed(..))
	}

	/// Create an `UncheckedExtrinsic` from a `Preamble` and the actual `Call`.
	pub fn from_parts(function: Call, preamble: Preamble<Address, Signature, Extension>) -> Self {
		Self { preamble, function, encoded_call: None }
	}

	/// New instance of a bare (ne unsigned) extrinsic.
	pub fn new_bare(function: Call) -> Self {
		Self::from_parts(function, Preamble::Bare(EXTRINSIC_FORMAT_VERSION))
	}

	/// New instance of a bare (ne unsigned) extrinsic on extrinsic format version 4.
	pub fn new_bare_legacy(function: Call) -> Self {
		Self::from_parts(function, Preamble::Bare(LEGACY_EXTRINSIC_FORMAT_VERSION))
	}

	/// New instance of an old-school signed transaction on extrinsic format version 4.
	pub fn new_signed(
		function: Call,
		signed: Address,
		signature: Signature,
		tx_ext: Extension,
	) -> Self {
		Self::from_parts(function, Preamble::Signed(signed, signature, tx_ext))
	}

	/// New instance of a new-school unsigned transaction.
	pub fn new_transaction(function: Call, tx_ext: Extension) -> Self {
		Self::from_parts(function, Preamble::General(EXTENSION_VERSION, tx_ext))
	}

	fn decode_with_len<I: Input>(input: &mut I, len: usize) -> Result<Self, codec::Error>
	where
		Preamble<Address, Signature, Extension>: Decode,
		Call: DecodeWithMemTracking,
	{
		let mut input = CountedInput::new(input);

		let preamble = Decode::decode(&mut input)?;

		struct CloneBytes<'a, I>(&'a mut I, Vec<u8>);
		impl<I: Input> Input for CloneBytes<'_, I> {
			fn remaining_len(&mut self) -> Result<Option<usize>, codec::Error> {
				self.0.remaining_len()
			}

			fn read(&mut self, into: &mut [u8]) -> Result<(), codec::Error> {
				self.0.read(into)?;

				self.1.extend_from_slice(into);
				Ok(())
			}

			fn descend_ref(&mut self) -> Result<(), codec::Error> {
				self.0.descend_ref()
			}

			fn ascend_ref(&mut self) {
				self.0.ascend_ref();
			}

			fn on_before_alloc_mem(&mut self, size: usize) -> Result<(), codec::Error> {
				self.0.on_before_alloc_mem(size)
			}
		}

		let mut clone_bytes = CloneBytes(&mut input, Vec::new());

		// Adds 1 byte to the `MAX_CALL_SIZE` as the decoding fails exactly at the given value and
		// the maximum should be allowed to fit in.
		let function =
			Call::decode_with_mem_limit(&mut clone_bytes, MAX_CALL_SIZE.saturating_add(1))?;

		let encoded_call = Some(clone_bytes.1);

		if input.count() != len as u64 {
			return Err("Invalid length prefix".into());
		}

		Ok(Self { preamble, function, encoded_call })
	}

	fn encode_without_prefix(&self) -> Vec<u8>
	where
		Preamble<Address, Signature, Extension>: Encode,
		Call: Encode,
	{
		let mut encoded = self.preamble.encode();

		match &self.encoded_call {
			Some(call) => {
				encoded.extend(call);
			},
			None => {
				self.function.encode_to(&mut encoded);
			},
		}

		encoded
	}
}

impl<Address, Call, Signature, Extension> ExtrinsicLike
	for UncheckedExtrinsic<Address, Call, Signature, Extension>
{
	fn is_signed(&self) -> Option<bool> {
		Some(matches!(self.preamble, Preamble::Signed(..)))
	}

	fn is_bare(&self) -> bool {
		matches!(self.preamble, Preamble::Bare(_))
	}
}

impl<Address, Call, Signature, Extra> ExtrinsicCall
	for UncheckedExtrinsic<Address, Call, Signature, Extra>
{
	type Call = Call;

	fn call(&self) -> &Call {
		&self.function
	}

	fn into_call(self) -> Self::Call {
		self.function
	}
}

// TODO: Migrate existing extension pipelines to support current `Signed` transactions as `General`
// transactions by adding an extension to validate signatures, as they are currently validated in
// the `Checkable` implementation for `Signed` transactions.

impl<LookupSource, AccountId, Call, Signature, Extension, Lookup> Checkable<Lookup>
	for UncheckedExtrinsic<LookupSource, Call, Signature, Extension>
where
	LookupSource: Member + MaybeDisplay,
	Call: Encode + Member + Dispatchable,
	Signature: Member + traits::Verify,
	<Signature as traits::Verify>::Signer: IdentifyAccount<AccountId = AccountId>,
	Extension: Encode + TransactionExtension<Call>,
	AccountId: Member + MaybeDisplay,
	Lookup: traits::Lookup<Source = LookupSource, Target = AccountId>,
{
	type Checked = CheckedExtrinsic<AccountId, Call, Extension>;

	fn check(self, lookup: &Lookup) -> Result<Self::Checked, TransactionValidityError> {
		Ok(match self.preamble {
			Preamble::Signed(signed, signature, tx_ext) => {
				let signed = lookup.lookup(signed)?;
				// The `Implicit` is "implicitly" included in the payload.
				let raw_payload = SignedPayload::new(
					CallAndMaybeEncoded { encoded: self.encoded_call, call: self.function },
					tx_ext,
				)?;
				if !raw_payload.using_encoded(|payload| signature.verify(payload, &signed)) {
					return Err(InvalidTransaction::BadProof.into());
				}
				let (function, tx_ext, _) = raw_payload.deconstruct();
				CheckedExtrinsic { format: ExtrinsicFormat::Signed(signed, tx_ext), function }
			},
			Preamble::General(extension_version, tx_ext) => CheckedExtrinsic {
				format: ExtrinsicFormat::General(extension_version, tx_ext),
				function: self.function,
			},
			Preamble::Bare(_) => {
				CheckedExtrinsic { format: ExtrinsicFormat::Bare, function: self.function }
			},
		})
	}

	#[cfg(feature = "try-runtime")]
	fn unchecked_into_checked_i_know_what_i_am_doing(
		self,
		lookup: &Lookup,
	) -> Result<Self::Checked, TransactionValidityError> {
		Ok(match self.preamble {
			Preamble::Signed(signed, _, tx_ext) => {
				let signed = lookup.lookup(signed)?;
				CheckedExtrinsic {
					format: ExtrinsicFormat::Signed(signed, tx_ext),
					function: self.function,
				}
			},
			Preamble::General(extension_version, tx_ext) => CheckedExtrinsic {
				format: ExtrinsicFormat::General(extension_version, tx_ext),
				function: self.function,
			},
			Preamble::Bare(_) => {
				CheckedExtrinsic { format: ExtrinsicFormat::Bare, function: self.function }
			},
		})
	}
}

impl<Address, Call: Dispatchable, Signature, Extension: TransactionExtension<Call>>
	ExtrinsicMetadata for UncheckedExtrinsic<Address, Call, Signature, Extension>
{
	const VERSIONS: &'static [u8] = &[LEGACY_EXTRINSIC_FORMAT_VERSION, EXTRINSIC_FORMAT_VERSION];
	type TransactionExtensions = Extension;
}

impl<Address, Call: Dispatchable, Signature, Extension: TransactionExtension<Call>>
	UncheckedExtrinsic<Address, Call, Signature, 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.preamble {
			Preamble::Bare(_) => Weight::zero(),
			Preamble::Signed(_, _, ext) | Preamble::General(_, ext) => ext.weight(&self.function),
		}
	}
}

impl<Address, Call, Signature, Extension, const MAX_CALL_SIZE: usize> Decode
	for UncheckedExtrinsic<Address, Call, Signature, Extension, MAX_CALL_SIZE>
where
	Address: Decode,
	Signature: Decode,
	Call: DecodeWithMemTracking,
	Extension: Decode,
{
	fn decode<I: Input>(input: &mut I) -> Result<Self, codec::Error> {
		// This is a little more complicated than usual since the binary format must be compatible
		// with SCALE's generic `Vec<u8>` type. Basically this just means accepting that there
		// will be a prefix of vector length.
		let expected_length: Compact<u32> = Decode::decode(input)?;

		Self::decode_with_len(input, expected_length.0 as usize)
	}
}

#[docify::export(unchecked_extrinsic_encode_impl)]
impl<Address, Call, Signature, Extension> Encode
	for UncheckedExtrinsic<Address, Call, Signature, Extension>
where
	Preamble<Address, Signature, Extension>: Encode,
	Call: Encode,
	Extension: Encode,
{
	fn encode(&self) -> Vec<u8> {
		let tmp = self.encode_without_prefix();

		let compact_len = codec::Compact::<u32>(tmp.len() as u32);

		// Allocate the output buffer with the correct length
		let mut output = Vec::with_capacity(compact_len.size_hint() + tmp.len());

		compact_len.encode_to(&mut output);
		output.extend(tmp);

		output
	}
}

impl<Address, Call, Signature, Extension> EncodeLike
	for UncheckedExtrinsic<Address, Call, Signature, Extension>
where
	Address: Encode,
	Signature: Encode,
	Call: Encode + Dispatchable,
	Extension: TransactionExtension<Call>,
{
}

#[cfg(feature = "serde")]
impl<Address: Encode, Signature: Encode, Call: Encode, Extension: Encode> serde::Serialize
	for UncheckedExtrinsic<Address, Call, Signature, Extension>
{
	fn serialize<S>(&self, seq: S) -> Result<S::Ok, S::Error>
	where
		S: ::serde::Serializer,
	{
		self.using_encoded(|bytes| seq.serialize_bytes(bytes))
	}
}

#[cfg(feature = "serde")]
impl<'a, Address: Decode, Signature: Decode, Call: DecodeWithMemTracking, Extension: Decode>
	serde::Deserialize<'a> for UncheckedExtrinsic<Address, Call, Signature, Extension>
{
	fn deserialize<D>(de: D) -> Result<Self, D::Error>
	where
		D: serde::Deserializer<'a>,
	{
		let r = sp_core::bytes::deserialize(de)?;
		Self::decode(&mut &r[..])
			.map_err(|e| serde::de::Error::custom(format!("Decode error: {}", e)))
	}
}

/// Something which holds the actual call and maybe its encoded form.
pub struct CallAndMaybeEncoded<T> {
	encoded: Option<Vec<u8>>,
	call: T,
}

impl<T> CallAndMaybeEncoded<T> {
	/// Converts `self` into the underlying call.
	pub fn into_call(self) -> T {
		self.call
	}
}

impl<T> From<T> for CallAndMaybeEncoded<T> {
	fn from(value: T) -> Self {
		Self { call: value, encoded: None }
	}
}

impl<T: Encode> Encode for CallAndMaybeEncoded<T> {
	fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
		match &self.encoded {
			Some(enc) => f(&enc),
			None => self.call.using_encoded(f),
		}
	}
}

/// A payload that has been signed for an unchecked extrinsics.
///
/// Note that the payload that we sign to produce unchecked extrinsic signature
/// is going to be different than the `SignaturePayload` - so the thing the extrinsic
/// actually contains.
pub struct SignedPayload<Call: Dispatchable, Extension: TransactionExtension<Call>>(
	(CallAndMaybeEncoded<Call>, Extension, Extension::Implicit),
);

impl<Call, Extension> SignedPayload<Call, Extension>
where
	Call: Encode + Dispatchable,
	Extension: TransactionExtension<Call>,
{
	/// Create new `SignedPayload` for extrinsic format version 4.
	///
	/// This function may fail if `implicit` of `Extension` is not available.
	pub fn new(
		call: impl Into<CallAndMaybeEncoded<Call>>,
		tx_ext: Extension,
	) -> Result<Self, TransactionValidityError> {
		let implicit = Extension::implicit(&tx_ext)?;
		Ok(Self((call.into(), tx_ext, implicit)))
	}

	/// Create new `SignedPayload` from raw components.
	pub fn from_raw(
		call: impl Into<CallAndMaybeEncoded<Call>>,
		tx_ext: Extension,
		implicit: Extension::Implicit,
	) -> Self {
		Self((call.into(), tx_ext, implicit))
	}

	/// Deconstruct the payload into it's components.
	pub fn deconstruct(self) -> (Call, Extension, Extension::Implicit) {
		let (call, ext, implicit) = self.0;
		(call.call, ext, implicit)
	}
}

impl<Call, Extension> Encode for SignedPayload<Call, Extension>
where
	Call: Encode + Dispatchable,
	Extension: TransactionExtension<Call>,
{
	/// Get an encoded version of this `blake2_256`-hashed payload.
	fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
		self.0.using_encoded(|payload| {
			if payload.len() > 256 {
				f(&blake2_256(payload)[..])
			} else {
				f(payload)
			}
		})
	}
}

impl<Call, Extension> EncodeLike for SignedPayload<Call, Extension>
where
	Call: Encode + Dispatchable,
	Extension: TransactionExtension<Call>,
{
}

impl<Address, Call, Signature, Extension>
	From<UncheckedExtrinsic<Address, Call, Signature, Extension>> for OpaqueExtrinsic
where
	Preamble<Address, Signature, Extension>: Encode,
	Call: Encode,
{
	fn from(extrinsic: UncheckedExtrinsic<Address, Call, Signature, Extension>) -> Self {
		Self::from_blob(extrinsic.encode_without_prefix())
	}
}

impl<Address, Call, Signature, Extension, const MAX_CALL_SIZE: usize> LazyExtrinsic
	for UncheckedExtrinsic<Address, Call, Signature, Extension, MAX_CALL_SIZE>
where
	Preamble<Address, Signature, Extension>: Decode,
	Call: DecodeWithMemTracking,
{
	fn decode_unprefixed(data: &[u8]) -> Result<Self, codec::Error> {
		Self::decode_with_len(&mut &data[..], data.len())
	}
}

#[cfg(test)]
mod legacy {
	use codec::{Compact, Decode, Encode, EncodeLike, Error, Input};
	use scale_info::{
		build::Fields, meta_type, Path, StaticTypeInfo, Type, TypeInfo, TypeParameter,
	};

	pub type UncheckedSignaturePayloadV4<Address, Signature, Extra> = (Address, Signature, Extra);

	#[derive(PartialEq, Eq, Clone, Debug)]
	pub struct UncheckedExtrinsicV4<Address, Call, Signature, Extra> {
		pub signature: Option<UncheckedSignaturePayloadV4<Address, Signature, Extra>>,
		pub function: Call,
	}

	impl<Address, Call, Signature, Extra> TypeInfo
		for UncheckedExtrinsicV4<Address, Call, Signature, Extra>
	where
		Address: StaticTypeInfo,
		Call: StaticTypeInfo,
		Signature: StaticTypeInfo,
		Extra: StaticTypeInfo,
	{
		type Identity = UncheckedExtrinsicV4<Address, Call, Signature, Extra>;

		fn type_info() -> Type {
			Type::builder()
				.path(Path::new("UncheckedExtrinsic", module_path!()))
				// Include the type parameter types, even though they are not used directly in any
				// of the described fields. These type definitions can be used by downstream
				// consumers to help construct the custom decoding from the opaque bytes (see
				// below).
				.type_params(vec![
					TypeParameter::new("Address", Some(meta_type::<Address>())),
					TypeParameter::new("Call", Some(meta_type::<Call>())),
					TypeParameter::new("Signature", Some(meta_type::<Signature>())),
					TypeParameter::new("Extra", Some(meta_type::<Extra>())),
				])
				.docs(&["OldUncheckedExtrinsic raw bytes, requires custom decoding routine"])
				// Because of the custom encoding, we can only accurately describe the encoding as
				// an opaque `Vec<u8>`. Downstream consumers will need to manually implement the
				// codec to encode/decode the `signature` and `function` fields.
				.composite(Fields::unnamed().field(|f| f.ty::<Vec<u8>>()))
		}
	}

	impl<Address, Call, Signature, Extra> UncheckedExtrinsicV4<Address, Call, Signature, Extra> {
		pub fn new_signed(
			function: Call,
			signed: Address,
			signature: Signature,
			extra: Extra,
		) -> Self {
			Self { signature: Some((signed, signature, extra)), function }
		}

		pub fn new_unsigned(function: Call) -> Self {
			Self { signature: None, function }
		}
	}

	impl<Address, Call, Signature, Extra> Decode
		for UncheckedExtrinsicV4<Address, Call, Signature, Extra>
	where
		Address: Decode,
		Signature: Decode,
		Call: Decode,
		Extra: Decode,
	{
		fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
			// This is a little more complicated than usual since the binary format must be
			// compatible with SCALE's generic `Vec<u8>` type. Basically this just means accepting
			// that there will be a prefix of vector length.
			let expected_length: Compact<u32> = Decode::decode(input)?;
			let before_length = input.remaining_len()?;

			let version = input.read_byte()?;

			let is_signed = version & 0b1000_0000 != 0;
			let version = version & 0b0111_1111;
			if version != 4u8 {
				return Err("Invalid transaction version".into());
			}

			let signature = is_signed.then(|| Decode::decode(input)).transpose()?;
			let function = Decode::decode(input)?;

			if let Some((before_length, after_length)) =
				input.remaining_len()?.and_then(|a| before_length.map(|b| (b, a)))
			{
				let length = before_length.saturating_sub(after_length);

				if length != expected_length.0 as usize {
					return Err("Invalid length prefix".into());
				}
			}

			Ok(Self { signature, function })
		}
	}

	#[docify::export(unchecked_extrinsic_encode_impl)]
	impl<Address, Call, Signature, Extra> Encode
		for UncheckedExtrinsicV4<Address, Call, Signature, Extra>
	where
		Address: Encode,
		Signature: Encode,
		Call: Encode,
		Extra: Encode,
	{
		fn encode(&self) -> Vec<u8> {
			let mut tmp = Vec::with_capacity(core::mem::size_of::<Self>());

			// 1 byte version id.
			match self.signature.as_ref() {
				Some(s) => {
					tmp.push(4u8 | 0b1000_0000);
					s.encode_to(&mut tmp);
				},
				None => {
					tmp.push(4u8 & 0b0111_1111);
				},
			}
			self.function.encode_to(&mut tmp);

			let compact_len = codec::Compact::<u32>(tmp.len() as u32);

			// Allocate the output buffer with the correct length
			let mut output = Vec::with_capacity(compact_len.size_hint() + tmp.len());

			compact_len.encode_to(&mut output);
			output.extend(tmp);

			output
		}
	}

	impl<Address, Call, Signature, Extra> EncodeLike
		for UncheckedExtrinsicV4<Address, Call, Signature, Extra>
	where
		Address: Encode,
		Signature: Encode,
		Call: Encode,
		Extra: Encode,
	{
	}
}

#[cfg(test)]
mod tests {
	use super::{legacy::UncheckedExtrinsicV4, *};
	use crate::{
		codec::{Decode, Encode},
		impl_tx_ext_default,
		testing::TestSignature as TestSig,
		traits::{FakeDispatchable, IdentityLookup, TransactionExtension},
	};
	use sp_io::hashing::blake2_256;

	type TestContext = IdentityLookup<u64>;
	type TestAccountId = u64;

	// Custom Call enum that supports sorting on decode
	#[derive(Debug, Clone, Eq, PartialEq, Encode, TypeInfo)]
	enum Call {
		Raw(Vec<u8>),
		Sort(Vec<u8>),
	}

	impl Decode for Call {
		fn decode<I: Input>(input: &mut I) -> Result<Self, codec::Error> {
			let variant = input.read_byte()?;
			match variant {
				0 => {
					let data = Vec::<u8>::decode(input)?;
					Ok(Call::Raw(data))
				},
				1 => {
					let mut data = Vec::<u8>::decode(input)?;
					// Sort the data on decode
					data.sort();
					Ok(Call::Sort(data))
				},
				_ => Err("Invalid Call variant".into()),
			}
		}
	}

	impl DecodeWithMemTracking for Call {}

	impl From<Call> for Vec<u8> {
		fn from(call: Call) -> Vec<u8> {
			match call {
				Call::Sort(data) | Call::Raw(data) => data,
			}
		}
	}

	impl From<Vec<u8>> for FakeDispatchable<Call> {
		fn from(value: Vec<u8>) -> Self {
			Self(Call::Raw(value))
		}
	}

	type TestCall = FakeDispatchable<Call>;

	const TEST_ACCOUNT: TestAccountId = 0;

	// NOTE: this is demonstration. One can simply use `()` for testing.
	#[derive(
		Debug,
		Encode,
		Decode,
		DecodeWithMemTracking,
		Clone,
		Eq,
		PartialEq,
		Ord,
		PartialOrd,
		TypeInfo,
	)]
	struct DummyExtension;
	impl TransactionExtension<TestCall> for DummyExtension {
		const IDENTIFIER: &'static str = "DummyExtension";
		type Implicit = ();
		type Val = ();
		type Pre = ();
		impl_tx_ext_default!(TestCall; weight validate prepare);
	}

	type Ex = UncheckedExtrinsic<TestAccountId, TestCall, TestSig, DummyExtension>;
	type CEx = CheckedExtrinsic<TestAccountId, TestCall, DummyExtension>;

	#[test]
	fn unsigned_codec_should_work() {
		let call: TestCall = Call::Raw(vec![0u8; 0]).into();
		let ux = Ex::new_bare(call);
		let encoded = ux.encode();
		assert_eq!(Ex::decode(&mut &encoded[..]), Ok(ux));
	}

	#[test]
	fn invalid_length_prefix_is_detected() {
		let ux = Ex::new_bare(Call::Raw(vec![0u8; 0]).into());
		let mut encoded = ux.encode();

		let length = Compact::<u32>::decode(&mut &encoded[..]).unwrap();
		Compact(length.0 + 10).encode_to(&mut &mut encoded[..1]);

		assert_eq!(Ex::decode(&mut &encoded[..]), Err("Invalid length prefix".into()));
	}

	#[test]
	fn transaction_codec_should_work() {
		let ux = Ex::new_transaction(vec![0u8; 0].into(), DummyExtension);
		let encoded = ux.encode();
		assert_eq!(Ex::decode(&mut &encoded[..]), Ok(ux));
	}

	#[test]
	fn signed_codec_should_work() {
		let ux = Ex::new_signed(
			vec![0u8; 0].into(),
			TEST_ACCOUNT,
			TestSig(TEST_ACCOUNT, (vec![0u8; 0], DummyExtension).encode()),
			DummyExtension,
		);
		let encoded = ux.encode();
		assert_eq!(Ex::decode(&mut &encoded[..]), Ok(ux));
	}

	#[test]
	fn large_signed_codec_should_work() {
		let ux = Ex::new_signed(
			vec![0u8; 0].into(),
			TEST_ACCOUNT,
			TestSig(
				TEST_ACCOUNT,
				(vec![0u8; 257], DummyExtension).using_encoded(blake2_256)[..].to_owned(),
			),
			DummyExtension,
		);
		let encoded = ux.encode();
		assert_eq!(Ex::decode(&mut &encoded[..]), Ok(ux));
	}

	#[test]
	fn unsigned_check_should_work() {
		let ux = Ex::new_bare(vec![0u8; 0].into());
		assert!(ux.is_inherent());
		assert_eq!(
			<Ex as Checkable<TestContext>>::check(ux, &Default::default()),
			Ok(CEx { format: ExtrinsicFormat::Bare, function: vec![0u8; 0].into() }),
		);
	}

	#[test]
	fn badly_signed_check_should_fail() {
		let ux = Ex::new_signed(
			vec![0u8; 0].into(),
			TEST_ACCOUNT,
			TestSig(TEST_ACCOUNT, vec![0u8; 0].into()),
			DummyExtension,
		);
		assert!(!ux.is_inherent());
		assert_eq!(
			<Ex as Checkable<TestContext>>::check(ux, &Default::default()),
			Err(InvalidTransaction::BadProof.into()),
		);
	}

	#[test]
	fn transaction_check_should_work() {
		let ux = Ex::new_transaction(vec![0u8; 0].into(), DummyExtension);
		assert!(!ux.is_inherent());
		assert_eq!(
			<Ex as Checkable<TestContext>>::check(ux, &Default::default()),
			Ok(CEx {
				format: ExtrinsicFormat::General(0, DummyExtension),
				function: vec![0u8; 0].into()
			}),
		);
	}

	#[test]
	fn signed_check_should_work() {
		let sig_payload = SignedPayload::from_raw(
			FakeDispatchable::from(vec![0u8; 0]),
			DummyExtension,
			DummyExtension.implicit().unwrap(),
		);
		let ux = Ex::new_signed(
			vec![0u8; 0].into(),
			TEST_ACCOUNT,
			TestSig(TEST_ACCOUNT, sig_payload.encode()),
			DummyExtension,
		);
		assert!(!ux.is_inherent());
		assert_eq!(
			<Ex as Checkable<TestContext>>::check(ux, &Default::default()),
			Ok(CEx {
				format: ExtrinsicFormat::Signed(TEST_ACCOUNT, DummyExtension),
				function: Call::Raw(vec![0u8; 0]).into()
			}),
		);
	}

	#[test]
	fn encoding_matches_vec() {
		let ex = Ex::new_bare(Call::Raw(vec![0u8; 0]).into());
		let encoded = ex.encode();
		let decoded = Ex::decode(&mut encoded.as_slice()).unwrap();
		assert_eq!(decoded, ex);
		let as_vec: Vec<u8> = Decode::decode(&mut encoded.as_slice()).unwrap();
		assert_eq!(as_vec.encode(), encoded);
	}

	#[test]
	fn conversion_to_opaque() {
		let ux = Ex::new_bare(Call::Raw(vec![0u8; 0]).into());
		let encoded = ux.encode();
		let opaque: OpaqueExtrinsic = ux.into();
		let opaque_encoded = opaque.encode();
		assert_eq!(opaque_encoded, encoded);
	}

	#[test]
	fn large_bad_prefix_should_work() {
		let encoded = (Compact::<u32>::from(u32::MAX), Preamble::<(), (), ()>::Bare(0)).encode();
		assert!(Ex::decode(&mut &encoded[..]).is_err());
	}

	#[test]
	fn legacy_short_signed_encode_decode() {
		let call: TestCall = Call::Raw(vec![0u8; 4]).into();
		let signed = TEST_ACCOUNT;
		let extension = DummyExtension;
		let implicit = extension.implicit().unwrap();
		let legacy_signature = TestSig(TEST_ACCOUNT, (&call, &extension, &implicit).encode());

		let old_ux =
			UncheckedExtrinsicV4::<TestAccountId, TestCall, TestSig, DummyExtension>::new_signed(
				call.clone(),
				signed,
				legacy_signature.clone(),
				extension.clone(),
			);

		let encoded_old_ux = old_ux.encode();
		let decoded_old_ux = Ex::decode(&mut &encoded_old_ux[..]).unwrap();

		assert_eq!(decoded_old_ux.function, call);
		assert_eq!(
			decoded_old_ux.preamble,
			Preamble::Signed(signed, legacy_signature.clone(), extension.clone())
		);

		let new_ux =
			Ex::new_signed(call.clone(), signed, legacy_signature.clone(), extension.clone());

		let new_checked = new_ux.check(&IdentityLookup::<TestAccountId>::default()).unwrap();
		let old_checked =
			decoded_old_ux.check(&IdentityLookup::<TestAccountId>::default()).unwrap();
		assert_eq!(new_checked, old_checked);
	}

	#[test]
	fn legacy_long_signed_encode_decode() {
		let call: TestCall = Call::Raw(vec![0u8; 257]).into();
		let signed = TEST_ACCOUNT;
		let extension = DummyExtension;
		let implicit = extension.implicit().unwrap();
		let signature = TestSig(
			TEST_ACCOUNT,
			blake2_256(&(&call, DummyExtension, &implicit).encode()[..]).to_vec(),
		);

		let old_ux =
			UncheckedExtrinsicV4::<TestAccountId, TestCall, TestSig, DummyExtension>::new_signed(
				call.clone(),
				signed,
				signature.clone(),
				extension.clone(),
			);

		let encoded_old_ux = old_ux.encode();
		let decoded_old_ux = Ex::decode(&mut &encoded_old_ux[..]).unwrap();

		assert_eq!(decoded_old_ux.function, call);
		assert_eq!(
			decoded_old_ux.preamble,
			Preamble::Signed(signed, signature.clone(), extension.clone())
		);

		let new_ux = Ex::new_signed(call.clone(), signed, signature.clone(), extension.clone());

		let new_checked = new_ux.check(&IdentityLookup::<TestAccountId>::default()).unwrap();
		let old_checked =
			decoded_old_ux.check(&IdentityLookup::<TestAccountId>::default()).unwrap();
		assert_eq!(new_checked, old_checked);
	}

	#[test]
	fn legacy_unsigned_encode_decode() {
		let call: TestCall = Call::Raw(vec![0u8; 0]).into();

		let old_ux =
			UncheckedExtrinsicV4::<TestAccountId, TestCall, TestSig, DummyExtension>::new_unsigned(
				call.clone(),
			);

		let encoded_old_ux = old_ux.encode();
		let decoded_old_ux = Ex::decode(&mut &encoded_old_ux[..]).unwrap();

		assert_eq!(decoded_old_ux.function, call);
		assert_eq!(decoded_old_ux.preamble, Preamble::Bare(LEGACY_EXTRINSIC_FORMAT_VERSION));

		let new_legacy_ux = Ex::new_bare_legacy(call.clone());
		assert_eq!(encoded_old_ux, new_legacy_ux.encode());

		let new_ux = Ex::new_bare(call.clone());
		let encoded_new_ux = new_ux.encode();
		let decoded_new_ux = Ex::decode(&mut &encoded_new_ux[..]).unwrap();
		assert_eq!(new_ux, decoded_new_ux);

		let new_checked = new_ux.check(&IdentityLookup::<TestAccountId>::default()).unwrap();
		let old_checked =
			decoded_old_ux.check(&IdentityLookup::<TestAccountId>::default()).unwrap();
		assert_eq!(new_checked, old_checked);
	}

	#[test]
	fn max_call_heap_size_should_be_checked() {
		// Should be able to decode an `UncheckedExtrinsic` that contains a call with
		// heap size < `MAX_CALL_HEAP_SIZE`
		let ux = Ex::new_bare(Call::Raw(vec![0u8; DEFAULT_MAX_CALL_SIZE]).into());
		let encoded = ux.encode();
		assert_eq!(Ex::decode(&mut &encoded[..]), Ok(ux));

		// Otherwise should fail
		let ux = Ex::new_bare(Call::Raw(vec![0u8; DEFAULT_MAX_CALL_SIZE + 1]).into());
		let encoded = ux.encode();
		assert_eq!(
			Ex::decode(&mut &encoded[..]).unwrap_err().to_string(),
			"Could not decode `FakeDispatchable.0`:\n\tHeap memory limit exceeded while decoding\n"
		);
	}

	/// Ensures that a decoded extrinsic encodes to the exact same encoded bytes from what it was
	/// decoded from.
	#[test]
	fn encoding_is_stable() {
		// Create a call with unsorted data
		let unsorted_data = vec![5u8, 3, 7, 1, 9, 2, 8, 4, 6, 0];
		let call = Call::Sort(unsorted_data.clone());

		let unsorted_encoded = call.encode();

		let sig_payload = SignedPayload::from_raw(
			FakeDispatchable::from(call.clone()),
			DummyExtension,
			DummyExtension.implicit().unwrap(),
		);
		let sig_payload_encoded = sig_payload.encode();

		let ux = Ex::new_signed(
			call.into(),
			TEST_ACCOUNT,
			TestSig(TEST_ACCOUNT, sig_payload_encoded.clone()),
			DummyExtension,
		);

		// Encode and decode the extrinsic
		// During decode, the Sort variant will sort the data
		let encoded = ux.encode();
		let decoded_ux = Ex::decode(&mut &encoded[..]).unwrap();

		// The decoded call should have sorted data
		let mut expected_sorted_data = unsorted_data;
		expected_sorted_data.sort();

		let expected_decoded_call =
			FakeDispatchable::from(Call::Sort(expected_sorted_data.clone()));
		assert_eq!(decoded_ux.function, expected_decoded_call);

		// Verify that the decoded call encodes differently than the original
		let sorted_encoded = Call::Sort(expected_sorted_data).encode();
		assert_ne!(
			unsorted_encoded, sorted_encoded,
			"Sorted and unsorted should encode differently"
		);

		// Ensure that we can verify the signature successfully.
		assert_eq!(
			<Ex as Checkable<TestContext>>::check(decoded_ux, &Default::default())
				.unwrap()
				.function,
			expected_decoded_call,
		)
	}
}