Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary type conversions #23

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ edition = "2021"

[dependencies]
ibc = { version = "0.29.0", default-features = false, features = ["serde"] }
serde = { version = "1.0", default-features = false }
displaydoc = { version = "0.2", default-features = false }
tiny-keccak = { version = "1.4" }

light-client = { git = "https://github.com/datachainlab/lcp", rev = "v0.2.11", default-features = false, features = ["ibc"] }
ethereum-ibc = { git = "https://github.com/datachainlab/ethereum-ibc-rs", rev = "v0.0.15", default-features = false }
ethereum-ibc = { git = "https://github.com/datachainlab/ethereum-ibc-rs", rev = "8243b634ae0953a9e0c2fd5f64c2b830723ab8f6", default-features = false }
144 changes: 77 additions & 67 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::errors::Error;
use crate::internal_prelude::*;
use crate::message::{ClientMessage, Header, Misbehaviour};
use crate::state::{gen_state_id, ClientState, ConsensusState};
use crate::state::gen_state_id;
use core::str::FromStr;
use core::time::Duration;
use ethereum_ibc::client_state::ClientState as EthereumClientState;
use ethereum_ibc::consensus_state::ConsensusState as EthereumConsensusState;
use ethereum_ibc::client_state::ClientState;
use ethereum_ibc::consensus_state::ConsensusState;
use ethereum_ibc::eth_client_type;
use ethereum_ibc::header::{ClientMessage, Header};
use ethereum_ibc::misbehaviour::Misbehaviour;
use ibc::core::ics02_client::client_state::{
downcast_client_state, ClientState as Ics02ClientState, UpdatedState,
};
Expand All @@ -22,6 +23,7 @@ use light_client::commitments::{
UpdateStateProxyMessage, ValidationContext, VerifyMembershipProxyMessage,
};
use light_client::ibc::IBCContext;
use light_client::types::proto::google::protobuf::Any as IBCAny;
use light_client::types::{Any, ClientId, Height, Time};
use light_client::{
CreateClientResult, HostClientReader, LightClient, MisbehaviourData, UpdateStateData,
Expand All @@ -42,7 +44,9 @@ impl<const SYNC_COMMITTEE_SIZE: usize> LightClient for EthereumLightClient<SYNC_
client_id: &ClientId,
) -> Result<Height, light_client::Error> {
let client_state: ClientState<SYNC_COMMITTEE_SIZE> =
ctx.client_state(client_id)?.try_into()?;
IBCAny::from(ctx.client_state(client_id)?)
.try_into()
.map_err(Error::ICS02)?;
Ok(client_state.latest_height().into())
}

Expand All @@ -52,10 +56,14 @@ impl<const SYNC_COMMITTEE_SIZE: usize> LightClient for EthereumLightClient<SYNC_
any_client_state: Any,
any_consensus_state: Any,
) -> Result<light_client::CreateClientResult, light_client::Error> {
let client_state = ClientState::<SYNC_COMMITTEE_SIZE>::try_from(any_client_state.clone())?;
let consensus_state = ConsensusState::try_from(any_consensus_state)?;
let any_client_state = IBCAny::from(any_client_state);
let any_consensus_state = IBCAny::from(any_consensus_state);
let client_state = ClientState::<SYNC_COMMITTEE_SIZE>::try_from(any_client_state.clone())
.map_err(Error::ICS02)?;
let consensus_state =
ConsensusState::try_from(any_consensus_state).map_err(Error::ICS02)?;
let _ = client_state
.initialise(consensus_state.0.clone().into())
.initialise(consensus_state.clone().into())
.map_err(Error::ICS02)?;

let height = client_state.latest_height().into();
Expand All @@ -68,7 +76,7 @@ impl<const SYNC_COMMITTEE_SIZE: usize> LightClient for EthereumLightClient<SYNC_
prev_state_id: None,
post_height: height,
post_state_id: state_id,
emitted_states: vec![EmittedState(height, any_client_state)],
emitted_states: vec![EmittedState(height, any_client_state.into())],
timestamp,
context: ValidationContext::Empty,
}
Expand All @@ -83,7 +91,10 @@ impl<const SYNC_COMMITTEE_SIZE: usize> LightClient for EthereumLightClient<SYNC_
client_id: ClientId,
any_message: Any,
) -> Result<light_client::UpdateClientResult, light_client::Error> {
match ClientMessage::<SYNC_COMMITTEE_SIZE>::try_from(any_message.clone())? {
let message =
ClientMessage::<SYNC_COMMITTEE_SIZE>::try_from(IBCAny::from(any_message.clone()))
.map_err(Error::IBC)?;
match message {
ClientMessage::Header(header) => Ok(self
.update_state(ctx, client_id, any_message, header)?
.into()),
Expand Down Expand Up @@ -191,7 +202,9 @@ impl<const SYNC_COMMITTEE_SIZE: usize> EthereumLightClient<SYNC_COMMITTEE_SIZE>
light_client::Error,
> {
let client_state: ClientState<SYNC_COMMITTEE_SIZE> =
ctx.client_state(&client_id)?.try_into()?;
IBCAny::from(ctx.client_state(&client_id)?)
.try_into()
.map_err(Error::ICS02)?;

if client_state.is_frozen() {
return Err(Error::ICS02(ClientError::ClientFrozen {
Expand All @@ -201,7 +214,9 @@ impl<const SYNC_COMMITTEE_SIZE: usize> EthereumLightClient<SYNC_COMMITTEE_SIZE>
}

let consensus_state: ConsensusState =
ctx.consensus_state(&client_id, &proof_height)?.try_into()?;
IBCAny::from(ctx.consensus_state(&client_id, &proof_height)?)
.try_into()
.map_err(Error::ICS02)?;

let proof: CommitmentProofBytes = proof.try_into().map_err(Error::ICS23)?;
let prefix: CommitmentPrefix = counterparty_prefix.try_into().map_err(Error::ICS23)?;
Expand All @@ -217,7 +232,9 @@ impl<const SYNC_COMMITTEE_SIZE: usize> EthereumLightClient<SYNC_COMMITTEE_SIZE>
header: Header<SYNC_COMMITTEE_SIZE>,
) -> Result<UpdateStateData, light_client::Error> {
let client_state: ClientState<SYNC_COMMITTEE_SIZE> =
ctx.client_state(&client_id)?.try_into()?;
IBCAny::from(ctx.client_state(&client_id)?)
.try_into()
.map_err(Error::ICS02)?;

if client_state.is_frozen() {
return Err(Error::ICS02(ClientError::ClientFrozen {
Expand All @@ -230,15 +247,17 @@ impl<const SYNC_COMMITTEE_SIZE: usize> EthereumLightClient<SYNC_COMMITTEE_SIZE>
let header_timestamp: Time = header.timestamp().into();
let trusted_height = header.trusted_sync_committee.height;

let trusted_consensus_state: ConsensusState = ctx
.consensus_state(&client_id, &trusted_height.into())
.map_err(|_| {
Error::ICS02(ClientError::ConsensusStateNotFound {
client_id: client_id.clone().into(),
height: trusted_height,
})
})?
.try_into()?;
let trusted_consensus_state: ConsensusState = IBCAny::from(
ctx.consensus_state(&client_id, &trusted_height.into())
.map_err(|_| {
Error::ICS02(ClientError::ConsensusStateNotFound {
client_id: client_id.clone().into(),
height: trusted_height,
})
})?,
)
.try_into()
.map_err(Error::ICS02)?;

// Use client_state to validate the new header against the latest consensus_state.
// This function will return the new client_state (its latest_height changed) and a
Expand All @@ -248,10 +267,7 @@ impl<const SYNC_COMMITTEE_SIZE: usize> EthereumLightClient<SYNC_COMMITTEE_SIZE>
consensus_state: new_consensus_state,
} = client_state
.check_header_and_update_state(
&IBCContext::<
EthereumClientState<SYNC_COMMITTEE_SIZE>,
EthereumConsensusState,
>::new(ctx),
&IBCContext::<ClientState<SYNC_COMMITTEE_SIZE>, ConsensusState>::new(ctx),
client_id.into(),
any_message.into(),
)
Expand All @@ -261,24 +277,20 @@ impl<const SYNC_COMMITTEE_SIZE: usize> EthereumLightClient<SYNC_COMMITTEE_SIZE>
})
})?;

let new_client_state = ClientState(
downcast_client_state::<EthereumClientState<SYNC_COMMITTEE_SIZE>>(
new_client_state.as_ref(),
)
.unwrap()
.clone(),
);
let new_consensus_state = ConsensusState(
downcast_consensus_state::<EthereumConsensusState>(new_consensus_state.as_ref())
let new_client_state =
downcast_client_state::<ClientState<SYNC_COMMITTEE_SIZE>>(new_client_state.as_ref())
.unwrap()
.clone();
let new_consensus_state =
downcast_consensus_state::<ConsensusState>(new_consensus_state.as_ref())
.unwrap()
.clone(),
);
.clone();

let prev_state_id = gen_state_id(client_state.clone(), trusted_consensus_state.clone())?;
let post_state_id = gen_state_id(new_client_state.clone(), new_consensus_state.clone())?;
Ok(UpdateStateData {
new_any_client_state: new_client_state.into(),
new_any_consensus_state: new_consensus_state.into(),
new_any_client_state: IBCAny::from(new_client_state).into(),
new_any_consensus_state: IBCAny::from(new_consensus_state).into(),
height,
message: UpdateStateProxyMessage {
prev_height: Some(trusted_height.into()),
Expand Down Expand Up @@ -306,7 +318,9 @@ impl<const SYNC_COMMITTEE_SIZE: usize> EthereumLightClient<SYNC_COMMITTEE_SIZE>
misbehaviour: Misbehaviour<SYNC_COMMITTEE_SIZE>,
) -> Result<MisbehaviourData, light_client::Error> {
let client_state: ClientState<SYNC_COMMITTEE_SIZE> =
ctx.client_state(&client_id)?.try_into()?;
IBCAny::from(ctx.client_state(&client_id)?)
.try_into()
.map_err(Error::ICS02)?;

if client_state.is_frozen() {
return Err(Error::ICS02(ClientError::ClientFrozen {
Expand All @@ -316,20 +330,19 @@ impl<const SYNC_COMMITTEE_SIZE: usize> EthereumLightClient<SYNC_COMMITTEE_SIZE>
}

let trusted_height = misbehaviour.trusted_sync_committee.height;
let trusted_consensus_state: ConsensusState = ctx
.consensus_state(&client_id, &trusted_height.into())
.map_err(|_| {
Error::ICS02(ClientError::ConsensusStateNotFound {
client_id: client_id.clone().into(),
height: trusted_height,
})
})?
.try_into()?;
let trusted_consensus_state: ConsensusState = IBCAny::from(
ctx.consensus_state(&client_id, &trusted_height.into())
.map_err(|_| {
Error::ICS02(ClientError::ConsensusStateNotFound {
client_id: client_id.clone().into(),
height: trusted_height,
})
})?,
)
.try_into()
.map_err(Error::ICS02)?;

let ibc_ctx =
IBCContext::<EthereumClientState<SYNC_COMMITTEE_SIZE>, EthereumConsensusState>::new(
ctx,
);
let ibc_ctx = IBCContext::<ClientState<SYNC_COMMITTEE_SIZE>, ConsensusState>::new(ctx);

let new_client_state = client_state
.check_misbehaviour_and_update_state(
Expand All @@ -342,16 +355,13 @@ impl<const SYNC_COMMITTEE_SIZE: usize> EthereumLightClient<SYNC_COMMITTEE_SIZE>
reason: e.to_string(),
})
})?;
let new_client_state = ClientState(
downcast_client_state::<EthereumClientState<SYNC_COMMITTEE_SIZE>>(
new_client_state.as_ref(),
)
.unwrap()
.clone(),
);
let new_client_state =
downcast_client_state::<ClientState<SYNC_COMMITTEE_SIZE>>(new_client_state.as_ref())
.unwrap()
.clone();

Ok(MisbehaviourData {
new_any_client_state: new_client_state.into(),
new_any_client_state: IBCAny::from(new_client_state).into(),
message: MisbehaviourProxyMessage {
prev_states: self.make_prev_states(
ctx,
Expand All @@ -366,7 +376,7 @@ impl<const SYNC_COMMITTEE_SIZE: usize> EthereumLightClient<SYNC_COMMITTEE_SIZE>
Time::unix_epoch(),
trusted_consensus_state.timestamp.into(),
)),
client_message: Any::from(misbehaviour),
client_message: IBCAny::from(misbehaviour).into(),
},
})
}
Expand All @@ -381,15 +391,15 @@ impl<const SYNC_COMMITTEE_SIZE: usize> EthereumLightClient<SYNC_COMMITTEE_SIZE>
let mut prev_states = Vec::new();
for height in heights {
let ibc_height = height.try_into().map_err(Error::ICS02)?;
let consensus_state: ConsensusState = ctx
.consensus_state(client_id, &height)
.map_err(|_| {
let consensus_state: ConsensusState =
IBCAny::from(ctx.consensus_state(client_id, &height).map_err(|_| {
Error::ICS02(ClientError::ConsensusStateNotFound {
client_id: client_id.clone().into(),
height: ibc_height,
})
})?
.try_into()?;
})?)
.try_into()
.map_err(Error::ICS02)?;
prev_states.push(PrevState {
height,
state_id: gen_state_id(client_state.clone(), consensus_state)?,
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ extern crate alloc;

pub mod client;
pub mod errors;
pub mod message;
pub mod state;
pub use ethereum_ibc as ibc;

Expand Down
Loading
Loading