From c64b566b095feddf65543b3b0348890b9af4cd95 Mon Sep 17 00:00:00 2001 From: bzawisto Date: Wed, 8 Mar 2023 14:21:18 +0100 Subject: [PATCH 1/8] Give precompile access to backend --- core/src/opcode.rs | 5 ++--- src/executor/stack/executor.rs | 35 +++++++++++++++++++++++----------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/core/src/opcode.rs b/core/src/opcode.rs index 4736755c6..c4cbf82c4 100644 --- a/core/src/opcode.rs +++ b/core/src/opcode.rs @@ -273,7 +273,7 @@ impl Opcode { } impl fmt::Display for Opcode { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { //write!(f, "{}", "urgh...") let op = match self { &Opcode::STOP => "STOP", @@ -423,6 +423,5 @@ impl fmt::Display for Opcode { _ => "UNKNOWNOP", }; write!(f, "{}", op) - } + } } - diff --git a/src/executor/stack/executor.rs b/src/executor/stack/executor.rs index 888e08d35..1f989147e 100644 --- a/src/executor/stack/executor.rs +++ b/src/executor/stack/executor.rs @@ -285,20 +285,23 @@ pub trait PrecompileHandle { /// Record a log. fn log(&mut self, address: H160, topics: Vec, data: Vec) -> Result<(), ExitError>; - /// Retreive the code address (what is the address of the precompile being called). + /// Retrieve the code address (what is the address of the precompile being called). fn code_address(&self) -> H160; - /// Retreive the input data the precompile is called with. + /// Retrieve the input data the precompile is called with. fn input(&self) -> &[u8]; - /// Retreive the context in which the precompile is executed. + /// Retrieve the context in which the precompile is executed. fn context(&self) -> &Context; /// Is the precompile call is done statically. fn is_static(&self) -> bool; - /// Retreive the gas limit of this call. + /// Retrieve the gas limit of this call. fn gas_limit(&self) -> Option; + + /// Retrieve backend + fn backend(&self) -> &dyn Backend; } /// A precompile result. @@ -335,8 +338,13 @@ impl PrecompileSet for () { /// * Is static /// /// In case of success returns the output and the cost. -pub type PrecompileFn = - fn(&[u8], Option, &Context, bool) -> Result<(PrecompileOutput, u64), PrecompileFailure>; +pub type PrecompileFn = fn( + &[u8], + Option, + &Context, + &dyn Backend, + bool, +) -> Result<(PrecompileOutput, u64), PrecompileFailure>; impl PrecompileSet for BTreeMap { fn execute(&self, handle: &mut impl PrecompileHandle) -> Option { @@ -347,8 +355,9 @@ impl PrecompileSet for BTreeMap { let gas_limit = handle.gas_limit(); let context = handle.context(); let is_static = handle.is_static(); + let backend = handle.backend(); - match (*precompile)(input, gas_limit, context, is_static) { + match (*precompile)(input, gas_limit, context, backend, is_static) { Ok((output, cost)) => { handle.record_cost(cost)?; Ok(output) @@ -1330,17 +1339,17 @@ impl<'inner, 'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> Pr Handler::log(self.executor, address, topics, data) } - /// Retreive the code address (what is the address of the precompile being called). + /// Retrieve the code address (what is the address of the precompile being called). fn code_address(&self) -> H160 { self.code_address } - /// Retreive the input data the precompile is called with. + /// Retrieve the input data the precompile is called with. fn input(&self) -> &[u8] { self.input } - /// Retreive the context in which the precompile is executed. + /// Retrieve the context in which the precompile is executed. fn context(&self) -> &Context { self.context } @@ -1350,8 +1359,12 @@ impl<'inner, 'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> Pr self.is_static } - /// Retreive the gas limit of this call. + /// Retrieve the gas limit of this call. fn gas_limit(&self) -> Option { self.gas_limit } + + fn backend(&self) -> &dyn Backend { + &self.executor.state + } } From 2845a693963a1cb9f171b009f66395df05d174a4 Mon Sep 17 00:00:00 2001 From: bzawisto Date: Fri, 10 Mar 2023 12:15:36 +0100 Subject: [PATCH 2/8] Adding an option to return sc code as json --- src/backend/memory.rs | 4 ++++ src/backend/mod.rs | 4 +++- src/executor/stack/memory.rs | 4 ++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/backend/memory.rs b/src/backend/memory.rs index 7382a5e22..799367317 100644 --- a/src/backend/memory.rs +++ b/src/backend/memory.rs @@ -142,6 +142,10 @@ impl<'vicinity> Backend for MemoryBackend<'vicinity> { .unwrap_or_default() } + fn code_as_json(&self, address: H160) -> Vec { + self.code(address) + } + fn storage(&self, address: H160, index: H256) -> H256 { self.state .get(&address) diff --git a/src/backend/mod.rs b/src/backend/mod.rs index 1a503cbcc..2a35a2392 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -79,7 +79,9 @@ pub trait Backend { fn basic(&self, address: H160) -> Basic; /// Get account code. fn code(&self, address: H160) -> Vec; - /// Get storage value of address at index. + /// Get account code formatted as json (if possible) + fn code_as_json(&self, address: H160) -> Vec; + /// Get storage value of address at inasex. fn storage(&self, address: H160, index: H256) -> H256; /// Get original storage value of address at index, if available. fn original_storage(&self, address: H160, index: H256) -> Option; diff --git a/src/executor/stack/memory.rs b/src/executor/stack/memory.rs index a1479279d..462124fe0 100644 --- a/src/executor/stack/memory.rs +++ b/src/executor/stack/memory.rs @@ -477,6 +477,10 @@ impl<'backend, 'config, B: Backend> Backend for MemoryStackState<'backend, 'conf .unwrap_or_else(|| self.backend.code(address)) } + fn code_as_json(&self, address: H160) -> Vec { + self.code(address) + } + fn storage(&self, address: H160, key: H256) -> H256 { self.substate .known_storage(address, key) From b9fc86962671f852a22b01dada7ed535013fe5c4 Mon Sep 17 00:00:00 2001 From: bzawisto Date: Fri, 10 Mar 2023 13:39:06 +0100 Subject: [PATCH 3/8] Bug fix --- src/executor/stack/memory.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/executor/stack/memory.rs b/src/executor/stack/memory.rs index 462124fe0..3df268ad9 100644 --- a/src/executor/stack/memory.rs +++ b/src/executor/stack/memory.rs @@ -478,7 +478,7 @@ impl<'backend, 'config, B: Backend> Backend for MemoryStackState<'backend, 'conf } fn code_as_json(&self, address: H160) -> Vec { - self.code(address) + self.backend.code_as_json(address) } fn storage(&self, address: H160, key: H256) -> H256 { From a514478ec16e3f9bed15adc3db9b65611d34026b Mon Sep 17 00:00:00 2001 From: bzawisto Date: Thu, 16 Mar 2023 09:29:46 +0100 Subject: [PATCH 4/8] Fetch contract substate --- src/backend/memory.rs | 4 ++++ src/backend/mod.rs | 2 ++ src/executor/stack/memory.rs | 4 ++++ 3 files changed, 10 insertions(+) diff --git a/src/backend/memory.rs b/src/backend/memory.rs index 799367317..03b594d40 100644 --- a/src/backend/memory.rs +++ b/src/backend/memory.rs @@ -156,6 +156,10 @@ impl<'vicinity> Backend for MemoryBackend<'vicinity> { fn original_storage(&self, address: H160, index: H256) -> Option { Some(self.storage(address, index)) } + + fn susbtate_as_json(&self, _address: H160, _indinces: &[String]) -> Vec { + Vec::new() + } } impl<'vicinity> ApplyBackend for MemoryBackend<'vicinity> { diff --git a/src/backend/mod.rs b/src/backend/mod.rs index 2a35a2392..7acfe850c 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -85,6 +85,8 @@ pub trait Backend { fn storage(&self, address: H160, index: H256) -> H256; /// Get original storage value of address at index, if available. fn original_storage(&self, address: H160, index: H256) -> Option; + /// Get contract substate with given array of indices + fn susbtate_as_json(&self, address: H160, indinces: &[String]) -> Vec; } /// EVM backend that can apply changes. diff --git a/src/executor/stack/memory.rs b/src/executor/stack/memory.rs index 3df268ad9..de87ef322 100644 --- a/src/executor/stack/memory.rs +++ b/src/executor/stack/memory.rs @@ -494,6 +494,10 @@ impl<'backend, 'config, B: Backend> Backend for MemoryStackState<'backend, 'conf self.backend.original_storage(address, key) } + + fn susbtate_as_json(&self, _address: H160, _indinces: &[String]) -> Vec { + Vec::new() + } } impl<'backend, 'config, B: Backend> StackState<'config> for MemoryStackState<'backend, 'config, B> { From 8938f6483d9708115ae6b96847528a8dd8779d9d Mon Sep 17 00:00:00 2001 From: bzawisto Date: Thu, 16 Mar 2023 10:05:05 +0100 Subject: [PATCH 5/8] Typo --- src/backend/memory.rs | 2 +- src/backend/mod.rs | 2 +- src/executor/stack/memory.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/backend/memory.rs b/src/backend/memory.rs index 03b594d40..781c9e907 100644 --- a/src/backend/memory.rs +++ b/src/backend/memory.rs @@ -157,7 +157,7 @@ impl<'vicinity> Backend for MemoryBackend<'vicinity> { Some(self.storage(address, index)) } - fn susbtate_as_json(&self, _address: H160, _indinces: &[String]) -> Vec { + fn susbtate_as_json(&self, _address: H160, _indices: &[String]) -> Vec { Vec::new() } } diff --git a/src/backend/mod.rs b/src/backend/mod.rs index 7acfe850c..619f47906 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -86,7 +86,7 @@ pub trait Backend { /// Get original storage value of address at index, if available. fn original_storage(&self, address: H160, index: H256) -> Option; /// Get contract substate with given array of indices - fn susbtate_as_json(&self, address: H160, indinces: &[String]) -> Vec; + fn susbtate_as_json(&self, address: H160, indices: &[String]) -> Vec; } /// EVM backend that can apply changes. diff --git a/src/executor/stack/memory.rs b/src/executor/stack/memory.rs index de87ef322..f51b6289e 100644 --- a/src/executor/stack/memory.rs +++ b/src/executor/stack/memory.rs @@ -495,7 +495,7 @@ impl<'backend, 'config, B: Backend> Backend for MemoryStackState<'backend, 'conf self.backend.original_storage(address, key) } - fn susbtate_as_json(&self, _address: H160, _indinces: &[String]) -> Vec { + fn susbtate_as_json(&self, _address: H160, _indices: &[String]) -> Vec { Vec::new() } } From 39b6bb0b637c5c7d711ff9f007ac8b27483b0e9c Mon Sep 17 00:00:00 2001 From: bzawisto Date: Thu, 16 Mar 2023 11:07:09 +0100 Subject: [PATCH 6/8] Missing vname added --- src/backend/memory.rs | 2 +- src/backend/mod.rs | 2 +- src/executor/stack/memory.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/backend/memory.rs b/src/backend/memory.rs index 781c9e907..4b2bb777a 100644 --- a/src/backend/memory.rs +++ b/src/backend/memory.rs @@ -157,7 +157,7 @@ impl<'vicinity> Backend for MemoryBackend<'vicinity> { Some(self.storage(address, index)) } - fn susbtate_as_json(&self, _address: H160, _indices: &[String]) -> Vec { + fn susbtate_as_json(&self, _address: H160, _vname: &str, _indices: &[String]) -> Vec { Vec::new() } } diff --git a/src/backend/mod.rs b/src/backend/mod.rs index 619f47906..cc9cd8da8 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -86,7 +86,7 @@ pub trait Backend { /// Get original storage value of address at index, if available. fn original_storage(&self, address: H160, index: H256) -> Option; /// Get contract substate with given array of indices - fn susbtate_as_json(&self, address: H160, indices: &[String]) -> Vec; + fn susbtate_as_json(&self, address: H160, vname: &str, indices: &[String]) -> Vec; } /// EVM backend that can apply changes. diff --git a/src/executor/stack/memory.rs b/src/executor/stack/memory.rs index f51b6289e..3212e16ef 100644 --- a/src/executor/stack/memory.rs +++ b/src/executor/stack/memory.rs @@ -495,7 +495,7 @@ impl<'backend, 'config, B: Backend> Backend for MemoryStackState<'backend, 'conf self.backend.original_storage(address, key) } - fn susbtate_as_json(&self, _address: H160, _indices: &[String]) -> Vec { + fn susbtate_as_json(&self, _address: H160, _vname: &str, _indices: &[String]) -> Vec { Vec::new() } } From bd1f6e7ced6d83558d5ab792100ce7edf982abe8 Mon Sep 17 00:00:00 2001 From: bzawisto Date: Mon, 27 Mar 2023 12:28:39 +0200 Subject: [PATCH 7/8] Fetch substate --- src/executor/stack/memory.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/executor/stack/memory.rs b/src/executor/stack/memory.rs index 3212e16ef..eb446eed8 100644 --- a/src/executor/stack/memory.rs +++ b/src/executor/stack/memory.rs @@ -495,8 +495,8 @@ impl<'backend, 'config, B: Backend> Backend for MemoryStackState<'backend, 'conf self.backend.original_storage(address, key) } - fn susbtate_as_json(&self, _address: H160, _vname: &str, _indices: &[String]) -> Vec { - Vec::new() + fn susbtate_as_json(&self, address: H160, vname: &str, indices: &[String]) -> Vec { + self.backend.susbtate_as_json(address, vname, indices) } } From 53485a9e76a3b6dc80d7a3dadf47a9784af8e4b0 Mon Sep 17 00:00:00 2001 From: bzawisto Date: Thu, 6 Apr 2023 08:56:56 +0200 Subject: [PATCH 8/8] Typo fix --- src/backend/memory.rs | 2 +- src/backend/mod.rs | 2 +- src/executor/stack/memory.rs | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/backend/memory.rs b/src/backend/memory.rs index 4b2bb777a..19b820e57 100644 --- a/src/backend/memory.rs +++ b/src/backend/memory.rs @@ -157,7 +157,7 @@ impl<'vicinity> Backend for MemoryBackend<'vicinity> { Some(self.storage(address, index)) } - fn susbtate_as_json(&self, _address: H160, _vname: &str, _indices: &[String]) -> Vec { + fn substate_as_json(&self, _address: H160, _vname: &str, _indices: &[String]) -> Vec { Vec::new() } } diff --git a/src/backend/mod.rs b/src/backend/mod.rs index cc9cd8da8..9af7a16c7 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -86,7 +86,7 @@ pub trait Backend { /// Get original storage value of address at index, if available. fn original_storage(&self, address: H160, index: H256) -> Option; /// Get contract substate with given array of indices - fn susbtate_as_json(&self, address: H160, vname: &str, indices: &[String]) -> Vec; + fn substate_as_json(&self, address: H160, vname: &str, indices: &[String]) -> Vec; } /// EVM backend that can apply changes. diff --git a/src/executor/stack/memory.rs b/src/executor/stack/memory.rs index eb446eed8..610268797 100644 --- a/src/executor/stack/memory.rs +++ b/src/executor/stack/memory.rs @@ -495,8 +495,8 @@ impl<'backend, 'config, B: Backend> Backend for MemoryStackState<'backend, 'conf self.backend.original_storage(address, key) } - fn susbtate_as_json(&self, address: H160, vname: &str, indices: &[String]) -> Vec { - self.backend.susbtate_as_json(address, vname, indices) + fn substate_as_json(&self, address: H160, vname: &str, indices: &[String]) -> Vec { + self.backend.substate_as_json(address, vname, indices) } }