-
Notifications
You must be signed in to change notification settings - Fork 391
chore(aggregation-mode): prevent rebuilding programs if not changed #1959
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
Closed
Closed
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
68d9540
chore: prevent recompilation by hashing files + features
MarcosNicolau 23f2fcc
revert: makefile changes
MarcosNicolau bafd0fb
fix: req-add docker in build
MarcosNicolau 71d8808
chore: update flags
MarcosNicolau 2bf04b8
chore: cargo fmt
MarcosNicolau d30a43d
refactor: build.rs
MarcosNicolau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,33 +1,91 @@ | ||
| use risc0_build::{DockerOptionsBuilder, GuestOptionsBuilder}; | ||
| use sha3::{Digest, Keccak256}; | ||
|
|
||
| use std::collections::HashMap; | ||
| use std::io::Read; | ||
| use std::path::Path; | ||
| use std::{env, fs}; | ||
|
|
||
| fn hash_files_and_features<P: AsRef<Path>>(paths: &[P], features: Vec<String>) -> String { | ||
| let mut hasher = Keccak256::new(); | ||
| for path in paths { | ||
| let mut f = fs::File::open(path).unwrap(); | ||
| let mut buffer = Vec::new(); | ||
| f.read_to_end(&mut buffer).unwrap(); | ||
| hasher.update(&buffer); | ||
| } | ||
|
|
||
| for feature in features { | ||
| hasher.update(&feature); | ||
| } | ||
|
|
||
| format!("{:x}", hasher.finalize()) | ||
| } | ||
|
|
||
| // Reference: https://docs.succinct.xyz/docs/sp1/writing-programs/compiling#advanced-build-options-1 | ||
| fn main() { | ||
| sp1_build::build_program_with_args("./aggregation_programs/sp1", { | ||
| sp1_build::BuildArgs { | ||
| output_directory: Some("./aggregation_programs/sp1/elf".to_string()), | ||
| binaries: vec![ | ||
| "sp1_user_proofs_aggregator_program".into(), | ||
| "sp1_chunk_aggregator_program".into(), | ||
| ], | ||
| // We use Docker to generate a reproducible ELF that will be identical across all platforms | ||
| // (https://docs.succinct.xyz/docs/sp1/writing-programs/compiling#production-builds) | ||
| docker: true, | ||
| ..Default::default() | ||
| } | ||
| }); | ||
|
|
||
| // With this containerized build process, we ensure that all builds of the guest code, | ||
| // regardless of the machine or local environment, will produce the same ImageID | ||
| let docker_options = DockerOptionsBuilder::default().build().unwrap(); | ||
| // Reference: https://github.com/risc0/risc0/blob/main/risc0/build/src/config.rs#L73-L90 | ||
| let guest_options = GuestOptionsBuilder::default() | ||
| .use_docker(docker_options) | ||
| .build() | ||
| .unwrap(); | ||
|
|
||
| risc0_build::embed_methods_with_options(HashMap::from([( | ||
| "risc0_aggregation_program", | ||
| guest_options, | ||
| )])); | ||
| let programs = [ | ||
| "build.rs", | ||
| "aggregation_programs/Cargo.toml", | ||
| "aggregation_programs/Cargo.lock", | ||
| "aggregation_programs/sp1/Cargo.toml", | ||
| "aggregation_programs/sp1/src/lib.rs", | ||
| "aggregation_programs/sp1/src/user_proofs_aggregator_main.rs", | ||
| "aggregation_programs/sp1/src/chunk_aggregator_main.rs", | ||
| "aggregation_programs/risc0/Cargo.toml", | ||
| "aggregation_programs/risc0/src/user_proofs_aggregator_main.rs", | ||
| "aggregation_programs/risc0/src/chunk_aggregator_main.rs", | ||
| "aggregation_programs/risc0/src/lib.rs", | ||
| ]; | ||
|
|
||
| for file in &programs { | ||
| println!("cargo:rerun-if-changed={}", file); | ||
| } | ||
|
|
||
| // Get all the env vars from rust (RUSTC, CARGO_FEATURES, etc) | ||
| // But filter those that don't affect the build of the program | ||
| let mut flags: Vec<String> = env::vars() | ||
| .filter(|(k, _)| k != "AGGREGATOR" || k != "RISC0_DEV_MODE" || k != "SP1_PROVER") | ||
| .map(|(k, v)| format!("{k}={v}")) | ||
| .collect(); | ||
| // Sort them to make it deterministic in spite of the order. | ||
| flags.sort(); | ||
|
|
||
| let hash = hash_files_and_features(&programs, flags); | ||
| let hash_file = Path::new("target/programs_hash.txt"); | ||
|
|
||
| let needs_build = if let Ok(prev) = fs::read_to_string(hash_file) { | ||
| prev != hash | ||
| } else { | ||
| true | ||
| }; | ||
|
|
||
| if needs_build { | ||
| sp1_build::build_program_with_args("./aggregation_programs/sp1", { | ||
| sp1_build::BuildArgs { | ||
| output_directory: Some("./aggregation_programs/sp1/elf".to_string()), | ||
| // We use Docker to generate a reproducible ELF that will be identical across all platforms | ||
| // (https://docs.succinct.xyz/docs/sp1/writing-programs/compiling#production-builds) | ||
| docker: true, | ||
| ..Default::default() | ||
| } | ||
| }); | ||
|
|
||
| // With this containerized build process, we ensure that all builds of the guest code, | ||
| // regardless of the machine or local environment, will produce the same ImageID | ||
| let docker_options = DockerOptionsBuilder::default().build().unwrap(); | ||
| // Reference: https://github.com/risc0/risc0/blob/main/risc0/build/src/config.rs#L73-L90 | ||
| let guest_options = GuestOptionsBuilder::default() | ||
| .use_docker(docker_options) | ||
| .build() | ||
| .unwrap(); | ||
| risc0_build::embed_methods_with_options(HashMap::from([( | ||
| "risc0_aggregation_program", | ||
| guest_options, | ||
| )])); | ||
|
|
||
| fs::write(hash_file, hash).unwrap(); | ||
| } else { | ||
| println!("Programs code unchanged — skipping programs build"); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These prints are not displayed
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These flags indicate that the
build.rsshould run if the file outputted has been modified. They weren't really working for me that is why the hash solution was applied. Right now, they are there just in case. You can read more in the source.Source: https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script