diff --git a/substrate/Cargo.lock b/substrate/Cargo.lock index f3fc76d630..375d03fa47 100644 --- a/substrate/Cargo.lock +++ b/substrate/Cargo.lock @@ -4988,7 +4988,6 @@ dependencies = [ "sr-primitives 2.0.0", "sr-version 2.0.0", "substrate-client 2.0.0", - "substrate-executor 2.0.0", "substrate-keystore 2.0.0", "substrate-network 2.0.0", "substrate-primitives 2.0.0", diff --git a/substrate/core/client/src/genesis.rs b/substrate/core/client/src/genesis.rs index 3ac93f4f57..4791a34d17 100644 --- a/substrate/core/client/src/genesis.rs +++ b/substrate/core/client/src/genesis.rs @@ -54,8 +54,7 @@ mod tests { native_executor_instance!( Executor, test_client::runtime::api::dispatch, - test_client::runtime::native_version, - test_client::runtime::WASM_BINARY + test_client::runtime::native_version ); fn executor() -> executor::NativeExecutor { @@ -153,6 +152,7 @@ mod tests { vec![AccountKeyring::One.into(), AccountKeyring::Two.into()], 1000, None, + vec![], ).genesis_map(); let genesis_hash = insert_genesis_block(&mut storage); @@ -181,6 +181,7 @@ mod tests { vec![AccountKeyring::One.into(), AccountKeyring::Two.into()], 1000, None, + vec![], ).genesis_map(); let genesis_hash = insert_genesis_block(&mut storage); @@ -209,6 +210,7 @@ mod tests { vec![AccountKeyring::One.into(), AccountKeyring::Two.into()], 68, None, + vec![], ).genesis_map(); let genesis_hash = insert_genesis_block(&mut storage); diff --git a/substrate/core/executor/src/native_executor.rs b/substrate/core/executor/src/native_executor.rs index 937e003353..1ebf7f3146 100644 --- a/substrate/core/executor/src/native_executor.rs +++ b/substrate/core/executor/src/native_executor.rs @@ -49,9 +49,6 @@ pub fn with_native_environment(ext: &mut dyn Externalities, /// Delegate for dispatching a CodeExecutor call to native code. pub trait NativeExecutionDispatch: Send + Sync { - /// Get the wasm code that the native dispatch will be equivalent to. - fn native_equivalent() -> &'static [u8]; - /// Dispatch a method and input data to be executed natively. fn dispatch(ext: &mut dyn Externalities, method: &str, data: &[u8]) -> Result>; @@ -211,19 +208,13 @@ impl CodeExecutor for NativeExecutor { + ( $pub:vis $name:ident, $dispatcher:path, $version:path $(,)?) => { /// A unit struct which implements `NativeExecutionDispatch` feeding in the hard-coded runtime. $pub struct $name; - $crate::native_executor_instance!(IMPL $name, $dispatcher, $version, $code); + $crate::native_executor_instance!(IMPL $name, $dispatcher, $version); }; - (IMPL $name:ident, $dispatcher:path, $version:path, $code:expr) => { + (IMPL $name:ident, $dispatcher:path, $version:path) => { impl $crate::NativeExecutionDispatch for $name { - fn native_equivalent() -> &'static [u8] { - // WARNING!!! This assumes that the runtime was built *before* the main project. Until we - // get a proper build script, this must be strictly adhered to or things will go wrong. - $code - } - fn dispatch( ext: &mut $crate::Externalities<$crate::Blake2Hasher>, method: &str, diff --git a/substrate/core/rpc/Cargo.toml b/substrate/core/rpc/Cargo.toml index f35408c7b3..234761708e 100644 --- a/substrate/core/rpc/Cargo.toml +++ b/substrate/core/rpc/Cargo.toml @@ -18,7 +18,6 @@ codec = { package = "parity-scale-codec", version = "1.0.0" } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" client = { package = "substrate-client", path = "../client" } -substrate-executor = { path = "../executor" } network = { package = "substrate-network", path = "../network" } primitives = { package = "substrate-primitives", path = "../primitives" } session = { package = "substrate-session", path = "../session" } diff --git a/substrate/core/rpc/src/state/tests.rs b/substrate/core/rpc/src/state/tests.rs index 0bb6d40e24..07e009d179 100644 --- a/substrate/core/rpc/src/state/tests.rs +++ b/substrate/core/rpc/src/state/tests.rs @@ -25,20 +25,24 @@ use test_client::{ consensus::BlockOrigin, runtime, }; -use substrate_executor::NativeExecutionDispatch; #[test] fn should_return_storage() { + const KEY: &[u8] = b":mock"; + const VALUE: &[u8] = b"hello world"; + let core = tokio::runtime::Runtime::new().unwrap(); - let client = Arc::new(test_client::new()); + let client = TestClientBuilder::new() + .add_extra_storage(KEY.to_vec(), VALUE.to_vec()) + .build(); let genesis_hash = client.genesis_hash(); - let client = State::new(client, Subscriptions::new(Arc::new(core.executor()))); - let key = StorageKey(b":code".to_vec()); + let client = State::new(Arc::new(client), Subscriptions::new(Arc::new(core.executor()))); + let key = StorageKey(KEY.to_vec()); assert_eq!( client.storage(key.clone(), Some(genesis_hash).into()) .map(|x| x.map(|x| x.0.len())).unwrap().unwrap() as usize, - LocalExecutor::native_equivalent().len(), + VALUE.len(), ); assert_matches!( client.storage_hash(key.clone(), Some(genesis_hash).into()).map(|x| x.is_some()), @@ -46,7 +50,7 @@ fn should_return_storage() { ); assert_eq!( client.storage_size(key.clone(), None).unwrap().unwrap() as usize, - LocalExecutor::native_equivalent().len(), + VALUE.len(), ); } diff --git a/substrate/core/test-runtime/client/src/lib.rs b/substrate/core/test-runtime/client/src/lib.rs index f1cbb6fd8c..ad5badf8bf 100644 --- a/substrate/core/test-runtime/client/src/lib.rs +++ b/substrate/core/test-runtime/client/src/lib.rs @@ -51,8 +51,7 @@ mod local_executor { native_executor_instance!( pub LocalExecutor, runtime::api::dispatch, - runtime::native_version, - runtime::WASM_BINARY + runtime::native_version ); } @@ -97,12 +96,34 @@ pub type LightExecutor = client::light::call_executor::RemoteOrLocalCallExecutor pub struct GenesisParameters { support_changes_trie: bool, heap_pages_override: Option, + extra_storage: Vec<(Vec, Vec)>, +} + +impl GenesisParameters { + fn genesis_config(&self) -> GenesisConfig { + GenesisConfig::new( + self.support_changes_trie, + vec![ + sr25519::Public::from(Sr25519Keyring::Alice).into(), + sr25519::Public::from(Sr25519Keyring::Bob).into(), + sr25519::Public::from(Sr25519Keyring::Charlie).into(), + ], + vec![ + AccountKeyring::Alice.into(), + AccountKeyring::Bob.into(), + AccountKeyring::Charlie.into(), + ], + 1000, + self.heap_pages_override, + self.extra_storage.clone(), + ) + } } impl generic_test_client::GenesisInit for GenesisParameters { fn genesis_storage(&self) -> (StorageOverlay, ChildrenStorageOverlay) { use codec::Encode; - let mut storage = genesis_config(self.support_changes_trie, self.heap_pages_override).genesis_map(); + let mut storage = self.genesis_config().genesis_map(); let child_roots = storage.1.iter().map(|(sk, child_map)| { let state_root = <<::Header as HeaderT>::Hashing as HashT>::trie_root( @@ -157,6 +178,13 @@ pub trait TestClientBuilderExt: Sized { /// Override the default value for Wasm heap pages. fn set_heap_pages(self, heap_pages: u64) -> Self; + /// Add an extra value into the genesis storage. + /// + /// # Panics + /// + /// Panics if the key is empty. + fn add_extra_storage>, V: Into>>(self, key: K, value: V) -> Self; + /// Build the test client. fn build(self) -> Client { self.build_with_longest_chain().0 @@ -182,28 +210,18 @@ impl TestClientBuilderExt for TestClientBuilder< self } + fn add_extra_storage>, V: Into>>(mut self, key: K, value: V) -> Self { + let key = key.into(); + assert!(!key.is_empty()); + self.genesis_init_mut().extra_storage.push((key, value.into())); + self + } + fn build_with_longest_chain(self) -> (Client, client::LongestChain) { self.build_with_native_executor(None) } } -fn genesis_config(support_changes_trie: bool, heap_pages_override: Option) -> GenesisConfig { - GenesisConfig::new( - support_changes_trie, - vec![ - sr25519::Public::from(Sr25519Keyring::Alice).into(), - sr25519::Public::from(Sr25519Keyring::Bob).into(), - sr25519::Public::from(Sr25519Keyring::Charlie).into(), - ], vec![ - AccountKeyring::Alice.into(), - AccountKeyring::Bob.into(), - AccountKeyring::Charlie.into(), - ], - 1000, - heap_pages_override, - ) -} - /// Creates new client instance used for tests. pub fn new() -> Client { TestClientBuilder::new().build() diff --git a/substrate/core/test-runtime/src/genesismap.rs b/substrate/core/test-runtime/src/genesismap.rs index 7686ed08bc..c2dd49156d 100644 --- a/substrate/core/test-runtime/src/genesismap.rs +++ b/substrate/core/test-runtime/src/genesismap.rs @@ -25,10 +25,12 @@ use sr_primitives::traits::{Block as BlockT, Hash as HashT, Header as HeaderT}; /// Configuration of a general Substrate test genesis block. pub struct GenesisConfig { - pub changes_trie_config: Option, - pub authorities: Vec, - pub balances: Vec<(AccountId, u64)>, - pub heap_pages_override: Option, + changes_trie_config: Option, + authorities: Vec, + balances: Vec<(AccountId, u64)>, + heap_pages_override: Option, + /// Additional storage key pairs that will be added to the genesis map. + extra_storage: Vec<(Vec, Vec)>, } impl GenesisConfig { @@ -38,6 +40,7 @@ impl GenesisConfig { endowed_accounts: Vec, balance: u64, heap_pages_override: Option, + extra_storage: Vec<(Vec, Vec)>, ) -> Self { GenesisConfig { changes_trie_config: match support_changes_trie { @@ -47,6 +50,7 @@ impl GenesisConfig { authorities: authorities.clone(), balances: endowed_accounts.into_iter().map(|a| (a, balance)).collect(), heap_pages_override, + extra_storage, } } @@ -70,6 +74,10 @@ impl GenesisConfig { map.insert(well_known_keys::CHANGES_TRIE_CONFIG.to_vec(), changes_trie_config.encode()); } map.insert(twox_128(&b"sys:auth"[..])[..].to_vec(), self.authorities.encode()); + // Finally, add the extra storage entries. + for (key, value) in self.extra_storage.iter().cloned() { + map.insert(key, value); + } (map, Default::default()) } } diff --git a/substrate/node-template/src/service.rs b/substrate/node-template/src/service.rs index 07c0aa26cf..7bb4cc8d52 100644 --- a/substrate/node-template/src/service.rs +++ b/substrate/node-template/src/service.rs @@ -18,8 +18,7 @@ pub use substrate_executor::NativeExecutor; native_executor_instance!( pub Executor, node_template_runtime::api::dispatch, - node_template_runtime::native_version, - WASM_BINARY + node_template_runtime::native_version ); construct_simple_protocol! { @@ -72,7 +71,7 @@ macro_rules! new_full_start { Ok(import_queue) })?; - + (builder, import_setup, inherent_data_providers, tasks_to_spawn) }} } diff --git a/substrate/node/executor/src/lib.rs b/substrate/node/executor/src/lib.rs index 874c5a1b6d..eba744f62a 100644 --- a/substrate/node/executor/src/lib.rs +++ b/substrate/node/executor/src/lib.rs @@ -30,8 +30,7 @@ use substrate_executor::native_executor_instance; native_executor_instance!( pub Executor, node_runtime::api::dispatch, - node_runtime::native_version, - node_runtime::WASM_BINARY + node_runtime::native_version ); #[cfg(test)]