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

refactor(l2): forbid panics #1367

Merged
merged 3 commits into from
Dec 3, 2024
Merged
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
88 changes: 0 additions & 88 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/l2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ expect_used = "deny"
indexing_slicing = "deny"
as_conversions = "deny"
unnecessary_cast = "warn"
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 @@ -31,3 +31,4 @@ expect_used = "deny"
indexing_slicing = "deny"
as_conversions = "deny"
unnecessary_cast = "warn"
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
8 changes: 1 addition & 7 deletions crates/l2/prover/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ ethrex-l2.workspace = true
zkvm_interface = { path = "./zkvm/interface", default-features = false }
risc0-zkvm = { version = "1.1.2" }

# revm (this dep is temporary, should be replaced with ethrex-vm after ExecutionDB is completely integrated into the L1)
revm = { version = "14.0.3", features = [
"std",
"serde",
"kzg-rs",
], default-features = false }

[dev-dependencies]
ethrex-vm.workspace = true
ethrex-storage.workspace = true
Expand All @@ -57,3 +50,4 @@ expect_used = "deny"
indexing_slicing = "deny"
as_conversions = "deny"
unnecessary_cast = "warn"
panic = "deny"
Loading
Loading