Skip to content

Commit

Permalink
feature(evm_loader): Add pricefix feature to the crate.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tirka committed Dec 5, 2021
1 parent ce1a29c commit f4fc1d3
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 6 deletions.
5 changes: 4 additions & 1 deletion evm-utils/programs/evm_loader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ authors = ["Vladimir Motylenko <[email protected]>"]
license = "Apache-2.0"
edition = "2018"

[features]
pricefix = []

[dependencies]
log = "0.4.8"
solana-logger = { path = "../../../logger", version = "1.3.11" }
Expand All @@ -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"

Expand Down
26 changes: 21 additions & 5 deletions evm-utils/programs/evm_loader/src/precompiles/compatibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<u8>> {
use evm_state::secp256k1::{Message, SECP256K1};
use evm_state::transactions::addr_from_public_key;
Expand Down Expand Up @@ -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<Vec<u8>> {
use ripemd160::{Digest, Ripemd160};
let mut hasher = Ripemd160::new();
Expand Down Expand Up @@ -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::<u32>();
Expand Down Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions evm-utils/programs/evm_loader/src/precompiles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
})
}
Expand Down Expand Up @@ -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,
(
Expand Down Expand Up @@ -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));
});

Expand All @@ -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,
(
Expand Down

0 comments on commit f4fc1d3

Please sign in to comment.