-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
lib.rs
488 lines (426 loc) · 16.8 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
use ethers::core::utils::{CompiledContract, Solc};
use eyre::Result;
use rayon::prelude::*;
use semver::{Version, VersionReq};
use std::{
collections::HashMap,
fs::File,
io::{BufRead, BufReader},
path::{Path, PathBuf},
time::Instant,
};
#[cfg(test)]
use std::sync::Mutex;
#[cfg(test)]
static LOCK: Lazy<Mutex<()>> = Lazy::new(|| Mutex::new(()));
#[cfg(test)]
use ethers::prelude::Lazy;
/// Supports building contracts
#[derive(Debug)]
pub struct SolcBuilder<'a> {
contracts: &'a str,
remappings: &'a [String],
lib_paths: &'a [String],
versions: Vec<Version>,
releases: Vec<Version>,
}
impl<'a> SolcBuilder<'a> {
pub fn new(
contracts: &'a str,
remappings: &'a [String],
lib_paths: &'a [String],
) -> Result<Self> {
let versions = svm::installed_versions().unwrap_or_default();
// Try to download the releases, if it fails default to empty
let releases = match tokio::runtime::Runtime::new()?.block_on(svm::all_versions()) {
Ok(inner) => inner,
Err(err) => {
tracing::error!("Failed to get upstream releases: {}", err);
Vec::new()
}
};
Ok(Self { contracts, remappings, lib_paths, versions, releases })
}
/// Builds all provided contract files with the specified compiler version.
/// Assumes that the lib-paths and remappings have already been specified and
/// that the correct compiler version is provided.
// FIXME: Does NOT support contracts with the same name.
#[tracing::instrument(skip(self, files))]
fn build(
&self,
version: &str,
files: Vec<String>,
) -> Result<HashMap<String, CompiledContract>> {
let compiler_path = find_installed_version_path(version)?
.ok_or_else(|| eyre::eyre!("version {} not installed", version))?;
// tracing::trace!(?files);
let mut solc = Solc::new_with_paths(files).solc_path(compiler_path);
let lib_paths = self
.lib_paths
.iter()
.filter(|path| PathBuf::from(path).exists())
.map(|path| {
std::fs::canonicalize(path).unwrap().into_os_string().into_string().unwrap()
})
.collect::<Vec<_>>()
.join(",");
// tracing::trace!(?lib_paths);
solc = solc.args(std::array::IntoIter::new(["--allow-paths", &lib_paths]));
// tracing::trace!(?self.remappings);
if !self.remappings.is_empty() {
solc = solc.args(self.remappings)
}
Ok(solc.build()?)
}
/// Builds all contracts with their corresponding compiler versions
#[tracing::instrument(skip(self))]
pub fn build_all(&mut self) -> Result<HashMap<String, CompiledContract>> {
tracing::info!("starting compilation");
let contracts_by_version = self.contract_versions()?;
let start = Instant::now();
let res = contracts_by_version
.into_par_iter()
.try_fold(HashMap::new, |mut map, (version, files)| {
let res = self.build(&version, files)?;
map.extend(res);
Ok::<_, eyre::Error>(map)
})
// Need to define the logic for combining the 2 maps in Rayon after the fold
.reduce(
|| Ok(HashMap::new()),
|prev: Result<HashMap<_, _>>, map: Result<HashMap<_, _>>| match (prev, map) {
(Ok(mut prev), Ok(map)) => {
prev.extend(map);
Ok(prev)
}
_ => Err(eyre::eyre!("compilation failed")),
},
);
let duration = Instant::now().duration_since(start);
tracing::info!(compilation_time = ?duration);
res
}
/// Given a Solidity file, it detects the latest compiler version which can be used
/// to build it, and returns it along with its canonicalized path. If the required
/// compiler version is not installed, it also proceeds to install it.
#[tracing::instrument(err)]
fn detect_version(&mut self, fname: &Path) -> Result<Option<(Version, String)>> {
let path = std::fs::canonicalize(fname)?;
// detects the required solc version
let sol_version = Self::version_req(&path)?;
let path_str = path
.into_os_string()
.into_string()
.map_err(|_| eyre::eyre!("invalid path, maybe not utf-8?"))?;
#[cfg(test)]
// take the lock in tests, we use this to enforce that
// a test does not run while a compiler version is being installed
let _lock = LOCK.lock();
// load the local / remote versions
let local_versions = Self::find_matching_installation(&mut self.versions, &sol_version);
let remote_versions = Self::find_matching_installation(&mut self.releases, &sol_version);
// if there's a better upstream version than the one we have, install it
let res = match (local_versions, remote_versions) {
(Some(local), None) => Some(local),
(Some(local), Some(remote)) => Some(if remote > local {
self.install_version(&remote);
remote
} else {
local
}),
(None, Some(version)) => {
self.install_version(&version);
Some(version)
}
// do nothing otherwise
_ => None,
}
.map(|version| (version, path_str));
Ok(res)
}
fn install_version(&mut self, version: &Version) {
println!("Installing {}", version);
// Blocking call to install it over RPC.
install_blocking(version).expect("could not install solc remotely");
self.versions.push(version.clone());
println!("Done!");
}
/// Gets a map of compiler version -> vec[contract paths]
fn contract_versions(&mut self) -> Result<HashMap<String, Vec<String>>> {
// Group contracts in the nones with the same version pragma
let files = glob::glob(self.contracts)?;
// tracing::trace!("Compiling files under {}", self.contracts);
println!("Compiling files under {}", self.contracts);
// get all the corresponding contract versions
let contracts = files
.filter_map(|fname| fname.ok())
.filter_map(|fname| self.detect_version(&fname).ok().flatten())
.fold(HashMap::new(), |mut map, (version, path)| {
let entry = map.entry(version.to_string()).or_insert_with(Vec::new);
entry.push(path);
map
});
if contracts.is_empty() {
eyre::bail!(
"no contracts were compiled. do you have the correct compiler version installed?"
)
}
Ok(contracts)
}
/// Parses the given Solidity file looking for the `pragma` definition and
/// returns the corresponding SemVer version requirement.
fn version_req(path: &Path) -> Result<VersionReq> {
let file = BufReader::new(File::open(path)?);
let version = file
.lines()
.map(|line| line.unwrap())
.find(|line| line.starts_with("pragma"))
.ok_or_else(|| eyre::eyre!("{:?} has no version", path))?;
let version = version
.replace("pragma solidity ", "")
.replace(";", "")
// needed to make it valid semver for things like
// `>=0.4.0 <0.5.0` => `>=0.4.0,<0.5.0`
.replace(" ", ",");
// Somehow, Solidity semver without an operator is considered to be "exact",
// but lack of operator automatically marks the operator as Caret, so we need
// to manually patch it? :shrug:
let exact = !matches!(&version[0..1], "*" | "^" | "=" | ">" | "<" | "~");
let mut version = VersionReq::parse(&version)?;
if exact {
version.comparators[0].op = semver::Op::Exact;
}
Ok(version)
}
/// Find a matching local installation for the specified required version
fn find_matching_installation(
versions: &mut [Version],
required_version: &VersionReq,
) -> Option<Version> {
// sort through them
versions.sort();
// iterate in reverse to find the last match
versions.iter().rev().find(|version| required_version.matches(version)).cloned()
}
}
/// Returns the path for an installed version
fn find_installed_version_path(version: &str) -> Result<Option<PathBuf>> {
let home_dir = svm::SVM_HOME.clone();
let path = std::fs::read_dir(home_dir)?
.into_iter()
.filter_map(|version| version.ok())
.map(|version_dir| version_dir.path())
.find(|path| path.to_string_lossy().contains(&version))
.map(|mut path| {
path.push(format!("solc-{}", &version));
path
});
Ok(path)
}
/// Blocking call to the svm installer for a specified version
fn install_blocking(version: &Version) -> Result<()> {
tokio::runtime::Runtime::new().unwrap().block_on(svm::install(version))?;
Ok(())
}
#[cfg(test)]
mod tests {
use ethers::core::rand::random;
use svm::SVM_HOME;
use super::*;
use std::{io::Write, str::FromStr};
#[test]
fn test_find_installed_version_path() {
let ver = "0.8.6";
let version = Version::from_str(ver).unwrap();
if !svm::installed_versions().unwrap().contains(&version) {
install_blocking(&version).unwrap();
}
let res = find_installed_version_path(&version.to_string()).unwrap();
let expected = SVM_HOME.join(ver).join(format!("solc-{}", ver));
assert_eq!(res.unwrap(), expected);
}
#[test]
fn does_not_find_not_installed_version() {
let ver = "1.1.1";
let version = Version::from_str(ver).unwrap();
let res = find_installed_version_path(&version.to_string()).unwrap();
assert!(res.is_none());
}
#[test]
fn test_find_latest_matching_installation() {
let mut versions = ["0.4.24", "0.5.1", "0.5.2"]
.iter()
.map(|version| Version::from_str(version).unwrap())
.collect::<Vec<_>>();
let required = VersionReq::from_str(">=0.4.24").unwrap();
let got = SolcBuilder::find_matching_installation(&mut versions, &required).unwrap();
assert_eq!(got, versions[2]);
}
#[test]
fn test_no_matching_installation() {
let mut versions = ["0.4.24", "0.5.1", "0.5.2"]
.iter()
.map(|version| Version::from_str(version).unwrap())
.collect::<Vec<_>>();
let required = VersionReq::from_str(">=0.6.0").unwrap();
let got = SolcBuilder::find_matching_installation(&mut versions, &required);
assert!(got.is_none());
}
// helper for testing solidity file versioning
struct TempSolidityFile {
version: String,
path: PathBuf,
}
use std::ops::Deref;
impl Deref for TempSolidityFile {
type Target = PathBuf;
fn deref(&self) -> &PathBuf {
&self.path
}
}
// mkdir -p
fn mkdir() -> PathBuf {
let dir = std::env::temp_dir().join(&random::<u64>().to_string()).join("contracts");
if !dir.exists() {
std::fs::create_dir_all(&dir).unwrap();
}
dir
}
// rm -rf
fn rmdir(dir: &Path) {
std::fs::remove_dir_all(&dir).unwrap();
}
impl TempSolidityFile {
fn new(dir: &Path, version: &str) -> Self {
let path = dir.join(format!("temp-{}-{}.sol", version, random::<u64>()));
let mut file = File::create(&path).unwrap();
file.write(format!("pragma solidity {};\n", version).as_bytes()).unwrap();
Self { path, version: version.to_string() }
}
}
#[test]
fn test_version_req() {
let dir = mkdir();
let versions = ["=0.1.2", "^0.5.6", ">=0.7.1", ">0.8.0"];
let files = versions.iter().map(|version| TempSolidityFile::new(&dir, version));
files.for_each(|file| {
let version_req = SolcBuilder::version_req(&file.path).unwrap();
assert_eq!(version_req, VersionReq::from_str(&file.version).unwrap());
});
// Solidity defines version ranges with a space, whereas the semver package
// requires them to be separated with a comma
let version_range = ">=0.8.0 <0.9.0";
let file = TempSolidityFile::new(&dir, version_range);
let version_req = SolcBuilder::version_req(&file.path).unwrap();
assert_eq!(version_req, VersionReq::from_str(">=0.8.0,<0.9.0").unwrap());
rmdir(&dir);
}
#[test]
// This test might be a bit hard t omaintain
fn test_detect_version() {
let dir = mkdir();
let mut builder = SolcBuilder::new("", &[], &[]).unwrap();
for (pragma, expected) in [
// pinned
("=0.4.14", "0.4.14"),
// pinned too
("0.4.14", "0.4.14"),
// The latest patch is 0.4.26
("^0.4.14", "0.4.26"),
// latest version above 0.5.0 -> we have to
// update this test whenever there's a new sol
// version. that's ok! good reminder to check the
// patch notes.
(">=0.5.0", "0.8.9"),
// range
(">=0.4.0 <0.5.0", "0.4.26"),
]
.iter()
{
// println!("Checking {}", pragma);
let file = TempSolidityFile::new(&dir, &pragma);
let res = builder.detect_version(&file.path).unwrap().unwrap();
assert_eq!(res.0, Version::from_str(&expected).unwrap());
}
rmdir(&dir);
}
#[test]
// Ensures that the contract versions get correctly assigned to a compiler
// version given a glob
fn test_contract_versions() {
let dir = mkdir();
let versions = [
// pinned
"=0.4.14",
// Up to later patches (caret implied)
"0.4.14",
// Up to later patches
"^0.4.14",
// any version above 0.5.0
">=0.5.0",
// range
">=0.4.0 <0.5.0",
];
versions.iter().for_each(|version| {
TempSolidityFile::new(&dir, version);
});
let dir_str = dir.clone().into_os_string().into_string().unwrap();
let glob = format!("{}/**/*.sol", dir_str);
let mut builder = SolcBuilder::new(&glob, &[], &[]).unwrap();
let versions = builder.contract_versions().unwrap();
assert_eq!(versions["0.4.14"].len(), 2);
assert_eq!(versions["0.4.26"].len(), 2);
assert_eq!(versions["0.8.9"].len(), 1);
rmdir(&dir);
}
fn get_glob(path: &str) -> String {
let path = std::fs::canonicalize(path).unwrap();
let mut path = path.into_os_string().into_string().unwrap();
path.push_str("/**/*.sol");
path
}
#[test]
fn test_build_all_versions() {
let path = get_glob("testdata/test-contract-versions");
let mut builder = SolcBuilder::new(&path, &[], &[]).unwrap();
let res = builder.build_all().unwrap();
// Contracts A to F
assert_eq!(res.keys().collect::<Vec<_>>().len(), 5);
}
#[test]
fn test_remappings() {
// Need to give the full paths here because we're running solc from the current
// directory and not the repo's root directory
let path = get_glob("testdata/test-contract-remappings");
let remappings = vec!["bar/=testdata/test-contract-remappings/lib/bar/".to_owned()];
let lib = std::fs::canonicalize("testdata/test-contract-remappings")
.unwrap()
.into_os_string()
.into_string()
.unwrap();
let libs = vec![lib];
let mut builder = SolcBuilder::new(&path, &remappings, &libs).unwrap();
let res = builder.build_all().unwrap();
// Foo & Bar
assert_eq!(res.keys().collect::<Vec<_>>().len(), 2);
}
fn canonicalized_path(path: &str) -> String {
std::fs::canonicalize(path).unwrap().into_os_string().into_string().unwrap()
}
#[test]
// This is useful if you want to import a library from e.g. `node_modules` (for
// whatever reason that may be) and from another path at the same time
fn test_multiple_libs() {
// Need to give the full paths here because we're running solc from the current
// directory and not the repo's root directory
let path = get_glob("testdata/test-contract-libs");
let libs = vec![
canonicalized_path("testdata/test-contract-libs/lib1"),
canonicalized_path("testdata/test-contract-libs/lib2"),
];
let mut builder = SolcBuilder::new(&path, &[], &libs).unwrap();
let res = builder.build_all().unwrap();
// Foo & Bar
assert_eq!(res.keys().collect::<Vec<_>>().len(), 3);
}
}