Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add small stubs patch #15822

Merged
merged 2 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions utils/disassembler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ mod tests {
use super::*;
use leo_span::symbol::create_session_if_not_set_then;
use snarkvm::{prelude::Testnet3, synthesizer::program::Program};
use std::fs;

type CurrentNetwork = Testnet3;

Expand All @@ -84,4 +85,14 @@ mod tests {
}
});
}
#[test]
fn array_test() {
create_session_if_not_set_then(|_| {
let program_from_file =
fs::read_to_string("../tmp/.aleo/registry/testnet3/zk_bitwise_stack_v0_0_2.aleo").unwrap();
dbg!(program_from_file.clone());
let program = disassemble_from_str(program_from_file).unwrap();
dbg!(program);
});
}
}
50 changes: 43 additions & 7 deletions utils/retriever/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ pub struct Retriever {
path: PathBuf,
lock_file: IndexMap<Program, LockContents>,
stubs: IndexMap<Symbol, Stub>,
explored: IndexSet<Program>,
dependency_graph: DiGraph<Symbol>,
}

Expand Down Expand Up @@ -171,14 +170,15 @@ impl Retriever {
path: path.to_path_buf(),
stubs: IndexMap::new(),
lock_file: lock_file_map,
explored: IndexSet::new(),
dependency_graph: DiGraph::new(IndexSet::new()),
})
}

// Retrieve all dependencies for a program
pub fn retrieve(&mut self) -> Result<IndexMap<Symbol, Stub>, UtilError> {
let mut programs_to_retrieve = self.programs.clone();
let mut explored: IndexSet<Program> = IndexSet::new();
let mut solo_programs: IndexSet<Symbol> = IndexSet::new();

while !programs_to_retrieve.is_empty() {
let (mut results, mut dependencies) = (Vec::new(), Vec::new());
Expand All @@ -195,15 +195,15 @@ impl Retriever {
}

// Mark as visited
if !self.explored.insert(program.clone()) {
if !explored.insert(program.clone()) {
Err(UtilError::circular_dependency_error(Default::default()))?;
}
}

for (stub, program, entry) in results {
// Add dependencies to list of dependencies
entry.dependencies.clone().iter().for_each(|dep| {
if !self.explored.contains(dep) {
if !explored.contains(dep) {
dependencies.push(dep.clone());
// Trim off `.aleo` from end of the program names to be consistent with formatting in AST
self.dependency_graph.add_edge(
Expand All @@ -213,6 +213,11 @@ impl Retriever {
}
});

// Add programs that do not have any dependencies to list of solo programs since they are not being added to dependency graph
if entry.dependencies.is_empty() {
solo_programs.insert(Symbol::intern(&program.name.clone()[..program.name.len() - 5]));
}

// Add stub to list of stubs
if let Some(existing) = self.stubs.insert(stub.stub_id.name.name, stub.clone()) {
Err(UtilError::duplicate_dependency_name_error(existing.stub_id.name.name, Default::default()))?;
Expand All @@ -231,14 +236,27 @@ impl Retriever {
// Check for dependency cycles
match self.dependency_graph.post_order() {
Ok(order) => {
// Return stubs in post order
Ok(order
// Collect all the stubs in the order specified by the dependency graph
let mut stubs: IndexMap<Symbol, Stub> = order
.iter()
.map(|id| match self.stubs.get(id) {
Some(s) => (*id, s.clone()),
None => panic!("Stub {id} not found"),
})
.collect())
.collect();

// Add all the stubs that do not have any dependencies
solo_programs.iter().for_each(|id| {
match self.stubs.get(id) {
Some(s) => {
if stubs.insert(*id, s.clone()).is_some() {
panic!("Stub {id} cannot both have dependencies and not have dependencies")
}
}
None => panic!("Stub {id} not found"),
};
});
Ok(stubs)
}
Err(DiGraphError::CycleDetected(_)) => Err(UtilError::circular_dependency_error(Default::default()))?,
}
Expand Down Expand Up @@ -371,4 +389,22 @@ mod tests {
// Reset $HOME
env::set_var("HOME", original_home);
}

#[test]
fn temp_dir_parent_test() {
// Set $HOME to tmp directory so that tests do not modify users real home directory
let original_home = env::var("HOME").unwrap();
env::set_var("HOME", "../tmp");

// Test pulling nested dependencies from network
const BUILD_DIRECTORY: &str = "../tmp/parent";
create_session_if_not_set_then(|_| {
let build_dir = PathBuf::from(BUILD_DIRECTORY);
let mut retriever = Retriever::new(&build_dir).expect("Failed to build retriever");
let _stubs = retriever.retrieve().expect("failed to retrieve");
});

// Reset $HOME
env::set_var("HOME", original_home);
}
}
10 changes: 10 additions & 0 deletions utils/tmp/.aleo/registry/testnet3/child.aleo
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
program child.aleo;

struct Test:
a as u32;
b as u32;


function foo:
input r0 as Test.private;
output r0.a as u32.private;
Loading
Loading