From a318557532b2a5b21fe8d618f2ddfcfbe257e359 Mon Sep 17 00:00:00 2001 From: Arsenii Lyashenko Date: Wed, 22 May 2024 18:11:51 +0300 Subject: [PATCH 1/5] Fix `ring-proof` compilation --- Cargo.lock | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b07af1123fd0a..136f3ae98d9d8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5144,7 +5144,7 @@ dependencies = [ [[package]] name = "fflonk" version = "0.1.0" -source = "git+https://github.com/w3f/fflonk#26a5045b24e169cffc1f9328ca83d71061145c40" +source = "git+https://github.com/w3f/fflonk#1e854f35e9a65d08b11a86291405cdc95baa0a35" dependencies = [ "ark-ec", "ark-ff", @@ -17590,6 +17590,10 @@ dependencies = [ "sp-std", ] +[[package]] +name = "sp-tasks" +version = "0.1.0" + [[package]] name = "sp-test-primitives" version = "2.0.0" @@ -19316,7 +19320,7 @@ checksum = "97fee6b57c6a41524a810daee9286c02d7752c4253064d0b05472833a438f675" dependencies = [ "cfg-if", "digest 0.10.7", - "rand 0.8.5", + "rand 0.7.3", "static_assertions", ] From c03b0d66983285e4329f378e2af305e707ed4f8d Mon Sep 17 00:00:00 2001 From: Arsenii Lyashenko Date: Wed, 12 Jun 2024 08:36:14 +0300 Subject: [PATCH 2/5] Add option to enable native execution in `LocalCallExecutor` --- Cargo.lock | 4 ---- substrate/client/api/src/call_executor.rs | 2 ++ .../client/service/src/client/call_executor.rs | 17 +++++++++++++++++ substrate/client/service/src/client/client.rs | 4 ++++ substrate/primitives/state-machine/src/lib.rs | 15 ++++++++++++++- 5 files changed, 37 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 136f3ae98d9d8..47483d2ae3510 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17590,10 +17590,6 @@ dependencies = [ "sp-std", ] -[[package]] -name = "sp-tasks" -version = "0.1.0" - [[package]] name = "sp-test-primitives" version = "2.0.0" diff --git a/substrate/client/api/src/call_executor.rs b/substrate/client/api/src/call_executor.rs index 49b51ccc943ed..4eb90ca3cdc92 100644 --- a/substrate/client/api/src/call_executor.rs +++ b/substrate/client/api/src/call_executor.rs @@ -48,6 +48,8 @@ pub trait CallExecutor: RuntimeVersionOf { /// The backend used by the node. type Backend: crate::backend::Backend; + fn gear_use_native(&mut self) {} + /// Returns the [`ExecutionExtensions`]. fn execution_extensions(&self) -> &ExecutionExtensions; diff --git a/substrate/client/service/src/client/call_executor.rs b/substrate/client/service/src/client/call_executor.rs index 86b5c7c61fcd2..c4cacd60e8da3 100644 --- a/substrate/client/service/src/client/call_executor.rs +++ b/substrate/client/service/src/client/call_executor.rs @@ -39,6 +39,7 @@ pub struct LocalCallExecutor { wasm_override: Arc>, wasm_substitutes: WasmSubstitutes, execution_extensions: Arc>, + gear_use_native: bool, } impl LocalCallExecutor @@ -71,6 +72,7 @@ where wasm_override: Arc::new(wasm_override), wasm_substitutes, execution_extensions: Arc::new(execution_extensions), + gear_use_native: false, }) } @@ -141,6 +143,7 @@ where wasm_override: self.wasm_override.clone(), wasm_substitutes: self.wasm_substitutes.clone(), execution_extensions: self.execution_extensions.clone(), + gear_use_native: self.gear_use_native, } } } @@ -155,6 +158,10 @@ where type Backend = B; + fn gear_use_native(&mut self) { + self.gear_use_native = true; + } + fn execution_extensions(&self) -> &ExecutionExtensions { &self.execution_extensions } @@ -237,6 +244,11 @@ where call_context, ) .set_parent_hash(at_hash); + + if self.gear_use_native { + state_machine.gear_use_native(); + } + state_machine.execute() }, None => { @@ -251,6 +263,11 @@ where call_context, ) .set_parent_hash(at_hash); + + if self.gear_use_native { + state_machine.gear_use_native(); + } + state_machine.execute() }, } diff --git a/substrate/client/service/src/client/client.rs b/substrate/client/service/src/client/client.rs index da4a4f66e2af1..4d1021e73a87b 100644 --- a/substrate/client/service/src/client/client.rs +++ b/substrate/client/service/src/client/client.rs @@ -452,6 +452,10 @@ where }) } + pub fn gear_use_native(&mut self) { + self.executor.gear_use_native(); + } + /// returns a reference to the block import notification sinks /// useful for test environments. pub fn import_notification_sinks(&self) -> &NotificationSinks> { diff --git a/substrate/primitives/state-machine/src/lib.rs b/substrate/primitives/state-machine/src/lib.rs index 0e2b9bfdfffcf..94c89382cf60a 100644 --- a/substrate/primitives/state-machine/src/lib.rs +++ b/substrate/primitives/state-machine/src/lib.rs @@ -209,6 +209,7 @@ mod execution { /// Used for logging. parent_hash: Option, context: CallContext, + gear_use_native: bool, } impl<'a, B, H, Exec> Drop for StateMachine<'a, B, H, Exec> @@ -250,6 +251,7 @@ mod execution { stats: StateMachineStats::default(), parent_hash: None, context, + gear_use_native: false, } } @@ -289,7 +291,14 @@ mod execution { let result = self .exec - .call(&mut ext, self.runtime_code, self.method, self.call_data, false, self.context) + .call( + &mut ext, + self.runtime_code, + self.method, + self.call_data, + self.gear_use_native, + self.context, + ) .0; self.overlay @@ -305,6 +314,10 @@ mod execution { result.map_err(|e| Box::new(e) as Box<_>) } + + pub fn gear_use_native(&mut self) { + self.gear_use_native = true; + } } /// Prove execution using the given state backend, overlayed changes, and call executor. From 0580bf615155ba7d4359e57c0bd0696bb8a0304c Mon Sep 17 00:00:00 2001 From: Arsenii Lyashenko Date: Mon, 17 Jun 2024 23:49:32 +0300 Subject: [PATCH 3/5] Add overlay getter for externalities --- substrate/primitives/externalities/src/lib.rs | 5 +++++ substrate/primitives/state-machine/src/ext.rs | 6 +++++- substrate/primitives/state-machine/src/lib.rs | 8 ++++---- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/substrate/primitives/externalities/src/lib.rs b/substrate/primitives/externalities/src/lib.rs index 411ec97a6b824..41ebf8edd1504 100644 --- a/substrate/primitives/externalities/src/lib.rs +++ b/substrate/primitives/externalities/src/lib.rs @@ -309,6 +309,11 @@ pub trait Externalities: ExtensionStore { /// /// Get all the keys that have been read or written to during the benchmark. fn get_read_and_written_keys(&self) -> Vec<(Vec, u32, u32, bool)>; + + /// Access to `OverlayChanges` for `gear-tasks`. + fn gear_overlayed_changes(&self) -> Option> { + None + } } /// Extension for the [`Externalities`] trait. diff --git a/substrate/primitives/state-machine/src/ext.rs b/substrate/primitives/state-machine/src/ext.rs index 11df46f2a4a3a..b3cee955a1fa2 100644 --- a/substrate/primitives/state-machine/src/ext.rs +++ b/substrate/primitives/state-machine/src/ext.rs @@ -159,7 +159,7 @@ where impl<'a, H, B> Externalities for Ext<'a, H, B> where - H: Hasher, + H: Hasher + 'static, H::Out: Ord + 'static + codec::Codec, B: Backend, { @@ -651,6 +651,10 @@ where fn get_read_and_written_keys(&self) -> Vec<(Vec, u32, u32, bool)> { self.backend.get_read_and_written_keys() } + + fn gear_overlayed_changes(&self) -> Option> { + Some(Box::new(self.overlay.clone())) + } } impl<'a, H, B> Ext<'a, H, B> diff --git a/substrate/primitives/state-machine/src/lib.rs b/substrate/primitives/state-machine/src/lib.rs index 94c89382cf60a..8c34154c70c1d 100644 --- a/substrate/primitives/state-machine/src/lib.rs +++ b/substrate/primitives/state-machine/src/lib.rs @@ -224,7 +224,7 @@ mod execution { impl<'a, B, H, Exec> StateMachine<'a, B, H, Exec> where - H: Hasher, + H: Hasher + 'static, H::Out: Ord + 'static + codec::Codec, Exec: CodeExecutor + Clone + 'static, B: Backend, @@ -331,7 +331,7 @@ mod execution { ) -> Result<(Vec, StorageProof), Box> where B: AsTrieBackend, - H: Hasher, + H: Hasher + 'static, H::Out: Ord + 'static + codec::Codec, Exec: CodeExecutor + Clone + 'static, { @@ -367,7 +367,7 @@ mod execution { ) -> Result<(Vec, StorageProof), Box> where S: trie_backend_essence::TrieBackendStorage, - H: Hasher, + H: Hasher + 'static, H::Out: Ord + 'static + codec::Codec, Exec: CodeExecutor + 'static + Clone, { @@ -429,7 +429,7 @@ mod execution { runtime_code: &RuntimeCode, ) -> Result, Box> where - H: Hasher, + H: Hasher + 'static, H::Out: Ord + 'static + codec::Codec, Exec: CodeExecutor + Clone + 'static, { From a3cfe7342272824b514108002d5860c1fa132fab Mon Sep 17 00:00:00 2001 From: Arsenii Lyashenko Date: Mon, 24 Jun 2024 21:38:36 +0300 Subject: [PATCH 4/5] Revert "Add option to enable native execution in `LocalCallExecutor`" This reverts commit c03b0d66983285e4329f378e2af305e707ed4f8d. --- Cargo.lock | 4 ++++ substrate/client/api/src/call_executor.rs | 2 -- .../client/service/src/client/call_executor.rs | 17 ----------------- substrate/client/service/src/client/client.rs | 4 ---- substrate/primitives/state-machine/src/lib.rs | 15 +-------------- 5 files changed, 5 insertions(+), 37 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 47483d2ae3510..136f3ae98d9d8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17590,6 +17590,10 @@ dependencies = [ "sp-std", ] +[[package]] +name = "sp-tasks" +version = "0.1.0" + [[package]] name = "sp-test-primitives" version = "2.0.0" diff --git a/substrate/client/api/src/call_executor.rs b/substrate/client/api/src/call_executor.rs index 4eb90ca3cdc92..49b51ccc943ed 100644 --- a/substrate/client/api/src/call_executor.rs +++ b/substrate/client/api/src/call_executor.rs @@ -48,8 +48,6 @@ pub trait CallExecutor: RuntimeVersionOf { /// The backend used by the node. type Backend: crate::backend::Backend; - fn gear_use_native(&mut self) {} - /// Returns the [`ExecutionExtensions`]. fn execution_extensions(&self) -> &ExecutionExtensions; diff --git a/substrate/client/service/src/client/call_executor.rs b/substrate/client/service/src/client/call_executor.rs index c4cacd60e8da3..86b5c7c61fcd2 100644 --- a/substrate/client/service/src/client/call_executor.rs +++ b/substrate/client/service/src/client/call_executor.rs @@ -39,7 +39,6 @@ pub struct LocalCallExecutor { wasm_override: Arc>, wasm_substitutes: WasmSubstitutes, execution_extensions: Arc>, - gear_use_native: bool, } impl LocalCallExecutor @@ -72,7 +71,6 @@ where wasm_override: Arc::new(wasm_override), wasm_substitutes, execution_extensions: Arc::new(execution_extensions), - gear_use_native: false, }) } @@ -143,7 +141,6 @@ where wasm_override: self.wasm_override.clone(), wasm_substitutes: self.wasm_substitutes.clone(), execution_extensions: self.execution_extensions.clone(), - gear_use_native: self.gear_use_native, } } } @@ -158,10 +155,6 @@ where type Backend = B; - fn gear_use_native(&mut self) { - self.gear_use_native = true; - } - fn execution_extensions(&self) -> &ExecutionExtensions { &self.execution_extensions } @@ -244,11 +237,6 @@ where call_context, ) .set_parent_hash(at_hash); - - if self.gear_use_native { - state_machine.gear_use_native(); - } - state_machine.execute() }, None => { @@ -263,11 +251,6 @@ where call_context, ) .set_parent_hash(at_hash); - - if self.gear_use_native { - state_machine.gear_use_native(); - } - state_machine.execute() }, } diff --git a/substrate/client/service/src/client/client.rs b/substrate/client/service/src/client/client.rs index 4d1021e73a87b..da4a4f66e2af1 100644 --- a/substrate/client/service/src/client/client.rs +++ b/substrate/client/service/src/client/client.rs @@ -452,10 +452,6 @@ where }) } - pub fn gear_use_native(&mut self) { - self.executor.gear_use_native(); - } - /// returns a reference to the block import notification sinks /// useful for test environments. pub fn import_notification_sinks(&self) -> &NotificationSinks> { diff --git a/substrate/primitives/state-machine/src/lib.rs b/substrate/primitives/state-machine/src/lib.rs index 8c34154c70c1d..a6baf91b3d405 100644 --- a/substrate/primitives/state-machine/src/lib.rs +++ b/substrate/primitives/state-machine/src/lib.rs @@ -209,7 +209,6 @@ mod execution { /// Used for logging. parent_hash: Option, context: CallContext, - gear_use_native: bool, } impl<'a, B, H, Exec> Drop for StateMachine<'a, B, H, Exec> @@ -251,7 +250,6 @@ mod execution { stats: StateMachineStats::default(), parent_hash: None, context, - gear_use_native: false, } } @@ -291,14 +289,7 @@ mod execution { let result = self .exec - .call( - &mut ext, - self.runtime_code, - self.method, - self.call_data, - self.gear_use_native, - self.context, - ) + .call(&mut ext, self.runtime_code, self.method, self.call_data, false, self.context) .0; self.overlay @@ -314,10 +305,6 @@ mod execution { result.map_err(|e| Box::new(e) as Box<_>) } - - pub fn gear_use_native(&mut self) { - self.gear_use_native = true; - } } /// Prove execution using the given state backend, overlayed changes, and call executor. From 8028b07de0a108c12d4af7f01171f9adee654f58 Mon Sep 17 00:00:00 2001 From: Arsenii Lyashenko Date: Mon, 24 Jun 2024 21:44:48 +0300 Subject: [PATCH 5/5] Add option to enable native execution in `NativeElseWasmExecutor` --- Cargo.lock | 4 ---- substrate/client/executor/src/executor.rs | 25 +++++++++++++++++++---- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 136f3ae98d9d8..47483d2ae3510 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17590,10 +17590,6 @@ dependencies = [ "sp-std", ] -[[package]] -name = "sp-tasks" -version = "0.1.0" - [[package]] name = "sp-test-primitives" version = "2.0.0" diff --git a/substrate/client/executor/src/executor.rs b/substrate/client/executor/src/executor.rs index 7c292a83da089..7410cc1b2ba7f 100644 --- a/substrate/client/executor/src/executor.rs +++ b/substrate/client/executor/src/executor.rs @@ -565,6 +565,7 @@ pub struct NativeElseWasmExecutor { /// Fallback wasm executor. wasm: WasmExecutor>, + gear_force_native: bool, } impl NativeElseWasmExecutor { @@ -601,7 +602,11 @@ impl NativeElseWasmExecutor { .with_runtime_cache_size(runtime_cache_size) .build(); - NativeElseWasmExecutor { native_version: D::native_version(), wasm } + NativeElseWasmExecutor { + native_version: D::native_version(), + wasm, + gear_force_native: false, + } } /// Create a new instance using the given [`WasmExecutor`]. @@ -610,7 +615,7 @@ impl NativeElseWasmExecutor { ExtendedHostFunctions, >, ) -> Self { - Self { native_version: D::native_version(), wasm: executor } + Self { native_version: D::native_version(), wasm: executor, gear_force_native: false } } /// Ignore missing function imports if set true. @@ -618,6 +623,10 @@ impl NativeElseWasmExecutor { pub fn allow_missing_host_functions(&mut self, allow_missing_host_functions: bool) { self.wasm.allow_missing_host_functions = allow_missing_host_functions } + + pub fn gear_force_native(&mut self) { + self.gear_force_native = true; + } } impl RuntimeVersionOf for NativeElseWasmExecutor { @@ -645,7 +654,7 @@ impl CodeExecutor for NativeElseWasmExecut runtime_code: &RuntimeCode, method: &str, data: &[u8], - use_native: bool, + mut use_native: bool, context: CallContext, ) -> (Result>, bool) { tracing::trace!( @@ -654,6 +663,10 @@ impl CodeExecutor for NativeElseWasmExecut "Executing function", ); + if self.gear_force_native { + use_native = true; + } + let on_chain_heap_alloc_strategy = if self.wasm.ignore_onchain_heap_pages { self.wasm.default_onchain_heap_alloc_strategy } else { @@ -711,7 +724,11 @@ impl CodeExecutor for NativeElseWasmExecut impl Clone for NativeElseWasmExecutor { fn clone(&self) -> Self { - NativeElseWasmExecutor { native_version: D::native_version(), wasm: self.wasm.clone() } + NativeElseWasmExecutor { + native_version: D::native_version(), + wasm: self.wasm.clone(), + gear_force_native: self.gear_force_native, + } } }