Skip to content

Commit e3d0d15

Browse files
authored
chore: better ContractsByArtifact flatten (#7107)
1 parent 82f2a26 commit e3d0d15

File tree

2 files changed

+30
-32
lines changed

2 files changed

+30
-32
lines changed

crates/common/src/contracts.rs

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Commonly used contract types and functions.
22
33
use alloy_json_abi::{Event, Function, JsonAbi};
4-
use alloy_primitives::{hex, Address, B256};
4+
use alloy_primitives::{hex, Address, Selector, B256};
55
use foundry_compilers::{
66
artifacts::{CompactContractBytecode, ContractBytecodeSome},
77
ArtifactId, ProjectPathsConfig,
@@ -43,36 +43,34 @@ impl ContractsByArtifact {
4343
Ok(contracts.first().cloned())
4444
}
4545

46-
/// Flattens a group of contracts into maps of all events and functions
47-
pub fn flatten(&self) -> (BTreeMap<[u8; 4], Function>, BTreeMap<B256, Event>, JsonAbi) {
48-
let flattened_funcs: BTreeMap<[u8; 4], Function> = self
49-
.iter()
50-
.flat_map(|(_name, (abi, _code))| {
51-
abi.functions()
52-
.map(|func| (func.selector().into(), func.clone()))
53-
.collect::<BTreeMap<[u8; 4], Function>>()
54-
})
55-
.collect();
46+
/// Flattens the contracts into functions, events and errors.
47+
pub fn flatten(&self) -> (BTreeMap<Selector, Function>, BTreeMap<B256, Event>, JsonAbi) {
48+
let mut funcs = BTreeMap::new();
49+
let mut events = BTreeMap::new();
50+
let mut errors_abi = JsonAbi::new();
51+
for (_name, (abi, _code)) in self.iter() {
52+
for func in abi.functions() {
53+
funcs.insert(func.selector(), func.clone());
54+
}
55+
for event in abi.events() {
56+
events.insert(event.selector(), event.clone());
57+
}
58+
for error in abi.errors() {
59+
errors_abi.errors.entry(error.name.clone()).or_default().push(error.clone());
60+
}
61+
}
62+
(funcs, events, errors_abi)
63+
}
5664

57-
let flattened_events: BTreeMap<B256, Event> = self
58-
.iter()
59-
.flat_map(|(_name, (abi, _code))| {
60-
abi.events()
61-
.map(|event| (event.selector(), event.clone()))
62-
.collect::<BTreeMap<B256, Event>>()
63-
})
64-
.collect();
65-
66-
// We need this for better revert decoding, and want it in abi form
67-
let mut errors_abi = JsonAbi::default();
68-
self.iter().for_each(|(_name, (abi, _code))| {
69-
abi.errors().for_each(|error| {
70-
let entry =
71-
errors_abi.errors.entry(error.name.clone()).or_insert_with(Default::default);
72-
entry.push(error.clone());
73-
});
74-
});
75-
(flattened_funcs, flattened_events, errors_abi)
65+
/// Flattens the errors into a single JsonAbi.
66+
pub fn flatten_errors(&self) -> JsonAbi {
67+
let mut errors_abi = JsonAbi::new();
68+
for (_name, (abi, _code)) in self.iter() {
69+
for error in abi.errors() {
70+
errors_abi.errors.entry(error.name.clone()).or_default().push(error.clone());
71+
}
72+
}
73+
errors_abi
7674
}
7775
}
7876

crates/forge/src/multi_runner.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,15 +316,15 @@ impl MultiContractRunnerBuilder {
316316
);
317317
}
318318

319-
let execution_info = known_contracts.flatten();
319+
let errors = known_contracts.flatten_errors();
320320
Ok(MultiContractRunner {
321321
contracts: deployable_contracts,
322322
known_contracts,
323323
evm_opts,
324324
env,
325325
evm_spec: self.evm_spec.unwrap_or(SpecId::MERGE),
326326
sender: self.sender,
327-
errors: Some(execution_info.2),
327+
errors: Some(errors),
328328
source_paths,
329329
fork: self.fork,
330330
cheats_config: self.cheats_config.unwrap_or_default().into(),

0 commit comments

Comments
 (0)