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

[Refactor] Make the compiler generic over the Network trait. #27947

Merged
merged 4 commits into from
May 14, 2024
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
14 changes: 7 additions & 7 deletions .circleci/token/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fi

# Swap in the private key of Alice.
echo "
NETWORK=testnet3
NETWORK=mainnet
PRIVATE_KEY=APrivateKey1zkp1w8PTxrRgGfAtfKUSq43iQyVbdQHfhGbiNPEg2LVSEXR
" > .env

Expand Down Expand Up @@ -52,7 +52,7 @@ leo run mint_public aleo13ssze66adjjkt795z9u5wpq8h6kn0y2657726h4h3e3wfnez4vqsm30

# Swap in the private key of Bob.
echo "
NETWORK=testnet3
NETWORK=mainnet
PRIVATE_KEY=APrivateKey1zkpFo72g7N9iFt3JzzeG8CqsS5doAiXyFvNCgk2oHvjRCzF
" > .env

Expand Down Expand Up @@ -86,7 +86,7 @@ leo run mint_private aleo17vy26rpdhqx4598y5gp7nvaa9rk7tnvl6ufhvvf4calsrrqdaqyshd

# Swap in the private key of Alice.
echo "
NETWORK=testnet3
NETWORK=mainnet
PRIVATE_KEY=APrivateKey1zkp1w8PTxrRgGfAtfKUSq43iQyVbdQHfhGbiNPEg2LVSEXR
" > .env

Expand Down Expand Up @@ -120,7 +120,7 @@ leo run transfer_public aleo17vy26rpdhqx4598y5gp7nvaa9rk7tnvl6ufhvvf4calsrrqdaqy

# Swap in the private key of Bob.
echo "
NETWORK=testnet3
NETWORK=mainnet
PRIVATE_KEY=APrivateKey1zkpFo72g7N9iFt3JzzeG8CqsS5doAiXyFvNCgk2oHvjRCzF
" > .env

Expand Down Expand Up @@ -158,7 +158,7 @@ leo run transfer_private "{

# Swap in the private key of Alice.
echo "
NETWORK=testnet3
NETWORK=mainnet
PRIVATE_KEY=APrivateKey1zkp1w8PTxrRgGfAtfKUSq43iQyVbdQHfhGbiNPEg2LVSEXR
" > .env

Expand Down Expand Up @@ -193,7 +193,7 @@ leo run transfer_public_to_private aleo17vy26rpdhqx4598y5gp7nvaa9rk7tnvl6ufhvvf4

# Swap in the private key of Bob.
echo "
NETWORK=testnet3
NETWORK=mainnet
PRIVATE_KEY=APrivateKey1zkpFo72g7N9iFt3JzzeG8CqsS5doAiXyFvNCgk2oHvjRCzF
" > .env

Expand Down Expand Up @@ -234,6 +234,6 @@ leo run transfer_private_to_public "{
# Swap in the private key of Alice.
# This is done to ensure that program.json is the same after every execution of ./run.sh.
echo "
NETWORK=testnet3
NETWORK=mainnet
PRIVATE_KEY=APrivateKey1zkp1w8PTxrRgGfAtfKUSq43iQyVbdQHfhGbiNPEg2LVSEXR
" > .env
4 changes: 2 additions & 2 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ updates:
schedule:
interval: daily
time: "10:00"
target-branch: "testnet3"
target-branch: "mainnet"
open-pull-requests-limit: 20
ignore:
- dependency-name: snarkvm-curves
Expand Down Expand Up @@ -89,4 +89,4 @@ updates:
- 0.8.3
- dependency-name: rand_xorshift
versions:
- 0.3.0
- 0.3.0
2 changes: 1 addition & 1 deletion .github/workflows/acl2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:

- name: Generate asts
run: |
# This currently has to be on testnet3 to work:
# This currently has to be on mainnet to work:
cd compiler/parser
cargo -q install --path . --example parser
# To ensure full file tests,
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ on:
pull_request:
push:
branches:
- testnet3
- mainnet
paths-ignore:
- 'docs/**'
- 'documentation/**'
Expand Down Expand Up @@ -189,7 +189,7 @@ jobs:
- name: Checkout
uses: actions/checkout@v2
with:
ref: testnet3
ref: mainnet

- name: Install sccache
env:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Code Coverage
on:
push:
branches:
- testnet3
- mainnet
paths-ignore:
- 'docs/**'
- 'documentation/**'
Expand Down Expand Up @@ -100,4 +100,4 @@ jobs:
run: sccache --show-stats

- name: Stop sccache server
run: sccache --stop-server || true
run: sccache --stop-server || true
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Checkout
uses: actions/checkout@v2
with:
ref: testnet3
ref: mainnet

- name: Install sccache
env:
Expand Down
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ Thank you for your interest in contributing to Leo! Below you can find some guid

Please follow the instructions below when filing pull requests:

- Ensure that your branch is forked from the current [testnet3](https://github.com/AleoHQ/leo/tree/testnet3) branch.
- Ensure that your branch is forked from the current [mainnet](https://github.com/AleoHQ/leo/tree/mainnet) branch.
- Fill out the provided markdown template for the feature or proposal. Be sure to link the pull request to any issues by using keywords. Example: "closes #130".
- Run `cargo fmt` before you commit; we use the `nightly` version of `rustfmt` to format the code, so you'll need to have the `nightly` toolchain installed on your machine; there's a [git hook](https://git-scm.com/docs/githooks) that ensures proper formatting before any commits can be made, and [`.rustfmt.toml`](https://github.com/AleoHQ/Leo/blob/testnet3/.rustfmt.toml) specifies some of the formatting conventions.
- Run `cargo fmt` before you commit; we use the `nightly` version of `rustfmt` to format the code, so you'll need to have the `nightly` toolchain installed on your machine; there's a [git hook](https://git-scm.com/docs/githooks) that ensures proper formatting before any commits can be made, and [`.rustfmt.toml`](https://github.com/AleoHQ/Leo/blob/mainnet/.rustfmt.toml) specifies some of the formatting conventions.
- Run `cargo clippy` to ensure that popular correctness and performance pitfalls are avoided.

## Style
Expand Down
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

<p align="center">
<a href="https://circleci.com/gh/AleoHQ/leo"><img src="https://circleci.com/gh/AleoHQ/leo.svg?style=svg&circle-token=00960191919c40be0774e00ce8f7fa1fcaa20c00"></a>
<a href="https://codecov.io/gh/AleoHQ/leo"><img src="https://codecov.io/gh/AleoHQ/leo/branch/testnet3/graph/badge.svg?token=S6MWO60SYL"/></a>
<a href="https://codecov.io/gh/AleoHQ/leo"><img src="https://codecov.io/gh/AleoHQ/leo/branch/mainnet/graph/badge.svg?token=S6MWO60SYL"/></a>
<a href="https://discord.gg/aleo"><img src="https://img.shields.io/discord/700454073459015690?logo=discord"/></a>
<a href="https://github.com/AleoHQ/leo/blob/testnet3/CONTRIBUTORS.md"><img src="https://img.shields.io/badge/contributors-393-ee8449"/></a>
<a href="https://github.com/AleoHQ/leo/blob/mainnet/CONTRIBUTORS.md"><img src="https://img.shields.io/badge/contributors-393-ee8449"/></a>
<a href="https://twitter.com/AleoHQ"><img src="https://img.shields.io/twitter/follow/AleoHQ?style=social"/></a>
</p>
<div id="top"></div>
Expand Down
3 changes: 3 additions & 0 deletions compiler/compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ version = "=1.11.0"
[dependencies.sha2]
version = "0.10"

[dependencies.snarkvm]
workspace = true

[dependencies.indexmap]
version = "1.9"
features = [ ]
Expand Down
20 changes: 13 additions & 7 deletions compiler/compiler/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,25 @@
//! The compiler for Leo programs.
//!
//! The [`Compiler`] type compiles Leo programs into R1CS circuits.

use crate::CompilerOptions;

pub use leo_ast::Ast;
use leo_ast::{NodeBuilder, Program, Stub};
use leo_errors::{emitter::Handler, CompilerError, Result};
pub use leo_passes::SymbolTable;
use leo_passes::*;
use leo_span::{source_map::FileName, symbol::with_session_globals, Symbol};

use sha2::{Digest, Sha256};
use std::{fs, path::PathBuf};
use snarkvm::prelude::Network;

use crate::CompilerOptions;
use indexmap::{IndexMap, IndexSet};
use sha2::{Digest, Sha256};
use std::{fs, path::PathBuf};

/// The primary entry point of the Leo compiler.
#[derive(Clone)]
pub struct Compiler<'a> {
pub struct Compiler<'a, N: Network> {
/// The handler is used for error and warning emissions.
handler: &'a Handler,
/// The path to the main leo file.
Expand All @@ -55,9 +58,11 @@ pub struct Compiler<'a> {
type_table: TypeTable,
/// The stubs for imported programs. Produced by `Retriever` module.
import_stubs: IndexMap<Symbol, Stub>,
// Allows the compiler to be generic over the network.
phantom: std::marker::PhantomData<N>,
}

impl<'a> Compiler<'a> {
impl<'a, N: Network> Compiler<'a, N> {
/// Returns a new Leo compiler.
pub fn new(
program_name: String,
Expand All @@ -83,6 +88,7 @@ impl<'a> Compiler<'a> {
assigner,
import_stubs,
type_table,
phantom: Default::default(),
}
}

Expand All @@ -106,7 +112,7 @@ impl<'a> Compiler<'a> {
let prg_sf = with_session_globals(|s| s.source_map.new_source(program_string, name));

// Use the parser to construct the abstract syntax tree (ast).
self.ast = leo_parser::parse_ast(self.handler, &self.node_builder, &prg_sf.src, prg_sf.start_pos)?;
self.ast = leo_parser::parse_ast::<N>(self.handler, &self.node_builder, &prg_sf.src, prg_sf.start_pos)?;

// If the program is imported, then check that the name of its program scope matches the file name.
// Note that parsing enforces that there is exactly one program scope in a file.
Expand Down Expand Up @@ -149,7 +155,7 @@ impl<'a> Compiler<'a> {

/// Runs the type checker pass.
pub fn type_checker_pass(&'a self, symbol_table: SymbolTable) -> Result<(SymbolTable, StructGraph, CallGraph)> {
let (symbol_table, struct_graph, call_graph) = TypeChecker::do_pass((
let (symbol_table, struct_graph, call_graph) = TypeChecker::<N>::do_pass((
&self.ast,
self.handler,
symbol_table,
Expand Down
3 changes: 2 additions & 1 deletion compiler/compiler/tests/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ fn run_test(test: Test, handler: &Handler, buf: &BufferEmitter) -> Result<Value,
handler.extend_if_error(process.add_program(&aleo_program).map_err(LeoError::Anyhow))?;

// Add the bytecode to the import stubs.
let stub = handler.extend_if_error(disassemble_from_str(&bytecode).map_err(|err| err.into()))?;
let stub =
handler.extend_if_error(disassemble_from_str::<CurrentNetwork>(&bytecode).map_err(|err| err.into()))?;
import_stubs.insert(Symbol::intern(&program_name), stub);

// Hash the ast files.
Expand Down
3 changes: 2 additions & 1 deletion compiler/compiler/tests/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ fn run_test(test: Test, handler: &Handler, buf: &BufferEmitter) -> Result<Value,
}

// Add the bytecode to the import stubs.
let stub = handler.extend_if_error(disassemble_from_str(&bytecode).map_err(|err| err.into()))?;
let stub =
handler.extend_if_error(disassemble_from_str::<CurrentNetwork>(&bytecode).map_err(|err| err.into()))?;
import_stubs.insert(Symbol::intern(&program_name), stub);

// Hash the ast files.
Expand Down
6 changes: 3 additions & 3 deletions compiler/compiler/tests/utilities/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ pub fn new_compiler(
main_file_path: PathBuf,
compiler_options: Option<CompilerOptions>,
import_stubs: IndexMap<Symbol, Stub>,
) -> Compiler<'_> {
) -> Compiler<'_, CurrentNetwork> {
let output_dir = PathBuf::from("/tmp/output/");
fs::create_dir_all(output_dir.clone()).unwrap();

Expand All @@ -178,7 +178,7 @@ pub fn parse_program<'a>(
cwd: Option<PathBuf>,
compiler_options: Option<CompilerOptions>,
import_stubs: IndexMap<Symbol, Stub>,
) -> Result<Compiler<'a>, LeoError> {
) -> Result<Compiler<'a, CurrentNetwork>, LeoError> {
let mut compiler = new_compiler(
program_name,
handler,
Expand Down Expand Up @@ -255,7 +255,7 @@ pub fn temp_dir() -> PathBuf {
tempfile::tempdir().expect("Failed to open temporary directory").into_path()
}

pub fn compile_and_process<'a>(parsed: &'a mut Compiler<'a>) -> Result<String, LeoError> {
pub fn compile_and_process<'a>(parsed: &'a mut Compiler<'a, CurrentNetwork>) -> Result<String, LeoError> {
parsed.add_import_stubs()?;

let st = parsed.symbol_table_pass()?;
Expand Down
4 changes: 3 additions & 1 deletion compiler/parser/examples/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ use std::{
path::{Path, PathBuf},
};

type CurrentNetwork = snarkvm::prelude::MainnetV0;

#[derive(Debug, Parser)]
#[clap(name = "leo parser", about = "Parse Leo AST and store it as a JSON")]
struct Opt {
Expand All @@ -48,7 +50,7 @@ fn main() -> Result<(), String> {

Handler::with(|h| {
let node_builder = NodeBuilder::default();
let ast = leo_parser::parse_ast(h, &node_builder, &code.src, code.start_pos)?;
let ast = leo_parser::parse_ast::<CurrentNetwork>(h, &node_builder, &code.src, code.start_pos)?;
let json = Ast::to_json_string(&ast)?;
println!("{json}");
Ok(json)
Expand Down
11 changes: 9 additions & 2 deletions compiler/parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,17 @@ pub use parser::*;
use leo_ast::{Ast, NodeBuilder};
use leo_errors::{emitter::Handler, Result};

use snarkvm::prelude::Network;

#[cfg(test)]
mod test;

/// Creates a new AST from a given file path and source code text.
pub fn parse_ast(handler: &Handler, node_builder: &NodeBuilder, source: &str, start_pos: BytePos) -> Result<Ast> {
Ok(Ast::new(parser::parse(handler, node_builder, source, start_pos)?))
pub fn parse_ast<N: Network>(
handler: &Handler,
node_builder: &NodeBuilder,
source: &str,
start_pos: BytePos,
) -> Result<Ast> {
Ok(Ast::new(parse::<N>(handler, node_builder, source, start_pos)?))
}
11 changes: 8 additions & 3 deletions compiler/parser/src/parser/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ use leo_ast::*;
use leo_errors::{emitter::Handler, ParserError, ParserWarning, Result};
use leo_span::{Span, Symbol};

use std::{fmt::Display, mem};
use snarkvm::prelude::Network;

use std::{fmt::Display, marker::PhantomData, mem};

/// Stores a program in tokenized format plus additional context.
/// May be converted into a [`Program`] AST by parsing all tokens.
pub(crate) struct ParserContext<'a> {
pub(crate) struct ParserContext<'a, N: Network> {
/// Handler used to side-channel emit errors from the parser.
pub(crate) handler: &'a Handler,
/// Counter used to generate unique node ids.
Expand All @@ -41,12 +43,14 @@ pub(crate) struct ParserContext<'a> {
pub(crate) disallow_struct_construction: bool,
/// The name of the program being parsed.
pub(crate) program_name: Option<Symbol>,
// Allows the parser to be generic over the network.
phantom: PhantomData<N>,
}

/// Dummy span used to appease borrow checker.
const DUMMY_EOF: SpannedToken = SpannedToken { token: Token::Eof, span: Span::dummy() };

impl<'a> ParserContext<'a> {
impl<'a, N: Network> ParserContext<'a, N> {
/// Returns a new [`ParserContext`] type given a vector of tokens.
pub fn new(handler: &'a Handler, node_builder: &'a NodeBuilder, mut tokens: Vec<SpannedToken>) -> Self {
// Strip out comments.
Expand All @@ -63,6 +67,7 @@ impl<'a> ParserContext<'a> {
token,
tokens,
program_name: None,
phantom: Default::default(),
};
p.bump();
p
Expand Down
Loading
Loading