Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Export data via cheatcode #5525

Closed
wants to merge 13 commits into from
681 changes: 440 additions & 241 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions abi/abi/HEVM.sol
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ serializeString(string,string,string[])(string)
serializeBytes(string,string,bytes)(string)
serializeBytes(string,string,bytes[])(string)
keyExists(string,string)(bool)
export(string, string)

pauseGasMetering()
resumeGasMetering()
Expand Down
58 changes: 58 additions & 0 deletions abi/src/bindings/hevm.rs

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

7 changes: 7 additions & 0 deletions evm/src/executor/inspector/cheatcodes/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,12 @@ fn key_exists(json_str: &str, key: &str) -> Result {
Ok(exists)
}

/// Export data for external consumption
fn export(state: &mut Cheatcodes, key: &str, value: &str) -> Result {
state.raw_exported_data.insert(key.to_string(), value.to_string());
Ok(Bytes::new())
}

#[instrument(level = "error", name = "ext", target = "evm::cheatcodes", skip_all)]
pub fn apply(state: &mut Cheatcodes, call: &HEVMCalls) -> Option<Result> {
Some(match call {
Expand Down Expand Up @@ -593,6 +599,7 @@ pub fn apply(state: &mut Cheatcodes, call: &HEVMCalls) -> Option<Result> {
HEVMCalls::WriteJson0(inner) => write_json(state, &inner.0, &inner.1, None),
HEVMCalls::WriteJson1(inner) => write_json(state, &inner.0, &inner.1, Some(&inner.2)),
HEVMCalls::KeyExists(inner) => key_exists(&inner.0, &inner.1),
HEVMCalls::Export(inner) => export(state, &inner.0, &inner.1),
_ => return None,
})
}
Expand Down
6 changes: 4 additions & 2 deletions evm/src/executor/inspector/cheatcodes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use self::{
use crate::{
abi::HEVMCalls,
executor::{
backend::DatabaseExt, inspector::cheatcodes::env::RecordedLogs, CHEATCODE_ADDRESS,
HARDHAT_CONSOLE_ADDRESS,
backend::DatabaseExt, inspector::cheatcodes::env::RecordedLogs, RawExportedData,
CHEATCODE_ADDRESS, HARDHAT_CONSOLE_ADDRESS,
},
utils::{b160_to_h160, b256_to_h256, h160_to_b160, ru256_to_u256},
};
Expand Down Expand Up @@ -194,6 +194,8 @@ pub struct Cheatcodes {
/// Breakpoints supplied by the `vm.breakpoint("<char>")` cheatcode
/// char -> pc
pub breakpoints: Breakpoints,

pub raw_exported_data: RawExportedData,
}

impl Cheatcodes {
Expand Down
6 changes: 6 additions & 0 deletions evm/src/executor/inspector/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub struct InspectorData {
pub cheatcodes: Option<Cheatcodes>,
pub script_wallets: Vec<LocalWallet>,
pub chisel_state: Option<(Stack, Memory, InstructionResult)>,
pub raw_exported_data: crate::executor::RawExportedData,
}

/// An inspector that calls multiple inspectors in sequence.
Expand Down Expand Up @@ -80,6 +81,11 @@ impl InspectorStack {
.as_ref()
.map(|cheatcodes| cheatcodes.script_wallets.clone())
.unwrap_or_default(),
raw_exported_data: self
.cheatcodes
.as_ref()
.map(|cheatcodes| cheatcodes.raw_exported_data.clone())
.unwrap_or_default(),
cheatcodes: self.cheatcodes,
chisel_state: self.chisel_state.unwrap_or_default().state,
}
Expand Down
Loading