Skip to content

Commit

Permalink
refactor: rm panics
Browse files Browse the repository at this point in the history
  • Loading branch information
fborello-lambda committed Dec 2, 2024
1 parent 9981422 commit f90dcaa
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 61 deletions.
1 change: 1 addition & 0 deletions crates/l2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ path = "./l2.rs"
unwrap_used = "deny"
expect_used = "deny"
indexing_slicing = "deny"
panic = "deny"
1 change: 1 addition & 0 deletions crates/l2/contracts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ path = "./deployer.rs"
unwrap_used = "deny"
expect_used = "deny"
indexing_slicing = "deny"
panic = "deny"
68 changes: 21 additions & 47 deletions crates/l2/contracts/deployer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use bytes::Bytes;
use colored::Colorize;
use ethereum_types::{Address, H160, H256};
use ethrex_core::U256;
use ethrex_l2::utils::config::errors;
use ethrex_l2::utils::eth_client::errors::EthClientError;
use ethrex_l2::utils::{
config::{read_env_as_lines, read_env_file, write_env},
Expand All @@ -15,7 +16,6 @@ use std::{
process::Command,
str::FromStr,
};
use tracing::warn;

struct SetupResult {
deployer_address: Address,
Expand Down Expand Up @@ -45,6 +45,8 @@ pub enum DeployError {
EthClientError(#[from] EthClientError),
#[error("Deployer decoding error: {0}")]
DecodingError(String),
#[error("Failed to interact with .env file, error: {0}")]
EnvFileError(#[from] errors::ConfigError),
}

// 0x4e59b44847b379578588920cA78FbF26c0B4956C
Expand All @@ -58,35 +60,20 @@ lazy_static::lazy_static! {
}

#[tokio::main]
async fn main() {
let Ok(setup_result) = setup() else {
panic!("Failed on setup");
};
if let Err(e) = download_contract_deps(&setup_result.contracts_path) {
panic!("Failed to download contracts {e}");
};
if let Err(e) = compile_contracts(&setup_result.contracts_path) {
panic!("Failed to compile contracts {e}");
};
async fn main() -> Result<(), DeployError> {
let setup_result = setup()?;
download_contract_deps(&setup_result.contracts_path)?;
compile_contracts(&setup_result.contracts_path)?;

let on_chain_proposer;
let bridge_address;
match deploy_contracts(
let (on_chain_proposer, bridge_address) = deploy_contracts(
setup_result.deployer_address,
setup_result.deployer_private_key,
&setup_result.eth_client,
&setup_result.contracts_path,
)
.await
{
Ok((ocp, ba)) => {
on_chain_proposer = ocp;
bridge_address = ba;
}
Err(e) => panic!("Failed to deploy contracts {e}"),
};
.await?;

if let Err(err) = initialize_contracts(
initialize_contracts(
setup_result.deployer_address,
setup_result.deployer_private_key,
setup_result.committer_address,
Expand All @@ -96,14 +83,9 @@ async fn main() {
setup_result.contract_verifier_address,
&setup_result.eth_client,
)
.await
{
panic!("Failed to initialize contracts: {err}");
}
.await?;

let Ok(env_lines) = read_env_as_lines() else {
panic!("Failed to read env file as lines.");
};
let env_lines = read_env_as_lines().map_err(DeployError::EnvFileError)?;

let mut wr_lines: Vec<String> = Vec::new();
let mut env_lines_iter = env_lines.into_iter();
Expand All @@ -122,18 +104,12 @@ async fn main() {
}
wr_lines.push(line);
}
if let Err(err) = write_env(wr_lines) {
panic!(
"{}",
format!("Failed to write changes to the .env file: {err}")
);
}
write_env(wr_lines).map_err(DeployError::EnvFileError)?;
Ok(())
}

fn setup() -> Result<SetupResult, DeployError> {
if let Err(e) = read_env_file() {
warn!("Failed to read .env file: {e}");
}
read_env_file()?;

let eth_client = EthClient::new(&read_env_var("ETH_RPC_URL")?);

Expand Down Expand Up @@ -650,13 +626,14 @@ async fn wait_for_transaction_receipt(

#[allow(clippy::unwrap_used)]
#[allow(clippy::expect_used)]
#[allow(clippy::panic)]
#[cfg(test)]
mod test {
use crate::{compile_contracts, download_contract_deps};
use crate::{compile_contracts, download_contract_deps, DeployError};
use std::{env, path::Path};

#[test]
fn test_contract_compilation() {
fn test_contract_compilation() -> Result<(), DeployError> {
let binding = env::current_dir().unwrap();
let parent_dir = binding.parent().unwrap();

Expand All @@ -676,14 +653,11 @@ mod test {
}
}

if download_contract_deps(Path::new("contracts")).is_err() {
panic!("failed to download contract deps");
};
if compile_contracts(Path::new("contracts")).is_err() {
panic!("failed to compile contracts");
};
download_contract_deps(Path::new("contracts"))?;
compile_contracts(Path::new("contracts"))?;

std::fs::remove_dir_all(solc_out).unwrap();
std::fs::remove_dir_all(lib).unwrap();
Ok(())
}
}
23 changes: 12 additions & 11 deletions crates/l2/proposer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ pub struct Proposer {
engine_config: EngineApiConfig,
block_production_interval: u64,
coinbase_address: Address,
jwt_secret: Vec<u8>,
}

pub async fn start_proposer(store: Store) {
info!("Starting Proposer");

if let Err(e) = read_env_file() {
panic!("Failed to read .env file: {e}");
error!("Failed to read .env file: {e}");
return;
}

let mut task_set = JoinSet::new();
Expand All @@ -39,10 +41,14 @@ pub async fn start_proposer(store: Store) {
match res {
Ok(Ok(_)) => {}
Ok(Err(err)) => {
panic!("Error starting Proposer: {err}");
error!("Error starting Proposer: {err}");
task_set.abort_all();
break;
}
Err(err) => {
panic!("Error starting Proposer: {err}");
error!("JoinSet error: {err}");
task_set.abort_all();
break;
}
};
}
Expand All @@ -63,10 +69,12 @@ impl Proposer {
proposer_config: &ProposerConfig,
engine_config: EngineApiConfig,
) -> Result<Self, ProposerError> {
let jwt_secret = std::fs::read(&engine_config.jwt_path)?;
Ok(Self {
engine_config,
block_production_interval: proposer_config.interval_ms,
coinbase_address: proposer_config.coinbase_address,
jwt_secret,
})
}

Expand All @@ -90,16 +98,9 @@ impl Proposer {
.ok_or(ProposerError::StorageDataIsNone)?
};

let Ok(jwt_secret) = std::fs::read(&self.engine_config.jwt_path) else {
panic!(
"Failed to read jwt_secret from: {}",
&self.engine_config.jwt_path
);
};

ethrex_dev::block_producer::start_block_producer(
self.engine_config.rpc_url.clone(),
jwt_secret.into(),
self.jwt_secret.clone().into(),
head_block_hash,
10,
self.block_production_interval,
Expand Down
1 change: 1 addition & 0 deletions crates/l2/prover/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,4 @@ gpu = ["risc0-zkvm/cuda"]
unwrap_used = "deny"
expect_used = "deny"
indexing_slicing = "deny"
panic = "deny"
7 changes: 5 additions & 2 deletions crates/l2/prover/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@ async fn main() {
.with_max_level(Level::INFO)
.finish();
if let Err(e) = tracing::subscriber::set_global_default(subscriber) {
error!("setting default subscriber failed: {e}");
error!("Failed setting tracing::subscriber: {e}");
return;
}

if let Err(e) = read_env_file() {
warn!("Failed to read .env file: {e}");
}

let Ok(config) = ProverClientConfig::from_env() else {
panic!("Failed to read ProverClientConfig from .env file");
error!("Failed to read ProverClientConfig from .env file");
return;
};

debug!("Prover Client has started");
init_client(config).await;
}
1 change: 1 addition & 0 deletions crates/l2/sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ path = "src/sdk.rs"
unwrap_used = "deny"
expect_used = "deny"
indexing_slicing = "deny"
panic = "deny"
4 changes: 3 additions & 1 deletion crates/l2/sdk/src/sdk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ pub async fn wait_for_transaction_receipt(
println!("[{try}/{max_retries}] Retrying to get transaction receipt for {tx_hash:#x}");

if max_retries == r#try {
panic!("Transaction receipt for {tx_hash:#x} not found after {max_retries} retries");
return Err(EthClientError::Custom(format!(
"Transaction receipt for {tx_hash:#x} not found after {max_retries} retries"
)));
}
r#try += 1;

Expand Down

0 comments on commit f90dcaa

Please sign in to comment.