Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add codec borsh feature #273

Merged
merged 20 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
163 changes: 159 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 @@ -97,6 +109,125 @@ pub struct ConnectionEnd {
delay_period: Duration,
}

mod sealed {
plafer marked this conversation as resolved.
Show resolved Hide resolved
use super::*;

#[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)
)]
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 parity_scale_codec::Encode for ConnectionEnd {
fn encode_to<T: parity_scale_codec::Output + ?Sized>(&self, writer: &mut T) {
let value = InnerConnectionEnd::from(self.clone());
value.encode_to(writer);
}
}

#[cfg(feature = "parity-scale-codec")]
impl parity_scale_codec::Decode for ConnectionEnd {
fn decode<I: parity_scale_codec::Input>(
input: &mut I,
) -> Result<Self, parity_scale_codec::Error> {
let result = InnerConnectionEnd::decode(input)?;
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 +378,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 +472,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
Loading