Skip to content

Commit

Permalink
feat: debug trace call
Browse files Browse the repository at this point in the history
  • Loading branch information
agostbiro committed Oct 3, 2023
1 parent f35dcc6 commit 59c4f67
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 76 deletions.
66 changes: 38 additions & 28 deletions crates/rethnet_evm/src/debug_trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,30 +57,8 @@ where
} = evm
.inspect_ref(&mut tracer)
.map_err(TransactionError::from)?;
let debug_result = match execution_result {
ExecutionResult::Success {
gas_used, output, ..
} => DebugTraceResult {
pass: true,
gas_used,
output: Some(output.into_data()),
logs: tracer.logs,
},
ExecutionResult::Revert { gas_used, output } => DebugTraceResult {
pass: false,
gas_used,
output: Some(output),
logs: tracer.logs,
},
ExecutionResult::Halt { gas_used, .. } => DebugTraceResult {
pass: false,
gas_used,
output: None,
logs: tracer.logs,
},
};

return Ok(debug_result);

return Ok(execution_result_to_debug_result(execution_result, tracer));
} else {
let evm = build_evm(
blockchain,
Expand All @@ -101,6 +79,35 @@ where
})
}

/// Convert an `ExecutionResult` to a `DebugTraceResult`.
pub fn execution_result_to_debug_result(
execution_result: ExecutionResult,
tracer: TracerEip3155,
) -> DebugTraceResult {
match execution_result {
ExecutionResult::Success {
gas_used, output, ..
} => DebugTraceResult {
pass: true,
gas_used,
output: Some(output.into_data()),
logs: tracer.logs,
},
ExecutionResult::Revert { gas_used, output } => DebugTraceResult {
pass: false,
gas_used,
output: Some(output),
logs: tracer.logs,
},
ExecutionResult::Halt { gas_used, .. } => DebugTraceResult {
pass: false,
gas_used,
output: None,
logs: tracer.logs,
},
}
}

/// Config options for `debug_trace_transaction`
#[derive(Debug, Default, Clone)]
pub struct DebugTraceConfig {
Expand Down Expand Up @@ -172,9 +179,11 @@ pub struct DebugTraceLogItem {
pub storage: Option<HashMap<String, String>>,
}

// Based on https://github.com/bluealloy/revm/blob/70cf969a25a45e3bb4e503926297d61a90c7eec5/crates/revm/src/inspector/tracer_eip3155.rs
// Original licensed under the MIT license.
struct TracerEip3155 {
/// An EIP-3155 compatible EVM tracer.
/// Based on [REVM TracerEip3155](https://github.com/bluealloy/revm/blob/70cf969a25a45e3bb4e503926297d61a90c7eec5/crates/revm/src/inspector/tracer_eip3155.rs).
/// Original licensed under the MIT license.
#[derive(Debug)]
pub struct TracerEip3155 {
config: DebugTraceConfig,

logs: Vec<DebugTraceLogItem>,
Expand All @@ -194,7 +203,8 @@ struct TracerEip3155 {
}

impl TracerEip3155 {
fn new(config: DebugTraceConfig) -> Self {
/// Create a new tracer.
pub fn new(config: DebugTraceConfig) -> Self {
Self {
config,
logs: Vec::default(),
Expand Down
5 changes: 4 additions & 1 deletion crates/rethnet_evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ pub use revm::{

pub use crate::{
block::*,
debug_trace::{debug_trace_transaction, DebugTraceConfig, DebugTraceLogItem, DebugTraceResult},
debug_trace::{
debug_trace_transaction, execution_result_to_debug_result, DebugTraceConfig,
DebugTraceLogItem, DebugTraceResult, TracerEip3155,
},
evm::SyncInspector,
mempool::{MemPool, OrderedTransaction},
miner::*,
Expand Down
43 changes: 40 additions & 3 deletions crates/rethnet_evm_napi/src/debug_trace.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use napi::bindgen_prelude::{BigInt, Buffer};
use napi::Status;
use napi_derive::napi;
use rethnet_eth::B256;
use rethnet_evm::{BlockEnv, CfgEnv};
use std::collections::HashMap;

use crate::transaction::PendingTransaction;
use rethnet_eth::B256;
use rethnet_evm::{
execution_result_to_debug_result, BlockEnv, CfgEnv, ResultAndState, TracerEip3155, TxEnv,
};

use crate::transaction::{PendingTransaction, TransactionRequest};
use crate::{
block::BlockConfig, blockchain::Blockchain, cast::TryCast, config::ConfigOptions, state::State,
};
Expand Down Expand Up @@ -46,6 +49,40 @@ pub async fn debug_trace_transaction(
Ok(result)
}

/// Get trace output for `debug_traceTransaction`
#[napi]
#[cfg_attr(feature = "tracing", tracing::instrument(skip_all))]
pub async fn debug_trace_call(
blockchain: &Blockchain,
state_manager: &State,
evm_config: ConfigOptions,
trace_config: DebugTraceConfig,
block_config: BlockConfig,
transaction: TransactionRequest,
) -> napi::Result<DebugTraceResult> {
let evm_config = CfgEnv::try_from(evm_config)?;
let block = BlockEnv::try_from(block_config)?;
let transaction = TxEnv::try_from(transaction)?;

let mut tracer = TracerEip3155::new(trace_config.into());

let ResultAndState {
result: execution_result,
..
} = rethnet_evm::guaranteed_dry_run(
&*blockchain.read().await,
&*state_manager.read().await,
evm_config,
transaction,
block,
Some(&mut tracer),
)
.await
.map_err(|e| napi::Error::new(Status::GenericFailure, e.to_string()))?;

execution_result_to_debug_result(execution_result, tracer).try_into()
}

#[napi(object)]
pub struct DebugTraceConfig {
pub disable_storage: Option<bool>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,9 @@ export class DualModeAdapter implements VMAdapter {
block: Block,
config: RpcDebugTracingConfig
): Promise<RpcDebugTraceOutput> {
return this._ethereumJSAdapter.traceCall(tx, block, config);
// We aren't comparing the result as the output is expected to differ.
const _ = await this._ethereumJSAdapter.traceCall(tx, block, config);
return this._rethnetAdapter.traceCall(tx, block, config);
}

public async setBlockContext(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
SpecId,
guaranteedDryRun,
debugTraceTransaction,
debugTraceCall,
run,
ConfigOptions,
State,
Expand Down Expand Up @@ -453,10 +454,49 @@ export class RethnetAdapter implements VMAdapter {

public async traceCall(
tx: TypedTransaction,
block: Block,
config: RpcDebugTracingConfig
blockContext: Block,
traceConfig: RpcDebugTracingConfig
): Promise<RpcDebugTraceOutput> {
throw new Error("traceCall not implemented for Rethnet");
const rethnetTx = ethereumjsTransactionToRethnetTransactionRequest(tx);

const difficulty = this._getBlockEnvDifficulty(
blockContext.header.difficulty
);

const prevRandao = await this._getBlockPrevRandao(
blockContext.header.number,
blockContext.header.mixHash
);

const specId = await this._blockchain.specAtBlockNumber(
blockContext.header.number
);
const evmConfig: ConfigOptions = {
chainId: this._common.chainId(),
specId,
limitContractCodeSize: this._limitContractCodeSize,
disableBlockGasLimit: true,
disableEip3607: true,
};

const result = await debugTraceCall(
this._blockchain,
this._state.asInner(),
evmConfig,
hardhatDebugTraceConfigToRethnet(traceConfig),
{
number: blockContext.header.number,
beneficiary: blockContext.header.coinbase.buf,
timestamp: blockContext.header.timestamp,
baseFee: 0n,
gasLimit: blockContext.header.gasLimit,
difficulty,
mixHash: prevRandao,
},
rethnetTx
);

return rethnetRpcDebugTraceToHardhat(result);
}

public async makeSnapshot(): Promise<Buffer> {
Expand Down
80 changes: 40 additions & 40 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1393,55 +1393,55 @@
mcl-wasm "^0.7.1"
rustbn.js "~0.2.0"

"@nomicfoundation/[email protected].0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-darwin-arm64/-/solidity-analyzer-darwin-arm64-0.1.0.tgz#83a7367342bd053a76d04bbcf4f373fef07cf760"
integrity sha512-vEF3yKuuzfMHsZecHQcnkUrqm8mnTWfJeEVFHpg+cO+le96xQA4lAJYdUan8pXZohQxv1fSReQsn4QGNuBNuCw==
"@nomicfoundation/[email protected].1":
version "0.1.1"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-darwin-arm64/-/solidity-analyzer-darwin-arm64-0.1.1.tgz#4c858096b1c17fe58a474fe81b46815f93645c15"
integrity sha512-KcTodaQw8ivDZyF+D76FokN/HdpgGpfjc/gFCImdLUyqB6eSWVaZPazMbeAjmfhx3R0zm/NYVzxwAokFKgrc0w==

"@nomicfoundation/[email protected].0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-darwin-x64/-/solidity-analyzer-darwin-x64-0.1.0.tgz#1225f7da647ae1ad25a87125664704ecc0af6ccc"
integrity sha512-dlHeIg0pTL4dB1l9JDwbi/JG6dHQaU1xpDK+ugYO8eJ1kxx9Dh2isEUtA4d02cQAl22cjOHTvifAk96A+ItEHA==
"@nomicfoundation/[email protected].1":
version "0.1.1"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-darwin-x64/-/solidity-analyzer-darwin-x64-0.1.1.tgz#6e25ccdf6e2d22389c35553b64fe6f3fdaec432c"
integrity sha512-XhQG4BaJE6cIbjAVtzGOGbK3sn1BO9W29uhk9J8y8fZF1DYz0Doj8QDMfpMu+A6TjPDs61lbsmeYodIDnfveSA==

"@nomicfoundation/[email protected].0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-freebsd-x64/-/solidity-analyzer-freebsd-x64-0.1.0.tgz#dbc052dcdfd50ae50fd5ae1788b69b4e0fa40040"
integrity sha512-WFCZYMv86WowDA4GiJKnebMQRt3kCcFqHeIomW6NMyqiKqhK1kIZCxSLDYsxqlx396kKLPN1713Q1S8tu68GKg==
"@nomicfoundation/[email protected].1":
version "0.1.1"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-freebsd-x64/-/solidity-analyzer-freebsd-x64-0.1.1.tgz#0a224ea50317139caeebcdedd435c28a039d169c"
integrity sha512-GHF1VKRdHW3G8CndkwdaeLkVBi5A9u2jwtlS7SLhBc8b5U/GcoL39Q+1CSO3hYqePNP+eV5YI7Zgm0ea6kMHoA==

"@nomicfoundation/[email protected].0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-linux-arm64-gnu/-/solidity-analyzer-linux-arm64-gnu-0.1.0.tgz#e6b2eea633995b557e74e881d2a43eab4760903d"
integrity sha512-DTw6MNQWWlCgc71Pq7CEhEqkb7fZnS7oly13pujs4cMH1sR0JzNk90Mp1zpSCsCs4oKan2ClhMlLKtNat/XRKQ==
"@nomicfoundation/[email protected].1":
version "0.1.1"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-linux-arm64-gnu/-/solidity-analyzer-linux-arm64-gnu-0.1.1.tgz#dfa085d9ffab9efb2e7b383aed3f557f7687ac2b"
integrity sha512-g4Cv2fO37ZsUENQ2vwPnZc2zRenHyAxHcyBjKcjaSmmkKrFr64yvzeNO8S3GBFCo90rfochLs99wFVGT/0owpg==

"@nomicfoundation/[email protected].0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-linux-arm64-musl/-/solidity-analyzer-linux-arm64-musl-0.1.0.tgz#af81107f5afa794f19988a368647727806e18dc4"
integrity sha512-wUpUnR/3GV5Da88MhrxXh/lhb9kxh9V3Jya2NpBEhKDIRCDmtXMSqPMXHZmOR9DfCwCvG6vLFPr/+YrPCnUN0w==
"@nomicfoundation/[email protected].1":
version "0.1.1"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-linux-arm64-musl/-/solidity-analyzer-linux-arm64-musl-0.1.1.tgz#c9e06b5d513dd3ab02a7ac069c160051675889a4"
integrity sha512-WJ3CE5Oek25OGE3WwzK7oaopY8xMw9Lhb0mlYuJl/maZVo+WtP36XoQTb7bW/i8aAdHW5Z+BqrHMux23pvxG3w==

"@nomicfoundation/[email protected].0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-linux-x64-gnu/-/solidity-analyzer-linux-x64-gnu-0.1.0.tgz#6877e1da1a06a9f08446070ab6e0a5347109f868"
integrity sha512-lR0AxK1x/MeKQ/3Pt923kPvwigmGX3OxeU5qNtQ9pj9iucgk4PzhbS3ruUeSpYhUxG50jN4RkIGwUMoev5lguw==
"@nomicfoundation/[email protected].1":
version "0.1.1"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-linux-x64-gnu/-/solidity-analyzer-linux-x64-gnu-0.1.1.tgz#8d328d16839e52571f72f2998c81e46bf320f893"
integrity sha512-5WN7leSr5fkUBBjE4f3wKENUy9HQStu7HmWqbtknfXkkil+eNWiBV275IOlpXku7v3uLsXTOKpnnGHJYI2qsdA==

"@nomicfoundation/[email protected].0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-linux-x64-musl/-/solidity-analyzer-linux-x64-musl-0.1.0.tgz#bb6cd83a0c259eccef4183796b6329a66cf7ebd9"
integrity sha512-A1he/8gy/JeBD3FKvmI6WUJrGrI5uWJNr5Xb9WdV+DK0F8msuOqpEByLlnTdLkXMwW7nSl3awvLezOs9xBHJEg==
"@nomicfoundation/[email protected].1":
version "0.1.1"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-linux-x64-musl/-/solidity-analyzer-linux-x64-musl-0.1.1.tgz#9b49d0634b5976bb5ed1604a1e1b736f390959bb"
integrity sha512-KdYMkJOq0SYPQMmErv/63CwGwMm5XHenEna9X9aB8mQmhDBrYrlAOSsIPgFCUSL0hjxE3xHP65/EPXR/InD2+w==

"@nomicfoundation/[email protected].0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-win32-arm64-msvc/-/solidity-analyzer-win32-arm64-msvc-0.1.0.tgz#9d4bca1cc9a1333fde985675083b0b7d165f6076"
integrity sha512-7x5SXZ9R9H4SluJZZP8XPN+ju7Mx+XeUMWZw7ZAqkdhP5mK19I4vz3x0zIWygmfE8RT7uQ5xMap0/9NPsO+ykw==
"@nomicfoundation/[email protected].1":
version "0.1.1"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-win32-arm64-msvc/-/solidity-analyzer-win32-arm64-msvc-0.1.1.tgz#e2867af7264ebbcc3131ef837878955dd6a3676f"
integrity sha512-VFZASBfl4qiBYwW5xeY20exWhmv6ww9sWu/krWSesv3q5hA0o1JuzmPHR4LPN6SUZj5vcqci0O6JOL8BPw+APg==

"@nomicfoundation/[email protected].0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-win32-ia32-msvc/-/solidity-analyzer-win32-ia32-msvc-0.1.0.tgz#0db5bfc6aa952bea4098d8d2c8947b4e5c4337ee"
integrity sha512-m7w3xf+hnE774YRXu+2mGV7RiF3QJtUoiYU61FascCkQhX3QMQavh7saH/vzb2jN5D24nT/jwvaHYX/MAM9zUw==
"@nomicfoundation/[email protected].1":
version "0.1.1"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-win32-ia32-msvc/-/solidity-analyzer-win32-ia32-msvc-0.1.1.tgz#0685f78608dd516c8cdfb4896ed451317e559585"
integrity sha512-JnFkYuyCSA70j6Si6cS1A9Gh1aHTEb8kOTBApp/c7NRTFGNMH8eaInKlyuuiIbvYFhlXW4LicqyYuWNNq9hkpQ==

"@nomicfoundation/[email protected].0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-win32-x64-msvc/-/solidity-analyzer-win32-x64-msvc-0.1.0.tgz#2e0f39a2924dcd77db6b419828595e984fabcb33"
integrity sha512-xCuybjY0sLJQnJhupiFAXaek2EqF0AP0eBjgzaalPXSNvCEN6ZYHvUzdA50ENDVeSYFXcUsYf3+FsD3XKaeptA==
"@nomicfoundation/[email protected].1":
version "0.1.1"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-win32-x64-msvc/-/solidity-analyzer-win32-x64-msvc-0.1.1.tgz#c9a44f7108646f083b82e851486e0f6aeb785836"
integrity sha512-HrVJr6+WjIXGnw3Q9u6KQcbZCtk0caVWhCdFADySvRyUxJ8PnzlaP+MhwNE8oyT8OZ6ejHBRrrgjSqDCFXGirw==

"@nomicfoundation/solidity-analyzer@^0.1.0":
version "0.1.1"
Expand Down

0 comments on commit 59c4f67

Please sign in to comment.