Skip to content

Commit

Permalink
arbitrum-client: add integration test setup
Browse files Browse the repository at this point in the history
  • Loading branch information
akirillo committed Nov 26, 2023
1 parent 3c5c76c commit 7acd57b
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

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

12 changes: 12 additions & 0 deletions arbitrum-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ name = "arbitrum-client"
version = "0.1.0"
edition = "2021"

[[test]]
name = "integration"
path = "integration/main.rs"
harness = false

[dependencies]
ethers = "2"
ark-bn254 = "0.4.0"
Expand All @@ -24,3 +29,10 @@ renegade-crypto = { path = "../renegade-crypto" }
serde = { workspace = true }
serde_with = "3.4"
postcard = { version = "1", features = ["alloc"] }

[dev-dependencies]
clap = { version = "4.0", features = ["derive"] }
eyre = { workspace = true }
test-helpers = { path = "../test-helpers" }
util = { path = "../util" }
json = "0.12"
17 changes: 17 additions & 0 deletions arbitrum-client/integration/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//! Constants used in the Arbitrum client integration tests
/// The default hostport that the Nitro devnet L2 node runs on
pub(crate) const DEFAULT_DEVNET_HOSTPORT: &str = "http://localhost:8547";

/// The default private key that the Nitro devnet is seeded with
pub(crate) const DEFAULT_DEVNET_PKEY: &str =
"0xb6b15c8cb491557369f3c7d2c287b053eb229daa9c22138887752191c9520659";

/// The deployments key in the `deployments.json` file
pub(crate) const DEPLOYMENTS_KEY: &str = "deployments";

/// The darkpool contract key in the `deployments.json` file
pub(crate) const DARKPOOL_CONTRACT_KEY: &str = "darkpool_contract";

/// The darkpool proxy contract key in the `deployments.json` file
pub(crate) const DARKPOOL_PROXY_CONTRACT_KEY: &str = "darkpool_proxy_contract";
18 changes: 18 additions & 0 deletions arbitrum-client/integration/helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//! Helper functions for Arbitrum client integration tests
use std::{fs::File, io::Read};
use eyre::{Result, eyre};

use crate::constants::DEPLOYMENTS_KEY;

/// Parse a `deployments.json` file to get the address of a deployed contract
pub fn parse_addr_from_deployments_file(file_path: &str, contract_key: &str) -> Result<String> {
let mut file_contents = String::new();
File::open(file_path)?.read_to_string(&mut file_contents)?;

let parsed_json = json::parse(&file_contents)?;
parsed_json[DEPLOYMENTS_KEY][contract_key]
.as_str()
.map(|s| s.to_string())
.ok_or_else(|| eyre!("Could not parse darkpool address from deployments file"))
}
95 changes: 95 additions & 0 deletions arbitrum-client/integration/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#![deny(unsafe_code)]
#![deny(missing_docs)]
#![deny(clippy::missing_docs_in_private_items)]
#![allow(incomplete_features)]
#![feature(generic_const_exprs)]

mod constants;
mod helpers;

use arbitrum_client::{
client::{ArbitrumClient, ArbitrumClientConfig},
constants::Chain,
};
use clap::Parser;
use constants::{DARKPOOL_CONTRACT_KEY, DARKPOOL_PROXY_CONTRACT_KEY};
use helpers::parse_addr_from_deployments_file;
use test_helpers::integration_test_main;
use util::{logging::LevelFilter, runtime::block_on_result};

use crate::constants::{DEFAULT_DEVNET_HOSTPORT, DEFAULT_DEVNET_PKEY};

#[derive(Debug, Clone, Parser)]
#[command(author, version, about, long_about=None)]
struct CliArgs {
/// The private key to use for signing transactions in the integration test
///
/// Defaults to the private key that the Nitro devnet is pre-seeded with
#[arg(short = 'p', long, default_value = DEFAULT_DEVNET_PKEY)]
private_key: String,

/// The location of a `deployments.json` file that contains the addresses of
/// the deployed contracts
#[arg(short, long)]
deployments_path: String,

// TODO: Add a flag for the contract artifacts to allow for building/deploying
// during test setup
/// The url of the Arbitrum RPC endpoint to use for the integration test
#[arg(long, default_value = DEFAULT_DEVNET_HOSTPORT)]
rpc_url: String,

/// The test to run
#[arg(short, long, value_parser)]
test: Option<String>,

/// The verbosity level of the test harness
#[arg(short, long)]
verbose: bool,
}

/// The arguments provided to every integration test
#[derive(Clone)]
struct IntegrationTestArgs {
/// The Arbitrum client that resolves to a locally running devnet node
client: ArbitrumClient,
}

impl From<CliArgs> for IntegrationTestArgs {
fn from(test_args: CliArgs) -> Self {
// Pull the contract address either from the CLI or a shared volume at the CLI
// specified path

let darkpool_addr = parse_addr_from_deployments_file(
&test_args.deployments_path,
DARKPOOL_PROXY_CONTRACT_KEY,
)
.unwrap();
let event_source =
parse_addr_from_deployments_file(&test_args.deployments_path, DARKPOOL_CONTRACT_KEY)
.unwrap();

// Build a client that references the darkpool
// We block on the client creation so that we can match the (synchronous)
// function signature of `From`, which is assumed to be implemented in
// the integration test harness
let client = block_on_result(ArbitrumClient::new(ArbitrumClientConfig {
chain: Chain::Devnet,
darkpool_addr,
event_source,
arb_priv_key: test_args.private_key,
rpc_url: test_args.rpc_url,
}))
.unwrap();

Self { client }
}
}

/// Setup code for the integration tests
fn setup_integration_tests(_test_args: &CliArgs) {
// Configure logging
util::logging::setup_system_logger(LevelFilter::INFO);
}

integration_test_main!(CliArgs, IntegrationTestArgs, setup_integration_tests);

0 comments on commit 7acd57b

Please sign in to comment.