diff --git a/firewood/src/merkle.rs b/firewood/src/merkle.rs index f567c193f..81648f568 100644 --- a/firewood/src/merkle.rs +++ b/firewood/src/merkle.rs @@ -1491,28 +1491,22 @@ mod tests { assert_eq!(encoded, new_node_encoded); } - #[test_case(leaf(Vec::new(), Vec::new()) ; "empty leaf encoding")] - #[test_case(leaf(vec![1, 2, 3], vec![4, 5]) ; "leaf encoding")] - #[test_case(branch(Some(b"value".to_vec()), vec![1, 2, 3].into()) ; "branch with chd")] - #[test_case(branch(Some(b"value".to_vec()), None); "branch without chd")] - #[test_case(branch(None, None); "branch without value and chd")] - fn node_encode_decode(node: Node) { - let merkle = create_test_merkle(); - let node_ref = merkle.put_node(node.clone()).unwrap(); - - let encoded = merkle.encode(&node_ref).unwrap(); - let new_node = Node::from(merkle.decode(encoded.as_ref()).unwrap()); - - assert_eq!(node, new_node); - } - - #[test_case(leaf(Vec::new(), Vec::new()) ; "empty leaf encoding")] - #[test_case(leaf(vec![1, 2, 3], vec![4, 5]) ; "leaf encoding")] - #[test_case(branch(Some(b"value".to_vec()), vec![1, 2, 3].into()) ; "branch with chd")] - #[test_case(branch(Some(b"value".to_vec()), Some(Vec::new())); "branch with empty chd")] - #[test_case(branch(Some(Vec::new()), vec![1, 2, 3].into()); "branch with empty value")] - fn node_encode_decode_plain(node: Node) { - let merkle = create_generic_test_merkle::(); + #[test_case(Bincode::new(), leaf(Vec::new(), Vec::new()) ; "empty leaf encoding with Bincode")] + #[test_case(Bincode::new(), leaf(vec![1, 2, 3], vec![4, 5]) ; "leaf encoding with Bincode")] + #[test_case(Bincode::new(), branch(Some(b"value".to_vec()), vec![1, 2, 3].into()) ; "branch with chd with Bincode")] + #[test_case(Bincode::new(), branch(Some(b"value".to_vec()), None); "branch without chd with Bincode")] + #[test_case(Bincode::new(), branch(None, None); "branch without value and chd with Bincode")] + #[test_case(PlainCodec::new(), leaf(Vec::new(), Vec::new()) ; "empty leaf encoding with PlainCodec")] + #[test_case(PlainCodec::new(), leaf(vec![1, 2, 3], vec![4, 5]) ; "leaf encoding with PlainCodec")] + #[test_case(PlainCodec::new(), branch(Some(b"value".to_vec()), vec![1, 2, 3].into()) ; "branch with chd with PlainCodec")] + #[test_case(PlainCodec::new(), branch(Some(b"value".to_vec()), Some(Vec::new())); "branch with empty chd with PlainCodec")] + #[test_case(PlainCodec::new(), branch(Some(Vec::new()), vec![1, 2, 3].into()); "branch with empty value with PlainCodec")] + fn node_encode_decode(_codec: T, node: Node) + where + T: BinarySerde, + for<'de> EncodedNode: serde::Serialize + serde::Deserialize<'de>, + { + let merkle = create_generic_test_merkle::(); let node_ref = merkle.put_node(node.clone()).unwrap(); let encoded = merkle.encode(&node_ref).unwrap(); diff --git a/firewood/src/merkle/node.rs b/firewood/src/merkle/node.rs index 74ac9336d..e3c672a69 100644 --- a/firewood/src/merkle/node.rs +++ b/firewood/src/merkle/node.rs @@ -722,6 +722,7 @@ pub trait BinarySerde { ) -> Result; } +#[derive(Default)] pub struct Bincode(pub bincode::DefaultOptions); impl Debug for Bincode { @@ -750,6 +751,7 @@ impl BinarySerde for Bincode { } } +#[derive(Default)] pub struct PlainCodec(pub bincode::DefaultOptions); impl Debug for PlainCodec {