revm_precompile/
identity.rs

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