From 17c4fa831f54d8573554551925b91165a58a4eab Mon Sep 17 00:00:00 2001 From: mohiiit Date: Tue, 24 Dec 2024 17:37:09 +0530 Subject: [PATCH] chore: removed expect --- crates/orchestrator/src/jobs/da_job/mod.rs | 46 ++++++++++--------- .../src/jobs/state_update_job/utils.rs | 2 +- crates/orchestrator/src/queue/job_queue.rs | 3 +- crates/orchestrator/src/workers/snos.rs | 12 +++-- .../atlantic-service/src/client.rs | 3 +- crates/settlement-clients/ethereum/src/lib.rs | 20 ++++---- crates/settlement-clients/starknet/src/lib.rs | 4 +- 7 files changed, 49 insertions(+), 41 deletions(-) diff --git a/crates/orchestrator/src/jobs/da_job/mod.rs b/crates/orchestrator/src/jobs/da_job/mod.rs index 54e96655..ab71ab6d 100644 --- a/crates/orchestrator/src/jobs/da_job/mod.rs +++ b/crates/orchestrator/src/jobs/da_job/mod.rs @@ -125,7 +125,11 @@ impl Job for DaJob { let blob_data_biguint = convert_to_biguint(blob_data.clone()); tracing::trace!(job_id = ?job.id, "Converted blob data to BigUint"); - let transformed_data = fft_transformation(blob_data_biguint); + let transformed_data = + fft_transformation(blob_data_biguint).wrap_err("Failed to apply FFT transformation").map_err(|e| { + tracing::error!(job_id = ?job.id, error = ?e, "Failed to apply FFT transformation"); + JobError::Other(OtherError(e)) + })?; // data transformation on the data tracing::trace!(job_id = ?job.id, "Applied FFT transformation"); @@ -204,17 +208,18 @@ impl Job for DaJob { } #[tracing::instrument(skip(elements))] -pub fn fft_transformation(elements: Vec) -> Vec { +pub fn fft_transformation(elements: Vec) -> Result, JobError> { let xs: Vec = (0..*BLOB_LEN) .map(|i| { let bin = format!("{:012b}", i); let bin_rev = bin.chars().rev().collect::(); - GENERATOR.modpow( - &BigUint::from_str_radix(&bin_rev, 2).expect("Not able to convert the parameters into exponent."), - &BLS_MODULUS, - ) + let exponent = BigUint::from_str_radix(&bin_rev, 2) + .wrap_err("Failed to convert binary string to exponent") + .map_err(|e| JobError::Other(OtherError(e)))?; + Ok(GENERATOR.modpow(&exponent, &BLS_MODULUS)) }) - .collect(); + .collect::, JobError>>()?; + let n = elements.len(); let mut transform: Vec = vec![BigUint::zero(); n]; @@ -223,7 +228,7 @@ pub fn fft_transformation(elements: Vec) -> Vec { transform[i] = (transform[i].clone().mul(&xs[i]).add(&elements[j])).rem(&*BLS_MODULUS); } } - transform + Ok(transform) } pub fn convert_to_biguint(elements: Vec) -> Vec { @@ -310,7 +315,7 @@ pub async fn state_update_to_blob_data( nonce = Some(get_current_nonce_result); } - let da_word = da_word(class_flag.is_some(), nonce, storage_entries.len() as u64); + let da_word = da_word(class_flag.is_some(), nonce, storage_entries.len() as u64)?; blob_data.push(address); blob_data.push(da_word); @@ -355,7 +360,7 @@ async fn store_blob_data(blob_data: Vec, block_number: u64, config: Arc /// DA word encoding: /// |---padding---|---class flag---|---new nonce---|---num changes---| /// 127 bits 1 bit 64 bits 64 bits -fn da_word(class_flag: bool, nonce_change: Option, num_changes: u64) -> Felt { +fn da_word(class_flag: bool, nonce_change: Option, num_changes: u64) -> Result { // padding of 127 bits let mut binary_string = "0".repeat(127); @@ -367,13 +372,8 @@ fn da_word(class_flag: bool, nonce_change: Option, num_changes: u64) -> Fe } // checking for nonce here - if let Some(_new_nonce) = nonce_change { - let bytes: [u8; 32] = nonce_change - .expect( - "Not able to convert the nonce_change var into [u8; 32] type. Possible Error : Improper parameter \ - length.", - ) - .to_bytes_be(); + if let Some(new_nonce) = nonce_change { + let bytes: [u8; 32] = new_nonce.to_bytes_be(); let biguint = BigUint::from_bytes_be(&bytes); let binary_string_local = format!("{:b}", biguint); let padded_binary_string = format!("{:0>64}", binary_string_local); @@ -387,12 +387,16 @@ fn da_word(class_flag: bool, nonce_change: Option, num_changes: u64) -> Fe let padded_binary_string = format!("{:0>64}", binary_representation); binary_string += &padded_binary_string; - let biguint = BigUint::from_str_radix(binary_string.as_str(), 2).expect("Invalid binary string"); + let biguint = BigUint::from_str_radix(binary_string.as_str(), 2) + .wrap_err("Failed to convert binary string to BigUint") + .map_err(|e| JobError::Other(OtherError(e)))?; // Now convert the BigUint to a decimal string let decimal_string = biguint.to_str_radix(10); - Felt::from_dec_str(&decimal_string).expect("issue while converting to fieldElement") + Felt::from_dec_str(&decimal_string) + .wrap_err("Failed to convert decimal string to FieldElement") + .map_err(|e| JobError::Other(OtherError(e))) } fn refactor_state_update(state_update: &mut StateDiff) { @@ -453,7 +457,7 @@ pub mod test { #[case] expected: String, ) { let new_nonce = if new_nonce > 0 { Some(Felt::from(new_nonce)) } else { None }; - let da_word = da_word(class_flag, new_nonce, num_changes); + let da_word = da_word(class_flag, new_nonce, num_changes).expect("Failed to create DA word"); let expected = Felt::from_dec_str(expected.as_str()).unwrap(); assert_eq!(da_word, expected); } @@ -562,7 +566,7 @@ pub mod test { // converting the data to its original format let ifft_blob_data = blob::recover(original_blob_data.clone()); // applying the fft function again on the original format - let fft_blob_data = fft_transformation(ifft_blob_data); + let fft_blob_data = fft_transformation(ifft_blob_data).expect("FFT transformation failed during test"); // ideally the data after fft transformation and the data before ifft should be same. assert_eq!(fft_blob_data, original_blob_data); diff --git a/crates/orchestrator/src/jobs/state_update_job/utils.rs b/crates/orchestrator/src/jobs/state_update_job/utils.rs index ab8bb7fb..a6acfe4e 100644 --- a/crates/orchestrator/src/jobs/state_update_job/utils.rs +++ b/crates/orchestrator/src/jobs/state_update_job/utils.rs @@ -59,7 +59,7 @@ pub fn bytes_to_vec_u8(bytes: &[u8]) -> color_eyre::Result> { let trimmed = line.trim(); assert!(!trimmed.is_empty()); - let result = U256::from_str(trimmed).expect("Unable to convert line"); + let result = U256::from_str(trimmed)?; let res_vec = result.to_be_bytes_vec(); let hex = to_padded_hex(res_vec.as_slice()); let vec_hex = hex_string_to_u8_vec(&hex) diff --git a/crates/orchestrator/src/queue/job_queue.rs b/crates/orchestrator/src/queue/job_queue.rs index 1527f493..93d079ff 100644 --- a/crates/orchestrator/src/queue/job_queue.rs +++ b/crates/orchestrator/src/queue/job_queue.rs @@ -231,7 +231,8 @@ fn parse_worker_message(message: &Delivery) -> Result job.internal_id.parse::().expect("Failed to parse job internal ID to u64"), - None => "0".to_string().parse::().expect("Failed to parse '0' to u64"), - }; + let latest_job_id = latest_job_in_db + .map(|job| { + job.internal_id + .parse::() + .wrap_err_with(|| format!("Failed to parse job internal ID: {}", job.internal_id)) + }) + .unwrap_or(Ok(0))?; // To be used when testing in specific block range let block_start = if let Some(min_block_to_process) = config.service_config().min_block_to_process { diff --git a/crates/prover-clients/atlantic-service/src/client.rs b/crates/prover-clients/atlantic-service/src/client.rs index 5b00436d..62ee2487 100644 --- a/crates/prover-clients/atlantic-service/src/client.rs +++ b/crates/prover-clients/atlantic-service/src/client.rs @@ -85,8 +85,7 @@ impl AtlanticClient { ) .send() .await - .map_err(AtlanticError::AddJobFailure) - .expect("Failed to add job"); + .map_err(AtlanticError::AddJobFailure)?; if response.status().is_success() { response.json().await.map_err(AtlanticError::AddJobFailure) diff --git a/crates/settlement-clients/ethereum/src/lib.rs b/crates/settlement-clients/ethereum/src/lib.rs index ce620ae4..4f978348 100644 --- a/crates/settlement-clients/ethereum/src/lib.rs +++ b/crates/settlement-clients/ethereum/src/lib.rs @@ -19,7 +19,7 @@ use alloy::signers::local::PrivateKeySigner; use alloy_primitives::Bytes; use async_trait::async_trait; use c_kzg::{Blob, Bytes32, KzgCommitment, KzgProof, KzgSettings}; -use color_eyre::eyre::{bail, eyre, Ok}; +use color_eyre::eyre::{bail, Ok}; use color_eyre::Result; use conversion::{get_input_data_for_eip_4844, prepare_sidecar}; use settlement_client_interface::{SettlementClient, SettlementVerificationStatus}; @@ -36,6 +36,7 @@ pub mod tests; pub mod types; use alloy::providers::RootProvider; use alloy::transports::http::Http; +use color_eyre::eyre::WrapErr; use lazy_static::lazy_static; use mockall::automock; use reqwest::Client; @@ -164,7 +165,10 @@ impl EthereumSettlementClient { &KZG_SETTINGS, )?; - if !eval { Err(eyre!("ERROR : Assertion failed, not able to verify the proof.")) } else { Ok(kzg_proof) } + if !eval { + bail!("ERROR : Assertion failed, not able to verify the proof."); + } + Ok(kzg_proof) } } @@ -238,14 +242,10 @@ impl SettlementClient for EthereumSettlementClient { // x_0_value : program_output[10] // Updated with starknet 0.13.2 spec - let kzg_proof = Self::build_proof( - state_diff, - Bytes32::from_bytes(program_output[X_0_POINT_OFFSET].as_slice()) - .expect("Not able to get x_0 point params."), - y_0, - ) - .expect("Unable to build KZG proof for given params.") - .to_owned(); + let x_0_point = Bytes32::from_bytes(program_output[X_0_POINT_OFFSET].as_slice()) + .wrap_err("Failed to get x_0 point params")?; + + let kzg_proof = Self::build_proof(state_diff, x_0_point, y_0).wrap_err("Failed to build KZG proof")?.to_owned(); let input_bytes = get_input_data_for_eip_4844(program_output, kzg_proof)?; diff --git a/crates/settlement-clients/starknet/src/lib.rs b/crates/settlement-clients/starknet/src/lib.rs index ade0d17b..b9b5598b 100644 --- a/crates/settlement-clients/starknet/src/lib.rs +++ b/crates/settlement-clients/starknet/src/lib.rs @@ -8,7 +8,7 @@ use std::sync::Arc; use appchain_core_contract_client::clients::StarknetCoreContractClient; use appchain_core_contract_client::interfaces::core_contract::CoreContract; use async_trait::async_trait; -use color_eyre::eyre::eyre; +use color_eyre::eyre::{eyre, WrapErr}; use color_eyre::Result; use crypto_bigint::Encoding; use lazy_static::lazy_static; @@ -250,7 +250,7 @@ impl SettlementClient for StarknetSettlementClient { return Err(eyre!("Could not fetch last block number from core contract.")); } - Ok(u64_from_felt(block_number[0]).expect("Failed to convert to u64")) + u64_from_felt(block_number[0]).wrap_err("Failed to convert block number from Felt to u64") } /// Returns the nonce for the wallet in use.