From d6f0490993bcf88f786c5271091aa9a84ff2fe69 Mon Sep 17 00:00:00 2001 From: Charlie Little Date: Mon, 21 Oct 2024 19:44:16 -0500 Subject: [PATCH 1/5] Support u16::MAX length keys --- src/proofs/encoding.rs | 10 +++++----- src/tree/kv.rs | 6 +++++- src/tree/link.rs | 10 ++++++++-- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/proofs/encoding.rs b/src/proofs/encoding.rs index 9f33f245..aa8a3dcd 100644 --- a/src/proofs/encoding.rs +++ b/src/proofs/encoding.rs @@ -18,10 +18,10 @@ impl Encode for Op { dest.write_all(kv_hash)?; } Op::Push(Node::KV(key, value)) => { - debug_assert!(key.len() < 256); + debug_assert!(key.len() < 65536); debug_assert!(value.len() < 65536); - - dest.write_all(&[0x03, key.len() as u8])?; + dest.write_all(&[0x03])?; + (key.len() as u16).encode_into(dest)?; dest.write_all(key)?; (value.len() as u16).encode_into(dest)?; dest.write_all(value)?; @@ -36,7 +36,7 @@ impl Encode for Op { Ok(match self { Op::Push(Node::Hash(_)) => 1 + HASH_LENGTH, Op::Push(Node::KVHash(_)) => 1 + HASH_LENGTH, - Op::Push(Node::KV(key, value)) => 4 + key.len() + value.len(), + Op::Push(Node::KV(key, value)) => 5 + key.len() + value.len(), Op::Parent => 1, Op::Child => 1, }) @@ -59,7 +59,7 @@ impl Decode for Op { Op::Push(Node::KVHash(hash)) } 0x03 => { - let key_len: u8 = Decode::decode(&mut input)?; + let key_len: u16 = Decode::decode(&mut input)?; let mut key = vec![0; key_len as usize]; input.read_exact(key.as_mut_slice())?; diff --git a/src/tree/kv.rs b/src/tree/kv.rs index 94fef4e0..213cb7f5 100644 --- a/src/tree/kv.rs +++ b/src/tree/kv.rs @@ -75,7 +75,11 @@ impl Encode for KV { #[inline] fn encoding_length(&self) -> Result { - debug_assert!(self.key().len() < 256, "Key length must be less than 256"); + debug_assert!( + self.key().len() < 65535, + "Key length must be less than 65535" + ); + Ok(HASH_LENGTH + self.value.len()) } } diff --git a/src/tree/link.rs b/src/tree/link.rs index 67c972c0..42aee4c7 100644 --- a/src/tree/link.rs +++ b/src/tree/link.rs @@ -221,7 +221,10 @@ impl Encode for Link { Link::Modified { .. } => panic!("No encoding for Link::Modified"), }; - debug_assert!(key.len() < 256, "Key length must be less than 256"); + debug_assert!( + self.key().len() < 65535, + "Key length must be less than 65535" + ); out.write_all(&[key.len() as u8])?; out.write_all(key)?; @@ -235,7 +238,10 @@ impl Encode for Link { #[inline] fn encoding_length(&self) -> Result { - debug_assert!(self.key().len() < 256, "Key length must be less than 256"); + debug_assert!( + self.key().len() < 65535, + "Key length must be less than 65535" + ); Ok(match self { Link::Reference { key, .. } => 1 + key.len() + 32 + 2, From eb64d322328e7fd0e21a905640f5f5fd0dabab6e Mon Sep 17 00:00:00 2001 From: Charlie Little Date: Mon, 21 Oct 2024 20:03:02 -0500 Subject: [PATCH 2/5] Update tests --- src/merk/chunks.rs | 49 +++++++++++++++++++++-------------------- src/proofs/encoding.rs | 8 +++---- src/proofs/query/mod.rs | 10 ++++----- src/tree/link.rs | 2 +- 4 files changed, 35 insertions(+), 34 deletions(-) diff --git a/src/merk/chunks.rs b/src/merk/chunks.rs index effcc0ec..a8b6f555 100644 --- a/src/merk/chunks.rs +++ b/src/merk/chunks.rs @@ -311,70 +311,71 @@ mod tests { assert_eq!( chunk, vec![ - 3, 8, 0, 0, 0, 0, 0, 0, 0, 18, 0, 60, 123, 123, 123, 123, 123, 123, 123, 123, 123, + 3, 0, 8, 0, 0, 0, 0, 0, 0, 0, 18, 0, 60, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, - 123, 123, 123, 3, 8, 0, 0, 0, 0, 0, 0, 0, 19, 0, 60, 123, 123, 123, 123, 123, 123, + 123, 123, 123, 123, 3, 0, 8, 0, 0, 0, 0, 0, 0, 0, 19, 0, 60, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, - 123, 123, 123, 123, 123, 123, 16, 3, 8, 0, 0, 0, 0, 0, 0, 0, 20, 0, 60, 123, 123, - 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, + 123, 123, 123, 123, 123, 123, 123, 123, 16, 3, 0, 8, 0, 0, 0, 0, 0, 0, 0, 20, 0, + 60, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, - 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 17, 3, 8, 0, 0, 0, 0, 0, 0, 0, - 21, 0, 60, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, + 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 17, 3, 0, 8, 0, 0, 0, + 0, 0, 0, 0, 21, 0, 60, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, - 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 16, 3, 8, 0, - 0, 0, 0, 0, 0, 0, 22, 0, 60, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, + 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 16, + 3, 0, 8, 0, 0, 0, 0, 0, 0, 0, 22, 0, 60, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, - 123, 3, 8, 0, 0, 0, 0, 0, 0, 0, 23, 0, 60, 123, 123, 123, 123, 123, 123, 123, 123, + 123, 123, 123, 123, 3, 0, 8, 0, 0, 0, 0, 0, 0, 0, 23, 0, 60, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, - 123, 123, 123, 123, 16, 3, 8, 0, 0, 0, 0, 0, 0, 0, 24, 0, 60, 123, 123, 123, 123, + 123, 123, 123, 123, 123, 123, 123, 123, 16, 3, 0, 8, 0, 0, 0, 0, 0, 0, 0, 24, 0, + 60, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, + 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 17, 17, 3, 0, 8, 0, 0, + 0, 0, 0, 0, 0, 25, 0, 60, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, - 123, 123, 123, 123, 123, 123, 123, 123, 17, 17, 3, 8, 0, 0, 0, 0, 0, 0, 0, 25, 0, - 60, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, - 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 16, 3, 8, 0, 0, 0, 0, - 0, 0, 0, 26, 0, 60, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, + 123, 16, 3, 0, 8, 0, 0, 0, 0, 0, 0, 0, 26, 0, 60, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, - 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 3, - 8, 0, 0, 0, 0, 0, 0, 0, 27, 0, 60, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, + 123, 123, 123, 123, 123, 123, 3, 0, 8, 0, 0, 0, 0, 0, 0, 0, 27, 0, 60, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, - 123, 123, 123, 16, 3, 8, 0, 0, 0, 0, 0, 0, 0, 28, 0, 60, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, + 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 16, 3, 0, 8, 0, 0, 0, 0, 0, 0, 0, + 28, 0, 60, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, - 123, 123, 123, 123, 123, 123, 123, 17, 3, 8, 0, 0, 0, 0, 0, 0, 0, 29, 0, 60, 123, + 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 17, 3, 0, 8, + 0, 0, 0, 0, 0, 0, 0, 29, 0, 60, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, - 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 16, 3, 8, 0, 0, 0, 0, 0, 0, - 0, 30, 0, 60, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, + 123, 123, 16, 3, 0, 8, 0, 0, 0, 0, 0, 0, 0, 30, 0, 60, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, - 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 3, 8, 0, 0, - 0, 0, 0, 0, 0, 31, 0, 60, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, + 123, 123, 123, 123, 123, 123, 123, 3, 0, 8, 0, 0, 0, 0, 0, 0, 0, 31, 0, 60, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, - 123, 16, 3, 8, 0, 0, 0, 0, 0, 0, 0, 32, 0, 60, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, + 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 16, 3, 0, 8, 0, 0, 0, 0, 0, + 0, 0, 32, 0, 60, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, - 123, 123, 123, 123, 123, 17, 17, 17 + 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 17, 17, + 17 ] ); } diff --git a/src/proofs/encoding.rs b/src/proofs/encoding.rs index aa8a3dcd..1a83d206 100644 --- a/src/proofs/encoding.rs +++ b/src/proofs/encoding.rs @@ -173,11 +173,11 @@ mod test { #[test] fn encode_push_kv() { let op = Op::Push(Node::KV(vec![1, 2, 3], vec![4, 5, 6])); - assert_eq!(op.encoding_length(), 10); + assert_eq!(op.encoding_length(), 11); let mut bytes = vec![]; op.encode_into(&mut bytes).unwrap(); - assert_eq!(bytes, vec![0x03, 3, 1, 2, 3, 0, 3, 4, 5, 6]); + assert_eq!(bytes, vec![0x03, 0, 3, 1, 2, 3, 0, 3, 4, 5, 6]); } #[test] @@ -203,7 +203,7 @@ mod test { #[test] #[should_panic] fn encode_push_kv_long_key() { - let op = Op::Push(Node::KV(vec![123; 300], vec![4, 5, 6])); + let op = Op::Push(Node::KV(vec![123; 70_000], vec![4, 5, 6])); let mut bytes = vec![]; op.encode_into(&mut bytes).unwrap(); } @@ -230,7 +230,7 @@ mod test { #[test] fn decode_push_kv() { - let bytes = [0x03, 3, 1, 2, 3, 0, 3, 4, 5, 6]; + let bytes = [0x03, 0, 3, 1, 2, 3, 0, 3, 4, 5, 6]; let op = Op::decode(&bytes[..]).expect("decode failed"); assert_eq!(op, Op::Push(Node::KV(vec![1, 2, 3], vec![4, 5, 6]))); } diff --git a/src/proofs/query/mod.rs b/src/proofs/query/mod.rs index 2dc7e735..11808910 100644 --- a/src/proofs/query/mod.rs +++ b/src/proofs/query/mod.rs @@ -932,11 +932,11 @@ mod test { assert_eq!( bytes, vec![ - 3, 1, 1, 0, 1, 1, 3, 1, 2, 0, 1, 2, 16, 3, 1, 3, 0, 1, 3, 3, 1, 4, 0, 1, 4, 16, 17, - 2, 169, 4, 73, 65, 62, 49, 160, 159, 37, 166, 195, 249, 63, 31, 23, 11, 169, 0, 24, - 104, 179, 211, 218, 38, 108, 129, 117, 232, 65, 101, 194, 157, 16, 1, 148, 241, - 151, 144, 247, 220, 92, 79, 70, 252, 168, 222, 27, 218, 53, 156, 0, 136, 161, 107, - 83, 78, 150, 246, 51, 230, 164, 248, 17, 30, 147, 91, 17 + 3, 0, 1, 1, 0, 1, 1, 3, 0, 1, 2, 0, 1, 2, 16, 3, 0, 1, 3, 0, 1, 3, 3, 0, 1, 4, 0, + 1, 4, 16, 17, 2, 169, 4, 73, 65, 62, 49, 160, 159, 37, 166, 195, 249, 63, 31, 23, + 11, 169, 0, 24, 104, 179, 211, 218, 38, 108, 129, 117, 232, 65, 101, 194, 157, 16, + 1, 148, 241, 151, 144, 247, 220, 92, 79, 70, 252, 168, 222, 27, 218, 53, 156, 0, + 136, 161, 107, 83, 78, 150, 246, 51, 230, 164, 248, 17, 30, 147, 91, 17 ] ); diff --git a/src/tree/link.rs b/src/tree/link.rs index 42aee4c7..e07cdf18 100644 --- a/src/tree/link.rs +++ b/src/tree/link.rs @@ -467,7 +467,7 @@ mod test { #[should_panic] fn encode_link_long_key() { let link = Link::Reference { - key: vec![123; 300], + key: vec![123; 70_000], child_heights: (123, 124), hash: [55; 32], }; From 9b55314c15dd3b04ec3043a0b9dd3c6f6039fbe8 Mon Sep 17 00:00:00 2001 From: Matt Bell Date: Fri, 1 Nov 2024 16:25:50 -0500 Subject: [PATCH 3/5] Fix off-by-one in key length assertions --- src/tree/kv.rs | 4 ++-- src/tree/link.rs | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/tree/kv.rs b/src/tree/kv.rs index 213cb7f5..42382c9f 100644 --- a/src/tree/kv.rs +++ b/src/tree/kv.rs @@ -76,8 +76,8 @@ impl Encode for KV { #[inline] fn encoding_length(&self) -> Result { debug_assert!( - self.key().len() < 65535, - "Key length must be less than 65535" + self.key().len() < 65536, + "Key length must be less than 65536" ); Ok(HASH_LENGTH + self.value.len()) diff --git a/src/tree/link.rs b/src/tree/link.rs index e07cdf18..8a305f4c 100644 --- a/src/tree/link.rs +++ b/src/tree/link.rs @@ -222,8 +222,8 @@ impl Encode for Link { }; debug_assert!( - self.key().len() < 65535, - "Key length must be less than 65535" + self.key().len() < 65536, + "Key length must be less than 65536" ); out.write_all(&[key.len() as u8])?; @@ -239,8 +239,8 @@ impl Encode for Link { #[inline] fn encoding_length(&self) -> Result { debug_assert!( - self.key().len() < 65535, - "Key length must be less than 65535" + self.key().len() < 65536, + "Key length must be less than 65536" ); Ok(match self { @@ -464,7 +464,7 @@ mod test { } #[test] - #[should_panic] + #[should_panic = "Key length must be less than 65536"] fn encode_link_long_key() { let link = Link::Reference { key: vec![123; 70_000], From 27bca231bc20c41704bd1e9b81deba7f86ccff53 Mon Sep 17 00:00:00 2001 From: Matt Bell Date: Fri, 1 Nov 2024 16:27:44 -0500 Subject: [PATCH 4/5] Use big-endian u16 instead of u8 for key length in storage encoding --- src/tree/link.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/tree/link.rs b/src/tree/link.rs index 8a305f4c..8ce8dde0 100644 --- a/src/tree/link.rs +++ b/src/tree/link.rs @@ -226,7 +226,7 @@ impl Encode for Link { "Key length must be less than 65536" ); - out.write_all(&[key.len() as u8])?; + out.write_all(&(key.len() as u16).to_be_bytes())?; out.write_all(key)?; out.write_all(hash)?; @@ -285,7 +285,7 @@ impl Decode for Link { ref mut child_heights, } = self { - let length = read_u8(&mut input)? as usize; + let length = read_u16(&mut input)? as usize; key.resize(length, 0); input.read_exact(key.as_mut())?; @@ -304,6 +304,13 @@ impl Decode for Link { impl Terminated for Link {} +#[inline] +fn read_u16(mut input: R) -> Result { + let mut length = [0, 0]; + input.read_exact(length.as_mut())?; + Ok(u16::from_be_bytes(length)) +} + #[inline] fn read_u8(mut input: R) -> Result { let mut length = [0]; From bf2e0caf1264508973b736e7be33429b43eb83e3 Mon Sep 17 00:00:00 2001 From: Matt Bell Date: Fri, 1 Nov 2024 16:27:59 -0500 Subject: [PATCH 5/5] Add long key length test --- src/tree/encoding.rs | 8 ++++---- src/tree/kv.rs | 1 + src/tree/link.rs | 21 ++++++++++++++++++--- src/tree/mod.rs | 4 ++-- 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/tree/encoding.rs b/src/tree/encoding.rs index 8f2cbcda..a56cd379 100644 --- a/src/tree/encoding.rs +++ b/src/tree/encoding.rs @@ -88,7 +88,7 @@ mod tests { assert_eq!( tree.encode(), vec![ - 1, 1, 2, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, + 1, 0, 1, 2, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 123, 124, 0, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 1 @@ -113,7 +113,7 @@ mod tests { assert_eq!( tree.encode(), vec![ - 1, 1, 2, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, + 1, 0, 1, 2, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 123, 124, 0, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 1 @@ -139,7 +139,7 @@ mod tests { assert_eq!( tree.encode(), vec![ - 1, 1, 2, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, + 1, 0, 1, 2, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 123, 124, 0, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 1 @@ -161,7 +161,7 @@ mod tests { #[test] fn decode_reference_tree() { let bytes = vec![ - 1, 1, 2, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, + 1, 0, 1, 2, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 123, 124, 0, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 1, diff --git a/src/tree/kv.rs b/src/tree/kv.rs index 42382c9f..ff9eae89 100644 --- a/src/tree/kv.rs +++ b/src/tree/kv.rs @@ -11,6 +11,7 @@ use std::{ // field and value field. /// Contains a key/value pair, and the hash of the key/value pair. +#[derive(Clone, Debug, PartialEq, Eq)] pub struct KV { pub(super) key: Vec, pub(super) value: Vec, diff --git a/src/tree/link.rs b/src/tree/link.rs index 8ce8dde0..313c7322 100644 --- a/src/tree/link.rs +++ b/src/tree/link.rs @@ -10,6 +10,7 @@ use super::Tree; /// Represents a reference to a child tree node. Links may or may not contain /// the child's `Tree` instance (storing its key if not). +#[derive(Debug, Clone, PartialEq, Eq)] pub enum Link { /// Represents a child tree node which has been pruned from memory, only /// retaining a reference to it (its key). The child node can always be @@ -464,15 +465,29 @@ mod test { assert_eq!( bytes, vec![ - 3, 1, 2, 3, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, - 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 123, 124 + 0, 3, 1, 2, 3, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, + 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 123, 124 ] ); } + #[test] + fn encode_link_long_key_valid() { + let link = Link::Reference { + key: vec![123; 60_000], + child_heights: (123, 124), + hash: [55; 32], + }; + let mut bytes = vec![]; + link.encode_into(&mut bytes).unwrap(); + + let decoded = Link::decode(&bytes[..]).unwrap(); + assert_eq!(decoded, link); + } + #[test] #[should_panic = "Key length must be less than 65536"] - fn encode_link_long_key() { + fn encode_link_long_key_invalid() { let link = Link::Reference { key: vec![123; 70_000], child_heights: (123, 124), diff --git a/src/tree/mod.rs b/src/tree/mod.rs index fb6226cc..db9c3665 100644 --- a/src/tree/mod.rs +++ b/src/tree/mod.rs @@ -26,7 +26,7 @@ pub use walk::{Fetch, RefWalker, Walker}; // relevant methods /// The fields of the `Tree` type, stored on the heap. -#[derive(Encode, Decode)] +#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub struct TreeInner { left: Option, right: Option, @@ -38,7 +38,7 @@ pub struct TreeInner { /// Trees' inner fields are stored on the heap so that nodes can recursively /// link to each other, and so we can detach nodes from their parents, then /// reattach without allocating or freeing heap memory. -#[derive(Encode, Decode)] +#[derive(Clone, PartialEq, Eq, Encode, Decode)] pub struct Tree { inner: Box, }