Skip to content

Commit

Permalink
add type conversion between Any and ClientMessage
Browse files Browse the repository at this point in the history
Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele committed Nov 6, 2024
1 parent d652f33 commit 5f8c79b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
2 changes: 2 additions & 0 deletions crates/ibc/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ pub enum Error {
},
/// proto missing field error: `{0}`
ProtoMissingFieldError(String),
/// unknown message type: `{0}`
UnknownMessageType(String),
}

impl Error {
Expand Down
37 changes: 35 additions & 2 deletions crates/ibc/src/header.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::errors::Error;
use crate::internal_prelude::*;
use crate::misbehaviour::Misbehaviour;
use crate::misbehaviour::{
Misbehaviour, ETHEREUM_FINALIZED_HEADER_MISBEHAVIOUR_TYPE_URL,
ETHEREUM_NEXT_SYNC_COMMITTEE_MISBEHAVIOUR_TYPE_URL,
};
use crate::types::{
convert_consensus_update_to_proto, convert_execution_update_to_proto,
convert_proto_to_consensus_update, convert_proto_to_execution_update, AccountUpdateInfo,
Expand All @@ -18,12 +21,42 @@ use prost::Message;
pub const ETHEREUM_HEADER_TYPE_URL: &str = "/ibc.lightclients.ethereum.v1.Header";

#[allow(clippy::large_enum_variant)]
#[derive(serde::Serialize, serde::Deserialize)]
#[derive(Clone, serde::Serialize, serde::Deserialize)]
pub enum ClientMessage<const SYNC_COMMITTEE_SIZE: usize> {
Header(Header<SYNC_COMMITTEE_SIZE>),
Misbehaviour(Misbehaviour<SYNC_COMMITTEE_SIZE>),
}

impl<const SYNC_COMMITTEE_SIZE: usize> Protobuf<IBCAny> for ClientMessage<SYNC_COMMITTEE_SIZE> {}

impl<const SYNC_COMMITTEE_SIZE: usize> TryFrom<IBCAny> for ClientMessage<SYNC_COMMITTEE_SIZE> {
type Error = Error;

fn try_from(raw: IBCAny) -> Result<Self, Self::Error> {
match raw.type_url.as_str() {
ETHEREUM_HEADER_TYPE_URL => {
let header = Header::<SYNC_COMMITTEE_SIZE>::try_from(raw)?;
Ok(Self::Header(header))
}
ETHEREUM_FINALIZED_HEADER_MISBEHAVIOUR_TYPE_URL
| ETHEREUM_NEXT_SYNC_COMMITTEE_MISBEHAVIOUR_TYPE_URL => {
let misbehaviour = Misbehaviour::<SYNC_COMMITTEE_SIZE>::try_from(raw)?;
Ok(Self::Misbehaviour(misbehaviour))
}
_ => Err(Error::UnknownMessageType(raw.type_url)),
}
}
}

impl<const SYNC_COMMITTEE_SIZE: usize> From<ClientMessage<SYNC_COMMITTEE_SIZE>> for IBCAny {
fn from(msg: ClientMessage<SYNC_COMMITTEE_SIZE>) -> Self {
match msg {
ClientMessage::Header(header) => IBCAny::from(header),
ClientMessage::Misbehaviour(misbehaviour) => IBCAny::from(misbehaviour),
}
}
}

#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct Header<const SYNC_COMMITTEE_SIZE: usize> {
pub trusted_sync_committee: TrustedSyncCommittee<SYNC_COMMITTEE_SIZE>,
Expand Down

0 comments on commit 5f8c79b

Please sign in to comment.