Skip to content

Commit

Permalink
Add codec borsh feature (#273)
Browse files Browse the repository at this point in the history
Closes #259
  • Loading branch information
DaviRain-Su authored Dec 16, 2022
1 parent edb9d7c commit cec7c31
Show file tree
Hide file tree
Showing 18 changed files with 508 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Add serialization and deserialization features for codec and borsh to the host
type in ics24 ([#259](https://github.com/cosmos/ibc-rs/issues/259))
7 changes: 7 additions & 0 deletions crates/ibc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ std = [
"tendermint/std",
]
clock = ["tendermint/clock", "time/std"]
parity-scale-codec = ["dep:parity-scale-codec", "dep:scale-info"]
borsh = ["dep:borsh"]

# This feature guards the unfinished implementation of ADR 5.
val_exec_ctx = []
Expand Down Expand Up @@ -66,6 +68,11 @@ derive_more = { version = "0.99.17", default-features = false, features = ["from
uint = { version = "0.9", default-features = false }
primitive-types = { version = "0.12.0", default-features = false, features = ["serde_no_std"] }
dyn-clone = "1.0.8"
## for codec encode or decode
parity-scale-codec = { version = "3.0.0", default-features = false, features = ["full"], optional = true }
scale-info = { version = "2.1.2", default-features = false, features = ["derive"], optional = true }
## for borsh encode or decode
borsh = {version = "0.9.3", default-features = false, optional = true }

[dependencies.tendermint]
version = "0.28"
Expand Down
12 changes: 12 additions & 0 deletions crates/ibc/src/core/ics02_client/client_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ use crate::prelude::*;
use core::fmt::{Display, Error as FmtError, Formatter};
use serde_derive::{Deserialize, Serialize};

#[cfg_attr(
feature = "parity-scale-codec",
derive(
parity_scale_codec::Encode,
parity_scale_codec::Decode,
scale_info::TypeInfo
)
)]
#[cfg_attr(
feature = "borsh",
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
/// Type of the client, depending on the specific consensus algorithm.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct ClientType(String);
Expand Down
12 changes: 12 additions & 0 deletions crates/ibc/src/core/ics02_client/height.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ use ibc_proto::ibc::core::client::v1::Height as RawHeight;

use crate::core::ics02_client::error::ClientError;

#[cfg_attr(
feature = "parity-scale-codec",
derive(
parity_scale_codec::Encode,
parity_scale_codec::Decode,
scale_info::TypeInfo
)
)]
#[cfg_attr(
feature = "borsh",
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[derive(Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Height {
/// Previously known as "epoch"
Expand Down
12 changes: 12 additions & 0 deletions crates/ibc/src/core/ics02_client/trust_threshold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ use crate::core::ics02_client::error::ClientError;
/// A typical trust threshold is 1/3 in practice.
/// This type accepts even a value of 0, (numerator = 0, denominator = 0),
/// which is used in the client state of an upgrading client.
#[cfg_attr(
feature = "parity-scale-codec",
derive(
parity_scale_codec::Encode,
parity_scale_codec::Decode,
scale_info::TypeInfo
)
)]
#[cfg_attr(
feature = "borsh",
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct TrustThreshold {
numerator: u64,
Expand Down
141 changes: 137 additions & 4 deletions crates/ibc/src/core/ics03_connection/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ use crate::core::ics24_host::error::ValidationError;
use crate::core::ics24_host::identifier::{ClientId, ConnectionId};
use crate::timestamp::ZERO_DURATION;

#[cfg_attr(
feature = "parity-scale-codec",
derive(
parity_scale_codec::Encode,
parity_scale_codec::Decode,
scale_info::TypeInfo
)
)]
#[cfg_attr(
feature = "borsh",
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct IdentifiedConnectionEnd {
pub connection_id: ConnectionId,
Expand Down Expand Up @@ -88,6 +100,10 @@ impl From<IdentifiedConnectionEnd> for RawIdentifiedConnection {
}
}

#[cfg_attr(
feature = "parity-scale-codec",
derive(parity_scale_codec::Encode, parity_scale_codec::Decode,)
)]
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ConnectionEnd {
pub state: State,
Expand All @@ -97,6 +113,99 @@ pub struct ConnectionEnd {
delay_period: Duration,
}

mod sealed {
use super::*;

#[cfg_attr(
feature = "borsh",
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
struct InnerConnectionEnd {
pub state: State,
client_id: ClientId,
counterparty: Counterparty,
versions: Vec<Version>,
delay_period_secs: u64,
delay_period_nanos: u32,
}

impl From<InnerConnectionEnd> for ConnectionEnd {
fn from(value: InnerConnectionEnd) -> Self {
Self {
state: value.state,
client_id: value.client_id,
counterparty: value.counterparty,
versions: value.versions,
delay_period: Duration::new(value.delay_period_secs, value.delay_period_nanos),
}
}
}

impl From<ConnectionEnd> for InnerConnectionEnd {
fn from(value: ConnectionEnd) -> Self {
Self {
state: value.state,
client_id: value.client_id,
counterparty: value.counterparty,
versions: value.versions,
delay_period_secs: value.delay_period.as_secs(),
delay_period_nanos: value.delay_period.subsec_nanos(),
}
}
}

#[cfg(feature = "borsh")]
impl borsh::BorshSerialize for ConnectionEnd {
fn serialize<W: borsh::maybestd::io::Write>(
&self,
writer: &mut W,
) -> borsh::maybestd::io::Result<()> {
let value = InnerConnectionEnd::from(self.clone());
borsh::BorshSerialize::serialize(&value, writer)
}
}

#[cfg(feature = "borsh")]
impl borsh::BorshDeserialize for ConnectionEnd {
fn deserialize(buf: &mut &[u8]) -> borsh::maybestd::io::Result<Self> {
let result = InnerConnectionEnd::deserialize(buf)?;
Ok(ConnectionEnd::from(result))
}
}

#[cfg(feature = "parity-scale-codec")]
impl scale_info::TypeInfo for ConnectionEnd {
type Identity = Self;

fn type_info() -> scale_info::Type {
scale_info::Type::builder()
.path(scale_info::Path::new("ConnectionEnd", module_path!()))
.composite(
scale_info::build::Fields::named()
.field(|f| f.ty::<State>().name("state").type_name("State"))
.field(|f| f.ty::<ClientId>().name("client_id").type_name("ClientId"))
.field(|f| {
f.ty::<Counterparty>()
.name("counterparty")
.type_name("Counterparty")
})
.field(|f| {
f.ty::<Counterparty>()
.name("counterparty")
.type_name("Counterparty")
})
.field(|f| {
f.ty::<Vec<Version>>()
.name("versions")
.type_name("Vec<Version>")
})
.field(|f| f.ty::<u64>().name("delay_period_secs").type_name("u64"))
.field(|f| f.ty::<u32>().name("delay_period_nanos").type_name("u32")),
)
}
}
}

impl Default for ConnectionEnd {
fn default() -> Self {
Self {
Expand Down Expand Up @@ -247,6 +356,18 @@ impl ConnectionEnd {
}
}

#[cfg_attr(
feature = "parity-scale-codec",
derive(
parity_scale_codec::Encode,
parity_scale_codec::Decode,
scale_info::TypeInfo
)
)]
#[cfg_attr(
feature = "borsh",
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Counterparty {
client_id: ClientId,
Expand Down Expand Up @@ -329,12 +450,24 @@ impl Counterparty {
}
}

#[cfg_attr(
feature = "parity-scale-codec",
derive(
parity_scale_codec::Encode,
parity_scale_codec::Decode,
scale_info::TypeInfo
)
)]
#[cfg_attr(
feature = "borsh",
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum State {
Uninitialized = 0,
Init = 1,
TryOpen = 2,
Open = 3,
Uninitialized = 0isize,
Init = 1isize,
TryOpen = 2isize,
Open = 3isize,
}

impl State {
Expand Down
12 changes: 12 additions & 0 deletions crates/ibc/src/core/ics03_connection/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ use crate::core::ics03_connection::error::ConnectionError;
use crate::core::ics04_channel::channel::Order;

/// Stores the identifier and the features supported by a version
#[cfg_attr(
feature = "parity-scale-codec",
derive(
parity_scale_codec::Encode,
parity_scale_codec::Decode,
scale_info::TypeInfo
)
)]
#[cfg_attr(
feature = "borsh",
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Version {
/// unique version identifier
Expand Down
76 changes: 68 additions & 8 deletions crates/ibc/src/core/ics04_channel/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ use ibc_proto::ibc::core::channel::v1::{
use crate::core::ics04_channel::{error::ChannelError, Version};
use crate::core::ics24_host::identifier::{ChannelId, ConnectionId, PortId};

#[cfg_attr(
feature = "parity-scale-codec",
derive(
parity_scale_codec::Encode,
parity_scale_codec::Decode,
scale_info::TypeInfo
)
)]
#[cfg_attr(
feature = "borsh",
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct IdentifiedChannelEnd {
pub port_id: PortId,
Expand Down Expand Up @@ -73,6 +85,18 @@ impl From<IdentifiedChannelEnd> for RawIdentifiedChannel {
}
}

#[cfg_attr(
feature = "parity-scale-codec",
derive(
parity_scale_codec::Encode,
parity_scale_codec::Decode,
scale_info::TypeInfo
)
)]
#[cfg_attr(
feature = "borsh",
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ChannelEnd {
pub state: State,
Expand Down Expand Up @@ -250,6 +274,18 @@ impl ChannelEnd {
}
}

#[cfg_attr(
feature = "parity-scale-codec",
derive(
parity_scale_codec::Encode,
parity_scale_codec::Decode,
scale_info::TypeInfo
)
)]
#[cfg_attr(
feature = "borsh",
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Counterparty {
pub port_id: PortId,
Expand Down Expand Up @@ -323,11 +359,23 @@ impl From<Counterparty> for RawCounterparty {
}
}

#[cfg_attr(
feature = "parity-scale-codec",
derive(
parity_scale_codec::Encode,
parity_scale_codec::Decode,
scale_info::TypeInfo
)
)]
#[cfg_attr(
feature = "borsh",
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub enum Order {
None = 0,
Unordered = 1,
Ordered = 2,
None = 0isize,
Unordered = 1isize,
Ordered = 2isize,
}

impl Default for Order {
Expand Down Expand Up @@ -381,13 +429,25 @@ impl FromStr for Order {
}
}

#[cfg_attr(
feature = "parity-scale-codec",
derive(
parity_scale_codec::Encode,
parity_scale_codec::Decode,
scale_info::TypeInfo
)
)]
#[cfg_attr(
feature = "borsh",
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum State {
Uninitialized = 0,
Init = 1,
TryOpen = 2,
Open = 3,
Closed = 4,
Uninitialized = 0isize,
Init = 1isize,
TryOpen = 2isize,
Open = 3isize,
Closed = 4isize,
}

impl State {
Expand Down
Loading

0 comments on commit cec7c31

Please sign in to comment.