Skip to content

Commit

Permalink
feat: stop supporting legacy console.sol signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Sep 19, 2024
1 parent da77402 commit 7d72887
Show file tree
Hide file tree
Showing 17 changed files with 157 additions and 932 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

4 changes: 2 additions & 2 deletions crates/cheatcodes/src/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ pub trait CheatcodesExecutor {
})
}

fn console_log<DB: DatabaseExt>(&mut self, ccx: &mut CheatsCtxt<DB>, message: String) {
self.get_inspector::<DB>(ccx.state).console_log(message);
fn console_log<DB: DatabaseExt>(&mut self, ccx: &mut CheatsCtxt<DB>, msg: &str) {
self.get_inspector::<DB>(ccx.state).console_log(msg);
}

/// Returns a mutable reference to the tracing inspector if it is available.
Expand Down
8 changes: 4 additions & 4 deletions crates/cheatcodes/src/test/assert.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{CheatcodesExecutor, CheatsCtxt, Result, Vm::*};
use alloy_primitives::{hex, I256, U256};
use foundry_evm_core::{
abi::{format_units_int, format_units_uint},
abi::console::{format_units_int, format_units_uint},
backend::{DatabaseExt, GLOBAL_FAIL_SLOT},
constants::CHEATCODE_ADDRESS,
};
Expand Down Expand Up @@ -180,16 +180,16 @@ fn handle_assertion_result<DB: DatabaseExt, E: CheatcodesExecutor, ERR>(
match result {
Ok(_) => Ok(Default::default()),
Err(err) => {
let error_msg = error_msg.unwrap_or("assertion failed").to_string();
let error_msg = error_msg.unwrap_or("assertion failed");
let msg = if format_error {
format!("{error_msg}: {}", error_formatter(&err))
} else {
error_msg
error_msg.to_string()
};
if ccx.state.config.assertions_revert {
Err(msg.into())
} else {
executor.console_log(ccx, msg);
executor.console_log(ccx, &msg);
ccx.ecx.sstore(CHEATCODE_ADDRESS, GLOBAL_FAIL_SLOT, U256::from(1))?;
Ok(Default::default())
}
Expand Down
1 change: 0 additions & 1 deletion crates/evm/abi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ alloy-sol-types = { workspace = true, features = ["json"] }

derive_more.workspace = true
itertools.workspace = true
rustc-hash.workspace = true

[dev-dependencies]
foundry-test-utils.workspace = true
File renamed without changes.
40 changes: 4 additions & 36 deletions crates/evm/abi/src/console.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python3
# Generates the JSON ABI for console.sol.

import json
import re
Expand All @@ -7,12 +8,10 @@


def main():
if len(sys.argv) < 4:
print(
f"Usage: {sys.argv[0]} <console.sol> <HardhatConsole.abi.json> <patches.rs>"
)
if len(sys.argv) != 3:
print(f"Usage: {sys.argv[0]} <console.sol> <Console.json>")
sys.exit(1)
[console_file, abi_file, patches_file] = sys.argv[1:4]
[console_file, abi_file] = sys.argv[1:3]

# Parse signatures from `console.sol`'s string literals
console_sol = open(console_file).read()
Expand Down Expand Up @@ -41,37 +40,6 @@ def main():
abi = combined["contracts"]["<stdin>:HardhatConsole"]["abi"]
open(abi_file, "w").write(json.dumps(abi, separators=(",", ":"), indent=None))

# Make patches
patches = []
for raw_sig in raw_sigs:
patched = raw_sig.replace("int", "int256")
if raw_sig != patched:
patches.append([raw_sig, patched])

# Generate the Rust patches map
codegen = "[\n"
for [original, patched] in patches:
codegen += f" // `{original}` -> `{patched}`\n"

original_selector = selector(original)
patched_selector = selector(patched)
codegen += f" // `{original_selector.hex()}` -> `{patched_selector.hex()}`\n"

codegen += (
f" ({list(iter(original_selector))}, {list(iter(patched_selector))}),\n"
)
codegen += "]\n"
open(patches_file, "w").write(codegen)


def keccak256(s):
r = subprocess.run(["cast", "keccak256", s], capture_output=True)
return bytes.fromhex(r.stdout.decode("utf8").strip()[2:])


def selector(s):
return keccak256(s)[:4]


if __name__ == "__main__":
main()
83 changes: 83 additions & 0 deletions crates/evm/abi/src/console/ds.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//! DSTest log interface.

use super::{format_units_int, format_units_uint};
use alloy_primitives::hex;
use alloy_sol_types::sol;
use derive_more::Display;
use itertools::Itertools;

// TODO: Use `UiFmt`

sol! {
#[sol(abi)]
#[derive(Display)]
interface Console {
#[display("{val}")]
event log(string val);

#[display("{}", hex::encode_prefixed(val))]
event logs(bytes val);

#[display("{val}")]
event log_address(address val);

#[display("{val}")]
event log_bytes32(bytes32 val);

#[display("{val}")]
event log_int(int val);

#[display("{val}")]
event log_uint(uint val);

#[display("{}", hex::encode_prefixed(val))]
event log_bytes(bytes val);

#[display("{val}")]
event log_string(string val);

#[display("[{}]", val.iter().format(", "))]
event log_array(uint256[] val);

#[display("[{}]", val.iter().format(", "))]
event log_array(int256[] val);

#[display("[{}]", val.iter().format(", "))]
event log_array(address[] val);

#[display("{key}: {val}")]
event log_named_address(string key, address val);

#[display("{key}: {val}")]
event log_named_bytes32(string key, bytes32 val);

#[display("{key}: {}", format_units_int(val, decimals))]
event log_named_decimal_int(string key, int val, uint decimals);

#[display("{key}: {}", format_units_uint(val, decimals))]
event log_named_decimal_uint(string key, uint val, uint decimals);

#[display("{key}: {val}")]
event log_named_int(string key, int val);

#[display("{key}: {val}")]
event log_named_uint(string key, uint val);

#[display("{key}: {}", hex::encode_prefixed(val))]
event log_named_bytes(string key, bytes val);

#[display("{key}: {val}")]
event log_named_string(string key, string val);

#[display("{key}: [{}]", val.iter().format(", "))]
event log_named_array(string key, uint256[] val);

#[display("{key}: [{}]", val.iter().format(", "))]
event log_named_array(string key, int256[] val);

#[display("{key}: [{}]", val.iter().format(", "))]
event log_named_array(string key, address[] val);
}
}

pub use Console::*;
57 changes: 0 additions & 57 deletions crates/evm/abi/src/console/hardhat.rs

This file was deleted.

14 changes: 14 additions & 0 deletions crates/evm/abi/src/console/hh.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//! Hardhat `console.sol` interface.

use alloy_sol_types::sol;
use foundry_common_fmt::*;
use foundry_macros::ConsoleFmt;

sol!(
#[sol(abi)]
#[derive(ConsoleFmt)]
Console,
"src/Console.json"
);

pub use Console::*;
83 changes: 3 additions & 80 deletions crates/evm/abi/src/console/mod.rs
Original file line number Diff line number Diff line change
@@ -1,84 +1,7 @@
use alloy_primitives::{hex, I256, U256};
use alloy_sol_types::sol;
use derive_more::Display;
use itertools::Itertools;
use alloy_primitives::{I256, U256};

mod hardhat;
pub use hardhat::*;

// TODO: Use `UiFmt`

sol! {
#[sol(abi)]
#[derive(Display)]
interface Console {
#[display("{val}")]
event log(string val);

#[display("{}", hex::encode_prefixed(val))]
event logs(bytes val);

#[display("{val}")]
event log_address(address val);

#[display("{val}")]
event log_bytes32(bytes32 val);

#[display("{val}")]
event log_int(int val);

#[display("{val}")]
event log_uint(uint val);

#[display("{}", hex::encode_prefixed(val))]
event log_bytes(bytes val);

#[display("{val}")]
event log_string(string val);

#[display("[{}]", val.iter().format(", "))]
event log_array(uint256[] val);

#[display("[{}]", val.iter().format(", "))]
event log_array(int256[] val);

#[display("[{}]", val.iter().format(", "))]
event log_array(address[] val);

#[display("{key}: {val}")]
event log_named_address(string key, address val);

#[display("{key}: {val}")]
event log_named_bytes32(string key, bytes32 val);

#[display("{key}: {}", format_units_int(val, decimals))]
event log_named_decimal_int(string key, int val, uint decimals);

#[display("{key}: {}", format_units_uint(val, decimals))]
event log_named_decimal_uint(string key, uint val, uint decimals);

#[display("{key}: {val}")]
event log_named_int(string key, int val);

#[display("{key}: {val}")]
event log_named_uint(string key, uint val);

#[display("{key}: {}", hex::encode_prefixed(val))]
event log_named_bytes(string key, bytes val);

#[display("{key}: {val}")]
event log_named_string(string key, string val);

#[display("{key}: [{}]", val.iter().format(", "))]
event log_named_array(string key, uint256[] val);

#[display("{key}: [{}]", val.iter().format(", "))]
event log_named_array(string key, int256[] val);

#[display("{key}: [{}]", val.iter().format(", "))]
event log_named_array(string key, address[] val);
}
}
pub mod ds;
pub mod hh;

pub fn format_units_int(x: &I256, decimals: &U256) -> String {
let (sign, x) = x.into_sign_and_abs();
Expand Down
Loading

0 comments on commit 7d72887

Please sign in to comment.