Skip to content

Commit

Permalink
Merge pull request #693 from g-r-a-n-t/harness-benchmarks
Browse files Browse the repository at this point in the history
Gas reporting
  • Loading branch information
g-r-a-n-t authored Apr 15, 2022
2 parents 316e6aa + 9755dd5 commit 182d189
Show file tree
Hide file tree
Showing 231 changed files with 1,976 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/test-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ serde_json = "1.0.64"
solc = {git = "https://github.com/g-r-a-n-t/solc-rust", rev = "52d4146", optional = true}
yultsur = {git = "https://github.com/g-r-a-n-t/yultsur", rev = "ae85470"}
indexmap = "1.6.2"
insta = "1.7.1"

# used by ethabi, we need to force the js feature for wasm support
getrandom = { version = "0.2.3", features = ["js"] }
Expand Down
54 changes: 53 additions & 1 deletion crates/test-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,54 @@ use fe_common::utils::keccak;
use fe_driver as driver;
use fe_yulgen::runtime::functions;
use primitive_types::{H160, U256};
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::fmt::{Display, Formatter};
use std::str::FromStr;
use yultsur::*;

#[macro_export]
macro_rules! assert_harness_gas_report {
($harness:ident) => {
assert_snapshot!(format!("{}", $harness.gas_reporter));
};
}

#[derive(Default, Debug)]
pub struct GasReporter {
records: RefCell<Vec<GasRecord>>,
}

impl GasReporter {
pub fn add_record(&self, description: &str, gas_used: u64) {
self.records.borrow_mut().push(GasRecord {
description: description.to_string(),
gas_used,
})
}

pub fn add_func_call_record(&self, function: &str, input: &[ethabi::Token], gas_used: u64) {
let description = format!("{}({:?})", function, input);
self.add_record(&description, gas_used)
}
}

impl Display for GasReporter {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
for record in self.records.borrow().iter() {
writeln!(f, "{} used {} gas", record.description, record.gas_used)?;
}

Ok(())
}
}

#[derive(Debug)]
pub struct GasRecord {
pub description: String,
pub gas_used: u64,
}

pub trait ToBeBytes {
fn to_be_bytes(&self) -> [u8; 32];
}
Expand All @@ -34,6 +78,7 @@ pub const DEFAULT_CALLER: &str = "1000000000000000000000000000000000000001";

#[allow(dead_code)]
pub struct ContractHarness {
pub gas_reporter: GasReporter,
pub address: H160,
pub abi: ethabi::Contract,
pub caller: H160,
Expand All @@ -46,6 +91,7 @@ impl ContractHarness {
let caller = address(DEFAULT_CALLER);

ContractHarness {
gas_reporter: GasReporter::default(),
address: contract_address,
abi,
caller,
Expand Down Expand Up @@ -92,6 +138,7 @@ impl ContractHarness {
output: Option<&ethabi::Token>,
) {
let actual_output = self.call_function(executor, name, input);

assert_eq!(
output.map(ToOwned::to_owned),
actual_output,
Expand All @@ -107,8 +154,13 @@ impl ContractHarness {
input: &[ethabi::Token],
) -> Option<ethabi::Token> {
let function = &self.abi.functions[name][0];
let start_gas = executor.used_gas();
let capture = self.capture_call(executor, name, input);
let gas_used = executor.used_gas() - start_gas;
self.gas_reporter
.add_func_call_record(name, input, gas_used);

match self.capture_call(executor, name, input) {
match capture {
evm::Capture::Exit((ExitReason::Succeed(_), output)) => function
.decode_output(&output)
.unwrap_or_else(|_| panic!("unable to decode output of {}: {:?}", name, &output))
Expand Down
3 changes: 3 additions & 0 deletions crates/tests/src/demo_erc20.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![cfg(feature = "solc-backend")]

use fe_compiler_test_utils::*;
use insta::assert_snapshot;

#[test]
fn erc20_token() {
Expand Down Expand Up @@ -177,5 +178,7 @@ fn erc20_token() {
),
],
);

assert_harness_gas_report!(harness);
});
}
3 changes: 3 additions & 0 deletions crates/tests/src/demo_guestbook.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![cfg(feature = "solc-backend")]

use fe_compiler_test_utils::*;
use insta::assert_snapshot;

#[test]
fn guest_book() {
Expand All @@ -17,5 +18,7 @@ fn guest_book() {
harness.test_function(&mut executor, "get_msg", &[sender], Some(&msg));

harness.events_emitted(executor, &[("Signed", &[msg])]);

assert_harness_gas_report!(harness);
})
}
4 changes: 4 additions & 0 deletions crates/tests/src/demo_uniswap.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![cfg(feature = "solc-backend")]

use fe_compiler_test_utils::*;
use insta::assert_snapshot;

#[test]
fn uniswap_contracts() {
Expand Down Expand Up @@ -283,5 +284,8 @@ fn uniswap_contracts() {
&[pair_address],
Some(&uint_token_from_dec_str("708")),
);

assert_harness_gas_report!(pair_harness);
assert_harness_gas_report!(factory_harness);
});
}
Loading

0 comments on commit 182d189

Please sign in to comment.