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

Commit ee62cfe

Browse files
committed
Merge remote-tracking branch 'origin/master' into gav-unique-hash-tweak
2 parents 93e2cf1 + 9dbf60c commit ee62cfe

File tree

5 files changed

+41
-3
lines changed

5 files changed

+41
-3
lines changed

frame/assets/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1654,7 +1654,7 @@ pub mod pallet {
16541654
impl<T: Config<I>, I: 'static> AccountTouch<T::AssetId, T::AccountId> for Pallet<T, I> {
16551655
type Balance = DepositBalanceOf<T, I>;
16561656

1657-
fn deposit_required() -> Self::Balance {
1657+
fn deposit_required(_: T::AssetId) -> Self::Balance {
16581658
T::AssetAccountDeposit::get()
16591659
}
16601660

frame/support/src/traits/misc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,8 +1160,8 @@ pub trait AccountTouch<AssetId, AccountId> {
11601160
/// The type for currency units of the deposit.
11611161
type Balance;
11621162

1163-
/// The deposit amount of a native currency required for creating an asset account.
1164-
fn deposit_required() -> Self::Balance;
1163+
/// The deposit amount of a native currency required for creating an account of the `asset`.
1164+
fn deposit_required(asset: AssetId) -> Self::Balance;
11651165

11661166
/// Create an account for `who` of the `asset` with a deposit taken from the `depositor`.
11671167
fn touch(asset: AssetId, who: AccountId, depositor: AccountId) -> DispatchResult;

frame/system/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666

6767
#[cfg(feature = "std")]
6868
use serde::Serialize;
69+
use sp_io::hashing::blake2_256;
6970
#[cfg(feature = "runtime-benchmarks")]
7071
use sp_runtime::traits::TrailingZeroInput;
7172
use sp_runtime::{
@@ -1316,6 +1317,8 @@ impl<T: Config> Pallet<T> {
13161317
// populate environment
13171318
ExecutionPhase::<T>::put(Phase::Initialization);
13181319
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[..]);
13191322
<Number<T>>::put(number);
13201323
<Digest<T>>::put(digest);
13211324
<ParentHash<T>>::put(parent_hash);
@@ -1365,6 +1368,7 @@ impl<T: Config> Pallet<T> {
13651368
);
13661369
ExecutionPhase::<T>::kill();
13671370
AllExtrinsicsLen::<T>::kill();
1371+
storage::unhashed::kill(well_known_keys::INTRABLOCK_ENTROPY);
13681372

13691373
// The following fields
13701374
//
@@ -1633,6 +1637,16 @@ impl<T: Config> Pallet<T> {
16331637
}
16341638
}
16351639

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+
16361650
/// Event handler which registers a provider when created.
16371651
pub struct Provider<T>(PhantomData<T>);
16381652
impl<T: Config> HandleLifetime<T::AccountId> for Provider<T> {

frame/system/src/tests.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,27 @@ fn origin_works() {
3434
assert_eq!(x.unwrap(), RawOrigin::<u64>::Signed(1u64));
3535
}
3636

37+
#[test]
38+
fn unique_datum_works() {
39+
new_test_ext().execute_with(|| {
40+
System::initialize(&1, &[0u8; 32].into(), &Default::default());
41+
assert!(sp_io::storage::exists(well_known_keys::INTRABLOCK_ENTROPY));
42+
43+
let h1 = unique(b"");
44+
let h2 = unique(b"");
45+
assert_ne!(h1, h2);
46+
47+
let h3 = unique(b"Hello");
48+
assert_ne!(h2, h3);
49+
50+
let h4 = unique(b"Hello");
51+
assert_ne!(h3, h4);
52+
53+
System::finalize();
54+
assert!(!sp_io::storage::exists(well_known_keys::INTRABLOCK_ENTROPY));
55+
});
56+
}
57+
3758
#[test]
3859
fn stored_map_works() {
3960
new_test_ext().execute_with(|| {

primitives/storage/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ pub mod well_known_keys {
204204
/// Encodes to `0x3a65787472696e7369635f696e646578`.
205205
pub const EXTRINSIC_INDEX: &[u8] = b":extrinsic_index";
206206

207+
/// Current intra-block entropy (a universally unique `[u8; 32]` value) is stored here.
208+
pub const INTRABLOCK_ENTROPY: &[u8] = b":intrablock_entropy";
209+
207210
/// Prefix of child storage keys.
208211
pub const CHILD_STORAGE_KEY_PREFIX: &[u8] = b":child_storage:";
209212

0 commit comments

Comments
 (0)