Skip to content

Commit

Permalink
Merge pull request #2 from datachainlab/misbehaviour-proto
Browse files Browse the repository at this point in the history
Add misbehaviour proto and refactor type converters

Signed-off-by: Jun Kimura <[email protected]
  • Loading branch information
bluele authored Apr 10, 2023
2 parents a0121de + 3c00905 commit 4842536
Show file tree
Hide file tree
Showing 10 changed files with 834 additions and 365 deletions.
1 change: 1 addition & 0 deletions crates/ibc/src/consensus_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub struct ConsensusState {
pub storage_root: CommitmentRoot,
/// timestamp from execution payload
pub timestamp: u64,
/// aggregate public key of current sync committee
pub current_sync_committee: PublicKey,
/// aggregate public key of next sync committee
pub next_sync_committee: Option<PublicKey>,
Expand Down
10 changes: 9 additions & 1 deletion crates/ibc/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use ethereum_consensus::{
types::U64,
};
use ibc::{
core::{ics02_client::error::ClientError, ContextError},
core::{ics02_client::error::ClientError, ics24_host::error::ValidationError, ContextError},
Height,
};

Expand Down Expand Up @@ -52,6 +52,8 @@ pub enum Error {
SSZDeserialize(ssz_rs::DeserializeError),
/// ics02 error: `{0}`
ICS02(ClientError),
/// ics24 error: `{0}`
ICS24(ValidationError),
}

impl From<ethereum_light_client_verifier::errors::Error> for Error {
Expand Down Expand Up @@ -92,6 +94,12 @@ impl From<ClientError> for Error {
}
}

impl From<ValidationError> for Error {
fn from(value: ValidationError) -> Self {
Self::ICS24(value)
}
}

impl From<ssz_rs::DeserializeError> for Error {
fn from(value: ssz_rs::DeserializeError) -> Self {
Self::SSZDeserialize(value)
Expand Down
308 changes: 17 additions & 291 deletions crates/ibc/src/header.rs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions crates/ibc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub mod consensus_state;
pub mod errors;
pub mod header;
pub mod misbehaviour;
pub mod types;
pub mod update;

mod internal_prelude {
Expand Down
161 changes: 154 additions & 7 deletions crates/ibc/src/misbehaviour.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
use crate::{header::TrustedSyncCommittee, update::ConsensusUpdateInfo};
use ethereum_light_client_verifier::misbehaviour::Misbehaviour as MisbehaviourData;
use crate::{
errors::Error,
types::{
convert_consensus_update_to_proto, convert_proto_to_consensus_update, TrustedSyncCommittee,
},
update::ConsensusUpdateInfo,
};
use alloc::string::ToString;
use bytes::Buf;
use core::str::FromStr;
use ethereum_ibc_proto::ibc::lightclients::ethereum::v1::{
FinalizedHeaderMisbehaviour as RawFinalizedHeaderMisbehaviour,
NextSyncCommitteeMisbehaviour as RawNextSyncCommitteeMisbehaviour,
};
use ethereum_light_client_verifier::misbehaviour::{
FinalizedHeaderMisbehaviour, Misbehaviour as MisbehaviourData, NextSyncCommitteeMisbehaviour,
};
use ibc::core::{
ics02_client::{error::ClientError, misbehaviour::Misbehaviour as Ics02Misbehaviour},
ics24_host::identifier::ClientId,
};
use ibc_proto::google::protobuf::Any;
use ibc_proto::google::protobuf::Any as IBCAny;
use ibc_proto::protobuf::Protobuf;
use prost::Message;
use serde::{Deserialize, Serialize};

pub const ETHEREUM_FINALIZED_HEADER_MISBEHAVIOUR_TYPE_URL: &str =
Expand All @@ -29,11 +46,141 @@ impl<const SYNC_COMMITTEE_SIZE: usize> Ics02Misbehaviour for Misbehaviour<SYNC_C
}
}

#[allow(unused_variables)]
impl<const SYNC_COMMITTEE_SIZE: usize> TryFrom<Any> for Misbehaviour<SYNC_COMMITTEE_SIZE> {
impl<const SYNC_COMMITTEE_SIZE: usize> Protobuf<RawFinalizedHeaderMisbehaviour>
for Misbehaviour<SYNC_COMMITTEE_SIZE>
{
}

impl<const SYNC_COMMITTEE_SIZE: usize> Protobuf<RawNextSyncCommitteeMisbehaviour>
for Misbehaviour<SYNC_COMMITTEE_SIZE>
{
}

impl<const SYNC_COMMITTEE_SIZE: usize> TryFrom<RawFinalizedHeaderMisbehaviour>
for Misbehaviour<SYNC_COMMITTEE_SIZE>
{
type Error = Error;
fn try_from(value: RawFinalizedHeaderMisbehaviour) -> Result<Self, Self::Error> {
Ok(Self {
client_id: ClientId::from_str(&value.client_id)?,
trusted_sync_committee: value.trusted_sync_committee.unwrap().try_into()?,
data: MisbehaviourData::FinalizedHeader(FinalizedHeaderMisbehaviour {
consensus_update_1: convert_proto_to_consensus_update(
value.consensus_update_1.unwrap(),
)?,
consensus_update_2: convert_proto_to_consensus_update(
value.consensus_update_2.unwrap(),
)?,
}),
})
}
}

impl<const SYNC_COMMITTEE_SIZE: usize> TryFrom<RawNextSyncCommitteeMisbehaviour>
for Misbehaviour<SYNC_COMMITTEE_SIZE>
{
type Error = Error;
fn try_from(value: RawNextSyncCommitteeMisbehaviour) -> Result<Self, Self::Error> {
Ok(Self {
client_id: ClientId::from_str(&value.client_id)?,
trusted_sync_committee: value.trusted_sync_committee.unwrap().try_into()?,
data: MisbehaviourData::NextSyncCommittee(NextSyncCommitteeMisbehaviour {
consensus_update_1: convert_proto_to_consensus_update(
value.consensus_update_1.unwrap(),
)?,
consensus_update_2: convert_proto_to_consensus_update(
value.consensus_update_2.unwrap(),
)?,
}),
})
}
}

impl<const SYNC_COMMITTEE_SIZE: usize> From<Misbehaviour<SYNC_COMMITTEE_SIZE>>
for RawFinalizedHeaderMisbehaviour
{
fn from(value: Misbehaviour<SYNC_COMMITTEE_SIZE>) -> Self {
let data = match value.data {
MisbehaviourData::FinalizedHeader(data) => data,
_ => panic!("unexpected misbehaviour type"),
};
Self {
client_id: value.client_id.as_str().to_string(),
trusted_sync_committee: Some(value.trusted_sync_committee.into()),
consensus_update_1: Some(convert_consensus_update_to_proto(data.consensus_update_1)),
consensus_update_2: Some(convert_consensus_update_to_proto(data.consensus_update_2)),
}
}
}

impl<const SYNC_COMMITTEE_SIZE: usize> From<Misbehaviour<SYNC_COMMITTEE_SIZE>>
for RawNextSyncCommitteeMisbehaviour
{
fn from(value: Misbehaviour<SYNC_COMMITTEE_SIZE>) -> Self {
let data = match value.data {
MisbehaviourData::NextSyncCommittee(data) => data,
_ => panic!("unexpected misbehaviour type"),
};
Self {
client_id: value.client_id.as_str().to_string(),
trusted_sync_committee: Some(value.trusted_sync_committee.into()),
consensus_update_1: Some(convert_consensus_update_to_proto(data.consensus_update_1)),
consensus_update_2: Some(convert_consensus_update_to_proto(data.consensus_update_2)),
}
}
}

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

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

fn try_from(value: Any) -> Result<Self, Self::Error> {
todo!()
fn try_from(raw: IBCAny) -> Result<Self, Self::Error> {
use core::ops::Deref;

match raw.type_url.as_str() {
ETHEREUM_FINALIZED_HEADER_MISBEHAVIOUR_TYPE_URL => {
decode_finalized_header_misbehaviour(raw.value.deref()).map_err(Into::into)
}
ETHEREUM_NEXT_SYNC_COMMITTEE_MISBEHAVIOUR_TYPE_URL => {
decode_next_sync_committee_misbehaviour(raw.value.deref()).map_err(Into::into)
}
_ => Err(ClientError::UnknownMisbehaviourType {
misbehaviour_type: raw.type_url,
}),
}
}
}

impl<const SYNC_COMMITTEE_SIZE: usize> From<Misbehaviour<SYNC_COMMITTEE_SIZE>> for IBCAny {
fn from(value: Misbehaviour<SYNC_COMMITTEE_SIZE>) -> Self {
match value.data {
MisbehaviourData::FinalizedHeader(_) => Self {
type_url: ETHEREUM_FINALIZED_HEADER_MISBEHAVIOUR_TYPE_URL.to_string(),
value: Protobuf::<RawFinalizedHeaderMisbehaviour>::encode_vec(&value)
.expect("encoding to `Any` from `TmHeader`"),
},
MisbehaviourData::NextSyncCommittee(_) => Self {
type_url: ETHEREUM_NEXT_SYNC_COMMITTEE_MISBEHAVIOUR_TYPE_URL.to_string(),
value: Protobuf::<RawNextSyncCommitteeMisbehaviour>::encode_vec(&value)
.expect("encoding to `Any` from `TmHeader`"),
},
}
}
}

fn decode_finalized_header_misbehaviour<const SYNC_COMMITTEE_SIZE: usize, B: Buf>(
buf: B,
) -> Result<Misbehaviour<SYNC_COMMITTEE_SIZE>, Error> {
RawFinalizedHeaderMisbehaviour::decode(buf)
.map_err(Error::Decode)?
.try_into()
}

fn decode_next_sync_committee_misbehaviour<const SYNC_COMMITTEE_SIZE: usize, B: Buf>(
buf: B,
) -> Result<Misbehaviour<SYNC_COMMITTEE_SIZE>, Error> {
RawNextSyncCommitteeMisbehaviour::decode(buf)
.map_err(Error::Decode)?
.try_into()
}
Loading

0 comments on commit 4842536

Please sign in to comment.