triehash clenaup in progress
This commit is contained in:
parent
f02bd0cd17
commit
710d3594a8
@ -139,7 +139,7 @@ impl NibblePair {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn shared_nibble_prefix_len(vec: &[NibblePair]) -> usize {
|
fn nibble_shared_prefix_len(vec: &[NibblePair]) -> usize {
|
||||||
if vec.len() == 0 {
|
if vec.len() == 0 {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -185,40 +185,55 @@ pub fn hash256(vec: &[NibblePair]) -> H256 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn hash256rlp(vec: &[NibblePair], pre_len: usize, stream: &mut RlpStream) {
|
fn hash256rlp(vec: &[NibblePair], pre_len: usize, stream: &mut RlpStream) {
|
||||||
match vec.len() {
|
let vlen = vec.len();
|
||||||
0 => {
|
|
||||||
|
// in case of empty slice, just append null
|
||||||
|
if vlen == 0 {
|
||||||
stream.append(&"");
|
stream.append(&"");
|
||||||
},
|
return;
|
||||||
1 => {
|
}
|
||||||
|
|
||||||
|
// if the slice contains just one item, append the suffix of the key
|
||||||
|
// and then append value
|
||||||
|
if vlen == 1 {
|
||||||
stream.append_list(2);
|
stream.append_list(2);
|
||||||
stream.append(&hex_prefix_encode(&vec[0].nibble[pre_len..], true));
|
stream.append(&hex_prefix_encode(&vec[0].nibble[pre_len..], true));
|
||||||
stream.append(&vec[0].data);
|
stream.append(&vec[0].data);
|
||||||
},
|
return;
|
||||||
_ => {
|
}
|
||||||
let shared_prefix = shared_nibble_prefix_len(vec);
|
|
||||||
|
|
||||||
match shared_prefix > pre_len {
|
// get length of the longest shared prefix in slice keys
|
||||||
true => {
|
let shared_prefix = nibble_shared_prefix_len(vec);
|
||||||
|
|
||||||
|
// if shared prefix is higher than current prefix append its
|
||||||
|
// new part of the key to the stream
|
||||||
|
// then recursively append suffixes of all items who had this key
|
||||||
|
if shared_prefix > pre_len {
|
||||||
stream.append_list(2);
|
stream.append_list(2);
|
||||||
stream.append(&hex_prefix_encode(&vec[0].nibble[pre_len..shared_prefix], false));
|
stream.append(&hex_prefix_encode(&vec[0].nibble[pre_len..shared_prefix], false));
|
||||||
hash256aux(vec, shared_prefix, stream);
|
hash256aux(vec, shared_prefix, stream);
|
||||||
},
|
return;
|
||||||
false => {
|
}
|
||||||
|
|
||||||
|
// an item for every possible nibble/suffix
|
||||||
|
// + 1 for data
|
||||||
stream.append_list(17);
|
stream.append_list(17);
|
||||||
|
|
||||||
// if first nibble len is equal to pre_len
|
// if first key len is equal to prefix_len, move to next element
|
||||||
// move forward
|
|
||||||
let mut begin = match pre_len == vec[0].nibble.len() {
|
let mut begin = match pre_len == vec[0].nibble.len() {
|
||||||
true => 1,
|
true => 1,
|
||||||
false => 0
|
false => 0
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// iterate over all possible nibbles
|
||||||
for i in 0..16 {
|
for i in 0..16 {
|
||||||
// cout how many successive elements have same next nibble
|
// cout how many successive elements have same next nibble
|
||||||
let len = vec[begin..].iter()
|
let len = vec[begin..].iter()
|
||||||
.map(| pair | pair.nibble[pre_len] )
|
.map(| pair | pair.nibble[pre_len] )
|
||||||
.take_while(|&q| q == i).count();
|
.take_while(|&q| q == i).count();
|
||||||
|
|
||||||
|
// if at least 1 successive element has the same nibble
|
||||||
|
// append their suffixes
|
||||||
match len {
|
match len {
|
||||||
0 => { stream.append(&""); },
|
0 => { stream.append(&""); },
|
||||||
_ => hash256aux(&vec[begin..(begin + len)], pre_len + 1, stream)
|
_ => hash256aux(&vec[begin..(begin + len)], pre_len + 1, stream)
|
||||||
@ -226,14 +241,11 @@ fn hash256rlp(vec: &[NibblePair], pre_len: usize, stream: &mut RlpStream) {
|
|||||||
begin += len;
|
begin += len;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if fist key len is equal prefix, append it's value
|
||||||
match pre_len == vec[0].nibble.len() {
|
match pre_len == vec[0].nibble.len() {
|
||||||
true => { stream.append(&vec[0].data); } ,
|
true => { stream.append(&vec[0].data); },
|
||||||
false => { stream.append(&""); }
|
false => { stream.append(&""); }
|
||||||
};
|
};
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn hash256aux(vec: &[NibblePair], pre_len: usize, stream: &mut RlpStream) {
|
fn hash256aux(vec: &[NibblePair], pre_len: usize, stream: &mut RlpStream) {
|
||||||
@ -249,7 +261,7 @@ fn hash256aux(vec: &[NibblePair], pre_len: usize, stream: &mut RlpStream) {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_shared_nibble_len() {
|
fn test_shared_nibble_len() {
|
||||||
let len = shared_nibble_prefix_len(&vec![
|
let len = nibble_shared_prefix_len(&vec![
|
||||||
NibblePair::new(vec![0, 1, 2, 3, 4, 5, 6], vec![]),
|
NibblePair::new(vec![0, 1, 2, 3, 4, 5, 6], vec![]),
|
||||||
NibblePair::new(vec![0, 1, 2, 3, 4, 5, 6], vec![]),
|
NibblePair::new(vec![0, 1, 2, 3, 4, 5, 6], vec![]),
|
||||||
]);
|
]);
|
||||||
@ -259,7 +271,7 @@ fn test_shared_nibble_len() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_shared_nibble_len2() {
|
fn test_shared_nibble_len2() {
|
||||||
let len = shared_nibble_prefix_len(&vec![
|
let len = nibble_shared_prefix_len(&vec![
|
||||||
NibblePair::new(vec![0, 1, 2, 3, 4, 5, 6], vec![]),
|
NibblePair::new(vec![0, 1, 2, 3, 4, 5, 6], vec![]),
|
||||||
NibblePair::new(vec![0, 1, 2, 3, 4, 5, 6], vec![]),
|
NibblePair::new(vec![0, 1, 2, 3, 4, 5, 6], vec![]),
|
||||||
NibblePair::new(vec![4, 1, 2, 3, 4, 5, 6], vec![])
|
NibblePair::new(vec![4, 1, 2, 3, 4, 5, 6], vec![])
|
||||||
@ -270,7 +282,7 @@ fn test_shared_nibble_len2() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_shared_nibble_len3() {
|
fn test_shared_nibble_len3() {
|
||||||
let len = shared_nibble_prefix_len(&vec![
|
let len = nibble_shared_prefix_len(&vec![
|
||||||
NibblePair::new(vec![0, 1, 2, 3, 4, 5, 6], vec![]),
|
NibblePair::new(vec![0, 1, 2, 3, 4, 5, 6], vec![]),
|
||||||
NibblePair::new(vec![0, 1, 2, 3, 4, 5, 6], vec![]),
|
NibblePair::new(vec![0, 1, 2, 3, 4, 5, 6], vec![]),
|
||||||
NibblePair::new(vec![0, 1, 2, 4, 4, 5, 6], vec![])
|
NibblePair::new(vec![0, 1, 2, 4, 4, 5, 6], vec![])
|
||||||
|
Loading…
Reference in New Issue
Block a user