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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
use codec::{Decode, EncodeLike, FullCodec, FullEncode};
use frame_support::{
	storage::{
		generator::{StorageDoubleMap as StorageDoubleMapT, StorageMap as StorageMapT},
		unhashed, StorageDoubleMap, StorageMap,
	},
	ReversibleStorageHasher,
};
use sp_std::prelude::*;

/// Utility to iterate through items in a storage map.
/// Forks from substrate, expose previous_key field
pub struct StorageMapIterator<K, V, Hasher> {
	prefix: Vec<u8>,
	pub previous_key: Vec<u8>,
	drain: bool,
	_phantom: sp_std::marker::PhantomData<(K, V, Hasher)>,
}

/// Forks from substrate
impl<K: Decode + Sized, V: Decode + Sized, Hasher: ReversibleStorageHasher> Iterator
	for StorageMapIterator<K, V, Hasher>
{
	type Item = (K, V);

	fn next(&mut self) -> Option<(K, V)> {
		loop {
			let maybe_next = sp_io::storage::next_key(&self.previous_key).filter(|n| n.starts_with(&self.prefix));
			break match maybe_next {
				Some(next) => {
					self.previous_key = next;
					match unhashed::get::<V>(&self.previous_key) {
						Some(value) => {
							if self.drain {
								unhashed::kill(&self.previous_key)
							}
							let mut key_material = Hasher::reverse(&self.previous_key[self.prefix.len()..]);
							match K::decode(&mut key_material) {
								Ok(key) => Some((key, value)),
								Err(_) => continue,
							}
						}
						None => continue,
					}
				}
				None => None,
			};
		}
	}
}

/// Shim for StorageMapIterator, add more features
pub struct StorageMapIteratorShim<K, V, H> {
	pub storage_map_iterator: StorageMapIterator<K, V, H>,
	pub remain_iterator_count: Option<u32>,
	pub finished: bool,
}

impl<K: Decode + Sized, V: Decode + Sized, H: ReversibleStorageHasher> Iterator for StorageMapIteratorShim<K, V, H> {
	type Item = <StorageMapIterator<K, V, H> as Iterator>::Item;

	fn next(&mut self) -> Option<Self::Item> {
		// check accumulated iteration count
		if let Some(remain_iterator_count) = self.remain_iterator_count {
			if remain_iterator_count == 0 {
				return None;
			} else {
				self.remain_iterator_count = Some(remain_iterator_count - 1);
			}
		}

		self.storage_map_iterator.next().or_else(|| {
			// mark this map iterator has already finished
			self.finished = true;
			None
		})
	}
}

/// A strongly-typed map in storage whose keys and values can be iterated over.
pub trait IterableStorageMapExtended<K: FullEncode, V: FullCodec>: StorageMap<K, V> {
	/// The type that iterates over all `(key, value)`.
	type Iterator: Iterator<Item = (K, V)>;

	/// Enumerate all elements in the map in no particular order. If you alter
	/// the map while doing this, you'll get undefined results.
	fn iter(max_iterations: Option<u32>, start_key: Option<Vec<u8>>) -> Self::Iterator;

	/// Remove all elements from the map and iterate through them in no
	/// particular order. If you add elements to the map while doing this,
	/// you'll get undefined results.
	fn drain(max_iterations: Option<u32>, start_key: Option<Vec<u8>>) -> Self::Iterator;
}

impl<K: FullCodec, V: FullCodec, G: StorageMapT<K, V>> IterableStorageMapExtended<K, V> for G
where
	G::Hasher: ReversibleStorageHasher,
{
	type Iterator = StorageMapIteratorShim<K, V, G::Hasher>;

	/// Enumerate all elements in the map.
	fn iter(max_iterations: Option<u32>, start_key: Option<Vec<u8>>) -> Self::Iterator {
		let prefix = G::prefix_hash();
		let previous_key = start_key
			.filter(|k| k.starts_with(&prefix))
			.unwrap_or_else(|| prefix.clone());
		let storage_map_iterator = StorageMapIterator {
			prefix,
			previous_key,
			drain: false,
			_phantom: Default::default(),
		};

		StorageMapIteratorShim {
			storage_map_iterator,
			remain_iterator_count: max_iterations,
			finished: false,
		}
	}

	/// Enumerate all elements in the map.
	fn drain(max_iterations: Option<u32>, start_key: Option<Vec<u8>>) -> Self::Iterator {
		let prefix = G::prefix_hash();

		let previous_key = start_key
			.filter(|k| k.starts_with(&prefix))
			.unwrap_or_else(|| prefix.clone());
		let storage_map_iterator = StorageMapIterator {
			prefix,
			previous_key,
			drain: true,
			_phantom: Default::default(),
		};

		StorageMapIteratorShim {
			storage_map_iterator,
			remain_iterator_count: max_iterations,
			finished: false,
		}
	}
}

/// Iterate over a prefix and decode raw_key and raw_value into `T`.
/// Forks from substrate, expose previous_key field
pub struct MapIterator<T> {
	prefix: Vec<u8>,
	pub previous_key: Vec<u8>,
	/// If true then value are removed while iterating
	drain: bool,
	/// Function that take `(raw_key_without_prefix, raw_value)` and decode `T`.
	/// `raw_key_without_prefix` is the raw storage key without the prefix
	/// iterated on.
	closure: fn(&[u8], &[u8]) -> Result<T, codec::Error>,
}

/// Forks from substrate
impl<T> Iterator for MapIterator<T> {
	type Item = T;

	fn next(&mut self) -> Option<Self::Item> {
		loop {
			let maybe_next = sp_io::storage::next_key(&self.previous_key).filter(|n| n.starts_with(&self.prefix));
			break match maybe_next {
				Some(next) => {
					self.previous_key = next;
					let raw_value = match unhashed::get_raw(&self.previous_key) {
						Some(raw_value) => raw_value,
						None => {
							frame_support::print("ERROR: next_key returned a key with no value in MapIterator");
							continue;
						}
					};
					if self.drain {
						unhashed::kill(&self.previous_key)
					}
					let raw_key_without_prefix = &self.previous_key[self.prefix.len()..];
					let item = match (self.closure)(raw_key_without_prefix, &raw_value[..]) {
						Ok(item) => item,
						Err(_e) => {
							frame_support::print("ERROR: (key, value) failed to decode in MapIterator");
							continue;
						}
					};

					Some(item)
				}
				None => None,
			};
		}
	}
}

/// Shim for MapIterator, add more features
pub struct MapIteratorShim<T> {
	pub map_iterator: MapIterator<T>,
	pub remain_iterator_count: Option<u32>,
	pub finished: bool,
}

impl<T> Iterator for MapIteratorShim<T> {
	type Item = <MapIterator<T> as Iterator>::Item;

	fn next(&mut self) -> Option<Self::Item> {
		// check accumulated iteration count
		if let Some(remain_iterator_count) = self.remain_iterator_count {
			if remain_iterator_count == 0 {
				return None;
			} else {
				self.remain_iterator_count = Some(remain_iterator_count - 1);
			}
		}

		self.map_iterator.next().or_else(|| {
			// mark this map iterator has already finished
			self.finished = true;
			None
		})
	}
}

/// A strongly-typed map in storage whose keys and values can be iterated over.
pub trait IterableStorageDoubleMapExtended<K1: FullCodec, K2: FullCodec, V: FullCodec>:
	StorageDoubleMap<K1, K2, V>
{
	/// The type that iterates over all `(key2, value)`.
	type PrefixIterator: Iterator<Item = (K2, V)>;

	/// The type that iterates over all `(key1, key2, value)`.
	type Iterator: Iterator<Item = (K1, K2, V)>;

	/// Enumerate all elements in the map with first key `k1` in no particular
	/// order. If you add or remove values whose first key is `k1` to the map
	/// while doing this, you'll get undefined results.
	fn iter_prefix(
		k1: impl EncodeLike<K1>,
		max_iterations: Option<u32>,
		start_key: Option<Vec<u8>>,
	) -> Self::PrefixIterator;

	/// Remove all elements from the map with first key `k1` and iterate through
	/// them in no particular order. If you add elements with first key `k1` to
	/// the map while doing this, you'll get undefined results.
	fn drain_prefix(
		k1: impl EncodeLike<K1>,
		max_iterations: Option<u32>,
		start_key: Option<Vec<u8>>,
	) -> Self::PrefixIterator;

	/// Enumerate all elements in the map in no particular order. If you add or
	/// remove values to the map while doing this, you'll get undefined results.
	fn iter(max_iterations: Option<u32>, start_key: Option<Vec<u8>>) -> Self::Iterator;

	/// Remove all elements from the map and iterate through them in no
	/// particular order. If you add elements to the map while doing this,
	/// you'll get undefined results.
	fn drain(max_iterations: Option<u32>, start_key: Option<Vec<u8>>) -> Self::Iterator;
}

impl<K1: FullCodec, K2: FullCodec, V: FullCodec, G: StorageDoubleMapT<K1, K2, V>>
	IterableStorageDoubleMapExtended<K1, K2, V> for G
where
	G::Hasher1: ReversibleStorageHasher,
	G::Hasher2: ReversibleStorageHasher,
{
	type PrefixIterator = MapIteratorShim<(K2, V)>;
	type Iterator = MapIteratorShim<(K1, K2, V)>;

	fn iter_prefix(
		k1: impl EncodeLike<K1>,
		max_iterations: Option<u32>,
		start_key: Option<Vec<u8>>,
	) -> Self::PrefixIterator {
		let prefix = G::storage_double_map_final_key1(k1);
		let previous_key = start_key
			.filter(|k| k.starts_with(&prefix))
			.unwrap_or_else(|| prefix.clone());

		let map_iterator = MapIterator {
			prefix,
			previous_key,
			drain: false,
			closure: |raw_key_without_prefix, mut raw_value| {
				let mut key_material = G::Hasher2::reverse(raw_key_without_prefix);
				Ok((K2::decode(&mut key_material)?, V::decode(&mut raw_value)?))
			},
		};

		MapIteratorShim {
			map_iterator,
			remain_iterator_count: max_iterations,
			finished: false,
		}
	}

	fn drain_prefix(
		k1: impl EncodeLike<K1>,
		max_iterations: Option<u32>,
		start_key: Option<Vec<u8>>,
	) -> Self::PrefixIterator {
		let mut shim = Self::iter_prefix(k1, max_iterations, start_key);
		shim.map_iterator.drain = true;
		shim
	}

	fn iter(max_iterations: Option<u32>, start_key: Option<Vec<u8>>) -> Self::Iterator {
		let prefix = G::prefix_hash();
		let previous_key = start_key
			.filter(|k| k.starts_with(&prefix))
			.unwrap_or_else(|| prefix.clone());

		let map_iterator = MapIterator {
			prefix,
			previous_key,
			drain: false,
			closure: |raw_key_without_prefix, mut raw_value| {
				let mut k1_k2_material = G::Hasher1::reverse(raw_key_without_prefix);
				let k1 = K1::decode(&mut k1_k2_material)?;
				let mut k2_material = G::Hasher2::reverse(k1_k2_material);
				let k2 = K2::decode(&mut k2_material)?;
				Ok((k1, k2, V::decode(&mut raw_value)?))
			},
		};

		MapIteratorShim {
			map_iterator,
			remain_iterator_count: max_iterations,
			finished: false,
		}
	}

	fn drain(max_iterations: Option<u32>, start_key: Option<Vec<u8>>) -> Self::Iterator {
		let mut shim = Self::iter(max_iterations, start_key);
		shim.map_iterator.drain = true;
		shim
	}
}