diff --git a/solana/solana-ibc/programs/solana-ibc/src/client_state.rs b/solana/solana-ibc/programs/solana-ibc/src/client_state.rs index 86e8ebbc..617a4865 100644 --- a/solana/solana-ibc/programs/solana-ibc/src/client_state.rs +++ b/solana/solana-ibc/programs/solana-ibc/src/client_state.rs @@ -286,7 +286,7 @@ impl ibc::clients::ics07_tendermint::ValidationContext for SolanaIbcStorage { client_id: &ClientId, height: &Height, ) -> Result, 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 = @@ -304,7 +304,7 @@ impl ibc::clients::ics07_tendermint::ValidationContext for SolanaIbcStorage { client_id: &ClientId, height: &Height, ) -> Result, 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 = diff --git a/solana/solana-ibc/programs/solana-ibc/src/execution_context.rs b/solana/solana-ibc/programs/solana-ibc/src/execution_context.rs index 9159feb9..fd3218f1 100644 --- a/solana/solana-ibc/programs/solana-ibc/src/execution_context.rs +++ b/solana/solana-ibc/programs/solana-ibc/src/execution_context.rs @@ -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; @@ -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(()) @@ -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(()) @@ -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(()) } @@ -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(()) } @@ -366,14 +364,10 @@ impl ExecutionContext for SolanaIbcStorage { let event_in_bytes: Vec = 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 }); } @@ -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)); } diff --git a/solana/solana-ibc/programs/solana-ibc/src/lib.rs b/solana/solana-ibc/programs/solana-ibc/src/lib.rs index d75640dd..b69daee9 100644 --- a/solana/solana-ibc/programs/solana-ibc/src/lib.rs +++ b/solana/solana-ibc/programs/solana-ibc/src/lib.rs @@ -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::*; @@ -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"); @@ -20,7 +24,7 @@ mod transfer; mod validation_context; // mod client_context; -#[program] +#[anchor_lang::program] pub mod solana_ibc { use super::*; diff --git a/solana/solana-ibc/programs/solana-ibc/src/validation_context.rs b/solana/solana-ibc/programs/solana-ibc/src/validation_context.rs index a8528703..a19c95c6 100644 --- a/solana/solana-ibc/programs/solana-ibc/src/validation_context.rs +++ b/solana/solana-ibc/programs/solana-ibc/src/validation_context.rs @@ -83,8 +83,8 @@ impl ValidationContext for SolanaIbcStorage { } fn host_height(&self) -> std::result::Result { - 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 { @@ -101,10 +101,9 @@ impl ValidationContext for SolanaIbcStorage { _height: &ibc::Height, ) -> std::result::Result { 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(), })) } @@ -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(