From a13b33cc87cadf73c07e7f7eaafafe1143a4d05c Mon Sep 17 00:00:00 2001 From: Nikolai Golub Date: Sat, 29 Jun 2024 11:08:25 +0200 Subject: [PATCH] Upgrade borsh to 1.0 (#27) --- Cargo.toml | 25 +++++++++---- src/namespaced_hash.rs | 9 ++--- src/simple_merkle/tree.rs | 76 ++++++++++++++++++++++++++++++++------- 3 files changed, 84 insertions(+), 26 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7787c6f..c46fdf5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,16 +13,27 @@ repository = "https://github.com/sovereign-labs/nmt-rs" [dependencies] sha2 = { version = "0.10.6", default-features = false } bytes = { version = "1", default-features = false } -serde = { version = "1", optional = true, features = ["derive"] } -borsh = { version = "0.10.0", optional = true } +serde = { version = "1", default-features = false, optional = true, features = ["derive"] } +borsh = { version = "1", default-features = false, features = ["derive"], optional = true } [dev-dependencies] -borsh = { version = "0.10.0" } +nmt-rs = { path = ".", features = ["borsh", "serde"] } +borsh = { version = "1" } serde_json = "1.0.96" -postcard = { version = "1.0.4" } -tendermint = {version = "0.35.0"} +postcard = { version = "1.0.4", features = ["use-std"] } +tendermint = { version = "0.35.0" } [features] default = ["std"] -serde = ["dep:serde", "postcard/use-std"] -std = [] +borsh = ["dep:borsh"] +serde = [ + "dep:serde", + "bytes/serde", + "nmt-rs/serde" +] +std = [ + "borsh?/std", + "serde?/std", + "bytes/std", + "sha2/std" +] diff --git a/src/namespaced_hash.rs b/src/namespaced_hash.rs index 78b0ef2..3b31d6c 100644 --- a/src/namespaced_hash.rs +++ b/src/namespaced_hash.rs @@ -197,9 +197,7 @@ pub struct NamespacedHash { #[cfg(any(test, feature = "borsh"))] impl borsh::BorshDeserialize for NamespacedHash { - fn deserialize_reader( - reader: &mut R, - ) -> borsh::maybestd::io::Result { + fn deserialize_reader(reader: &mut R) -> borsh::io::Result { let mut min_ns = NamespaceId([0u8; NS_ID_SIZE]); reader.read_exact(&mut min_ns.0)?; @@ -399,15 +397,12 @@ impl TryFrom<&[u8]> for NamespacedHash { mod tests { use crate::NamespacedHash; use borsh::de::BorshDeserialize; - use borsh::ser::BorshSerialize; #[test] fn test_namespaced_hash_borsh() { let hash = NamespacedHash::<8>::try_from([8u8; 48].as_ref()).unwrap(); - let serialized = hash - .try_to_vec() - .expect("Serialization to vec must succeed"); + let serialized = borsh::to_vec(&hash).expect("Serialization to vec must succeed"); let got = NamespacedHash::deserialize(&mut &serialized[..]).expect("serialized hash is correct"); diff --git a/src/simple_merkle/tree.rs b/src/simple_merkle/tree.rs index 6240fcb..de97e46 100644 --- a/src/simple_merkle/tree.rs +++ b/src/simple_merkle/tree.rs @@ -48,27 +48,53 @@ impl::Output>, M: MerkleHash + Default> Default /// A trait for hashing data into a merkle tree pub trait MerkleHash { - /// The output of this hasher - #[cfg(all(not(feature = "serde"), feature = "std"))] + // --- no-std + /// The output of this hasher. + #[cfg(all(not(feature = "serde"), not(feature = "borsh"), not(feature = "std")))] type Output: Debug + PartialEq + Eq + Clone + Default + Hash; - /// The output of this hasher - #[cfg(all(not(feature = "serde"), not(feature = "std")))] - type Output: Debug + PartialEq + Eq + Clone + Default + Hash + Ord; + /// The output of this hasher. + #[cfg(all(feature = "serde", not(feature = "borsh"), not(feature = "std")))] + type Output: Debug + + PartialEq + + Eq + + Clone + + Default + + Hash + + serde::Serialize + + serde::de::DeserializeOwned; - /// The output of this hasher - #[cfg(all(feature = "serde", not(feature = "std")))] + /// The output of this hasher. + #[cfg(all(feature = "borsh", not(feature = "serde"), not(feature = "std")))] type Output: Debug + PartialEq + Eq + Clone + Default + Hash + + borsh::BorshSerialize + + borsh::BorshDeserialize; + + /// The output of this hasher. + #[cfg(all(feature = "borsh", feature = "serde", not(feature = "std")))] + type Output: Debug + + PartialEq + + Eq + + Clone + + Default + + Hash + + borsh::BorshSerialize + + borsh::BorshDeserialize + serde::Serialize + serde::de::DeserializeOwned; - /// The output of this hasher - #[cfg(all(feature = "serde", feature = "std"))] + // --- std + /// The output of this hasher. + #[cfg(all(not(feature = "serde"), not(feature = "borsh"), feature = "std"))] + type Output: Debug + PartialEq + Eq + Clone + Default + Hash + Ord; + + /// The output of this hasher. + #[cfg(all(feature = "serde", not(feature = "borsh"), feature = "std"))] type Output: Debug + PartialEq + Eq @@ -79,12 +105,38 @@ pub trait MerkleHash { + serde::Serialize + serde::de::DeserializeOwned; - /// The hash of the empty tree. This is often defined as the hash of the empty string + /// The output of this hasher. + #[cfg(all(not(feature = "serde"), feature = "borsh", feature = "std"))] + type Output: Debug + + PartialEq + + Eq + + Clone + + Default + + Hash + + Ord + + borsh::BorshSerialize + + borsh::BorshDeserialize; + + /// The output of this hasher. + #[cfg(all(feature = "serde", feature = "borsh", feature = "std"))] + type Output: Debug + + PartialEq + + Eq + + Clone + + Default + + Hash + + Ord + + serde::Serialize + + serde::de::DeserializeOwned + + borsh::BorshSerialize + + borsh::BorshDeserialize; + + /// The hash of the empty tree. This is often defined as the hash of the empty string. const EMPTY_ROOT: Self::Output; - /// Hashes data as a "leaf" of the tree. This operation *should* be domain separated + /// Hashes data as a "leaf" of the tree. This operation *should* be domain separated. fn hash_leaf(&self, data: &[u8]) -> Self::Output; - /// Hashes two digests into one. This operation *should* be domain separated + /// Hashes two digests into one. This operation *should* be domain separated. fn hash_nodes(&self, l: &Self::Output, r: &Self::Output) -> Self::Output; }