Skip to content

Commit

Permalink
chore: disallow casts with possible truncations (axelarnetwork#463)
Browse files Browse the repository at this point in the history
  • Loading branch information
cgorenflo authored Jun 19, 2024
1 parent 24d96ab commit f6096f8
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 17 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ tokio-util = "0.7.11"

[workspace.lints.clippy]
arithmetic_side_effects = "deny"
cast_possible_truncation = "deny"

[profile.release]
opt-level = 3
Expand Down
1 change: 1 addition & 0 deletions ampd/src/block_height_monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ mod tests {
use async_trait::async_trait;

#[test]
#[allow(clippy::cast_possible_truncation)]
async fn latest_block_height_should_work() {
let block: tendermint::Block =
serde_json::from_str(include_str!("tests/axelar_block.json")).unwrap();
Expand Down
2 changes: 2 additions & 0 deletions ampd/src/event_sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ mod tests {
}

#[test]
#[allow(clippy::cast_possible_truncation)]
async fn start_from_should_work() {
let block_count = 10;
let block: tendermint::Block =
Expand Down Expand Up @@ -424,6 +425,7 @@ mod tests {
}

#[test]
#[allow(clippy::cast_possible_truncation)]
async fn stream_should_work() {
let block_count = 10;
let block: tendermint::Block =
Expand Down
6 changes: 3 additions & 3 deletions contracts/gateway/tests/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,15 +334,15 @@ fn test_cases_for_duplicate_msgs() -> (
}

fn generate_msgs_with_all_statuses(
count_per_status: i32,
count_per_status: u8,
) -> HashMap<VerificationStatus, Vec<Message>> {
all_statuses()
.into_iter()
.map(|status| (status, generate_msgs(status, count_per_status)))
.collect::<HashMap<VerificationStatus, Vec<Message>>>()
}

fn generate_msgs(namespace: impl Debug, count: i32) -> Vec<Message> {
fn generate_msgs(namespace: impl Debug, count: u8) -> Vec<Message> {
(0..count)
.map(|i| Message {
cc_id: CrossChainId {
Expand All @@ -352,7 +352,7 @@ fn generate_msgs(namespace: impl Debug, count: i32) -> Vec<Message> {
destination_address: "idc".parse().unwrap(),
destination_chain: "mock-chain-2".parse().unwrap(),
source_address: "idc".parse().unwrap(),
payload_hash: [i as u8; 32],
payload_hash: [i; 32],
})
.collect()
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/rewards/src/contract/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,7 @@ mod test {
let rewards_claimed = distribute_rewards(
mock_deps.as_mut().storage,
pool_id,
block_height_started + epoch_duration * (epoch_count + 2) as u64,
block_height_started + epoch_duration * (epoch_count as u64 + 2),
None,
)
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion contracts/router/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ mod test {
.unwrap();
}

#[allow(clippy::arithmetic_side_effects)]
#[allow(clippy::arithmetic_side_effects, clippy::cast_possible_truncation)]
fn generate_messages(
src_chain: &Chain,
dest_chain: &Chain,
Expand Down
1 change: 1 addition & 0 deletions contracts/service-registry/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1763,6 +1763,7 @@ mod test {
}

#[test]
#[allow(clippy::cast_possible_truncation)]
fn get_active_verifiers_should_not_return_less_than_min() {
let mut deps = setup();

Expand Down
14 changes: 7 additions & 7 deletions contracts/voting-verifier/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,27 +207,27 @@ mod test {
deps
}

fn message_id(id: &str, index: u64, msg_id_format: &MessageIdFormat) -> nonempty::String {
fn message_id(id: &str, index: u32, msg_id_format: &MessageIdFormat) -> nonempty::String {
let tx_hash = Keccak256::digest(id.as_bytes()).into();
match msg_id_format {
MessageIdFormat::HexTxHashAndEventIndex => HexTxHashAndEventIndex {
tx_hash,
event_index: index as u32,
event_index: index,
}
.to_string()
.parse()
.unwrap(),
MessageIdFormat::Base58TxDigestAndEventIndex => Base58TxDigestAndEventIndex {
tx_digest: tx_hash,
event_index: index as u32,
event_index: index,
}
.to_string()
.parse()
.unwrap(),
}
}

fn messages(len: u64, msg_id_format: &MessageIdFormat) -> Vec<Message> {
fn messages(len: u32, msg_id_format: &MessageIdFormat) -> Vec<Message> {
(0..len)
.map(|i| Message {
cc_id: CrossChainId {
Expand Down Expand Up @@ -362,7 +362,7 @@ mod test {
let mut deps = setup(verifiers.clone(), &msg_id_format);
let messages_count = 5;
let messages_in_progress = 3;
let messages = messages(messages_count as u64, &msg_id_format);
let messages = messages(messages_count as u32, &msg_id_format);

execute(
deps.as_mut(),
Expand Down Expand Up @@ -1157,7 +1157,7 @@ mod test {

// simulate a majority of verifiers voting for succeeded on chain
verifiers.iter().enumerate().for_each(|(i, verifier)| {
if i >= majority as usize {
if i as u64 >= majority {
return;
}
let msg = ExecuteMsg::Vote {
Expand Down Expand Up @@ -1266,7 +1266,7 @@ mod test {
// which is one less than the updated majority. The messages
// should not receive enough votes to be considered verified
verifiers.iter().enumerate().for_each(|(i, verifier)| {
if i >= old_majority as usize {
if i as u64 >= old_majority {
return;
}
let msg = ExecuteMsg::Vote {
Expand Down
7 changes: 5 additions & 2 deletions contracts/voting-verifier/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,17 @@ pub fn vote(

let results_after_voting = get_poll_results(&poll);

let quorum_events = (results_after_voting.difference(results_before_voting))
let quorum_events = results_after_voting
.difference(results_before_voting)
.expect("failed to substract poll results")
.0
.into_iter()
.enumerate()
.filter_map(|(idx, vote)| vote.map(|vote| (idx, vote)))
.map(|(index_in_poll, vote)| {
make_quorum_event(&vote, index_in_poll as u32, &poll_id, &poll, &deps)
let idx = u32::try_from(index_in_poll)
.expect("the amount of votes should never overflow u32");
make_quorum_event(&vote, idx, &poll_id, &poll, &deps)
})
.collect::<Result<Vec<Event>, _>>()?;

Expand Down
4 changes: 2 additions & 2 deletions contracts/voting-verifier/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,13 @@ mod tests {
);
}

fn message(id: u64) -> Message {
fn message(id: u32) -> Message {
Message {
cc_id: CrossChainId {
chain: "source-chain".parse().unwrap(),
id: HexTxHashAndEventIndex {
tx_hash: [0; 32],
event_index: id as u32,
event_index: id,
}
.to_string()
.try_into()
Expand Down
2 changes: 1 addition & 1 deletion packages/axelar-wasm-std/src/voting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ impl WeightedPoll {
return Err(Error::PollExpired);
}

if votes.len() != self.poll_size as usize {
if votes.len() as u64 != self.poll_size {
return Err(Error::InvalidVoteSize);
}

Expand Down

0 comments on commit f6096f8

Please sign in to comment.