Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
feat(solc): serialize the optimizer runs as well
Browse files Browse the repository at this point in the history
  • Loading branch information
gakonst committed Jan 1, 2022
1 parent 5d94ee6 commit 5d4c54d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
24 changes: 21 additions & 3 deletions ethers-solc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use crate::{
error::{SolcError, SolcIoError},
};
use error::Result;
use serde::{Deserialize, Serialize};
use std::{
borrow::Cow, collections::BTreeMap, convert::TryInto, fmt, fs, marker::PhantomData,
path::PathBuf,
Expand All @@ -45,6 +46,21 @@ pub mod project_util;
/// Name of the json file containing the compiler versions used per file
pub const SOLIDITY_VERSIONS: &str = "solidity-versions.json";

#[derive(Clone, Serialize, Deserialize)]
/// Deserialized version of artifacts file which contains the paths and solidity
/// versions for each artifact.
pub struct SourceVersions {
/// The optimizer runs used during compilation
// NB: Currently we compile all versions with the same [`Settings`] object.
// If that were to change, we'd need to move this filed inside the map, depending
// on how we determine which files get compiled with optimizations on and which not.
#[serde(skip_serializing_if = "Option::is_none")]
pub optimizer_runs: Option<usize>,
/// Mapping from compiler version string to a vector of all the files
/// that were compiled with that compiler
pub versions: BTreeMap<String, Vec<PathBuf>>,
}

/// Represents a project workspace and handles `solc` compiling of all contracts in that workspace.
#[derive(Debug)]
pub struct Project<Artifacts: ArtifactOutput = MinimalCombinedArtifacts> {
Expand Down Expand Up @@ -335,7 +351,7 @@ impl<Artifacts: ArtifactOutput> Project<Artifacts> {
/// Returns the `solidity-versions.json` file under the artifacts directory
/// which contains the Solidity compiler versions which were used to compile
/// each file.
pub fn source_versions(&self) -> Result<BTreeMap<String, Vec<PathBuf>>> {
pub fn source_versions(&self) -> Result<SourceVersions> {
let versions_file_path = self.artifacts_path().join(SOLIDITY_VERSIONS);
let versions_file = File::open(versions_file_path)?;
Ok(serde_json::from_reader(versions_file)?)
Expand All @@ -345,19 +361,21 @@ impl<Artifacts: ArtifactOutput> Project<Artifacts> {
&self,
sources_by_version: BTreeMap<Solc, BTreeMap<PathBuf, Source>>,
) -> Result<()> {
let runs = self.solc_config.settings.optimizer.runs;
let solidity_versions = sources_by_version
.iter()
.map(|(solc, sources)| {
let mut version = solc.version()?;
// skips the platform from the compiler version string
let trimmed = &version.build.as_str()[..15];
version.build = semver::BuildMetadata::new(trimmed)?;
Ok((format!("v{}", version), sources.keys().collect::<Vec<_>>()))
Ok((format!("v{}", version), sources.keys().cloned().collect::<Vec<_>>()))
})
.collect::<Result<BTreeMap<_, _>>>()?;
let sources_versions = SourceVersions { optimizer_runs: runs, versions: solidity_versions };
let versions_file_path = self.artifacts_path().join(SOLIDITY_VERSIONS);
let versions_file = File::create(versions_file_path)?;
serde_json::to_writer(versions_file, &solidity_versions)?;
serde_json::to_writer(versions_file, &sources_versions)?;
Ok(())
}

Expand Down
5 changes: 2 additions & 3 deletions ethers-solc/tests/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

use ethers_solc::{
cache::SOLIDITY_FILES_CACHE_FILENAME, project_util::*, MinimalCombinedArtifacts, Project,
ProjectPathsConfig, SOLIDITY_VERSIONS,
ProjectPathsConfig, SourceVersions, SOLIDITY_VERSIONS,
};
use std::{
collections::BTreeMap,
io,
path::{Path, PathBuf},
};
Expand Down Expand Up @@ -55,7 +54,7 @@ fn can_compile_dapp_sample() {

let solc_versions_path = project.project().artifacts_path().join(SOLIDITY_VERSIONS);
let reader = std::fs::File::open(solc_versions_path).unwrap();
let _: BTreeMap<String, Vec<PathBuf>> = serde_json::from_reader(reader).unwrap();
let _: SourceVersions = serde_json::from_reader(reader).unwrap();

// delete artifacts
std::fs::remove_dir_all(&project.paths().artifacts).unwrap();
Expand Down

0 comments on commit 5d4c54d

Please sign in to comment.