Skip to main content

revm_precompile/
identity.rs

1//! Identity precompile returns
2use super::calc_linear_cost;
3use crate::{
4    eth_precompile_fn, EthPrecompileOutput, EthPrecompileResult, Precompile, PrecompileHalt,
5    PrecompileId,
6};
7use primitives::Bytes;
8
9eth_precompile_fn!(identity_precompile, identity_run);
10
11/// Address of the identity precompile.
12pub const FUN: Precompile = Precompile::new(
13    PrecompileId::Identity,
14    crate::u64_to_address(4),
15    identity_precompile,
16);
17
18/// The base cost of the operation
19pub const IDENTITY_BASE: u64 = 15;
20/// The cost per word
21pub const IDENTITY_PER_WORD: u64 = 3;
22
23/// Takes the input bytes, copies them, and returns it as the output.
24///
25/// See: <https://ethereum.github.io/yellowpaper/paper.pdf>
26///
27/// See: <https://etherscan.io/address/0000000000000000000000000000000000000004>
28pub fn identity_run(input: &[u8], gas_limit: u64) -> EthPrecompileResult {
29    let gas_used = calc_linear_cost(input.len(), IDENTITY_BASE, IDENTITY_PER_WORD);
30    if gas_used > gas_limit {
31        return Err(PrecompileHalt::OutOfGas);
32    }
33    Ok(EthPrecompileOutput::new(
34        gas_used,
35        Bytes::copy_from_slice(input),
36    ))
37}