Skip to content

Commit 545f8c7

Browse files
hu55a1n1DaviRain-SuplaferFarhad-Shabani
authored
Add serde feature (#331)
* Add serde feature * Fix clippy warnings * Feature gate serde imports * Fix for `ErasedSerialize` * Fix unused imports * Fix CI * cargo fmt * Add serde feature to no-std-check's ibc dep * Document ErasedSerialize re-export * Move TimeoutHeight serde into it's own module * Move ClientState serde tests into it's own module * Fix typo * Apply suggestions from code review Co-authored-by: Philippe Laferrière <[email protected]> Signed-off-by: Shoaib Ahmed <[email protected]> * Update issue templates and CONTRIBUTING.md (#345) * Update issue templates and CONTRIBUTING.md * Reflect review input * few edits Co-authored-by: Philippe Laferriere <[email protected]> * make transfer application serde-dependent Signed-off-by: Shoaib Ahmed <[email protected]> Co-authored-by: Davirain <[email protected]> Co-authored-by: Philippe Laferrière <[email protected]> Co-authored-by: Farhad Shabani <[email protected]>
1 parent 62fbd14 commit 545f8c7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+376
-208
lines changed

ci/no-std-check/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ edition = "2021"
55
resolver = "2"
66

77
[dependencies]
8-
ibc = { path = "../../crates/ibc", default-features = false, features = ["mocks-no-std"] }
8+
ibc = { path = "../../crates/ibc", default-features = false, features = ["serde", "mocks-no-std"] }
99
ibc-proto = { version = "0.24.1", default-features = false, features = ["parity-scale-codec", "borsh"]}
1010
tendermint = { version = "0.28.0", default-features = false }
1111
tendermint-proto = { version = "0.28.0", default-features = false }

crates/ibc/Cargo.toml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,26 @@ std = [
3939
parity-scale-codec = ["dep:parity-scale-codec", "dep:scale-info"]
4040
borsh = ["dep:borsh"]
4141

42+
# This feature is required for token transfer (ICS-20)
43+
serde = ["dep:serde", "dep:serde_derive", "serde_json", "erased-serde"]
44+
4245
# This feature guards the unfinished implementation of ADR 5.
4346
val_exec_ctx = []
4447

4548
# This feature grants access to development-time mocking libraries, such as `MockContext` or `MockHeader`.
4649
# Depends on the `testgen` suite for generating Tendermint light blocks.
4750
mocks = ["tendermint-testgen", "tendermint/clock", "cfg-if", "parking_lot"]
4851
mocks-no-std = ["cfg-if"]
49-
serde = []
5052

5153
[dependencies]
5254
# Proto definitions for all IBC-related interfaces, e.g., connections or channels.
5355
ibc-proto = { version = "0.24.1", default-features = false, features = ["parity-scale-codec", "borsh"] }
5456
ics23 = { version = "0.9.0", default-features = false, features = ["host-functions"] }
5557
time = { version = ">=0.3.0, <0.3.18", default-features = false }
56-
serde_derive = { version = "1.0.104", default-features = false }
57-
serde = { version = "1.0", default-features = false }
58-
serde_json = { version = "1", default-features = false }
59-
erased-serde = { version = "0.3", default-features = false, features = ["alloc"] }
58+
serde_derive = { version = "1.0.104", default-features = false, optional = true }
59+
serde = { version = "1.0", default-features = false, optional = true }
60+
serde_json = { version = "1", default-features = false, optional = true }
61+
erased-serde = { version = "0.3", default-features = false, features = ["alloc"], optional = true }
6062
tracing = { version = "0.1.36", default-features = false }
6163
prost = { version = "0.11", default-features = false }
6264
bytes = { version = "1.2.1", default-features = false }

crates/ibc/src/applications/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
//! Various packet encoding semantics which underpin the various types of transactions.
22
3+
#[cfg(feature = "serde")]
34
pub mod transfer;

crates/ibc/src/applications/transfer/acknowledgement.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use core::fmt::{Display, Error as FmtError, Formatter};
22

3-
use serde::{Deserialize, Serialize};
4-
53
use super::error::TokenTransferError;
64
use crate::core::ics26_routing::context::Acknowledgement as AckTrait;
75
use crate::prelude::*;
@@ -13,21 +11,23 @@ pub const ACK_ERR_STR: &str = "error handling packet on destination chain: see e
1311
/// A successful acknowledgement, equivalent to `base64::encode(0x01)`.
1412
pub const ACK_SUCCESS_B64: &str = "AQ==";
1513

16-
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
14+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
15+
#[derive(Clone, Debug, PartialEq, Eq)]
1716
pub enum ConstAckSuccess {
18-
#[serde(rename = "AQ==")]
17+
#[cfg_attr(feature = "serde", serde(rename = "AQ=="))]
1918
Success,
2019
}
2120

22-
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
21+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
22+
#[derive(Clone, Debug, PartialEq, Eq)]
2323
pub enum Acknowledgement {
2424
/// Successful Acknowledgement
2525
/// e.g. `{"result":"AQ=="}`
26-
#[serde(rename = "result")]
26+
#[cfg_attr(feature = "serde", serde(rename = "result"))]
2727
Success(ConstAckSuccess),
2828
/// Error Acknowledgement
2929
/// e.g. `{"error":"cannot unmarshal ICS-20 transfer packet data"}`
30-
#[serde(rename = "error")]
30+
#[cfg_attr(feature = "serde", serde(rename = "error"))]
3131
Error(String),
3232
}
3333

crates/ibc/src/applications/transfer/amount.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
use core::str::FromStr;
22
use derive_more::{Display, From, Into};
3-
use serde::{Deserialize, Serialize};
43

54
use super::error::TokenTransferError;
65
use primitive_types::U256;
76

87
/// A type for representing token transfer amounts.
9-
#[derive(
10-
Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize, Display, From, Into,
11-
)]
8+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9+
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Display, From, Into)]
1210
pub struct Amount(U256);
1311

1412
impl Amount {

crates/ibc/src/applications/transfer/coin.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ use core::fmt::{Display, Error as FmtError, Formatter};
22
use core::str::{from_utf8, FromStr};
33
use ibc_proto::cosmos::base::v1beta1::Coin as ProtoCoin;
44
use safe_regex::regex;
5-
use serde::{Deserialize, Serialize};
65

76
use super::amount::Amount;
87
use super::denom::{BaseDenom, PrefixedDenom};
98
use super::error::TokenTransferError;
109
use crate::prelude::*;
10+
11+
#[cfg(feature = "serde")]
1112
use crate::serializers::serde_string;
1213

1314
/// A `Coin` type with fully qualified `PrefixedDenom`.
@@ -19,12 +20,13 @@ pub type BaseCoin = Coin<BaseDenom>;
1920
pub type RawCoin = Coin<String>;
2021

2122
/// Coin defines a token with a denomination and an amount.
22-
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
23+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
24+
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
2325
pub struct Coin<D> {
2426
/// Denomination
2527
pub denom: D,
2628
/// Amount
27-
#[serde(with = "serde_string")]
29+
#[cfg_attr(feature = "serde", serde(with = "serde_string"))]
2830
pub amount: Amount,
2931
}
3032

crates/ibc/src/applications/transfer/context.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,14 @@ pub fn on_chan_open_init(
178178
});
179179
}
180180

181-
if !version.is_empty() && version != &Version::ics20() {
181+
if !version.is_empty() && version != &Version::new(VERSION.to_string()) {
182182
return Err(TokenTransferError::InvalidVersion {
183-
expect_version: Version::ics20(),
183+
expect_version: Version::new(VERSION.to_string()),
184184
got_version: version.clone(),
185185
});
186186
}
187187

188-
Ok((ModuleExtras::empty(), Version::ics20()))
188+
Ok((ModuleExtras::empty(), Version::new(VERSION.to_string())))
189189
}
190190

191191
#[allow(clippy::too_many_arguments)]
@@ -204,14 +204,14 @@ pub fn on_chan_open_try(
204204
got_order: order,
205205
});
206206
}
207-
if counterparty_version != &Version::ics20() {
207+
if counterparty_version != &Version::new(VERSION.to_string()) {
208208
return Err(TokenTransferError::InvalidCounterpartyVersion {
209-
expect_version: Version::ics20(),
209+
expect_version: Version::new(VERSION.to_string()),
210210
got_version: counterparty_version.clone(),
211211
});
212212
}
213213

214-
Ok((ModuleExtras::empty(), Version::ics20()))
214+
Ok((ModuleExtras::empty(), Version::new(VERSION.to_string())))
215215
}
216216

217217
pub fn on_chan_open_ack(
@@ -220,9 +220,9 @@ pub fn on_chan_open_ack(
220220
_channel_id: &ChannelId,
221221
counterparty_version: &Version,
222222
) -> Result<ModuleExtras, TokenTransferError> {
223-
if counterparty_version != &Version::ics20() {
223+
if counterparty_version != &Version::new(VERSION.to_string()) {
224224
return Err(TokenTransferError::InvalidCounterpartyVersion {
225-
expect_version: Version::ics20(),
225+
expect_version: Version::new(VERSION.to_string()),
226226
got_version: counterparty_version.clone(),
227227
});
228228
}
@@ -335,6 +335,7 @@ pub fn on_timeout_packet(
335335

336336
#[cfg(test)]
337337
pub(crate) mod test {
338+
use super::*;
338339
use subtle_encoding::bech32;
339340

340341
use crate::applications::transfer::context::{cosmos_adr028_escrow_address, on_chan_open_try};
@@ -347,7 +348,6 @@ pub(crate) mod test {
347348
use crate::core::ics04_channel::Version;
348349
use crate::core::ics24_host::identifier::{ChannelId, ConnectionId, PortId};
349350
use crate::handler::HandlerOutputBuilder;
350-
use crate::prelude::*;
351351
use crate::test_utils::{get_dummy_transfer_module, DummyTransferModule};
352352

353353
use super::on_chan_open_init;
@@ -436,15 +436,15 @@ pub(crate) mod test {
436436
)
437437
.unwrap();
438438

439-
assert_eq!(out_version, Version::ics20());
439+
assert_eq!(out_version, Version::new(VERSION.to_string()));
440440
}
441441

442442
/// If the relayer passed in the only supported version (ics20), then return ics20
443443
#[test]
444444
fn test_on_chan_open_init_ics20_version() {
445445
let (mut ctx, order, connection_hops, port_id, channel_id, counterparty) = get_defaults();
446446

447-
let in_version = Version::ics20();
447+
let in_version = Version::new(VERSION.to_string());
448448
let (_, out_version) = on_chan_open_init(
449449
&mut ctx,
450450
order,
@@ -456,7 +456,7 @@ pub(crate) mod test {
456456
)
457457
.unwrap();
458458

459-
assert_eq!(out_version, Version::ics20());
459+
assert_eq!(out_version, Version::new(VERSION.to_string()));
460460
}
461461

462462
/// If the relayer passed in an unsupported version, then fail
@@ -483,7 +483,7 @@ pub(crate) mod test {
483483
fn test_on_chan_open_try_counterparty_correct_version() {
484484
let (mut ctx, order, connection_hops, port_id, channel_id, counterparty) = get_defaults();
485485

486-
let counterparty_version = Version::ics20();
486+
let counterparty_version = Version::new(VERSION.to_string());
487487

488488
let (_, out_version) = on_chan_open_try(
489489
&mut ctx,
@@ -496,7 +496,7 @@ pub(crate) mod test {
496496
)
497497
.unwrap();
498498

499-
assert_eq!(out_version, Version::ics20());
499+
assert_eq!(out_version, Version::new(VERSION.to_string()));
500500
}
501501

502502
/// If the counterparty doesn't support ics20, then fail

crates/ibc/src/applications/transfer/denom.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@ use core::str::FromStr;
33

44
use derive_more::{Display, From};
55
use ibc_proto::ibc::applications::transfer::v1::DenomTrace as RawDenomTrace;
6-
use serde::{Deserialize, Serialize};
76

87
use super::error::TokenTransferError;
98
use crate::core::ics24_host::identifier::{ChannelId, PortId};
109
use crate::prelude::*;
10+
11+
#[cfg(feature = "serde")]
1112
use crate::serializers::serde_string;
1213

1314
/// Base denomination type
14-
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize, Display)]
15-
#[serde(transparent)]
15+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
16+
#[cfg_attr(feature = "serde", serde(transparent))]
17+
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Display)]
1618
pub struct BaseDenom(String);
1719

1820
impl BaseDenom {
@@ -147,10 +149,11 @@ impl Display for TracePath {
147149
}
148150

149151
/// A type that contains the base denomination for ICS20 and the source tracing information path.
150-
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
152+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
153+
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
151154
pub struct PrefixedDenom {
152155
/// A series of `{port-id}/{channel-id}`s for tracing the source of the token.
153-
#[serde(with = "serde_string")]
156+
#[cfg_attr(feature = "serde", serde(with = "serde_string"))]
154157
pub trace_path: TracePath,
155158
/// Base denomination of the relayed fungible token.
156159
pub base_denom: BaseDenom,

crates/ibc/src/applications/transfer/packet.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@ use core::convert::TryFrom;
33
use core::str::FromStr;
44

55
use ibc_proto::ibc::applications::transfer::v2::FungibleTokenPacketData as RawPacketData;
6-
use serde::{Deserialize, Serialize};
76

87
use super::error::TokenTransferError;
98
use super::{Amount, PrefixedCoin, PrefixedDenom};
109
use crate::signer::Signer;
1110

12-
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
13-
#[serde(try_from = "RawPacketData", into = "RawPacketData")]
11+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
12+
#[cfg_attr(
13+
feature = "serde",
14+
serde(try_from = "RawPacketData", into = "RawPacketData")
15+
)]
16+
#[derive(Clone, Debug, PartialEq, Eq)]
1417
pub struct PacketData {
1518
pub token: PrefixedCoin,
1619
pub sender: Signer,

0 commit comments

Comments
 (0)