From 4dbf92eabd4c5dcef6864fe886046a0d892881cf Mon Sep 17 00:00:00 2001 From: Willem Wyndham Date: Mon, 11 Mar 2024 17:25:09 -0400 Subject: [PATCH] fix: clippy --- cmd/soroban-rpc/lib/preflight/src/lib.rs | 58 +++++++++++++----------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/cmd/soroban-rpc/lib/preflight/src/lib.rs b/cmd/soroban-rpc/lib/preflight/src/lib.rs index fd80b018..cc0d9783 100644 --- a/cmd/soroban-rpc/lib/preflight/src/lib.rs +++ b/cmd/soroban-rpc/lib/preflight/src/lib.rs @@ -20,6 +20,7 @@ use soroban_simulation::simulation::{ }; use soroban_simulation::{AutoRestoringSnapshotSource, NetworkConfig, SnapshotSourceWithArchive}; use std::cell::RefCell; +use std::convert::TryInto; use std::ffi::{CStr, CString}; use std::panic; use std::ptr::null_mut; @@ -135,11 +136,7 @@ impl CPreflightResult { let mut result = Self { error: string_to_c(error), auth: xdr_vec_to_c(invoke_hf_result.auth), - result: option_xdr_to_c( - invoke_hf_result - .invoke_result - .map_or_else(|_| None, |v| Some(v)), - ), + result: option_xdr_to_c(invoke_hf_result.invoke_result.ok()), min_fee: invoke_hf_result .transaction_data .as_ref() @@ -147,8 +144,8 @@ impl CPreflightResult { transaction_data: option_xdr_to_c(invoke_hf_result.transaction_data), // TODO: Diagnostic and contract events should be separated in the response events: xdr_vec_to_c(invoke_hf_result.diagnostic_events), - cpu_instructions: invoke_hf_result.simulated_instructions as u64, - memory_bytes: invoke_hf_result.simulated_memory as u64, + cpu_instructions: invoke_hf_result.simulated_instructions.into(), + memory_bytes: invoke_hf_result.simulated_memory.into(), ..Default::default() }; if let Some(p) = restore_preamble { @@ -222,10 +219,16 @@ fn preflight_invoke_hf_op_or_maybe_panic( let mut adjustment_config = SimulationAdjustmentConfig::default_adjustment(); // It would be reasonable to extend `resource_config` to be compatible with `adjustment_config` // in order to let the users customize the resource/fee adjustments in a more granular fashion. - adjustment_config.instructions.additive_factor = adjustment_config - .instructions - .additive_factor - .max(resource_config.instruction_leeway.min(u32::MAX as u64) as u32); + // Is safe to unwrap unchecked because the value is at most u32::MAX. + adjustment_config.instructions.additive_factor = unsafe { + adjustment_config.instructions.additive_factor.max( + resource_config + .instruction_leeway + .min(u64::from(u32::MAX)) + .try_into() + .unwrap_unchecked(), + ) + }; // Here we assume that no input auth means that the user requests the recording auth. let auth_entries = if invoke_hf_op.auth.is_empty() { None @@ -291,10 +294,10 @@ fn preflight_footprint_ttl_op_or_maybe_panic( preflight_extend_ttl_op(extend_op, footprint.read_only.as_slice(), go_storage, &network_config, &ledger_info) }, OperationBody::RestoreFootprint(_) => { - preflight_restore_op(footprint.read_write.as_slice(), go_storage, &network_config, &ledger_info) + Ok(preflight_restore_op(footprint.read_write.as_slice(), go_storage, &network_config, &ledger_info)) } _ => Err(anyhow!("encountered unsupported operation type: '{:?}', instead of 'ExtendFootprintTtl' or 'RestoreFootprint' operations.", - op_body.discriminant()).into()) + op_body.discriminant())) } } fn preflight_extend_ttl_op( @@ -307,9 +310,9 @@ fn preflight_extend_ttl_op( let auto_restore_snapshot = AutoRestoringSnapshotSource::new(go_storage.clone(), ledger_info)?; let simulation_result = simulate_extend_ttl_op( &auto_restore_snapshot, - &network_config, + network_config, &SimulationAdjustmentConfig::default_adjustment(), - &ledger_info, + ledger_info, keys_to_extend, extend_op.extend_to, ); @@ -317,9 +320,9 @@ fn preflight_extend_ttl_op( Ok(r) => ( Some(r.transaction_data), auto_restore_snapshot.simulate_restore_keys_op( - &network_config, + network_config, &SimulationAdjustmentConfig::default_adjustment(), - &ledger_info, + ledger_info, ), ), Err(e) => (None, Err(e)), @@ -338,22 +341,22 @@ fn preflight_restore_op( go_storage: Rc, network_config: &NetworkConfig, ledger_info: &LedgerInfo, -) -> Result { +) -> CPreflightResult { let simulation_result = simulate_restore_op( go_storage.as_ref(), - &network_config, + network_config, &SimulationAdjustmentConfig::default_adjustment(), - &ledger_info, + ledger_info, keys_to_restore, ); let error_str = extract_error_string(&simulation_result, go_storage.as_ref()); - Ok(CPreflightResult::new_from_transaction_data( + CPreflightResult::new_from_transaction_data( simulation_result .map(|r| Some(r.transaction_data)) .unwrap_or(None), None, error_str, - )) + ) } fn preflight_error(str: String) -> CPreflightResult { @@ -573,11 +576,11 @@ impl SnapshotSourceWithArchive for GoLedgerStorage { Err(e) => { // Store the internal error in the storage as the info won't be propagated from simulation. if let Ok(mut err) = self.internal_error.try_borrow_mut() { - *err = Some(e.into()); + *err = Some(e); } // Errors that occur in storage are not recoverable, so we force host to halt by passing // it an internal error. - return Err((ScErrorType::Storage, ScErrorCode::InternalError).into()); + Err((ScErrorType::Storage, ScErrorCode::InternalError).into()) } } } @@ -589,9 +592,10 @@ fn extract_error_string(simulation_result: &Result, go_storage: &GoLedgerS Err(e) => { // Override any simulation result with a storage error (if any). Simulation does not propagate the storage // errors, but these provide more exact information on the root cause. - match go_storage.internal_error.borrow().as_ref() { - Some(e) => format!("{e:?}"), - None => format!("{e:?}"), + if let Some(e) = go_storage.internal_error.borrow().as_ref() { + format!("{e:?}") + } else { + format!("{e:?}") } } }