From f4fc1d3213f62279f30dfee40213d82df2d1a513 Mon Sep 17 00:00:00 2001 From: Maksim Vykhota Date: Mon, 6 Dec 2021 01:19:53 +0200 Subject: [PATCH] feature(evm_loader): Add `pricefix` feature to the crate. --- evm-utils/programs/evm_loader/Cargo.toml | 5 ++- .../src/precompiles/compatibility.rs | 26 +++++++++++--- .../evm_loader/src/precompiles/mod.rs | 36 +++++++++++++++++++ 3 files changed, 61 insertions(+), 6 deletions(-) diff --git a/evm-utils/programs/evm_loader/Cargo.toml b/evm-utils/programs/evm_loader/Cargo.toml index 30aa6b418d..c3a70cbadf 100644 --- a/evm-utils/programs/evm_loader/Cargo.toml +++ b/evm-utils/programs/evm_loader/Cargo.toml @@ -6,6 +6,9 @@ authors = ["Vladimir Motylenko "] license = "Apache-2.0" edition = "2018" +[features] +pricefix = [] + [dependencies] log = "0.4.8" solana-logger = { path = "../../../logger", version = "1.3.11" } @@ -27,7 +30,7 @@ ripemd160 = "0.9.1" num-derive = "0.3" num-traits = "0.2" num = { version = "0.1", default-features = false, features = ["bigint"] } -substrate-bn = { git = "https://github.com/paritytech/bn.git", default-features = false } +substrate-bn = { version = "0.6.0", default-features = false } byteorder = "1.4.3" eip-152 = "0.1.0" diff --git a/evm-utils/programs/evm_loader/src/precompiles/compatibility.rs b/evm-utils/programs/evm_loader/src/precompiles/compatibility.rs index 379f0fae94..588186ee5e 100644 --- a/evm-utils/programs/evm_loader/src/precompiles/compatibility.rs +++ b/evm-utils/programs/evm_loader/src/precompiles/compatibility.rs @@ -63,13 +63,18 @@ impl Precompile for EcRecover { .expect("Serialization of static data should be determenistic and never fail.") } + #[cfg(not(feature = "pricefix"))] fn price(source: &[u8]) -> u64 { - // FIXME: Pricer::Const { price: 3000 } - // Ref.: https://ethereum.github.io/yellowpaper/paper.pdf p.21 (204) let num_words = (source.len() / 32) as u64; 60 + 12 * num_words } + #[cfg(feature = "pricefix")] + fn price(_source: &[u8]) -> u64 { + // Ref.: https://ethereum.github.io/yellowpaper/paper.pdf p.21 (204) + 3000 + } + fn implementation(source: &[u8], _cx: PrecompileContext) -> Result> { use evm_state::secp256k1::{Message, SECP256K1}; use evm_state::transactions::addr_from_public_key; @@ -139,12 +144,17 @@ impl Precompile for Ripemd160 { .expect("Serialization of static data should be determenistic and never fail.") } + #[cfg(not(feature = "pricefix"))] fn price(source: &[u8]) -> u64 { - // FIXME: linear_price(source, 600, 120, 32) - // Ref.: https://ethereum.github.io/yellowpaper/paper.pdf p.22 (219) linear_price(source, 60, 12, 32) } + #[cfg(feature = "pricefix")] + fn price(source: &[u8]) -> u64 { + // Ref.: https://ethereum.github.io/yellowpaper/paper.pdf p.22 (219) + linear_price(source, 600, 102, 32) + } + fn implementation(source: &[u8], _cx: PrecompileContext) -> Result> { use ripemd160::{Digest, Ripemd160}; let mut hasher = Ripemd160::new(); @@ -566,7 +576,6 @@ impl Precompile for Blake2F { } fn price(source: &[u8]) -> u64 { - // FIXME: set correct value const GAS_PER_ROUND: u64 = 1; const FOUR: usize = std::mem::size_of::(); @@ -636,6 +645,13 @@ impl Precompile for Blake2F { } } +#[cfg(feature = "pricefix")] +fn linear_price(source: &[u8], base: u64, per_word: u64, word_len: usize) -> u64 { + let num_words = ((source.len() + word_len - 1) / word_len) as u64; + base + per_word * num_words +} + +#[cfg(not(feature = "pricefix"))] fn linear_price(source: &[u8], base: u64, per_word: u64, word_len: usize) -> u64 { let num_words = (source.len() / word_len) as u64; base + per_word * num_words diff --git a/evm-utils/programs/evm_loader/src/precompiles/mod.rs b/evm-utils/programs/evm_loader/src/precompiles/mod.rs index a3e3722c32..8fabc909b2 100644 --- a/evm-utils/programs/evm_loader/src/precompiles/mod.rs +++ b/evm-utils/programs/evm_loader/src/precompiles/mod.rs @@ -265,6 +265,11 @@ mod test { ); let result = result.unwrap().unwrap(); println!("{}", hex::encode(&result.1)); + + #[cfg(not(feature = "pricefix"))] + assert_eq!(result, (ExitSucceed::Returned, input.to_vec(), 15)); + + #[cfg(feature = "pricefix")] assert_eq!(result, (ExitSucceed::Returned, input.to_vec(), 18)); }) } @@ -311,6 +316,19 @@ mod test { ); let result = result.unwrap().unwrap(); println!("{}", hex::encode(&result.1)); + + #[cfg(not(feature = "pricefix"))] + assert_eq!( + result, + ( + ExitSucceed::Returned, + hex!("0000000000000000000000009c1185a5c5e9fc54612808977ee8f548b2258d31") + .to_vec(), + 60 + ) + ); + + #[cfg(feature = "pricefix")] assert_eq!( result, ( @@ -344,6 +362,11 @@ mod test { ); let result = result.unwrap().unwrap(); println!("{}", hex::encode(&result.1)); + + #[cfg(not(feature = "pricefix"))] + assert_eq!(result, (ExitSucceed::Returned, vec![], 108)); + + #[cfg(feature = "pricefix")] assert_eq!(result, (ExitSucceed::Returned, vec![], 3000)); }); @@ -358,6 +381,19 @@ mod test { ); let result = result.unwrap().unwrap(); println!("{}", hex::encode(&result.1)); + + #[cfg(not(feature = "pricefix"))] + assert_eq!( + result, + ( + ExitSucceed::Returned, + hex!("000000000000000000000000c08b5542d177ac6686946920409741463a15dddb") + .to_vec(), + 108 + ) + ); + + #[cfg(feature = "pricefix")] assert_eq!( result, (