Skip to content

Commit

Permalink
chore: better ContractsByArtifact flatten
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Feb 13, 2024
1 parent fd87629 commit ff4acfe
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 32 deletions.
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 @@ -318,15 +318,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

0 comments on commit ff4acfe

Please sign in to comment.