1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use crate::sparse_merkle::utils::partition;
use bitvec::prelude::*;
use diem_crypto::{
hash::{CryptoHash, SPARSE_MERKLE_PLACEHOLDER_HASH},
HashValue,
};
use diem_types::proof::{SparseMerkleInternalNode, SparseMerkleLeafNode, SparseMerkleProof};
use std::collections::{BTreeMap, HashMap};
type Cache = HashMap<BitVec<Msb0, u8>, HashValue>;
struct NaiveSubTree<'a> {
leaves: &'a [(HashValue, HashValue)],
depth: usize,
}
impl<'a> NaiveSubTree<'a> {
fn get_proof(
&'a self,
key: &HashValue,
cache: &mut Cache,
) -> (Option<SparseMerkleLeafNode>, Vec<HashValue>) {
if self.is_empty() {
(None, Vec::new())
} else if self.leaves.len() == 1 {
let only_leaf = self.leaves[0];
(
Some(SparseMerkleLeafNode::new(only_leaf.0, only_leaf.1)),
Vec::new(),
)
} else {
let (left, right) = self.children();
if key.bit(self.depth) {
let (ret_leaf, mut ret_siblings) = right.get_proof(key, cache);
ret_siblings.push(left.get_hash(cache));
(ret_leaf, ret_siblings)
} else {
let (ret_leaf, mut ret_siblings) = left.get_proof(key, cache);
ret_siblings.push(right.get_hash(cache));
(ret_leaf, ret_siblings)
}
}
}
fn is_empty(&self) -> bool {
self.leaves.is_empty()
}
fn get_hash(&self, cache: &mut Cache) -> HashValue {
if self.leaves.is_empty() {
return *SPARSE_MERKLE_PLACEHOLDER_HASH;
}
let position = self.leaves[0]
.0
.view_bits()
.split_at(self.depth)
.0
.to_bitvec();
match cache.get(&position) {
Some(hash) => *hash,
None => {
let hash = self.get_hash_uncached(cache);
cache.insert(position, hash);
hash
}
}
}
fn get_hash_uncached(&self, cache: &mut Cache) -> HashValue {
assert!(!self.leaves.is_empty());
if self.leaves.len() == 1 {
let only_leaf = self.leaves[0];
SparseMerkleLeafNode::new(only_leaf.0, only_leaf.1).hash()
} else {
let (left, right) = self.children();
SparseMerkleInternalNode::new(left.get_hash(cache), right.get_hash(cache)).hash()
}
}
fn children(&self) -> (Self, Self) {
let pivot = partition(self.leaves, self.depth);
let (left, right) = self.leaves.split_at(pivot);
(
Self {
leaves: left,
depth: self.depth + 1,
},
Self {
leaves: right,
depth: self.depth + 1,
},
)
}
}
#[derive(Clone, Default)]
pub struct NaiveSmt {
leaves: Vec<(HashValue, HashValue)>,
cache: Cache,
}
impl NaiveSmt {
pub fn new<V: CryptoHash>(leaves: &[(HashValue, &V)]) -> Self {
Self::default().update(leaves)
}
pub fn update<V: CryptoHash>(self, updates: &[(HashValue, &V)]) -> Self {
let mut leaves = self.leaves.into_iter().collect::<BTreeMap<_, _>>();
let mut new_leaves = updates
.iter()
.map(|(address, value)| (*address, value.hash()))
.collect::<BTreeMap<_, _>>();
leaves.append(&mut new_leaves);
Self {
leaves: leaves.into_iter().collect::<Vec<_>>(),
cache: Cache::new(),
}
}
pub fn get_proof<V: CryptoHash>(&mut self, key: &HashValue) -> SparseMerkleProof<V> {
let root = NaiveSubTree {
leaves: &self.leaves,
depth: 0,
};
let (leaf, siblings) = root.get_proof(key, &mut self.cache);
SparseMerkleProof::new(leaf, siblings)
}
pub fn get_root_hash(&mut self) -> HashValue {
let root = NaiveSubTree {
leaves: &self.leaves,
depth: 0,
};
root.get_hash(&mut self.cache)
}
}