Introduce function into frame System (#14149)

This commit is contained in:
Gavin Wood
2023-05-15 18:11:33 +01:00
committed by GitHub
parent 0a105eed6f
commit f30e6df370
3 changed files with 38 additions and 0 deletions
+14
View File
@@ -66,6 +66,7 @@
#[cfg(feature = "std")]
use serde::Serialize;
use sp_io::hashing::blake2_256;
#[cfg(feature = "runtime-benchmarks")]
use sp_runtime::traits::TrailingZeroInput;
use sp_runtime::{
@@ -1316,6 +1317,8 @@ impl<T: Config> Pallet<T> {
// populate environment
ExecutionPhase::<T>::put(Phase::Initialization);
storage::unhashed::put(well_known_keys::EXTRINSIC_INDEX, &0u32);
let entropy = (b"frame_system::initialize", parent_hash).using_encoded(blake2_256);
storage::unhashed::put(well_known_keys::INTRABLOCK_ENTROPY, &entropy[..]);
<Number<T>>::put(number);
<Digest<T>>::put(digest);
<ParentHash<T>>::put(parent_hash);
@@ -1365,6 +1368,7 @@ impl<T: Config> Pallet<T> {
);
ExecutionPhase::<T>::kill();
AllExtrinsicsLen::<T>::kill();
storage::unhashed::kill(well_known_keys::INTRABLOCK_ENTROPY);
// The following fields
//
@@ -1633,6 +1637,16 @@ impl<T: Config> Pallet<T> {
}
}
/// Returns a 32 byte datum which is guaranteed to be universally unique. `entropy` is provided
/// as a facility to reduce the potential for precalculating results.
pub fn unique(entropy: impl Encode) -> [u8; 32] {
let mut last = [0u8; 32];
sp_io::storage::read(well_known_keys::INTRABLOCK_ENTROPY, &mut last[..], 0);
let next = (b"frame_system::unique", entropy, last).using_encoded(blake2_256);
sp_io::storage::set(well_known_keys::INTRABLOCK_ENTROPY, &next.encode());
next
}
/// Event handler which registers a provider when created.
pub struct Provider<T>(PhantomData<T>);
impl<T: Config> HandleLifetime<T::AccountId> for Provider<T> {