Skip to content

Commit

Permalink
refactor: Refactor code in ics06_solomachine folder
Browse files Browse the repository at this point in the history
  • Loading branch information
DaviRain-Su committed Apr 25, 2023
1 parent cf28e11 commit 39e29c9
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 29 deletions.
11 changes: 7 additions & 4 deletions crates/ibc/src/clients/ics06_solomachine/error.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use crate::prelude::*;

use crate::core::ics02_client::error::ClientError;
use crate::core::ics24_host::identifier::{ChainId, ClientId};
use crate::Height;
use core::time::Duration;

use crate::timestamp::ParseTimestampError;
use displaydoc::Display;

#[derive(Debug, Display)]
Expand All @@ -17,6 +14,12 @@ pub enum Error {
EmptyConsensusStatePublicKey,
/// invlid height
InvalidHeight(ClientError),
/// invalid raw client id: `{client_id}`
InvalidRawClientId { client_id: String },
/// unknow data type: `{0}`
UnknownDataType(i32),
/// prase time error
ParseTimeError(ParseTimestampError),
}

impl From<Error> for ClientError {
Expand Down
21 changes: 10 additions & 11 deletions crates/ibc/src/clients/ics06_solomachine/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ use ibc_proto::google::protobuf::Any;
use ibc_proto::ibc::lightclients::solomachine::v1::Header as RawSolHeader;
use ibc_proto::protobuf::Protobuf;
use prost::Message;

pub const SOLOMACHINE_HEADER_TYPE_URL: &str = "/ibc.lightclients.solomachine.v1.Header";

/// Header defines a solo machine consensus header
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, PartialEq)]
pub struct Header {
/// sequence to update solo machine public key at
pub sequence: u64,
pub timestamp: u64,
pub sequence: Height,
pub timestamp: Timestamp,
pub signature: Vec<u8>,
pub new_public_key: Option<Any>,
pub new_diversifier: String,
Expand All @@ -38,11 +37,11 @@ impl Display for Header {

impl crate::core::ics02_client::header::Header for Header {
fn height(&self) -> Height {
todo!()
self.sequence
}

fn timestamp(&self) -> Timestamp {
todo!()
self.timestamp
}
}

Expand All @@ -56,6 +55,12 @@ impl TryFrom<RawSolHeader> for Header {
}
}

impl From<Header> for RawSolHeader {
fn from(value: Header) -> Self {
todo!()
}
}

impl Protobuf<Any> for Header {}

impl TryFrom<Any> for Header {
Expand Down Expand Up @@ -86,9 +91,3 @@ impl From<Header> for Any {
pub fn decode_header<B: Buf>(buf: B) -> Result<Header, Error> {
RawSolHeader::decode(buf).map_err(Error::Decode)?.try_into()
}

impl From<Header> for RawSolHeader {
fn from(value: Header) -> Self {
todo!()
}
}
35 changes: 30 additions & 5 deletions crates/ibc/src/clients/ics06_solomachine/misbehaviour.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::clients::ics06_solomachine::error::Error;
use crate::core::ics02_client::error::ClientError;
use crate::prelude::*;
use crate::core::ics24_host::identifier::ClientId;
use crate::{prelude::*, Height};
use bytes::Buf;
use ibc_proto::google::protobuf::Any;
use ibc_proto::ibc::lightclients::solomachine::v1::Misbehaviour as RawSolMisbehaviour;
Expand All @@ -18,8 +19,8 @@ pub const SOLOMACHINE_MISBEHAVIOUR_TYPE_URL: &str = "/ibc.lightclients.solomachi
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, PartialEq)]
pub struct Misbehaviour {
pub client_id: String,
pub sequence: u64,
pub client_id: ClientId,
pub sequence: Height,
pub signature_one: Option<SignatureAndData>,
pub signature_two: Option<SignatureAndData>,
}
Expand All @@ -30,13 +31,37 @@ impl TryFrom<RawSolMisbehaviour> for Misbehaviour {
type Error = Error;

fn try_from(raw: RawSolMisbehaviour) -> Result<Self, Self::Error> {
todo!()
let client_id: ClientId = raw
.client_id
.parse()
.map_err(|_| Error::InvalidRawClientId {
client_id: raw.client_id.clone(),
})?;
let sequence = Height::new(0, raw.sequence).map_err(Error::InvalidHeight)?;
let signature_one: Option<SignatureAndData> =
raw.signature_one.map(TryInto::try_into).transpose()?;
let signature_two: Option<SignatureAndData> =
raw.signature_two.map(TryInto::try_into).transpose()?;
Ok(Self {
client_id,
sequence,
signature_one,
signature_two,
})
}
}

impl From<Misbehaviour> for RawSolMisbehaviour {
fn from(value: Misbehaviour) -> Self {
todo!()
let client_id = value.client_id.to_string();
let sequence = value.sequence.revision_height();

Self {
client_id,
sequence,
signature_one: value.signature_one.map(Into::into),
signature_two: value.signature_two.map(Into::into),
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::clients::ics06_solomachine::error::Error;
use crate::clients::ics06_solomachine::types::DataType;
use crate::prelude::*;
use crate::timestamp::Timestamp;
use ibc_proto::ibc::lightclients::solomachine::v1::SignatureAndData as RawSignatureAndData;
use ibc_proto::protobuf::Protobuf;

Expand All @@ -12,7 +13,7 @@ pub struct SignatureAndData {
pub signature: Vec<u8>,
pub data_type: DataType,
pub data: Vec<u8>,
pub timestamp: u64,
pub timestamp: Timestamp,
}

impl Protobuf<RawSignatureAndData> for SignatureAndData {}
Expand All @@ -21,12 +22,27 @@ impl TryFrom<RawSignatureAndData> for SignatureAndData {
type Error = Error;

fn try_from(raw: RawSignatureAndData) -> Result<Self, Self::Error> {
todo!()
let signature = raw.signature;
let data_type = DataType::try_from(raw.data_type)?;
let data = raw.data;
let timestamp =
Timestamp::from_nanoseconds(raw.timestamp).map_err(Error::ParseTimeError)?;
Ok(Self {
signature,
data_type,
data,
timestamp,
})
}
}

impl From<SignatureAndData> for RawSignatureAndData {
fn from(value: SignatureAndData) -> Self {
todo!()
Self {
signature: value.signature,
data_type: i32::from(value.data_type),
data: value.data,
timestamp: value.timestamp.nanoseconds(),
}
}
}
69 changes: 63 additions & 6 deletions crates/ibc/src/clients/ics06_solomachine/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,73 @@ pub enum DataType {
Header,
}

impl TryFrom<RawDataType> for DataType {
type Error = Error;

fn try_from(raw: RawDataType) -> Result<Self, Self::Error> {
todo!()
impl From<RawDataType> for DataType {
fn from(raw: RawDataType) -> Self {
match raw {
RawDataType::UninitializedUnspecified => Self::UninitializedUnspecified,
RawDataType::ClientState => Self::ClientState,
RawDataType::ConsensusState => Self::ConsensusState,
RawDataType::ConnectionState => Self::ConnectionState,
RawDataType::ChannelState => Self::ChannelState,
RawDataType::PacketCommitment => Self::PacketCommitment,
RawDataType::PacketAcknowledgement => Self::PacketAcknowledgement,
RawDataType::PacketReceiptAbsence => Self::PacketReceiptAbsence,
RawDataType::NextSequenceRecv => Self::NextSequenceRecv,
RawDataType::Header => Self::Header,
}
}
}

impl From<DataType> for RawDataType {
fn from(value: DataType) -> Self {
todo!()
match value {
DataType::UninitializedUnspecified => Self::UninitializedUnspecified,
DataType::ClientState => Self::ClientState,
DataType::ConsensusState => Self::ConsensusState,
DataType::ConnectionState => Self::ConnectionState,
DataType::ChannelState => Self::ChannelState,
DataType::PacketCommitment => Self::PacketCommitment,
DataType::PacketAcknowledgement => Self::PacketAcknowledgement,
DataType::PacketReceiptAbsence => Self::PacketReceiptAbsence,
DataType::NextSequenceRecv => Self::NextSequenceRecv,
DataType::Header => Self::Header,
}
}
}

impl TryFrom<i32> for DataType {
type Error = Error;
fn try_from(value: i32) -> Result<Self, Self::Error> {
let data_type = match value {
0 => Self::UninitializedUnspecified,
1 => Self::ClientState,
2 => Self::ConsensusState,
3 => Self::ConnectionState,
4 => Self::ChannelState,
5 => Self::PacketCommitment,
6 => Self::PacketAcknowledgement,
7 => Self::PacketReceiptAbsence,
8 => Self::NextSequenceRecv,
9 => Self::Header,
i => return Err(Error::UnknownDataType(i)),
};
Ok(data_type)
}
}

impl From<DataType> for i32 {
fn from(value: DataType) -> Self {
match value {
DataType::UninitializedUnspecified => 0,
DataType::ClientState => 1,
DataType::ConsensusState => 2,
DataType::ConnectionState => 3,
DataType::ChannelState => 4,
DataType::PacketCommitment => 5,
DataType::PacketAcknowledgement => 6,
DataType::PacketReceiptAbsence => 7,
DataType::NextSequenceRecv => 8,
DataType::Header => 9,
}
}
}

0 comments on commit 39e29c9

Please sign in to comment.