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

chore: better ContractsByArtifact flatten #7107

Merged
merged 1 commit into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 28 additions & 30 deletions crates/common/src/contracts.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Commonly used contract types and functions.

use alloy_json_abi::{Event, Function, JsonAbi};
use alloy_primitives::{hex, Address, B256};
use alloy_primitives::{hex, Address, Selector, B256};
use foundry_compilers::{
artifacts::{CompactContractBytecode, ContractBytecodeSome},
ArtifactId, ProjectPathsConfig,
Expand Down Expand Up @@ -43,36 +43,34 @@ impl ContractsByArtifact {
Ok(contracts.first().cloned())
}

/// Flattens a group of contracts into maps of all events and functions
pub fn flatten(&self) -> (BTreeMap<[u8; 4], Function>, BTreeMap<B256, Event>, JsonAbi) {
let flattened_funcs: BTreeMap<[u8; 4], Function> = self
.iter()
.flat_map(|(_name, (abi, _code))| {
abi.functions()
.map(|func| (func.selector().into(), func.clone()))
.collect::<BTreeMap<[u8; 4], Function>>()
})
.collect();
/// Flattens the contracts into functions, events and errors.
pub fn flatten(&self) -> (BTreeMap<Selector, Function>, BTreeMap<B256, Event>, JsonAbi) {
let mut funcs = BTreeMap::new();
let mut events = BTreeMap::new();
let mut errors_abi = JsonAbi::new();
for (_name, (abi, _code)) in self.iter() {
for func in abi.functions() {
funcs.insert(func.selector(), func.clone());
}
for event in abi.events() {
events.insert(event.selector(), event.clone());
}
for error in abi.errors() {
errors_abi.errors.entry(error.name.clone()).or_default().push(error.clone());
}
}
(funcs, events, errors_abi)
}

let flattened_events: BTreeMap<B256, Event> = self
.iter()
.flat_map(|(_name, (abi, _code))| {
abi.events()
.map(|event| (event.selector(), event.clone()))
.collect::<BTreeMap<B256, Event>>()
})
.collect();

// We need this for better revert decoding, and want it in abi form
let mut errors_abi = JsonAbi::default();
self.iter().for_each(|(_name, (abi, _code))| {
abi.errors().for_each(|error| {
let entry =
errors_abi.errors.entry(error.name.clone()).or_insert_with(Default::default);
entry.push(error.clone());
});
});
(flattened_funcs, flattened_events, errors_abi)
/// Flattens the errors into a single JsonAbi.
pub fn flatten_errors(&self) -> JsonAbi {
let mut errors_abi = JsonAbi::new();
for (_name, (abi, _code)) in self.iter() {
for error in abi.errors() {
errors_abi.errors.entry(error.name.clone()).or_default().push(error.clone());
}
}
errors_abi
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/forge/src/multi_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,15 +316,15 @@ impl MultiContractRunnerBuilder {
);
}

let execution_info = known_contracts.flatten();
let errors = known_contracts.flatten_errors();
Ok(MultiContractRunner {
contracts: deployable_contracts,
known_contracts,
evm_opts,
env,
evm_spec: self.evm_spec.unwrap_or(SpecId::MERGE),
sender: self.sender,
errors: Some(execution_info.2),
errors: Some(errors),
source_paths,
fork: self.fork,
cheats_config: self.cheats_config.unwrap_or_default().into(),
Expand Down
Loading