Skip to content

Commit

Permalink
refactor: replace internal CallKind (#6824)
Browse files Browse the repository at this point in the history
* refactor: replace internal `CallKind`

* chore: fmt
  • Loading branch information
onbjerg authored Jan 16, 2024
1 parent b3f57f1 commit e4de7b7
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 77 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/debugger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ foundry-common.workspace = true
foundry-compilers.workspace = true
foundry-evm-core.workspace = true
foundry-evm-traces.workspace = true
revm-inspectors.workspace = true

alloy-primitives.workspace = true

Expand Down
8 changes: 3 additions & 5 deletions crates/debugger/src/tui/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
use crate::{Debugger, ExitReason};
use alloy_primitives::Address;
use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers, MouseEvent, MouseEventKind};
use foundry_evm_core::{
debug::{DebugNodeFlat, DebugStep},
utils::CallKind,
};
use foundry_evm_core::debug::{DebugNodeFlat, DebugStep};
use revm_inspectors::tracing::types::CallKind;
use std::{cell::RefCell, ops::ControlFlow};

/// This is currently used to remember last scroll position so screen doesn't wiggle as much.
Expand Down Expand Up @@ -271,7 +269,7 @@ impl DebuggerContext<'_> {
if let Some(step) = node.steps.iter().position(|step| step.pc == *pc) {
self.draw_memory.inner_call_index = i;
self.current_step = step;
break
break;
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion crates/debugger/src/tui/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::context::DebuggerContext;
use crate::op::OpcodeParam;
use alloy_primitives::U256;
use foundry_compilers::sourcemap::SourceElement;
use foundry_evm_core::{debug::Instruction, utils::CallKind};
use foundry_evm_core::debug::Instruction;
use ratatui::{
layout::{Alignment, Constraint, Direction, Layout, Rect},
style::{Color, Modifier, Style},
Expand All @@ -13,6 +13,7 @@ use ratatui::{
widgets::{Block, Borders, Paragraph, Wrap},
};
use revm::interpreter::opcode;
use revm_inspectors::tracing::types::CallKind;
use std::{cmp, collections::VecDeque, fmt::Write, io};

impl DebuggerContext<'_> {
Expand Down
1 change: 1 addition & 0 deletions crates/evm/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ revm = { workspace = true, default-features = false, features = [
"arbitrary",
"optimism",
] }
revm-inspectors.workspace = true
alloy-providers = { workspace = true }
alloy-transport = { workspace = true }
alloy-rpc-types = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/evm/core/src/debug.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::utils::CallKind;
use alloy_primitives::{Address, U256};
use revm::interpreter::OpCode;
use revm_inspectors::tracing::types::CallKind;
use serde::{Deserialize, Serialize};
use std::fmt::Display;

Expand Down
66 changes: 3 additions & 63 deletions crates/evm/core/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,79 +1,19 @@
use alloy_json_abi::{Function, JsonAbi};
use alloy_primitives::FixedBytes;
use alloy_rpc_types::{Block, Transaction};
use ethers_core::types::{ActionType, CallType, Chain, H256, U256};
use ethers_core::types::{Chain, H256, U256};
use eyre::ContextCompat;
use foundry_common::types::ToAlloy;
use revm::{
interpreter::{CallScheme, InstructionResult},
primitives::{CreateScheme, Eval, Halt, SpecId, TransactTo},
interpreter::InstructionResult,
primitives::{Eval, Halt, SpecId, TransactTo},
};
use serde::{Deserialize, Serialize};

pub use foundry_compilers::utils::RuntimeOrHandle;
pub use revm::primitives::State as StateChangeset;

pub use crate::ic::*;

// TODO(onbjerg): Remove this and use `CallKind` from the tracer.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
#[derive(Default)]
pub enum CallKind {
#[default]
Call,
StaticCall,
CallCode,
DelegateCall,
Create,
Create2,
}

impl From<CallScheme> for CallKind {
fn from(scheme: CallScheme) -> Self {
match scheme {
CallScheme::Call => CallKind::Call,
CallScheme::StaticCall => CallKind::StaticCall,
CallScheme::CallCode => CallKind::CallCode,
CallScheme::DelegateCall => CallKind::DelegateCall,
}
}
}

impl From<CreateScheme> for CallKind {
fn from(create: CreateScheme) -> Self {
match create {
CreateScheme::Create => CallKind::Create,
CreateScheme::Create2 { .. } => CallKind::Create2,
}
}
}

impl From<CallKind> for ActionType {
fn from(kind: CallKind) -> Self {
match kind {
CallKind::Call | CallKind::StaticCall | CallKind::DelegateCall | CallKind::CallCode => {
ActionType::Call
}
CallKind::Create => ActionType::Create,
CallKind::Create2 => ActionType::Create,
}
}
}

impl From<CallKind> for CallType {
fn from(ty: CallKind) -> Self {
match ty {
CallKind::Call => CallType::Call,
CallKind::StaticCall => CallType::StaticCall,
CallKind::CallCode => CallType::CallCode,
CallKind::DelegateCall => CallType::DelegateCall,
CallKind::Create => CallType::None,
CallKind::Create2 => CallType::None,
}
}
}

/// Small helper function to convert [U256] into [H256].
#[inline]
pub fn u256_to_h256_le(u: U256) -> H256 {
Expand Down
2 changes: 1 addition & 1 deletion crates/evm/evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ revm = { workspace = true, default-features = false, features = [
"optional_no_base_fee",
"arbitrary",
] }

revm-inspectors.workspace = true
ethers-core.workspace = true
ethers-signers.workspace = true
itertools.workspace = true
Expand Down
5 changes: 3 additions & 2 deletions crates/evm/evm/src/inspectors/debugger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use foundry_evm_core::{
backend::DatabaseExt,
constants::CHEATCODE_ADDRESS,
debug::{DebugArena, DebugNode, DebugStep, Instruction},
utils::{gas_used, CallKind},
utils::gas_used,
};
use revm::{
interpreter::{
Expand All @@ -13,6 +13,7 @@ use revm::{
},
EVMData, Inspector,
};
use revm_inspectors::tracing::types::CallKind;

/// An inspector that collects debug nodes on every step of the interpreter.
#[derive(Clone, Debug, Default)]
Expand Down Expand Up @@ -127,7 +128,7 @@ impl<DB: DatabaseExt> Inspector<DB> for Debugger {
// TODO: Does this increase gas cost?
if let Err(err) = data.journaled_state.load_account(call.caller, data.db) {
let gas = Gas::new(call.gas_limit);
return (InstructionResult::Revert, None, gas, err.abi_encode_revert())
return (InstructionResult::Revert, None, gas, err.abi_encode_revert());
}

let nonce = data.journaled_state.account(call.caller).info.nonce;
Expand Down
2 changes: 2 additions & 0 deletions crates/forge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ ethers-middleware.workspace = true
ethers-providers.workspace = true
ethers-signers.workspace = true

revm-inspectors.workspace = true

comfy-table = "7"
eyre.workspace = true
proptest = "1"
Expand Down
6 changes: 2 additions & 4 deletions crates/forge/bin/cmd/script/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ use foundry_common::{
types::{ToAlloy, ToEthers},
SELECTOR_LEN,
};
use foundry_evm::{
constants::DEFAULT_CREATE2_DEPLOYER,
traces::{CallKind, CallTraceDecoder},
};
use foundry_evm::{constants::DEFAULT_CREATE2_DEPLOYER, traces::CallTraceDecoder};
use revm_inspectors::tracing::types::CallKind;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

Expand Down

0 comments on commit e4de7b7

Please sign in to comment.