Skip to content

Commit

Permalink
Bump borsh to v1 (#1245)
Browse files Browse the repository at this point in the history
* Remove `borsh::maybestd`

* Add #[borsh(use_discriminant)] attributes

* Undo toml formatting

* Update cw-check Cargo.lock

* Fix clippy warnings

* Switch to using borsh::from_slice
  • Loading branch information
seanchen1991 authored May 31, 2024
1 parent dcf8c36 commit 5779e86
Show file tree
Hide file tree
Showing 15 changed files with 91 additions and 133 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ authors = [ "Informal Systems <[email protected]>" ]
[workspace.dependencies]
# external dependencies
base64 = { version = "0.21", default-features = false }
borsh = { version = "0.10", default-features = false }
borsh = { version = "1", default-features = false, features = [ "derive" ] }
displaydoc = { version = "0.2", default-features = false }
prost = { version = "0.12", default-features = false }
derive_more = { version = "0.99.17", default-features = false, features = [ "from", "into", "display", "try_into" ] }
Expand Down Expand Up @@ -106,7 +106,7 @@ ibc-client-wasm-types = { version = "0.53.0", path = "./ibc-clients/ics08-
ibc-app-transfer-types = { version = "0.53.0", path = "./ibc-apps/ics20-transfer/types", default-features = false }
ibc-app-nft-transfer-types = { version = "0.53.0", path = "./ibc-apps/ics721-nft-transfer/types", default-features = false }

ibc-proto = { version = "0.44.0", default-features = false }
ibc-proto = { version = "0.45.0", default-features = false }

# cosmos dependencies
tendermint = { version = "0.36.0", default-features = false }
Expand Down
94 changes: 47 additions & 47 deletions ci/cw-check/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ci/no-std-check/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ resolver = "2"

[dependencies]
ibc = { path = "../../ibc", default-features = false, features = [ "serde" ] }
ibc-proto = { version = "0.44", default-features = false, features = [
ibc-proto = { version = "0.45", default-features = false, features = [
"parity-scale-codec",
"borsh",
"serde",
Expand Down
17 changes: 5 additions & 12 deletions ibc-apps/ics20-transfer/types/src/amount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ impl parity_scale_codec::WrapperTypeEncode for Amount {}

#[cfg(feature = "borsh")]
impl borsh::BorshSerialize for Amount {
fn serialize<W: borsh::maybestd::io::Write>(
&self,
writer: &mut W,
) -> borsh::maybestd::io::Result<()> {
fn serialize<W: borsh::io::Write>(&self, writer: &mut W) -> borsh::io::Result<()> {
// Note: a "word" is 8 bytes (i.e. a u64)
let words = self.as_slice();
let bytes: Vec<u8> = words.iter().flat_map(|word| word.to_be_bytes()).collect();
Expand All @@ -44,17 +41,15 @@ impl borsh::BorshSerialize for Amount {
}
#[cfg(feature = "borsh")]
impl borsh::BorshDeserialize for Amount {
fn deserialize_reader<R: borsh::maybestd::io::Read>(
reader: &mut R,
) -> borsh::maybestd::io::Result<Self> {
fn deserialize_reader<R: borsh::io::Read>(reader: &mut R) -> borsh::io::Result<Self> {
const NUM_BYTES_IN_U64: usize = 8;
const NUM_WORDS_IN_U256: usize = 4;

let mut buf = [0; 32];
let bytes_read = reader.read(&mut buf)?;
if bytes_read != 32 {
return Err(borsh::maybestd::io::Error::new(
borsh::maybestd::io::ErrorKind::InvalidInput,
return Err(borsh::io::Error::new(
borsh::io::ErrorKind::InvalidInput,
format!("Expected to read 32 bytes, read {bytes_read}"),
));
}
Expand Down Expand Up @@ -150,15 +145,13 @@ mod tests {
#[cfg(feature = "borsh")]
#[test]
fn borsh_amount() {
use borsh::BorshDeserialize;

let value = Amount::from(42);
let serialized = borsh::to_vec(&value).unwrap();

// Amount is supposed to be a U256 according to the spec, which is 32 bytes
assert_eq!(serialized.len(), 32);

let value_deserialized = Amount::try_from_slice(&serialized).unwrap();
let value_deserialized = borsh::from_slice::<Amount>(&serialized).unwrap();

assert_eq!(value, value_deserialized);
}
Expand Down
17 changes: 5 additions & 12 deletions ibc-apps/ics721-nft-transfer/types/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,21 +192,16 @@ pub struct ClassUri(

#[cfg(feature = "borsh")]
impl borsh::BorshSerialize for ClassUri {
fn serialize<W: borsh::maybestd::io::Write>(
&self,
writer: &mut W,
) -> borsh::maybestd::io::Result<()> {
fn serialize<W: borsh::io::Write>(&self, writer: &mut W) -> borsh::io::Result<()> {
borsh::BorshSerialize::serialize(&self.to_string(), writer)
}
}

#[cfg(feature = "borsh")]
impl borsh::BorshDeserialize for ClassUri {
fn deserialize_reader<R: borsh::maybestd::io::Read>(
reader: &mut R,
) -> borsh::maybestd::io::Result<Self> {
fn deserialize_reader<R: borsh::io::Read>(reader: &mut R) -> borsh::io::Result<Self> {
let uri = String::deserialize_reader(reader)?;
Ok(ClassUri::from_str(&uri).map_err(|_| borsh::maybestd::io::ErrorKind::Other)?)
Ok(ClassUri::from_str(&uri).map_err(|_| borsh::io::ErrorKind::Other)?)
}
}

Expand Down Expand Up @@ -447,10 +442,8 @@ mod tests {
#[test]
fn test_borsh_roundtrip() {
fn borsh_roundtrip(class_uri: ClassUri) {
use borsh::{BorshDeserialize, BorshSerialize};

let class_uri_bytes = class_uri.try_to_vec().unwrap();
let res = ClassUri::try_from_slice(&class_uri_bytes).unwrap();
let class_uri_bytes = borsh::to_vec(&class_uri).unwrap();
let res = borsh::from_slice::<ClassUri>(&class_uri_bytes).unwrap();

assert_eq!(class_uri, res);
}
Expand Down
Loading

0 comments on commit 5779e86

Please sign in to comment.