From c789909ee4326186f5136598a2e1faf97203110b Mon Sep 17 00:00:00 2001 From: Davirain Date: Tue, 25 Apr 2023 20:06:39 +0800 Subject: [PATCH] Refactor ICS06 Solomachine client, consensus and packet handling --- .../src/clients/ics06_solomachine/error.rs | 3 +++ .../types/channel_state_data.rs | 14 ++++++++-- .../types/client_stata_data.rs | 5 ++-- .../types/consensus_state_data.rs | 5 ++-- .../types/next_sequence_recv_data.rs | 10 +++++-- .../types/packet_acknowledgement_data.rs | 10 +++++-- .../types/packet_commitment_data.rs | 10 +++++-- .../ics06_solomachine/types/sign_bytes.rs | 26 ++++++++++++++----- .../types/timestamped_signature_data.rs | 13 +++++++--- 9 files changed, 75 insertions(+), 21 deletions(-) diff --git a/crates/ibc/src/clients/ics06_solomachine/error.rs b/crates/ibc/src/clients/ics06_solomachine/error.rs index b0d6c123d..8035c94e7 100644 --- a/crates/ibc/src/clients/ics06_solomachine/error.rs +++ b/crates/ibc/src/clients/ics06_solomachine/error.rs @@ -1,3 +1,4 @@ +use crate::core::ics04_channel::error::ChannelError; use crate::prelude::*; use crate::core::ics02_client::error::ClientError; @@ -20,6 +21,8 @@ pub enum Error { UnknownDataType(i32), /// prase time error ParseTimeError(ParseTimestampError), + /// Channel error: `{0}` + ChannelError(ChannelError), } impl From for ClientError { diff --git a/crates/ibc/src/clients/ics06_solomachine/types/channel_state_data.rs b/crates/ibc/src/clients/ics06_solomachine/types/channel_state_data.rs index 836a4cc93..12a39888e 100644 --- a/crates/ibc/src/clients/ics06_solomachine/types/channel_state_data.rs +++ b/crates/ibc/src/clients/ics06_solomachine/types/channel_state_data.rs @@ -17,12 +17,22 @@ impl TryFrom for ChannelStateData { type Error = Error; fn try_from(raw: RawChannelStateData) -> Result { - todo!() + Ok(Self { + path: raw.path, + channel: raw + .channel + .map(TryInto::try_into) + .transpose() + .map_err(Error::ChannelError)?, + }) } } impl From for RawChannelStateData { fn from(value: ChannelStateData) -> Self { - todo!() + Self { + path: value.path, + channel: value.channel.map(Into::into), + } } } diff --git a/crates/ibc/src/clients/ics06_solomachine/types/client_stata_data.rs b/crates/ibc/src/clients/ics06_solomachine/types/client_stata_data.rs index a99cff820..107f35347 100644 --- a/crates/ibc/src/clients/ics06_solomachine/types/client_stata_data.rs +++ b/crates/ibc/src/clients/ics06_solomachine/types/client_stata_data.rs @@ -1,6 +1,6 @@ +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; @@ -8,7 +8,8 @@ use ibc_proto::protobuf::Protobuf; #[derive(Clone, PartialEq)] pub struct ClientStateData { pub path: Vec, - pub client_state: Option, + // Ics06 solomachine client state + pub client_state: Option, } impl Protobuf for ClientStateData {} diff --git a/crates/ibc/src/clients/ics06_solomachine/types/consensus_state_data.rs b/crates/ibc/src/clients/ics06_solomachine/types/consensus_state_data.rs index eca0bf847..f2f702bb3 100644 --- a/crates/ibc/src/clients/ics06_solomachine/types/consensus_state_data.rs +++ b/crates/ibc/src/clients/ics06_solomachine/types/consensus_state_data.rs @@ -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; @@ -10,7 +10,8 @@ use ibc_proto::protobuf::Protobuf; #[derive(Clone, PartialEq)] pub struct ConsensusStateData { pub path: Vec, - pub consensus_state: Option, + // ics06 solomachine client consensus state + pub consensus_state: Option, } impl Protobuf for ConsensusStateData {} diff --git a/crates/ibc/src/clients/ics06_solomachine/types/next_sequence_recv_data.rs b/crates/ibc/src/clients/ics06_solomachine/types/next_sequence_recv_data.rs index ef7fb8402..2c83d9c65 100644 --- a/crates/ibc/src/clients/ics06_solomachine/types/next_sequence_recv_data.rs +++ b/crates/ibc/src/clients/ics06_solomachine/types/next_sequence_recv_data.rs @@ -18,12 +18,18 @@ impl TryFrom for NextSequenceRecvData { type Error = Error; fn try_from(raw: RawNextSequenceRecvData) -> Result { - todo!() + Ok(Self { + path: raw.path, + next_seq_recv: raw.next_seq_recv, + }) } } impl From for RawNextSequenceRecvData { fn from(value: NextSequenceRecvData) -> Self { - todo!() + Self { + path: value.path, + next_seq_recv: value.next_seq_recv, + } } } diff --git a/crates/ibc/src/clients/ics06_solomachine/types/packet_acknowledgement_data.rs b/crates/ibc/src/clients/ics06_solomachine/types/packet_acknowledgement_data.rs index 826fe8121..56a712846 100644 --- a/crates/ibc/src/clients/ics06_solomachine/types/packet_acknowledgement_data.rs +++ b/crates/ibc/src/clients/ics06_solomachine/types/packet_acknowledgement_data.rs @@ -17,12 +17,18 @@ impl TryFrom for PacketAcknowledgementData { type Error = Error; fn try_from(raw: RawPacketAcknowledgementData) -> Result { - todo!() + Ok(Self { + path: raw.path, + acknowledgement: raw.acknowledgement, + }) } } impl From for RawPacketAcknowledgementData { fn from(value: PacketAcknowledgementData) -> Self { - todo!() + Self { + path: value.path, + acknowledgement: value.acknowledgement, + } } } diff --git a/crates/ibc/src/clients/ics06_solomachine/types/packet_commitment_data.rs b/crates/ibc/src/clients/ics06_solomachine/types/packet_commitment_data.rs index dc7196b99..1c13aae2c 100644 --- a/crates/ibc/src/clients/ics06_solomachine/types/packet_commitment_data.rs +++ b/crates/ibc/src/clients/ics06_solomachine/types/packet_commitment_data.rs @@ -17,12 +17,18 @@ impl TryFrom for PacketCommitmentData { type Error = Error; fn try_from(raw: RawPacketCommitmentData) -> Result { - todo!() + Ok(Self { + path: raw.path, + commitment: raw.commitment, + }) } } impl From for RawPacketCommitmentData { fn from(value: PacketCommitmentData) -> Self { - todo!() + Self { + path: value.path, + commitment: value.commitment, + } } } diff --git a/crates/ibc/src/clients/ics06_solomachine/types/sign_bytes.rs b/crates/ibc/src/clients/ics06_solomachine/types/sign_bytes.rs index a44ffb55a..1736df266 100644 --- a/crates/ibc/src/clients/ics06_solomachine/types/sign_bytes.rs +++ b/crates/ibc/src/clients/ics06_solomachine/types/sign_bytes.rs @@ -1,7 +1,9 @@ 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. @@ -9,10 +11,10 @@ use ibc_proto::ibc::lightclients::solomachine::v1::TimestampedSignatureData as R #[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, } @@ -23,12 +25,24 @@ impl TryFrom for SignBytes { type Error = Error; fn try_from(raw: RawSignBytes) -> Result { - 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 for RawTimestampedSignatureData { +impl From 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, + } } } diff --git a/crates/ibc/src/clients/ics06_solomachine/types/timestamped_signature_data.rs b/crates/ibc/src/clients/ics06_solomachine/types/timestamped_signature_data.rs index 6c8e4c13b..6120d4a96 100644 --- a/crates/ibc/src/clients/ics06_solomachine/types/timestamped_signature_data.rs +++ b/crates/ibc/src/clients/ics06_solomachine/types/timestamped_signature_data.rs @@ -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; @@ -9,7 +10,7 @@ use ibc_proto::protobuf::Protobuf; #[derive(Clone, PartialEq)] pub struct TimestampedSignatureData { pub signature_data: Vec, - pub timestamp: u64, + pub timestamp: Timestamp, } impl Protobuf for TimestampedSignatureData {} @@ -18,12 +19,18 @@ impl TryFrom for TimestampedSignatureData { type Error = Error; fn try_from(raw: RawTimestampedSignatureData) -> Result { - todo!() + Ok(Self { + signature_data: raw.signature_data, + timestamp: Timestamp::from_nanoseconds(raw.timestamp).map_err(Error::ParseTimeError)?, + }) } } impl From for RawTimestampedSignatureData { fn from(value: TimestampedSignatureData) -> Self { - todo!() + Self { + signature_data: value.signature_data, + timestamp: value.timestamp.nanoseconds(), + } } }