Reduce usage of Blake2Hasher (#5132)

This reduces the usage of `Blake2Hasher` in the code base and replaces
it with `BlakeTwo256`. The most important change is the removal of the
custom extern function for `Blake2Hasher`. The runtime `Hash` trait is
now also simplified and directly requires that the implementing type
implements `Hashable`.
This commit is contained in:
Benjamin Kampmann
2020-03-05 08:51:03 +01:00
committed by GitHub
parent 406fa981bb
commit 5a33228ea9
64 changed files with 372 additions and 451 deletions
+8 -11
View File
@@ -21,13 +21,11 @@ use frame_support::{
weights::{GetDispatchInfo, DispatchInfo, DispatchClass},
};
use sp_core::{
Blake2Hasher, NeverNativeValue, map,
traits::Externalities,
storage::{well_known_keys, Storage},
NeverNativeValue, map, traits::Externalities, storage::{well_known_keys, Storage},
};
use sp_runtime::{
ApplyExtrinsicResult, Fixed64,
traits::{Hash as HashT, Convert},
traits::{Hash as HashT, Convert, BlakeTwo256},
transaction_validity::InvalidTransaction,
};
use pallet_contracts::ContractAddressFor;
@@ -93,7 +91,6 @@ fn changes_trie_block() -> (Vec<u8>, Hash) {
)
}
/// block 1 and 2 must be created together to ensure transactions are only signed once (since they
/// are not guaranteed to be deterministic) and to ensure that the correct state is propagated
/// from block1's execution to block2 to derive the correct storage_root.
@@ -161,7 +158,7 @@ fn block_with_size(time: u64, nonce: u32, size: usize) -> (Vec<u8>, Hash) {
#[test]
fn panic_execution_with_foreign_code_gives_error() {
let mut t = TestExternalities::<Blake2Hasher>::new_with_code(BLOATY_CODE, Storage {
let mut t = TestExternalities::<BlakeTwo256>::new_with_code(BLOATY_CODE, Storage {
top: map![
<frame_system::Account<Runtime>>::hashed_key_for(alice()) => {
(69u128, 0u8, 0u128, 0u128, 0u128).encode()
@@ -197,7 +194,7 @@ fn panic_execution_with_foreign_code_gives_error() {
#[test]
fn bad_extrinsic_with_native_equivalent_code_gives_error() {
let mut t = TestExternalities::<Blake2Hasher>::new_with_code(COMPACT_CODE, Storage {
let mut t = TestExternalities::<BlakeTwo256>::new_with_code(COMPACT_CODE, Storage {
top: map![
<frame_system::Account<Runtime>>::hashed_key_for(alice()) => {
(0u32, 0u8, 69u128, 0u128, 0u128, 0u128).encode()
@@ -233,7 +230,7 @@ fn bad_extrinsic_with_native_equivalent_code_gives_error() {
#[test]
fn successful_execution_with_native_equivalent_code_gives_ok() {
let mut t = TestExternalities::<Blake2Hasher>::new_with_code(COMPACT_CODE, Storage {
let mut t = TestExternalities::<BlakeTwo256>::new_with_code(COMPACT_CODE, Storage {
top: map![
<frame_system::Account<Runtime>>::hashed_key_for(alice()) => {
(0u32, 0u8, 111 * DOLLARS, 0u128, 0u128, 0u128).encode()
@@ -275,7 +272,7 @@ fn successful_execution_with_native_equivalent_code_gives_ok() {
#[test]
fn successful_execution_with_foreign_code_gives_ok() {
let mut t = TestExternalities::<Blake2Hasher>::new_with_code(BLOATY_CODE, Storage {
let mut t = TestExternalities::<BlakeTwo256>::new_with_code(BLOATY_CODE, Storage {
top: map![
<frame_system::Account<Runtime>>::hashed_key_for(alice()) => {
(0u32, 0u8, 111 * DOLLARS, 0u128, 0u128, 0u128).encode()
@@ -700,7 +697,7 @@ fn native_big_block_import_fails_on_fallback() {
#[test]
fn panic_execution_gives_error() {
let mut t = TestExternalities::<Blake2Hasher>::new_with_code(BLOATY_CODE, Storage {
let mut t = TestExternalities::<BlakeTwo256>::new_with_code(BLOATY_CODE, Storage {
top: map![
<pallet_balances::TotalIssuance<Runtime>>::hashed_key().to_vec() => {
0_u128.encode()
@@ -731,7 +728,7 @@ fn panic_execution_gives_error() {
#[test]
fn successful_execution_gives_ok() {
let mut t = TestExternalities::<Blake2Hasher>::new_with_code(COMPACT_CODE, Storage {
let mut t = TestExternalities::<BlakeTwo256>::new_with_code(COMPACT_CODE, Storage {
top: map![
<frame_system::Account<Runtime>>::hashed_key_for(alice()) => {
(0u32, 0u8, 111 * DOLLARS, 0u128, 0u128, 0u128).encode()
+6 -8
View File
@@ -17,10 +17,8 @@
use codec::{Encode, Decode};
use frame_support::Hashable;
use sp_state_machine::TestExternalities as CoreTestExternalities;
use sp_core::{
Blake2Hasher, NeverNativeValue, NativeOrEncoded, traits::{CodeExecutor, RuntimeCode},
};
use sp_runtime::{ApplyExtrinsicResult, traits::Header as HeaderT};
use sp_core::{NeverNativeValue, NativeOrEncoded, traits::{CodeExecutor, RuntimeCode}};
use sp_runtime::{ApplyExtrinsicResult, traits::{Header as HeaderT, BlakeTwo256}};
use sc_executor::{NativeExecutor, WasmExecutionMethod};
use sc_executor::error::Result;
@@ -66,7 +64,7 @@ pub fn executor_call<
R:Decode + Encode + PartialEq,
NC: FnOnce() -> std::result::Result<R, String> + std::panic::UnwindSafe
>(
t: &mut TestExternalities<Blake2Hasher>,
t: &mut TestExternalities<BlakeTwo256>,
method: &str,
data: &[u8],
use_native: bool,
@@ -85,7 +83,7 @@ pub fn executor_call<
)
}
pub fn new_test_ext(code: &[u8], support_changes_trie: bool) -> TestExternalities<Blake2Hasher> {
pub fn new_test_ext(code: &[u8], support_changes_trie: bool) -> TestExternalities<BlakeTwo256> {
let mut ext = TestExternalities::new_with_code(
code,
node_testing::genesis::config(support_changes_trie, Some(code)).build_storage().unwrap(),
@@ -99,7 +97,7 @@ pub fn new_test_ext(code: &[u8], support_changes_trie: bool) -> TestExternalitie
/// `extrinsics` must be a list of valid extrinsics, i.e. none of the extrinsics for example
/// can report `ExhaustResources`. Otherwise, this function panics.
pub fn construct_block(
env: &mut TestExternalities<Blake2Hasher>,
env: &mut TestExternalities<BlakeTwo256>,
number: BlockNumber,
parent_hash: Hash,
extrinsics: Vec<CheckedExtrinsic>,
@@ -111,7 +109,7 @@ pub fn construct_block(
// calculate the header fields that we can.
let extrinsics_root =
Layout::<Blake2Hasher>::ordered_trie_root(extrinsics.iter().map(Encode::encode))
Layout::<BlakeTwo256>::ordered_trie_root(extrinsics.iter().map(Encode::encode))
.to_fixed_bytes()
.into();
+3 -9
View File
@@ -20,14 +20,8 @@ use frame_support::{
traits::Currency,
weights::GetDispatchInfo,
};
use sp_core::{
Blake2Hasher, NeverNativeValue, map,
storage::Storage,
};
use sp_runtime::{
Fixed64, Perbill,
traits::Convert,
};
use sp_core::{NeverNativeValue, map, storage::Storage};
use sp_runtime::{Fixed64, Perbill, traits::{Convert, BlakeTwo256}};
use node_runtime::{
CheckedExtrinsic, Call, Runtime, Balances, TransactionPayment, TransactionBaseFee,
TransactionByteFee, WeightFeeCoefficient,
@@ -136,7 +130,7 @@ fn transaction_fee_is_correct_ultimate() {
// - 1 MILLICENTS in substrate node.
// - 1 milli-dot based on current polkadot runtime.
// (this baed on assigning 0.1 CENT to the cheapest tx with `weight = 100`)
let mut t = TestExternalities::<Blake2Hasher>::new_with_code(COMPACT_CODE, Storage {
let mut t = TestExternalities::<BlakeTwo256>::new_with_code(COMPACT_CODE, Storage {
top: map![
<frame_system::Account<Runtime>>::hashed_key_for(alice()) => {
(0u32, 0u8, 100 * DOLLARS, 0 * DOLLARS, 0 * DOLLARS, 0 * DOLLARS).encode()