Skip to content
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
57 changes: 57 additions & 0 deletions src/artifacts/output_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,20 @@ impl OutputSelection {
pub fn empty_file_output_select() -> FileOutputSelection {
Default::default()
}

/// Returns true if this output selection is a subset of the other output selection.
/// TODO: correctly process wildcard keys to reduce false negatives
pub fn is_subset_of(&self, other: &Self) -> bool {
self.0.iter().all(|(file, selection)| {
other.0.get(file).map_or(false, |other_selection| {
selection.iter().all(|(contract, outputs)| {
other_selection.get(contract).map_or(false, |other_outputs| {
outputs.iter().all(|output| other_outputs.contains(output))
})
})
})
})
}
}

// this will make sure that if the `FileOutputSelection` for a certain file is empty will be
Expand Down Expand Up @@ -584,6 +598,49 @@ mod tests {
assert_eq!(s, r#"{"contract.sol":{"*":[]}}"#);
}

#[test]
fn outputselection_subset_of() {
let output_selection = OutputSelection::from(BTreeMap::from([(
"*".to_string(),
BTreeMap::from([(
"*".to_string(),
vec!["abi".to_string(), "evm.bytecode".to_string()],
)]),
)]));

let output_selection_abi = OutputSelection::from(BTreeMap::from([(
"*".to_string(),
BTreeMap::from([("*".to_string(), vec!["abi".to_string()])]),
)]));

assert!(output_selection_abi.is_subset_of(&output_selection));
assert!(!output_selection.is_subset_of(&output_selection_abi));

let output_selection_empty = OutputSelection::from(BTreeMap::from([(
"*".to_string(),
BTreeMap::from([("*".to_string(), vec![])]),
)]));

assert!(output_selection_empty.is_subset_of(&output_selection));
assert!(output_selection_empty.is_subset_of(&output_selection_abi));
assert!(!output_selection.is_subset_of(&output_selection_empty));
assert!(!output_selection_abi.is_subset_of(&output_selection_empty));

let output_selecttion_specific = OutputSelection::from(BTreeMap::from([(
"Contract.sol".to_string(),
BTreeMap::from([(
"Contract".to_string(),
vec![
"abi".to_string(),
"evm.bytecode".to_string(),
"evm.deployedBytecode".to_string(),
],
)]),
)]));

assert!(!output_selecttion_specific.is_subset_of(&output_selection));
}

#[test]
fn deployed_bytecode_from_str() {
assert_eq!(
Expand Down
4 changes: 2 additions & 2 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -776,8 +776,8 @@ impl<'a, T: ArtifactOutput> ArtifactsCacheInner<'a, T> {
return true;
}

if self.project.solc_config != entry.solc_config {
trace!("solc config changed");
if !self.project.solc_config.can_use_cached(&entry.solc_config) {
trace!("solc config not compatible");
return true;
}

Expand Down
32 changes: 32 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,38 @@ impl SolcConfig {
pub fn builder() -> SolcConfigBuilder {
SolcConfigBuilder::default()
}

/// Returns true if artifacts compiled with given `cached` config are compatible with this
/// config and if compilation can be skipped.
///
/// Ensures that all settings fields are equal except for `output_selection` which is required
/// to be a subset of `cached.output_selection`.
pub fn can_use_cached(&self, cached: &SolcConfig) -> bool {
let SolcConfig { settings } = self;
let Settings {
stop_after,
remappings,
optimizer,
model_checker,
metadata,
output_selection,
evm_version,
via_ir,
debug,
libraries,
} = settings;

*stop_after == cached.settings.stop_after
&& *remappings == cached.settings.remappings
&& *optimizer == cached.settings.optimizer
&& *model_checker == cached.settings.model_checker
&& *metadata == cached.settings.metadata
&& *evm_version == cached.settings.evm_version
&& *via_ir == cached.settings.via_ir
&& *debug == cached.settings.debug
&& *libraries == cached.settings.libraries
&& output_selection.is_subset_of(&cached.settings.output_selection)
}
}

impl From<SolcConfig> for Settings {
Expand Down