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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use crate::{
mmr::{
storage::{OffchainStorage, RuntimeStorage, Storage},
utils::NodesUtils,
Hasher, Node, NodeOf,
},
primitives::{self, Error},
Config, HashingOf,
};
#[cfg(not(feature = "std"))]
use sp_std::vec;
pub fn verify_leaf_proof<H, L>(
root: H::Output,
leaf: Node<H, L>,
proof: primitives::Proof<H::Output>,
) -> Result<bool, Error>
where
H: sp_runtime::traits::Hash,
L: primitives::FullLeaf,
{
let size = NodesUtils::new(proof.leaf_count).size();
let leaf_position = mmr_lib::leaf_index_to_pos(proof.leaf_index);
let p = mmr_lib::MerkleProof::<Node<H, L>, Hasher<H, L>>::new(
size,
proof.items.into_iter().map(Node::Hash).collect(),
);
p.verify(Node::Hash(root), vec![(leaf_position, leaf)])
.map_err(|e| Error::Verify.log_debug(e))
}
pub struct Mmr<StorageType, T, I, L>
where
T: Config<I>,
I: 'static,
L: primitives::FullLeaf,
Storage<StorageType, T, I, L>: mmr_lib::MMRStore<NodeOf<T, I, L>>,
{
mmr: mmr_lib::MMR<NodeOf<T, I, L>, Hasher<HashingOf<T, I>, L>, Storage<StorageType, T, I, L>>,
leaves: u64,
}
impl<StorageType, T, I, L> Mmr<StorageType, T, I, L>
where
T: Config<I>,
I: 'static,
L: primitives::FullLeaf,
Storage<StorageType, T, I, L>: mmr_lib::MMRStore<NodeOf<T, I, L>>,
{
pub fn new(leaves: u64) -> Self {
let size = NodesUtils::new(leaves).size();
Self { mmr: mmr_lib::MMR::new(size, Default::default()), leaves }
}
pub fn verify_leaf_proof(
&self,
leaf: L,
proof: primitives::Proof<<T as Config<I>>::Hash>,
) -> Result<bool, Error> {
let p = mmr_lib::MerkleProof::<NodeOf<T, I, L>, Hasher<HashingOf<T, I>, L>>::new(
self.mmr.mmr_size(),
proof.items.into_iter().map(Node::Hash).collect(),
);
let position = mmr_lib::leaf_index_to_pos(proof.leaf_index);
let root = self.mmr.get_root().map_err(|e| Error::GetRoot.log_error(e))?;
p.verify(root, vec![(position, Node::Data(leaf))])
.map_err(|e| Error::Verify.log_debug(e))
}
#[cfg(test)]
pub fn size(&self) -> u64 {
self.mmr.mmr_size()
}
}
impl<T, I, L> Mmr<RuntimeStorage, T, I, L>
where
T: Config<I>,
I: 'static,
L: primitives::FullLeaf,
{
pub fn push(&mut self, leaf: L) -> Option<u64> {
let position =
self.mmr.push(Node::Data(leaf)).map_err(|e| Error::Push.log_error(e)).ok()?;
self.leaves += 1;
Some(position)
}
pub fn finalize(self) -> Result<(u64, <T as Config<I>>::Hash), Error> {
let root = self.mmr.get_root().map_err(|e| Error::GetRoot.log_error(e))?;
self.mmr.commit().map_err(|e| Error::Commit.log_error(e))?;
Ok((self.leaves, root.hash()))
}
}
impl<T, I, L> Mmr<OffchainStorage, T, I, L>
where
T: Config<I>,
I: 'static,
L: primitives::FullLeaf + codec::Decode,
{
pub fn generate_proof(
&self,
leaf_index: u64,
) -> Result<(L, primitives::Proof<<T as Config<I>>::Hash>), Error> {
let position = mmr_lib::leaf_index_to_pos(leaf_index);
let store = <Storage<OffchainStorage, T, I, L>>::default();
let leaf = match mmr_lib::MMRStore::get_elem(&store, position) {
Ok(Some(Node::Data(leaf))) => leaf,
e => return Err(Error::LeafNotFound.log_debug(e)),
};
let leaf_count = self.leaves;
self.mmr
.gen_proof(vec![position])
.map_err(|e| Error::GenerateProof.log_error(e))
.map(|p| primitives::Proof {
leaf_index,
leaf_count,
items: p.proof_items().iter().map(|x| x.hash()).collect(),
})
.map(|p| (leaf, p))
}
}