ovr_fp_evm/
precompile.rs

1// SPDX-License-Identifier: Apache-2.0
2// This file is part of Frontier.
3//
4// Copyright (c) 2020 Parity Technologies (UK) Ltd.
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
18pub use evm::{
19	executor::stack::{PrecompileFailure, PrecompileOutput, PrecompileSet},
20	Context, ExitError, ExitRevert, ExitSucceed,
21};
22
23pub type PrecompileResult = Result<PrecompileOutput, PrecompileFailure>;
24
25/// One single precompile used by EVM engine.
26pub trait Precompile {
27	/// Try to execute the precompile. Calculate the amount of gas needed with given `input` and
28	/// `target_gas`. Return `Ok(status, output, gas_used)` if the execution is
29	/// successful. Otherwise return `Err(_)`.
30	fn execute(
31		input: &[u8],
32		target_gas: Option<u64>,
33		context: &Context,
34		is_static: bool,
35	) -> PrecompileResult;
36}
37
38pub trait LinearCostPrecompile {
39	const BASE: u64;
40	const WORD: u64;
41
42	fn execute(
43		input: &[u8],
44		cost: u64,
45	) -> core::result::Result<(ExitSucceed, Vec<u8>), PrecompileFailure>;
46}
47
48impl<T: LinearCostPrecompile> Precompile for T {
49	fn execute(input: &[u8], target_gas: Option<u64>, _: &Context, _: bool) -> PrecompileResult {
50		let cost = ensure_linear_cost(target_gas, input.len() as u64, T::BASE, T::WORD)?;
51
52		let (exit_status, output) = T::execute(input, cost)?;
53		Ok(PrecompileOutput {
54			exit_status,
55			cost,
56			output,
57			logs: Default::default(),
58		})
59	}
60}
61
62/// Linear gas cost
63fn ensure_linear_cost(
64	target_gas: Option<u64>,
65	len: u64,
66	base: u64,
67	word: u64,
68) -> Result<u64, PrecompileFailure> {
69	let cost = base
70		.checked_add(word.checked_mul(len.saturating_add(31) / 32).ok_or(
71			PrecompileFailure::Error {
72				exit_status: ExitError::OutOfGas,
73			},
74		)?)
75		.ok_or(PrecompileFailure::Error {
76			exit_status: ExitError::OutOfGas,
77		})?;
78
79	if let Some(target_gas) = target_gas {
80		if cost > target_gas {
81			return Err(PrecompileFailure::Error {
82				exit_status: ExitError::OutOfGas,
83			});
84		}
85	}
86
87	Ok(cost)
88}