From 3f63301c3a42f834da651a8f39adb9be79a90e96 Mon Sep 17 00:00:00 2001 From: sinev-valentine Date: Wed, 8 Jun 2022 12:14:50 +0300 Subject: [PATCH 01/10] add serde macro --- runtime/src/context.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/runtime/src/context.rs b/runtime/src/context.rs index 433072686..674763d18 100644 --- a/runtime/src/context.rs +++ b/runtime/src/context.rs @@ -2,6 +2,8 @@ use crate::{H160, U256, H256}; /// Create scheme. #[derive(Clone, Copy, Eq, PartialEq, Debug)] +#[cfg_attr(feature = "with-codec", derive(codec::Encode, codec::Decode))] +#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))] pub enum CreateScheme { /// Legacy create scheme of `CREATE`. Legacy { @@ -23,6 +25,8 @@ pub enum CreateScheme { /// Call scheme. #[derive(Clone, Copy, Eq, PartialEq, Debug)] +#[cfg_attr(feature = "with-codec", derive(codec::Encode, codec::Decode))] +#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))] pub enum CallScheme { /// `CALL` Call, From b550db520a1f58cd115a5f5f10d769b39435ffa3 Mon Sep 17 00:00:00 2001 From: sinev-valentine Date: Thu, 9 Jun 2022 16:57:04 +0300 Subject: [PATCH 02/10] update --- runtime/src/lib.rs | 2 +- runtime/src/tracing.rs | 24 ++++++++++++------------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 36725b6d6..9e550b30c 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -13,7 +13,7 @@ extern crate alloc; -#[cfg(feature = "tracing")] +// #[cfg(feature = "tracing")] pub mod tracing; #[cfg(feature = "tracing")] diff --git a/runtime/src/tracing.rs b/runtime/src/tracing.rs index a0f136266..a7526785b 100644 --- a/runtime/src/tracing.rs +++ b/runtime/src/tracing.rs @@ -3,7 +3,7 @@ use crate::{Context, Opcode, Stack, Memory, Capture, ExitReason, Trap}; use crate::{H160, U256}; -environmental::environmental!(listener: dyn EventListener + 'static); +// environmental::environmental!(listener: dyn EventListener + 'static); pub trait EventListener { fn event( @@ -39,15 +39,15 @@ pub enum Event<'a> { }, } -/// Run closure with provided listener. -pub fn using R>( - new: &mut (dyn EventListener + 'static), - f: F -) -> R { - listener::using(new, f) -} - -pub(crate) fn with(f: F) { - listener::with(f); -} +// Run closure with provided listener. +// pub fn using R>( +// new: &mut (dyn EventListener + 'static), +// f: F +// ) -> R { +// listener::using(new, f) +// } +// +// pub(crate) fn with(f: F) { +// listener::with(f); +// } From 324f08e4d67d821e17a2f98acbcb6f472d3ca0a9 Mon Sep 17 00:00:00 2001 From: sinev-valentine Date: Fri, 10 Jun 2022 00:11:59 +0300 Subject: [PATCH 03/10] trace impl --- core/src/error.rs | 2 + core/src/lib.rs | 2 +- core/src/opcode.rs | 2 + runtime/Cargo.toml | 2 + runtime/src/lib.rs | 28 ++++---- runtime/src/tracing.rs | 152 ++++++++++++++++++++++++++++++++--------- 6 files changed, 141 insertions(+), 47 deletions(-) diff --git a/core/src/error.rs b/core/src/error.rs index 2ccba8f3f..2483c2621 100644 --- a/core/src/error.rs +++ b/core/src/error.rs @@ -5,6 +5,8 @@ pub type Trap = Opcode; /// Capture represents the result of execution. #[derive(Clone, Copy, Debug, Eq, PartialEq)] +#[cfg_attr(feature = "with-codec", derive(codec::Encode, codec::Decode))] +#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))] pub enum Capture { /// The machine has exited. It cannot be executed again. Exit(E), diff --git a/core/src/lib.rs b/core/src/lib.rs index 8abfd2ecf..0aa8204d2 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -69,7 +69,7 @@ impl Machine { pub fn memory_mut(&mut self) -> &mut Memory { &mut self.memory } /// Return a reference of the program counter. - pub fn position(&self) -> &Result { + pub fn position(&self) -> &Result { &self.position } diff --git a/core/src/opcode.rs b/core/src/opcode.rs index 9ea968b97..08162b246 100644 --- a/core/src/opcode.rs +++ b/core/src/opcode.rs @@ -1,6 +1,8 @@ #![allow(clippy::use_self)] /// Opcode enum. One-to-one corresponding to an `u8` value. #[derive(Clone, Copy, Debug, Eq, PartialEq)] +#[cfg_attr(feature = "with-codec", derive(codec::Encode, codec::Decode))] +#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))] pub struct Opcode(pub u8); // Core opcodes. diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index 09b75528f..817802d62 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -15,6 +15,8 @@ codec = { package = "parity-scale-codec", version = "1.3", default-features = fa serde = { version = "1.0", default-features = false, features = ["derive"], optional = true } serde_bytes = { version = "0.11.5", optional = true } environmental = { version = "1.1.2", default-features = false, optional = true} +bincode = "1.3.3" +solana-program = { version = "=1.9.12", path = "/home/user/CLionProjects/neonlabs/solana/sdk/program", default_features = false } [features] default = ["std"] diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 9e550b30c..4c5328ecf 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -13,15 +13,15 @@ extern crate alloc; -// #[cfg(feature = "tracing")] +#[cfg(feature = "tracing")] pub mod tracing; #[cfg(feature = "tracing")] macro_rules! event { - ($x:expr) => { - use crate::tracing::Event::*; - crate::tracing::with(|listener| listener.event($x)); - } + ($x:expr) => { + use crate::tracing::Event::*; + crate::tracing::send($x); + }; } #[cfg(not(feature = "tracing"))] @@ -40,6 +40,8 @@ pub use crate::context::{CreateScheme, CallScheme, Context}; pub use crate::interrupt::{Resolve, ResolveCall, ResolveCreate}; pub use crate::handler::{Transfer, Handler}; pub use crate::eval::{save_return_value, save_created_address, Control}; +#[cfg(feature = "tracing")] +pub use crate::tracing::Event; use alloc::vec::Vec; @@ -48,11 +50,11 @@ macro_rules! step { let mut skip_step_result_event = true; if let Some((opcode, stack)) = $self.machine.inspect() { event!(Step { - context: &$self.context, + context: $self.context.clone(), opcode, - position: $self.machine.position(), - stack, - memory: $self.machine.memory() + position: $self.machine.position().clone(), + stack: stack.clone(), + memory: $self.machine.memory().clone() }); skip_step_result_event = false; @@ -77,10 +79,10 @@ macro_rules! step { if !skip_step_result_event { event!(StepResult { - result: &result, - return_value: &$self.machine.return_value(), - stack: $self.machine.stack(), - memory: $self.machine.memory(), + result: result, + return_value: $self.machine.return_value(), + stack: $self.machine.stack().clone(), + memory: $self.machine.memory().clone(), }); } diff --git a/runtime/src/tracing.rs b/runtime/src/tracing.rs index a7526785b..1e06a383a 100644 --- a/runtime/src/tracing.rs +++ b/runtime/src/tracing.rs @@ -1,31 +1,123 @@ -//! Allows to listen to runtime events. +//! Tools for tracing runtime events -use crate::{Context, Opcode, Stack, Memory, Capture, ExitReason, Trap}; -use crate::{H160, U256}; +// use evm::Context; +// use evm::{H160, H256, U256, Stack, Memory, Opcode, Capture, Trap}; +// use evm_runtime::{CreateScheme, ExitReason, Transfer}; +use solana_program::tracer_api; -// environmental::environmental!(listener: dyn EventListener + 'static); +use crate::{H160, H256, U256, Context, Opcode, Stack, Memory, Capture, ExitReason, Trap, CreateScheme,Transfer}; +use alloc::vec::Vec; -pub trait EventListener { - fn event( - &mut self, - event: Event - ); -} - -#[derive(Debug, Copy, Clone)] -pub enum Event<'a> { +/// Trace event +#[derive(Debug, Clone)] +#[derive(serde::Serialize, serde::Deserialize)] +pub enum Event { + /// Call event + Call { + /// Called code address + code_address: H160, + /// Transfer parameters + transfer: Option, + /// Input data provided to the call + #[serde(with = "serde_bytes")] + input: Vec, + /// Target gas + target_gas: Option, + /// Static call flag + is_static: bool, + /// Runtime context + context: Context, + }, + /// Create event + Create { + /// Creator address + caller: H160, + /// Address of the created account + address: H160, + /// Scheme + scheme: CreateScheme, + /// Value the created account is endowed with + value: U256, + /// Init code + #[serde(with = "serde_bytes")] + init_code: Vec, + /// Target Gas + target_gas: Option, + }, + /// Suicide event + Suicide { + /// Suicided address + address: H160, + /// Suicided contract heir + target: H160, + /// Balance before suicide + balance: U256, + }, + /// Exit event + Exit { + /// Exit reason + reason: ExitReason, + /// Return value + #[serde(with = "serde_bytes")] + return_value: Vec, + }, + /// Transactional Call event + TransactCall { + /// Caller account address + caller: H160, + /// Destination account address + address: H160, + /// Value transferred to the destination account + value: U256, + /// Input data provided to the call + #[serde(with = "serde_bytes")] + data: Vec, + /// Gas Limit + gas_limit: U256, + }, + /// Transactional Create event + TransactCreate { + /// Creator address + caller: H160, + /// Value the created account is endowed with + value: U256, + /// Init code + #[serde(with = "serde_bytes")] + init_code: Vec, + /// Gas limit + gas_limit: U256, + /// Address of the created account + address: H160, + }, + /// Transactional Create2 event + TransactCreate2 { + /// Creator address + caller: H160, + /// Value the created account is endowed with + value: U256, + /// Init code + #[serde(with = "serde_bytes")] + init_code: Vec, + /// Salt + salt: H256, + /// Gas limit + gas_limit: U256, + /// Address of the created account + address: H160, + }, Step { - context: &'a Context, + context: Context, opcode: Opcode, - position: &'a Result, - stack: &'a Stack, - memory: &'a Memory + position: Result, + stack: Stack, + memory: Memory }, StepResult { - result: &'a Result<(), Capture>, - return_value: &'a [u8], - stack: &'a Stack, - memory: &'a Memory + result: Result<(), Capture>, + #[serde(with = "serde_bytes")] + return_value: Vec, + stack: Stack, + memory: Memory }, SLoad { address: H160, @@ -37,17 +129,11 @@ pub enum Event<'a> { index: U256, value: U256 }, -} -// Run closure with provided listener. -// pub fn using R>( -// new: &mut (dyn EventListener + 'static), -// f: F -// ) -> R { -// listener::using(new, f) -// } -// -// pub(crate) fn with(f: F) { -// listener::with(f); -// } +} +pub fn send(event: Event){ + let mut message : Vec = Vec::new(); + bincode::serialize_into(&mut message, &event).unwrap(); + tracer_api::send_trace_message(message.as_slice()); +} From 748c3533d6a8eddfe569509153d95fc76d4d41af Mon Sep 17 00:00:00 2001 From: sinev-valentine Date: Tue, 14 Jun 2022 21:59:26 +0300 Subject: [PATCH 04/10] EventOnStask impl --- core/src/memory.rs | 3 +- core/src/stack.rs | 5 + runtime/src/eval/system.rs | 21 +++-- runtime/src/lib.rs | 74 ++++++++++++--- runtime/src/tracing.rs | 185 +++++++++++++++++++++++++++++++++++-- 5 files changed, 256 insertions(+), 32 deletions(-) diff --git a/core/src/memory.rs b/core/src/memory.rs index 99698b3af..54c4390a8 100644 --- a/core/src/memory.rs +++ b/core/src/memory.rs @@ -49,7 +49,7 @@ impl Memory { self.len() == 0 } - pub fn data(&self) -> &[u8] { + pub fn data(&self) -> &[u8] { &self.data } @@ -153,4 +153,5 @@ impl Memory { self.set(memory_offset, data_by_offset, Some(len)) } + } diff --git a/core/src/stack.rs b/core/src/stack.rs index 8a96d4382..0c59e3af0 100644 --- a/core/src/stack.rs +++ b/core/src/stack.rs @@ -167,4 +167,9 @@ impl Stack { Ok(()) } + + pub fn data(&self) -> &[U256] { + &self.data + } + } diff --git a/runtime/src/eval/system.rs b/runtime/src/eval/system.rs index 299869bd9..df2f949aa 100644 --- a/runtime/src/eval/system.rs +++ b/runtime/src/eval/system.rs @@ -1,8 +1,6 @@ use core::cmp::min; use alloc::vec::Vec; -use crate::{Runtime, ExitError, Handler, Capture, Transfer, ExitReason, - CreateScheme, CallScheme, Context, ExitSucceed, ExitFatal, - H160, H256, U256}; +use crate::{Runtime, ExitError, Handler, Capture, Transfer, ExitReason, CreateScheme, CallScheme, Context, ExitSucceed, ExitFatal, H160, H256, U256, SLoadTrace, SStoreTrace}; use super::Control; pub fn sha3(runtime: &mut Runtime, handler: &H) -> Control { @@ -180,11 +178,13 @@ pub fn sload(runtime: &mut Runtime, handler: &H) -> Control { let value = handler.storage(runtime.context.address, index); push_u256!(runtime, value); - event!(SLoad { - address: runtime.context.address, - index, - value - }); + event!(SLoad( + SLoadTrace{ + address: runtime.context.address, + index, + value + } + )); Control::Continue } @@ -192,11 +192,12 @@ pub fn sload(runtime: &mut Runtime, handler: &H) -> Control { pub fn sstore(runtime: &mut Runtime, handler: &mut H) -> Control { pop_u256!(runtime, index, value); - event!(SStore { + event!(SStore( SStoreTrace{ address: runtime.context.address, index, value - }); + } + )); match handler.set_storage(runtime.context.address, index, value) { Ok(()) => Control::Continue, diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 4c5328ecf..772c5ea37 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -19,8 +19,24 @@ pub mod tracing; #[cfg(feature = "tracing")] macro_rules! event { ($x:expr) => { - use crate::tracing::Event::*; - crate::tracing::send($x); + use crate::tracing::EventOnStack::*; + use solana_program::{tracer_api, compute_meter_remaining, compute_meter_set_remaining}; + + let mut remaining: u64 =0; + compute_meter_remaining::compute_meter_remaining(&mut remaining); + + // let mut message : Vec = Vec::new(); + // bincode::serialize_into(&mut message, &$x).unwrap(); + // let mut remaining1: u64 =0; + // compute_meter_remaining::compute_meter_remaining(&mut remaining1); + + let ptr = &$x as *const _ as *const u8; + + tracer_api::send_trace_message(ptr); + // let mut remaining1: u64 =0; + // compute_meter_remaining::compute_meter_remaining(&mut remaining1); + + compute_meter_set_remaining::compute_meter_set_remaining(remaining+12); }; } @@ -41,21 +57,36 @@ pub use crate::interrupt::{Resolve, ResolveCall, ResolveCreate}; pub use crate::handler::{Transfer, Handler}; pub use crate::eval::{save_return_value, save_created_address, Control}; #[cfg(feature = "tracing")] -pub use crate::tracing::Event; +pub use crate::tracing::*; use alloc::vec::Vec; +// use solana_program::{compute_meter_remaining, compute_meter_set_remaining}; macro_rules! step { ( $self:expr, $handler:expr, $return:tt $($err:path)?; $($ok:path)? ) => ({ let mut skip_step_result_event = true; if let Some((opcode, stack)) = $self.machine.inspect() { - event!(Step { - context: $self.context.clone(), - opcode, - position: $self.machine.position().clone(), - stack: stack.clone(), - memory: $self.machine.memory().clone() - }); + let mut remaining: u64 =0; + compute_meter_remaining::compute_meter_remaining(&mut remaining); + event!(Step( + StepTrace { + context: $self.context.clone(), + opcode, + position: $self.machine.position().clone(), + stack: StackOnStack{ + data: $self.machine.stack().data(), + data_len: $self.machine.stack().len(), + limit: $self.machine.stack().limit() + }, + memory: MemoryOnStack{ + data: $self.machine.memory().data(), + data_len: $self.machine.memory().len(), + effective_len: $self.machine.memory().effective_len(), + limit: $self.machine.memory().limit(), + } + } + )); + compute_meter_set_remaining::compute_meter_set_remaining(remaining+12); skip_step_result_event = false; match $handler.pre_validate(&$self.context, opcode, stack) { @@ -77,13 +108,26 @@ macro_rules! step { let result = $self.machine.step(); + let return_value = $self.machine.return_value(); + if !skip_step_result_event { - event!(StepResult { + event!(StepResult (StepResultTrace{ result: result, - return_value: $self.machine.return_value(), - stack: $self.machine.stack().clone(), - memory: $self.machine.memory().clone(), - }); + return_value: return_value.as_slice(), + return_value_len: return_value.len(), + + stack: StackOnStack{ + data: $self.machine.stack().data(), + data_len: $self.machine.stack().len(), + limit: $self.machine.stack().limit() + }, + memory: MemoryOnStack{ + data: $self.machine.memory().data(), + data_len: $self.machine.memory().len(), + effective_len: $self.machine.memory().effective_len(), + limit: $self.machine.memory().limit(), + } + })); } match result { diff --git a/runtime/src/tracing.rs b/runtime/src/tracing.rs index 1e06a383a..f437d1d17 100644 --- a/runtime/src/tracing.rs +++ b/runtime/src/tracing.rs @@ -3,15 +3,15 @@ // use evm::Context; // use evm::{H160, H256, U256, Stack, Memory, Opcode, Capture, Trap}; // use evm_runtime::{CreateScheme, ExitReason, Transfer}; -use solana_program::tracer_api; use crate::{H160, H256, U256, Context, Opcode, Stack, Memory, Capture, ExitReason, Trap, CreateScheme,Transfer}; use alloc::vec::Vec; +// use solana_program::{tracer_api, compute_meter_remaining, compute_meter_set_remaining}; /// Trace event #[derive(Debug, Clone)] #[derive(serde::Serialize, serde::Deserialize)] -pub enum Event { +pub enum Event{ /// Call event Call { /// Called code address @@ -132,8 +132,181 @@ pub enum Event { } -pub fn send(event: Event){ - let mut message : Vec = Vec::new(); - bincode::serialize_into(&mut message, &event).unwrap(); - tracer_api::send_trace_message(message.as_slice()); +/// EVM stack. +#[derive(Clone, Debug)] +pub struct StackOnStack<'a> { + // #[cfg_attr(feature = "with-serde", serde(with="serde_vec_u256"))] + pub data: &'a[U256], + pub data_len: usize, + pub limit: usize, } + +#[derive(Clone, Debug)] +pub struct MemoryOnStack<'a> { + // #[cfg_attr(feature = "with-serde", serde(with = "serde_bytes"))] + pub data: &'a[u8], + pub data_len: usize, + pub effective_len: usize, + pub limit: usize, +} + + +#[derive(Debug, Clone)] +pub struct CallTrace<'a>{ + /// Called code address + pub code_address: H160, + /// Transfer parameters + pub transfer: Option, + /// Input data provided to the call + pub input: &'a[u8], + pub input_len: usize, + /// Target gas + pub target_gas: Option, + /// Static call flag + pub is_static: bool, + /// Runtime context + pub context: Context, +} + +#[derive(Debug, Clone)] +pub struct CreateTrace<'a>{ + /// Creator address + pub caller: H160, + /// Address of the created account + pub address: H160, + /// Scheme + pub scheme: CreateScheme, + /// Value the created account is endowed with + pub value: U256, + /// Init code + pub init_code: &'a[u8], + pub init_code_len: usize, + /// Target Gas + pub target_gas: Option, +} + +#[derive(Debug, Clone)] +pub struct ExitTrace<'a>{ + /// Exit reason + pub reason: ExitReason, + /// Return value + pub return_value: &'a[u8], + pub return_value_len: usize, +} + +#[derive(Debug, Clone)] +pub struct SuicideTrace{ + /// Suicided address + pub address: H160, + /// Suicided contract heir + pub target: H160, + /// Balance before suicide + pub balance: U256, +} + +#[derive(Debug, Clone)] +pub struct TransactCallTrace<'a>{ + /// Caller account address + pub caller: H160, + /// Destination account address + pub address: H160, + /// Value transferred to the destination account + pub value: U256, + /// Input data provided to the call + pub data: &'a[u8], + pub data_len: usize, + /// Gas Limit + pub gas_limit: U256, +} + + +#[derive(Debug, Clone)] +pub struct TransactCreateTrace<'a>{ + /// Creator address + pub caller: H160, + /// Value the created account is endowed with + pub value: U256, + /// Init code + pub init_code: &'a[u8], + pub init_code_len: usize, + /// Gas limit + pub gas_limit: U256, + /// Address of the created account + pub address: H160, +} + +#[derive(Debug, Clone)] +pub struct TransactCreate2Trace<'a>{ + /// Creator address + pub caller: H160, + /// Value the created account is endowed with + pub value: U256, + /// Init code + pub init_code: &'a[u8], + pub init_code_len: usize, + /// Salt + pub salt: H256, + /// Gas limit + pub gas_limit: U256, + /// Address of the created account + pub address: H160, +} + +#[derive(Debug, Clone)] +pub struct StepTrace<'a>{ + pub context: Context, + pub opcode: Opcode, + pub position: Result, + pub stack: StackOnStack<'a>, + pub memory: MemoryOnStack<'a>, +} + +#[derive(Debug, Clone)] +pub struct StepResultTrace<'a>{ + pub result: Result<(), Capture>, + pub return_value: &'a[u8], + pub return_value_len: usize, + pub stack: StackOnStack<'a>, + pub memory: MemoryOnStack<'a> +} + +#[derive(Debug, Clone)] +pub struct SLoadTrace{ + pub address: H160, + pub index: U256, + pub value: U256 +} + +#[derive(Debug, Clone)] +pub struct SStoreTrace { + pub address: H160, + pub index: U256, + pub value: U256 +} + +/// Trace event +#[derive(Debug, Clone)] +pub enum EventOnStack<'a>{ + Call(CallTrace<'a>) , + Create(CreateTrace<'a>) , + Suicide(SuicideTrace) , + Exit(ExitTrace<'a>) , + TransactCall(TransactCallTrace<'a>) , + TransactCreate(TransactCreateTrace<'a>) , + TransactCreate2(TransactCreate2Trace<'a>) , + Step(StepTrace<'a>) , + StepResult(StepResultTrace<'a>), + SLoad(SLoadTrace), + SStore(SStoreTrace), +} + +// pub fn send(event: Event){ +// let mut remaining: u64 =0; +// compute_meter_remaining::compute_meter_remaining(&mut remaining); +// +// let mut message : Vec = Vec::new(); +// bincode::serialize_into(&mut message, &event).unwrap(); +// tracer_api::send_trace_message(message.as_slice()); +// +// compute_meter_set_remaining::compute_meter_set_remaining(remaining+12); +// } From d68bfad15afc79bd7a92a9531020ee222aa6ac92 Mon Sep 17 00:00:00 2001 From: sinev-valentine Date: Wed, 15 Jun 2022 18:52:33 +0300 Subject: [PATCH 05/10] update trace sending --- runtime/src/lib.rs | 15 ++++---- runtime/src/tracing.rs | 80 +++++++++++++++++------------------------- 2 files changed, 42 insertions(+), 53 deletions(-) diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 772c5ea37..faf721d49 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -68,22 +68,25 @@ macro_rules! step { if let Some((opcode, stack)) = $self.machine.inspect() { let mut remaining: u64 =0; compute_meter_remaining::compute_meter_remaining(&mut remaining); + use alloc::vec; + let vec : Vec = vec![1 ,2, 3]; event!(Step( StepTrace { context: $self.context.clone(), opcode, position: $self.machine.position().clone(), stack: StackOnStack{ - data: $self.machine.stack().data(), + data: $self.machine.stack().data() as *const _ as *const u8 as u64, data_len: $self.machine.stack().len(), limit: $self.machine.stack().limit() }, memory: MemoryOnStack{ - data: $self.machine.memory().data(), + data: $self.machine.memory().data() as *const _ as *const u8 as u64, data_len: $self.machine.memory().len(), effective_len: $self.machine.memory().effective_len(), limit: $self.machine.memory().limit(), - } + }, + vec : vec, } )); compute_meter_set_remaining::compute_meter_set_remaining(remaining+12); @@ -113,16 +116,16 @@ macro_rules! step { if !skip_step_result_event { event!(StepResult (StepResultTrace{ result: result, - return_value: return_value.as_slice(), + return_value: return_value.as_slice() as *const _ as *const u8 as u64, return_value_len: return_value.len(), stack: StackOnStack{ - data: $self.machine.stack().data(), + data: $self.machine.stack().data() as *const _ as *const u8 as u64, data_len: $self.machine.stack().len(), limit: $self.machine.stack().limit() }, memory: MemoryOnStack{ - data: $self.machine.memory().data(), + data: $self.machine.memory().data() as *const _ as *const u8 as u64, data_len: $self.machine.memory().len(), effective_len: $self.machine.memory().effective_len(), limit: $self.machine.memory().limit(), diff --git a/runtime/src/tracing.rs b/runtime/src/tracing.rs index f437d1d17..86b47d67f 100644 --- a/runtime/src/tracing.rs +++ b/runtime/src/tracing.rs @@ -134,17 +134,15 @@ pub enum Event{ /// EVM stack. #[derive(Clone, Debug)] -pub struct StackOnStack<'a> { - // #[cfg_attr(feature = "with-serde", serde(with="serde_vec_u256"))] - pub data: &'a[U256], +pub struct StackOnStack{ + pub data: u64,/// &[U256], pub data_len: usize, pub limit: usize, } #[derive(Clone, Debug)] -pub struct MemoryOnStack<'a> { - // #[cfg_attr(feature = "with-serde", serde(with = "serde_bytes"))] - pub data: &'a[u8], +pub struct MemoryOnStack { + pub data: u64, // &'a[u8], pub data_len: usize, pub effective_len: usize, pub limit: usize, @@ -152,13 +150,13 @@ pub struct MemoryOnStack<'a> { #[derive(Debug, Clone)] -pub struct CallTrace<'a>{ +pub struct CallTrace{ /// Called code address pub code_address: H160, /// Transfer parameters pub transfer: Option, /// Input data provided to the call - pub input: &'a[u8], + pub input: u64, pub input_len: usize, /// Target gas pub target_gas: Option, @@ -169,7 +167,7 @@ pub struct CallTrace<'a>{ } #[derive(Debug, Clone)] -pub struct CreateTrace<'a>{ +pub struct CreateTrace{ /// Creator address pub caller: H160, /// Address of the created account @@ -179,18 +177,16 @@ pub struct CreateTrace<'a>{ /// Value the created account is endowed with pub value: U256, /// Init code - pub init_code: &'a[u8], + pub init_code: u64, pub init_code_len: usize, /// Target Gas pub target_gas: Option, } #[derive(Debug, Clone)] -pub struct ExitTrace<'a>{ - /// Exit reason +pub struct ExitTrace{ pub reason: ExitReason, - /// Return value - pub return_value: &'a[u8], + pub return_value: u64, pub return_value_len: usize, } @@ -205,7 +201,7 @@ pub struct SuicideTrace{ } #[derive(Debug, Clone)] -pub struct TransactCallTrace<'a>{ +pub struct TransactCallTrace{ /// Caller account address pub caller: H160, /// Destination account address @@ -213,7 +209,7 @@ pub struct TransactCallTrace<'a>{ /// Value transferred to the destination account pub value: U256, /// Input data provided to the call - pub data: &'a[u8], + pub data: u64, pub data_len: usize, /// Gas Limit pub gas_limit: U256, @@ -221,13 +217,13 @@ pub struct TransactCallTrace<'a>{ #[derive(Debug, Clone)] -pub struct TransactCreateTrace<'a>{ +pub struct TransactCreateTrace{ /// Creator address pub caller: H160, /// Value the created account is endowed with pub value: U256, /// Init code - pub init_code: &'a[u8], + pub init_code: u64, pub init_code_len: usize, /// Gas limit pub gas_limit: U256, @@ -236,13 +232,13 @@ pub struct TransactCreateTrace<'a>{ } #[derive(Debug, Clone)] -pub struct TransactCreate2Trace<'a>{ +pub struct TransactCreate2Trace{ /// Creator address pub caller: H160, /// Value the created account is endowed with pub value: U256, /// Init code - pub init_code: &'a[u8], + pub init_code: u64, pub init_code_len: usize, /// Salt pub salt: H256, @@ -253,21 +249,22 @@ pub struct TransactCreate2Trace<'a>{ } #[derive(Debug, Clone)] -pub struct StepTrace<'a>{ +pub struct StepTrace{ pub context: Context, pub opcode: Opcode, pub position: Result, - pub stack: StackOnStack<'a>, - pub memory: MemoryOnStack<'a>, + pub stack: StackOnStack, + pub memory: MemoryOnStack, + pub vec: Vec, } #[derive(Debug, Clone)] -pub struct StepResultTrace<'a>{ +pub struct StepResultTrace{ pub result: Result<(), Capture>, - pub return_value: &'a[u8], + pub return_value: u64, pub return_value_len: usize, - pub stack: StackOnStack<'a>, - pub memory: MemoryOnStack<'a> + pub stack: StackOnStack, + pub memory: MemoryOnStack, } #[derive(Debug, Clone)] @@ -286,27 +283,16 @@ pub struct SStoreTrace { /// Trace event #[derive(Debug, Clone)] -pub enum EventOnStack<'a>{ - Call(CallTrace<'a>) , - Create(CreateTrace<'a>) , +pub enum EventOnStack{ + Call(CallTrace) , + Create(CreateTrace) , Suicide(SuicideTrace) , - Exit(ExitTrace<'a>) , - TransactCall(TransactCallTrace<'a>) , - TransactCreate(TransactCreateTrace<'a>) , - TransactCreate2(TransactCreate2Trace<'a>) , - Step(StepTrace<'a>) , - StepResult(StepResultTrace<'a>), + Exit(ExitTrace) , + TransactCall(TransactCallTrace) , + TransactCreate(TransactCreateTrace) , + TransactCreate2(TransactCreate2Trace) , + Step(StepTrace) , + StepResult(StepResultTrace), SLoad(SLoadTrace), SStore(SStoreTrace), } - -// pub fn send(event: Event){ -// let mut remaining: u64 =0; -// compute_meter_remaining::compute_meter_remaining(&mut remaining); -// -// let mut message : Vec = Vec::new(); -// bincode::serialize_into(&mut message, &event).unwrap(); -// tracer_api::send_trace_message(message.as_slice()); -// -// compute_meter_set_remaining::compute_meter_set_remaining(remaining+12); -// } From 0b13637df94a24d3bfecd84521c9abcd3d835de0 Mon Sep 17 00:00:00 2001 From: sinev-valentine Date: Wed, 22 Jun 2022 11:15:16 +0300 Subject: [PATCH 06/10] bpf-units correction impl --- Cargo.toml | 3 +- core/Cargo.toml | 2 + {runtime => core}/src/context.rs | 14 ++ core/src/lib.rs | 125 +++++++++---- core/src/memory.rs | 21 ++- core/src/stack.rs | 10 ++ core/src/tracing.rs | 151 ++++++++++++++++ runtime/Cargo.toml | 3 +- runtime/src/eval/system.rs | 52 ++++-- runtime/src/handler.rs | 16 +- runtime/src/lib.rs | 156 +--------------- runtime/src/tracing.rs | 298 ------------------------------- src/backend/mod.rs | 4 +- 13 files changed, 334 insertions(+), 521 deletions(-) rename {runtime => core}/src/context.rs (77%) create mode 100644 core/src/tracing.rs delete mode 100644 runtime/src/tracing.rs diff --git a/Cargo.toml b/Cargo.toml index 217ff22fd..26cb93bdf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,8 @@ default = ["std"] with-codec = ["codec", "evm-core/with-codec", "evm-runtime/with-codec"] with-serde = ["serde", "serde_bytes", "evm-core/with-serde", "evm-runtime/with-serde"] std = ["evm-core/std", "evm-runtime/std", "sha3/std", "serde/std", "codec/std", "log/std"] -tracing = ["evm-runtime/tracing"] +tracing = ["evm-runtime/tracing", "evm-core/tracing"] + #[workspace] #members = [ # "core", diff --git a/core/Cargo.toml b/core/Cargo.toml index c182a4137..8d2d06a6e 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -17,6 +17,7 @@ impl-rlp = { version = "0.3", default-features = false } rlp = { version = "0.5", default-features = false } serde = { version = "1.0", default-features = false, features = ["derive"], optional = true } serde_bytes = { version = "0.11.5", optional = true } +solana-program = { version = "=1.9.12", path = "/home/user/CLionProjects/neonlabs/solana/sdk/program", default_features = false } [dev-dependencies] hex = "0.4" @@ -26,3 +27,4 @@ default = ["std"] with-codec = ["codec"] with-serde = ["serde", "serde_bytes"] std = ["log/std", "codec/std", "serde/std"] +tracing = [] diff --git a/runtime/src/context.rs b/core/src/context.rs similarity index 77% rename from runtime/src/context.rs rename to core/src/context.rs index 674763d18..65861575b 100644 --- a/runtime/src/context.rs +++ b/core/src/context.rs @@ -50,3 +50,17 @@ pub struct Context { /// Apparent value of the EVM. pub apparent_value: U256, } + + +/// Transfer from source to target, with given value. +#[derive(Clone, Copy, Debug)] +#[cfg_attr(feature = "with-codec", derive(codec::Encode, codec::Decode))] +#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))] +pub struct Transfer { + /// Source address. + pub source: H160, + /// Target address. + pub target: H160, + /// Transfer value. + pub value: U256, +} diff --git a/core/src/lib.rs b/core/src/lib.rs index 0aa8204d2..973875d7b 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -1,7 +1,8 @@ //! Core layer for EVM. #![deny(warnings)] -#![forbid(unused_variables, unused_imports)] +// #![forbid(unused_variables, unused_imports)] +// #![forbid( unused_imports)] #![deny(clippy::all, clippy::pedantic, clippy::nursery)] #![allow( clippy::module_name_repetitions, @@ -22,6 +23,9 @@ mod error; mod eval; mod utils; mod primitive_types; +mod context; +#[cfg(feature = "tracing")] +mod tracing; pub use crate::memory::Memory; pub use crate::stack::Stack; @@ -29,11 +33,53 @@ pub use crate::valids::Valids; pub use crate::opcode::Opcode; pub use crate::error::{Trap, Capture, ExitReason, ExitSucceed, ExitError, ExitRevert, ExitFatal}; pub use crate::primitive_types::{H160, H256, U256, U512}; +pub use crate::context::{Context, CreateScheme, CallScheme, Transfer}; use core::ops::Range; use alloc::vec::Vec; use crate::eval::{eval, Control}; +#[cfg(feature = "tracing")] +pub use crate::tracing::*; + +#[cfg(feature = "tracing")] +#[allow(unused_imports)] +use solana_program::{compute_meter_remaining, compute_meter_set_remaining, tracer_api}; + +#[macro_export] +#[cfg(feature = "tracing")] +macro_rules! event { + ($x:expr) => { + // use crate::tracing::Event::*; + // use solana_program::tracer_api; + + // let mut remaining: u64 =0; + // compute_meter_remaining::compute_meter_remaining(&mut remaining); + // remaining = remaining + 7; + let _ptr = &$x as *const _ as *const u8; + tracer_api::send_trace_message(ptr); + + // compute_meter_set_remaining::compute_meter_set_remaining(remaining); + }; +} + + +#[macro_export] +#[cfg(not(feature = "tracing"))] +macro_rules! event { + ($x:expr) => {} +} + +#[cfg(feature = "tracing")] +extern "C" { + fn sol_compute_meter_remaining() -> u64; +} + +#[cfg(feature = "tracing")] +extern "C" { + fn sol_compute_meter_set_remaining() -> u64; +} + /// Core execution layer for EVM. #[cfg_attr(feature = "with-codec", derive(codec::Encode, codec::Decode))] #[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))] @@ -126,7 +172,11 @@ impl Machine { } /// Loop stepping the machine, until it stops. - pub fn run(&mut self, max_steps: u64, mut pre_validate: F) -> (u64, Capture) + pub fn run(&mut self, + max_steps: u64, + mut pre_validate: F, + #[allow(unused_variables)] context : &Context + ) -> (u64, Capture) where F: FnMut(Opcode, &Stack) -> Result<(), ExitError> { for step in 0..max_steps { @@ -143,61 +193,66 @@ impl Machine { } }; + + // + // #[cfg(feature = "tracing")] + // unsafe {sol_compute_meter_remaining();} + // event!(Event::Step( + // StepTrace { + // context: context, + // opcode, + // position: &self.position, + // stack: &self.stack, + // memory: &self.memory, + // } + // )); + // #[cfg(feature = "tracing")] + // unsafe {sol_compute_meter_set_remaining();} + if let Err(error) = pre_validate(opcode, &self.stack()) { let reason = ExitReason::from(error); self.exit(reason); return (step, Capture::Exit(reason)); } - match eval(self, opcode, position) { + let result = match eval(self, opcode, position) { Control::Continue(p) => { self.position = Ok(position + p); + Ok(()) }, Control::Exit(reason) => { self.exit(reason); - return (step, Capture::Exit(reason)) + Err(Capture::Exit(reason)) }, Control::Jump(p) => { self.position = Ok(p); + Ok(()) }, Control::Trap(opcode) => { self.position = Ok(position + 1); - return (step, Capture::Trap(opcode)); + Err(Capture::Trap(opcode)) }, + }; + + + #[cfg(feature = "tracing")] + unsafe {sol_compute_meter_remaining();} + event!(Event::StepResult (StepResultTrace{ + result: &result, + return_value: &self.return_value(), + stack: &self.stack, + memory: &self.memory + })); + #[cfg(feature = "tracing")] + unsafe {sol_compute_meter_set_remaining();} + + if let Err(capture) = result { + return (step, capture) } } (max_steps, Capture::Exit(ExitReason::StepLimitReached)) } - /// Step the machine, executing one opcode. It then returns. - pub fn step(&mut self) -> Result<(), Capture> { - let position = *self.position.as_ref().map_err(|reason| Capture::Exit(reason.clone()))?; - - let opcode = if let Some(opcode) = self.code.get(position).map(|v| Opcode(*v)) { - opcode - } else { - self.position = Err(ExitSucceed::Stopped.into()); - return Err(Capture::Exit(ExitSucceed::Stopped.into())) - }; - - match eval(self, opcode, position) { - Control::Continue(p) => { - self.position = Ok(position + p); - Ok(()) - }, - Control::Exit(e) => { - self.position = Err(e.clone()); - Err(Capture::Exit(e)) - }, - Control::Jump(p) => { - self.position = Ok(p); - Ok(()) - }, - Control::Trap(opcode) => { - self.position = Ok(position + 1); - Err(Capture::Trap(opcode)) - }, - } - } } + diff --git a/core/src/memory.rs b/core/src/memory.rs index 54c4390a8..a02836266 100644 --- a/core/src/memory.rs +++ b/core/src/memory.rs @@ -17,14 +17,21 @@ pub struct Memory { impl Memory { /// Create a new memory with the given limit. #[must_use] - pub const fn new(limit: usize) -> Self { + pub const fn new(limit: usize) -> Self { + Self { + data: Vec::new(), + effective_len: 0_usize, + limit, + } + } + + pub fn from(data: &[u8], len: usize, limit: usize) -> Self { Self { - data: Vec::new(), - effective_len: 0_usize, + data: Vec::from(data), + effective_len: len, limit, } } - /// Memory limit. #[must_use] pub const fn limit(&self) -> usize { @@ -49,9 +56,9 @@ impl Memory { self.len() == 0 } - pub fn data(&self) -> &[u8] { - &self.data - } + pub fn data(&self) -> &[u8] { &self.data } + + pub fn data_vec(&self) -> &Vec { &self.data } /// Resize the memory, making it cover the memory region of `offset..(offset /// + len)`, with 32 bytes as the step. If the length is zero, this function diff --git a/core/src/stack.rs b/core/src/stack.rs index 0c59e3af0..91ab783f3 100644 --- a/core/src/stack.rs +++ b/core/src/stack.rs @@ -68,6 +68,12 @@ impl Stack { } } + pub fn from(data: &[U256], limit: usize) -> Self { + Self { + data: Vec::from(data), + limit, + } + } /// Stack limit. #[must_use] pub const fn limit(&self) -> usize { @@ -172,4 +178,8 @@ impl Stack { &self.data } + pub fn data_vec(&self) -> &Vec { + &self.data + } + } diff --git a/core/src/tracing.rs b/core/src/tracing.rs new file mode 100644 index 000000000..c118534d4 --- /dev/null +++ b/core/src/tracing.rs @@ -0,0 +1,151 @@ +//! Tools for tracing runtime events + +// use evm::Context; +// use evm::{H160, H256, U256, Stack, Memory, Opcode, Capture, Trap}; +// use evm_runtime::{CreateScheme, ExitReason, Transfer}; + +use crate::{H160, H256, U256, Context, Opcode, Stack, Memory, Capture, ExitReason, Trap, CreateScheme,Transfer}; +use alloc::vec::Vec; +// use solana_program::{tracer_api, compute_meter_remaining, compute_meter_set_remaining}; + + +#[derive(Debug, Clone)] +pub struct CallTrace<'a>{ + /// Called code address + pub code_address: H160, + /// Transfer parameters + pub transfer: &'a Option, + /// Input data provided to the call + pub input: &'a Vec, + /// Target gas + pub target_gas: Option, + /// Static call flag + pub is_static: bool, + /// Runtime context + pub context: &'a Context, +} + +#[derive(Debug, Clone)] +pub struct CreateTrace<'a>{ + /// Creator address + pub caller: H160, + /// Address of the created account + pub address: H160, + /// Scheme + pub scheme: CreateScheme, + /// Value the created account is endowed with + pub value: U256, + /// Init code + pub init_code: &'a Vec, + /// Target Gas + pub target_gas: Option, +} + +#[derive(Debug, Clone)] +pub struct SuicideTrace{ + /// Suicided address + pub address: H160, + /// Suicided contract heir + pub target: H160, + /// Balance before suicide + pub balance: U256, +} + +#[derive(Debug, Clone)] +pub struct ExitTrace<'a>{ + pub reason: &'a ExitReason, + pub return_value: &'a Vec, +} + + +#[derive(Debug, Clone)] +pub struct TransactCallTrace<'a>{ + /// Caller account address + pub caller: H160, + /// Destination account address + pub address: H160, + /// Value transferred to the destination account + pub value: U256, + /// Input data provided to the call + pub data: &'a Vec, + /// Gas Limit + pub gas_limit: U256, +} + +#[derive(Debug, Clone)] +pub struct TransactCreateTrace<'a>{ + /// Creator address + pub caller: H160, + /// Value the created account is endowed with + pub value: U256, + /// Init code + pub init_code: &'a Vec, + /// Gas limit + pub gas_limit: U256, + /// Address of the created account + pub address: H160, +} + +#[derive(Debug, Clone)] +pub struct TransactCreate2Trace<'a>{ + /// Creator address + pub caller: H160, + /// Value the created account is endowed with + pub value: U256, + /// Init code + pub init_code: &'a Vec, + /// Salt + pub salt: H256, + /// Gas limit + pub gas_limit: U256, + /// Address of the created account + pub address: H160, +} + +#[derive(Debug, Clone)] +pub struct StepTrace<'a>{ + pub context: &'a Context, + pub opcode: Opcode, + pub position: &'a Result, + pub stack: &'a Stack, + pub memory: &'a Memory, +} + +#[derive(Debug, Clone)] +pub struct StepResultTrace<'a>{ + pub result: &'a Result<(), Capture>, + pub return_value: &'a Vec, + pub stack: &'a Stack, + pub memory: &'a Memory, +} + +#[derive(Debug, Clone)] +pub struct SLoadTrace{ + pub address: H160, + pub index: U256, + pub value: U256 +} + +#[derive(Debug, Clone)] +pub struct SStoreTrace { + pub address: H160, + pub index: U256, + pub value: U256 +} + +/// Trace event +#[derive(Debug, Clone)] +#[allow(dead_code)] +pub enum Event<'a>{ + Call(CallTrace<'a>) , + Create(CreateTrace<'a>) , + Suicide(SuicideTrace) , + Exit(ExitTrace<'a>) , + TransactCall(TransactCallTrace<'a>) , + TransactCreate(TransactCreateTrace<'a>) , + TransactCreate2(TransactCreate2Trace<'a>) , + Step(StepTrace<'a>) , + StepResult(StepResultTrace<'a>), + SLoad(SLoadTrace), + SStore(SStoreTrace), +} diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index 817802d62..e2cbbd83b 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -24,5 +24,6 @@ with-codec = ["codec"] with-serde = ["serde", "serde_bytes"] std = ["evm-core/std", "sha3/std", "environmental/std"] tracing = [ - "environmental" + "environmental", + "evm-core/tracing" ] diff --git a/runtime/src/eval/system.rs b/runtime/src/eval/system.rs index df2f949aa..5339775f6 100644 --- a/runtime/src/eval/system.rs +++ b/runtime/src/eval/system.rs @@ -1,7 +1,17 @@ use core::cmp::min; use alloc::vec::Vec; -use crate::{Runtime, ExitError, Handler, Capture, Transfer, ExitReason, CreateScheme, CallScheme, Context, ExitSucceed, ExitFatal, H160, H256, U256, SLoadTrace, SStoreTrace}; +use crate::{Runtime, ExitError, Handler, Capture, Transfer, ExitReason, CreateScheme, CallScheme, Context, ExitSucceed, ExitFatal, H160, H256, U256}; use super::Control; +// use evm_core::event; + +// #[cfg(feature = "tracing")] +// use evm_core::*; +// #[cfg(feature = "tracing")] +// use solana_program::{compute_meter_remaining, compute_meter_set_remaining}; + +// #[cfg(feature = "tracing")] +// use solana_program::tracer_api; + pub fn sha3(runtime: &mut Runtime, handler: &H) -> Control { pop_u256!(runtime, from, len); @@ -178,13 +188,20 @@ pub fn sload(runtime: &mut Runtime, handler: &H) -> Control { let value = handler.storage(runtime.context.address, index); push_u256!(runtime, value); - event!(SLoad( - SLoadTrace{ - address: runtime.context.address, - index, - value - } - )); + // #[cfg(feature = "tracing")] + // let mut remaining: u64 =0; + // #[cfg(feature = "tracing")] + // compute_meter_remaining::compute_meter_remaining(&mut remaining); + // + // event!(Event::SLoad( + // SLoadTrace{ + // address: runtime.context.address, + // index, + // value + // } + // )); + // #[cfg(feature = "tracing")] + // compute_meter_set_remaining::compute_meter_set_remaining(remaining+12); Control::Continue } @@ -192,12 +209,19 @@ pub fn sload(runtime: &mut Runtime, handler: &H) -> Control { pub fn sstore(runtime: &mut Runtime, handler: &mut H) -> Control { pop_u256!(runtime, index, value); - event!(SStore( SStoreTrace{ - address: runtime.context.address, - index, - value - } - )); + // #[cfg(feature = "tracing")] + // let mut remaining: u64 =0; + // #[cfg(feature = "tracing")] + // compute_meter_remaining::compute_meter_remaining(&mut remaining); + // + // event!(Event::SStore( SStoreTrace{ + // address: runtime.context.address, + // index, + // value + // } + // )); + // #[cfg(feature = "tracing")] + // compute_meter_set_remaining::compute_meter_set_remaining(remaining+12); match handler.set_storage(runtime.context.address, index, value) { Ok(()) => Control::Continue, diff --git a/runtime/src/handler.rs b/runtime/src/handler.rs index 6cf23e025..698203003 100644 --- a/runtime/src/handler.rs +++ b/runtime/src/handler.rs @@ -1,20 +1,8 @@ use alloc::vec::Vec; use crate::{Capture, Stack, ExitError, Opcode, - CreateScheme, Context, Machine, ExitReason, + Machine, ExitReason, H160, H256, U256}; - -/// Transfer from source to target, with given value. -#[derive(Clone, Copy, Debug)] -#[cfg_attr(feature = "with-codec", derive(codec::Encode, codec::Decode))] -#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))] -pub struct Transfer { - /// Source address. - pub source: H160, - /// Target address. - pub target: H160, - /// Transfer value. - pub value: U256, -} +use evm_core::{Context, CreateScheme, Transfer}; /// EVM context handler. pub trait Handler { diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index faf721d49..6ffa37d52 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -9,161 +9,27 @@ clippy::missing_panics_doc )] #![cfg_attr(not(feature = "std"), no_std)] -#![cfg_attr(not(feature = "tracing"), forbid(unused_imports))] +// #![cfg_attr(not(feature = "tracing"), forbid(unused_imports))] extern crate alloc; -#[cfg(feature = "tracing")] -pub mod tracing; - -#[cfg(feature = "tracing")] -macro_rules! event { - ($x:expr) => { - use crate::tracing::EventOnStack::*; - use solana_program::{tracer_api, compute_meter_remaining, compute_meter_set_remaining}; - - let mut remaining: u64 =0; - compute_meter_remaining::compute_meter_remaining(&mut remaining); - - // let mut message : Vec = Vec::new(); - // bincode::serialize_into(&mut message, &$x).unwrap(); - // let mut remaining1: u64 =0; - // compute_meter_remaining::compute_meter_remaining(&mut remaining1); - - let ptr = &$x as *const _ as *const u8; - - tracer_api::send_trace_message(ptr); - // let mut remaining1: u64 =0; - // compute_meter_remaining::compute_meter_remaining(&mut remaining1); - - compute_meter_set_remaining::compute_meter_set_remaining(remaining+12); - }; -} - -#[cfg(not(feature = "tracing"))] -macro_rules! event { - ($x:expr) => {} -} mod eval; -mod context; mod interrupt; mod handler; pub use evm_core::*; -pub use crate::context::{CreateScheme, CallScheme, Context}; pub use crate::interrupt::{Resolve, ResolveCall, ResolveCreate}; -pub use crate::handler::{Transfer, Handler}; +pub use crate::handler::Handler; pub use crate::eval::{save_return_value, save_created_address, Control}; -#[cfg(feature = "tracing")] -pub use crate::tracing::*; +// #[cfg(feature = "tracing")] +// pub use evm_core::tracing::*; -use alloc::vec::Vec; +// #[cfg(feature = "tracing")] // use solana_program::{compute_meter_remaining, compute_meter_set_remaining}; -macro_rules! step { - ( $self:expr, $handler:expr, $return:tt $($err:path)?; $($ok:path)? ) => ({ - let mut skip_step_result_event = true; - if let Some((opcode, stack)) = $self.machine.inspect() { - let mut remaining: u64 =0; - compute_meter_remaining::compute_meter_remaining(&mut remaining); - use alloc::vec; - let vec : Vec = vec![1 ,2, 3]; - event!(Step( - StepTrace { - context: $self.context.clone(), - opcode, - position: $self.machine.position().clone(), - stack: StackOnStack{ - data: $self.machine.stack().data() as *const _ as *const u8 as u64, - data_len: $self.machine.stack().len(), - limit: $self.machine.stack().limit() - }, - memory: MemoryOnStack{ - data: $self.machine.memory().data() as *const _ as *const u8 as u64, - data_len: $self.machine.memory().len(), - effective_len: $self.machine.memory().effective_len(), - limit: $self.machine.memory().limit(), - }, - vec : vec, - } - )); - compute_meter_set_remaining::compute_meter_set_remaining(remaining+12); - skip_step_result_event = false; - - match $handler.pre_validate(&$self.context, opcode, stack) { - Ok(()) => (), - Err(e) => { - $self.machine.exit(e.clone().into()); - $self.status = Err(e.into()); - }, - } - } - - match &$self.status { - Ok(()) => (), - Err(e) => { - #[allow(unused_parens)] - $return $($err)*(Capture::Exit(e.clone())) - }, - } - - let result = $self.machine.step(); - - let return_value = $self.machine.return_value(); - - if !skip_step_result_event { - event!(StepResult (StepResultTrace{ - result: result, - return_value: return_value.as_slice() as *const _ as *const u8 as u64, - return_value_len: return_value.len(), - - stack: StackOnStack{ - data: $self.machine.stack().data() as *const _ as *const u8 as u64, - data_len: $self.machine.stack().len(), - limit: $self.machine.stack().limit() - }, - memory: MemoryOnStack{ - data: $self.machine.memory().data() as *const _ as *const u8 as u64, - data_len: $self.machine.memory().len(), - effective_len: $self.machine.memory().effective_len(), - limit: $self.machine.memory().limit(), - } - })); - } - - match result { - Ok(()) => $($ok)?(()), - Err(Capture::Exit(e)) => { - $self.status = Err(e.clone()); - #[allow(unused_parens)] - $return $($err)*(Capture::Exit(e)) - }, - Err(Capture::Trap(opcode)) => { - match eval::eval($self, opcode, $handler) { - eval::Control::Continue => $($ok)?(()), - eval::Control::CallInterrupt(interrupt) => { - let resolve = ResolveCall::new($self); - #[allow(unused_parens)] - $return $($err)*(Capture::Trap(Resolve::Call(interrupt, resolve))) - }, - eval::Control::CreateInterrupt(interrupt) => { - let resolve = ResolveCreate::new($self); - #[allow(unused_parens)] - $return $($err)*(Capture::Trap(Resolve::Create(interrupt, resolve))) - }, - eval::Control::Exit(exit) => { - $self.machine.exit(exit.clone().into()); - $self.status = Err(exit.clone()); - #[allow(unused_parens)] - $return $($err)*(Capture::Exit(exit)) - }, - } - }, - } - }); -} +use alloc::vec::Vec; /// EVM runtime. /// @@ -209,14 +75,6 @@ impl Runtime { &self.machine } - /// Step the runtime. - pub fn step<'a, H: Handler>( - &'a mut self, - handler: &mut H, - ) -> Result<(), Capture>> { - step!(self, handler, return Err; Ok) - } - /// Loop stepping the runtime until it stops. pub fn run<'a, H: Handler>( &'a mut self, @@ -233,7 +91,7 @@ impl Runtime { let (steps_executed, capture) = { let context = &self.context; let pre_validate = |opcode, stack: &Stack| { handler.pre_validate(context, opcode, stack) }; - self.machine.run(max_steps - steps, pre_validate) + self.machine.run(max_steps - steps, pre_validate, &self.context) }; steps += steps_executed; diff --git a/runtime/src/tracing.rs b/runtime/src/tracing.rs deleted file mode 100644 index 86b47d67f..000000000 --- a/runtime/src/tracing.rs +++ /dev/null @@ -1,298 +0,0 @@ -//! Tools for tracing runtime events - -// use evm::Context; -// use evm::{H160, H256, U256, Stack, Memory, Opcode, Capture, Trap}; -// use evm_runtime::{CreateScheme, ExitReason, Transfer}; - -use crate::{H160, H256, U256, Context, Opcode, Stack, Memory, Capture, ExitReason, Trap, CreateScheme,Transfer}; -use alloc::vec::Vec; -// use solana_program::{tracer_api, compute_meter_remaining, compute_meter_set_remaining}; - -/// Trace event -#[derive(Debug, Clone)] -#[derive(serde::Serialize, serde::Deserialize)] -pub enum Event{ - /// Call event - Call { - /// Called code address - code_address: H160, - /// Transfer parameters - transfer: Option, - /// Input data provided to the call - #[serde(with = "serde_bytes")] - input: Vec, - /// Target gas - target_gas: Option, - /// Static call flag - is_static: bool, - /// Runtime context - context: Context, - }, - /// Create event - Create { - /// Creator address - caller: H160, - /// Address of the created account - address: H160, - /// Scheme - scheme: CreateScheme, - /// Value the created account is endowed with - value: U256, - /// Init code - #[serde(with = "serde_bytes")] - init_code: Vec, - /// Target Gas - target_gas: Option, - }, - /// Suicide event - Suicide { - /// Suicided address - address: H160, - /// Suicided contract heir - target: H160, - /// Balance before suicide - balance: U256, - }, - /// Exit event - Exit { - /// Exit reason - reason: ExitReason, - /// Return value - #[serde(with = "serde_bytes")] - return_value: Vec, - }, - /// Transactional Call event - TransactCall { - /// Caller account address - caller: H160, - /// Destination account address - address: H160, - /// Value transferred to the destination account - value: U256, - /// Input data provided to the call - #[serde(with = "serde_bytes")] - data: Vec, - /// Gas Limit - gas_limit: U256, - }, - /// Transactional Create event - TransactCreate { - /// Creator address - caller: H160, - /// Value the created account is endowed with - value: U256, - /// Init code - #[serde(with = "serde_bytes")] - init_code: Vec, - /// Gas limit - gas_limit: U256, - /// Address of the created account - address: H160, - }, - /// Transactional Create2 event - TransactCreate2 { - /// Creator address - caller: H160, - /// Value the created account is endowed with - value: U256, - /// Init code - #[serde(with = "serde_bytes")] - init_code: Vec, - /// Salt - salt: H256, - /// Gas limit - gas_limit: U256, - /// Address of the created account - address: H160, - }, - Step { - context: Context, - opcode: Opcode, - position: Result, - stack: Stack, - memory: Memory - }, - StepResult { - result: Result<(), Capture>, - #[serde(with = "serde_bytes")] - return_value: Vec, - stack: Stack, - memory: Memory - }, - SLoad { - address: H160, - index: U256, - value: U256 - }, - SStore { - address: H160, - index: U256, - value: U256 - }, - -} - -/// EVM stack. -#[derive(Clone, Debug)] -pub struct StackOnStack{ - pub data: u64,/// &[U256], - pub data_len: usize, - pub limit: usize, -} - -#[derive(Clone, Debug)] -pub struct MemoryOnStack { - pub data: u64, // &'a[u8], - pub data_len: usize, - pub effective_len: usize, - pub limit: usize, -} - - -#[derive(Debug, Clone)] -pub struct CallTrace{ - /// Called code address - pub code_address: H160, - /// Transfer parameters - pub transfer: Option, - /// Input data provided to the call - pub input: u64, - pub input_len: usize, - /// Target gas - pub target_gas: Option, - /// Static call flag - pub is_static: bool, - /// Runtime context - pub context: Context, -} - -#[derive(Debug, Clone)] -pub struct CreateTrace{ - /// Creator address - pub caller: H160, - /// Address of the created account - pub address: H160, - /// Scheme - pub scheme: CreateScheme, - /// Value the created account is endowed with - pub value: U256, - /// Init code - pub init_code: u64, - pub init_code_len: usize, - /// Target Gas - pub target_gas: Option, -} - -#[derive(Debug, Clone)] -pub struct ExitTrace{ - pub reason: ExitReason, - pub return_value: u64, - pub return_value_len: usize, -} - -#[derive(Debug, Clone)] -pub struct SuicideTrace{ - /// Suicided address - pub address: H160, - /// Suicided contract heir - pub target: H160, - /// Balance before suicide - pub balance: U256, -} - -#[derive(Debug, Clone)] -pub struct TransactCallTrace{ - /// Caller account address - pub caller: H160, - /// Destination account address - pub address: H160, - /// Value transferred to the destination account - pub value: U256, - /// Input data provided to the call - pub data: u64, - pub data_len: usize, - /// Gas Limit - pub gas_limit: U256, -} - - -#[derive(Debug, Clone)] -pub struct TransactCreateTrace{ - /// Creator address - pub caller: H160, - /// Value the created account is endowed with - pub value: U256, - /// Init code - pub init_code: u64, - pub init_code_len: usize, - /// Gas limit - pub gas_limit: U256, - /// Address of the created account - pub address: H160, -} - -#[derive(Debug, Clone)] -pub struct TransactCreate2Trace{ - /// Creator address - pub caller: H160, - /// Value the created account is endowed with - pub value: U256, - /// Init code - pub init_code: u64, - pub init_code_len: usize, - /// Salt - pub salt: H256, - /// Gas limit - pub gas_limit: U256, - /// Address of the created account - pub address: H160, -} - -#[derive(Debug, Clone)] -pub struct StepTrace{ - pub context: Context, - pub opcode: Opcode, - pub position: Result, - pub stack: StackOnStack, - pub memory: MemoryOnStack, - pub vec: Vec, -} - -#[derive(Debug, Clone)] -pub struct StepResultTrace{ - pub result: Result<(), Capture>, - pub return_value: u64, - pub return_value_len: usize, - pub stack: StackOnStack, - pub memory: MemoryOnStack, -} - -#[derive(Debug, Clone)] -pub struct SLoadTrace{ - pub address: H160, - pub index: U256, - pub value: U256 -} - -#[derive(Debug, Clone)] -pub struct SStoreTrace { - pub address: H160, - pub index: U256, - pub value: U256 -} - -/// Trace event -#[derive(Debug, Clone)] -pub enum EventOnStack{ - Call(CallTrace) , - Create(CreateTrace) , - Suicide(SuicideTrace) , - Exit(ExitTrace) , - TransactCall(TransactCallTrace) , - TransactCreate(TransactCreateTrace) , - TransactCreate2(TransactCreate2Trace) , - Step(StepTrace) , - StepResult(StepResultTrace), - SLoad(SLoadTrace), - SStore(SStoreTrace), -} diff --git a/src/backend/mod.rs b/src/backend/mod.rs index cce6047f7..9e1fe77ad 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -6,8 +6,8 @@ extern crate alloc; use alloc::vec::Vec; use core::convert::Infallible; -use evm_runtime::CreateScheme; -use crate::{Capture, Transfer, ExitReason, H160, H256, U256}; +use evm_core::{CreateScheme, Transfer}; +use crate::{Capture, ExitReason, H160, H256, U256}; /// Basic account information. #[derive(Clone, Eq, PartialEq, Debug, Default)] From a7f3656fff337a9efb1905f189734f6c2c1ca2a8 Mon Sep 17 00:00:00 2001 From: sinev-valentine Date: Thu, 23 Jun 2022 12:52:23 +0300 Subject: [PATCH 07/10] remove unused code --- core/src/context.rs | 4 --- core/src/error.rs | 2 -- core/src/lib.rs | 63 +++++++++----------------------------- core/src/memory.rs | 13 ++++---- core/src/opcode.rs | 2 -- core/src/stack.rs | 4 --- core/src/tracing.rs | 11 ++----- runtime/Cargo.toml | 9 ++---- runtime/src/eval/system.rs | 52 +++++++++++-------------------- runtime/src/lib.rs | 7 +---- src/lib.rs | 1 + 11 files changed, 45 insertions(+), 123 deletions(-) diff --git a/core/src/context.rs b/core/src/context.rs index 65861575b..02f2100ec 100644 --- a/core/src/context.rs +++ b/core/src/context.rs @@ -2,8 +2,6 @@ use crate::{H160, U256, H256}; /// Create scheme. #[derive(Clone, Copy, Eq, PartialEq, Debug)] -#[cfg_attr(feature = "with-codec", derive(codec::Encode, codec::Decode))] -#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))] pub enum CreateScheme { /// Legacy create scheme of `CREATE`. Legacy { @@ -25,8 +23,6 @@ pub enum CreateScheme { /// Call scheme. #[derive(Clone, Copy, Eq, PartialEq, Debug)] -#[cfg_attr(feature = "with-codec", derive(codec::Encode, codec::Decode))] -#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))] pub enum CallScheme { /// `CALL` Call, diff --git a/core/src/error.rs b/core/src/error.rs index 2483c2621..2ccba8f3f 100644 --- a/core/src/error.rs +++ b/core/src/error.rs @@ -5,8 +5,6 @@ pub type Trap = Opcode; /// Capture represents the result of execution. #[derive(Clone, Copy, Debug, Eq, PartialEq)] -#[cfg_attr(feature = "with-codec", derive(codec::Encode, codec::Decode))] -#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))] pub enum Capture { /// The machine has exited. It cannot be executed again. Exit(E), diff --git a/core/src/lib.rs b/core/src/lib.rs index 973875d7b..7a78e0447 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -1,8 +1,7 @@ //! Core layer for EVM. #![deny(warnings)] -// #![forbid(unused_variables, unused_imports)] -// #![forbid( unused_imports)] +#![forbid(unused_variables, unused_imports)] #![deny(clippy::all, clippy::pedantic, clippy::nursery)] #![allow( clippy::module_name_repetitions, @@ -43,43 +42,23 @@ use crate::eval::{eval, Control}; pub use crate::tracing::*; #[cfg(feature = "tracing")] -#[allow(unused_imports)] -use solana_program::{compute_meter_remaining, compute_meter_set_remaining, tracer_api}; +use solana_program::tracer_api; #[macro_export] #[cfg(feature = "tracing")] macro_rules! event { ($x:expr) => { - // use crate::tracing::Event::*; - // use solana_program::tracer_api; - - // let mut remaining: u64 =0; - // compute_meter_remaining::compute_meter_remaining(&mut remaining); - // remaining = remaining + 7; - let _ptr = &$x as *const _ as *const u8; - tracer_api::send_trace_message(ptr); - - // compute_meter_set_remaining::compute_meter_set_remaining(remaining); + let ptr = &$x as *const _ as *const u8; + tracer_api::send_trace_message(ptr); }; } - #[macro_export] #[cfg(not(feature = "tracing"))] macro_rules! event { ($x:expr) => {} } -#[cfg(feature = "tracing")] -extern "C" { - fn sol_compute_meter_remaining() -> u64; -} - -#[cfg(feature = "tracing")] -extern "C" { - fn sol_compute_meter_set_remaining() -> u64; -} - /// Core execution layer for EVM. #[cfg_attr(feature = "with-codec", derive(codec::Encode, codec::Decode))] #[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))] @@ -115,7 +94,7 @@ impl Machine { pub fn memory_mut(&mut self) -> &mut Memory { &mut self.memory } /// Return a reference of the program counter. - pub fn position(&self) -> &Result { + pub fn position(&self) -> &Result { &self.position } @@ -175,7 +154,7 @@ impl Machine { pub fn run(&mut self, max_steps: u64, mut pre_validate: F, - #[allow(unused_variables)] context : &Context + _context : &Context ) -> (u64, Capture) where F: FnMut(Opcode, &Stack) -> Result<(), ExitError> { @@ -193,21 +172,15 @@ impl Machine { } }; - - // - // #[cfg(feature = "tracing")] - // unsafe {sol_compute_meter_remaining();} - // event!(Event::Step( - // StepTrace { - // context: context, - // opcode, - // position: &self.position, - // stack: &self.stack, - // memory: &self.memory, - // } - // )); - // #[cfg(feature = "tracing")] - // unsafe {sol_compute_meter_set_remaining();} + event!(Event::Step( + StepTrace { + context: _context, + opcode, + position: &self.position, + stack: &self.stack, + memory: &self.memory, + } + )); if let Err(error) = pre_validate(opcode, &self.stack()) { let reason = ExitReason::from(error); @@ -234,17 +207,12 @@ impl Machine { }, }; - - #[cfg(feature = "tracing")] - unsafe {sol_compute_meter_remaining();} event!(Event::StepResult (StepResultTrace{ result: &result, return_value: &self.return_value(), stack: &self.stack, memory: &self.memory })); - #[cfg(feature = "tracing")] - unsafe {sol_compute_meter_set_remaining();} if let Err(capture) = result { return (step, capture) @@ -255,4 +223,3 @@ impl Machine { } } - diff --git a/core/src/memory.rs b/core/src/memory.rs index a02836266..ca3f75df2 100644 --- a/core/src/memory.rs +++ b/core/src/memory.rs @@ -17,13 +17,13 @@ pub struct Memory { impl Memory { /// Create a new memory with the given limit. #[must_use] - pub const fn new(limit: usize) -> Self { - Self { - data: Vec::new(), - effective_len: 0_usize, - limit, - } + pub const fn new(limit: usize) -> Self { + Self { + data: Vec::new(), + effective_len: 0_usize, + limit, } + } pub fn from(data: &[u8], len: usize, limit: usize) -> Self { Self { @@ -160,5 +160,4 @@ impl Memory { self.set(memory_offset, data_by_offset, Some(len)) } - } diff --git a/core/src/opcode.rs b/core/src/opcode.rs index 08162b246..9ea968b97 100644 --- a/core/src/opcode.rs +++ b/core/src/opcode.rs @@ -1,8 +1,6 @@ #![allow(clippy::use_self)] /// Opcode enum. One-to-one corresponding to an `u8` value. #[derive(Clone, Copy, Debug, Eq, PartialEq)] -#[cfg_attr(feature = "with-codec", derive(codec::Encode, codec::Decode))] -#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))] pub struct Opcode(pub u8); // Core opcodes. diff --git a/core/src/stack.rs b/core/src/stack.rs index 91ab783f3..c30aafbec 100644 --- a/core/src/stack.rs +++ b/core/src/stack.rs @@ -174,10 +174,6 @@ impl Stack { Ok(()) } - pub fn data(&self) -> &[U256] { - &self.data - } - pub fn data_vec(&self) -> &Vec { &self.data } diff --git a/core/src/tracing.rs b/core/src/tracing.rs index c118534d4..0087fa21b 100644 --- a/core/src/tracing.rs +++ b/core/src/tracing.rs @@ -1,12 +1,5 @@ -//! Tools for tracing runtime events - -// use evm::Context; -// use evm::{H160, H256, U256, Stack, Memory, Opcode, Capture, Trap}; -// use evm_runtime::{CreateScheme, ExitReason, Transfer}; - -use crate::{H160, H256, U256, Context, Opcode, Stack, Memory, Capture, ExitReason, Trap, CreateScheme,Transfer}; +use crate::{H160, H256, U256, Context, Opcode, Stack, Memory, Capture, ExitReason, Trap, CreateScheme, Transfer}; use alloc::vec::Vec; -// use solana_program::{tracer_api, compute_meter_remaining, compute_meter_set_remaining}; #[derive(Debug, Clone)] @@ -135,7 +128,7 @@ pub struct SStoreTrace { /// Trace event #[derive(Debug, Clone)] -#[allow(dead_code)] +// #[allow(dead_code)] pub enum Event<'a>{ Call(CallTrace<'a>) , Create(CreateTrace<'a>) , diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index e2cbbd83b..9365fa452 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -14,16 +14,11 @@ sha3 = { version = "0.8", default-features = false } codec = { package = "parity-scale-codec", version = "1.3", default-features = false, features = ["derive", "full"], optional = true } serde = { version = "1.0", default-features = false, features = ["derive"], optional = true } serde_bytes = { version = "0.11.5", optional = true } -environmental = { version = "1.1.2", default-features = false, optional = true} -bincode = "1.3.3" solana-program = { version = "=1.9.12", path = "/home/user/CLionProjects/neonlabs/solana/sdk/program", default_features = false } [features] default = ["std"] with-codec = ["codec"] with-serde = ["serde", "serde_bytes"] -std = ["evm-core/std", "sha3/std", "environmental/std"] -tracing = [ - "environmental", - "evm-core/tracing" -] +std = ["evm-core/std", "sha3/std"] +tracing = ["evm-core/tracing"] diff --git a/runtime/src/eval/system.rs b/runtime/src/eval/system.rs index 5339775f6..b387adb63 100644 --- a/runtime/src/eval/system.rs +++ b/runtime/src/eval/system.rs @@ -2,15 +2,13 @@ use core::cmp::min; use alloc::vec::Vec; use crate::{Runtime, ExitError, Handler, Capture, Transfer, ExitReason, CreateScheme, CallScheme, Context, ExitSucceed, ExitFatal, H160, H256, U256}; use super::Control; -// use evm_core::event; +use evm_core::event; -// #[cfg(feature = "tracing")] -// use evm_core::*; -// #[cfg(feature = "tracing")] -// use solana_program::{compute_meter_remaining, compute_meter_set_remaining}; +#[cfg(feature = "tracing")] +use evm_core::{Event, SStoreTrace, SLoadTrace}; -// #[cfg(feature = "tracing")] -// use solana_program::tracer_api; +#[cfg(feature = "tracing")] +use solana_program::tracer_api; pub fn sha3(runtime: &mut Runtime, handler: &H) -> Control { @@ -188,20 +186,13 @@ pub fn sload(runtime: &mut Runtime, handler: &H) -> Control { let value = handler.storage(runtime.context.address, index); push_u256!(runtime, value); - // #[cfg(feature = "tracing")] - // let mut remaining: u64 =0; - // #[cfg(feature = "tracing")] - // compute_meter_remaining::compute_meter_remaining(&mut remaining); - // - // event!(Event::SLoad( - // SLoadTrace{ - // address: runtime.context.address, - // index, - // value - // } - // )); - // #[cfg(feature = "tracing")] - // compute_meter_set_remaining::compute_meter_set_remaining(remaining+12); + event!(Event::SLoad( + SLoadTrace{ + address: runtime.context.address, + index, + value + } + )); Control::Continue } @@ -209,19 +200,12 @@ pub fn sload(runtime: &mut Runtime, handler: &H) -> Control { pub fn sstore(runtime: &mut Runtime, handler: &mut H) -> Control { pop_u256!(runtime, index, value); - // #[cfg(feature = "tracing")] - // let mut remaining: u64 =0; - // #[cfg(feature = "tracing")] - // compute_meter_remaining::compute_meter_remaining(&mut remaining); - // - // event!(Event::SStore( SStoreTrace{ - // address: runtime.context.address, - // index, - // value - // } - // )); - // #[cfg(feature = "tracing")] - // compute_meter_set_remaining::compute_meter_set_remaining(remaining+12); + event!(Event::SStore( SStoreTrace{ + address: runtime.context.address, + index, + value + } + )); match handler.set_storage(runtime.context.address, index, value) { Ok(()) => Control::Continue, diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 6ffa37d52..00f02958f 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -9,7 +9,7 @@ clippy::missing_panics_doc )] #![cfg_attr(not(feature = "std"), no_std)] -// #![cfg_attr(not(feature = "tracing"), forbid(unused_imports))] +#![cfg_attr(not(feature = "tracing"), forbid(unused_imports))] extern crate alloc; @@ -23,11 +23,6 @@ pub use evm_core::*; pub use crate::interrupt::{Resolve, ResolveCall, ResolveCreate}; pub use crate::handler::Handler; pub use crate::eval::{save_return_value, save_created_address, Control}; -// #[cfg(feature = "tracing")] -// pub use evm_core::tracing::*; - -// #[cfg(feature = "tracing")] -// use solana_program::{compute_meter_remaining, compute_meter_set_remaining}; use alloc::vec::Vec; diff --git a/src/lib.rs b/src/lib.rs index 395121265..84682de15 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,4 +16,5 @@ extern crate alloc; pub use evm_core::*; pub use evm_runtime::*; +pub use evm_core::event; pub mod backend; From 539e5e9d14a557f2581b4599f99bcba6325bd900 Mon Sep 17 00:00:00 2001 From: sinev-valentine Date: Thu, 23 Jun 2022 17:51:08 +0300 Subject: [PATCH 08/10] update --- src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 84682de15..395121265 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,5 +16,4 @@ extern crate alloc; pub use evm_core::*; pub use evm_runtime::*; -pub use evm_core::event; pub mod backend; From c461a6a676e4efbcee52254c6e3bb44a5c365ae5 Mon Sep 17 00:00:00 2001 From: sinev-valentine Date: Fri, 24 Jun 2022 11:16:56 +0300 Subject: [PATCH 09/10] update --- core/Cargo.toml | 3 ++- core/src/lib.rs | 2 +- runtime/Cargo.toml | 3 ++- runtime/src/eval/system.rs | 2 +- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/core/Cargo.toml b/core/Cargo.toml index fa6c39503..834a5fab9 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -19,7 +19,8 @@ borsh = { version = "0.9" } serde = { version = "1.0", default-features = false, features = ["derive"], optional = true } impl-serde = { version = "0.3", optional = true } serde_bytes = { version = "0.11.5", optional = true } -solana-program = { version = "=1.9.12", path = "/home/user/CLionProjects/neonlabs/solana/sdk/program", default_features = false } +#solana-program = { version = "=1.9.12", path = "/home/user/CLionProjects/neonlabs/solana/sdk/program", default_features = false } +solana-program_neon = { version = "=1.9.12", path = "/home/user/CLionProjects/neonlabs/solana/sdk/program", default_features = false, package = "solana-program" } [dev-dependencies] hex = "0.4" diff --git a/core/src/lib.rs b/core/src/lib.rs index e569b9a44..0a493c8e6 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -41,7 +41,7 @@ use crate::eval::{eval, Control}; pub use crate::tracing::*; #[cfg(feature = "tracing")] -use solana_program::tracer_api; +use solana_program_neon::tracer_api; #[macro_export] #[cfg(feature = "tracing")] diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index 879171175..142fca483 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -14,7 +14,8 @@ sha3 = { version = "0.8", default-features = false } codec = { package = "parity-scale-codec", version = "1.3", default-features = false, features = ["derive", "full"], optional = true } serde = { version = "1.0", default-features = false, features = ["derive"], optional = true } serde_bytes = { version = "0.11.5", optional = true } -solana-program = { version = "=1.9.12", path = "/home/user/CLionProjects/neonlabs/solana/sdk/program", default_features = false } +#solana-program = { version = "=1.9.12", path = "/home/user/CLionProjects/neonlabs/solana/sdk/program", default_features = false } +solana-program_neon = { version = "=1.9.12", path = "/home/user/CLionProjects/neonlabs/solana/sdk/program", default_features = false, package = "solana-program" } borsh = { version = "0.9" } [features] diff --git a/runtime/src/eval/system.rs b/runtime/src/eval/system.rs index b387adb63..cef4712ac 100644 --- a/runtime/src/eval/system.rs +++ b/runtime/src/eval/system.rs @@ -8,7 +8,7 @@ use evm_core::event; use evm_core::{Event, SStoreTrace, SLoadTrace}; #[cfg(feature = "tracing")] -use solana_program::tracer_api; +use solana_program_neon::tracer_api; pub fn sha3(runtime: &mut Runtime, handler: &H) -> Control { From e9e86f3527f3c706c0beaab0555f5cb8efb0a923 Mon Sep 17 00:00:00 2001 From: sinev-valentine Date: Fri, 24 Jun 2022 19:25:13 +0300 Subject: [PATCH 10/10] update --- Cargo.toml | 1 - core/Cargo.toml | 2 -- core/src/lib.rs | 7 +++++-- runtime/Cargo.toml | 2 -- runtime/src/eval/system.rs | 2 +- 5 files changed, 6 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 26cb93bdf..d545b17c0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,6 @@ with-codec = ["codec", "evm-core/with-codec", "evm-runtime/with-codec"] with-serde = ["serde", "serde_bytes", "evm-core/with-serde", "evm-runtime/with-serde"] std = ["evm-core/std", "evm-runtime/std", "sha3/std", "serde/std", "codec/std", "log/std"] tracing = ["evm-runtime/tracing", "evm-core/tracing"] - #[workspace] #members = [ # "core", diff --git a/core/Cargo.toml b/core/Cargo.toml index 834a5fab9..506557757 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -19,8 +19,6 @@ borsh = { version = "0.9" } serde = { version = "1.0", default-features = false, features = ["derive"], optional = true } impl-serde = { version = "0.3", optional = true } serde_bytes = { version = "0.11.5", optional = true } -#solana-program = { version = "=1.9.12", path = "/home/user/CLionProjects/neonlabs/solana/sdk/program", default_features = false } -solana-program_neon = { version = "=1.9.12", path = "/home/user/CLionProjects/neonlabs/solana/sdk/program", default_features = false, package = "solana-program" } [dev-dependencies] hex = "0.4" diff --git a/core/src/lib.rs b/core/src/lib.rs index 0a493c8e6..5d40ea221 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -41,14 +41,17 @@ use crate::eval::{eval, Control}; pub use crate::tracing::*; #[cfg(feature = "tracing")] -use solana_program_neon::tracer_api; +extern "C" {fn sol_send_trace_message(val: *const u8) -> u64;} + #[macro_export] #[cfg(feature = "tracing")] macro_rules! event { ($x:expr) => { let ptr = &$x as *const _ as *const u8; - tracer_api::send_trace_message(ptr); + unsafe { + sol_send_trace_message(ptr); + } }; } diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index 142fca483..73349d7f0 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -14,8 +14,6 @@ sha3 = { version = "0.8", default-features = false } codec = { package = "parity-scale-codec", version = "1.3", default-features = false, features = ["derive", "full"], optional = true } serde = { version = "1.0", default-features = false, features = ["derive"], optional = true } serde_bytes = { version = "0.11.5", optional = true } -#solana-program = { version = "=1.9.12", path = "/home/user/CLionProjects/neonlabs/solana/sdk/program", default_features = false } -solana-program_neon = { version = "=1.9.12", path = "/home/user/CLionProjects/neonlabs/solana/sdk/program", default_features = false, package = "solana-program" } borsh = { version = "0.9" } [features] diff --git a/runtime/src/eval/system.rs b/runtime/src/eval/system.rs index cef4712ac..08d345497 100644 --- a/runtime/src/eval/system.rs +++ b/runtime/src/eval/system.rs @@ -8,7 +8,7 @@ use evm_core::event; use evm_core::{Event, SStoreTrace, SLoadTrace}; #[cfg(feature = "tracing")] -use solana_program_neon::tracer_api; +extern "C" {fn sol_send_trace_message(val: *const u8) -> u64;} pub fn sha3(runtime: &mut Runtime, handler: &H) -> Control {