Skip to content

Commit

Permalink
NDEV-2252: Clean up tracer implementation (#208)
Browse files Browse the repository at this point in the history
* NDEV-2252: Use preserve_order serde_json feature

* NDEV-2252: Make StructLog and StructLoggerResult Geth-compatible

NDEV-2252: Fix StructLog field order

NDEV-2252: Fix StructLoggerResult field order

NDEV-2252: Output storage only on SLOAD and SSTORE opcodes

NDEV-2252: Output storage in hex format without 0x prefix

NDEV-2252: Output memory in hex format without 0x prefix

NDEV-2252: Add StructLoggerResult serialization tests

NDEV-2252: Fix StructLog format

NDEV-2252: Add github link

* NDEV-2252: Make tracer_config optional

* NDEV-2252: Remove unused trace Events

* NDEV-2252: Add more links to Geth structs
  • Loading branch information
andreisilviudragnea authored and afalaleev committed Oct 18, 2023
1 parent 07ef2ed commit a11ae14
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 164 deletions.
5 changes: 3 additions & 2 deletions evm_loader/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion evm_loader/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ evm-loader = { path = "../program", default-features = false, features = ["log"]
solana-sdk = "=1.16.15"
solana-client = "=1.16.15"
serde = "1.0.186"
serde_json = "1.0.85"
serde_json = { version = "1.0.107", features = ["preserve_order"] }
ethnum = { version = "1.4", default-features = false, features = ["serde"] }
tokio = { version = "1", features = ["full"] }
tracing = "0.1"
Expand Down
2 changes: 1 addition & 1 deletion evm_loader/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ solana-clap-utils = "=1.16.15"
solana-cli-config = "=1.16.15"
hex = "0.4.2"
serde = "1.0.186"
serde_json = "1.0.85"
serde_json = { version = "1.0.107", features = ["preserve_order"] }
log = "0.4.17"
fern = "0.6"
ethnum = { version = "1.4", default-features = false, features = ["serde"] }
Expand Down
2 changes: 1 addition & 1 deletion evm_loader/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ spl-associated-token-account = { version = "~1.1", default-features = false, fea
bs58 = "0.4.0"
hex = "0.4.2"
serde = "1.0.186"
serde_json = "1.0.105"
serde_json = { version = "1.0.107", features = ["preserve_order"] }
log = "0.4.17"
rand = "0.8"
ethnum = { version = "1.4", default-features = false, features = ["serde"] }
Expand Down
2 changes: 1 addition & 1 deletion evm_loader/program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ borsh = "0.9"
bincode = "1"
serde_bytes = "0.11.12"
serde = { version = "1.0.186", default-features = false, features = ["derive", "rc"] }
serde_json = { version = "1.0.105", optional = true }
serde_json = { version = "1.0.107", features = ["preserve_order"], optional = true }
ethnum = { version = "1.4", default-features = false, features = ["serde"] }
const_format = { version = "0.2.21" }
cfg-if = { version = "1.0" }
Expand Down
68 changes: 4 additions & 64 deletions evm_loader/program/src/evm/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ use std::ops::Range;
use solana_program::program_memory::{sol_memcpy, sol_memset};

use crate::error::Error;
#[cfg(not(target_os = "solana"))]
use crate::evm::tracing::TracerTypeOpt;

use super::utils::checked_next_multiple_of_32;
use super::{tracing_event, Buffer};
use super::Buffer;

const MAX_MEMORY_SIZE: usize = 64 * 1024;
const MEMORY_CAPACITY: usize = 1024;
Expand All @@ -20,23 +18,14 @@ pub struct Memory {
data: *mut u8,
capacity: usize,
size: usize,
#[cfg(not(target_os = "solana"))]
tracer: TracerTypeOpt,
}

impl Memory {
pub fn new(#[cfg(not(target_os = "solana"))] tracer: TracerTypeOpt) -> Self {
Self::with_capacity(
MEMORY_CAPACITY,
#[cfg(not(target_os = "solana"))]
tracer,
)
pub fn new() -> Self {
Self::with_capacity(MEMORY_CAPACITY)
}

pub fn with_capacity(
capacity: usize,
#[cfg(not(target_os = "solana"))] tracer: TracerTypeOpt,
) -> Self {
pub fn with_capacity(capacity: usize) -> Self {
unsafe {
let layout = Layout::from_size_align_unchecked(capacity, MEMORY_ALIGN);
let data = crate::allocator::EVM.alloc_zeroed(layout);
Expand All @@ -48,8 +37,6 @@ impl Memory {
data,
capacity,
size: 0,
#[cfg(not(target_os = "solana"))]
tracer,
}
}
}
Expand All @@ -70,8 +57,6 @@ impl Memory {
data,
capacity,
size: v.len(),
#[cfg(not(target_os = "solana"))]
tracer: None,
}
}
}
Expand Down Expand Up @@ -157,14 +142,6 @@ impl Memory {
}

pub fn write_32(&mut self, offset: usize, value: &[u8; 32]) -> Result<(), Error> {
tracing_event!(
self,
super::tracing::Event::MemorySet {
offset,
data: value.to_vec()
}
);

self.realloc(offset, 32)?;

unsafe {
Expand All @@ -176,14 +153,6 @@ impl Memory {
}

pub fn write_byte(&mut self, offset: usize, value: u8) -> Result<(), Error> {
tracing_event!(
self,
super::tracing::Event::MemorySet {
offset,
data: vec![value]
}
);

self.realloc(offset, 1)?;

unsafe {
Expand Down Expand Up @@ -214,45 +183,16 @@ impl Memory {

match source_offset {
source_offset if source_offset >= source.len() => {
tracing_event!(
self,
super::tracing::Event::MemorySet {
offset,
data: vec![0; length]
}
);

sol_memset(data, 0, length);
}
source_offset if (source_offset + length) > source.len() => {
let source = &source[source_offset..];

tracing_event!(
self,
super::tracing::Event::MemorySet {
offset,
data: {
let mut buffer = vec![0_u8; length];
buffer[..source.len()].copy_from_slice(source);
buffer
}
}
);

data[..source.len()].copy_from_slice(source);
data[source.len()..].fill(0_u8);
}
source_offset => {
let source = &source[source_offset..source_offset + length];

tracing_event!(
self,
super::tracing::Event::MemorySet {
offset,
data: source.to_vec()
}
);

sol_memcpy(data, source, length);
}
}
Expand Down
30 changes: 6 additions & 24 deletions evm_loader/program/src/evm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,14 +284,8 @@ impl<B: Database> Machine<B> {
call_data: trx.extract_call_data(),
return_data: Buffer::empty(),
return_range: 0..0,
stack: Stack::new(
#[cfg(not(target_os = "solana"))]
tracer.clone(),
),
memory: Memory::new(
#[cfg(not(target_os = "solana"))]
tracer.clone(),
),
stack: Stack::new(),
memory: Memory::new(),
pc: 0_usize,
is_static: false,
reason: Reason::Call,
Expand Down Expand Up @@ -336,14 +330,8 @@ impl<B: Database> Machine<B> {
gas_limit: trx.gas_limit(),
return_data: Buffer::empty(),
return_range: 0..0,
stack: Stack::new(
#[cfg(not(target_os = "solana"))]
tracer.clone(),
),
memory: Memory::new(
#[cfg(not(target_os = "solana"))]
tracer.clone(),
),
stack: Stack::new(),
memory: Memory::new(),
pc: 0_usize,
is_static: false,
reason: Reason::Create,
Expand Down Expand Up @@ -449,14 +437,8 @@ impl<B: Database> Machine<B> {
call_data,
return_data: Buffer::empty(),
return_range: 0..0,
stack: Stack::new(
#[cfg(not(target_os = "solana"))]
self.tracer.clone(),
),
memory: Memory::new(
#[cfg(not(target_os = "solana"))]
self.tracer.clone(),
),
stack: Stack::new(),
memory: Memory::new(),
pc: 0_usize,
is_static: self.is_static,
reason,
Expand Down
1 change: 0 additions & 1 deletion evm_loader/program/src/evm/opcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,6 @@ impl<B: Database> Machine<B> {
let index = self.stack.pop_u256()?;
let value = *self.stack.pop_array()?;

tracing_event!(self, super::tracing::Event::StorageSet { index, value });
tracing_event!(self, super::tracing::Event::StorageAccess { index, value });

backend.set_storage(self.context.contract, index, value)?;
Expand Down
22 changes: 2 additions & 20 deletions evm_loader/program/src/evm/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,19 @@ use std::{

use ethnum::{I256, U256};

#[cfg(not(target_os = "solana"))]
use crate::evm::TracerTypeOpt;
use crate::{error::Error, types::Address};

use super::tracing_event;

const ELEMENT_SIZE: usize = 32;
const STACK_SIZE: usize = ELEMENT_SIZE * 128;

pub struct Stack {
begin: *mut u8,
end: *mut u8,
top: *mut u8,
#[cfg(not(target_os = "solana"))]
tracer: TracerTypeOpt,
}

impl Stack {
pub fn new(#[cfg(not(target_os = "solana"))] tracer: TracerTypeOpt) -> Self {
pub fn new() -> Self {
let (begin, end) = unsafe {
let layout = Layout::from_size_align_unchecked(STACK_SIZE, ELEMENT_SIZE);
let begin = crate::allocator::EVM.alloc(layout);
Expand All @@ -42,8 +36,6 @@ impl Stack {
begin,
end,
top: begin,
#[cfg(not(target_os = "solana"))]
tracer,
}
}

Expand All @@ -70,13 +62,6 @@ impl Stack {
return Err(Error::StackOverflow);
}

tracing_event!(
self,
super::tracing::Event::StackPush {
value: unsafe { *self.read() }
}
);

unsafe {
self.top = self.top.add(32);
}
Expand Down Expand Up @@ -315,10 +300,7 @@ impl<'de> serde::Deserialize<'de> for Stack {
return Err(E::invalid_length(v.len(), &self));
}

let mut stack = Stack::new(
#[cfg(not(target_os = "solana"))]
None,
);
let mut stack = Stack::new();
unsafe {
stack.top = stack.begin.add(v.len());

Expand Down
18 changes: 6 additions & 12 deletions evm_loader/program/src/evm/tracing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,13 @@ pub enum Event {
gas_used: u64,
return_data: Option<Vec<u8>>,
},
StackPush {
value: [u8; 32],
},
MemorySet {
offset: usize,
data: Vec<u8>,
},
StorageSet {
index: U256,
value: [u8; 32],
},
StorageAccess {
index: U256,
value: [u8; 32],
},
}

/// See <https://github.com/ethereum/go-ethereum/blob/master/internal/ethapi/api.go#L993>
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BlockOverrides {
Expand All @@ -83,6 +73,7 @@ pub struct BlockOverrides {
pub base_fee: Option<U256>, // NOT SUPPORTED BY Neon EVM
}

/// See <https://github.com/ethereum/go-ethereum/blob/master/internal/ethapi/api.go#L942>
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AccountOverride {
Expand All @@ -108,8 +99,10 @@ impl AccountOverride {
}
}

/// See <https://github.com/ethereum/go-ethereum/blob/master/internal/ethapi/api.go#L951>
pub type AccountOverrides = HashMap<Address, AccountOverride>;

/// See <https://github.com/ethereum/go-ethereum/blob/master/eth/tracers/api.go#L151>
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
#[allow(clippy::module_name_repetitions, clippy::struct_excessive_bools)]
Expand All @@ -124,9 +117,10 @@ pub struct TraceConfig {
pub enable_return_data: bool,
pub tracer: Option<String>,
pub timeout: Option<String>,
pub tracer_config: Value,
pub tracer_config: Option<Value>,
}

/// See <https://github.com/ethereum/go-ethereum/blob/master/eth/tracers/api.go#L163>
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
#[allow(clippy::module_name_repetitions)]
Expand Down
Loading

0 comments on commit a11ae14

Please sign in to comment.