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 RISC0_ETHEREUM_PATH env to ffi #120

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ rust-project.json
target/
tmp/
out/
cache/

# Ignore .pb files generated by profiling
*.pb
Expand Down
21 changes: 21 additions & 0 deletions build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ const ELF_LIB_HEADER: &str = r#"pragma solidity ^0.8.20;
library Elf {
"#;

const FFI_MANIFEST_PATH: &str = "ffi/Cargo.toml";

/// Options for building and code generation.
#[derive(Debug, Clone, Default)]
#[non_exhaustive] // more options may be added in the future.
Expand Down Expand Up @@ -142,6 +144,25 @@ pub fn generate_elf_sol(guests: &[GuestListEntry]) -> Result<Vec<u8>> {
forge_fmt(file_content.as_bytes()).context("failed to format image ID file")
}

/// Build risc0-ethereum forge ffi.
pub fn build_ffi(risc0_ethereum_path: &str) -> Result<()> {
let ffi_path = Path::new(risc0_ethereum_path).join(FFI_MANIFEST_PATH);
let status = Command::new("cargo")
.arg("build")
.arg("--manifest-path")
.arg(ffi_path.as_os_str())
.arg("--bin")
.arg("risc0-forge-ffi")
.status()?;
if !status.success() {
bail!(
"risc0-ethereum forge ffi build failed with exit code: {:?}",
status.code()
)
}
Ok(())
}

/// Uses forge fmt as a subprocess to format the given Solidity source.
fn forge_fmt(src: &[u8]) -> Result<Vec<u8>> {
// Spawn `forge fmt`
Expand Down
3 changes: 2 additions & 1 deletion contracts/src/test/RiscZeroCheats.sol
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ abstract contract RiscZeroCheats is CommonBase {
function prove(string memory elf_path, bytes memory input) internal returns (bytes memory, bytes memory) {
string[] memory imageRunnerInput = new string[](10);
uint256 i = 0;
string memory risc0EthereumPath = vm.envOr("RISC0_ETHEREUM_PATH", string("lib/risc0-ethereum"));
imageRunnerInput[i++] = "cargo";
imageRunnerInput[i++] = "run";
imageRunnerInput[i++] = "--manifest-path";
imageRunnerInput[i++] = "lib/risc0-ethereum/ffi/Cargo.toml";
imageRunnerInput[i++] = string.concat(risc0EthereumPath, "/ffi/Cargo.toml");
imageRunnerInput[i++] = "--bin";
imageRunnerInput[i++] = "risc0-forge-ffi";
imageRunnerInput[i++] = "-q";
Expand Down
9 changes: 8 additions & 1 deletion examples/erc20-counter/methods/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
use std::{collections::HashMap, env};

use risc0_build::{embed_methods_with_options, DockerOptions, GuestOptions};
use risc0_build_ethereum::generate_solidity_files;
use risc0_build_ethereum::{build_ffi, generate_solidity_files};

// Paths where the generated Solidity files will be written.
const SOLIDITY_IMAGE_ID_PATH: &str = "../contracts/ImageID.sol";
const SOLIDITY_ELF_PATH: &str = "../contracts/Elf.sol";

const RISC0_ETHEREUM_PATH: &str = "../../..";

fn main() {
// Builds can be made deterministic, and thereby reproducible, by using Docker to build the
// guest. Check the RISC0_USE_DOCKER variable and use Docker to build the guest if set.
Expand All @@ -43,4 +45,9 @@ fn main() {
.with_elf_sol_path(SOLIDITY_ELF_PATH);

generate_solidity_files(guests.as_slice(), &solidity_opts).unwrap();

// Build risc0-ethereum forge ffi.
let risc0_ethereum_path =
&env::var("RISC0_ETHEREUM_PATH").unwrap_or_else(|_| RISC0_ETHEREUM_PATH.to_string());
build_ffi(risc0_ethereum_path).unwrap();
}
Loading