Skip to content

Commit ea155d3

Browse files
authored
Merge pull request #555 from Chia-Network/prefer-backrefs
tie-break in favor of back-references
2 parents a5c2a42 + 357486a commit ea155d3

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

fuzz/fuzz_targets/serializer_cmp.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub fn compare_back_references(allocator: &Allocator, node: NodePtr) -> io::Resu
6363
temp.write_all(&[BACK_REFERENCE])?;
6464
write_atom(&mut temp, &path)?;
6565
let temp = temp.into_inner();
66-
assert!(temp.len() < node_serialized_length as usize);
66+
assert!(temp.len() <= node_serialized_length as usize);
6767
}
6868
}
6969
None => match allocator.sexp(node_to_write) {

src/serde/read_cache_lookup.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ impl ReadCacheLookup {
142142
RandomState::default(),
143143
);
144144

145-
let max_bytes_for_path_encoding = serialized_length - 2; // 1 byte for 0xfe, 1 min byte for savings
145+
let max_bytes_for_path_encoding = serialized_length - 1; // 1 byte for 0xfe
146146
let max_path_length: usize = (max_bytes_for_path_encoding.saturating_mul(8) - 1)
147147
.try_into()
148148
.unwrap_or(usize::MAX);
@@ -156,10 +156,13 @@ impl ReadCacheLookup {
156156
if *node == self.root_hash {
157157
// make sure we never return a path that needs more (or the
158158
// same) bytes to serialize than the node we're referencing.
159-
if let Some(path_len) = atom_length_bits(path.len() as u64) {
160-
if path_len < max_bytes_for_path_encoding {
161-
let p = reversed_path_to_vec_u8(path);
162-
possible_responses.push(p);
159+
// path.len() + 1 is because reversed_path_to_vec_u8() will
160+
// also add the "terminator" bit, at the far left (MSB)
161+
// if we have 8 steps to traverse, we need 9 bits to represent
162+
// it as a path
163+
if let Some(path_len) = atom_length_bits(path.len() as u64 + 1) {
164+
if path_len <= max_bytes_for_path_encoding {
165+
possible_responses.push(reversed_path_to_vec_u8(path));
163166
}
164167
}
165168
continue;

0 commit comments

Comments
 (0)