leo_package/lib.rs
1// Copyright (C) 2019-2026 Provable Inc.
2// This file is part of the Leo library.
3
4// The Leo library is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// The Leo library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
16
17//! This crate deals with Leo packages on the file system and network.
18//!
19//! The main type is `Package`, which deals with Leo packages on the local filesystem.
20//! A Leo package directory is intended to have a structure like this:
21//! .
22//! ├── program.json
23//! ├── build
24//! │ ├── my_program
25//! │ │ ├── my_program.aleo
26//! │ │ └── abi.json
27//! │ └── credits
28//! │ └── credits.aleo
29//! ├── src
30//! │ └── main.leo
31//! └── tests
32//! └── test_something.leo
33//!
34//! Inside `build`, every compilation unit - the package's own program or
35//! library, its local dependencies, and fetched network imports - gets its own
36//! `build/<name>/` directory with the same shape. When compiler-debug AST
37//! snapshots are requested they appear under `build/<name>/snapshots/`.
38//!
39//! For packages that live inside a workspace (a directory whose `workspace.json`
40//! is an ancestor), `build/` moves to the workspace root rather than the
41//! package's own directory. Every member's per-unit subdirectory is then keyed
42//! by unit name under `<workspace_root>/build/<name>/`, so a unit built once
43//! is reused across members instead of being rebuilt per member.
44//!
45//! The file `program.json` is a manifest containing the program name, version, description,
46//! and license, together with information about its dependencies.
47//!
48//! Such a directory structure, together with a `.gitignore` file, may be created
49//! on the file system using `Package::initialize`.
50//! ```no_run
51//! # use leo_ast::NetworkName;
52//! # use leo_package::{Package};
53//! let path = Package::initialize("my_package", "path/to/parent", false).unwrap();
54//! ```
55//!
56//! `tests` is where unit test files may be placed.
57//!
58//! Given an existing directory with such a structure, a `Package` may be created from it with
59//! `Package::from_directory`:
60//! ```no_run
61//! # use leo_ast::NetworkName;
62//! use leo_package::Package;
63//! let package = Package::from_directory("path/to/package", "/home/me/.aleo", false, false, Some(NetworkName::TestnetV0), Some("http://localhost:3030"), 3).unwrap();
64//! ```
65//! This will read the manifest and keep their data in `package.manifest`.
66//! It will also process dependencies and store them in topological order in `package.compilation_units`. This processing
67//! will involve fetching bytecode from the network for network dependencies.
68//! If the `no_cache` option (3rd parameter) is set to `true`, the package will not use the dependency cache.
69//! The endpoint and network are optional and are only needed if the package has network dependencies.
70//!
71//! If you want to simply read the manifest file without processing dependencies, use
72//! `Package::from_directory_no_graph`.
73//!
74//! `CompilationUnit` generally doesn't need to be created directly, as `Package` will create `CompilationUnit`s
75//! for the main program and all dependencies. However, if you'd like to fetch bytecode for
76//! a program, you can use `CompilationUnit::fetch`.
77
78#![forbid(unsafe_code)]
79
80mod errors;
81
82use leo_ast::NetworkName;
83use leo_errors::{Backtraced, Result};
84use leo_span::Symbol;
85
86use std::path::Path;
87
88mod dependency;
89pub use dependency::*;
90
91mod location;
92pub use location::*;
93
94mod manifest;
95pub use manifest::*;
96
97mod package;
98pub use package::*;
99
100mod compilation_unit;
101pub use compilation_unit::*;
102
103mod workspace;
104pub use workspace::*;
105
106pub const SOURCE_DIRECTORY: &str = "src";
107
108pub const MAIN_FILENAME: &str = "main.leo";
109
110pub const LIB_FILENAME: &str = "lib.leo";
111
112pub const BUILD_DIRECTORY: &str = "build";
113
114pub const ABI_FILENAME: &str = "abi.json";
115
116/// Name of the per-unit subdirectory holding interface ABI JSON files.
117pub const INTERFACES_DIRNAME: &str = "interfaces";
118
119/// Name of the per-unit subdirectory holding compiler-debug AST snapshots.
120/// Created lazily on first write; absent on builds that don't request snapshots.
121pub const SNAPSHOTS_DIRNAME: &str = "snapshots";
122
123pub const TESTS_DIRECTORY: &str = "tests";
124
125/// Maximum allowed program size in bytes.
126pub const MAX_PROGRAM_SIZE: usize =
127 <snarkvm::prelude::TestnetV0 as snarkvm::prelude::Network>::MAX_PROGRAM_SIZE.last().unwrap().1;
128
129/// The edition of a deployed program on the Aleo network.
130/// Edition 0 is the initial deployment, and increments with each upgrade.
131pub type Edition = u16;
132
133/// Strips a trailing `.aleo` (the Aleo program-ID suffix) from a compilation
134/// unit name, yielding the bare name.
135///
136/// `CompilationUnit` names are bare for local packages but `.aleo`-suffixed for
137/// network programs; build paths key on the bare name so the two are unified.
138pub fn bare_unit_name(name: &str) -> &str {
139 name.strip_suffix(".aleo").unwrap_or(name)
140}
141
142/// Converts a valid program or library name into a `Symbol`.
143///
144/// Names must either end with `.aleo` or contain no periods; otherwise an error is returned.
145fn symbol(name: &str) -> Result<Symbol> {
146 if name.ends_with(".aleo") || !name.contains('.') {
147 Ok(Symbol::intern(name))
148 } else {
149 Err(crate::errors::invalid_network_name(name).into())
150 }
151}
152
153/// Checks whether a string is a valid Aleo program name.
154///
155/// A valid program name must end with `.aleo` and the base name (without the
156/// suffix) must satisfy Aleo package naming rules.
157pub fn is_valid_program_name(name: &str) -> bool {
158 let Some(rest) = name.strip_suffix(".aleo") else {
159 tracing::error!("Program names must end with `.aleo`.");
160 return false;
161 };
162
163 is_valid_package_name(rest)
164}
165
166/// Checks whether a string is a valid Aleo library name.
167///
168/// Library names must satisfy Aleo package naming rules but do not require
169/// a `.aleo` suffix.
170pub fn is_valid_library_name(name: &str) -> bool {
171 is_valid_package_name(name)
172}
173
174/// Checks whether a string satisfies general Aleo package naming rules.
175///
176/// Names must be nonempty, start with a letter, contain only ASCII alphanumeric
177/// characters or underscores, avoid reserved keywords, and not contain "aleo".
178fn is_valid_package_name(name: &str) -> bool {
179 // Check that the name is nonempty.
180 if name.is_empty() {
181 tracing::error!("Aleo names must be nonempty");
182 return false;
183 }
184
185 let first = name.chars().next().unwrap();
186
187 // Check that the first character is not an underscore.
188 if first == '_' {
189 tracing::error!("Aleo names cannot begin with an underscore");
190 return false;
191 }
192
193 // Check that the first character is not a number.
194 if first.is_numeric() {
195 tracing::error!("Aleo names cannot begin with a number");
196 return false;
197 }
198
199 // Check valid characters.
200 if name.chars().any(|c| !c.is_ascii_alphanumeric() && c != '_') {
201 tracing::error!("Aleo names can only contain ASCII alphanumeric characters and underscores.");
202 return false;
203 }
204
205 // Check reserved keywords.
206 if reserved_keywords().any(|kw| kw == name) {
207 tracing::error!(
208 "Aleo names cannot be a SnarkVM reserved keyword. Reserved keywords are: {}.",
209 reserved_keywords().collect::<Vec<_>>().join(", ")
210 );
211 return false;
212 }
213
214 // Disallow "aleo"
215 if name.contains("aleo") {
216 tracing::error!("Aleo names cannot contain the keyword `aleo`.");
217 return false;
218 }
219
220 true
221}
222
223/// Get the list of all reserved and restricted keywords from snarkVM.
224/// These keywords cannot be used as program names.
225/// See: https://github.com/ProvableHQ/snarkVM/blob/046a2964f75576b2c4afbab9aa9eabc43ceb6dc3/synthesizer/program/src/lib.rs#L192
226pub fn reserved_keywords() -> impl Iterator<Item = &'static str> {
227 use snarkvm::prelude::{Program, TestnetV0};
228
229 // Flatten RESTRICTED_KEYWORDS by ignoring ConsensusVersion
230 let restricted = Program::<TestnetV0>::RESTRICTED_KEYWORDS.iter().flat_map(|(_, kws)| kws.iter().copied());
231
232 Program::<TestnetV0>::KEYWORDS.iter().copied().chain(restricted)
233}
234
235/// Creates a configured ureq agent for Leo network requests.
236///
237/// Disables `http_status_as_error` so 4xx/5xx responses return `Ok(Response)`
238/// instead of `Err(StatusCode)`. This preserves response bodies which often
239/// contain useful error details from the server.
240pub fn create_http_agent() -> ureq::Agent {
241 ureq::Agent::config_builder().max_redirects(0).http_status_as_error(false).build().new_agent()
242}
243
244/// Retries a fallible network operation with exponential backoff.
245///
246/// Attempts the operation `retries + 1` times. Delays between attempts are
247/// 1 s, 2 s, 4 s, …, capped at 64 s. Returns the result of the last attempt.
248///
249/// Only use this for idempotent, read-only network calls (GET requests);
250/// never use it for state-mutating calls such as transaction broadcasts.
251pub fn retry_network_call<T, E: std::fmt::Display>(
252 network_retries: u32,
253 mut f: impl FnMut() -> std::result::Result<T, E>,
254) -> std::result::Result<T, E> {
255 let mut result = f();
256 for attempt in 1..=network_retries {
257 if result.is_ok() {
258 break;
259 }
260 let delay_secs = 2u64.pow(attempt - 1).min(64);
261 eprintln!("⚠️ Network request failed, retrying in {delay_secs}s (attempt {attempt}/{network_retries})...");
262 std::thread::sleep(std::time::Duration::from_secs(delay_secs));
263 result = f();
264 }
265 result
266}
267
268// Fetch the given endpoint url and return the sanitized response.
269pub fn fetch_from_network(url: &str, network_retries: u32) -> Result<String, Backtraced> {
270 fetch_from_network_plain(url, network_retries).map(|s| s.replace("\\n", "\n").replace('\"', ""))
271}
272
273pub fn fetch_from_network_plain(url: &str, network_retries: u32) -> Result<String, Backtraced> {
274 // Retry only on transport-level failures (connection errors, timeouts, etc.).
275 // HTTP 3xx/4xx/5xx responses are not retried since they reflect persistent conditions.
276 let agent = create_http_agent();
277 let mut response = retry_network_call(network_retries, || {
278 agent
279 .get(url)
280 .header("X-Leo-Version", env!("CARGO_PKG_VERSION"))
281 .call()
282 .map_err(|e| crate::errors::failed_to_retrieve_from_endpoint(url, e))
283 })?;
284 match response.status().as_u16() {
285 200..=299 => Ok(response.body_mut().read_to_string().unwrap()),
286 301 => Err(crate::errors::endpoint_moved_error(url)),
287 _ => Err(crate::errors::network_error(url, response.status())),
288 }
289}
290
291/// Fetch the given program from the network and return the program as a string.
292// TODO (@d0cd) Unify with `leo_package::CompilationUnit::fetch`.
293pub fn fetch_program_from_network(
294 name: &str,
295 endpoint: &str,
296 network: NetworkName,
297 network_retries: u32,
298) -> Result<String, Backtraced> {
299 let url = format!("{endpoint}/{network}/program/{name}");
300 let program = fetch_from_network(&url, network_retries)?;
301 Ok(program)
302}
303
304/// Fetch the latest edition of a program from the network.
305///
306/// Returns the actual latest edition number for the given program.
307/// This should be used instead of defaulting to arbitrary edition numbers.
308pub fn fetch_latest_edition(
309 name: &str,
310 endpoint: &str,
311 network: NetworkName,
312 network_retries: u32,
313) -> Result<Edition, Backtraced> {
314 // Strip the .aleo suffix if present for the URL.
315 let name_without_suffix = name.strip_suffix(".aleo").unwrap_or(name);
316
317 let url = format!("{endpoint}/{network}/program/{name_without_suffix}.aleo/latest_edition");
318 let contents = fetch_from_network(&url, network_retries)?;
319 contents.parse::<u16>().map_err(|e| {
320 crate::errors::failed_to_retrieve_from_endpoint(url, format!("Failed to parse edition as u16: {e}"))
321 })
322}
323
324// Verify that a fetched program is valid aleo instructions.
325pub fn verify_valid_program(name: &str, program: &str) -> Result<(), Backtraced> {
326 use snarkvm::prelude::{Program, TestnetV0};
327 use std::str::FromStr as _;
328
329 // Check if the program size exceeds the maximum allowed limit.
330 let program_size = program.len();
331
332 if program_size > MAX_PROGRAM_SIZE {
333 return Err(crate::errors::program_size_limit_exceeded(name, program_size, MAX_PROGRAM_SIZE));
334 }
335
336 // Parse the program to verify it's valid Aleo instructions.
337 match Program::<TestnetV0>::from_str(program) {
338 Ok(_) => Ok(()),
339 Err(_) => Err(crate::errors::snarkvm_parsing_error(name)),
340 }
341}
342
343pub fn filename_no_leo_extension(path: &Path) -> Option<&str> {
344 filename_no_extension(path, ".leo")
345}
346
347pub fn filename_no_aleo_extension(path: &Path) -> Option<&str> {
348 filename_no_extension(path, ".aleo")
349}
350
351fn filename_no_extension<'a>(path: &'a Path, extension: &'static str) -> Option<&'a str> {
352 path.file_name().and_then(|os_str| os_str.to_str()).and_then(|s| s.strip_suffix(extension))
353}