Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade borsh to 1.0 #27

Merged
merged 10 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
9 changes: 2 additions & 7 deletions src/namespaced_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,7 @@ pub struct NamespacedHash<const NS_ID_SIZE: usize> {

#[cfg(any(test, feature = "borsh"))]
impl<const NS_ID_SIZE: usize> borsh::BorshDeserialize for NamespacedHash<NS_ID_SIZE> {
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 mut min_ns = NamespaceId([0u8; NS_ID_SIZE]);
reader.read_exact(&mut min_ns.0)?;

Expand Down Expand Up @@ -399,15 +397,12 @@ impl<const NS_ID_SIZE: usize> TryFrom<&[u8]> for NamespacedHash<NS_ID_SIZE> {
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");
Expand Down
76 changes: 64 additions & 12 deletions src/simple_merkle/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,53 @@ impl<Db: PreimageDb<<M as MerkleHash>::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
Expand All @@ -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;
}

Expand Down