1use crate::{MAX_PROGRAM_SIZE, *};
18
19use leo_errors::{PackageError, Result, UtilError};
20use leo_span::Symbol;
21
22use snarkvm::prelude::{Program as SvmProgram, TestnetV0};
23
24use indexmap::{IndexMap, IndexSet};
25use std::path::Path;
26
27fn find_cached_edition(cache_directory: &Path, name: &str) -> Option<u16> {
30 let program_cache = cache_directory.join(name);
31 if !program_cache.exists() {
32 return None;
33 }
34
35 std::fs::read_dir(&program_cache)
37 .ok()?
38 .filter_map(|entry| entry.ok())
39 .filter_map(|entry| {
40 let file_name = entry.file_name();
41 let name = file_name.to_str()?;
42 name.parse::<u16>().ok()
43 })
44 .max()
45}
46
47#[derive(Clone, Debug, PartialEq, Eq)]
49pub enum PackageKind {
50 Program,
52 Library,
54 Test,
56}
57
58impl PackageKind {
59 pub fn is_program(&self) -> bool {
60 matches!(self, Self::Program)
61 }
62
63 pub fn is_library(&self) -> bool {
64 matches!(self, Self::Library)
65 }
66
67 pub fn is_test(&self) -> bool {
68 matches!(self, Self::Test)
69 }
70}
71
72#[derive(Clone, Debug)]
74pub struct CompilationUnit {
75 pub name: Symbol,
79 pub data: ProgramData,
80 pub edition: Option<u16>,
81 pub dependencies: IndexSet<Dependency>,
82 pub is_local: bool,
83 pub kind: PackageKind,
84}
85
86impl CompilationUnit {
87 pub fn from_aleo_path<P: AsRef<Path>>(name: Symbol, path: P, map: &IndexMap<Symbol, Dependency>) -> Result<Self> {
90 Self::from_aleo_path_impl(name, path.as_ref(), map)
91 }
92
93 fn from_aleo_path_impl(name: Symbol, path: &Path, map: &IndexMap<Symbol, Dependency>) -> Result<Self> {
94 let bytecode = std::fs::read_to_string(path).map_err(|e| {
95 UtilError::util_file_io_error(format_args!("Trying to read aleo file at {}", path.display()), e)
96 })?;
97
98 let dependencies = parse_dependencies_from_aleo(name, &bytecode, map)?;
99
100 Ok(CompilationUnit {
101 name,
102 data: ProgramData::Bytecode(bytecode),
103 edition: None,
104 dependencies,
105 is_local: true,
106 kind: PackageKind::Program,
107 })
108 }
109
110 pub fn from_package_path<P: AsRef<Path>>(name: Symbol, path: P) -> Result<Self> {
113 Self::from_package_path_impl(name, path.as_ref())
114 }
115
116 fn from_package_path_impl(name: Symbol, path: &Path) -> Result<Self> {
117 let manifest = Manifest::read_from_file(path.join(MANIFEST_FILENAME))?;
118 let manifest_symbol = crate::symbol(&manifest.program)?;
119 if name != manifest_symbol {
120 return Err(
121 PackageError::conflicting_manifest(format_args!("{name}"), format_args!("{manifest_symbol}")).into()
122 );
123 }
124 let source_directory = path.join(SOURCE_DIRECTORY);
125 source_directory.read_dir().map_err(|e| {
126 UtilError::util_file_io_error(format_args!("Failed to read directory {}", source_directory.display()), e)
127 })?;
128
129 let main_path = source_directory.join(MAIN_FILENAME);
130 let lib_path = source_directory.join(LIB_FILENAME);
131
132 let (source_path, kind) = match (main_path.exists(), lib_path.exists()) {
133 (true, true) => {
134 return Err(PackageError::ambiguous_entry_file(
135 source_directory.display(),
136 MAIN_FILENAME,
137 LIB_FILENAME,
138 )
139 .into());
140 }
141 (true, false) => (main_path, PackageKind::Program),
142 (false, true) => (lib_path, PackageKind::Library),
143 (false, false) => {
144 return Err(
145 PackageError::invalid_entry_file(source_directory.display(), MAIN_FILENAME, LIB_FILENAME).into()
146 );
147 }
148 };
149
150 Ok(CompilationUnit {
151 name,
152 data: ProgramData::SourcePath { directory: path.to_path_buf(), source: source_path },
153 edition: None,
154 dependencies: manifest
155 .dependencies
156 .unwrap_or_default()
157 .into_iter()
158 .map(|dependency| canonicalize_dependency_path_relative_to(path, dependency))
159 .collect::<Result<IndexSet<_>, _>>()?,
160 is_local: true,
161 kind,
162 })
163 }
164
165 pub fn from_test_path<P: AsRef<Path>>(source_path: P, main_program: Dependency) -> Result<Self> {
172 Self::from_path_test_impl(source_path.as_ref(), main_program)
173 }
174
175 fn from_path_test_impl(source_path: &Path, main_program: Dependency) -> Result<Self> {
176 let name = filename_no_leo_extension(source_path)
177 .ok_or_else(|| PackageError::failed_path(source_path.display(), ""))?;
178 let test_directory = source_path.parent().ok_or_else(|| {
179 UtilError::failed_to_open_file(format_args!("Failed to find directory for test {}", source_path.display()))
180 })?;
181 let package_directory = test_directory.parent().ok_or_else(|| {
182 UtilError::failed_to_open_file(format_args!("Failed to find package for test {}", source_path.display()))
183 })?;
184 let manifest = Manifest::read_from_file(package_directory.join(MANIFEST_FILENAME))?;
185 let mut dependencies = manifest
186 .dev_dependencies
187 .unwrap_or_default()
188 .into_iter()
189 .map(|dependency| canonicalize_dependency_path_relative_to(package_directory, dependency))
190 .collect::<Result<IndexSet<_>, _>>()?;
191 dependencies.insert(main_program);
192
193 Ok(CompilationUnit {
194 name: Symbol::intern(&(name.to_owned() + ".aleo")),
195 edition: None,
196 data: ProgramData::SourcePath {
197 directory: test_directory.to_path_buf(),
198 source: source_path.to_path_buf(),
199 },
200 dependencies,
201 is_local: true,
202 kind: PackageKind::Test,
203 })
204 }
205
206 pub fn fetch<P: AsRef<Path>>(
209 name: Symbol,
210 edition: Option<u16>,
211 home_path: P,
212 network: NetworkName,
213 endpoint: &str,
214 no_cache: bool,
215 ) -> Result<Self> {
216 Self::fetch_impl(name, edition, home_path.as_ref(), network, endpoint, no_cache)
217 }
218
219 fn fetch_impl(
220 name: Symbol,
221 edition: Option<u16>,
222 home_path: &Path,
223 network: NetworkName,
224 endpoint: &str,
225 no_cache: bool,
226 ) -> Result<Self> {
227 let name = Symbol::intern(name.to_string().strip_suffix(".aleo").unwrap_or(&name.to_string()));
230
231 let cache_directory = home_path.join(format!("registry/{network}"));
233
234 let edition = match edition {
237 _ if name == Symbol::intern("credits") => 0,
239 Some(edition) => edition,
240 None if !no_cache => {
241 match find_cached_edition(&cache_directory, &name.to_string()) {
243 Some(cached_edition) => cached_edition,
244 None => crate::fetch_latest_edition(&name.to_string(), endpoint, network)?,
245 }
246 }
247 None => crate::fetch_latest_edition(&name.to_string(), endpoint, network)?,
249 };
250
251 let cache_directory = cache_directory.join(format!("{name}/{edition}"));
255 let full_cache_path = cache_directory.join(format!("{name}.aleo"));
256 if !cache_directory.exists() {
257 std::fs::create_dir_all(&cache_directory).map_err(|err| {
259 UtilError::util_file_io_error(format!("Could not write path {}", cache_directory.display()), err)
260 })?;
261 }
262
263 let existing_bytecode = match full_cache_path.exists() {
265 false => None,
266 true => {
267 let existing_contents = std::fs::read_to_string(&full_cache_path).map_err(|e| {
268 UtilError::util_file_io_error(
269 format_args!("Trying to read cached file at {}", full_cache_path.display()),
270 e,
271 )
272 })?;
273 Some(existing_contents)
274 }
275 };
276
277 let bytecode = match (existing_bytecode, no_cache) {
278 (Some(bytecode), false) => bytecode,
280 (existing, _) => {
282 let primary_url = if name == Symbol::intern("credits") {
284 format!("{endpoint}/{network}/program/credits.aleo")
285 } else {
286 format!("{endpoint}/{network}/program/{name}.aleo/{edition}")
287 };
288 let secondary_url = format!("{endpoint}/{network}/program/{name}.aleo");
289 let contents = fetch_from_network(&primary_url)
290 .or_else(|_| fetch_from_network(&secondary_url))
291 .map_err(|err| {
292 UtilError::failed_to_retrieve_from_endpoint(
293 primary_url,
294 format_args!("Failed to fetch program `{name}` from network `{network}`: {err}"),
295 )
296 })?;
297
298 if let Some(existing_contents) = existing
300 && existing_contents != contents
301 {
302 println!(
303 "Warning: The cached file at `{}` is different from the one fetched from the network. The cached file will be overwritten.",
304 full_cache_path.display()
305 );
306 }
307
308 std::fs::write(&full_cache_path, &contents).map_err(|err| {
310 UtilError::util_file_io_error(
311 format_args!("Could not open file `{}`", full_cache_path.display()),
312 err,
313 )
314 })?;
315
316 contents
317 }
318 };
319
320 let dependencies = parse_dependencies_from_aleo(name, &bytecode, &IndexMap::new())?;
321
322 Ok(CompilationUnit {
323 name: Symbol::intern(&(name.to_string() + ".aleo")),
326 data: ProgramData::Bytecode(bytecode),
327 edition: Some(edition),
328 dependencies,
329 is_local: false,
330 kind: PackageKind::Program,
331 })
332 }
333}
334
335fn canonicalize_dependency_path_relative_to(base: &Path, mut dependency: Dependency) -> Result<Dependency> {
340 if let Some(path) = &mut dependency.path
341 && !path.is_absolute()
342 {
343 let joined = base.join(&path);
344 *path = joined.canonicalize().map_err(|e| PackageError::failed_path(joined.display(), e))?;
345 }
346 Ok(dependency)
347}
348
349fn parse_dependencies_from_aleo(
351 name: Symbol,
352 bytecode: &str,
353 existing: &IndexMap<Symbol, Dependency>,
354) -> Result<IndexSet<Dependency>> {
355 let program_size = bytecode.len();
357
358 if program_size > MAX_PROGRAM_SIZE {
359 return Err(leo_errors::LeoError::UtilError(UtilError::program_size_limit_exceeded(
360 name,
361 program_size,
362 MAX_PROGRAM_SIZE,
363 )));
364 }
365
366 let svm_program: SvmProgram<TestnetV0> = bytecode.parse().map_err(|_| UtilError::snarkvm_parsing_error(name))?;
368 let dependencies = svm_program
369 .imports()
370 .keys()
371 .map(|program_id| {
372 if let Some(dependency) = existing.get(&Symbol::intern(&program_id.to_string())) {
375 dependency.clone()
376 } else {
377 let name = program_id.to_string();
378 Dependency { name, location: Location::Network, path: None, edition: None }
379 }
380 })
381 .collect();
382 Ok(dependencies)
383}