From da215e17ed4a54ef6f3401a827403e0f088020a0 Mon Sep 17 00:00:00 2001 From: Nikolai Golub Date: Fri, 28 Jun 2024 14:10:36 +0200 Subject: [PATCH 01/10] Upgrade borsh to 1.0 --- Cargo.toml | 9 +++++---- src/namespaced_hash.rs | 4 ++-- src/simple_merkle/tree.rs | 16 +++++++++++++++- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7787c6f..033a3cc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,15 +14,16 @@ repository = "https://github.com/sovereign-labs/nmt-rs" 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 } +borsh = { version = "1", features = ["derive"], optional = true } [dev-dependencies] -borsh = { version = "0.10.0" } +borsh = { version = "1" } serde_json = "1.0.96" postcard = { version = "1.0.4" } -tendermint = {version = "0.35.0"} +tendermint = { version = "0.35.0" } [features] default = ["std"] -serde = ["dep:serde", "postcard/use-std"] std = [] +serde = ["dep:serde", "postcard/use-std"] +borsh = ["dep:borsh", "std"] diff --git a/src/namespaced_hash.rs b/src/namespaced_hash.rs index 78b0ef2..d1832d5 100644 --- a/src/namespaced_hash.rs +++ b/src/namespaced_hash.rs @@ -197,9 +197,9 @@ pub struct NamespacedHash { #[cfg(any(test, feature = "borsh"))] impl borsh::BorshDeserialize for NamespacedHash { - fn deserialize_reader( + fn deserialize_reader( reader: &mut R, - ) -> borsh::maybestd::io::Result { + ) -> std::io::Result { let mut min_ns = NamespaceId([0u8; NS_ID_SIZE]); reader.read_exact(&mut min_ns.0)?; diff --git a/src/simple_merkle/tree.rs b/src/simple_merkle/tree.rs index 6240fcb..24b6e32 100644 --- a/src/simple_merkle/tree.rs +++ b/src/simple_merkle/tree.rs @@ -68,7 +68,7 @@ pub trait MerkleHash { + serde::de::DeserializeOwned; /// The output of this hasher - #[cfg(all(feature = "serde", feature = "std"))] + #[cfg(all(feature = "serde", not(feature = "borsh"), feature = "std"))] type Output: Debug + PartialEq + Eq @@ -79,6 +79,20 @@ pub trait MerkleHash { + serde::Serialize + serde::de::DeserializeOwned; + /// 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; From cfd9ea2c58a43c6848e7c01d72c70c087b631433 Mon Sep 17 00:00:00 2001 From: Nikolai Golub Date: Fri, 28 Jun 2024 14:15:55 +0200 Subject: [PATCH 02/10] Fix tests --- Cargo.toml | 1 + src/namespaced_hash.rs | 4 +--- src/simple_merkle/tree.rs | 11 +++++++++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 033a3cc..25668d3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ serde = { version = "1", optional = true, features = ["derive"] } borsh = { version = "1", features = ["derive"], optional = true } [dev-dependencies] +nmt-rs = { path = ".", features = ["borsh", "serde"]} borsh = { version = "1" } serde_json = "1.0.96" postcard = { version = "1.0.4" } diff --git a/src/namespaced_hash.rs b/src/namespaced_hash.rs index d1832d5..f9e1df5 100644 --- a/src/namespaced_hash.rs +++ b/src/namespaced_hash.rs @@ -399,14 +399,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() + let serialized = borsh::to_vec(&hash) .expect("Serialization to vec must succeed"); let got = diff --git a/src/simple_merkle/tree.rs b/src/simple_merkle/tree.rs index 24b6e32..07aa2a1 100644 --- a/src/simple_merkle/tree.rs +++ b/src/simple_merkle/tree.rs @@ -79,6 +79,17 @@ pub trait MerkleHash { + serde::Serialize + serde::de::DeserializeOwned; + #[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 From 717b4a1239a25e5217fa858f9e1fa994b371779d Mon Sep 17 00:00:00 2001 From: Nikolai Golub Date: Fri, 28 Jun 2024 14:21:00 +0200 Subject: [PATCH 03/10] Fixing formatting --- src/namespaced_hash.rs | 7 ++----- src/simple_merkle/tree.rs | 36 ++++++++++++++++++------------------ 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/src/namespaced_hash.rs b/src/namespaced_hash.rs index f9e1df5..c504c2e 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, - ) -> std::io::Result { + fn deserialize_reader(reader: &mut R) -> std::io::Result { let mut min_ns = NamespaceId([0u8; NS_ID_SIZE]); reader.read_exact(&mut min_ns.0)?; @@ -404,8 +402,7 @@ mod tests { fn test_namespaced_hash_borsh() { let hash = NamespacedHash::<8>::try_from([8u8; 48].as_ref()).unwrap(); - let serialized = borsh::to_vec(&hash) - .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 07aa2a1..f30e99a 100644 --- a/src/simple_merkle/tree.rs +++ b/src/simple_merkle/tree.rs @@ -81,28 +81,28 @@ pub trait MerkleHash { #[cfg(all(not(feature = "serde"), feature = "borsh", feature = "std"))] type Output: Debug - + PartialEq - + Eq - + Clone - + Default - + Hash - + Ord - + borsh::BorshSerialize - + borsh::BorshDeserialize; + + 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; + + 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; From 13904906a7bcebf21ec0609e74be96cb0f2cf8e6 Mon Sep 17 00:00:00 2001 From: Nikolai Golub Date: Fri, 28 Jun 2024 20:28:56 +0200 Subject: [PATCH 04/10] Moving in the some direction --- Cargo.toml | 19 ++++++++++++------- src/namespaced_hash.rs | 2 +- src/simple_merkle/tree.rs | 26 ++++++++++++++++++++------ 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 25668d3..5bfcfc0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,18 +13,23 @@ 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 = "1", features = ["derive"], optional = true } +serde = { version = "1", default-features = false, optional = true, features = ["derive"] } +borsh = { version = "1", default-features = false, features = ["derive"], optional = true } [dev-dependencies] -nmt-rs = { path = ".", features = ["borsh", "serde"]} +nmt-rs = { path = ".", features = ["borsh", "serde"] } borsh = { version = "1" } serde_json = "1.0.96" -postcard = { version = "1.0.4" } +postcard = { version = "1.0.4", features = ["use-std"] } tendermint = { version = "0.35.0" } [features] default = ["std"] -std = [] -serde = ["dep:serde", "postcard/use-std"] -borsh = ["dep:borsh", "std"] +borsh = ["dep:borsh"] +serde = ["dep:serde"] +std = [ + "borsh/std", + "serde/std", +] + + diff --git a/src/namespaced_hash.rs b/src/namespaced_hash.rs index c504c2e..3b31d6c 100644 --- a/src/namespaced_hash.rs +++ b/src/namespaced_hash.rs @@ -197,7 +197,7 @@ pub struct NamespacedHash { #[cfg(any(test, feature = "borsh"))] impl borsh::BorshDeserialize for NamespacedHash { - fn deserialize_reader(reader: &mut R) -> std::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)?; diff --git a/src/simple_merkle/tree.rs b/src/simple_merkle/tree.rs index f30e99a..4e9661e 100644 --- a/src/simple_merkle/tree.rs +++ b/src/simple_merkle/tree.rs @@ -48,16 +48,14 @@ impl::Output>, M: MerkleHash + Default> Default /// A trait for hashing data into a merkle tree pub trait MerkleHash { + // --- no-std /// The output of this hasher - #[cfg(all(not(feature = "serde"), feature = "std"))] - type Output: Debug + PartialEq + Eq + Clone + Default + Hash; - - /// The output of this hasher - #[cfg(all(not(feature = "serde"), not(feature = "std")))] + #[cfg(all(not(feature = "serde"), not(feautre = "borsh"), not(feature = "std")))] + // WHY ORD HERE BUT NOT with serde? type Output: Debug + PartialEq + Eq + Clone + Default + Hash + Ord; /// The output of this hasher - #[cfg(all(feature = "serde", not(feature = "std")))] + #[cfg(all(feature = "serde", not(feature = "borsh"), not(feature = "std")))] type Output: Debug + PartialEq + Eq @@ -67,6 +65,22 @@ pub trait MerkleHash { + serde::Serialize + serde::de::DeserializeOwned; + /// 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; + + // --- std + /// The output of this hasher + #[cfg(all(not(feature = "serde"), not(feature = "borsh"), feature = "std"))] + type Output: Debug + PartialEq + Eq + Clone + Default + Hash; + /// The output of this hasher #[cfg(all(feature = "serde", not(feature = "borsh"), feature = "std"))] type Output: Debug From d92514eb91bc071f4d2e7996fd5e4a9060acbcc5 Mon Sep 17 00:00:00 2001 From: Nikolai Golub Date: Fri, 28 Jun 2024 20:31:38 +0200 Subject: [PATCH 05/10] Fixing another typo --- src/simple_merkle/tree.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/simple_merkle/tree.rs b/src/simple_merkle/tree.rs index 4e9661e..073ddc3 100644 --- a/src/simple_merkle/tree.rs +++ b/src/simple_merkle/tree.rs @@ -50,7 +50,7 @@ impl::Output>, M: MerkleHash + Default> Default pub trait MerkleHash { // --- no-std /// The output of this hasher - #[cfg(all(not(feature = "serde"), not(feautre = "borsh"), not(feature = "std")))] + #[cfg(all(not(feature = "serde"), not(feature = "borsh"), not(feature = "std")))] // WHY ORD HERE BUT NOT with serde? type Output: Debug + PartialEq + Eq + Clone + Default + Hash + Ord; From 1af1ab8e3a41ca99e209b73e05bd7a4e17a45c28 Mon Sep 17 00:00:00 2001 From: Nikolai Golub Date: Fri, 28 Jun 2024 20:38:08 +0200 Subject: [PATCH 06/10] Adding ord everywhere --- src/simple_merkle/tree.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/simple_merkle/tree.rs b/src/simple_merkle/tree.rs index 073ddc3..c55f93f 100644 --- a/src/simple_merkle/tree.rs +++ b/src/simple_merkle/tree.rs @@ -51,7 +51,6 @@ pub trait MerkleHash { // --- no-std /// The output of this hasher #[cfg(all(not(feature = "serde"), not(feature = "borsh"), not(feature = "std")))] - // WHY ORD HERE BUT NOT with serde? type Output: Debug + PartialEq + Eq + Clone + Default + Hash + Ord; /// The output of this hasher @@ -62,6 +61,7 @@ pub trait MerkleHash { + Clone + Default + Hash + + Ord + serde::Serialize + serde::de::DeserializeOwned; @@ -73,13 +73,14 @@ pub trait MerkleHash { + Clone + Default + Hash + + Ord + borsh::BorshSerialize + borsh::BorshDeserialize; // --- std /// The output of this hasher #[cfg(all(not(feature = "serde"), not(feature = "borsh"), feature = "std"))] - type Output: Debug + PartialEq + Eq + Clone + Default + Hash; + type Output: Debug + PartialEq + Eq + Clone + Default + Hash + Ord; /// The output of this hasher #[cfg(all(feature = "serde", not(feature = "borsh"), feature = "std"))] From 8ebe7a3459fffb11311bf177267bbbbc88f72efa Mon Sep 17 00:00:00 2001 From: Nikolai Golub Date: Fri, 28 Jun 2024 20:44:26 +0200 Subject: [PATCH 07/10] no_std == no Ord --- src/simple_merkle/tree.rs | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/simple_merkle/tree.rs b/src/simple_merkle/tree.rs index c55f93f..3b99f52 100644 --- a/src/simple_merkle/tree.rs +++ b/src/simple_merkle/tree.rs @@ -49,11 +49,11 @@ impl::Output>, M: MerkleHash + Default> Default /// A trait for hashing data into a merkle tree pub trait MerkleHash { // --- no-std - /// The output of this hasher + /// The output of this hasher. #[cfg(all(not(feature = "serde"), not(feature = "borsh"), not(feature = "std")))] - type Output: Debug + PartialEq + Eq + Clone + Default + Hash + Ord; + type Output: Debug + PartialEq + Eq + Clone + Default + Hash; - /// The output of this hasher + /// The output of this hasher. #[cfg(all(feature = "serde", not(feature = "borsh"), not(feature = "std")))] type Output: Debug + PartialEq @@ -61,11 +61,10 @@ pub trait MerkleHash { + Clone + Default + Hash - + Ord + serde::Serialize + serde::de::DeserializeOwned; - /// The output of this hasher + /// The output of this hasher. #[cfg(all(feature = "borsh", not(feature = "serde"), not(feature = "std")))] type Output: Debug + PartialEq @@ -73,16 +72,15 @@ pub trait MerkleHash { + Clone + Default + Hash - + Ord + borsh::BorshSerialize + borsh::BorshDeserialize; // --- std - /// The output of this hasher + /// 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 + /// The output of this hasher. #[cfg(all(feature = "serde", not(feature = "borsh"), feature = "std"))] type Output: Debug + PartialEq @@ -94,6 +92,7 @@ pub trait MerkleHash { + serde::Serialize + serde::de::DeserializeOwned; + /// The output of this hasher. #[cfg(all(not(feature = "serde"), feature = "borsh", feature = "std"))] type Output: Debug + PartialEq @@ -105,7 +104,7 @@ pub trait MerkleHash { + borsh::BorshSerialize + borsh::BorshDeserialize; - /// The output of this hasher + /// The output of this hasher. #[cfg(all(feature = "serde", feature = "borsh", feature = "std"))] type Output: Debug + PartialEq @@ -119,12 +118,12 @@ pub trait MerkleHash { + borsh::BorshSerialize + borsh::BorshDeserialize; - /// The hash of the empty tree. This is often defined as the hash of the empty string + /// 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; } From 1dbb8cefa89031577b98fca9b12d6e1c9e996cdc Mon Sep 17 00:00:00 2001 From: Nikolai Golub Date: Fri, 28 Jun 2024 20:46:50 +0200 Subject: [PATCH 08/10] Remove empty lines --- Cargo.toml | 2 -- 1 file changed, 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5bfcfc0..f278e11 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,5 +31,3 @@ std = [ "borsh/std", "serde/std", ] - - From 7fcb0dcba9dc788fc19851587b8e3dcee2ad92fa Mon Sep 17 00:00:00 2001 From: Nikolai Golub Date: Fri, 28 Jun 2024 20:49:43 +0200 Subject: [PATCH 09/10] Question mark + zepter --- Cargo.toml | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f278e11..c46fdf5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,8 +26,14 @@ tendermint = { version = "0.35.0" } [features] default = ["std"] borsh = ["dep:borsh"] -serde = ["dep:serde"] +serde = [ + "dep:serde", + "bytes/serde", + "nmt-rs/serde" +] std = [ - "borsh/std", - "serde/std", + "borsh?/std", + "serde?/std", + "bytes/std", + "sha2/std" ] From c5f3546b7806f060947eff4b2e12033b9bf888f2 Mon Sep 17 00:00:00 2001 From: Nikolai Golub Date: Fri, 28 Jun 2024 20:54:20 +0200 Subject: [PATCH 10/10] Fixed it! --- src/simple_merkle/tree.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/simple_merkle/tree.rs b/src/simple_merkle/tree.rs index 3b99f52..de97e46 100644 --- a/src/simple_merkle/tree.rs +++ b/src/simple_merkle/tree.rs @@ -75,6 +75,19 @@ pub trait MerkleHash { + 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; + // --- std /// The output of this hasher. #[cfg(all(not(feature = "serde"), not(feature = "borsh"), feature = "std"))]