Skip to content

Commit

Permalink
apps: fix lint warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
tzemanovic committed May 13, 2024
1 parent f1ae1fa commit 5871e3d
Show file tree
Hide file tree
Showing 25 changed files with 154 additions and 58 deletions.
1 change: 1 addition & 0 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/apps/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ serde_json = {workspace = true, features = ["raw_value"]}
serde.workspace = true
sha2.workspace = true
signal-hook.workspace = true
smooth-operator.workspace = true
sysinfo.workspace = true
tar.workspace = true
tempfile.workspace = true
Expand Down
2 changes: 2 additions & 0 deletions crates/apps/src/lib/bench_utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Library code for benchmarks provides a wrapper of the ledger's shell
//! `BenchShell` and helper functions to generate transactions.
#![allow(clippy::arithmetic_side_effects)]

use std::cell::RefCell;
use std::collections::BTreeSet;
use std::fs::{File, OpenOptions};
Expand Down
2 changes: 2 additions & 0 deletions crates/apps/src/lib/client/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::arithmetic_side_effects)]

pub mod masp;
pub mod rpc;
pub mod tx;
Expand Down
6 changes: 5 additions & 1 deletion crates/apps/src/lib/config/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ impl<'de> Deserialize<'de> for GenesisAddress {
impl<'de> serde::de::Visitor<'de> for FieldVisitor {
type Value = GenesisAddress;

fn expecting(&self, formatter: &mut Formatter) -> std::fmt::Result {
fn expecting(
&self,
formatter: &mut Formatter<'_>,
) -> std::fmt::Result {
formatter.write_str(
"a bech32m encoded public key or an established address",
)
Expand Down Expand Up @@ -324,6 +327,7 @@ pub struct Parameters {
///
/// This includes adding the Ethereum bridge parameters and
/// adding a specified number of validators.
#[allow(clippy::arithmetic_side_effects)]
#[cfg(all(any(test, feature = "benches"), not(feature = "integration")))]
pub fn make_dev_genesis(
num_validators: u64,
Expand Down
15 changes: 12 additions & 3 deletions crates/apps/src/lib/config/genesis/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,14 @@ impl Finalized {
if !is_localhost {
set_ip(&mut config.ledger.cometbft.rpc.laddr, "0.0.0.0");
}
set_port(&mut config.ledger.cometbft.rpc.laddr, first_port + 1);
set_port(&mut config.ledger.cometbft.proxy_app, first_port + 2);
set_port(
&mut config.ledger.cometbft.rpc.laddr,
first_port.checked_add(1).expect("Port must not overflow"),
);
set_port(
&mut config.ledger.cometbft.proxy_app,
first_port.checked_add(2).expect("Port must not overflow"),
);

// Validator node should turned off peer exchange reactor
config.ledger.cometbft.p2p.pex = false;
Expand Down Expand Up @@ -310,7 +316,10 @@ impl Finalized {
.ok()
.map(Hash::sha256);

let min_duration: i64 = 60 * 60 * 24 * 365 / (epochs_per_year as i64);
let epy_i64 = i64::try_from(epochs_per_year)
.expect("`epochs_per_year` must not exceed `i64::MAX`");
#[allow(clippy::arithmetic_side_effects)]
let min_duration: i64 = 60 * 60 * 24 * 365 / epy_i64;
let epoch_duration = EpochDuration {
min_num_of_blocks,
min_duration: namada::core::time::Duration::seconds(min_duration)
Expand Down
10 changes: 7 additions & 3 deletions crates/apps/src/lib/node/ledger/broadcaster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,18 @@ impl Broadcaster {
} else {
DEFAULT_BROADCAST_TIMEOUT
};
let now = {
#[allow(clippy::disallowed_methods)]
time::Instant::now()
};
let status_result = time::Sleep {
strategy: time::Constant(time::Duration::from_secs(1)),
}
.timeout(
#[allow(clippy::arithmetic_side_effects)]
{
#[allow(clippy::disallowed_methods)]
time::Instant::now()
} + time::Duration::from_secs(timeout),
now + time::Duration::from_secs(timeout)
},
|| async {
match self.client.status().await {
Ok(status) => ControlFlow::Break(status),
Expand Down
5 changes: 4 additions & 1 deletion crates/apps/src/lib/node/ledger/ethereum_oracle/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ pub mod eth_events {
/// Check if the minimum number of confirmations has been
/// reached at the input block height.
pub fn is_confirmed(&self, height: &Uint256) -> bool {
self.confirmations <= height.clone() - self.block_height.clone()
use num_traits::CheckedSub;

self.confirmations
<= height.checked_sub(&self.block_height).unwrap_or_default()
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/apps/src/lib/node/ledger/ethereum_oracle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ async fn process_events_in_block<C: RpcClient>(
let last_processed_block_ref = oracle.last_processed_block.borrow();
let last_processed_block = last_processed_block_ref.as_ref();
let backoff = oracle.backoff;
#[allow(clippy::disallowed_methods)]
#[allow(clippy::arithmetic_side_effects)]
let deadline = Instant::now() + oracle.ceiling;
let latest_block = match oracle
.client
Expand Down
14 changes: 8 additions & 6 deletions crates/apps/src/lib/node/ledger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ async fn run_aux_setup(
let available_memory_bytes = sys.available_memory();
tracing::info!(
"Available memory: {}",
Byte::from_bytes(available_memory_bytes as u128)
Byte::from_bytes(u128::from(available_memory_bytes))
.get_appropriate_unit(true)
);
available_memory_bytes
Expand All @@ -421,7 +421,7 @@ async fn run_aux_setup(
};
tracing::info!(
"VP WASM compilation cache size: {}",
Byte::from_bytes(vp_wasm_compilation_cache as u128)
Byte::from_bytes(u128::from(vp_wasm_compilation_cache))
.get_appropriate_unit(true)
);

Expand All @@ -444,7 +444,7 @@ async fn run_aux_setup(
};
tracing::info!(
"Tx WASM compilation cache size: {}",
Byte::from_bytes(tx_wasm_compilation_cache as u128)
Byte::from_bytes(u128::from(tx_wasm_compilation_cache))
.get_appropriate_unit(true)
);

Expand All @@ -464,7 +464,7 @@ async fn run_aux_setup(
};
tracing::info!(
"RocksDB block cache size: {}",
Byte::from_bytes(db_block_cache_size_bytes as u128)
Byte::from_bytes(u128::from(db_block_cache_size_bytes))
.get_appropriate_unit(true)
);

Expand Down Expand Up @@ -529,8 +529,10 @@ fn start_abci_broadcaster_shell(
};

// Setup DB cache, it must outlive the DB instance that's in the shell
let db_cache =
rocksdb::Cache::new_lru_cache(db_block_cache_size_bytes as usize);
let db_cache = rocksdb::Cache::new_lru_cache(
usize::try_from(db_block_cache_size_bytes)
.expect("`db_block_cache_size_bytes` must not exceed `usize::MAX`"),
);

// Construct our ABCI application.
let tendermint_mode = config.shell.tendermint_mode.clone();
Expand Down
29 changes: 23 additions & 6 deletions crates/apps/src/lib/node/ledger/shell/block_alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,15 @@ impl<State> BlockAllocator<State> {
/// to each [`TxBin`] instance in a [`BlockAllocator`].
#[inline]
fn unoccupied_space_in_bytes(&self) -> u64 {
let total_bin_space =
self.protocol_txs.occupied + self.normal_txs.space.occupied;
self.block.allotted - total_bin_space
let total_bin_space = self
.protocol_txs
.occupied
.checked_add(self.normal_txs.space.occupied)
.expect("Shouldn't overflow");
self.block
.allotted
.checked_sub(total_bin_space)
.expect("Shouldn't underflow")
}
}

Expand All @@ -220,7 +226,9 @@ impl<R: Resource> TxBin<R> {
/// Return the amount of resource left in this [`TxBin`].
#[inline]
pub fn resource_left(&self) -> u64 {
self.allotted - self.occupied
self.allotted
.checked_sub(self.occupied)
.expect("Shouldn't underflow")
}

/// Construct a new [`TxBin`], with a capacity of `max_capacity`.
Expand Down Expand Up @@ -255,7 +263,10 @@ impl<R: Resource> TxBin<R> {
bin_resource: bin_size,
});
}
let occupied = self.occupied + resource;
let occupied = self
.occupied
.checked_add(resource)
.expect("Shouldn't overflow");
if occupied <= self.allotted {
self.occupied = occupied;
Ok(())
Expand Down Expand Up @@ -322,14 +333,20 @@ pub mod threshold {

/// Return a [`Threshold`] over some free space.
pub fn over(self, free_space_in_bytes: u64) -> u64 {
(self.0 * free_space_in_bytes).to_integer()
use num_traits::ops::checked::CheckedMul;
(self
.0
.checked_mul(&free_space_in_bytes.into())
.expect("Must not overflow"))
.to_integer()
}
}

/// Divide free space in half.
pub const ONE_HALF: Threshold = Threshold::new(1, 2);
}

#[allow(clippy::arithmetic_side_effects, clippy::cast_possible_truncation)]
#[cfg(test)]
mod tests {

Expand Down
24 changes: 19 additions & 5 deletions crates/apps/src/lib/node/ledger/shell/finalize_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ where
"Will begin a new epoch {} in {} blocks starting at height {}",
current_epoch.next(),
EPOCH_SWITCH_BLOCKS_DELAY,
height.0 + u64::from(EPOCH_SWITCH_BLOCKS_DELAY)
height
.0
.checked_add(u64::from(EPOCH_SWITCH_BLOCKS_DELAY))
.expect("Shouldn't overflow")
);
}
tracing::debug!(
Expand Down Expand Up @@ -624,10 +627,20 @@ where
// Get the number of blocks in the last epoch
let first_block_of_last_epoch =
self.state.in_mem().block.pred_epochs.first_block_heights
[last_epoch.0 as usize]
.0;
let num_blocks_in_last_epoch =
self.state.in_mem().block.height.0 - first_block_of_last_epoch;
[usize::try_from(last_epoch.0)
.expect("Last epoch shouldn't exceed `usize::MAX`")]
.0;
let num_blocks_in_last_epoch = self
.state
.in_mem()
.block
.height
.0
.checked_sub(first_block_of_last_epoch)
.expect(
"First block of last epoch must always be lower than or equal \
to current block height",
);

// PoS inflation
namada_proof_of_stake::rewards::apply_inflation(
Expand Down Expand Up @@ -741,6 +754,7 @@ fn pos_votes_from_abci(

/// We test the failure cases of [`finalize_block`]. The happy flows
/// are covered by the e2e tests.
#[allow(clippy::arithmetic_side_effects, clippy::cast_possible_truncation)]
#[cfg(test)]
mod test_finalize_block {
use std::collections::BTreeMap;
Expand Down
8 changes: 5 additions & 3 deletions crates/apps/src/lib/node/ledger/shell/init_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,10 @@ where
let ts: protobuf::Timestamp = init.time.into();
let initial_height = init.initial_height.into();
// TODO hacky conversion, depends on https://github.com/informalsystems/tendermint-rs/issues/870
let genesis_time: DateTimeUtc = (Utc
.timestamp_opt(ts.seconds, ts.nanos as u32))
let genesis_time: DateTimeUtc = (Utc.timestamp_opt(
ts.seconds,
u32::try_from(ts.nanos).expect("Time nanos cannot be negative"),
))
.single()
.expect("genesis time should be a valid timestamp")
.into();
Expand Down Expand Up @@ -757,7 +759,7 @@ where
pub fn new(
shell: &'shell mut Shell<D, H>,
dry_run: bool,
) -> InitChainValidation<D, H> {
) -> InitChainValidation<'_, D, H> {
Self {
shell,
errors: vec![],
Expand Down
Loading

0 comments on commit 5871e3d

Please sign in to comment.