Skip to content

Commit

Permalink
Refactor ICS06 Solomachine client, consensus and packet handling
Browse files Browse the repository at this point in the history
  • Loading branch information
DaviRain-Su committed Apr 25, 2023
1 parent 39e29c9 commit c789909
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 21 deletions.
3 changes: 3 additions & 0 deletions crates/ibc/src/clients/ics06_solomachine/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::core::ics04_channel::error::ChannelError;
use crate::prelude::*;

use crate::core::ics02_client::error::ClientError;
Expand All @@ -20,6 +21,8 @@ pub enum Error {
UnknownDataType(i32),
/// prase time error
ParseTimeError(ParseTimestampError),
/// Channel error: `{0}`
ChannelError(ChannelError),
}

impl From<Error> for ClientError {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,22 @@ impl TryFrom<RawChannelStateData> for ChannelStateData {
type Error = Error;

fn try_from(raw: RawChannelStateData) -> Result<Self, Self::Error> {
todo!()
Ok(Self {
path: raw.path,
channel: raw
.channel
.map(TryInto::try_into)
.transpose()
.map_err(Error::ChannelError)?,
})
}
}

impl From<ChannelStateData> for RawChannelStateData {
fn from(value: ChannelStateData) -> Self {
todo!()
Self {
path: value.path,
channel: value.channel.map(Into::into),
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use crate::clients::ics06_solomachine::client_state::ClientState;
use crate::clients::ics06_solomachine::error::Error;
use crate::prelude::*;
use ibc_proto::google::protobuf::Any;
use ibc_proto::ibc::lightclients::solomachine::v1::ClientStateData as RawClientStateData;
use ibc_proto::protobuf::Protobuf;

/// ClientStateData returns the SignBytes data for client state verification.
#[derive(Clone, PartialEq)]
pub struct ClientStateData {
pub path: Vec<u8>,
pub client_state: Option<Any>,
// Ics06 solomachine client state
pub client_state: Option<ClientState>,
}

impl Protobuf<RawClientStateData> for ClientStateData {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::clients::ics06_solomachine::consensus_state::ConsensusState;
use crate::clients::ics06_solomachine::error::Error;
use crate::prelude::*;
use ibc_proto::google::protobuf::Any;
use ibc_proto::ibc::lightclients::solomachine::v1::ConsensusStateData as RawConsensusStateData;
use ibc_proto::protobuf::Protobuf;

Expand All @@ -10,7 +10,8 @@ use ibc_proto::protobuf::Protobuf;
#[derive(Clone, PartialEq)]
pub struct ConsensusStateData {
pub path: Vec<u8>,
pub consensus_state: Option<Any>,
// ics06 solomachine client consensus state
pub consensus_state: Option<ConsensusState>,
}

impl Protobuf<RawConsensusStateData> for ConsensusStateData {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@ impl TryFrom<RawNextSequenceRecvData> for NextSequenceRecvData {
type Error = Error;

fn try_from(raw: RawNextSequenceRecvData) -> Result<Self, Self::Error> {
todo!()
Ok(Self {
path: raw.path,
next_seq_recv: raw.next_seq_recv,
})
}
}

impl From<NextSequenceRecvData> for RawNextSequenceRecvData {
fn from(value: NextSequenceRecvData) -> Self {
todo!()
Self {
path: value.path,
next_seq_recv: value.next_seq_recv,
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@ impl TryFrom<RawPacketAcknowledgementData> for PacketAcknowledgementData {
type Error = Error;

fn try_from(raw: RawPacketAcknowledgementData) -> Result<Self, Self::Error> {
todo!()
Ok(Self {
path: raw.path,
acknowledgement: raw.acknowledgement,
})
}
}

impl From<PacketAcknowledgementData> for RawPacketAcknowledgementData {
fn from(value: PacketAcknowledgementData) -> Self {
todo!()
Self {
path: value.path,
acknowledgement: value.acknowledgement,
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@ impl TryFrom<RawPacketCommitmentData> for PacketCommitmentData {
type Error = Error;

fn try_from(raw: RawPacketCommitmentData) -> Result<Self, Self::Error> {
todo!()
Ok(Self {
path: raw.path,
commitment: raw.commitment,
})
}
}

impl From<PacketCommitmentData> for RawPacketCommitmentData {
fn from(value: PacketCommitmentData) -> Self {
todo!()
Self {
path: value.path,
commitment: value.commitment,
}
}
}
26 changes: 20 additions & 6 deletions crates/ibc/src/clients/ics06_solomachine/types/sign_bytes.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
use crate::clients::ics06_solomachine::error::Error;
use crate::prelude::*;
use crate::timestamp::Timestamp;
use ibc_proto::ibc::lightclients::solomachine::v1::SignBytes as RawSignBytes;
use ibc_proto::ibc::lightclients::solomachine::v1::TimestampedSignatureData as RawTimestampedSignatureData;

use super::DataType;
// use ibc_proto::protobuf::Protobuf;

/// SignBytes defines the signed bytes used for signature verification.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, PartialEq)]
pub struct SignBytes {
pub sequence: u64,
pub timestamp: u64,
pub timestamp: Timestamp,
pub diversifier: String,
/// type of the data used
pub data_type: i32,
pub data_type: DataType,
/// marshaled data
pub data: Vec<u8>,
}
Expand All @@ -23,12 +25,24 @@ impl TryFrom<RawSignBytes> for SignBytes {
type Error = Error;

fn try_from(raw: RawSignBytes) -> Result<Self, Self::Error> {
todo!()
Ok(Self {
sequence: raw.sequence,
timestamp: Timestamp::from_nanoseconds(raw.timestamp).map_err(Error::ParseTimeError)?,
diversifier: raw.diversifier,
data_type: DataType::try_from(raw.data_type)?,
data: raw.data,
})
}
}

impl From<SignBytes> for RawTimestampedSignatureData {
impl From<SignBytes> for RawSignBytes {
fn from(value: SignBytes) -> Self {
todo!()
Self {
sequence: value.sequence,
timestamp: value.timestamp.nanoseconds(),
diversifier: value.diversifier,
data_type: i32::from(value.data_type),
data: value.data,
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::clients::ics06_solomachine::error::Error;
use crate::prelude::*;
use crate::timestamp::Timestamp;
use ibc_proto::ibc::lightclients::solomachine::v1::TimestampedSignatureData as RawTimestampedSignatureData;
use ibc_proto::protobuf::Protobuf;

Expand All @@ -9,7 +10,7 @@ use ibc_proto::protobuf::Protobuf;
#[derive(Clone, PartialEq)]
pub struct TimestampedSignatureData {
pub signature_data: Vec<u8>,
pub timestamp: u64,
pub timestamp: Timestamp,
}

impl Protobuf<RawTimestampedSignatureData> for TimestampedSignatureData {}
Expand All @@ -18,12 +19,18 @@ impl TryFrom<RawTimestampedSignatureData> for TimestampedSignatureData {
type Error = Error;

fn try_from(raw: RawTimestampedSignatureData) -> Result<Self, Self::Error> {
todo!()
Ok(Self {
signature_data: raw.signature_data,
timestamp: Timestamp::from_nanoseconds(raw.timestamp).map_err(Error::ParseTimeError)?,
})
}
}

impl From<TimestampedSignatureData> for RawTimestampedSignatureData {
fn from(value: TimestampedSignatureData) -> Self {
todo!()
Self {
signature_data: value.signature_data,
timestamp: value.timestamp.nanoseconds(),
}
}
}

0 comments on commit c789909

Please sign in to comment.