1
// This file is part of Substrate.
2

            
3
// Copyright (C) Parity Technologies (UK) Ltd.
4
// SPDX-License-Identifier: Apache-2.0
5

            
6
// Licensed under the Apache License, Version 2.0 (the "License");
7
// you may not use this file except in compliance with the License.
8
// You may obtain a copy of the License at
9
//
10
// 	http://www.apache.org/licenses/LICENSE-2.0
11
//
12
// Unless required by applicable law or agreed to in writing, software
13
// distributed under the License is distributed on an "AS IS" BASIS,
14
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
// See the License for the specific language governing permissions and
16
// limitations under the License.
17

            
18
//! Merkle Mountain Range utilities.
19

            
20
use codec::Encode;
21
use mmr_lib::helper;
22

            
23
#[cfg(not(feature = "std"))]
24
use alloc::vec::Vec;
25
use sp_runtime::traits::{CheckedAdd, CheckedSub, Header, One};
26

            
27
use crate::{Error, LeafIndex, NodeIndex};
28

            
29
/// Get the first block with MMR.
30
pub fn first_mmr_block_num<H: Header>(
31
	best_block_num: H::Number,
32
	mmr_leaf_count: LeafIndex,
33
) -> Result<H::Number, Error> {
34
	let mmr_blocks_count = mmr_leaf_count.try_into().map_err(|_| {
35
		Error::InvalidNumericOp
36
			.log_debug("The number of leaves couldn't be converted to a block number.")
37
	})?;
38
	best_block_num
39
		.checked_sub(&mmr_blocks_count)
40
		.and_then(|last_non_mmr_block| last_non_mmr_block.checked_add(&One::one()))
41
		.ok_or_else(|| {
42
			Error::InvalidNumericOp
43
				.log_debug("The best block should be greater than the number of mmr blocks.")
44
		})
45
}
46

            
47
/// Convert a block number into a leaf index.
48
pub fn block_num_to_leaf_index<H: Header>(
49
	block_num: H::Number,
50
	first_mmr_block_num: H::Number,
51
) -> Result<LeafIndex, Error> {
52
	let leaf_idx = block_num.checked_sub(&first_mmr_block_num).ok_or_else(|| {
53
		Error::InvalidNumericOp
54
			.log_debug("The provided block should be greater than the first mmr block.")
55
	})?;
56

            
57
	leaf_idx.try_into().map_err(|_| {
58
		Error::InvalidNumericOp.log_debug("Couldn't convert the leaf index to `LeafIndex`.")
59
	})
60
}
61

            
62
/// MMR nodes & size -related utilities.
63
pub struct NodesUtils {
64
	no_of_leaves: LeafIndex,
65
}
66

            
67
impl NodesUtils {
68
	/// Create new instance of MMR nodes utilities for given number of leaves.
69
1038736
	pub fn new(no_of_leaves: LeafIndex) -> Self {
70
1038736
		Self { no_of_leaves }
71
1038736
	}
72

            
73
	/// Calculate number of peaks in the MMR.
74
519368
	pub fn number_of_peaks(&self) -> NodeIndex {
75
519368
		self.number_of_leaves().count_ones() as NodeIndex
76
519368
	}
77

            
78
	/// Return the number of leaves in the MMR.
79
519368
	pub fn number_of_leaves(&self) -> LeafIndex {
80
519368
		self.no_of_leaves
81
519368
	}
82

            
83
	/// Calculate the total size of MMR (number of nodes).
84
259684
	pub fn size(&self) -> NodeIndex {
85
259684
		2 * self.no_of_leaves - self.number_of_peaks()
86
259684
	}
87

            
88
	/// Calculate `LeafIndex` for the leaf that added `node_index` to the MMR.
89
	pub fn leaf_index_that_added_node(node_index: NodeIndex) -> LeafIndex {
90
		let rightmost_leaf_pos = Self::rightmost_leaf_node_index_from_pos(node_index);
91
		Self::leaf_node_index_to_leaf_index(rightmost_leaf_pos)
92
	}
93

            
94
	// Translate a _leaf_ `NodeIndex` to its `LeafIndex`.
95
	fn leaf_node_index_to_leaf_index(pos: NodeIndex) -> LeafIndex {
96
		if pos == 0 {
97
			return 0
98
		}
99
		let peaks = helper::get_peaks(pos);
100
		(pos + peaks.len() as u64) >> 1
101
	}
102

            
103
	// Starting from any node position get position of rightmost leaf; this is the leaf
104
	// responsible for the addition of node `pos`.
105
	fn rightmost_leaf_node_index_from_pos(pos: NodeIndex) -> NodeIndex {
106
		pos - (helper::pos_height_in_tree(pos) as u64)
107
	}
108

            
109
	/// Starting from any leaf index, get the sequence of positions of the nodes added
110
	/// to the mmr when this leaf was added (inclusive of the leaf's position itself).
111
	/// That is, all of these nodes are right children of their respective parents.
112
	pub fn right_branch_ending_in_leaf(leaf_index: LeafIndex) -> Vec<NodeIndex> {
113
		let pos = helper::leaf_index_to_pos(leaf_index);
114
		let num_parents = leaf_index.trailing_ones() as u64;
115
		return (pos..=pos + num_parents).collect()
116
	}
117

            
118
	/// Build offchain key from `parent_hash` of block that originally added node `pos` to MMR.
119
	///
120
	/// This combination makes the offchain (key,value) entry resilient to chain forks.
121
323562
	pub fn node_temp_offchain_key<H: Header>(
122
323562
		prefix: &[u8],
123
323562
		pos: NodeIndex,
124
323562
		parent_hash: H::Hash,
125
323562
	) -> Vec<u8> {
126
323562
		(prefix, pos, parent_hash).encode()
127
323562
	}
128

            
129
	/// Build canonical offchain key for node `pos` in MMR.
130
	///
131
	/// Used for nodes added by now finalized blocks.
132
	/// Never read keys using `node_canon_offchain_key` unless you sure that
133
	/// there's no `node_offchain_key` key in the storage.
134
	pub fn node_canon_offchain_key(prefix: &[u8], pos: NodeIndex) -> alloc::vec::Vec<u8> {
135
		(prefix, pos).encode()
136
	}
137
}
138

            
139
#[cfg(test)]
140
mod tests {
141
	use super::*;
142
	use mmr_lib::helper::leaf_index_to_pos;
143

            
144
	#[test]
145
	fn should_calculate_node_index_from_leaf_index() {
146
		for index in 0..100000 {
147
			let pos = leaf_index_to_pos(index);
148
			assert_eq!(NodesUtils::leaf_node_index_to_leaf_index(pos), index);
149
		}
150
	}
151

            
152
	#[test]
153
	fn should_calculate_right_branch_correctly() {
154
		fn left_jump_sequence(leaf_index: LeafIndex) -> Vec<u64> {
155
			let pos = leaf_index_to_pos(leaf_index);
156
			let mut right_branch_ending_in_leaf = vec![pos];
157
			let mut next_pos = pos + 1;
158
			while mmr_lib::helper::pos_height_in_tree(next_pos) > 0 {
159
				right_branch_ending_in_leaf.push(next_pos);
160
				next_pos += 1;
161
			}
162
			right_branch_ending_in_leaf
163
		}
164

            
165
		for leaf_index in 0..100000 {
166
			let pos = mmr_lib::helper::leaf_index_to_pos(leaf_index);
167
			assert_eq!(NodesUtils::right_branch_ending_in_leaf(pos), left_jump_sequence(pos));
168
		}
169
	}
170

            
171
	#[test]
172
	fn should_calculate_rightmost_leaf_node_index_from_pos() {
173
		for pos in 0..100000 {
174
			let leaf_pos = NodesUtils::rightmost_leaf_node_index_from_pos(pos);
175
			let leaf_index = NodesUtils::leaf_node_index_to_leaf_index(leaf_pos);
176
			assert!(NodesUtils::right_branch_ending_in_leaf(leaf_index).contains(&pos));
177
		}
178
	}
179

            
180
	#[test]
181
	fn should_calculate_depth_correctly() {
182
		assert_eq!(
183
			vec![0, 1, 2, 3, 4, 9, 15, 21]
184
				.into_iter()
185
				.map(|n| NodesUtils::new(n).number_of_leaves())
186
				.collect::<Vec<_>>(),
187
			vec![0, 1, 2, 3, 4, 9, 15, 21]
188
		);
189
	}
190

            
191
	#[test]
192
	fn should_calculate_number_of_peaks_correctly() {
193
		assert_eq!(
194
			vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 21]
195
				.into_iter()
196
				.map(|n| NodesUtils::new(n).number_of_peaks())
197
				.collect::<Vec<_>>(),
198
			vec![0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 3]
199
		);
200
	}
201

            
202
	#[test]
203
	fn should_calculate_the_size_correctly() {
204
		let leaves = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 21];
205
		let sizes = vec![0, 1, 3, 4, 7, 8, 10, 11, 15, 16, 18, 19, 22, 23, 25, 26, 39];
206
		assert_eq!(
207
			leaves
208
				.clone()
209
				.into_iter()
210
				.map(|n| NodesUtils::new(n).size())
211
				.collect::<Vec<_>>(),
212
			sizes.clone()
213
		);
214
	}
215
}