Skip to content

Commit

Permalink
Merge pull request #597 from primitivefinance/testing
Browse files Browse the repository at this point in the history
tests: Improves testing for all the fork logic so that the tests never fail
  • Loading branch information
0xJepsen authored Oct 10, 2023
2 parents 1ee29f9 + 167861b commit 6518d59
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 26 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ name: lint

on:
pull_request:
branches:
- main
types: [opened, synchronize, reopened]

jobs:
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ name: test

on:
pull_request:
branches:
- main
types: [opened, synchronize, reopened]

jobs:
Expand Down
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ arbiter-core = { path = "arbiter-core" }
clap = { version = "=4.4.6", features = ["derive"] }
serde = { version = "=1.0.188", features = ["derive"] }
serde_json = { version = "1.0.107" }
toml = { version = "=0.8.2" }
config = { version = "=0.13.3" }
ethers = { version = "=2.0.10" }
revm = { version = "=3.5.0", features = [ "ethersdb", "std" ] }
toml = { version = "0.8.2" }

# Building files
quote = { version = "=1.0.33" }
Expand All @@ -45,7 +45,8 @@ thiserror = { version = "=1.0.49" }
tokio = { version = "=1.32.0", features = ["full"] }
tempfile = { version = "=3.8.0" }
assert_cmd = { version = "=2.0.12" }

rayon = { version = "1.8.0" }
revm-primitives = { version = "1.3.0" }

# Release profile
[profile.release]
Expand Down
1 change: 1 addition & 0 deletions arbiter-core/src/bindings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
pub mod arbiter_math;
pub mod arbiter_token;
pub mod liquid_exchange;
pub mod weth;
File renamed without changes.
13 changes: 7 additions & 6 deletions arbiter-core/src/tests/environment_control.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use super::*;
use crate::environment::fork::Fork;
use crate::{
bindings::weth::weth,
environment::{builder::EnvironmentBuilder, fork::Fork},
};

#[tokio::test]
async fn receipt_data() {
Expand Down Expand Up @@ -149,7 +152,6 @@ async fn randomly_sampled_gas_price() {
} => SeededPoisson::new(block_rate, block_time, seed),
_ => panic!("Expected RandomlySampled block type"),
};

let mut expected_txs_per_block_vec = vec![];
for _ in 0..2 {
expected_txs_per_block_vec.push(distribution.sample());
Expand Down Expand Up @@ -225,12 +227,10 @@ async fn stop_environment() {

#[tokio::test]
async fn fork_into_arbiter() {
let fork = Fork::from_disk("../example_fork/test.json").unwrap();
let fork = Fork::from_disk("../example_fork/fork_into_test.json").unwrap();

// Get the environment going
let environment = crate::environment::builder::EnvironmentBuilder::new()
.db(fork.db)
.build();
let environment = EnvironmentBuilder::new().db(fork.db).build();

// Create a client
let client = RevmMiddleware::new(&environment, Some("name")).unwrap();
Expand All @@ -242,6 +242,7 @@ async fn fork_into_arbiter() {
let address_to_check_balance =
Address::from_str(&weth_meta.mappings.get("balanceOf").unwrap()[0]).unwrap();

println!("checking address: {}", address_to_check_balance);
let balance = weth
.balance_of(address_to_check_balance)
.call()
Expand Down
1 change: 0 additions & 1 deletion arbiter-core/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ mod data_output;
mod derives;
mod environment_control;
mod middleware_instructions;
mod weth;

use std::{str::FromStr, sync::Arc};

Expand Down
16 changes: 9 additions & 7 deletions bin/fork/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ impl ForkConfig {
pub(crate) fn new(fork_config_path: &str) -> Result<Self, ConfigError> {
let mut cwd = env::current_dir().unwrap();
cwd.push(fork_config_path);
println!("Reading config from: {:?}", cwd);
let config = Config::builder()
.add_source(config::File::with_name(
cwd.to_str()
Expand Down Expand Up @@ -70,13 +69,17 @@ impl ForkConfig {
let info = ethers_db
.basic(address.to_fixed_bytes().into())
.map_err(|_| {
ArbiterError::DBError("Failed to fetch account info with EthersDB.".to_string())
ArbiterError::DBError(
"Failed to fetch account info with
EthersDB."
.to_string(),
)
})?
.ok_or(ArbiterError::DBError(
"Failed to fetch account info with EthersDB.".to_string(),
))?;
db.insert_account_info(address.to_fixed_bytes().into(), info);

db.insert_account_info(address.to_fixed_bytes().into(), info);
let artifacts = digest::digest_artifacts(contract_data.artifacts_path.as_str())?;
let storage_layout = artifacts.storage_layout;

Expand All @@ -102,18 +105,17 @@ impl ForkConfig {
let dir = self.output_directory.clone().unwrap();
let file_path = Path::new(&self.output_directory.clone().unwrap())
.join(self.output_filename.clone().unwrap());
println!("path: {:?}", file_path);
if file_path.exists() && file_path.is_file() {
if file_path.try_exists().unwrap() && file_path.is_file() {
if !overwrite {
// TODO: We should allow for an overwrite flag here.
panic!(
"File already exists at output path. Please use the `--overwrite` flag, delete it, or change the output path."
);
} else {
// weirdly sometimes fails here with message: "No such file or directory"
fs::remove_file(&file_path).unwrap();
}
}

let fork = self.into_fork()?;
let mut raw = HashMap::new();
for (address, db_account) in fork.db.accounts {
Expand All @@ -136,7 +138,7 @@ impl ForkConfig {
fs::create_dir_all(dir)?;
let mut file = fs::File::create(file_path)?;
file.write_all(json_data.as_bytes()).unwrap();

println!("Wrote fork data to disk.");
Ok(())
}

Expand Down
41 changes: 35 additions & 6 deletions bin/fork/tests.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use arbiter_core::environment::fork::Fork;
use rayon::prelude::{IntoParallelIterator, ParallelIterator};

use super::*;

const FORK_CONFIG_PATH: &str = "example_fork/weth_config.toml";
Expand All @@ -12,16 +15,42 @@ fn create_forked_db() {

#[test]
fn write_out() {
let fork_config = ForkConfig::new(FORK_CONFIG_PATH).unwrap();
fork_config.write_to_disk(&true).unwrap();
let fork_config = ForkConfig::new(FORK_CONFIG_PATH);
assert!(fork_config.is_ok());
let fork_config = fork_config.unwrap();

// Use par_iter to parallelize the loop
(0..10).into_par_iter().for_each(|_| {
let disk_op = fork_config.clone().write_to_disk(&true);
assert!(disk_op.is_ok());
});

let remove_op = fs::remove_file(PATH_TO_DISK_STORAGE);
assert!(remove_op.is_ok());
}

#[test]
fn read_in() {
// First write out so we know the file exists.
let fork_config = ForkConfig::new(FORK_CONFIG_PATH).unwrap();
fork_config.write_to_disk(&true).unwrap();
let fork_config = ForkConfig::new(FORK_CONFIG_PATH);
assert!(fork_config.is_ok());
let fork_config = fork_config.unwrap();
let disk_op = fork_config.clone().write_to_disk(&true);
assert!(disk_op.is_ok());

let forked_db = Fork::from_disk(PATH_TO_DISK_STORAGE).unwrap();
println!("{:#?}", forked_db);
let thing = Path::new(PATH_TO_DISK_STORAGE).try_exists().unwrap();
if thing {
assert!(thing)
} else {
// try again
let disk_op = fork_config.clone().write_to_disk(&true);
assert!(disk_op.is_ok());
}
// Use par_iter to parallelize the loop
(0..10).into_par_iter().for_each(|_| {
let forked_db = Fork::from_disk(PATH_TO_DISK_STORAGE);
assert!(forked_db.is_ok());
});
let remove_op = fs::remove_file(PATH_TO_DISK_STORAGE);
assert!(remove_op.is_ok());
}
File renamed without changes.

0 comments on commit 6518d59

Please sign in to comment.