Skip to content

Commit

Permalink
frame-system: uniques remove one encode call (paritytech#14154)
Browse files Browse the repository at this point in the history
* frame-system: `uniques` remove one `encode` call

* ".git/.scripts/commands/fmt/fmt.sh"

---------

Co-authored-by: command-bot <>
  • Loading branch information
bkchr authored and nathanwhit committed Jul 19, 2023
1 parent 5c2fe95 commit 9a935b9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
22 changes: 11 additions & 11 deletions frame/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1618,21 +1618,21 @@ impl<T: Config> Pallet<T> {
.ok_or(Error::<T>::FailedToExtractRuntimeVersion)?;

cfg_if::cfg_if! {
if #[cfg(all(feature = "runtime-benchmarks", not(test)))] {
if #[cfg(all(feature = "runtime-benchmarks", not(test)))] {
// Let's ensure the compiler doesn't optimize our fetching of the runtime version away.
core::hint::black_box((new_version, current_version));
Ok(())
} else {
if new_version.spec_name != current_version.spec_name {
return Err(Error::<T>::InvalidSpecName.into())
}
} else {
if new_version.spec_name != current_version.spec_name {
return Err(Error::<T>::InvalidSpecName.into())
}

if new_version.spec_version <= current_version.spec_version {
return Err(Error::<T>::SpecVersionNeedsToIncrease.into())
}
if new_version.spec_version <= current_version.spec_version {
return Err(Error::<T>::SpecVersionNeedsToIncrease.into())
}

Ok(())
}
Ok(())
}
}
}
}
Expand All @@ -1643,7 +1643,7 @@ 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());
sp_io::storage::set(well_known_keys::INTRABLOCK_ENTROPY, &next);
next
}

Expand Down
16 changes: 16 additions & 0 deletions frame/system/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,29 @@ fn unique_datum_works() {
assert!(sp_io::storage::exists(well_known_keys::INTRABLOCK_ENTROPY));

let h1 = unique(b"");
assert_eq!(
32,
sp_io::storage::read(well_known_keys::INTRABLOCK_ENTROPY, &mut [], 0).unwrap()
);
let h2 = unique(b"");
assert_eq!(
32,
sp_io::storage::read(well_known_keys::INTRABLOCK_ENTROPY, &mut [], 0).unwrap()
);
assert_ne!(h1, h2);

let h3 = unique(b"Hello");
assert_eq!(
32,
sp_io::storage::read(well_known_keys::INTRABLOCK_ENTROPY, &mut [], 0).unwrap()
);
assert_ne!(h2, h3);

let h4 = unique(b"Hello");
assert_eq!(
32,
sp_io::storage::read(well_known_keys::INTRABLOCK_ENTROPY, &mut [], 0).unwrap()
);
assert_ne!(h3, h4);

System::finalize();
Expand Down

0 comments on commit 9a935b9

Please sign in to comment.