Skip to content

Commit 07dd858

Browse files
authored
Fix Timestamp borsh failed and Improve parity-scale-codec Timestamp (#688)
* refactor(ibc): clean up Cargo.toml and update timestamp implementation and tests. * Refactor Timestamp's encoding to remove conditional implementation * test: add serde test for timestamp module with parity-scale-codec feature * Create 687-fix-borsh-timstamp-ser-der.md
1 parent 044a973 commit 07dd858

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- Timestamp ser and der failed on borsh feature
2+
([#687](https://github.com/cosmos/ibc-rs/issues/687))

crates/ibc/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ all-features = true
1818

1919
[features]
2020
default = ["std"]
21-
std = [
21+
std = [
2222
"ibc-proto/std",
2323
"ics23/std",
2424
"serde/std",

crates/ibc/src/core/timestamp.rs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,7 @@ impl borsh::BorshSerialize for Timestamp {
3434
&self,
3535
writer: &mut W,
3636
) -> borsh::maybestd::io::Result<()> {
37-
let timestamp = if let Some(time) = self.time {
38-
time.unix_timestamp_nanos()
39-
} else {
40-
// When the value in `Time` is `None` we give the timestamp a default value of 0
41-
0
42-
};
37+
let timestamp = self.nanoseconds();
4338
borsh::BorshSerialize::serialize(&timestamp, writer)
4439
}
4540
}
@@ -58,13 +53,7 @@ impl borsh::BorshDeserialize for Timestamp {
5853
#[cfg(feature = "parity-scale-codec")]
5954
impl parity_scale_codec::Encode for Timestamp {
6055
fn encode_to<T: parity_scale_codec::Output + ?Sized>(&self, writer: &mut T) {
61-
let timestamp = if let Some(time) = self.time {
62-
time.unix_timestamp_nanos()
63-
} else {
64-
// When the value in `Time` is `None` we give the timestamp a default value of 0
65-
0
66-
};
67-
56+
let timestamp = self.nanoseconds();
6857
timestamp.encode_to(writer);
6958
}
7059
}
@@ -389,4 +378,22 @@ mod tests {
389378
let inner = res.unwrap();
390379
assert!(inner > sleep_duration);
391380
}
381+
382+
#[test]
383+
#[cfg(feature = "borsh")]
384+
fn test_timestamp_borsh_ser_der() {
385+
use borsh::{BorshDeserialize, BorshSerialize};
386+
let timestamp = Timestamp::now();
387+
let encode_timestamp = timestamp.try_to_vec().unwrap();
388+
let _ = Timestamp::try_from_slice(&encode_timestamp).unwrap();
389+
}
390+
391+
#[test]
392+
#[cfg(feature = "parity-scale-codec")]
393+
fn test_timestamp_parity_scale_codec_ser_der() {
394+
use parity_scale_codec::{Decode, Encode};
395+
let timestamp = Timestamp::now();
396+
let encode_timestamp = timestamp.encode();
397+
let _ = Timestamp::decode(&mut encode_timestamp.as_slice()).unwrap();
398+
}
392399
}

0 commit comments

Comments
 (0)