Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Introduce function into frame System (#14149)
Browse files Browse the repository at this point in the history
  • Loading branch information
gavofyork authored May 15, 2023
1 parent e4b08db commit 9dbf60c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
14 changes: 14 additions & 0 deletions frame/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
//
Expand Down Expand Up @@ -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> {
Expand Down
21 changes: 21 additions & 0 deletions frame/system/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,27 @@ fn origin_works() {
assert_eq!(x.unwrap(), RawOrigin::<u64>::Signed(1u64));
}

#[test]
fn unique_datum_works() {
new_test_ext().execute_with(|| {
System::initialize(&1, &[0u8; 32].into(), &Default::default());
assert!(sp_io::storage::exists(well_known_keys::INTRABLOCK_ENTROPY));

let h1 = unique(b"");
let h2 = unique(b"");
assert_ne!(h1, h2);

let h3 = unique(b"Hello");
assert_ne!(h2, h3);

let h4 = unique(b"Hello");
assert_ne!(h3, h4);

System::finalize();
assert!(!sp_io::storage::exists(well_known_keys::INTRABLOCK_ENTROPY));
});
}

#[test]
fn stored_map_works() {
new_test_ext().execute_with(|| {
Expand Down
3 changes: 3 additions & 0 deletions primitives/storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ pub mod well_known_keys {
/// Encodes to `0x3a65787472696e7369635f696e646578`.
pub const EXTRINSIC_INDEX: &[u8] = b":extrinsic_index";

/// Current intra-block entropy (a universally unique `[u8; 32]` value) is stored here.
pub const INTRABLOCK_ENTROPY: &[u8] = b":intrablock_entropy";

/// Prefix of child storage keys.
pub const CHILD_STORAGE_KEY_PREFIX: &[u8] = b":child_storage:";

Expand Down

0 comments on commit 9dbf60c

Please sign in to comment.