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
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This file is part of Frontier.
//
// Copyright (c) 2019-2022 Moonsong Labs.
// Copyright (c) 2023 Parity Technologies (UK) Ltd.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! Provide utils to assemble precompiles and precompilesets into a
//! final precompile set with security checks. All security checks are enabled by
//! default and must be disabled explicely throught type annotations.

use crate::{
	evm::handle::PrecompileHandleExt,
	solidity::{codec::String, revert::revert},
	EvmResult,
};
use fp_evm::{
	ExitError, IsPrecompileResult, Precompile, PrecompileFailure, PrecompileHandle,
	PrecompileResult, PrecompileSet,
};
use frame_support::pallet_prelude::Get;
use impl_trait_for_tuples::impl_for_tuples;
use pallet_evm::AddressMapping;
use sp_core::{H160, H256};
use sp_std::{
	cell::RefCell, collections::btree_map::BTreeMap, marker::PhantomData, ops::RangeInclusive, vec,
	vec::Vec,
};

/// Trait representing checks that can be made on a precompile call.
/// Types implementing this trait are made to be chained in a tuple.
///
/// For that reason every method returns an Option, None meaning that
/// the implementor have no constraint and the decision is left to
/// latter elements in the chain. If None is returned by all elements of
/// the chain then sensible defaults are used.
///
/// Both `PrecompileAt` and `PrecompileSetStartingWith` have a type parameter that must
/// implement this trait to configure the checks of the precompile(set) it represents.
pub trait PrecompileChecks {
	#[inline(always)]
	/// Is there a limit to the amount of recursions this precompile
	/// can make using subcalls? 0 means this specific precompile will not
	/// be callable as a subcall of itself, 1 will allow one level of recursion,
	/// etc...
	///
	/// If all checks return None, defaults to `Some(0)` (no recursion allowed).
	fn recursion_limit() -> Option<Option<u16>> {
		None
	}

	#[inline(always)]
	/// Does this precompile supports being called with DELEGATECALL or CALLCODE?
	///
	/// If all checks return None, defaults to `false`.
	fn accept_delegate_call() -> Option<bool> {
		None
	}

	#[inline(always)]
	/// Is this precompile callable by a smart contract?
	///
	/// If all checks return None, defaults to `false`.
	fn callable_by_smart_contract(_caller: H160, _called_selector: Option<u32>) -> Option<bool> {
		None
	}

	#[inline(always)]
	/// Is this precompile callable by a precompile?
	///
	/// If all checks return None, defaults to `false`.
	fn callable_by_precompile(_caller: H160, _called_selector: Option<u32>) -> Option<bool> {
		None
	}

	#[inline(always)]
	/// Is this precompile able to do subcalls?
	///
	/// If all checks return None, defaults to `false`.
	fn allow_subcalls() -> Option<bool> {
		None
	}

	/// Summarize the checks when being called by a smart contract.
	fn callable_by_smart_contract_summary() -> Option<String> {
		None
	}

	/// Summarize the checks when being called by a precompile.
	fn callable_by_precompile_summary() -> Option<String> {
		None
	}
}

#[derive(Debug, Clone)]
pub enum DiscriminantResult<T> {
	Some(T, u64),
	None(u64),
	OutOfGas,
}

impl<T> From<DiscriminantResult<T>> for IsPrecompileResult {
	fn from(val: DiscriminantResult<T>) -> Self {
		match val {
			DiscriminantResult::<T>::Some(_, extra_cost) => IsPrecompileResult::Answer {
				is_precompile: true,
				extra_cost,
			},
			DiscriminantResult::<T>::None(extra_cost) => IsPrecompileResult::Answer {
				is_precompile: false,
				extra_cost,
			},
			DiscriminantResult::<T>::OutOfGas => IsPrecompileResult::OutOfGas,
		}
	}
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "testing", derive(serde::Serialize, serde::Deserialize))]
pub enum PrecompileKind {
	Single(H160),
	Prefixed(Vec<u8>),
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "testing", derive(serde::Serialize, serde::Deserialize))]
pub struct PrecompileCheckSummary {
	pub name: Option<String>,
	pub precompile_kind: PrecompileKind,
	pub recursion_limit: Option<u16>,
	pub accept_delegate_call: bool,
	pub callable_by_smart_contract: String,
	pub callable_by_precompile: String,
}

#[impl_for_tuples(0, 20)]
impl PrecompileChecks for Tuple {
	#[inline(always)]
	fn recursion_limit() -> Option<Option<u16>> {
		for_tuples!(#(
			if let Some(check) = Tuple::recursion_limit() {
				return Some(check);
			}
		)*);

		None
	}

	#[inline(always)]
	fn accept_delegate_call() -> Option<bool> {
		for_tuples!(#(
			if let Some(check) = Tuple::accept_delegate_call() {
				return Some(check);
			}
		)*);

		None
	}

	#[inline(always)]
	fn callable_by_smart_contract(caller: H160, called_selector: Option<u32>) -> Option<bool> {
		for_tuples!(#(
			if let Some(check) = Tuple::callable_by_smart_contract(caller, called_selector) {
				return Some(check);
			}
		)*);

		None
	}

	#[inline(always)]
	fn callable_by_precompile(caller: H160, called_selector: Option<u32>) -> Option<bool> {
		for_tuples!(#(
			if let Some(check) = Tuple::callable_by_precompile(caller, called_selector) {
				return Some(check);
			}
		)*);

		None
	}

	#[inline(always)]
	fn allow_subcalls() -> Option<bool> {
		for_tuples!(#(
			if let Some(check) = Tuple::allow_subcalls() {
				return Some(check);
			}
		)*);

		None
	}

	fn callable_by_smart_contract_summary() -> Option<String> {
		for_tuples!(#(
			if let Some(check) = Tuple::callable_by_smart_contract_summary() {
				return Some(check);
			}
		)*);

		None
	}

	fn callable_by_precompile_summary() -> Option<String> {
		for_tuples!(#(
			if let Some(check) = Tuple::callable_by_precompile_summary() {
				return Some(check);
			}
		)*);

		None
	}
}

/// Precompile can be called using DELEGATECALL/CALLCODE.
pub struct AcceptDelegateCall;

impl PrecompileChecks for AcceptDelegateCall {
	#[inline(always)]
	fn accept_delegate_call() -> Option<bool> {
		Some(true)
	}
}

/// Precompile is able to do subcalls with provided nesting limit.
pub struct SubcallWithMaxNesting<const R: u16>;

impl<const R: u16> PrecompileChecks for SubcallWithMaxNesting<R> {
	#[inline(always)]
	fn recursion_limit() -> Option<Option<u16>> {
		Some(Some(R))
	}

	#[inline(always)]
	fn allow_subcalls() -> Option<bool> {
		Some(true)
	}
}

pub trait SelectorFilter {
	fn is_allowed(_caller: H160, _selector: Option<u32>) -> bool;

	fn description() -> String;
}
pub struct ForAllSelectors;
impl SelectorFilter for ForAllSelectors {
	fn is_allowed(_caller: H160, _selector: Option<u32>) -> bool {
		true
	}

	fn description() -> String {
		"Allowed for all selectors and callers".into()
	}
}

pub struct OnlyFrom<T>(PhantomData<T>);
impl<T: Get<H160>> SelectorFilter for OnlyFrom<T> {
	fn is_allowed(caller: H160, _selector: Option<u32>) -> bool {
		caller == T::get()
	}

	fn description() -> String {
		alloc::format!("Allowed for all selectors only if called from {}", T::get())
	}
}

pub struct CallableByContract<T = ForAllSelectors>(PhantomData<T>);

impl<T: SelectorFilter> PrecompileChecks for CallableByContract<T> {
	#[inline(always)]
	fn callable_by_smart_contract(caller: H160, called_selector: Option<u32>) -> Option<bool> {
		Some(T::is_allowed(caller, called_selector))
	}

	fn callable_by_smart_contract_summary() -> Option<String> {
		Some(T::description())
	}
}

/// Precompiles are allowed to call this precompile.
pub struct CallableByPrecompile<T = ForAllSelectors>(PhantomData<T>);

impl<T: SelectorFilter> PrecompileChecks for CallableByPrecompile<T> {
	#[inline(always)]
	fn callable_by_precompile(caller: H160, called_selector: Option<u32>) -> Option<bool> {
		Some(T::is_allowed(caller, called_selector))
	}

	fn callable_by_precompile_summary() -> Option<String> {
		Some(T::description())
	}
}

/// The type of EVM address.
#[derive(PartialEq)]
#[cfg_attr(feature = "std", derive(Debug))]
pub enum AddressType {
	/// The code stored at the address is less than 5 bytes, but not well known.
	Unknown,
	/// No code is stored at the address, therefore is EOA.
	EOA,
	/// The 5-byte magic constant for a precompile is stored at the address.
	Precompile,
	/// The code is greater than 5-bytes, potentially a Smart Contract.
	Contract,
}

/// Retrieves the type of address demarcated by `AddressType`.
pub fn get_address_type<R: pallet_evm::Config>(
	handle: &mut impl PrecompileHandle,
	address: H160,
) -> Result<AddressType, ExitError> {
	// AccountCodesMetadata:
	// Blake2128(16) + H160(20) + CodeMetadata(40)
	handle.record_db_read::<R>(76)?;
	let code_len = pallet_evm::Pallet::<R>::account_code_metadata(address).size;

	// 0 => either EOA or precompile without dummy code
	if code_len == 0 {
		return Ok(AddressType::EOA);
	}

	// dummy code is 5 bytes long, so any other len means it is a contract.
	if code_len != 5 {
		return Ok(AddressType::Contract);
	}

	// check code matches dummy code
	handle.record_db_read::<R>(code_len as usize)?;
	let code = pallet_evm::AccountCodes::<R>::get(address);
	if code == [0x60, 0x00, 0x60, 0x00, 0xfd] {
		return Ok(AddressType::Precompile);
	}

	Ok(AddressType::Unknown)
}

fn is_address_eoa_or_precompile<R: pallet_evm::Config>(
	handle: &mut impl PrecompileHandle,
	address: H160,
) -> Result<bool, ExitError> {
	match get_address_type::<R>(handle, address)? {
		AddressType::EOA | AddressType::Precompile => Ok(true),
		_ => Ok(false),
	}
}

/// Common checks for precompile and precompile sets.
/// Don't contain recursion check as precompile sets have recursion check for each member.
fn common_checks<R: pallet_evm::Config, C: PrecompileChecks>(
	handle: &mut impl PrecompileHandle,
) -> EvmResult<()> {
	let code_address = handle.code_address();
	let caller = handle.context().caller;

	// Check DELEGATECALL config.
	let accept_delegate_call = C::accept_delegate_call().unwrap_or(false);
	if !accept_delegate_call && code_address != handle.context().address {
		return Err(revert("Cannot be called with DELEGATECALL or CALLCODE"));
	}

	// Extract which selector is called.
	let selector = handle.input().get(0..4).map(|bytes| {
		let mut buffer = [0u8; 4];
		buffer.copy_from_slice(bytes);
		u32::from_be_bytes(buffer)
	});

	// Is this selector callable from a smart contract?
	let callable_by_smart_contract =
		C::callable_by_smart_contract(caller, selector).unwrap_or(false);
	if !callable_by_smart_contract && !is_address_eoa_or_precompile::<R>(handle, caller)? {
		return Err(revert("Function not callable by smart contracts"));
	}

	// Is this selector callable from a precompile?
	let callable_by_precompile = C::callable_by_precompile(caller, selector).unwrap_or(false);
	if !callable_by_precompile && is_precompile_or_fail::<R>(caller, handle.remaining_gas())? {
		return Err(revert("Function not callable by precompiles"));
	}

	Ok(())
}

pub fn is_precompile_or_fail<R: pallet_evm::Config>(address: H160, gas: u64) -> EvmResult<bool> {
	match <R as pallet_evm::Config>::PrecompilesValue::get().is_precompile(address, gas) {
		IsPrecompileResult::Answer { is_precompile, .. } => Ok(is_precompile),
		IsPrecompileResult::OutOfGas => Err(PrecompileFailure::Error {
			exit_status: ExitError::OutOfGas,
		}),
	}
}

pub struct AddressU64<const N: u64>;
impl<const N: u64> Get<H160> for AddressU64<N> {
	#[inline(always)]
	fn get() -> H160 {
		H160::from_low_u64_be(N)
	}
}

pub struct RestrictiveHandle<'a, H> {
	handle: &'a mut H,
	allow_subcalls: bool,
}

impl<'a, H: PrecompileHandle> PrecompileHandle for RestrictiveHandle<'a, H> {
	fn call(
		&mut self,
		address: H160,
		transfer: Option<evm::Transfer>,
		input: Vec<u8>,
		target_gas: Option<u64>,
		is_static: bool,
		context: &evm::Context,
	) -> (evm::ExitReason, Vec<u8>) {
		if !self.allow_subcalls {
			return (
				evm::ExitReason::Revert(evm::ExitRevert::Reverted),
				crate::solidity::revert::revert_as_bytes("subcalls disabled for this precompile"),
			);
		}

		self.handle
			.call(address, transfer, input, target_gas, is_static, context)
	}

	fn record_cost(&mut self, cost: u64) -> Result<(), evm::ExitError> {
		self.handle.record_cost(cost)
	}

	fn remaining_gas(&self) -> u64 {
		self.handle.remaining_gas()
	}

	fn log(
		&mut self,
		address: H160,
		topics: Vec<H256>,
		data: Vec<u8>,
	) -> Result<(), evm::ExitError> {
		self.handle.log(address, topics, data)
	}

	fn code_address(&self) -> H160 {
		self.handle.code_address()
	}

	fn input(&self) -> &[u8] {
		self.handle.input()
	}

	fn context(&self) -> &evm::Context {
		self.handle.context()
	}

	fn is_static(&self) -> bool {
		self.handle.is_static()
	}

	fn gas_limit(&self) -> Option<u64> {
		self.handle.gas_limit()
	}

	fn record_external_cost(
		&mut self,
		ref_time: Option<u64>,
		proof_size: Option<u64>,
		storage_growth: Option<u64>,
	) -> Result<(), ExitError> {
		self.handle
			.record_external_cost(ref_time, proof_size, storage_growth)
	}

	fn refund_external_cost(&mut self, ref_time: Option<u64>, proof_size: Option<u64>) {
		self.handle.refund_external_cost(ref_time, proof_size)
	}
}

/// Allows to know if a precompile is active or not.
/// This allows to detect deactivated precompile, that are still considered precompiles by
/// the EVM but that will always revert when called.
pub trait IsActivePrecompile {
	/// Is the provided address an active precompile, a precompile that has
	/// not be deactivated. Note that a deactivated precompile is still considered a precompile
	/// for the EVM, but it will always revert when called.
	fn is_active_precompile(&self, address: H160, gas: u64) -> IsPrecompileResult;
}

// INDIVIDUAL PRECOMPILE(SET)

/// A fragment of a PrecompileSet. Should be implemented as is it
/// was a PrecompileSet containing only the precompile(set) it wraps.
/// They can be combined into a real PrecompileSet using `PrecompileSetBuilder`.
pub trait PrecompileSetFragment {
	/// Instanciate the fragment.
	fn new() -> Self;

	/// Execute the fragment.
	fn execute<R: pallet_evm::Config>(
		&self,
		handle: &mut impl PrecompileHandle,
	) -> Option<PrecompileResult>;

	/// Is the provided address a precompile in this fragment?
	fn is_precompile(&self, address: H160, gas: u64) -> IsPrecompileResult;

	/// Return the list of addresses covered by this fragment.
	fn used_addresses(&self) -> Vec<H160>;

	/// Summarize
	fn summarize_checks(&self) -> Vec<PrecompileCheckSummary>;
}

/// Wraps a stateless precompile: a type implementing the `Precompile` trait.
/// Type parameters allow to define:
/// - A: The address of the precompile
/// - R: The recursion limit (defaults to 1)
/// - D: If DELEGATECALL is supported (default to no)
pub struct PrecompileAt<A, P, C = ()> {
	current_recursion_level: RefCell<u16>,
	_phantom: PhantomData<(A, P, C)>,
}

impl<A, P, C> PrecompileSetFragment for PrecompileAt<A, P, C>
where
	A: Get<H160>,
	P: Precompile,
	C: PrecompileChecks,
{
	#[inline(always)]
	fn new() -> Self {
		Self {
			current_recursion_level: RefCell::new(0),
			_phantom: PhantomData,
		}
	}

	#[inline(always)]
	fn execute<R: pallet_evm::Config>(
		&self,
		handle: &mut impl PrecompileHandle,
	) -> Option<PrecompileResult> {
		let code_address = handle.code_address();

		// Check if this is the address of the precompile.
		if A::get() != code_address {
			return None;
		}

		// Perform common checks.
		if let Err(err) = common_checks::<R, C>(handle) {
			return Some(Err(err));
		}

		// Check and increase recursion level if needed.
		let recursion_limit = C::recursion_limit().unwrap_or(Some(0));
		if let Some(max_recursion_level) = recursion_limit {
			match self.current_recursion_level.try_borrow_mut() {
				Ok(mut recursion_level) => {
					if *recursion_level > max_recursion_level {
						return Some(Err(revert("Precompile is called with too high nesting")));
					}

					*recursion_level += 1;
				}
				// We don't hold the borrow and are in single-threaded code, thus we should
				// not be able to fail borrowing in nested calls.
				Err(_) => return Some(Err(revert("Couldn't check precompile nesting"))),
			}
		}

		// Subcall protection.
		let allow_subcalls = C::allow_subcalls().unwrap_or(false);
		let mut handle = RestrictiveHandle {
			handle,
			allow_subcalls,
		};

		let res = P::execute(&mut handle);

		// Decrease recursion level if needed.
		if recursion_limit.is_some() {
			match self.current_recursion_level.try_borrow_mut() {
				Ok(mut recursion_level) => {
					*recursion_level -= 1;
				}
				// We don't hold the borrow and are in single-threaded code, thus we should
				// not be able to fail borrowing in nested calls.
				Err(_) => return Some(Err(revert("Couldn't check precompile nesting"))),
			}
		}

		Some(res)
	}

	#[inline(always)]
	fn is_precompile(&self, address: H160, _gas: u64) -> IsPrecompileResult {
		IsPrecompileResult::Answer {
			is_precompile: address == A::get(),
			extra_cost: 0,
		}
	}

	#[inline(always)]
	fn used_addresses(&self) -> Vec<H160> {
		vec![A::get()]
	}

	fn summarize_checks(&self) -> Vec<PrecompileCheckSummary> {
		vec![PrecompileCheckSummary {
			name: None,
			precompile_kind: PrecompileKind::Single(A::get()),
			recursion_limit: C::recursion_limit().unwrap_or(Some(0)),
			accept_delegate_call: C::accept_delegate_call().unwrap_or(false),
			callable_by_smart_contract: C::callable_by_smart_contract_summary()
				.unwrap_or_else(|| "Not callable".into()),
			callable_by_precompile: C::callable_by_precompile_summary()
				.unwrap_or_else(|| "Not callable".into()),
		}]
	}
}

impl<A, P, C> IsActivePrecompile for PrecompileAt<A, P, C>
where
	A: Get<H160>,
{
	#[inline(always)]
	fn is_active_precompile(&self, address: H160, _gas: u64) -> IsPrecompileResult {
		IsPrecompileResult::Answer {
			is_precompile: address == A::get(),
			extra_cost: 0,
		}
	}
}

/// Wraps an inner PrecompileSet with all its addresses starting with
/// a common prefix.
/// Type parameters allow to define:
/// - A: The common prefix
/// - D: If DELEGATECALL is supported (default to no)
pub struct PrecompileSetStartingWith<A, P, C = ()> {
	precompile_set: P,
	current_recursion_level: RefCell<BTreeMap<H160, u16>>,
	_phantom: PhantomData<(A, C)>,
}

impl<A, P, C> PrecompileSetFragment for PrecompileSetStartingWith<A, P, C>
where
	A: Get<&'static [u8]>,
	P: PrecompileSet + Default,
	C: PrecompileChecks,
{
	#[inline(always)]
	fn new() -> Self {
		Self {
			precompile_set: P::default(),
			current_recursion_level: RefCell::new(BTreeMap::new()),
			_phantom: PhantomData,
		}
	}

	#[inline(always)]
	fn execute<R: pallet_evm::Config>(
		&self,
		handle: &mut impl PrecompileHandle,
	) -> Option<PrecompileResult> {
		let code_address = handle.code_address();
		if !is_precompile_or_fail::<R>(code_address, handle.remaining_gas()).ok()? {
			return None;
		}
		// Perform common checks.
		if let Err(err) = common_checks::<R, C>(handle) {
			return Some(Err(err));
		}

		// Check and increase recursion level if needed.
		let recursion_limit = C::recursion_limit().unwrap_or(Some(0));
		if let Some(max_recursion_level) = recursion_limit {
			match self.current_recursion_level.try_borrow_mut() {
				Ok(mut recursion_level_map) => {
					let recursion_level = recursion_level_map.entry(code_address).or_insert(0);

					if *recursion_level > max_recursion_level {
						return Some(Err(revert("Precompile is called with too high nesting")));
					}

					*recursion_level += 1;
				}
				// We don't hold the borrow and are in single-threaded code, thus we should
				// not be able to fail borrowing in nested calls.
				Err(_) => return Some(Err(revert("Couldn't check precompile nesting"))),
			}
		}

		// Subcall protection.
		let allow_subcalls = C::allow_subcalls().unwrap_or(false);
		let mut handle = RestrictiveHandle {
			handle,
			allow_subcalls,
		};

		let res = self.precompile_set.execute(&mut handle);

		// Decrease recursion level if needed.
		if recursion_limit.is_some() {
			match self.current_recursion_level.try_borrow_mut() {
				Ok(mut recursion_level_map) => {
					let recursion_level = match recursion_level_map.get_mut(&code_address) {
						Some(recursion_level) => recursion_level,
						None => return Some(Err(revert("Couldn't retreive precompile nesting"))),
					};

					*recursion_level -= 1;
				}
				// We don't hold the borrow and are in single-threaded code, thus we should
				// not be able to fail borrowing in nested calls.
				Err(_) => return Some(Err(revert("Couldn't check precompile nesting"))),
			}
		}

		res
	}

	#[inline(always)]
	fn is_precompile(&self, address: H160, gas: u64) -> IsPrecompileResult {
		if address.as_bytes().starts_with(A::get()) {
			return self.precompile_set.is_precompile(address, gas);
		}
		IsPrecompileResult::Answer {
			is_precompile: false,
			extra_cost: 0,
		}
	}

	#[inline(always)]
	fn used_addresses(&self) -> Vec<H160> {
		// TODO: We currently can't get the list of used addresses.
		vec![]
	}

	fn summarize_checks(&self) -> Vec<PrecompileCheckSummary> {
		let prefix = A::get();

		vec![PrecompileCheckSummary {
			name: None,
			precompile_kind: PrecompileKind::Prefixed(prefix.to_vec()),
			recursion_limit: C::recursion_limit().unwrap_or(Some(0)),
			accept_delegate_call: C::accept_delegate_call().unwrap_or(false),
			callable_by_smart_contract: C::callable_by_smart_contract_summary()
				.unwrap_or_else(|| "Not callable".into()),
			callable_by_precompile: C::callable_by_precompile_summary()
				.unwrap_or_else(|| "Not callable".into()),
		}]
	}
}

impl<A, P, C> IsActivePrecompile for PrecompileSetStartingWith<A, P, C>
where
	Self: PrecompileSetFragment,
{
	#[inline(always)]
	fn is_active_precompile(&self, address: H160, gas: u64) -> IsPrecompileResult {
		self.is_precompile(address, gas)
	}
}

/// Make a precompile that always revert.
/// Can be useful when writing tests.
pub struct RevertPrecompile<A>(PhantomData<A>);

impl<A> PrecompileSetFragment for RevertPrecompile<A>
where
	A: Get<H160>,
{
	#[inline(always)]
	fn new() -> Self {
		Self(PhantomData)
	}

	#[inline(always)]
	fn execute<R: pallet_evm::Config>(
		&self,
		handle: &mut impl PrecompileHandle,
	) -> Option<PrecompileResult> {
		if A::get() == handle.code_address() {
			Some(Err(revert("revert")))
		} else {
			None
		}
	}

	#[inline(always)]
	fn is_precompile(&self, address: H160, _gas: u64) -> IsPrecompileResult {
		IsPrecompileResult::Answer {
			is_precompile: address == A::get(),
			extra_cost: 0,
		}
	}

	#[inline(always)]
	fn used_addresses(&self) -> Vec<H160> {
		vec![A::get()]
	}

	fn summarize_checks(&self) -> Vec<PrecompileCheckSummary> {
		vec![PrecompileCheckSummary {
			name: None,
			precompile_kind: PrecompileKind::Single(A::get()),
			recursion_limit: Some(0),
			accept_delegate_call: true,
			callable_by_smart_contract: "Reverts in all cases".into(),
			callable_by_precompile: "Reverts in all cases".into(),
		}]
	}
}

impl<A> IsActivePrecompile for RevertPrecompile<A> {
	#[inline(always)]
	fn is_active_precompile(&self, _address: H160, _gas: u64) -> IsPrecompileResult {
		IsPrecompileResult::Answer {
			is_precompile: true,
			extra_cost: 0,
		}
	}
}

/// A precompile that was removed from a precompile set.
/// Still considered a precompile but is inactive and always revert.
pub struct RemovedPrecompileAt<A>(PhantomData<A>);
impl<A> PrecompileSetFragment for RemovedPrecompileAt<A>
where
	A: Get<H160>,
{
	#[inline(always)]
	fn new() -> Self {
		Self(PhantomData)
	}

	#[inline(always)]
	fn execute<R: pallet_evm::Config>(
		&self,
		handle: &mut impl PrecompileHandle,
	) -> Option<PrecompileResult> {
		if A::get() == handle.code_address() {
			Some(Err(revert("Removed precompile")))
		} else {
			None
		}
	}

	#[inline(always)]
	fn is_precompile(&self, address: H160, _gas: u64) -> IsPrecompileResult {
		IsPrecompileResult::Answer {
			is_precompile: address == A::get(),
			extra_cost: 0,
		}
	}

	#[inline(always)]
	fn used_addresses(&self) -> Vec<H160> {
		vec![A::get()]
	}

	fn summarize_checks(&self) -> Vec<PrecompileCheckSummary> {
		vec![PrecompileCheckSummary {
			name: None,
			precompile_kind: PrecompileKind::Single(A::get()),
			recursion_limit: Some(0),
			accept_delegate_call: true,
			callable_by_smart_contract: "Reverts in all cases".into(),
			callable_by_precompile: "Reverts in all cases".into(),
		}]
	}
}

impl<A> IsActivePrecompile for RemovedPrecompileAt<A> {
	#[inline(always)]
	fn is_active_precompile(&self, _address: H160, _gas: u64) -> IsPrecompileResult {
		IsPrecompileResult::Answer {
			is_precompile: false,
			extra_cost: 0,
		}
	}
}

// COMPOSITION OF PARTS
#[impl_for_tuples(1, 100)]
impl PrecompileSetFragment for Tuple {
	#[inline(always)]
	fn new() -> Self {
		(for_tuples!(#(
			Tuple::new()
		),*))
	}

	#[inline(always)]
	fn execute<R: pallet_evm::Config>(
		&self,
		handle: &mut impl PrecompileHandle,
	) -> Option<PrecompileResult> {
		for_tuples!(#(
			if let Some(res) = self.Tuple.execute::<R>(handle) {
				return Some(res);
			}
		)*);

		None
	}

	#[inline(always)]
	fn is_precompile(&self, address: H160, gas: u64) -> IsPrecompileResult {
		for_tuples!(#(
			if let IsPrecompileResult::Answer {
			is_precompile: true,
				..
			} = self.Tuple.is_precompile(address, gas) { return IsPrecompileResult::Answer {
				is_precompile: true,
				extra_cost: 0,
				}
			};
		)*);
		IsPrecompileResult::Answer {
			is_precompile: false,
			extra_cost: 0,
		}
	}

	#[inline(always)]
	fn used_addresses(&self) -> Vec<H160> {
		let mut used_addresses = vec![];

		for_tuples!(#(
			let mut inner = self.Tuple.used_addresses();
			used_addresses.append(&mut inner);
		)*);

		used_addresses
	}

	fn summarize_checks(&self) -> Vec<PrecompileCheckSummary> {
		let mut checks = Vec::new();

		for_tuples!(#(
			let mut inner = self.Tuple.summarize_checks();
			checks.append(&mut inner);
		)*);

		checks
	}
}

#[impl_for_tuples(1, 100)]
impl IsActivePrecompile for Tuple {
	#[inline(always)]
	fn is_active_precompile(&self, address: H160, gas: u64) -> IsPrecompileResult {
		for_tuples!(#(
			if let IsPrecompileResult::Answer {
				is_precompile: true,
				..
			} = self.Tuple.is_active_precompile(address, gas) { return IsPrecompileResult::Answer {
				is_precompile: true,
				extra_cost: 0,
			} };
		)*);
		IsPrecompileResult::Answer {
			is_precompile: false,
			extra_cost: 0,
		}
	}
}

/// Wraps a precompileset fragment into a range, and will skip processing it if the address
/// is out of the range.
pub struct PrecompilesInRangeInclusive<R, P> {
	inner: P,
	range: RangeInclusive<H160>,
	_phantom: PhantomData<R>,
}

impl<S, E, P> PrecompileSetFragment for PrecompilesInRangeInclusive<(S, E), P>
where
	S: Get<H160>,
	E: Get<H160>,
	P: PrecompileSetFragment,
{
	fn new() -> Self {
		Self {
			inner: P::new(),
			range: RangeInclusive::new(S::get(), E::get()),
			_phantom: PhantomData,
		}
	}

	fn execute<R: pallet_evm::Config>(
		&self,
		handle: &mut impl PrecompileHandle,
	) -> Option<PrecompileResult> {
		if self.range.contains(&handle.code_address()) {
			self.inner.execute::<R>(handle)
		} else {
			None
		}
	}

	fn is_precompile(&self, address: H160, gas: u64) -> IsPrecompileResult {
		if self.range.contains(&address) {
			self.inner.is_precompile(address, gas)
		} else {
			IsPrecompileResult::Answer {
				is_precompile: false,
				extra_cost: 0,
			}
		}
	}

	fn used_addresses(&self) -> Vec<H160> {
		self.inner.used_addresses()
	}

	fn summarize_checks(&self) -> Vec<PrecompileCheckSummary> {
		self.inner.summarize_checks()
	}
}

impl<S, E, P> IsActivePrecompile for PrecompilesInRangeInclusive<(S, E), P>
where
	P: IsActivePrecompile,
{
	fn is_active_precompile(&self, address: H160, gas: u64) -> IsPrecompileResult {
		if self.range.contains(&address) {
			self.inner.is_active_precompile(address, gas)
		} else {
			IsPrecompileResult::Answer {
				is_precompile: false,
				extra_cost: 0,
			}
		}
	}
}

/// Wraps a tuple of `PrecompileSetFragment` to make a real `PrecompileSet`.
pub struct PrecompileSetBuilder<R, P> {
	inner: P,
	_phantom: PhantomData<R>,
}

impl<R: pallet_evm::Config, P: PrecompileSetFragment> PrecompileSet for PrecompileSetBuilder<R, P> {
	fn execute(&self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {
		self.inner.execute::<R>(handle)
	}

	fn is_precompile(&self, address: H160, gas: u64) -> IsPrecompileResult {
		self.inner.is_precompile(address, gas)
	}
}

impl<R, P: IsActivePrecompile> IsActivePrecompile for PrecompileSetBuilder<R, P> {
	fn is_active_precompile(&self, address: H160, gas: u64) -> IsPrecompileResult {
		self.inner.is_active_precompile(address, gas)
	}
}

impl<R: pallet_evm::Config, P: PrecompileSetFragment> Default for PrecompileSetBuilder<R, P> {
	fn default() -> Self {
		Self::new()
	}
}

impl<R: pallet_evm::Config, P: PrecompileSetFragment> PrecompileSetBuilder<R, P> {
	/// Create a new instance of the PrecompileSet.
	pub fn new() -> Self {
		Self {
			inner: P::new(),
			_phantom: PhantomData,
		}
	}

	/// Return the list of mapped addresses contained in this PrecompileSet.
	pub fn used_addresses() -> impl Iterator<Item = R::AccountId> {
		Self::used_addresses_h160().map(R::AddressMapping::into_account_id)
	}

	/// Return the list of H160 addresses contained in this PrecompileSet.
	pub fn used_addresses_h160() -> impl Iterator<Item = H160> {
		Self::new().inner.used_addresses().into_iter()
	}

	pub fn summarize_checks(&self) -> Vec<PrecompileCheckSummary> {
		self.inner.summarize_checks()
	}
}