Skip to content

Commit

Permalink
chore: fix clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal committed Oct 5, 2023
1 parent bce5e56 commit 186aaa4
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub struct Contract;

#[contractimpl]
impl Contract {
#[allow(clippy::unnecessary_fold)]
pub fn add(a: UdtEnum, b: UdtEnum) -> i64 {
let a = match a {
UdtEnum::UdtA => 0,
Expand All @@ -45,7 +46,7 @@ impl Contract {
UdtEnum::UdtA => 0,
UdtEnum::UdtB(udt) => udt.a + udt.b,
UdtEnum::UdtC(val) => val as i64,
UdtEnum::UdtD(tup) => tup.0 + tup.1.iter().fold(0i64, |sum, i| sum + i),
UdtEnum::UdtD(tup) => tup.0 + tup.1.iter().sum::<i64>(),
};
a + b
}
Expand Down
33 changes: 20 additions & 13 deletions cmd/soroban-rpc/lib/preflight/src/fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ use std::convert::{TryFrom, TryInto};
/// Serialize XDR size for any `ExpirationEntry` ledger entry.
const EXPIRATION_ENTRY_SIZE: u32 = 48;

// TODO: this should perhaps be an new type
#[allow(clippy::too_many_arguments)]
pub(crate) fn compute_host_function_transaction_data_and_min_fee(
op: &InvokeHostFunctionOp,
pre_storage: &LedgerStorage,
post_storage: &Storage,
budget: &Budget,
events: &Vec<DiagnosticEvent>,
events: &[DiagnosticEvent],
invocation_result: &ScVal,
bucket_list_size: u64,
current_ledger_seq: u32,
Expand Down Expand Up @@ -127,8 +129,9 @@ fn estimate_max_transaction_size_for_operation(
Ok(u32::try_from(envelope_size)?)
}

#[allow(clippy::cast_possible_truncation)]
fn calculate_host_function_soroban_resources(
ledger_changes: &Vec<LedgerEntryChange>,
ledger_changes: &[LedgerEntryChange],
footprint: &Footprint,
budget: &Budget,
) -> Result<SorobanResources> {
Expand Down Expand Up @@ -162,6 +165,7 @@ fn calculate_host_function_soroban_resources(
})
}

#[allow(clippy::cast_possible_wrap)]
fn get_fee_configurations(
ledger_storage: &LedgerStorage,
bucket_list_size: u64,
Expand Down Expand Up @@ -232,18 +236,19 @@ fn get_fee_configurations(
}

// Calculate the implicit ExpirationEntry bytes that will be read for expirable LedgerEntries
fn calculate_expiration_entry_bytes(ledger_entries: &Vec<LedgerKey>) -> Result<u32> {
Ok(ledger_entries
fn calculate_expiration_entry_bytes(ledger_entries: &[LedgerKey]) -> u32 {
ledger_entries
.iter()
.map(|lk| match lk {
LedgerKey::ContractData(_) | LedgerKey::ContractCode(_) => EXPIRATION_ENTRY_SIZE,
_ => 0,
})
.sum())
.sum()
}

#[allow(clippy::cast_possible_truncation)]
fn calculate_unmodified_ledger_entry_bytes(
ledger_entries: &Vec<LedgerKey>,
ledger_entries: &[LedgerKey],
pre_storage: &LedgerStorage,
include_expired: bool,
) -> Result<u32> {
Expand All @@ -258,7 +263,7 @@ fn calculate_unmodified_ledger_entry_bytes(
Ok(res as u32)
}

fn calculate_contract_events_size_bytes(events: &Vec<DiagnosticEvent>) -> Result<u32> {
fn calculate_contract_events_size_bytes(events: &[DiagnosticEvent]) -> Result<u32> {
let mut res: u32 = 0;
for e in events {
if e.event.type_ != ContractEventType::Contract
Expand Down Expand Up @@ -302,7 +307,7 @@ fn finalize_transaction_data_and_min_fee(
.context("failed to obtain configuration settings from the network")?;
let (non_refundable_fee, refundable_fee) =
compute_transaction_resource_fee(transaction_resources, &fee_configuration);
let rent_fee = compute_rent_fee(&rent_changes, &rent_fee_configuration, current_ledger_seq);
let rent_fee = compute_rent_fee(rent_changes, &rent_fee_configuration, current_ledger_seq);
let transaction_data = SorobanTransactionData {
resources: soroban_resources,
refundable_fee: refundable_fee + rent_fee,
Expand Down Expand Up @@ -330,10 +335,10 @@ pub(crate) fn compute_bump_footprint_exp_transaction_data_and_min_fee(
)
.context("cannot compute bump rent changes")?;

let expiration_bytes: u32 = calculate_expiration_entry_bytes(footprint.read_only.as_vec())?;
let expiration_bytes = calculate_expiration_entry_bytes(footprint.read_only.as_slice());

let unmodified_entry_bytes = calculate_unmodified_ledger_entry_bytes(
footprint.read_only.as_vec(),
footprint.read_only.as_slice(),
ledger_storage,
false,
)
Expand Down Expand Up @@ -363,7 +368,7 @@ pub(crate) fn compute_bump_footprint_exp_transaction_data_and_min_fee(
contract_events_size_bytes: 0,
};
finalize_transaction_data_and_min_fee(
&ledger_storage,
ledger_storage,
&transaction_resources,
soroban_resources,
&rent_changes,
Expand All @@ -372,6 +377,7 @@ pub(crate) fn compute_bump_footprint_exp_transaction_data_and_min_fee(
)
}

#[allow(clippy::cast_possible_truncation)]
fn compute_bump_footprint_rent_changes(
footprint: &LedgerFootprint,
ledger_storage: &LedgerStorage,
Expand All @@ -380,7 +386,7 @@ fn compute_bump_footprint_rent_changes(
) -> Result<Vec<LedgerEntryRentChange>> {
let mut rent_changes: Vec<LedgerEntryRentChange> =
Vec::with_capacity(footprint.read_only.len());
for key in (&footprint).read_only.as_vec() {
for key in footprint.read_only.as_slice() {
let unmodified_entry_and_expiration = ledger_storage
.get(key, false)
.with_context(|| format!("cannot find bump footprint ledger entry with key {key:?}"))?;
Expand Down Expand Up @@ -426,7 +432,7 @@ pub(crate) fn compute_restore_footprint_transaction_data_and_min_fee(
)
.context("cannot compute restore rent changes")?;

let expiration_bytes: u32 = calculate_expiration_entry_bytes(footprint.read_write.as_vec())?;
let expiration_bytes = calculate_expiration_entry_bytes(footprint.read_write.as_vec());
let write_bytes = calculate_unmodified_ledger_entry_bytes(
footprint.read_write.as_vec(),
ledger_storage,
Expand Down Expand Up @@ -465,6 +471,7 @@ pub(crate) fn compute_restore_footprint_transaction_data_and_min_fee(
)
}

#[allow(clippy::cast_possible_truncation)]
fn compute_restore_footprint_rent_changes(
footprint: &LedgerFootprint,
ledger_storage: &LedgerStorage,
Expand Down
6 changes: 3 additions & 3 deletions cmd/soroban-rpc/lib/preflight/src/ledger_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl LedgerStorage {
if res.xdr.is_null() {
return Err(Error::NotFound);
}
let v = from_c_xdr(res.clone());
let v = from_c_xdr(res);
unsafe { FreeGoXDR(res) };
Ok(v)
}
Expand Down Expand Up @@ -250,7 +250,7 @@ impl SnapshotSource for LedgerStorage {
}
let entry_and_expiration =
<LedgerStorage>::get(self, key, false).map_err(HostError::from)?;
return Ok((entry_and_expiration.0.into(), entry_and_expiration.1));
Ok((entry_and_expiration.0.into(), entry_and_expiration.1))
}

fn has(&self, key: &Rc<LedgerKey>) -> Result<bool, HostError> {
Expand All @@ -260,6 +260,6 @@ impl SnapshotSource for LedgerStorage {
return Ok(false);
}
}
return result.map(|_| true);
result.map(|_| true)
}
}
2 changes: 1 addition & 1 deletion cmd/soroban-rpc/lib/preflight/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ fn option_xdr_to_c(v: Option<impl WriteXdr>) -> CXDR {
fn xdr_vec_to_c(v: Vec<impl WriteXdr>) -> CXDRVector {
let c_v = v.into_iter().map(xdr_to_c).collect();
let (array, len) = vec_to_c_array(c_v);
return CXDRVector { array, len };
CXDRVector { array, len }
}

fn string_to_c(str: String) -> *mut libc::c_char {
Expand Down
5 changes: 2 additions & 3 deletions cmd/soroban-rpc/lib/preflight/src/preflight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ fn host_events_to_diagnostic_events(events: &Events) -> Vec<DiagnosticEvent> {
}
res
}

#[allow(clippy::cast_sign_loss)]
fn get_budget_from_network_config_params(ledger_storage: &LedgerStorage) -> Result<Budget> {
let ConfigSettingEntry::ContractComputeV0(compute) =
ledger_storage.get_configuration_setting(ConfigSettingId::ContractComputeV0)?
Expand All @@ -228,10 +228,9 @@ fn get_budget_from_network_config_params(ledger_storage: &LedgerStorage) -> Resu
else {
bail!("unexpected config setting entry for CostParamsMemoryBytes key");
};

let budget = Budget::try_from_configs(
compute.tx_max_instructions as u64,
compute.tx_memory_limit as u64,
u64::from(compute.tx_memory_limit),
cost_params_cpu,
cost_params_memory,
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/soroban-rpc/lib/preflight/src/state_expiration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,5 @@ pub(crate) fn get_restored_ledger_sequence(
current_ledger_seq: u32,
min_persistent_entry_expiration: u32,
) -> u32 {
return current_ledger_seq + min_persistent_entry_expiration - 1;
current_ledger_seq + min_persistent_entry_expiration - 1
}

0 comments on commit 186aaa4

Please sign in to comment.