Skip to content

Commit

Permalink
Support u16::MAX length keys
Browse files Browse the repository at this point in the history
  • Loading branch information
cwlittle committed Oct 22, 2024
1 parent cc49630 commit d6f0490
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
10 changes: 5 additions & 5 deletions src/proofs/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand All @@ -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,
})
Expand All @@ -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())?;

Expand Down
6 changes: 5 additions & 1 deletion src/tree/kv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ impl Encode for KV {

#[inline]
fn encoding_length(&self) -> Result<usize> {
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())
}
}
Expand Down
10 changes: 8 additions & 2 deletions src/tree/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand All @@ -235,7 +238,10 @@ impl Encode for Link {

#[inline]
fn encoding_length(&self) -> Result<usize> {
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,
Expand Down

0 comments on commit d6f0490

Please sign in to comment.