From c03b0d66983285e4329f378e2af305e707ed4f8d Mon Sep 17 00:00:00 2001 From: Arsenii Lyashenko Date: Wed, 12 Jun 2024 08:36:14 +0300 Subject: [PATCH] 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 136f3ae98d9d..47483d2ae351 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 49b51ccc943e..4eb90ca3cdc9 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 86b5c7c61fcd..c4cacd60e8da 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 da4a4f66e2af..4d1021e73a87 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 0e2b9bfdfffc..94c89382cf60 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.