fix(solc): do not overwrite existing cache (#629)

previously if 1 file changed, it'd overwrite the existing cache on disk with just
that 1 changed file. this change reads the cache from disk and merges it with the new checksums
This commit is contained in:
Georgios Konstantopoulos 2021-11-26 21:16:09 +02:00 committed by GitHub
parent e1476a1156
commit 7bb90935ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 5 deletions

1
Cargo.lock generated
View File

@ -1190,6 +1190,7 @@ dependencies = [
"tempdir",
"thiserror",
"tokio",
"tracing",
"walkdir",
]

View File

@ -8,7 +8,7 @@ use crate::{
use serde::{Deserialize, Serialize};
use std::{
collections::BTreeMap,
fs,
fs::{self, File},
path::{Path, PathBuf},
time::{Duration, UNIX_EPOCH},
};
@ -36,7 +36,7 @@ impl SolFilesCache {
/// use ethers_solc::artifacts::Source;
/// use ethers_solc::cache::SolFilesCache;
/// let files = Source::read_all_from("./sources").unwrap();
/// let config = SolFilesCache::builder().insert_files(files).unwrap();
/// let config = SolFilesCache::builder().insert_files(files, None).unwrap();
/// ```
pub fn builder() -> SolFilesCacheBuilder {
SolFilesCacheBuilder::default()
@ -200,7 +200,7 @@ impl SolFilesCacheBuilder {
self
}
pub fn insert_files(self, sources: Sources) -> Result<SolFilesCache> {
pub fn insert_files(self, sources: Sources, dest: Option<PathBuf>) -> Result<SolFilesCache> {
let format = self.format.unwrap_or_else(|| HH_FORMAT_VERSION.to_string());
let solc_config =
self.solc_config.map(Ok).unwrap_or_else(|| SolcConfig::builder().build())?;
@ -234,7 +234,23 @@ impl SolFilesCacheBuilder {
files.insert(file, entry);
}
Ok(SolFilesCache { format, files })
let cache = if let Some(ref dest) = dest {
if dest.exists() {
// read the existing cache and extend it by the files that changed
// (if we just wrote to the cache file, we'd overwrite the existing data)
let reader = std::io::BufReader::new(File::open(dest)?);
let mut cache: SolFilesCache = serde_json::from_reader(reader)?;
assert_eq!(cache.format, format);
cache.files.extend(files);
cache
} else {
SolFilesCache { format, files }
}
} else {
SolFilesCache { format, files }
};
Ok(cache)
}
}

View File

@ -98,7 +98,7 @@ impl<Artifacts: ArtifactOutput> Project<Artifacts> {
let mut cache = SolFilesCache::builder()
.root(&self.paths.root)
.solc_config(self.solc_config.clone())
.insert_files(sources)?;
.insert_files(sources, Some(self.paths.cache.clone()))?;
// add the artifacts for each file to the cache entry
for (file, artifacts) in artifacts {