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 3 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
10 changes: 6 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ 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" }
nmt-rs = { path = ".", features = ["borsh", "serde"]}
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"]
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: std::io::Read>(reader: &mut R) -> std::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
27 changes: 26 additions & 1 deletion src/simple_merkle/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -79,6 +79,31 @@ 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
+ 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;

Expand Down
Loading