diff --git a/core/client/src/client.rs b/core/client/src/client.rs index 041933cfffaff..dbf130f733246 100644 --- a/core/client/src/client.rs +++ b/core/client/src/client.rs @@ -1534,7 +1534,7 @@ impl backend::AuxStore for Client pub(crate) mod tests { use std::collections::HashMap; use super::*; - use primitives::twox_128; + use primitives::blake2_256; use runtime_primitives::traits::DigestItem as DigestItemT; use runtime_primitives::generic::DigestItem; use test_client::{self, TestClient, AccountKeyring}; @@ -1584,12 +1584,12 @@ pub(crate) mod tests { } // prepare test cases - let alice = twox_128(&runtime::system::balance_of_key(AccountKeyring::Alice.into())).to_vec(); - let bob = twox_128(&runtime::system::balance_of_key(AccountKeyring::Bob.into())).to_vec(); - let charlie = twox_128(&runtime::system::balance_of_key(AccountKeyring::Charlie.into())).to_vec(); - let dave = twox_128(&runtime::system::balance_of_key(AccountKeyring::Dave.into())).to_vec(); - let eve = twox_128(&runtime::system::balance_of_key(AccountKeyring::Eve.into())).to_vec(); - let ferdie = twox_128(&runtime::system::balance_of_key(AccountKeyring::Ferdie.into())).to_vec(); + let alice = blake2_256(&runtime::system::balance_of_key(AccountKeyring::Alice.into())).to_vec(); + let bob = blake2_256(&runtime::system::balance_of_key(AccountKeyring::Bob.into())).to_vec(); + let charlie = blake2_256(&runtime::system::balance_of_key(AccountKeyring::Charlie.into())).to_vec(); + let dave = blake2_256(&runtime::system::balance_of_key(AccountKeyring::Dave.into())).to_vec(); + let eve = blake2_256(&runtime::system::balance_of_key(AccountKeyring::Eve.into())).to_vec(); + let ferdie = blake2_256(&runtime::system::balance_of_key(AccountKeyring::Ferdie.into())).to_vec(); let test_cases = vec![ (1, 4, alice.clone(), vec![(4, 0), (1, 0)]), (1, 3, alice.clone(), vec![(1, 0)]), diff --git a/core/client/src/light/fetcher.rs b/core/client/src/light/fetcher.rs index 4cbbc819b3141..7ddd6ee2f6d1a 100644 --- a/core/client/src/light/fetcher.rs +++ b/core/client/src/light/fetcher.rs @@ -404,7 +404,7 @@ pub mod tests { use crate::light::fetcher::{Fetcher, FetchChecker, LightDataChecker, RemoteCallRequest, RemoteHeaderRequest}; use crate::light::blockchain::tests::{DummyStorage, DummyBlockchain}; - use primitives::{twox_128, Blake2Hasher}; + use primitives::{blake2_256, Blake2Hasher}; use primitives::storage::{StorageKey, well_known_keys}; use runtime_primitives::generic::BlockId; use state_machine::Backend; @@ -587,7 +587,7 @@ pub mod tests { // we're testing this test case here: // (1, 4, dave.clone(), vec![(4, 0), (1, 1), (1, 0)]), let (remote_client, remote_roots, _) = prepare_client_with_key_changes(); - let dave = twox_128(&runtime::system::balance_of_key(AccountKeyring::Dave.into())).to_vec(); + let dave = blake2_256(&runtime::system::balance_of_key(AccountKeyring::Dave.into())).to_vec(); let dave = StorageKey(dave); // 'fetch' changes proof from remote node: @@ -699,7 +699,7 @@ pub mod tests { let (remote_client, remote_roots, _) = prepare_client_with_key_changes(); let local_cht_root = cht::compute_root::( 4, 0, remote_roots.iter().cloned().map(|ct| Ok(Some(ct)))).unwrap(); - let dave = twox_128(&runtime::system::balance_of_key(AccountKeyring::Dave.into())).to_vec(); + let dave = blake2_256(&runtime::system::balance_of_key(AccountKeyring::Dave.into())).to_vec(); let dave = StorageKey(dave); // 'fetch' changes proof from remote node: diff --git a/core/executor/src/wasm_executor.rs b/core/executor/src/wasm_executor.rs index 42af29e9bae03..d3fab07fc16a1 100644 --- a/core/executor/src/wasm_executor.rs +++ b/core/executor/src/wasm_executor.rs @@ -28,7 +28,7 @@ use wasmi::memory_units::{Pages}; use state_machine::Externalities; use crate::error::{Error, ErrorKind, Result}; use crate::wasm_utils::UserError; -use primitives::{blake2_256, twox_128, twox_256, ed25519, sr25519, Pair}; +use primitives::{blake2_128, blake2_256, twox_64, twox_128, twox_256, ed25519, sr25519, Pair}; use primitives::hexdisplay::HexDisplay; use primitives::sandbox as sandbox_primitives; use primitives::{H256, Blake2Hasher}; @@ -418,6 +418,30 @@ impl_function_executor!(this: FunctionExecutor<'e, E>, ext_chain_id() -> u64 => { Ok(this.ext.chain_id()) }, + ext_twox_64(data: *const u8, len: u32, out: *mut u8) => { + let result: [u8; 8] = if len == 0 { + let hashed = twox_64(&[0u8; 0]); + debug_trace!(target: "xxhash", "XXhash: '' -> {}", HexDisplay::from(&hashed)); + this.hash_lookup.insert(hashed.to_vec(), vec![]); + hashed + } else { + let key = this.memory.get(data, len as usize).map_err(|_| UserError("Invalid attempt to get key in ext_twox_64"))?; + let hashed_key = twox_64(&key); + debug_trace!(target: "xxhash", "XXhash: {} -> {}", + if let Ok(_skey) = ::std::str::from_utf8(&key) { + _skey + } else { + &format!("{}", HexDisplay::from(&key)) + }, + HexDisplay::from(&hashed_key) + ); + this.hash_lookup.insert(hashed_key.to_vec(), key); + hashed_key + }; + + this.memory.set(out, &result).map_err(|_| UserError("Invalid attempt to set result in ext_twox_64"))?; + Ok(()) + }, ext_twox_128(data: *const u8, len: u32, out: *mut u8) => { let result: [u8; 16] = if len == 0 { let hashed = twox_128(&[0u8; 0]); @@ -451,6 +475,21 @@ impl_function_executor!(this: FunctionExecutor<'e, E>, this.memory.set(out, &result).map_err(|_| UserError("Invalid attempt to set result in ext_twox_256"))?; Ok(()) }, + ext_blake2_128(data: *const u8, len: u32, out: *mut u8) => { + let result: [u8; 16] = if len == 0 { + let hashed = blake2_128(&[0u8; 0]); + this.hash_lookup.insert(hashed.to_vec(), vec![]); + hashed + } else { + let key = this.memory.get(data, len as usize).map_err(|_| UserError("Invalid attempt to get key in ext_blake2_128"))?; + let hashed_key = blake2_128(&key); + this.hash_lookup.insert(hashed_key.to_vec(), key); + hashed_key + }; + + this.memory.set(out, &result).map_err(|_| UserError("Invalid attempt to set result in ext_blake2_128"))?; + Ok(()) + }, ext_blake2_256(data: *const u8, len: u32, out: *mut u8) => { let result: [u8; 32] = if len == 0 { blake2_256(&[0u8; 0]) @@ -910,6 +949,21 @@ mod tests { ); } + #[test] + fn blake2_128_should_work() { + let mut ext = TestExternalities::default(); + let test_code = include_bytes!("../wasm/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm"); + assert_eq!( + WasmExecutor::new().call(&mut ext, 8, &test_code[..], "test_blake2_128", &[]).unwrap(), + blake2_128(&b""[..]).encode() + ); + assert_eq!( + WasmExecutor::new().call(&mut ext, 8, &test_code[..], "test_blake2_128", b"Hello world!").unwrap(), + blake2_128(&b"Hello world!"[..]).encode() + ); + } + + #[test] fn twox_256_should_work() { let mut ext = TestExternalities::default(); diff --git a/core/executor/wasm/src/lib.rs b/core/executor/wasm/src/lib.rs index dda9c617333a8..294c21d146ee0 100644 --- a/core/executor/wasm/src/lib.rs +++ b/core/executor/wasm/src/lib.rs @@ -7,7 +7,7 @@ use alloc::vec::Vec; use alloc::slice; use runtime_io::{ - set_storage, storage, clear_prefix, print, blake2_256, + set_storage, storage, clear_prefix, print, blake2_128, blake2_256, twox_128, twox_256, ed25519_verify, sr25519_verify, enumerated_trie_root }; @@ -68,6 +68,7 @@ impl_stubs!( input.to_vec() }, test_blake2_256 => |input| blake2_256(input).to_vec(), + test_blake2_128 => |input| blake2_128(input).to_vec(), test_twox_256 => |input| twox_256(input).to_vec(), test_twox_128 => |input| twox_128(input).to_vec(), test_ed25519_verify => |input: &[u8]| { diff --git a/core/primitives/Cargo.toml b/core/primitives/Cargo.toml index d3222a5ea4609..6a03aa3d394e6 100644 --- a/core/primitives/Cargo.toml +++ b/core/primitives/Cargo.toml @@ -34,6 +34,8 @@ regex = {version = "1.1", optional = true } substrate-serializer = { path = "../serializer" } pretty_assertions = "0.5" heapsize = "0.4" +hex-literal = "0.1" +rand = "0.6" [features] default = ["std"] diff --git a/core/primitives/benches/benches.rs b/core/primitives/benches/benches.rs new file mode 100644 index 0000000000000..b81ef9dc4252d --- /dev/null +++ b/core/primitives/benches/benches.rs @@ -0,0 +1,60 @@ +// Copyright 2019 Parity Technologies +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// TODO: Move benchmark to criterion #2354 +#![feature(test)] + +extern crate test; +use hex_literal::{hex, hex_impl}; +use substrate_primitives::hashing::{twox_128, blake2_128}; + + +const MAX_KEY_SIZE: u32 = 32; + +fn data_set() -> Vec> { + use rand::SeedableRng; + use rand::Rng; + + let rnd: [u8; 32] = rand::rngs::StdRng::seed_from_u64(12).gen(); + let mut rnd = rnd.iter().cycle(); + let mut res = Vec::new(); + for size in 1..=MAX_KEY_SIZE { + for _ in 0..1_000 { + let value = (0..size) + .map(|_| rnd.next().unwrap().clone()) + .collect(); + res.push(value); + } + } + res +} + +fn bench_hash_128(b: &mut test::Bencher, f: &Fn(&[u8]) -> [u8; 16]) { + let data_set = data_set(); + b.iter(|| { + for data in &data_set { + let _a = f(data); + } + }); +} + +#[bench] +fn bench_blake2_128(b: &mut test::Bencher) { + bench_hash_128(b, &blake2_128); +} + +#[bench] +fn bench_twox_128(b: &mut test::Bencher) { + bench_hash_128(b, &twox_128); +} diff --git a/core/primitives/src/hashing.rs b/core/primitives/src/hashing.rs index 814048fea848d..87312ce6e46e9 100644 --- a/core/primitives/src/hashing.rs +++ b/core/primitives/src/hashing.rs @@ -55,6 +55,23 @@ pub fn blake2_128(data: &[u8]) -> [u8; 16] { r } +/// Do a XX 64-bit hash and place result in `dest`. +pub fn twox_64_into(data: &[u8], dest: &mut [u8; 8]) { + use ::core::hash::Hasher; + let mut h0 = twox_hash::XxHash::with_seed(0); + h0.write(data); + let r0 = h0.finish(); + use byteorder::{ByteOrder, LittleEndian}; + LittleEndian::write_u64(&mut dest[0..8], r0); +} + +/// Do a XX 64-bit hash and return result. +pub fn twox_64(data: &[u8]) -> [u8; 8] { + let mut r: [u8; 8] = [0; 8]; + twox_64_into(data, &mut r); + r +} + /// Do a XX 128-bit hash and place result in `dest`. pub fn twox_128_into(data: &[u8], dest: &mut [u8; 16]) { use ::core::hash::Hasher; diff --git a/core/primitives/src/lib.rs b/core/primitives/src/lib.rs index f078b5446f53b..f800f096db6c3 100644 --- a/core/primitives/src/lib.rs +++ b/core/primitives/src/lib.rs @@ -46,7 +46,7 @@ pub use impl_serde::serialize as bytes; #[cfg(feature = "std")] pub mod hashing; #[cfg(feature = "std")] -pub use hashing::{blake2_256, twox_128, twox_256}; +pub use hashing::{blake2_128, blake2_256, twox_64, twox_128, twox_256}; #[cfg(feature = "std")] pub mod hexdisplay; pub mod crypto; diff --git a/core/rpc/src/state/tests.rs b/core/rpc/src/state/tests.rs index a63b8489eb090..dee64de13442e 100644 --- a/core/rpc/src/state/tests.rs +++ b/core/rpc/src/state/tests.rs @@ -17,7 +17,7 @@ use super::*; use self::error::{Error, ErrorKind}; -use sr_io::twox_128; +use sr_io::blake2_256; use assert_matches::assert_matches; use consensus::BlockOrigin; use test_client::{self, runtime, AccountKeyring, TestClient, BlockBuilderExt}; @@ -88,7 +88,7 @@ fn should_send_initial_storage_changes_and_notifications() { { let api = State::new(Arc::new(test_client::new()), Subscriptions::new(remote)); - let alice_balance_key = twox_128(&test_runtime::system::balance_of_key(AccountKeyring::Alice.into())); + let alice_balance_key = blake2_256(&test_runtime::system::balance_of_key(AccountKeyring::Alice.into())); api.subscribe_storage(Default::default(), subscriber, Some(vec![ StorageKey(alice_balance_key.to_vec()), @@ -147,7 +147,7 @@ fn should_query_storage() { let block2_hash = add_block(1); let genesis_hash = client.genesis_hash(); - let alice_balance_key = twox_128(&test_runtime::system::balance_of_key(AccountKeyring::Alice.into())); + let alice_balance_key = blake2_256(&test_runtime::system::balance_of_key(AccountKeyring::Alice.into())); let mut expected = vec![ StorageChangeSet { diff --git a/core/sr-io/with_std.rs b/core/sr-io/with_std.rs index 1f4ce56fc9b0e..d694b1457d2c1 100644 --- a/core/sr-io/with_std.rs +++ b/core/sr-io/with_std.rs @@ -18,8 +18,8 @@ pub use parity_codec as codec; // re-export hashing functions. pub use primitives::{ - blake2_256, twox_128, twox_256, ed25519, Blake2Hasher, sr25519, - Pair + blake2_128, blake2_256, twox_128, twox_256, twox_64, ed25519, Blake2Hasher, + sr25519, Pair }; pub use tiny_keccak::keccak256 as keccak_256; // Switch to this after PoC-3 diff --git a/core/sr-io/without_std.rs b/core/sr-io/without_std.rs index 08b1a3c70a525..456ae3cef7ee3 100644 --- a/core/sr-io/without_std.rs +++ b/core/sr-io/without_std.rs @@ -273,7 +273,9 @@ extern_functions! { /// Hash calculation and verification fn ext_blake2_256_enumerated_trie_root(values_data: *const u8, lens_data: *const u32, lens_len: u32, result: *mut u8); + fn ext_blake2_128(data: *const u8, len: u32, out: *mut u8); fn ext_blake2_256(data: *const u8, len: u32, out: *mut u8); + fn ext_twox_64(data: *const u8, len: u32, out: *mut u8); fn ext_twox_128(data: *const u8, len: u32, out: *mut u8); fn ext_twox_256(data: *const u8, len: u32, out: *mut u8); fn ext_keccak_256(data: *const u8, len: u32, out: *mut u8); @@ -544,6 +546,15 @@ pub fn blake2_256(data: &[u8]) -> [u8; 32] { result } +/// Conduct a 128-bit Blake2 hash. +pub fn blake2_128(data: &[u8]) -> [u8; 16] { + let mut result: [u8; 16] = Default::default(); + unsafe { + ext_blake2_128.get()(data.as_ptr(), data.len() as u32, result.as_mut_ptr()); + } + result +} + /// Conduct a 256-bit Keccak hash. pub fn keccak_256(data: &[u8]) -> [u8; 32] { let mut result: [u8; 32] = Default::default(); @@ -571,6 +582,15 @@ pub fn twox_128(data: &[u8]) -> [u8; 16] { result } +/// Conduct two XX hashes to give a 64-bit result. +pub fn twox_64(data: &[u8]) -> [u8; 8] { + let mut result: [u8; 8] = Default::default(); + unsafe { + ext_twox_64.get()(data.as_ptr(), data.len() as u32, result.as_mut_ptr()); + } + result +} + /// Verify a ed25519 signature. pub fn ed25519_verify>(sig: &[u8; 64], msg: &[u8], pubkey: P) -> bool { unsafe { diff --git a/core/sr-primitives/src/lib.rs b/core/sr-primitives/src/lib.rs index 5167e57072a2b..f59ea101d651b 100644 --- a/core/sr-primitives/src/lib.rs +++ b/core/sr-primitives/src/lib.rs @@ -90,14 +90,6 @@ pub use serde_derive::{Serialize, Deserialize}; /// Complex storage builder stuff. #[cfg(feature = "std")] pub trait BuildStorage: Sized { - /// Hash given slice. - /// - /// Default to xx128 hashing. - fn hash(data: &[u8]) -> [u8; 16] { - let r = runtime_io::twox_128(data); - log::trace!(target: "build_storage", "{} <= {}", substrate_primitives::hexdisplay::HexDisplay::from(&r), ascii_format(data)); - r - } /// Build the storage out of this builder. fn build_storage(self) -> Result<(StorageOverlay, ChildrenStorageOverlay), String> { let mut storage = Default::default(); diff --git a/core/test-runtime/src/genesismap.rs b/core/test-runtime/src/genesismap.rs index 13e9e5ec9a55d..be1c784a52778 100644 --- a/core/test-runtime/src/genesismap.rs +++ b/core/test-runtime/src/genesismap.rs @@ -17,7 +17,7 @@ //! Tool for creating the genesis block. use std::collections::HashMap; -use runtime_io::twox_128; +use runtime_io::{blake2_256, twox_128}; use super::AccountId; use parity_codec::{Encode, KeyedVec, Joiner}; use primitives::{ChangesTrieConfiguration, map, storage::well_known_keys}; @@ -47,7 +47,7 @@ impl GenesisConfig { let wasm_runtime = include_bytes!("../wasm/target/wasm32-unknown-unknown/release/substrate_test_runtime.compact.wasm").to_vec(); let mut map: HashMap, Vec> = self.balances.iter() .map(|&(ref account, balance)| (account.to_keyed_vec(b"balance:"), vec![].and(&balance))) - .map(|(k, v)| (twox_128(&k[..])[..].to_vec(), v.to_vec())) + .map(|(k, v)| (blake2_256(&k[..])[..].to_vec(), v.to_vec())) .chain(vec![ (well_known_keys::CODE.into(), wasm_runtime), (well_known_keys::HEAP_PAGES.into(), vec![].and(&(16 as u64))), diff --git a/core/test-runtime/src/system.rs b/core/test-runtime/src/system.rs index 266b7130d4167..51f12966dc84e 100644 --- a/core/test-runtime/src/system.rs +++ b/core/test-runtime/src/system.rs @@ -18,7 +18,7 @@ //! and depositing logs. use rstd::prelude::*; -use runtime_io::{storage_root, enumerated_trie_root, storage_changes_root, twox_128}; +use runtime_io::{storage_root, enumerated_trie_root, storage_changes_root, twox_128, blake2_256}; use runtime_support::storage::{self, StorageValue, StorageMap}; use runtime_support::storage_items; use runtime_primitives::traits::{Hash as HashT, BlakeTwo256, Digest as DigestT}; @@ -45,11 +45,11 @@ pub fn balance_of_key(who: AccountId) -> Vec { } pub fn balance_of(who: AccountId) -> u64 { - storage::get_or(&balance_of_key(who), 0) + storage::hashed::get_or(&blake2_256, &balance_of_key(who), 0) } pub fn nonce_of(who: AccountId) -> u64 { - storage::get_or(&who.to_keyed_vec(NONCE_OF), 0) + storage::hashed::get_or(&blake2_256, &who.to_keyed_vec(NONCE_OF), 0) } /// Get authorities at given block. @@ -152,7 +152,7 @@ pub fn validate_transaction(utx: Extrinsic) -> TransactionValidity { let tx = utx.transfer(); let nonce_key = tx.from.to_keyed_vec(NONCE_OF); - let expected_nonce: u64 = storage::get_or(&nonce_key, 0); + let expected_nonce: u64 = storage::hashed::get_or(&blake2_256, &nonce_key, 0); if tx.nonce < expected_nonce { return TransactionValidity::Invalid(ApplyError::Stale as i8); } @@ -241,26 +241,26 @@ fn execute_transaction_backend(utx: &Extrinsic) -> ApplyResult { fn execute_transfer_backend(tx: &Transfer) -> ApplyResult { // check nonce let nonce_key = tx.from.to_keyed_vec(NONCE_OF); - let expected_nonce: u64 = storage::get_or(&nonce_key, 0); + let expected_nonce: u64 = storage::hashed::get_or(&blake2_256, &nonce_key, 0); if !(tx.nonce == expected_nonce) { return Err(ApplyError::Stale) } // increment nonce in storage - storage::put(&nonce_key, &(expected_nonce + 1)); + storage::hashed::put(&blake2_256, &nonce_key, &(expected_nonce + 1)); // check sender balance let from_balance_key = tx.from.to_keyed_vec(BALANCE_OF); - let from_balance: u64 = storage::get_or(&from_balance_key, 0); + let from_balance: u64 = storage::hashed::get_or(&blake2_256, &from_balance_key, 0); // enact transfer if !(tx.amount <= from_balance) { return Err(ApplyError::CantPay) } let to_balance_key = tx.to.to_keyed_vec(BALANCE_OF); - let to_balance: u64 = storage::get_or(&to_balance_key, 0); - storage::put(&from_balance_key, &(from_balance - tx.amount)); - storage::put(&to_balance_key, &(to_balance + tx.amount)); + let to_balance: u64 = storage::hashed::get_or(&blake2_256, &to_balance_key, 0); + storage::hashed::put(&blake2_256, &from_balance_key, &(from_balance - tx.amount)); + storage::hashed::put(&blake2_256, &to_balance_key, &(to_balance + tx.amount)); Ok(ApplyOutcome::Success) } @@ -295,7 +295,7 @@ fn info_expect_equal_hash(given: &Hash, expected: &Hash) { mod tests { use super::*; - use runtime_io::{with_externalities, twox_128, TestExternalities}; + use runtime_io::{with_externalities, twox_128, blake2_256, TestExternalities}; use parity_codec::{Joiner, KeyedVec}; use substrate_test_client::{AuthorityKeyring, AccountKeyring}; use crate::{Header, Transfer}; @@ -313,7 +313,7 @@ mod tests { twox_128(&0u32.to_keyed_vec(well_known_keys::AUTHORITY_PREFIX)).to_vec() => AuthorityKeyring::Alice.to_raw_public().to_vec(), twox_128(&1u32.to_keyed_vec(well_known_keys::AUTHORITY_PREFIX)).to_vec() => AuthorityKeyring::Bob.to_raw_public().to_vec(), twox_128(&2u32.to_keyed_vec(well_known_keys::AUTHORITY_PREFIX)).to_vec() => AuthorityKeyring::Charlie.to_raw_public().to_vec(), - twox_128(&AccountKeyring::Alice.to_raw_public().to_keyed_vec(b"balance:")).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0] + blake2_256(&AccountKeyring::Alice.to_raw_public().to_keyed_vec(b"balance:")).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0] ]) } diff --git a/node/executor/src/lib.rs b/node/executor/src/lib.rs index c855a4e6f00c5..d1a8e0cda3583 100644 --- a/node/executor/src/lib.rs +++ b/node/executor/src/lib.rs @@ -34,7 +34,7 @@ mod tests { use keyring::{AuthorityKeyring, AccountKeyring}; use runtime_support::{Hashable, StorageValue, StorageMap, traits::Currency}; use state_machine::{CodeExecutor, Externalities, TestExternalities}; - use primitives::{twox_128, Blake2Hasher, ChangesTrieConfiguration, NeverNativeValue, + use primitives::{twox_128, blake2_256, Blake2Hasher, ChangesTrieConfiguration, NeverNativeValue, NativeOrEncoded}; use node_primitives::{Hash, BlockNumber, AccountId}; use runtime_primitives::traits::{Header as HeaderT, Hash as HashT}; @@ -119,13 +119,13 @@ mod tests { #[test] fn panic_execution_with_foreign_code_gives_error() { let mut t = TestExternalities::::new_with_code(BLOATY_CODE, map![ - twox_128(&>::key_for(alice())).to_vec() => vec![69u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + blake2_256(&>::key_for(alice())).to_vec() => vec![69u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], twox_128(>::key()).to_vec() => vec![69u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], twox_128(>::key()).to_vec() => vec![0u8; 16], twox_128(>::key()).to_vec() => vec![0u8; 16], twox_128(>::key()).to_vec() => vec![0u8; 16], twox_128(>::key()).to_vec() => vec![0u8; 16], - twox_128(&>::key_for(0)).to_vec() => vec![0u8; 32], + blake2_256(&>::key_for(0)).to_vec() => vec![0u8; 32], twox_128(>::key()).to_vec() => vec![70u8; 16], twox_128(>::key()).to_vec() => vec![0u8; 16] ]); @@ -152,13 +152,13 @@ mod tests { #[test] fn bad_extrinsic_with_native_equivalent_code_gives_error() { let mut t = TestExternalities::::new_with_code(COMPACT_CODE, map![ - twox_128(&>::key_for(alice())).to_vec() => vec![69u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + blake2_256(&>::key_for(alice())).to_vec() => vec![69u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], twox_128(>::key()).to_vec() => vec![69u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], twox_128(>::key()).to_vec() => vec![0u8; 16], twox_128(>::key()).to_vec() => vec![0u8; 16], twox_128(>::key()).to_vec() => vec![0u8; 16], twox_128(>::key()).to_vec() => vec![0u8; 16], - twox_128(&>::key_for(0)).to_vec() => vec![0u8; 32], + blake2_256(&>::key_for(0)).to_vec() => vec![0u8; 32], twox_128(>::key()).to_vec() => vec![70u8; 16], twox_128(>::key()).to_vec() => vec![0u8; 16] ]); @@ -185,13 +185,13 @@ mod tests { #[test] fn successful_execution_with_native_equivalent_code_gives_ok() { let mut t = TestExternalities::::new_with_code(COMPACT_CODE, map![ - twox_128(&>::key_for(alice())).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + blake2_256(&>::key_for(alice())).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], twox_128(>::key()).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], twox_128(>::key()).to_vec() => vec![0u8; 16], twox_128(>::key()).to_vec() => vec![0u8; 16], twox_128(>::key()).to_vec() => vec![0u8; 16], twox_128(>::key()).to_vec() => vec![0u8; 16], - twox_128(&>::key_for(0)).to_vec() => vec![0u8; 32], + blake2_256(&>::key_for(0)).to_vec() => vec![0u8; 32], twox_128(>::key()).to_vec() => vec![0u8; 16], twox_128(>::key()).to_vec() => vec![0u8; 16] ]); @@ -222,13 +222,13 @@ mod tests { #[test] fn successful_execution_with_foreign_code_gives_ok() { let mut t = TestExternalities::::new_with_code(BLOATY_CODE, map![ - twox_128(&>::key_for(alice())).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + blake2_256(&>::key_for(alice())).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], twox_128(>::key()).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], twox_128(>::key()).to_vec() => vec![0u8; 16], twox_128(>::key()).to_vec() => vec![0u8; 16], twox_128(>::key()).to_vec() => vec![0u8; 16], twox_128(>::key()).to_vec() => vec![0u8; 16], - twox_128(&>::key_for(0)).to_vec() => vec![0u8; 32], + blake2_256(&>::key_for(0)).to_vec() => vec![0u8; 32], twox_128(>::key()).to_vec() => vec![0u8; 16], twox_128(>::key()).to_vec() => vec![0u8; 16] ]); @@ -796,13 +796,13 @@ mod tests { fn panic_execution_gives_error() { let foreign_code = include_bytes!("../../runtime/wasm/target/wasm32-unknown-unknown/release/node_runtime.wasm"); let mut t = TestExternalities::::new_with_code(foreign_code, map![ - twox_128(&>::key_for(alice())).to_vec() => vec![69u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + blake2_256(&>::key_for(alice())).to_vec() => vec![69u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], twox_128(>::key()).to_vec() => vec![69u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], twox_128(>::key()).to_vec() => vec![0u8; 16], twox_128(>::key()).to_vec() => vec![0u8; 16], twox_128(>::key()).to_vec() => vec![0u8; 16], twox_128(>::key()).to_vec() => vec![0u8; 16], - twox_128(&>::key_for(0)).to_vec() => vec![0u8; 32], + blake2_256(&>::key_for(0)).to_vec() => vec![0u8; 32], twox_128(>::key()).to_vec() => vec![70u8; 16], twox_128(>::key()).to_vec() => vec![0u8; 16] ]); @@ -818,13 +818,13 @@ mod tests { fn successful_execution_gives_ok() { let foreign_code = include_bytes!("../../runtime/wasm/target/wasm32-unknown-unknown/release/node_runtime.compact.wasm"); let mut t = TestExternalities::::new_with_code(foreign_code, map![ - twox_128(&>::key_for(alice())).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + blake2_256(&>::key_for(alice())).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], twox_128(>::key()).to_vec() => vec![111u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], twox_128(>::key()).to_vec() => vec![0u8; 16], twox_128(>::key()).to_vec() => vec![0u8; 16], twox_128(>::key()).to_vec() => vec![0u8; 16], twox_128(>::key()).to_vec() => vec![0u8; 16], - twox_128(&>::key_for(0)).to_vec() => vec![0u8; 32], + blake2_256(&>::key_for(0)).to_vec() => vec![0u8; 32], twox_128(>::key()).to_vec() => vec![0u8; 16], twox_128(>::key()).to_vec() => vec![0u8; 16] ]); diff --git a/node/runtime/src/lib.rs b/node/runtime/src/lib.rs index d74e7a17e73b5..94b910ed00b90 100644 --- a/node/runtime/src/lib.rs +++ b/node/runtime/src/lib.rs @@ -59,7 +59,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_name: create_runtime_str!("node"), impl_name: create_runtime_str!("substrate-node"), authoring_version: 10, - spec_version: 57, + spec_version: 58, impl_version: 58, apis: RUNTIME_API_VERSIONS, }; diff --git a/srml/executive/src/lib.rs b/srml/executive/src/lib.rs index 37b1b709f76e9..bf504d0918b6d 100644 --- a/srml/executive/src/lib.rs +++ b/srml/executive/src/lib.rs @@ -397,7 +397,7 @@ mod tests { header: Header { parent_hash: [69u8; 32].into(), number: 1, - state_root: hex!("49cd58a254ccf6abc4a023d9a22dcfc421e385527a250faec69f8ad0d8ed3e48").into(), + state_root: hex!("4c10fddf15e63c91ff2aa13ab3a9b7f6b19938d533829489e72ba40278a08fac").into(), extrinsics_root: hex!("03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314").into(), digest: Digest { logs: vec![], }, }, diff --git a/srml/indices/src/lib.rs b/srml/indices/src/lib.rs index 76261796c8b78..4a6010f800dec 100644 --- a/srml/indices/src/lib.rs +++ b/srml/indices/src/lib.rs @@ -96,16 +96,17 @@ decl_storage! { }): T::AccountIndex; /// The enumeration sets. - pub EnumSet get(enum_set): map T::AccountIndex => Vec; + pub EnumSet get(enum_set) build(|config: &GenesisConfig| { + (0..(config.ids.len() + ENUM_SET_SIZE - 1) / ENUM_SET_SIZE) + .map(|i| ( + T::AccountIndex::sa(i), + config.ids[i * ENUM_SET_SIZE..config.ids.len().min((i + 1) * ENUM_SET_SIZE)].to_owned(), + )) + .collect::>() + }): map T::AccountIndex => Vec; } add_extra_genesis { config(ids): Vec; - build(|storage: &mut primitives::StorageOverlay, _: &mut primitives::ChildrenStorageOverlay, config: &GenesisConfig| { - for i in 0..(config.ids.len() + ENUM_SET_SIZE - 1) / ENUM_SET_SIZE { - storage.insert(GenesisConfig::::hash(&>::key_for(T::AccountIndex::sa(i))).to_vec(), - config.ids[i * ENUM_SET_SIZE..config.ids.len().min((i + 1) * ENUM_SET_SIZE)].to_owned().encode()); - } - }); } } diff --git a/srml/metadata/src/lib.rs b/srml/metadata/src/lib.rs index 9b03daafa65d3..8749b37cac039 100644 --- a/srml/metadata/src/lib.rs +++ b/srml/metadata/src/lib.rs @@ -253,17 +253,30 @@ impl std::fmt::Debug for DefaultByteGetter { } } +/// Hasher used by storage maps +#[derive(Clone, PartialEq, Eq, Encode)] +#[cfg_attr(feature = "std", derive(Decode, Debug, Serialize))] +pub enum StorageHasher { + Blake2_128, + Blake2_256, + Twox128, + Twox256, + Twox64Concat, +} + /// A storage function type. #[derive(Clone, PartialEq, Eq, Encode)] #[cfg_attr(feature = "std", derive(Decode, Debug, Serialize))] pub enum StorageFunctionType { Plain(DecodeDifferentStr), Map { + hasher: StorageHasher, key: DecodeDifferentStr, value: DecodeDifferentStr, is_linked: bool, }, DoubleMap { + hasher: StorageHasher, key1: DecodeDifferentStr, key2: DecodeDifferentStr, value: DecodeDifferentStr, @@ -312,8 +325,10 @@ pub enum RuntimeMetadata { V1(RuntimeMetadataDeprecated), /// Version 2 for runtime metadata. No longer used. V2(RuntimeMetadataDeprecated), - /// Version 3 for runtime metadata. - V3(RuntimeMetadataV3), + /// Version 3 for runtime metadata. No longer used. + V3(RuntimeMetadataDeprecated), + /// Version 4 for runtime metadata. + V4(RuntimeMetadataV4), } /// Enum that should fail. @@ -336,7 +351,7 @@ impl Decode for RuntimeMetadataDeprecated { /// The metadata of a runtime. #[derive(Eq, Encode, PartialEq)] #[cfg_attr(feature = "std", derive(Decode, Debug, Serialize))] -pub struct RuntimeMetadataV3 { +pub struct RuntimeMetadataV4 { pub modules: DecodeDifferentArray, } diff --git a/srml/support/procedural/src/storage/impls.rs b/srml/support/procedural/src/storage/impls.rs index 5a8f7f65d5d8e..2708fdb213386 100644 --- a/srml/support/procedural/src/storage/impls.rs +++ b/srml/support/procedural/src/storage/impls.rs @@ -67,13 +67,13 @@ impl<'a, I: Iterator> Impls<'a, I> { let mutate_impl = if !is_option { quote!{ - >::put(&val, storage) + >::put(&val, storage) } } else { quote!{ match val { - Some(ref val) => >::put(&val, storage), - None => >::kill(storage), + Some(ref val) => >::put(&val, storage), + None => >::kill(storage), } } }; @@ -96,9 +96,12 @@ impl<'a, I: Iterator> Impls<'a, I> { // generator for value quote!{ #( #[ #attrs ] )* - #visibility struct #name<#traitinstance: #traittype, #instance #bound_instantiable #equal_default_instance>(#scrate::storage::generator::PhantomData<(#traitinstance #comma_instance)>); + #visibility struct #name<#traitinstance: #traittype, #instance #bound_instantiable #equal_default_instance> + (#scrate::rstd::marker::PhantomData<(#traitinstance #comma_instance)>); - impl<#traitinstance: #traittype, #instance #bound_instantiable> #scrate::storage::generator::StorageValue<#typ> for #name<#traitinstance, #instance> { + impl<#traitinstance: #traittype, #instance #bound_instantiable> + #scrate::storage::hashed::generator::StorageValue<#typ> for #name<#traitinstance, #instance> + { type Query = #value_type; /// Get the storage key. @@ -107,20 +110,20 @@ impl<'a, I: Iterator> Impls<'a, I> { } /// Load the value from the provided storage instance. - fn get(storage: &S) -> Self::Query { - storage.get(>::key()) + fn get>(storage: &S) -> Self::Query { + storage.get(>::key()) .#option_simple_1(|| #fielddefault) } /// Take a value from storage, removing it afterwards. - fn take(storage: &S) -> Self::Query { - storage.take(>::key()) + fn take>(storage: &S) -> Self::Query { + storage.take(>::key()) .#option_simple_1(|| #fielddefault) } /// Mutate the value under a key. - fn mutate R, S: #scrate::GenericStorage>(f: F, storage: &S) -> R { - let mut val = >::get(storage); + fn mutate R, S: #scrate::HashedStorage<#scrate::Twox128>>(f: F, storage: &S) -> R { + let mut val = >::get(storage); let ret = f(&mut val); #mutate_impl ; @@ -130,7 +133,7 @@ impl<'a, I: Iterator> Impls<'a, I> { } } - pub fn map(self, kty: &syn::Type) -> TokenStream2 { + pub fn map(self, hasher: TokenStream2, kty: &syn::Type) -> TokenStream2 { let Self { scrate, visibility, @@ -147,15 +150,17 @@ impl<'a, I: Iterator> Impls<'a, I> { let DeclStorageTypeInfos { typ, value_type, is_option, .. } = type_infos; let option_simple_1 = option_unwrap(is_option); + let as_map = quote!{ > }; + let mutate_impl = if !is_option { quote!{ - >::insert(key, &val, storage) + #as_map::insert(key, &val, storage) } } else { quote!{ match val { - Some(ref val) => >::insert(key, &val, storage), - None => >::remove(key, storage), + Some(ref val) => #as_map::insert(key, &val, storage), + None => #as_map::remove(key, storage), } } }; @@ -178,11 +183,16 @@ impl<'a, I: Iterator> Impls<'a, I> { // generator for map quote!{ #( #[ #attrs ] )* - #visibility struct #name<#traitinstance: #traittype, #instance #bound_instantiable #equal_default_instance>(#scrate::storage::generator::PhantomData<(#traitinstance #comma_instance)>); + #visibility struct #name<#traitinstance: #traittype, #instance #bound_instantiable #equal_default_instance> + (#scrate::rstd::marker::PhantomData<(#traitinstance #comma_instance)>); - impl<#traitinstance: #traittype, #instance #bound_instantiable> #scrate::storage::generator::StorageMap<#kty, #typ> for #name<#traitinstance, #instance> { + impl<#traitinstance: #traittype, #instance #bound_instantiable> + #scrate::storage::hashed::generator::StorageMap<#kty, #typ> for #name<#traitinstance, #instance> + { type Query = #value_type; + type Hasher = #scrate::#hasher; + /// Get the prefix key in storage. fn prefix() -> &'static [u8] { #final_prefix @@ -190,26 +200,26 @@ impl<'a, I: Iterator> Impls<'a, I> { /// Get the storage key used to fetch a value corresponding to a specific key. fn key_for(x: &#kty) -> #scrate::rstd::vec::Vec { - let mut key = >::prefix().to_vec(); + let mut key = #as_map::prefix().to_vec(); #scrate::codec::Encode::encode_to(x, &mut key); key } /// Load the value associated with the given key from the map. - fn get(key: &#kty, storage: &S) -> Self::Query { - let key = >::key_for(key); + fn get>(key: &#kty, storage: &S) -> Self::Query { + let key = #as_map::key_for(key); storage.get(&key[..]).#option_simple_1(|| #fielddefault) } /// Take the value, reading and removing it. - fn take(key: &#kty, storage: &S) -> Self::Query { - let key = >::key_for(key); + fn take>(key: &#kty, storage: &S) -> Self::Query { + let key = #as_map::key_for(key); storage.take(&key[..]).#option_simple_1(|| #fielddefault) } /// Mutate the value under a key - fn mutate R, S: #scrate::GenericStorage>(key: &#kty, f: F, storage: &S) -> R { - let mut val = >::get(key, storage); + fn mutate R, S: #scrate::HashedStorage<#scrate::#hasher>>(key: &#kty, f: F, storage: &S) -> R { + let mut val = #as_map::get(key, storage); let ret = f(&mut val); #mutate_impl ; @@ -220,7 +230,7 @@ impl<'a, I: Iterator> Impls<'a, I> { } } - pub fn linked_map(self, kty: &syn::Type) -> TokenStream2 { + pub fn linked_map(self, hasher: TokenStream2, kty: &syn::Type) -> TokenStream2 { let Self { scrate, visibility, @@ -264,8 +274,8 @@ impl<'a, I: Iterator> Impls<'a, I> { let name_lowercase = name.to_string().to_lowercase(); let inner_module = syn::Ident::new(&format!("__linked_map_details_for_{}_do_not_use", name_lowercase), name.span()); let linkage = syn::Ident::new(&format!("__LinkageFor{}DoNotUse", name), name.span()); - let phantom_data = quote! { #scrate::storage::generator::PhantomData }; - let as_map = quote!{ > }; + let phantom_data = quote! { #scrate::rstd::marker::PhantomData }; + let as_map = quote!{ > }; let put_or_insert = quote! { match linkage { Some(linkage) => storage.put(key_for, &(val, linkage)), @@ -316,14 +326,17 @@ impl<'a, I: Iterator> Impls<'a, I> { pub _data: #phantom_data, } - impl<'a, S: #scrate::GenericStorage, #traitinstance: #traittype, #instance #bound_instantiable> Iterator for Enumerator<'a, S, #kty, (#typ, #traitinstance, #instance)> + impl<'a, S: #scrate::HashedStorage<#scrate::#hasher>, #traitinstance: #traittype, #instance #bound_instantiable> + Iterator for Enumerator<'a, S, #kty, (#typ, #traitinstance, #instance)> where #traitinstance: 'a { type Item = (#kty, #typ); fn next(&mut self) -> Option { let next = self.next.take()?; - let key_for = as #scrate::storage::generator::StorageMap<#kty, #typ>>::key_for(&next); + let key_for = + as #scrate::storage::hashed::generator::StorageMap<#kty, #typ>>::key_for(&next); + let (val, linkage): (#typ, Linkage<#kty>) = self.storage.get(&*key_for) .expect("previous/next only contain existing entires; we enumerate using next; entry exists; qed"); self.next = linkage.next; @@ -336,26 +349,26 @@ impl<'a, I: Iterator> Impls<'a, I> { /// /// Takes care of updating previous and next elements points /// as well as updates head if the element is first or last. - fn remove_linkage(linkage: Linkage<#kty>, storage: &S); + fn remove_linkage>(linkage: Linkage<#kty>, storage: &S); /// Read the contained data and it's linkage. - fn read_with_linkage(storage: &S, key: &[u8]) -> Option<(#value_type, Linkage<#kty>)>; + fn read_with_linkage>(storage: &S, key: &[u8]) -> Option<(#value_type, Linkage<#kty>)>; /// Generate linkage for newly inserted element. /// /// Takes care of updating head and previous head's pointer. - fn new_head_linkage( + fn new_head_linkage>( storage: &S, key: &#kty, ) -> Linkage<#kty>; /// Read current head pointer. - fn read_head(storage: &S) -> Option<#kty>; + fn read_head>(storage: &S) -> Option<#kty>; /// Overwrite current head pointer. /// /// If `None` is given head is removed from storage. - fn write_head(storage: &S, head: Option<&#kty>); + fn write_head>(storage: &S, head: Option<&#kty>); } } }; @@ -365,7 +378,7 @@ impl<'a, I: Iterator> Impls<'a, I> { #visibility struct #name<#traitinstance: #traittype, #instance #bound_instantiable #equal_default_instance>(#phantom_data<(#traitinstance #comma_instance)>); impl<#traitinstance: #traittype, #instance #bound_instantiable> self::#inner_module::Utils<#traitinstance, #instance> for #name<#traitinstance, #instance> { - fn remove_linkage( + fn remove_linkage>( linkage: self::#inner_module::Linkage<#kty>, storage: &S, ) { @@ -394,14 +407,14 @@ impl<'a, I: Iterator> Impls<'a, I> { } } - fn read_with_linkage( + fn read_with_linkage>( storage: &S, key: &[u8], ) -> Option<(#value_type, self::#inner_module::Linkage<#kty>)> { storage.get(key) } - fn new_head_linkage( + fn new_head_linkage>( storage: &S, key: &#kty, ) -> self::#inner_module::Linkage<#kty> { @@ -433,11 +446,11 @@ impl<'a, I: Iterator> Impls<'a, I> { } } - fn read_head(storage: &S) -> Option<#kty> { + fn read_head>(storage: &S) -> Option<#kty> { storage.get(#final_head_key) } - fn write_head(storage: &S, head: Option<&#kty>) { + fn write_head>(storage: &S, head: Option<&#kty>) { match head { Some(head) => storage.put(#final_head_key, head), None => storage.kill(#final_head_key), @@ -451,9 +464,13 @@ impl<'a, I: Iterator> Impls<'a, I> { #structure - impl<#traitinstance: #traittype, #instance #bound_instantiable> #scrate::storage::generator::StorageMap<#kty, #typ> for #name<#traitinstance, #instance> { + impl<#traitinstance: #traittype, #instance #bound_instantiable> + #scrate::storage::hashed::generator::StorageMap<#kty, #typ> for #name<#traitinstance, #instance> + { type Query = #value_type; + type Hasher = #scrate::#hasher; + /// Get the prefix key in storage. fn prefix() -> &'static [u8] { #final_prefix @@ -467,12 +484,12 @@ impl<'a, I: Iterator> Impls<'a, I> { } /// Load the value associated with the given key from the map. - fn get(key: &#kty, storage: &S) -> Self::Query { + fn get>(key: &#kty, storage: &S) -> Self::Query { storage.get(&*#as_map::key_for(key)).#option_simple_1(|| #fielddefault) } /// Take the value, reading and removing it. - fn take(key: &#kty, storage: &S) -> Self::Query { + fn take>(key: &#kty, storage: &S) -> Self::Query { use self::#inner_module::Utils; let res: Option<(#value_type, self::#inner_module::Linkage<#kty>)> = storage.take(&*#as_map::key_for(key)); @@ -486,12 +503,12 @@ impl<'a, I: Iterator> Impls<'a, I> { } /// Remove the value under a key. - fn remove(key: &#kty, storage: &S) { + fn remove>(key: &#kty, storage: &S) { #as_map::take(key, storage); } /// Store a value to be associated with the given key from the map. - fn insert(key: &#kty, val: &#typ, storage: &S) { + fn insert>(key: &#kty, val: &#typ, storage: &S) { use self::#inner_module::Utils; let key_for = &*#as_map::key_for(key); @@ -505,7 +522,7 @@ impl<'a, I: Iterator> Impls<'a, I> { } /// Mutate the value under a key - fn mutate R, S: #scrate::GenericStorage>(key: &#kty, f: F, storage: &S) -> R { + fn mutate R, S: #scrate::HashedStorage<#scrate::#hasher>>(key: &#kty, f: F, storage: &S) -> R { use self::#inner_module::Utils; let key_for = &*#as_map::key_for(key); @@ -519,20 +536,22 @@ impl<'a, I: Iterator> Impls<'a, I> { } } - impl<#traitinstance: 'static + #traittype, #instance #bound_instantiable> #scrate::storage::generator::EnumerableStorageMap<#kty, #typ> for #name<#traitinstance, #instance> { - fn head(storage: &S) -> Option<#kty> { + impl<#traitinstance: 'static + #traittype, #instance #bound_instantiable> + #scrate::storage::hashed::generator::EnumerableStorageMap<#kty, #typ> for #name<#traitinstance, #instance> + { + fn head>(storage: &S) -> Option<#kty> { use self::#inner_module::Utils; Self::read_head(storage) } - fn enumerate<'a, S: #scrate::GenericStorage>(storage: &'a S) -> #scrate::storage::generator::Box + 'a> where + fn enumerate<'a, S: #scrate::HashedStorage<#scrate::#hasher>>(storage: &'a S) -> #scrate::rstd::boxed::Box + 'a> where #kty: 'a, #typ: 'a, { use self::#inner_module::{Utils, Enumerator}; - #scrate::storage::generator::Box::new(Enumerator { + #scrate::rstd::boxed::Box::new(Enumerator { next: Self::read_head(storage), storage, _data: #phantom_data::<(#typ, #traitinstance, #instance)>::default(), @@ -542,7 +561,7 @@ impl<'a, I: Iterator> Impls<'a, I> { } } - pub fn double_map(self, k1ty: &syn::Type, k2ty: &syn::Type, k2_hasher: TokenStream2) -> TokenStream2 { + pub fn double_map(self, hasher: TokenStream2, k1ty: &syn::Type, k2ty: &syn::Type, k2_hasher: TokenStream2) -> TokenStream2 { let Self { scrate, visibility, @@ -593,11 +612,20 @@ impl<'a, I: Iterator> Impls<'a, I> { // generator for double map quote!{ #( #[ #attrs ] )* - #visibility struct #name<#traitinstance: #traittype, #instance #bound_instantiable #equal_default_instance>(#scrate::storage::generator::PhantomData<(#traitinstance #comma_instance)>); + #visibility struct #name<#traitinstance: #traittype, #instance #bound_instantiable #equal_default_instance> + (#scrate::rstd::marker::PhantomData<(#traitinstance #comma_instance)>); - impl<#traitinstance: #traittype, #instance #bound_instantiable> #scrate::storage::unhashed::generator::StorageDoubleMap<#k1ty, #k2ty, #typ> for #name<#traitinstance, #instance> { + impl<#traitinstance: #traittype, #instance #bound_instantiable> + #scrate::storage::unhashed::generator::StorageDoubleMap<#k1ty, #k2ty, #typ> for #name<#traitinstance, #instance> + { type Query = #value_type; + fn prefix_for(k1: &#k1ty) -> Vec { + let mut key = #as_double_map::prefix().to_vec(); + #scrate::codec::Encode::encode_to(k1, &mut key); + #scrate::Hashable::#hasher(&key).to_vec() + } + fn prefix() -> &'static [u8] { #final_prefix } @@ -608,17 +636,17 @@ impl<'a, I: Iterator> Impls<'a, I> { key } - fn get(key1: &#k1ty, key2: &#k2ty, storage: &S) -> Self::Query { + fn get(key1: &#k1ty, key2: &#k2ty, storage: &S) -> Self::Query { let key = #as_double_map::key_for(key1, key2); storage.get(&key).#option_simple_1(|| #fielddefault) } - fn take(key1: &#k1ty, key2: &#k2ty, storage: &S) -> Self::Query { + fn take(key1: &#k1ty, key2: &#k2ty, storage: &S) -> Self::Query { let key = #as_double_map::key_for(key1, key2); storage.take(&key).#option_simple_1(|| #fielddefault) } - fn mutate R, S: #scrate::GenericUnhashedStorage>(key1: &#k1ty, key2: &#k2ty, f: F, storage: &S) -> R { + fn mutate R, S: #scrate::UnhashedStorage>(key1: &#k1ty, key2: &#k2ty, f: F, storage: &S) -> R { let mut val = #as_double_map::get(key1, key2, storage); let ret = f(&mut val); diff --git a/srml/support/procedural/src/storage/mod.rs b/srml/support/procedural/src/storage/mod.rs index 82290e0de458f..649a48a1813ed 100644 --- a/srml/support/procedural/src/storage/mod.rs +++ b/srml/support/procedural/src/storage/mod.rs @@ -23,6 +23,8 @@ use srml_support_procedural_tools::{ToTokens, Parse, custom_keyword, custom_keyw use syn::{Ident, Token}; use syn::token::CustomKeyword; +use proc_macro2::TokenStream as TokenStream2; +use quote::quote; mod impls; @@ -138,6 +140,7 @@ enum DeclStorageType { #[derive(Parse, ToTokens, Debug)] struct DeclStorageMap { pub map_keyword: ext::CustomToken, + pub hasher: Option, pub key: syn::Type, pub ass_keyword: Token![=>], pub value: syn::Type, @@ -146,6 +149,7 @@ struct DeclStorageMap { #[derive(Parse, ToTokens, Debug)] struct DeclStorageLinkedMap { pub map_keyword: ext::CustomToken, + pub hasher: Option, pub key: syn::Type, pub ass_keyword: Token![=>], pub value: syn::Type, @@ -154,19 +158,22 @@ struct DeclStorageLinkedMap { #[derive(Parse, ToTokens, Debug)] struct DeclStorageDoubleMap { pub map_keyword: ext::CustomToken, + pub hasher: Option, pub key1: syn::Type, pub comma_keyword: Token![,], - pub key2_hasher: DeclStorageDoubleMapHasher, + pub key2_hasher: Hasher, pub key2: ext::Parens, pub ass_keyword: Token![=>], pub value: syn::Type, } #[derive(Parse, ToTokens, Debug)] -enum DeclStorageDoubleMapHasher { +enum Hasher { Blake2_256(ext::CustomToken), + Blake2_128(ext::CustomToken), Twox256(ext::CustomToken), Twox128(ext::CustomToken), + Twox64Concat(ext::CustomToken), } #[derive(Parse, ToTokens, Debug)] @@ -175,6 +182,64 @@ struct DeclStorageDefault { pub expr: syn::Expr, } +#[derive(Parse, ToTokens, Debug)] +struct SetHasher { + pub hasher_keyword: ext::CustomToken, + pub inner: ext::Parens, +} + +#[derive(Debug, Clone)] +enum HasherKind { + Blake2_256, + Blake2_128, + Twox256, + Twox128, + Twox64Concat, +} + +impl From<&SetHasher> for HasherKind { + fn from(set_hasher: &SetHasher) -> Self { + match set_hasher.inner.content { + Hasher::Blake2_256(_) => HasherKind::Blake2_256, + Hasher::Blake2_128(_) => HasherKind::Blake2_128, + Hasher::Twox256(_) => HasherKind::Twox256, + Hasher::Twox128(_) => HasherKind::Twox128, + Hasher::Twox64Concat(_) => HasherKind::Twox64Concat, + } + } +} +impl HasherKind { + fn into_storage_hasher_struct(&self) -> TokenStream2 { + match self { + HasherKind::Blake2_256 => quote!( Blake2_256 ), + HasherKind::Blake2_128 => quote!( Blake2_128 ), + HasherKind::Twox256 => quote!( Twox256 ), + HasherKind::Twox128 => quote!( Twox128 ), + HasherKind::Twox64Concat => quote!( Twox64Concat ), + } + } + + fn into_hashable_fn(&self) -> TokenStream2 { + match self { + HasherKind::Blake2_256 => quote!( blake2_256 ), + HasherKind::Blake2_128 => quote!( blake2_128 ), + HasherKind::Twox256 => quote!( twox_256 ), + HasherKind::Twox128 => quote!( twox_128 ), + HasherKind::Twox64Concat => quote!( twox_64_concat), + } + } + + fn into_metadata(&self) -> TokenStream2 { + match self { + HasherKind::Blake2_256 => quote!( StorageHasher::Blake2_256 ), + HasherKind::Blake2_128 => quote!( StorageHasher::Blake2_128 ), + HasherKind::Twox256 => quote!( StorageHasher::Twox256 ), + HasherKind::Twox128 => quote!( StorageHasher::Twox128 ), + HasherKind::Twox64Concat => quote!( StorageHasher::Twox64Concat ), + } + } +} + custom_keyword_impl!(SpecificHiddenCrate, "hiddencrate", "hiddencrate as keyword"); custom_keyword_impl!(DeclStorageConfig, "config", "build as keyword"); custom_keyword!(ConfigKeyword, "config", "config as keyword"); @@ -186,6 +251,9 @@ custom_keyword!(MapKeyword, "map", "map as keyword"); custom_keyword!(LinkedMapKeyword, "linked_map", "linked_map as keyword"); custom_keyword!(DoubleMapKeyword, "double_map", "double_map as keyword"); custom_keyword!(Blake2_256Keyword, "blake2_256", "Blake2_256 as keyword"); -custom_keyword!(Twox256Keyword, "twox_256", "Twox_256 as keyword"); -custom_keyword!(Twox128Keyword, "twox_128", "Twox_128 as keyword"); +custom_keyword!(Blake2_128Keyword, "blake2_128", "Blake2_128 as keyword"); +custom_keyword!(Twox256Keyword, "twox_256", "Twox256 as keyword"); +custom_keyword!(Twox128Keyword, "twox_128", "Twox128 as keyword"); +custom_keyword!(Twox64ConcatKeyword, "twox_64_concat", "Twox64Concat as keyword"); custom_keyword_impl!(ExtraGenesisSkipPhantomDataField, "extra_genesis_skip_phantom_data_field", "extra_genesis_skip_phantom_data_field as keyword"); +custom_keyword_impl!(SetHasher, "hasher", "storage hasher"); diff --git a/srml/support/procedural/src/storage/transformation.rs b/srml/support/procedural/src/storage/transformation.rs index f00b5e8309bcc..205fccdea574d 100644 --- a/srml/support/procedural/src/storage/transformation.rs +++ b/srml/support/procedural/src/storage/transformation.rs @@ -156,13 +156,13 @@ pub fn decl_storage_impl(input: TokenStream) -> TokenStream { impl<#traitinstance: 'static + #traittype, #instance #bound_instantiable> #module_ident<#traitinstance, #instance> { #impl_store_fns #[doc(hidden)] - pub fn store_metadata() -> #scrate::storage::generator::StorageMetadata { - #scrate::storage::generator::StorageMetadata { - functions: #scrate::storage::generator::DecodeDifferent::Encode(#store_functions_to_metadata) , + pub fn store_metadata() -> #scrate::metadata::StorageMetadata { + #scrate::metadata::StorageMetadata { + functions: #scrate::metadata::DecodeDifferent::Encode(#store_functions_to_metadata) , } } #[doc(hidden)] - pub fn store_metadata_functions() -> &'static [#scrate::storage::generator::StorageFunctionMetadata] { + pub fn store_metadata_functions() -> &'static [#scrate::metadata::StorageFunctionMetadata] { #store_functions_to_metadata } #[doc(hidden)] @@ -284,7 +284,7 @@ fn decl_store_extra_genesis( use #scrate::codec::{Encode, Decode}; let v = (#builder)(&self); - <#name<#traitinstance, #instance> as #scrate::storage::generator::StorageValue<#typ>>::put(&v, &storage); + <#name<#traitinstance, #instance> as #scrate::storage::hashed::generator::StorageValue<#typ>>::put(&v, &storage); }} }, DeclStorageTypeInfosKind::Map { key_type, .. } => { @@ -294,7 +294,7 @@ fn decl_store_extra_genesis( let data = (#builder)(&self); for (k, v) in data.into_iter() { - <#name<#traitinstance, #instance> as #scrate::storage::generator::StorageMap<#key_type, #typ>>::insert(&k, &v, &storage); + <#name<#traitinstance, #instance> as #scrate::storage::hashed::generator::StorageMap<#key_type, #typ>>::insert(&k, &v, &storage); } }} }, @@ -402,7 +402,7 @@ fn decl_store_extra_genesis( quote!{ #[serde(skip)] - pub _genesis_phantom_data: #scrate::storage::generator::PhantomData<(#traitinstance #comma_instance)>, + pub _genesis_phantom_data: #scrate::rstd::marker::PhantomData<(#traitinstance #comma_instance)>, }, quote!{ _genesis_phantom_data: Default::default(), @@ -440,12 +440,12 @@ fn decl_store_extra_genesis( #[cfg(feature = "std")] impl#fparam_impl #scrate::runtime_primitives::BuildStorage for GenesisConfig#sparam { fn assimilate_storage(self, r: &mut #scrate::runtime_primitives::StorageOverlay, c: &mut #scrate::runtime_primitives::ChildrenStorageOverlay) -> ::std::result::Result<(), String> { - use #scrate::rstd::{cell::RefCell, marker::PhantomData}; - let storage = (RefCell::new(r), PhantomData::::default()); + use #scrate::rstd::cell::RefCell; + let storage = RefCell::new(r); #builders - let r = storage.0.into_inner(); + let r = storage.into_inner(); #scall(r, c, &self); @@ -589,14 +589,14 @@ fn decl_storage_items( DeclStorageTypeInfosKind::Simple => { i.simple_value() }, - DeclStorageTypeInfosKind::Map { key_type, is_linked: false } => { - i.map(key_type) + DeclStorageTypeInfosKind::Map { key_type, is_linked: false, hasher } => { + i.map(hasher.into_storage_hasher_struct(), key_type) }, - DeclStorageTypeInfosKind::Map { key_type, is_linked: true } => { - i.linked_map(key_type) + DeclStorageTypeInfosKind::Map { key_type, is_linked: true, hasher } => { + i.linked_map(hasher.into_storage_hasher_struct(), key_type) }, - DeclStorageTypeInfosKind::DoubleMap { key1_type, key2_type, key2_hasher } => { - i.double_map(key1_type, key2_type, key2_hasher) + DeclStorageTypeInfosKind::DoubleMap { key1_type, key2_type, key2_hasher, hasher } => { + i.double_map(hasher.into_hashable_fn(), key1_type, key2_type, key2_hasher) }, }; impls.extend(implementation) @@ -662,15 +662,15 @@ fn impl_store_fns( quote!{ #( #[ #attrs ] )* pub fn #get_fn() -> #value_type { - <#name<#traitinstance, #instance> as #scrate::storage::generator::StorageValue<#typ>> :: get(&#scrate::storage::RuntimeStorage) + <#name<#traitinstance, #instance> as #scrate::storage::hashed::generator::StorageValue<#typ>> :: get(&#scrate::storage::RuntimeStorage) } } }, DeclStorageTypeInfosKind::Map { key_type, .. } => { quote!{ #( #[ #attrs ] )* - pub fn #get_fn>(key: K) -> #value_type { - <#name<#traitinstance, #instance> as #scrate::storage::generator::StorageMap<#key_type, #typ>> :: get(key.borrow(), &#scrate::storage::RuntimeStorage) + pub fn #get_fn>(key: K) -> #value_type { + <#name<#traitinstance, #instance> as #scrate::storage::hashed::generator::StorageMap<#key_type, #typ>> :: get(key.borrow(), &#scrate::storage::RuntimeStorage) } } } @@ -678,8 +678,8 @@ fn impl_store_fns( quote!{ pub fn #get_fn(k1: KArg1, k2: KArg2) -> #value_type where - KArg1: #scrate::storage::generator::Borrow<#key1_type>, - KArg2: #scrate::storage::generator::Borrow<#key2_type>, + KArg1: #scrate::rstd::borrow::Borrow<#key1_type>, + KArg2: #scrate::rstd::borrow::Borrow<#key2_type>, { <#name<#traitinstance> as #scrate::storage::unhashed::generator::StorageDoubleMap<#key1_type, #key2_type, #typ>> :: get(k1.borrow(), k2.borrow(), &#scrate::storage::RuntimeStorage) } @@ -727,42 +727,46 @@ fn store_functions_to_metadata ( let stype = match type_infos.kind { DeclStorageTypeInfosKind::Simple => { quote!{ - #scrate::storage::generator::StorageFunctionType::Plain( - #scrate::storage::generator::DecodeDifferent::Encode(#styp), + #scrate::metadata::StorageFunctionType::Plain( + #scrate::metadata::DecodeDifferent::Encode(#styp), ) } }, - DeclStorageTypeInfosKind::Map { key_type, is_linked } => { + DeclStorageTypeInfosKind::Map { key_type, is_linked, hasher } => { + let hasher = hasher.into_metadata(); let kty = clean_type_string("e!(#key_type).to_string()); quote!{ - #scrate::storage::generator::StorageFunctionType::Map { - key: #scrate::storage::generator::DecodeDifferent::Encode(#kty), - value: #scrate::storage::generator::DecodeDifferent::Encode(#styp), + #scrate::metadata::StorageFunctionType::Map { + hasher: #scrate::metadata::#hasher, + key: #scrate::metadata::DecodeDifferent::Encode(#kty), + value: #scrate::metadata::DecodeDifferent::Encode(#styp), is_linked: #is_linked, } } }, - DeclStorageTypeInfosKind::DoubleMap { key1_type, key2_type, key2_hasher } => { + DeclStorageTypeInfosKind::DoubleMap { key1_type, key2_type, key2_hasher, hasher } => { + let hasher = hasher.into_metadata(); let k1ty = clean_type_string("e!(#key1_type).to_string()); let k2ty = clean_type_string("e!(#key2_type).to_string()); let k2_hasher = clean_type_string(&key2_hasher.to_string()); quote!{ - #scrate::storage::generator::StorageFunctionType::DoubleMap { - key1: #scrate::storage::generator::DecodeDifferent::Encode(#k1ty), - key2: #scrate::storage::generator::DecodeDifferent::Encode(#k2ty), - value: #scrate::storage::generator::DecodeDifferent::Encode(#styp), - key2_hasher: #scrate::storage::generator::DecodeDifferent::Encode(#k2_hasher), + #scrate::metadata::StorageFunctionType::DoubleMap { + hasher: #scrate::metadata::#hasher, + key1: #scrate::metadata::DecodeDifferent::Encode(#k1ty), + key2: #scrate::metadata::DecodeDifferent::Encode(#k2ty), + value: #scrate::metadata::DecodeDifferent::Encode(#styp), + key2_hasher: #scrate::metadata::DecodeDifferent::Encode(#k2_hasher), } } }, }; let modifier = if type_infos.is_option { quote!{ - #scrate::storage::generator::StorageFunctionModifier::Optional + #scrate::metadata::StorageFunctionModifier::Optional } } else { quote!{ - #scrate::storage::generator::StorageFunctionModifier::Default + #scrate::metadata::StorageFunctionModifier::Default } }; let default = default_value.inner.as_ref().map(|d| &d.expr) @@ -786,16 +790,16 @@ fn store_functions_to_metadata ( let struct_name = proc_macro2::Ident::new(&("__GetByteStruct".to_string() + &str_name), name.span()); let cache_name = proc_macro2::Ident::new(&("__CACHE_GET_BYTE_STRUCT_".to_string() + &str_name), name.span()); let item = quote! { - #scrate::storage::generator::StorageFunctionMetadata { - name: #scrate::storage::generator::DecodeDifferent::Encode(#str_name), + #scrate::metadata::StorageFunctionMetadata { + name: #scrate::metadata::DecodeDifferent::Encode(#str_name), modifier: #modifier, ty: #stype, - default: #scrate::storage::generator::DecodeDifferent::Encode( - #scrate::storage::generator::DefaultByteGetter( + default: #scrate::metadata::DecodeDifferent::Encode( + #scrate::metadata::DefaultByteGetter( &#struct_name::<#traitinstance, #instance>(#scrate::rstd::marker::PhantomData) ) ), - documentation: #scrate::storage::generator::DecodeDifferent::Encode(&[ #docs ]), + documentation: #scrate::metadata::DecodeDifferent::Encode(&[ #docs ]), }, }; items.extend(item); @@ -806,7 +810,7 @@ fn store_functions_to_metadata ( #[allow(non_upper_case_globals)] static #cache_name: #scrate::once_cell::sync::OnceCell<#scrate::rstd::vec::Vec> = #scrate::once_cell::sync::OnceCell::INIT; #[cfg(feature = "std")] - impl<#traitinstance: #traittype, #instance #bound_instantiable> #scrate::storage::generator::DefaultByte for #struct_name<#traitinstance, #instance> { + impl<#traitinstance: #traittype, #instance #bound_instantiable> #scrate::metadata::DefaultByte for #struct_name<#traitinstance, #instance> { fn default_byte(&self) -> #scrate::rstd::vec::Vec { use #scrate::codec::Encode; #cache_name.get_or_init(|| { @@ -816,7 +820,7 @@ fn store_functions_to_metadata ( } } #[cfg(not(feature = "std"))] - impl<#traitinstance: #traittype, #instance #bound_instantiable> #scrate::storage::generator::DefaultByte for #struct_name<#traitinstance, #instance> { + impl<#traitinstance: #traittype, #instance #bound_instantiable> #scrate::metadata::DefaultByte for #struct_name<#traitinstance, #instance> { fn default_byte(&self) -> #scrate::rstd::vec::Vec { use #scrate::codec::Encode; let def_val: #value_type = #default; @@ -848,10 +852,12 @@ pub(crate) struct DeclStorageTypeInfos<'a> { enum DeclStorageTypeInfosKind<'a> { Simple, Map { + hasher: HasherKind, key_type: &'a syn::Type, is_linked: bool, }, DoubleMap { + hasher: HasherKind, key1_type: &'a syn::Type, key2_type: &'a syn::Type, key2_hasher: TokenStream2, @@ -871,14 +877,17 @@ fn get_type_infos(storage_type: &DeclStorageType) -> DeclStorageTypeInfos { let (value_type, kind) = match storage_type { DeclStorageType::Simple(ref st) => (st, DeclStorageTypeInfosKind::Simple), DeclStorageType::Map(ref map) => (&map.value, DeclStorageTypeInfosKind::Map { + hasher: map.hasher.as_ref().map(|h| h.into()).unwrap_or(HasherKind::Blake2_256), key_type: &map.key, is_linked: false, }), DeclStorageType::LinkedMap(ref map) => (&map.value, DeclStorageTypeInfosKind::Map { + hasher: map.hasher.as_ref().map(|h| h.into()).unwrap_or(HasherKind::Blake2_256), key_type: &map.key, is_linked: true, }), DeclStorageType::DoubleMap(ref map) => (&map.value, DeclStorageTypeInfosKind::DoubleMap { + hasher: map.hasher.as_ref().map(|h| h.into()).unwrap_or(HasherKind::Blake2_256), key1_type: &map.key1, key2_type: &map.key2.content, key2_hasher: { let h = &map.key2_hasher; quote! { #h } }, diff --git a/srml/support/procedural/tools/src/syn_ext.rs b/srml/support/procedural/tools/src/syn_ext.rs index c2136b2cd8f96..c6b0b4aefbd70 100644 --- a/srml/support/procedural/tools/src/syn_ext.rs +++ b/srml/support/procedural/tools/src/syn_ext.rs @@ -72,7 +72,7 @@ groups_impl!(Braces, Brace, Brace, parse_braces); groups_impl!(Brackets, Bracket, Bracket, parse_brackets); groups_impl!(Parens, Paren, Parenthesis, parse_parens); -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct CustomToken(std::marker::PhantomData); impl Parse for CustomToken { diff --git a/srml/support/src/hashable.rs b/srml/support/src/hashable.rs index 886c88b23a3e8..b3ee2b3612c1e 100644 --- a/srml/support/src/hashable.rs +++ b/srml/support/src/hashable.rs @@ -17,15 +17,24 @@ //! Hashable trait. use crate::codec::Codec; -use runtime_io::{blake2_256, twox_128, twox_256}; +use runtime_io::{blake2_128, blake2_256, twox_128, twox_256}; +use crate::storage::hashed::generator::StorageHasher; +use crate::Twox64Concat; +use crate::rstd::prelude::Vec; +// This trait must be kept coherent with srml-support-procedural HasherKind usage pub trait Hashable: Sized { + fn blake2_128(&self) -> [u8; 16]; fn blake2_256(&self) -> [u8; 32]; fn twox_128(&self) -> [u8; 16]; fn twox_256(&self) -> [u8; 32]; + fn twox_64_concat(&self) -> Vec; } impl Hashable for T { + fn blake2_128(&self) -> [u8; 16] { + self.using_encoded(blake2_128) + } fn blake2_256(&self) -> [u8; 32] { self.using_encoded(blake2_256) } @@ -35,4 +44,7 @@ impl Hashable for T { fn twox_256(&self) -> [u8; 32] { self.using_encoded(twox_256) } + fn twox_64_concat(&self) -> Vec { + self.using_encoded(Twox64Concat::hash) + } } diff --git a/srml/support/src/lib.rs b/srml/support/src/lib.rs index d99db6ddb8287..4e9325c867fc9 100644 --- a/srml/support/src/lib.rs +++ b/srml/support/src/lib.rs @@ -35,8 +35,8 @@ pub use once_cell; pub use paste; pub use sr_primitives as runtime_primitives; -pub use self::storage::generator::Storage as GenericStorage; -pub use self::storage::unhashed::generator::UnhashedStorage as GenericUnhashedStorage; +pub use self::storage::hashed::generator::{HashedStorage, Twox256, Twox128, Blake2_256, Blake2_128, Twox64Concat}; +pub use self::storage::unhashed::generator::UnhashedStorage; #[macro_use] pub mod dispatch; @@ -56,7 +56,7 @@ pub mod inherent; mod double_map; pub mod traits; -pub use self::storage::{StorageVec, StorageList, StorageValue, StorageMap, EnumerableStorageMap, StorageDoubleMap}; +pub use self::storage::{StorageList, StorageValue, StorageMap, EnumerableStorageMap, StorageDoubleMap}; pub use self::hashable::Hashable; pub use self::dispatch::{Parameter, Dispatchable, Callable, IsSubType}; pub use self::double_map::StorageDoubleMapWithHasher; @@ -185,7 +185,7 @@ mod tests { pub use srml_metadata::{ DecodeDifferent, StorageMetadata, StorageFunctionMetadata, StorageFunctionType, StorageFunctionModifier, - DefaultByte, DefaultByteGetter, + DefaultByte, DefaultByteGetter, StorageHasher }; pub use rstd::marker::PhantomData; @@ -209,11 +209,11 @@ mod tests { decl_storage! { trait Store for Module as Example { - pub Data get(data) build(|_| vec![(15u32, 42u64)]): linked_map u32 => u64; - pub GenericData get(generic_data): linked_map T::BlockNumber => T::BlockNumber; + pub Data get(data) build(|_| vec![(15u32, 42u64)]): linked_map hasher(twox_64_concat) u32 => u64; + pub GenericData get(generic_data): linked_map hasher(twox_128) T::BlockNumber => T::BlockNumber; pub GenericData2 get(generic_data2): linked_map T::BlockNumber => Option; - pub DataDM config(test_config) build(|_| vec![(15u32, 16u32, 42u64)]): double_map u32, blake2_256(u32) => u64; + pub DataDM config(test_config) build(|_| vec![(15u32, 16u32, 42u64)]): double_map hasher(twox_64_concat) u32, blake2_256(u32) => u64; pub GenericDataDM: double_map T::BlockNumber, twox_128(T::BlockNumber) => T::BlockNumber; pub GenericData2DM: double_map T::BlockNumber, twox_256(T::BlockNumber) => Option; } @@ -354,6 +354,7 @@ mod tests { name: DecodeDifferent::Encode("Data"), modifier: StorageFunctionModifier::Default, ty: StorageFunctionType::Map{ + hasher: StorageHasher::Twox64Concat, key: DecodeDifferent::Encode("u32"), value: DecodeDifferent::Encode("u64"), is_linked: true }, default: DecodeDifferent::Encode( @@ -365,6 +366,7 @@ mod tests { name: DecodeDifferent::Encode("GenericData"), modifier: StorageFunctionModifier::Default, ty: StorageFunctionType::Map{ + hasher: StorageHasher::Twox128, key: DecodeDifferent::Encode("T::BlockNumber"), value: DecodeDifferent::Encode("T::BlockNumber"), is_linked: true }, default: DecodeDifferent::Encode( @@ -376,6 +378,7 @@ mod tests { name: DecodeDifferent::Encode("GenericData2"), modifier: StorageFunctionModifier::Optional, ty: StorageFunctionType::Map{ + hasher: StorageHasher::Blake2_256, key: DecodeDifferent::Encode("T::BlockNumber"), value: DecodeDifferent::Encode("T::BlockNumber"), is_linked: true }, default: DecodeDifferent::Encode( @@ -387,6 +390,7 @@ mod tests { name: DecodeDifferent::Encode("DataDM"), modifier: StorageFunctionModifier::Default, ty: StorageFunctionType::DoubleMap{ + hasher: StorageHasher::Twox64Concat, key1: DecodeDifferent::Encode("u32"), key2: DecodeDifferent::Encode("u32"), value: DecodeDifferent::Encode("u64"), @@ -401,6 +405,7 @@ mod tests { name: DecodeDifferent::Encode("GenericDataDM"), modifier: StorageFunctionModifier::Default, ty: StorageFunctionType::DoubleMap{ + hasher: StorageHasher::Blake2_256, key1: DecodeDifferent::Encode("T::BlockNumber"), key2: DecodeDifferent::Encode("T::BlockNumber"), value: DecodeDifferent::Encode("T::BlockNumber"), @@ -415,6 +420,7 @@ mod tests { name: DecodeDifferent::Encode("GenericData2DM"), modifier: StorageFunctionModifier::Optional, ty: StorageFunctionType::DoubleMap{ + hasher: StorageHasher::Blake2_256, key1: DecodeDifferent::Encode("T::BlockNumber"), key2: DecodeDifferent::Encode("T::BlockNumber"), value: DecodeDifferent::Encode("T::BlockNumber"), diff --git a/srml/support/src/metadata.rs b/srml/support/src/metadata.rs index f7594d27b7c2b..407408b52c847 100644 --- a/srml/support/src/metadata.rs +++ b/srml/support/src/metadata.rs @@ -16,10 +16,14 @@ pub use srml_metadata::{ DecodeDifferent, FnEncode, RuntimeMetadata, - ModuleMetadata, RuntimeMetadataV3, + ModuleMetadata, RuntimeMetadataV4, DefaultByteGetter, RuntimeMetadataPrefixed, + StorageMetadata, StorageFunctionMetadata, + StorageFunctionType, StorageFunctionModifier, + DefaultByte, StorageHasher }; + /// Implements the metadata support for the given runtime and all its modules. /// /// Example: @@ -36,8 +40,8 @@ macro_rules! impl_runtime_metadata { ) => { impl $runtime { pub fn metadata() -> $crate::metadata::RuntimeMetadataPrefixed { - $crate::metadata::RuntimeMetadata::V3 ( - $crate::metadata::RuntimeMetadataV3 { + $crate::metadata::RuntimeMetadata::V4 ( + $crate::metadata::RuntimeMetadataV4 { modules: $crate::__runtime_modules_to_metadata!($runtime;; $( $rest )*), } ).into() @@ -377,8 +381,8 @@ mod tests { event_module2::Module with Event Storage Call, ); - const EXPECTED_METADATA: RuntimeMetadata = RuntimeMetadata::V3( - RuntimeMetadataV3 { + const EXPECTED_METADATA: RuntimeMetadata = RuntimeMetadata::V4( + RuntimeMetadataV4 { modules: DecodeDifferent::Encode(&[ ModuleMetadata { name: DecodeDifferent::Encode("system"), diff --git a/srml/support/src/storage/child.rs b/srml/support/src/storage/child.rs new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/srml/support/src/storage/hashed/generator.rs b/srml/support/src/storage/hashed/generator.rs new file mode 100644 index 0000000000000..06ec85995d25e --- /dev/null +++ b/srml/support/src/storage/hashed/generator.rs @@ -0,0 +1,257 @@ +// Copyright 2019 Parity Technologies (UK) Ltd. +// This file is part of Substrate. + +// Substrate is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Substrate is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Substrate. If not, see . + +//! Abstract storage to use on HashedStorage trait + +use crate::codec; +use crate::rstd::prelude::{Vec, Box}; +#[cfg(feature = "std")] +use crate::storage::unhashed::generator::UnhashedStorage; +use runtime_io::{twox_64, twox_128, blake2_128, twox_256, blake2_256}; + +pub trait StorageHasher: 'static { + type Output: AsRef<[u8]>; + fn hash(x: &[u8]) -> Self::Output; +} + +/// Hash storage keys with `concat(twox128(key), key)` +pub struct Twox64Concat; +impl StorageHasher for Twox64Concat { + type Output = Vec; + fn hash(x: &[u8]) -> Vec { + twox_64(x) + .into_iter() + .chain(x.into_iter()) + .cloned() + .collect::>() + } +} + +#[test] +fn test_twox_64_concat() { + let r = Twox64Concat::hash(b"foo"); + assert_eq!(r.split_at(8), (&twox_128(b"foo")[..8], &b"foo"[..])) +} + +/// Hash storage keys with blake2 128 +pub struct Blake2_128; +impl StorageHasher for Blake2_128 { + type Output = [u8; 16]; + fn hash(x: &[u8]) -> [u8; 16] { + blake2_128(x) + } +} + +/// Hash storage keys with blake2 256 +pub struct Blake2_256; +impl StorageHasher for Blake2_256 { + type Output = [u8; 32]; + fn hash(x: &[u8]) -> [u8; 32] { + blake2_256(x) + } +} + +/// Hash storage keys with twox 128 +pub struct Twox128; +impl StorageHasher for Twox128 { + type Output = [u8; 16]; + fn hash(x: &[u8]) -> [u8; 16] { + twox_128(x) + } +} + +/// Hash storage keys with twox 256 +pub struct Twox256; +impl StorageHasher for Twox256 { + type Output = [u8; 32]; + fn hash(x: &[u8]) -> [u8; 32] { + twox_256(x) + } +} + +/// Abstraction around storage. +pub trait HashedStorage { + /// true if the key exists in storage. + fn exists(&self, key: &[u8]) -> bool; + + /// Load the bytes of a key from storage. Can panic if the type is incorrect. + fn get(&self, key: &[u8]) -> Option; + + /// Load the bytes of a key from storage. Can panic if the type is incorrect. Will panic if + /// it's not there. + fn require(&self, key: &[u8]) -> T { + self.get(key).expect("Required values must be in storage") + } + + /// Load the bytes of a key from storage. Can panic if the type is incorrect. The type's + /// default is returned if it's not there. + fn get_or_default(&self, key: &[u8]) -> T { + self.get(key).unwrap_or_default() + } + + /// Put a value in under a key. + fn put(&self, key: &[u8], val: &T); + + /// Remove the bytes of a key from storage. + fn kill(&self, key: &[u8]); + + /// Take a value from storage, deleting it after reading. + fn take(&self, key: &[u8]) -> Option { + let value = self.get(key); + self.kill(key); + value + } + + /// Take a value from storage, deleting it after reading. + fn take_or_panic(&self, key: &[u8]) -> T { + self.take(key).expect("Required values must be in storage") + } + + /// Take a value from storage, deleting it after reading. + fn take_or_default(&self, key: &[u8]) -> T { + self.take(key).unwrap_or_default() + } +} + +// We use a construct like this during when genesis storage is being built. +#[cfg(feature = "std")] +impl HashedStorage for std::cell::RefCell<&mut sr_primitives::StorageOverlay> { + fn exists(&self, key: &[u8]) -> bool { + UnhashedStorage::exists(self, &H::hash(key).as_ref()) + } + + fn get(&self, key: &[u8]) -> Option { + UnhashedStorage::get(self, &H::hash(key).as_ref()) + } + + fn put(&self, key: &[u8], val: &T) { + UnhashedStorage::put(self, &H::hash(key).as_ref(), val) + } + + fn kill(&self, key: &[u8]) { + UnhashedStorage::kill(self, &H::hash(key).as_ref()) + } +} + +/// A strongly-typed value kept in storage. +pub trait StorageValue { + /// The type that get/take returns. + type Query; + + /// Get the storage key. + fn key() -> &'static [u8]; + + /// true if the value is defined in storage. + fn exists>(storage: &S) -> bool { + storage.exists(Self::key()) + } + + /// Load the value from the provided storage instance. + fn get>(storage: &S) -> Self::Query; + + /// Take a value from storage, removing it afterwards. + fn take>(storage: &S) -> Self::Query; + + /// Store a value under this key into the provided storage instance. + fn put>(val: &T, storage: &S) { + storage.put(Self::key(), val) + } + + /// Mutate this value + fn mutate R, S: HashedStorage>(f: F, storage: &S) -> R; + + /// Clear the storage value. + fn kill>(storage: &S) { + storage.kill(Self::key()) + } +} + +/// A strongly-typed list in storage. +pub trait StorageList { + /// Get the prefix key in storage. + fn prefix() -> &'static [u8]; + + /// Get the key used to put the length field. + fn len_key() -> Vec; + + /// Get the storage key used to fetch a value at a given index. + fn key_for(index: u32) -> Vec; + + /// Read out all the items. + fn items>(storage: &S) -> Vec; + + /// Set the current set of items. + fn set_items>(items: &[T], storage: &S); + + /// Set the item at the given index. + fn set_item>(index: u32, item: &T, storage: &S); + + /// Load the value at given index. Returns `None` if the index is out-of-bounds. + fn get>(index: u32, storage: &S) -> Option; + + /// Load the length of the list + fn len>(storage: &S) -> u32; + + /// Clear the list. + fn clear>(storage: &S); +} + +/// A strongly-typed map in storage. +pub trait StorageMap { + /// The type that get/take returns. + type Query; + + type Hasher: StorageHasher; + + /// Get the prefix key in storage. + fn prefix() -> &'static [u8]; + + /// Get the storage key used to fetch a value corresponding to a specific key. + fn key_for(x: &K) -> Vec; + + /// true if the value is defined in storage. + fn exists>(key: &K, storage: &S) -> bool { + storage.exists(&Self::key_for(key)[..]) + } + + /// Load the value associated with the given key from the map. + fn get>(key: &K, storage: &S) -> Self::Query; + + /// Take the value under a key. + fn take>(key: &K, storage: &S) -> Self::Query; + + /// Store a value to be associated with the given key from the map. + fn insert>(key: &K, val: &V, storage: &S) { + storage.put(&Self::key_for(key)[..], val); + } + + /// Remove the value under a key. + fn remove>(key: &K, storage: &S) { + storage.kill(&Self::key_for(key)[..]); + } + + /// Mutate the value under a key. + fn mutate R, S: HashedStorage>(key: &K, f: F, storage: &S) -> R; +} + +/// A `StorageMap` with enumerable entries. +pub trait EnumerableStorageMap: StorageMap { + /// Return current head element. + fn head>(storage: &S) -> Option; + + /// Enumerate all elements in the map. + fn enumerate<'a, S: HashedStorage>(storage: &'a S) -> Box + 'a> where K: 'a, V: 'a; +} diff --git a/srml/support/src/storage/hashed/mod.rs b/srml/support/src/storage/hashed/mod.rs new file mode 100644 index 0000000000000..5c65cf0513b26 --- /dev/null +++ b/srml/support/src/storage/hashed/mod.rs @@ -0,0 +1,223 @@ +// Copyright 2019 Parity Technologies (UK) Ltd. +// This file is part of Substrate. + +// Substrate is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Substrate is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Substrate. If not, see . + +//! Operation on runtime storage using hashed keys. + +pub mod generator; +use super::unhashed; +use crate::rstd::prelude::*; +use crate::rstd::borrow::Borrow; +use runtime_io::{self, twox_128}; +use crate::codec::{Codec, Encode, Decode, KeyedVec}; + +/// Return the value of the item in storage under `key`, or `None` if there is no explicit entry. +pub fn get R, R: AsRef<[u8]>>(hash: &HashFn, key: &[u8]) -> Option { + unhashed::get(&hash(key).as_ref()) +} + +/// Return the value of the item in storage under `key`, or the type's default if there is no +/// explicit entry. +pub fn get_or_default R, R: AsRef<[u8]>>(hash: &HashFn, key: &[u8]) -> T { + unhashed::get_or_default(&hash(key).as_ref()) +} + +/// Return the value of the item in storage under `key`, or `default_value` if there is no +/// explicit entry. +pub fn get_or R, R: AsRef<[u8]>>(hash: &HashFn, key: &[u8], default_value: T) -> T { + unhashed::get_or(&hash(key).as_ref(), default_value) +} + +/// Return the value of the item in storage under `key`, or `default_value()` if there is no +/// explicit entry. +pub fn get_or_else T, HashFn: Fn(&[u8]) -> R, R: AsRef<[u8]>>(hash: &HashFn, key: &[u8], default_value: F) -> T { + unhashed::get_or_else(&hash(key).as_ref(), default_value) +} + +/// Put `value` in storage under `key`. +pub fn put R, R: AsRef<[u8]>>(hash: &HashFn, key: &[u8], value: &T) { + unhashed::put(&hash(key).as_ref(), value) +} + +/// Remove `key` from storage, returning its value if it had an explicit entry or `None` otherwise. +pub fn take R, R: AsRef<[u8]>>(hash: &HashFn, key: &[u8]) -> Option { + unhashed::take(&hash(key).as_ref()) +} + +/// Remove `key` from storage, returning its value, or, if there was no explicit entry in storage, +/// the default for its type. +pub fn take_or_default R, R: AsRef<[u8]>>(hash: &HashFn, key: &[u8]) -> T { + unhashed::take_or_default(&hash(key).as_ref()) +} + +/// Return the value of the item in storage under `key`, or `default_value` if there is no +/// explicit entry. Ensure there is no explicit entry on return. +pub fn take_or R, R: AsRef<[u8]>>(hash: &HashFn, key: &[u8], default_value: T) -> T { + unhashed::take_or(&hash(key).as_ref(), default_value) +} + +/// Return the value of the item in storage under `key`, or `default_value()` if there is no +/// explicit entry. Ensure there is no explicit entry on return. +pub fn take_or_else T, HashFn: Fn(&[u8]) -> R, R: AsRef<[u8]>>(hash: &HashFn, key: &[u8], default_value: F) -> T { + unhashed::take_or_else(&hash(key).as_ref(), default_value) +} + +/// Check to see if `key` has an explicit entry in storage. +pub fn exists R, R: AsRef<[u8]>>(hash: &HashFn, key: &[u8]) -> bool { + unhashed::exists(&hash(key).as_ref()) +} + +/// Ensure `key` has no explicit entry in storage. +pub fn kill R, R: AsRef<[u8]>>(hash: &HashFn, key: &[u8]) { + unhashed::kill(&hash(key).as_ref()) +} + +/// Get a Vec of bytes from storage. +pub fn get_raw R, R: AsRef<[u8]>>(hash: &HashFn, key: &[u8]) -> Option> { + unhashed::get_raw(&hash(key).as_ref()) +} + +/// Put a raw byte slice into storage. +pub fn put_raw R, R: AsRef<[u8]>>(hash: &HashFn, key: &[u8], value: &[u8]) { + unhashed::put_raw(&hash(key).as_ref(), value) +} + +/// A trait to conveniently store a vector of storable data. +/// +/// It uses twox_128 hasher. Final keys in trie are `twox_128(concatenation(PREFIX,count))` +pub trait StorageVec { + type Item: Default + Sized + Codec; + const PREFIX: &'static [u8]; + + /// Get the current set of items. + fn items() -> Vec { + (0..Self::count()).into_iter().map(Self::item).collect() + } + + /// Set the current set of items. + fn set_items(items: I) + where + I: IntoIterator, + T: Borrow, + { + let mut count: u32 = 0; + + for i in items.into_iter() { + put(&twox_128, &count.to_keyed_vec(Self::PREFIX), i.borrow()); + count = count.checked_add(1).expect("exceeded runtime storage capacity"); + } + + Self::set_count(count); + } + + /// Push an item. + fn push(item: &Self::Item) { + let len = Self::count(); + put(&twox_128, &len.to_keyed_vec(Self::PREFIX), item); + Self::set_count(len + 1); + } + + fn set_item(index: u32, item: &Self::Item) { + if index < Self::count() { + put(&twox_128, &index.to_keyed_vec(Self::PREFIX), item); + } + } + + fn clear_item(index: u32) { + if index < Self::count() { + kill(&twox_128, &index.to_keyed_vec(Self::PREFIX)); + } + } + + fn item(index: u32) -> Self::Item { + get_or_default(&twox_128, &index.to_keyed_vec(Self::PREFIX)) + } + + fn set_count(count: u32) { + (count..Self::count()).for_each(Self::clear_item); + put(&twox_128, &b"len".to_keyed_vec(Self::PREFIX), &count); + } + + fn count() -> u32 { + get_or_default(&twox_128, &b"len".to_keyed_vec(Self::PREFIX)) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use runtime_io::{twox_128, TestExternalities, with_externalities}; + + #[test] + fn integers_can_be_stored() { + let mut t = TestExternalities::default(); + with_externalities(&mut t, || { + let x = 69u32; + put(&twox_128, b":test", &x); + let y: u32 = get(&twox_128, b":test").unwrap(); + assert_eq!(x, y); + }); + with_externalities(&mut t, || { + let x = 69426942i64; + put(&twox_128, b":test", &x); + let y: i64 = get(&twox_128, b":test").unwrap(); + assert_eq!(x, y); + }); + } + + #[test] + fn bools_can_be_stored() { + let mut t = TestExternalities::default(); + with_externalities(&mut t, || { + let x = true; + put(&twox_128, b":test", &x); + let y: bool = get(&twox_128, b":test").unwrap(); + assert_eq!(x, y); + }); + + with_externalities(&mut t, || { + let x = false; + put(&twox_128, b":test", &x); + let y: bool = get(&twox_128, b":test").unwrap(); + assert_eq!(x, y); + }); + } + + #[test] + fn vecs_can_be_retrieved() { + let mut t = TestExternalities::default(); + with_externalities(&mut t, || { + runtime_io::set_storage(&twox_128(b":test"), b"\x2cHello world"); + let x = b"Hello world".to_vec(); + let y = get::, _, _>(&twox_128, b":test").unwrap(); + assert_eq!(x, y); + }); + } + + #[test] + fn vecs_can_be_stored() { + let mut t = TestExternalities::default(); + let x = b"Hello world".to_vec(); + + with_externalities(&mut t, || { + put(&twox_128, b":test", &x); + }); + + with_externalities(&mut t, || { + let y: Vec = get(&twox_128, b":test").unwrap(); + assert_eq!(x, y); + }); + } +} diff --git a/srml/support/src/storage/mod.rs b/srml/support/src/storage/mod.rs index b1b8766b9028a..eaf1282764bba 100644 --- a/srml/support/src/storage/mod.rs +++ b/srml/support/src/storage/mod.rs @@ -18,12 +18,14 @@ use crate::rstd::prelude::*; use crate::rstd::borrow::Borrow; -use runtime_io::{self, twox_128}; use crate::codec::{Codec, Encode, Decode, KeyedVec, Input}; +use hashed::generator::{HashedStorage, StorageHasher}; +use unhashed::generator::UnhashedStorage; #[macro_use] -pub mod generator; +pub mod storage_items; pub mod unhashed; +pub mod hashed; struct IncrementalInput<'a> { key: &'a [u8], @@ -54,108 +56,36 @@ impl<'a> Input for IncrementalChildInput<'a> { } } - -/// Return the value of the item in storage under `key`, or `None` if there is no explicit entry. -pub fn get(key: &[u8]) -> Option { - unhashed::get(&twox_128(key)) -} - -/// Return the value of the item in storage under `key`, or the type's default if there is no -/// explicit entry. -pub fn get_or_default(key: &[u8]) -> T { - unhashed::get_or_default(&twox_128(key)) -} - -/// Return the value of the item in storage under `key`, or `default_value` if there is no -/// explicit entry. -pub fn get_or(key: &[u8], default_value: T) -> T { - unhashed::get_or(&twox_128(key), default_value) -} - -/// Return the value of the item in storage under `key`, or `default_value()` if there is no -/// explicit entry. -pub fn get_or_else T>(key: &[u8], default_value: F) -> T { - unhashed::get_or_else(&twox_128(key), default_value) -} - -/// Put `value` in storage under `key`. -pub fn put(key: &[u8], value: &T) { - unhashed::put(&twox_128(key), value) -} - -/// Remove `key` from storage, returning its value if it had an explicit entry or `None` otherwise. -pub fn take(key: &[u8]) -> Option { - unhashed::take(&twox_128(key)) -} - -/// Remove `key` from storage, returning its value, or, if there was no explicit entry in storage, -/// the default for its type. -pub fn take_or_default(key: &[u8]) -> T { - unhashed::take_or_default(&twox_128(key)) -} - -/// Return the value of the item in storage under `key`, or `default_value` if there is no -/// explicit entry. Ensure there is no explicit entry on return. -pub fn take_or(key: &[u8], default_value: T) -> T { - unhashed::take_or(&twox_128(key), default_value) -} - -/// Return the value of the item in storage under `key`, or `default_value()` if there is no -/// explicit entry. Ensure there is no explicit entry on return. -pub fn take_or_else T>(key: &[u8], default_value: F) -> T { - unhashed::take_or_else(&twox_128(key), default_value) -} - -/// Check to see if `key` has an explicit entry in storage. -pub fn exists(key: &[u8]) -> bool { - unhashed::exists(&twox_128(key)) -} - -/// Ensure `key` has no explicit entry in storage. -pub fn kill(key: &[u8]) { - unhashed::kill(&twox_128(key)) -} - -/// Get a Vec of bytes from storage. -pub fn get_raw(key: &[u8]) -> Option> { - unhashed::get_raw(&twox_128(key)) -} - -/// Put a raw byte slice into storage. -pub fn put_raw(key: &[u8], value: &[u8]) { - unhashed::put_raw(&twox_128(key), value) -} - /// The underlying runtime storage. pub struct RuntimeStorage; -impl crate::GenericStorage for RuntimeStorage { +impl HashedStorage for RuntimeStorage { fn exists(&self, key: &[u8]) -> bool { - exists(key) + hashed::exists(&H::hash, key) } /// Load the bytes of a key from storage. Can panic if the type is incorrect. fn get(&self, key: &[u8]) -> Option { - get(key) + hashed::get(&H::hash, key) } /// Put a value in under a key. fn put(&self, key: &[u8], val: &T) { - put(key, val) + hashed::put(&H::hash, key, val) } /// Remove the bytes of a key from storage. fn kill(&self, key: &[u8]) { - kill(key) + hashed::kill(&H::hash, key) } /// Take a value from storage, deleting it after reading. fn take(&self, key: &[u8]) -> Option { - take(key) + hashed::take(&H::hash, key) } } -impl crate::GenericUnhashedStorage for RuntimeStorage { +impl UnhashedStorage for RuntimeStorage { fn exists(&self, key: &[u8]) -> bool { unhashed::exists(key) } @@ -213,11 +143,11 @@ pub trait StorageValue { fn take() -> Self::Query; } -impl StorageValue for U where U: generator::StorageValue { +impl StorageValue for U where U: hashed::generator::StorageValue { type Query = U::Query; fn key() -> &'static [u8] { - >::key() + >::key() } fn exists() -> bool { U::exists(&RuntimeStorage) @@ -269,17 +199,17 @@ pub trait StorageList { fn clear(); } -impl StorageList for U where U: generator::StorageList { +impl StorageList for U where U: hashed::generator::StorageList { fn prefix() -> &'static [u8] { - >::prefix() + >::prefix() } fn len_key() -> Vec { - >::len_key() + >::len_key() } fn key_for(index: u32) -> Vec { - >::key_for(index) + >::key_for(index) } fn items() -> Vec { @@ -337,15 +267,15 @@ pub trait StorageMap { fn take>(key: KeyArg) -> Self::Query; } -impl StorageMap for U where U: generator::StorageMap { +impl StorageMap for U where U: hashed::generator::StorageMap { type Query = U::Query; fn prefix() -> &'static [u8] { - >::prefix() + >::prefix() } fn key_for>(key: KeyArg) -> Vec { - >::key_for(key.borrow()) + >::key_for(key.borrow()) } fn exists>(key: KeyArg) -> bool { @@ -385,13 +315,13 @@ pub trait EnumerableStorageMap: StorageMap { fn enumerate() -> Box> where K: 'static, V: 'static; } -impl EnumerableStorageMap for U where U: generator::EnumerableStorageMap { +impl EnumerableStorageMap for U where U: hashed::generator::EnumerableStorageMap { fn head() -> Option { - >::head(&RuntimeStorage) + >::head(&RuntimeStorage) } fn enumerate() -> Box> where K: 'static, V: 'static { - >::enumerate(&RuntimeStorage) + >::enumerate(&RuntimeStorage) } } @@ -498,72 +428,13 @@ where } } -/// A trait to conveniently store a vector of storable data. -pub trait StorageVec { - type Item: Default + Sized + Codec; - const PREFIX: &'static [u8]; - - /// Get the current set of items. - fn items() -> Vec { - (0..Self::count()).into_iter().map(Self::item).collect() - } - - /// Set the current set of items. - fn set_items(items: I) - where - I: IntoIterator, - T: Borrow, - { - let mut count: u32 = 0; - - for i in items.into_iter() { - put(&count.to_keyed_vec(Self::PREFIX), i.borrow()); - count = count.checked_add(1).expect("exceeded runtime storage capacity"); - } - - Self::set_count(count); - } - - /// Push an item. - fn push(item: &Self::Item) { - let len = Self::count(); - put(&len.to_keyed_vec(Self::PREFIX), item); - Self::set_count(len + 1); - } - - fn set_item(index: u32, item: &Self::Item) { - if index < Self::count() { - put(&index.to_keyed_vec(Self::PREFIX), item); - } - } - - fn clear_item(index: u32) { - if index < Self::count() { - kill(&index.to_keyed_vec(Self::PREFIX)); - } - } - - fn item(index: u32) -> Self::Item { - get_or_default(&index.to_keyed_vec(Self::PREFIX)) - } - - fn set_count(count: u32) { - (count..Self::count()).for_each(Self::clear_item); - put(&b"len".to_keyed_vec(Self::PREFIX), &count); - } - - fn count() -> u32 { - get_or_default(&b"len".to_keyed_vec(Self::PREFIX)) - } -} - /// child storage NOTE could replace unhashed by having only one kind of storage (root being null storage /// key (storage_key can become Option<&[u8]>). /// This module is a currently only a variant of unhashed with additional `storage_key`. /// Note that `storage_key` must be unique and strong (strong in the sense of being long enough to /// avoid collision from a resistant hash function (which unique implies)). pub mod child { - use super::{runtime_io, Codec, Decode, Vec, IncrementalChildInput}; + use super::{Codec, Decode, Vec, IncrementalChildInput}; /// Return the value of the item in storage under `key`, or `None` if there is no explicit entry. pub fn get(storage_key: &[u8], key: &[u8]) -> Option { @@ -654,71 +525,3 @@ pub mod child { pub use super::unhashed::StorageVec; } - -#[cfg(test)] -mod tests { - use super::*; - use runtime_io::{twox_128, TestExternalities, with_externalities}; - - #[test] - fn integers_can_be_stored() { - let mut t = TestExternalities::default(); - with_externalities(&mut t, || { - let x = 69u32; - put(b":test", &x); - let y: u32 = get(b":test").unwrap(); - assert_eq!(x, y); - }); - with_externalities(&mut t, || { - let x = 69426942i64; - put(b":test", &x); - let y: i64 = get(b":test").unwrap(); - assert_eq!(x, y); - }); - } - - #[test] - fn bools_can_be_stored() { - let mut t = TestExternalities::default(); - with_externalities(&mut t, || { - let x = true; - put(b":test", &x); - let y: bool = get(b":test").unwrap(); - assert_eq!(x, y); - }); - - with_externalities(&mut t, || { - let x = false; - put(b":test", &x); - let y: bool = get(b":test").unwrap(); - assert_eq!(x, y); - }); - } - - #[test] - fn vecs_can_be_retrieved() { - let mut t = TestExternalities::default(); - with_externalities(&mut t, || { - runtime_io::set_storage(&twox_128(b":test"), b"\x2cHello world"); - let x = b"Hello world".to_vec(); - let y = get::>(b":test").unwrap(); - assert_eq!(x, y); - - }); - } - - #[test] - fn vecs_can_be_stored() { - let mut t = TestExternalities::default(); - let x = b"Hello world".to_vec(); - - with_externalities(&mut t, || { - put(b":test", &x); - }); - - with_externalities(&mut t, || { - let y: Vec = get(b":test").unwrap(); - assert_eq!(x, y); - }); - } -} diff --git a/srml/support/src/storage/generator.rs b/srml/support/src/storage/storage_items.rs similarity index 74% rename from srml/support/src/storage/generator.rs rename to srml/support/src/storage/storage_items.rs index 97bfc6dc200ec..720cac64c5361 100644 --- a/srml/support/src/storage/generator.rs +++ b/srml/support/src/storage/storage_items.rs @@ -46,10 +46,6 @@ //!# fn main() { } //! ``` -use crate::codec; -use crate::rstd::vec::Vec; -#[cfg(feature = "std")] -use crate::storage::unhashed::generator::UnhashedStorage; #[doc(hidden)] pub use crate::rstd::borrow::Borrow; #[doc(hidden)] @@ -57,176 +53,6 @@ pub use crate::rstd::marker::PhantomData; #[doc(hidden)] pub use crate::rstd::boxed::Box; -pub use srml_metadata::{ - DecodeDifferent, StorageMetadata, StorageFunctionMetadata, - StorageFunctionType, StorageFunctionModifier, - DefaultByte, DefaultByteGetter, -}; - -/// Abstraction around storage. -pub trait Storage { - /// true if the key exists in storage. - fn exists(&self, key: &[u8]) -> bool; - - /// Load the bytes of a key from storage. Can panic if the type is incorrect. - fn get(&self, key: &[u8]) -> Option; - - /// Load the bytes of a key from storage. Can panic if the type is incorrect. Will panic if - /// it's not there. - fn require(&self, key: &[u8]) -> T { self.get(key).expect("Required values must be in storage") } - - /// Load the bytes of a key from storage. Can panic if the type is incorrect. The type's - /// default is returned if it's not there. - fn get_or_default(&self, key: &[u8]) -> T { self.get(key).unwrap_or_default() } - - /// Put a value in under a key. - fn put(&self, key: &[u8], val: &T); - - /// Remove the bytes of a key from storage. - fn kill(&self, key: &[u8]); - - /// Take a value from storage, deleting it after reading. - fn take(&self, key: &[u8]) -> Option { - let value = self.get(key); - self.kill(key); - value - } - - /// Take a value from storage, deleting it after reading. - fn take_or_panic(&self, key: &[u8]) -> T { self.take(key).expect("Required values must be in storage") } - - /// Take a value from storage, deleting it after reading. - fn take_or_default(&self, key: &[u8]) -> T { self.take(key).unwrap_or_default() } -} - -// We use a construct like this during when genesis storage is being built. -#[cfg(feature = "std")] -impl Storage for (crate::rstd::cell::RefCell<&mut sr_primitives::StorageOverlay>, PhantomData) { - fn exists(&self, key: &[u8]) -> bool { - UnhashedStorage::exists(self, &S::hash(key)) - } - - fn get(&self, key: &[u8]) -> Option { - UnhashedStorage::get(self, &S::hash(key)) - } - - fn put(&self, key: &[u8], val: &T) { - UnhashedStorage::put(self, &S::hash(key), val) - } - - fn kill(&self, key: &[u8]) { - UnhashedStorage::kill(self, &S::hash(key)) - } -} - -/// A strongly-typed value kept in storage. -pub trait StorageValue { - /// The type that get/take returns. - type Query; - - /// Get the storage key. - fn key() -> &'static [u8]; - - /// true if the value is defined in storage. - fn exists(storage: &S) -> bool { - storage.exists(Self::key()) - } - - /// Load the value from the provided storage instance. - fn get(storage: &S) -> Self::Query; - - /// Take a value from storage, removing it afterwards. - fn take(storage: &S) -> Self::Query; - - /// Store a value under this key into the provided storage instance. - fn put(val: &T, storage: &S) { - storage.put(Self::key(), val) - } - - /// Mutate this value - fn mutate R, S: Storage>(f: F, storage: &S) -> R; - - /// Clear the storage value. - fn kill(storage: &S) { - storage.kill(Self::key()) - } -} - -/// A strongly-typed list in storage. -pub trait StorageList { - /// Get the prefix key in storage. - fn prefix() -> &'static [u8]; - - /// Get the key used to put the length field. - fn len_key() -> Vec; - - /// Get the storage key used to fetch a value at a given index. - fn key_for(index: u32) -> Vec; - - /// Read out all the items. - fn items(storage: &S) -> Vec; - - /// Set the current set of items. - fn set_items(items: &[T], storage: &S); - - /// Set the item at the given index. - fn set_item(index: u32, item: &T, storage: &S); - - /// Load the value at given index. Returns `None` if the index is out-of-bounds. - fn get(index: u32, storage: &S) -> Option; - - /// Load the length of the list - fn len(storage: &S) -> u32; - - /// Clear the list. - fn clear(storage: &S); -} - -/// A strongly-typed map in storage. -pub trait StorageMap { - /// The type that get/take returns. - type Query; - - /// Get the prefix key in storage. - fn prefix() -> &'static [u8]; - - /// Get the storage key used to fetch a value corresponding to a specific key. - fn key_for(x: &K) -> Vec; - - /// true if the value is defined in storage. - fn exists(key: &K, storage: &S) -> bool { - storage.exists(&Self::key_for(key)[..]) - } - - /// Load the value associated with the given key from the map. - fn get(key: &K, storage: &S) -> Self::Query; - - /// Take the value under a key. - fn take(key: &K, storage: &S) -> Self::Query; - - /// Store a value to be associated with the given key from the map. - fn insert(key: &K, val: &V, storage: &S) { - storage.put(&Self::key_for(key)[..], val); - } - - /// Remove the value under a key. - fn remove(key: &K, storage: &S) { - storage.kill(&Self::key_for(key)[..]); - } - - /// Mutate the value under a key. - fn mutate R, S: Storage>(key: &K, f: F, storage: &S) -> R; -} - -/// A `StorageMap` with enumerable entries. -pub trait EnumerableStorageMap: StorageMap { - /// Return current head element. - fn head(storage: &S) -> Option; - - /// Enumerate all elements in the map. - fn enumerate<'a, S: Storage>(storage: &'a S) -> Box + 'a> where K: 'a, V: 'a; -} - // FIXME #1466 Remove this in favor of `decl_storage` macro. /// Declares strongly-typed wrappers around codec-compatible types in storage. #[macro_export] @@ -352,12 +178,12 @@ macro_rules! __storage_items_internal { // generator for values. (($($vis:tt)*) ($get_fn:ident) ($wraptype:ident $gettype:ty) ($getter:ident) ($taker:ident) $name:ident : $key:expr => $ty:ty) => { $crate::__storage_items_internal!{ ($($vis)*) () ($wraptype $gettype) ($getter) ($taker) $name : $key => $ty } - pub fn $get_fn() -> $gettype { <$name as $crate::storage::generator::StorageValue<$ty>> :: get(&$crate::storage::RuntimeStorage) } + pub fn $get_fn() -> $gettype { <$name as $crate::storage::hashed::generator::StorageValue<$ty>> :: get(&$crate::storage::RuntimeStorage) } }; (($($vis:tt)*) () ($wraptype:ident $gettype:ty) ($getter:ident) ($taker:ident) $name:ident : $key:expr => $ty:ty) => { $($vis)* struct $name; - impl $crate::storage::generator::StorageValue<$ty> for $name { + impl $crate::storage::hashed::generator::StorageValue<$ty> for $name { type Query = $gettype; /// Get the storage key. @@ -366,29 +192,29 @@ macro_rules! __storage_items_internal { } /// Load the value from the provided storage instance. - fn get(storage: &S) -> Self::Query { + fn get>(storage: &S) -> Self::Query { storage.$getter($key) } /// Take a value from storage, removing it afterwards. - fn take(storage: &S) -> Self::Query { + fn take>(storage: &S) -> Self::Query { storage.$taker($key) } /// Mutate this value. - fn mutate R, S: $crate::GenericStorage>(f: F, storage: &S) -> R { - let mut val = >::get(storage); + fn mutate R, S: $crate::HashedStorage<$crate::Twox128>>(f: F, storage: &S) -> R { + let mut val = >::get(storage); let ret = f(&mut val); $crate::__handle_wrap_internal!($wraptype { // raw type case - >::put(&val, storage) + >::put(&val, storage) } { // Option<> type case match val { - Some(ref val) => >::put(&val, storage), - None => >::kill(storage), + Some(ref val) => >::put(&val, storage), + None => >::kill(storage), } }); @@ -400,15 +226,17 @@ macro_rules! __storage_items_internal { (($($vis:tt)*) ($get_fn:ident) ($wraptype:ident $gettype:ty) ($getter:ident) ($taker:ident) $name:ident : $prefix:expr => map [$kty:ty => $ty:ty]) => { $crate::__storage_items_internal!{ ($($vis)*) () ($wraptype $gettype) ($getter) ($taker) $name : $prefix => map [$kty => $ty] } pub fn $get_fn>(key: K) -> $gettype { - <$name as $crate::storage::generator::StorageMap<$kty, $ty>> :: get(key.borrow(), &$crate::storage::RuntimeStorage) + <$name as $crate::storage::hashed::generator::StorageMap<$kty, $ty>> :: get(key.borrow(), &$crate::storage::RuntimeStorage) } }; (($($vis:tt)*) () ($wraptype:ident $gettype:ty) ($getter:ident) ($taker:ident) $name:ident : $prefix:expr => map [$kty:ty => $ty:ty]) => { $($vis)* struct $name; - impl $crate::storage::generator::StorageMap<$kty, $ty> for $name { + impl $crate::storage::hashed::generator::StorageMap<$kty, $ty> for $name { type Query = $gettype; + type Hasher = $crate::Blake2_256; + /// Get the prefix key in storage. fn prefix() -> &'static [u8] { $prefix @@ -422,31 +250,31 @@ macro_rules! __storage_items_internal { } /// Load the value associated with the given key from the map. - fn get(key: &$kty, storage: &S) -> Self::Query { - let key = <$name as $crate::storage::generator::StorageMap<$kty, $ty>>::key_for(key); + fn get>(key: &$kty, storage: &S) -> Self::Query { + let key = <$name as $crate::storage::hashed::generator::StorageMap<$kty, $ty>>::key_for(key); storage.$getter(&key[..]) } /// Take the value, reading and removing it. - fn take(key: &$kty, storage: &S) -> Self::Query { - let key = <$name as $crate::storage::generator::StorageMap<$kty, $ty>>::key_for(key); + fn take>(key: &$kty, storage: &S) -> Self::Query { + let key = <$name as $crate::storage::hashed::generator::StorageMap<$kty, $ty>>::key_for(key); storage.$taker(&key[..]) } /// Mutate the value under a key. - fn mutate R, S: $crate::GenericStorage>(key: &$kty, f: F, storage: &S) -> R { - let mut val = >::take(key, storage); + fn mutate R, S: $crate::HashedStorage>(key: &$kty, f: F, storage: &S) -> R { + let mut val = >::take(key, storage); let ret = f(&mut val); $crate::__handle_wrap_internal!($wraptype { // raw type case - >::insert(key, &val, storage) + >::insert(key, &val, storage) } { // Option<> type case match val { - Some(ref val) => >::insert(key, &val, storage), - None => >::remove(key, storage), + Some(ref val) => >::insert(key, &val, storage), + None => >::remove(key, storage), } }); @@ -459,19 +287,19 @@ macro_rules! __storage_items_internal { $($vis)* struct $name; impl $name { - fn clear_item(index: u32, storage: &S) { - if index < <$name as $crate::storage::generator::StorageList<$ty>>::len(storage) { - storage.kill(&<$name as $crate::storage::generator::StorageList<$ty>>::key_for(index)); + fn clear_item>(index: u32, storage: &S) { + if index < <$name as $crate::storage::hashed::generator::StorageList<$ty>>::len(storage) { + storage.kill(&<$name as $crate::storage::hashed::generator::StorageList<$ty>>::key_for(index)); } } - fn set_len(count: u32, storage: &S) { - (count..<$name as $crate::storage::generator::StorageList<$ty>>::len(storage)).for_each(|i| $name::clear_item(i, storage)); - storage.put(&<$name as $crate::storage::generator::StorageList<$ty>>::len_key(), &count); + fn set_len>(count: u32, storage: &S) { + (count..<$name as $crate::storage::hashed::generator::StorageList<$ty>>::len(storage)).for_each(|i| $name::clear_item(i, storage)); + storage.put(&<$name as $crate::storage::hashed::generator::StorageList<$ty>>::len_key(), &count); } } - impl $crate::storage::generator::StorageList<$ty> for $name { + impl $crate::storage::hashed::generator::StorageList<$ty> for $name { /// Get the prefix key in storage. fn prefix() -> &'static [u8] { $prefix @@ -492,43 +320,43 @@ macro_rules! __storage_items_internal { } /// Read out all the items. - fn items(storage: &S) -> $crate::rstd::vec::Vec<$ty> { - (0..<$name as $crate::storage::generator::StorageList<$ty>>::len(storage)) - .map(|i| <$name as $crate::storage::generator::StorageList<$ty>>::get(i, storage).expect("all items within length are set; qed")) + fn items>(storage: &S) -> $crate::rstd::vec::Vec<$ty> { + (0..<$name as $crate::storage::hashed::generator::StorageList<$ty>>::len(storage)) + .map(|i| <$name as $crate::storage::hashed::generator::StorageList<$ty>>::get(i, storage).expect("all items within length are set; qed")) .collect() } /// Set the current set of items. - fn set_items(items: &[$ty], storage: &S) { + fn set_items>(items: &[$ty], storage: &S) { $name::set_len(items.len() as u32, storage); items.iter() .enumerate() - .for_each(|(i, item)| <$name as $crate::storage::generator::StorageList<$ty>>::set_item(i as u32, item, storage)); + .for_each(|(i, item)| <$name as $crate::storage::hashed::generator::StorageList<$ty>>::set_item(i as u32, item, storage)); } - fn set_item(index: u32, item: &$ty, storage: &S) { - if index < <$name as $crate::storage::generator::StorageList<$ty>>::len(storage) { - storage.put(&<$name as $crate::storage::generator::StorageList<$ty>>::key_for(index)[..], item); + fn set_item>(index: u32, item: &$ty, storage: &S) { + if index < <$name as $crate::storage::hashed::generator::StorageList<$ty>>::len(storage) { + storage.put(&<$name as $crate::storage::hashed::generator::StorageList<$ty>>::key_for(index)[..], item); } } /// Load the value at given index. Returns `None` if the index is out-of-bounds. - fn get(index: u32, storage: &S) -> Option<$ty> { - storage.get(&<$name as $crate::storage::generator::StorageList<$ty>>::key_for(index)[..]) + fn get>(index: u32, storage: &S) -> Option<$ty> { + storage.get(&<$name as $crate::storage::hashed::generator::StorageList<$ty>>::key_for(index)[..]) } /// Load the length of the list. - fn len(storage: &S) -> u32 { - storage.get(&<$name as $crate::storage::generator::StorageList<$ty>>::len_key()).unwrap_or_default() + fn len>(storage: &S) -> u32 { + storage.get(&<$name as $crate::storage::hashed::generator::StorageList<$ty>>::len_key()).unwrap_or_default() } /// Clear the list. - fn clear(storage: &S) { - for i in 0..<$name as $crate::storage::generator::StorageList<$ty>>::len(storage) { + fn clear>(storage: &S) { + for i in 0..<$name as $crate::storage::hashed::generator::StorageList<$ty>>::len(storage) { $name::clear_item(i, storage); } - storage.kill(&<$name as $crate::storage::generator::StorageList<$ty>>::len_key()[..]) + storage.kill(&<$name as $crate::storage::hashed::generator::StorageList<$ty>>::len_key()[..]) } } }; @@ -556,27 +384,11 @@ macro_rules! __handle_wrap_internal { mod tests { use std::collections::HashMap; use std::cell::RefCell; - use codec::{Decode, Encode}; use super::*; + use crate::metadata::*; + use crate::metadata::StorageHasher; use crate::rstd::marker::PhantomData; - - impl Storage for RefCell, Vec>> { - fn exists(&self, key: &[u8]) -> bool { - self.borrow_mut().get(key).is_some() - } - - fn get(&self, key: &[u8]) -> Option { - self.borrow_mut().get(key).map(|v| T::decode(&mut &v[..]).unwrap()) - } - - fn put(&self, key: &[u8], val: &T) { - self.borrow_mut().insert(key.to_owned(), val.encode()); - } - - fn kill(&self, key: &[u8]) { - self.borrow_mut().remove(key); - } - } + use crate::storage::hashed::generator::*; storage_items! { Value: b"a" => u32; @@ -586,7 +398,8 @@ mod tests { #[test] fn value() { - let storage = RefCell::new(HashMap::new()); + let mut overlay = HashMap::new(); + let storage = RefCell::new(&mut overlay); assert!(Value::get(&storage).is_none()); Value::put(&100_000, &storage); assert_eq!(Value::get(&storage), Some(100_000)); @@ -596,7 +409,8 @@ mod tests { #[test] fn list() { - let storage = RefCell::new(HashMap::new()); + let mut overlay = HashMap::new(); + let storage = RefCell::new(&mut overlay); assert_eq!(List::len(&storage), 0); assert!(List::items(&storage).is_empty()); @@ -615,7 +429,8 @@ mod tests { #[test] fn map() { - let storage = RefCell::new(HashMap::new()); + let mut overlay = HashMap::new(); + let storage = RefCell::new(&mut overlay); assert!(Map::get(&5, &storage).is_none()); Map::insert(&5, &[1; 32], &storage); assert_eq!(Map::get(&5, &storage), Some([1; 32])); @@ -625,7 +440,7 @@ mod tests { } pub trait Trait { - type Origin: codec::Encode + codec::Decode + ::std::default::Default; + type Origin: crate::codec::Encode + crate::codec::Decode + ::std::default::Default; type BlockNumber; } @@ -814,6 +629,7 @@ mod tests { name: DecodeDifferent::Encode("MAPU32"), modifier: StorageFunctionModifier::Optional, ty: StorageFunctionType::Map { + hasher: StorageHasher::Blake2_256, key: DecodeDifferent::Encode("u32"), value: DecodeDifferent::Encode("String"), is_linked: false, @@ -827,6 +643,7 @@ mod tests { name: DecodeDifferent::Encode("PUBMAPU32"), modifier: StorageFunctionModifier::Optional, ty: StorageFunctionType::Map { + hasher: StorageHasher::Blake2_256, key: DecodeDifferent::Encode("u32"), value: DecodeDifferent::Encode("String"), is_linked: false, @@ -840,6 +657,7 @@ mod tests { name: DecodeDifferent::Encode("MAPU32MYDEF"), modifier: StorageFunctionModifier::Optional, ty: StorageFunctionType::Map { + hasher: StorageHasher::Blake2_256, key: DecodeDifferent::Encode("u32"), value: DecodeDifferent::Encode("String"), is_linked: false, @@ -853,6 +671,7 @@ mod tests { name: DecodeDifferent::Encode("PUBMAPU32MYDEF"), modifier: StorageFunctionModifier::Optional, ty: StorageFunctionType::Map { + hasher: StorageHasher::Blake2_256, key: DecodeDifferent::Encode("u32"), value: DecodeDifferent::Encode("String"), is_linked: false, @@ -866,6 +685,7 @@ mod tests { name: DecodeDifferent::Encode("GETMAPU32"), modifier: StorageFunctionModifier::Default, ty: StorageFunctionType::Map { + hasher: StorageHasher::Blake2_256, key: DecodeDifferent::Encode("u32"), value: DecodeDifferent::Encode("String"), is_linked: false, @@ -879,6 +699,7 @@ mod tests { name: DecodeDifferent::Encode("PUBGETMAPU32"), modifier: StorageFunctionModifier::Default, ty: StorageFunctionType::Map { + hasher: StorageHasher::Blake2_256, key: DecodeDifferent::Encode("u32"), value: DecodeDifferent::Encode("String"), is_linked: false, @@ -892,6 +713,7 @@ mod tests { name: DecodeDifferent::Encode("GETMAPU32MYDEF"), modifier: StorageFunctionModifier::Default, ty: StorageFunctionType::Map { + hasher: StorageHasher::Blake2_256, key: DecodeDifferent::Encode("u32"), value: DecodeDifferent::Encode("String"), is_linked: false, @@ -905,6 +727,7 @@ mod tests { name: DecodeDifferent::Encode("PUBGETMAPU32MYDEF"), modifier: StorageFunctionModifier::Default, ty: StorageFunctionType::Map { + hasher: StorageHasher::Blake2_256, key: DecodeDifferent::Encode("u32"), value: DecodeDifferent::Encode("String"), is_linked: false, @@ -918,6 +741,7 @@ mod tests { name: DecodeDifferent::Encode("LINKEDMAPU32"), modifier: StorageFunctionModifier::Optional, ty: StorageFunctionType::Map { + hasher: StorageHasher::Blake2_256, key: DecodeDifferent::Encode("u32"), value: DecodeDifferent::Encode("String"), is_linked: true, @@ -931,6 +755,7 @@ mod tests { name: DecodeDifferent::Encode("PUBLINKEDMAPU32MYDEF"), modifier: StorageFunctionModifier::Optional, ty: StorageFunctionType::Map { + hasher: StorageHasher::Blake2_256, key: DecodeDifferent::Encode("u32"), value: DecodeDifferent::Encode("String"), is_linked: true, @@ -944,6 +769,7 @@ mod tests { name: DecodeDifferent::Encode("GETLINKEDMAPU32"), modifier: StorageFunctionModifier::Default, ty: StorageFunctionType::Map { + hasher: StorageHasher::Blake2_256, key: DecodeDifferent::Encode("u32"), value: DecodeDifferent::Encode("String"), is_linked: true, @@ -957,6 +783,7 @@ mod tests { name: DecodeDifferent::Encode("PUBGETLINKEDMAPU32MYDEF"), modifier: StorageFunctionModifier::Default, ty: StorageFunctionType::Map { + hasher: StorageHasher::Blake2_256, key: DecodeDifferent::Encode("u32"), value: DecodeDifferent::Encode("String"), is_linked: true, diff --git a/srml/support/src/storage/unhashed/generator.rs b/srml/support/src/storage/unhashed/generator.rs index 2b046013bb944..f485057157176 100644 --- a/srml/support/src/storage/unhashed/generator.rs +++ b/srml/support/src/storage/unhashed/generator.rs @@ -15,7 +15,6 @@ // along with Substrate. If not, see . use crate::codec; -use runtime_io::twox_128; use crate::rstd::vec::Vec; /// Abstraction around storage with unhashed access. @@ -59,26 +58,26 @@ pub trait UnhashedStorage { // We use a construct like this during when genesis storage is being built. #[cfg(feature = "std")] -impl UnhashedStorage for (crate::rstd::cell::RefCell<&mut sr_primitives::StorageOverlay>, H) { +impl UnhashedStorage for std::cell::RefCell<&mut sr_primitives::StorageOverlay> { fn exists(&self, key: &[u8]) -> bool { - self.0.borrow().contains_key(key) + self.borrow().contains_key(key) } fn get(&self, key: &[u8]) -> Option { - self.0.borrow().get(key) + self.borrow().get(key) .map(|x| codec::Decode::decode(&mut x.as_slice()).expect("Unable to decode expected type.")) } fn put(&self, key: &[u8], val: &T) { - self.0.borrow_mut().insert(key.to_vec(), codec::Encode::encode(val)); + self.borrow_mut().insert(key.to_vec(), codec::Encode::encode(val)); } fn kill(&self, key: &[u8]) { - self.0.borrow_mut().remove(key); + self.borrow_mut().remove(key); } fn kill_prefix(&self, prefix: &[u8]) { - self.0.borrow_mut().retain(|key, _| { + self.borrow_mut().retain(|key, _| { !key.starts_with(prefix) }) } @@ -107,11 +106,7 @@ pub trait StorageDoubleMap fn key_for(k1: &K1, k2: &K2) -> Vec; /// Get the storage prefix used to fetch keys corresponding to a specific key1. - fn prefix_for(k1: &K1) -> Vec { - let mut key = Self::prefix().to_vec(); - codec::Encode::encode_to(k1, &mut key); - twox_128(&key).to_vec() - } + fn prefix_for(k1: &K1) -> Vec; /// true if the value is defined in storage. fn exists(k1: &K1, k2: &K2, storage: &S) -> bool { diff --git a/srml/support/src/storage/unhashed/mod.rs b/srml/support/src/storage/unhashed/mod.rs index 225c6756b80de..40e18d0cd212a 100644 --- a/srml/support/src/storage/unhashed/mod.rs +++ b/srml/support/src/storage/unhashed/mod.rs @@ -17,7 +17,7 @@ //! Operation on unhashed runtime storage use crate::rstd::borrow::Borrow; -use super::{runtime_io, Codec, Encode, Decode, KeyedVec, Vec, IncrementalInput}; +use super::{Codec, Encode, Decode, KeyedVec, Vec, IncrementalInput}; pub mod generator;