Skip to content

Commit

Permalink
fix: clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal authored and gitbutler-client committed Mar 26, 2024
1 parent 4f5c12c commit 4dbf92e
Showing 1 changed file with 31 additions and 27 deletions.
58 changes: 31 additions & 27 deletions cmd/soroban-rpc/lib/preflight/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -135,20 +136,16 @@ 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()
.map_or_else(|| 0, |r| r.resource_fee),
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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -307,19 +310,19 @@ 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,
);
let (maybe_transaction_data, maybe_restore_result) = match simulation_result {
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)),
Expand All @@ -338,22 +341,22 @@ fn preflight_restore_op(
go_storage: Rc<GoLedgerStorage>,
network_config: &NetworkConfig,
ledger_info: &LedgerInfo,
) -> Result<CPreflightResult> {
) -> 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 {
Expand Down Expand Up @@ -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())
}
}
}
Expand All @@ -589,9 +592,10 @@ fn extract_error_string<T>(simulation_result: &Result<T>, 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:?}")
}
}
}
Expand Down

0 comments on commit 4dbf92e

Please sign in to comment.