Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip #2256

Open
wants to merge 4 commits into
base: spr/main/96859c27
Choose a base branch
from
Open

wip #2256

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

28 changes: 28 additions & 0 deletions crates/infra_utils/src/command.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use std::env;
use std::process::Command;

use crate::path::project_path;

#[cfg(test)]
#[path = "command_test.rs"]
mod command_test;

/// Returns a shell command originating from the project root, with cargo environment variables
/// filtered out.
///
/// # Arguments
/// * `command_name` - The shell command name.
///
/// # Returns
/// * A [`std::process::Command`] object with the current directory set to the project root, and
/// cleared out cargo related environment variables.
pub fn create_shell_command(command_name: &str) -> Command {
let project_path = project_path().expect("Failed to get project path");
let mut command = Command::new(command_name);
command.current_dir(&project_path);
// Filter out all CARGO_ environment variables.
env::vars().filter(|(key, _)| key.starts_with("CARGO_")).for_each(|(key, _)| {
command.env_remove(key);
});
command
}
12 changes: 12 additions & 0 deletions crates/infra_utils/src/command_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use crate::command::create_shell_command;

#[test]
fn create_shell_command_example() {
let mut ls_command = create_shell_command("ls");
let output = ls_command.output().expect("Failed to execute command");
let stdout = String::from_utf8_lossy(&output.stdout);

assert!(output.status.success());
// Project root should contain the `crates` directory.
assert!(stdout.contains("crates"));
}
1 change: 1 addition & 0 deletions crates/infra_utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod command;
pub mod path;
27 changes: 15 additions & 12 deletions crates/infra_utils/src/path.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::env;
use std::path::{Path, PathBuf};
use std::sync::LazyLock;
use std::{env, fs};

use thiserror::Error;

Expand All @@ -10,11 +10,8 @@ mod path_test;

#[derive(Debug, Error)]
pub enum PathResolutionError {
// TODO(Arni): Handle manifest dir not exist here?
#[error("No file exists at '{path}'")]
PathDoesNotExist { path: PathBuf },
/// This error is raised when file existence can be neither confirmed nor denied. See
/// [`std::path::Path::try_exists`] for more information.
/// This error is raised when the file path does not exist, or when a non-final component in a
/// path is not a directory. See [`std::fs::canonicalize`] for more information.
#[error(transparent)]
IoError(#[from] std::io::Error),
}
Expand All @@ -23,6 +20,7 @@ pub enum PathResolutionError {
static PATH_TO_CARGO_MANIFEST_DIR: LazyLock<Option<PathBuf>> =
LazyLock::new(|| env::var("CARGO_MANIFEST_DIR").ok().map(|dir| Path::new(&dir).into()));

// TODO(Tsabary): should not be public. Use a getter instead.
pub fn cargo_manifest_dir() -> Option<PathBuf> {
PATH_TO_CARGO_MANIFEST_DIR.clone()
}
Expand All @@ -34,16 +32,21 @@ pub fn cargo_manifest_dir() -> Option<PathBuf> {
/// * `relative_path` - A string slice representing the relative path from the project root.
///
/// # Returns
/// * An absolute `PathBuf` representing the resolved path starting from the project root.
/// * A `PathBuf` representing the resolved path starting from the project root.
pub fn resolve_project_relative_path(relative_path: &str) -> Result<PathBuf, PathResolutionError> {
let base_dir = path_of_project_root();

let path = base_dir.join(relative_path);
if !path.try_exists()? {
return Err(PathResolutionError::PathDoesNotExist { path });
}
let absolute_path = fs::canonicalize(path)?;

Ok(absolute_path)
}

Ok(path)
/// Returns the absolute path of the project root directory.
///
/// # Returns
/// * A `PathBuf` representing the path of the project root.
pub fn project_path() -> Result<PathBuf, PathResolutionError> {
resolve_project_relative_path(".")
}

fn path_of_project_root() -> PathBuf {
Expand Down
8 changes: 2 additions & 6 deletions crates/infra_utils/src/path_test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::path::{path_of_project_root, resolve_project_relative_path, PathResolutionError};
use crate::path::{path_of_project_root, resolve_project_relative_path};

// TODO: Add a test for PathResolutionError::IoError.
#[test]
Expand All @@ -8,11 +8,7 @@ fn resolve_project_relative_path_on_non_existent_path() {
assert!(!expected_path.exists());
let result = resolve_project_relative_path(relative_path);

if let Err(PathResolutionError::PathDoesNotExist { path }) = result {
assert_eq!(path, expected_path);
} else {
panic!("Expected PathDoesNotExist error, got {:?}", result);
}
assert!(result.is_err(), "Expected an non-existent path error, got {:?}", result);
}

#[test]
Expand Down
7 changes: 7 additions & 0 deletions crates/starknet_integration_tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ blockifier.workspace = true
cairo-lang-starknet-classes.workspace = true
futures.workspace = true
indexmap.workspace = true
infra_utils.workspace = true
mempool_test_utils.workspace = true
papyrus_common.workspace = true
papyrus_consensus.workspace = true
Expand Down Expand Up @@ -44,8 +45,14 @@ tempfile.workspace = true
tokio.workspace = true
tracing.workspace = true


[dev-dependencies]
futures.workspace = true
pretty_assertions.workspace = true
rstest.workspace = true
starknet_sequencer_infra.workspace = true


[[bin]]
name = "end_to_end_integration_test"
path = "src/bin/end_to_end_integration_test.rs"
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::path::PathBuf;
use std::process::Stdio;
use std::time::Duration;

use infra_utils::path::resolve_project_relative_path;
use mempool_test_utils::starknet_api_test_utils::{AccountId, MultiAccountTransactionGenerator};
use papyrus_execution::execution_utils::get_nonce_at;
use papyrus_storage::state::StateStorageReader;
Expand All @@ -14,6 +15,7 @@ use starknet_api::state::StateNumber;
use starknet_integration_tests::integration_test_setup::IntegrationTestSetup;
use starknet_integration_tests::utils::{create_integration_test_tx_generator, send_account_txs};
use starknet_sequencer_infra::trace_util::configure_tracing;
use starknet_sequencer_node::test_utils::compilation::compile_node_result;
use starknet_types_core::felt::Felt;
use tokio::process::{Child, Command};
use tokio::task::{self, JoinHandle};
Expand All @@ -28,25 +30,53 @@ fn tx_generator() -> MultiAccountTransactionGenerator {
// TODO(Tsabary): Move to a suitable util location.
async fn spawn_node_child_task(node_config_path: PathBuf) -> Child {
// Get the current working directory for the project
let project_path = env::current_dir().expect("Failed to get current directory").join("../..");

// TODO(Tsabary): Capture output to a log file, and present it in case of a failure.
// TODO(Tsabary): Change invocation from "cargo run" to separate compilation and invocation
// (build, and then invoke the binary).
Command::new("cargo")
.arg("run")
.arg("--bin")
.arg("starknet_sequencer_node")
.arg("--quiet")
// let project_path = env::current_dir().expect("Failed to get current
// directory").join("../..");

let compile_result = compile_node_result();
info!("Compilation result {:?}.", compile_result);

assert!(compile_result.is_ok(), "Compilation failed.");
// let compilation_result = Command::new("cargo")
// .arg("build")
// .arg("--bin")
// .arg("starknet_sequencer_node")
// .arg("--quiet")
// .current_dir(&project_path)
// .status().await;

info!("Compiling the starknet_sequencer_node binary");
let project_path = resolve_project_relative_path(".").expect("Failed to resolve project path");
info!("project_path {:?}", project_path);

// Run `cargo build` to compile the project
Command::new("target/debug/starknet_sequencer_node")
.current_dir(&project_path)
.arg("--")
.arg("--config_file")
.arg(node_config_path.to_str().unwrap())
.stderr(Stdio::inherit())
.stdout(Stdio::null())
.kill_on_drop(true) // Required for stopping the node when the handle is dropped.
.spawn()
.expect("Failed to spawn the sequencer node.")

// // TODO(Tsabary): Capture output to a log file, and present it in case of a failure.
// // TODO(Tsabary): Change invocation from "cargo run" to separate compilation and invocation
// // (build, and then invoke the binary).
// Command::new("cargo")
// .arg("run")
// .arg("--bin")
// .arg("starknet_sequencer_node")
// .arg("--quiet")
// .current_dir(&project_path)
// .arg("--")
// .arg("--config_file")
// .arg(node_config_path.to_str().unwrap())
// .stderr(Stdio::inherit())
// .stdout(Stdio::null())
// .kill_on_drop(true) // Required for stopping the node when the handle is dropped.
// .spawn()
// .expect("Failed to spawn the sequencer node.")
}

async fn spawn_run_node(node_config_path: PathBuf) -> JoinHandle<()> {
Expand Down
Loading
Loading