Skip to content

Commit

Permalink
solana-ibc: fix clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
mina86 committed Oct 10, 2023
1 parent 8350731 commit b138459
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 56 deletions.
4 changes: 2 additions & 2 deletions solana/solana-ibc/programs/solana-ibc/src/client_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ impl ibc::clients::ics07_tendermint::ValidationContext for SolanaIbcStorage {
client_id: &ClientId,
height: &Height,
) -> Result<Option<Self::AnyConsensusState>, ContextError> {
let end_height = (height.revision_number() + 1, 1 as u64);
let end_height = (height.revision_number() + 1, 1);
match self.consensus_states.get(&(client_id.to_string(), end_height)) {
Some(data) => {
let result: Self::AnyConsensusState =
Expand All @@ -304,7 +304,7 @@ impl ibc::clients::ics07_tendermint::ValidationContext for SolanaIbcStorage {
client_id: &ClientId,
height: &Height,
) -> Result<Option<Self::AnyConsensusState>, ContextError> {
let end_height = (height.revision_number(), 1 as u64);
let end_height = (height.revision_number(), 1);
match self.consensus_states.get(&(client_id.to_string(), end_height)) {
Some(data) => {
let result: Self::AnyConsensusState =
Expand Down
75 changes: 32 additions & 43 deletions solana/solana-ibc/programs/solana-ibc/src/execution_context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::BTreeMap;

use anchor_lang::prelude::*;
use anchor_lang::emit;
use anchor_lang::solana_program::msg;
use ibc::core::events::IbcEvent;
use ibc::core::ics02_client::ClientExecutionContext;
use ibc::core::ics03_connection::connection::ConnectionEnd;
Expand Down Expand Up @@ -100,13 +101,13 @@ impl ExecutionContext for SolanaIbcStorage {
self.client_processed_times
.insert(client_id.to_string().clone(), new_map);
}
self.client_processed_times.get_mut(&client_id.to_string()).and_then(
self.client_processed_times.get_mut(&client_id.to_string()).map(
|processed_times| {
Some(BTreeMap::insert(
BTreeMap::insert(
processed_times,
(height.revision_number(), height.revision_height()),
timestamp.nanoseconds(),
))
)
},
);
Ok(())
Expand Down Expand Up @@ -135,16 +136,16 @@ impl ExecutionContext for SolanaIbcStorage {
self.client_processed_heights
.insert(client_id.to_string().clone(), new_map);
}
self.client_processed_heights.get_mut(&client_id.to_string()).and_then(
self.client_processed_heights.get_mut(&client_id.to_string()).map(
|processed_heights| {
Some(BTreeMap::insert(
BTreeMap::insert(
processed_heights,
(height.revision_number(), height.revision_height()),
(
host_height.revision_number(),
host_height.revision_height(),
),
))
)
},
);
Ok(())
Expand Down Expand Up @@ -217,19 +218,17 @@ impl ExecutionContext for SolanaIbcStorage {
commitment_path: &CommitmentPath,
) -> std::result::Result<(), ContextError> {
msg!("delete_packet_commitment: path: {}", commitment_path);
//
self.packet_commitment_sequence_sets
.get_mut(&(
commitment_path.port_id.clone().to_string(),
commitment_path.channel_id.clone().to_string(),
))
.map(|sequences| {
let index = sequences
.iter()
.position(|x| *x == u64::from(commitment_path.sequence))
.unwrap();
sequences.remove(index);
});
let sequences = self.packet_commitment_sequence_sets.get_mut(&(
commitment_path.port_id.clone().to_string(),
commitment_path.channel_id.clone().to_string(),
));
if let Some(sequences) = sequences {
let index = sequences
.iter()
.position(|x| *x == u64::from(commitment_path.sequence))
.unwrap();
sequences.remove(index);
};
Ok(())
}

Expand Down Expand Up @@ -276,15 +275,14 @@ impl ExecutionContext for SolanaIbcStorage {
ack_path: &AckPath,
) -> std::result::Result<(), ContextError> {
msg!("delete_packet_acknowledgement: path: {}", ack_path,);
self.packet_acknowledgement_sequence_sets
.get_mut(&(
ack_path.port_id.clone().to_string(),
ack_path.channel_id.clone().to_string(),
))
.map(|sequences| {
let sequence_as_u64: u64 = ack_path.sequence.into();
sequences.remove(sequence_as_u64 as usize);
});
let sequences = self.packet_acknowledgement_sequence_sets.get_mut(&(
ack_path.port_id.clone().to_string(),
ack_path.channel_id.clone().to_string(),
));
if let Some(sequences) = sequences {
let sequence_as_u64: u64 = ack_path.sequence.into();
sequences.remove(sequence_as_u64 as usize);
}
Ok(())
}

Expand Down Expand Up @@ -366,14 +364,10 @@ impl ExecutionContext for SolanaIbcStorage {
let event_in_bytes: Vec<u8> = bincode::serialize(&event).unwrap();
let inner_host_height =
(host_height.revision_height(), host_height.revision_number());
if self.ibc_events_history.contains_key(&inner_host_height) {
self.ibc_events_history
.get_mut(&inner_host_height)
.map(|events| events.push(event_in_bytes.clone()));
} else {
self.ibc_events_history
.insert(inner_host_height, vec![event_in_bytes.clone()]);
}
self.ibc_events_history
.entry(inner_host_height)
.or_default()
.push(event_in_bytes.clone());
emit!(EmitIBCEvent { ibc_event: event_in_bytes });
}

Expand All @@ -391,10 +385,5 @@ fn record_packet_sequence(
sequence: &Sequence,
) {
let key = (port_id.clone().to_string(), channel_id.clone().to_string());
if hash_map.contains_key(&key) {
hash_map.insert(key.clone(), Vec::new());
}
hash_map.get_mut(&key).map(|sequences| {
sequences.push(u64::from(*sequence));
});
hash_map.entry(key).or_default().push(u64::from(*sequence));
}
8 changes: 6 additions & 2 deletions solana/solana-ibc/programs/solana-ibc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// anchor_lang::error::Error and anchor_lang::Result is ≥ 160 bytes and there’s
// not much we can do about it.
#![allow(clippy::result_large_err)]

use std::collections::BTreeMap;

use anchor_lang::prelude::*;
Expand All @@ -6,7 +10,7 @@ use ibc::core::ics24_host::identifier::PortId;
use ibc::core::router::{Module, ModuleId, Router};
use module_holder::ModuleHolder;

const SOLANA_IBC_STORAGE_SEED: &'static [u8] = b"solana_ibc_storage";
const SOLANA_IBC_STORAGE_SEED: &[u8] = b"solana_ibc_storage";

declare_id!("7MEuaEwNMsjVCJy9N31ZgvQf1dFkRNXYFREaAjMsoE5g");

Expand All @@ -20,7 +24,7 @@ mod transfer;
mod validation_context;
// mod client_context;

#[program]
#[anchor_lang::program]
pub mod solana_ibc {
use super::*;

Expand Down
17 changes: 8 additions & 9 deletions solana/solana-ibc/programs/solana-ibc/src/validation_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ impl ValidationContext for SolanaIbcStorage {
}

fn host_height(&self) -> std::result::Result<ibc::Height, ContextError> {
Ok(ibc::Height::new(self.height.0, self.height.1)
.map_err(|e| ContextError::ClientError(e))?)
ibc::Height::new(self.height.0, self.height.1)
.map_err(ContextError::ClientError)
}

fn host_timestamp(&self) -> std::result::Result<Timestamp, ContextError> {
Expand All @@ -101,10 +101,9 @@ impl ValidationContext for SolanaIbcStorage {
_height: &ibc::Height,
) -> std::result::Result<Self::AnyConsensusState, ContextError> {
Err(ContextError::ClientError(ClientError::ClientSpecific {
description: format!(
"The `host_consensus_state` is not supported on Solana \
protocol."
),
description: "The `host_consensus_state` is not supported on \
Solana protocol."
.into(),
}))
}

Expand Down Expand Up @@ -355,16 +354,16 @@ impl ValidationContext for SolanaIbcStorage {
&self,
signer: &ibc::Signer,
) -> std::result::Result<(), ContextError> {
match Pubkey::from_str(&signer.to_string()) {
match Pubkey::from_str(signer.as_ref()) {
Ok(_) => Ok(()),
Err(e) => Err(ContextError::ClientError(ClientError::Other {
description: format!("Invalid signer: {:?}", e).to_string(),
description: format!("Invalid signer: {e:?}"),
})),
}
}

fn get_client_validation_context(&self) -> &Self::ClientValidationContext {
&self
self
}

fn get_compatible_versions(
Expand Down

0 comments on commit b138459

Please sign in to comment.