Skip to content

Commit cec7c31

Browse files
authored
Add codec borsh feature (#273)
Closes #259
1 parent edb9d7c commit cec7c31

File tree

18 files changed

+508
-13
lines changed

18 files changed

+508
-13
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- Add serialization and deserialization features for codec and borsh to the host
2+
type in ics24 ([#259](https://github.com/cosmos/ibc-rs/issues/259))

crates/ibc/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ std = [
3737
"tendermint/std",
3838
]
3939
clock = ["tendermint/clock", "time/std"]
40+
parity-scale-codec = ["dep:parity-scale-codec", "dep:scale-info"]
41+
borsh = ["dep:borsh"]
4042

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

7077
[dependencies.tendermint]
7178
version = "0.28"

crates/ibc/src/core/ics02_client/client_type.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@ use crate::prelude::*;
22
use core::fmt::{Display, Error as FmtError, Formatter};
33
use serde_derive::{Deserialize, Serialize};
44

5+
#[cfg_attr(
6+
feature = "parity-scale-codec",
7+
derive(
8+
parity_scale_codec::Encode,
9+
parity_scale_codec::Decode,
10+
scale_info::TypeInfo
11+
)
12+
)]
13+
#[cfg_attr(
14+
feature = "borsh",
15+
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
16+
)]
517
/// Type of the client, depending on the specific consensus algorithm.
618
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
719
pub struct ClientType(String);

crates/ibc/src/core/ics02_client/height.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ use ibc_proto::ibc::core::client::v1::Height as RawHeight;
1212

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

15+
#[cfg_attr(
16+
feature = "parity-scale-codec",
17+
derive(
18+
parity_scale_codec::Encode,
19+
parity_scale_codec::Decode,
20+
scale_info::TypeInfo
21+
)
22+
)]
23+
#[cfg_attr(
24+
feature = "borsh",
25+
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
26+
)]
1527
#[derive(Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
1628
pub struct Height {
1729
/// Previously known as "epoch"

crates/ibc/src/core/ics02_client/trust_threshold.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ use crate::core::ics02_client::error::ClientError;
2323
/// A typical trust threshold is 1/3 in practice.
2424
/// This type accepts even a value of 0, (numerator = 0, denominator = 0),
2525
/// which is used in the client state of an upgrading client.
26+
#[cfg_attr(
27+
feature = "parity-scale-codec",
28+
derive(
29+
parity_scale_codec::Encode,
30+
parity_scale_codec::Decode,
31+
scale_info::TypeInfo
32+
)
33+
)]
34+
#[cfg_attr(
35+
feature = "borsh",
36+
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
37+
)]
2638
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
2739
pub struct TrustThreshold {
2840
numerator: u64,

crates/ibc/src/core/ics03_connection/connection.rs

Lines changed: 137 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ use crate::core::ics24_host::error::ValidationError;
2323
use crate::core::ics24_host::identifier::{ClientId, ConnectionId};
2424
use crate::timestamp::ZERO_DURATION;
2525

26+
#[cfg_attr(
27+
feature = "parity-scale-codec",
28+
derive(
29+
parity_scale_codec::Encode,
30+
parity_scale_codec::Decode,
31+
scale_info::TypeInfo
32+
)
33+
)]
34+
#[cfg_attr(
35+
feature = "borsh",
36+
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
37+
)]
2638
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
2739
pub struct IdentifiedConnectionEnd {
2840
pub connection_id: ConnectionId,
@@ -88,6 +100,10 @@ impl From<IdentifiedConnectionEnd> for RawIdentifiedConnection {
88100
}
89101
}
90102

103+
#[cfg_attr(
104+
feature = "parity-scale-codec",
105+
derive(parity_scale_codec::Encode, parity_scale_codec::Decode,)
106+
)]
91107
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
92108
pub struct ConnectionEnd {
93109
pub state: State,
@@ -97,6 +113,99 @@ pub struct ConnectionEnd {
97113
delay_period: Duration,
98114
}
99115

116+
mod sealed {
117+
use super::*;
118+
119+
#[cfg_attr(
120+
feature = "borsh",
121+
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
122+
)]
123+
struct InnerConnectionEnd {
124+
pub state: State,
125+
client_id: ClientId,
126+
counterparty: Counterparty,
127+
versions: Vec<Version>,
128+
delay_period_secs: u64,
129+
delay_period_nanos: u32,
130+
}
131+
132+
impl From<InnerConnectionEnd> for ConnectionEnd {
133+
fn from(value: InnerConnectionEnd) -> Self {
134+
Self {
135+
state: value.state,
136+
client_id: value.client_id,
137+
counterparty: value.counterparty,
138+
versions: value.versions,
139+
delay_period: Duration::new(value.delay_period_secs, value.delay_period_nanos),
140+
}
141+
}
142+
}
143+
144+
impl From<ConnectionEnd> for InnerConnectionEnd {
145+
fn from(value: ConnectionEnd) -> Self {
146+
Self {
147+
state: value.state,
148+
client_id: value.client_id,
149+
counterparty: value.counterparty,
150+
versions: value.versions,
151+
delay_period_secs: value.delay_period.as_secs(),
152+
delay_period_nanos: value.delay_period.subsec_nanos(),
153+
}
154+
}
155+
}
156+
157+
#[cfg(feature = "borsh")]
158+
impl borsh::BorshSerialize for ConnectionEnd {
159+
fn serialize<W: borsh::maybestd::io::Write>(
160+
&self,
161+
writer: &mut W,
162+
) -> borsh::maybestd::io::Result<()> {
163+
let value = InnerConnectionEnd::from(self.clone());
164+
borsh::BorshSerialize::serialize(&value, writer)
165+
}
166+
}
167+
168+
#[cfg(feature = "borsh")]
169+
impl borsh::BorshDeserialize for ConnectionEnd {
170+
fn deserialize(buf: &mut &[u8]) -> borsh::maybestd::io::Result<Self> {
171+
let result = InnerConnectionEnd::deserialize(buf)?;
172+
Ok(ConnectionEnd::from(result))
173+
}
174+
}
175+
176+
#[cfg(feature = "parity-scale-codec")]
177+
impl scale_info::TypeInfo for ConnectionEnd {
178+
type Identity = Self;
179+
180+
fn type_info() -> scale_info::Type {
181+
scale_info::Type::builder()
182+
.path(scale_info::Path::new("ConnectionEnd", module_path!()))
183+
.composite(
184+
scale_info::build::Fields::named()
185+
.field(|f| f.ty::<State>().name("state").type_name("State"))
186+
.field(|f| f.ty::<ClientId>().name("client_id").type_name("ClientId"))
187+
.field(|f| {
188+
f.ty::<Counterparty>()
189+
.name("counterparty")
190+
.type_name("Counterparty")
191+
})
192+
.field(|f| {
193+
f.ty::<Counterparty>()
194+
.name("counterparty")
195+
.type_name("Counterparty")
196+
})
197+
.field(|f| {
198+
f.ty::<Vec<Version>>()
199+
.name("versions")
200+
.type_name("Vec<Version>")
201+
})
202+
.field(|f| f.ty::<u64>().name("delay_period_secs").type_name("u64"))
203+
.field(|f| f.ty::<u32>().name("delay_period_nanos").type_name("u32")),
204+
)
205+
}
206+
}
207+
}
208+
100209
impl Default for ConnectionEnd {
101210
fn default() -> Self {
102211
Self {
@@ -247,6 +356,18 @@ impl ConnectionEnd {
247356
}
248357
}
249358

359+
#[cfg_attr(
360+
feature = "parity-scale-codec",
361+
derive(
362+
parity_scale_codec::Encode,
363+
parity_scale_codec::Decode,
364+
scale_info::TypeInfo
365+
)
366+
)]
367+
#[cfg_attr(
368+
feature = "borsh",
369+
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
370+
)]
250371
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
251372
pub struct Counterparty {
252373
client_id: ClientId,
@@ -329,12 +450,24 @@ impl Counterparty {
329450
}
330451
}
331452

453+
#[cfg_attr(
454+
feature = "parity-scale-codec",
455+
derive(
456+
parity_scale_codec::Encode,
457+
parity_scale_codec::Decode,
458+
scale_info::TypeInfo
459+
)
460+
)]
461+
#[cfg_attr(
462+
feature = "borsh",
463+
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
464+
)]
332465
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
333466
pub enum State {
334-
Uninitialized = 0,
335-
Init = 1,
336-
TryOpen = 2,
337-
Open = 3,
467+
Uninitialized = 0isize,
468+
Init = 1isize,
469+
TryOpen = 2isize,
470+
Open = 3isize,
338471
}
339472

340473
impl State {

crates/ibc/src/core/ics03_connection/version.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ use crate::core::ics03_connection::error::ConnectionError;
1111
use crate::core::ics04_channel::channel::Order;
1212

1313
/// Stores the identifier and the features supported by a version
14+
#[cfg_attr(
15+
feature = "parity-scale-codec",
16+
derive(
17+
parity_scale_codec::Encode,
18+
parity_scale_codec::Decode,
19+
scale_info::TypeInfo
20+
)
21+
)]
22+
#[cfg_attr(
23+
feature = "borsh",
24+
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
25+
)]
1426
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
1527
pub struct Version {
1628
/// unique version identifier

crates/ibc/src/core/ics04_channel/channel.rs

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ use ibc_proto::ibc::core::channel::v1::{
1515
use crate::core::ics04_channel::{error::ChannelError, Version};
1616
use crate::core::ics24_host::identifier::{ChannelId, ConnectionId, PortId};
1717

18+
#[cfg_attr(
19+
feature = "parity-scale-codec",
20+
derive(
21+
parity_scale_codec::Encode,
22+
parity_scale_codec::Decode,
23+
scale_info::TypeInfo
24+
)
25+
)]
26+
#[cfg_attr(
27+
feature = "borsh",
28+
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
29+
)]
1830
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
1931
pub struct IdentifiedChannelEnd {
2032
pub port_id: PortId,
@@ -73,6 +85,18 @@ impl From<IdentifiedChannelEnd> for RawIdentifiedChannel {
7385
}
7486
}
7587

88+
#[cfg_attr(
89+
feature = "parity-scale-codec",
90+
derive(
91+
parity_scale_codec::Encode,
92+
parity_scale_codec::Decode,
93+
scale_info::TypeInfo
94+
)
95+
)]
96+
#[cfg_attr(
97+
feature = "borsh",
98+
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
99+
)]
76100
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
77101
pub struct ChannelEnd {
78102
pub state: State,
@@ -250,6 +274,18 @@ impl ChannelEnd {
250274
}
251275
}
252276

277+
#[cfg_attr(
278+
feature = "parity-scale-codec",
279+
derive(
280+
parity_scale_codec::Encode,
281+
parity_scale_codec::Decode,
282+
scale_info::TypeInfo
283+
)
284+
)]
285+
#[cfg_attr(
286+
feature = "borsh",
287+
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
288+
)]
253289
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
254290
pub struct Counterparty {
255291
pub port_id: PortId,
@@ -323,11 +359,23 @@ impl From<Counterparty> for RawCounterparty {
323359
}
324360
}
325361

362+
#[cfg_attr(
363+
feature = "parity-scale-codec",
364+
derive(
365+
parity_scale_codec::Encode,
366+
parity_scale_codec::Decode,
367+
scale_info::TypeInfo
368+
)
369+
)]
370+
#[cfg_attr(
371+
feature = "borsh",
372+
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
373+
)]
326374
#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Serialize)]
327375
pub enum Order {
328-
None = 0,
329-
Unordered = 1,
330-
Ordered = 2,
376+
None = 0isize,
377+
Unordered = 1isize,
378+
Ordered = 2isize,
331379
}
332380

333381
impl Default for Order {
@@ -381,13 +429,25 @@ impl FromStr for Order {
381429
}
382430
}
383431

432+
#[cfg_attr(
433+
feature = "parity-scale-codec",
434+
derive(
435+
parity_scale_codec::Encode,
436+
parity_scale_codec::Decode,
437+
scale_info::TypeInfo
438+
)
439+
)]
440+
#[cfg_attr(
441+
feature = "borsh",
442+
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
443+
)]
384444
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
385445
pub enum State {
386-
Uninitialized = 0,
387-
Init = 1,
388-
TryOpen = 2,
389-
Open = 3,
390-
Closed = 4,
446+
Uninitialized = 0isize,
447+
Init = 1isize,
448+
TryOpen = 2isize,
449+
Open = 3isize,
450+
Closed = 4isize,
391451
}
392452

393453
impl State {

0 commit comments

Comments
 (0)