|
66 | 66 |
|
67 | 67 | #[cfg(feature = "std")] |
68 | 68 | use serde::Serialize; |
| 69 | +use sp_io::hashing::blake2_256; |
69 | 70 | #[cfg(feature = "runtime-benchmarks")] |
70 | 71 | use sp_runtime::traits::TrailingZeroInput; |
71 | 72 | use sp_runtime::{ |
@@ -1316,6 +1317,8 @@ impl<T: Config> Pallet<T> { |
1316 | 1317 | // populate environment |
1317 | 1318 | ExecutionPhase::<T>::put(Phase::Initialization); |
1318 | 1319 | storage::unhashed::put(well_known_keys::EXTRINSIC_INDEX, &0u32); |
| 1320 | + let entropy = (b"frame_system::initialize", parent_hash).using_encoded(blake2_256); |
| 1321 | + storage::unhashed::put(well_known_keys::INTRABLOCK_ENTROPY, &entropy[..]); |
1319 | 1322 | <Number<T>>::put(number); |
1320 | 1323 | <Digest<T>>::put(digest); |
1321 | 1324 | <ParentHash<T>>::put(parent_hash); |
@@ -1365,6 +1368,7 @@ impl<T: Config> Pallet<T> { |
1365 | 1368 | ); |
1366 | 1369 | ExecutionPhase::<T>::kill(); |
1367 | 1370 | AllExtrinsicsLen::<T>::kill(); |
| 1371 | + storage::unhashed::kill(well_known_keys::INTRABLOCK_ENTROPY); |
1368 | 1372 |
|
1369 | 1373 | // The following fields |
1370 | 1374 | // |
@@ -1633,6 +1637,16 @@ impl<T: Config> Pallet<T> { |
1633 | 1637 | } |
1634 | 1638 | } |
1635 | 1639 |
|
| 1640 | +/// Returns a 32 byte datum which is guaranteed to be universally unique. `entropy` is provided |
| 1641 | +/// as a facility to reduce the potential for precalculating results. |
| 1642 | +pub fn unique(entropy: impl Encode) -> [u8; 32] { |
| 1643 | + let mut last = [0u8; 32]; |
| 1644 | + sp_io::storage::read(well_known_keys::INTRABLOCK_ENTROPY, &mut last[..], 0); |
| 1645 | + let next = (b"frame_system::unique", entropy, last).using_encoded(blake2_256); |
| 1646 | + sp_io::storage::set(well_known_keys::INTRABLOCK_ENTROPY, &next.encode()); |
| 1647 | + next |
| 1648 | +} |
| 1649 | + |
1636 | 1650 | /// Event handler which registers a provider when created. |
1637 | 1651 | pub struct Provider<T>(PhantomData<T>); |
1638 | 1652 | impl<T: Config> HandleLifetime<T::AccountId> for Provider<T> { |
|
0 commit comments