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

Include Optional Cargo Stylus Version Flag for Verification #93

Merged
merged 3 commits into from
Sep 3, 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
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ resolver = "2"

[workspace.package]
authors = ["Offchain Labs"]
version = "0.5.1"
version = "0.5.2"
edition = "2021"
homepage = "https://arbitrum.io"
license = "MIT OR Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ RUN apt-get update && apt-get install -y git
RUN rustup target add x86_64-unknown-linux-gnu
RUN git clone https://github.com/offchainlabs/cargo-stylus.git
WORKDIR /cargo-stylus
RUN git checkout v0.5.1
RUN git checkout v0.5.2
RUN cargo build --release --manifest-path main/Cargo.toml
FROM --platform=linux/amd64 rust:1.80
COPY --from=builder /cargo-stylus/target/release/cargo-stylus /usr/local/bin/cargo-stylus
41 changes: 28 additions & 13 deletions main/src/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use crate::constants::TOOLCHAIN_FILE_NAME;
use crate::macros::greyln;
use crate::project::extract_toolchain_channel;

fn image_exists() -> Result<bool> {
let image_name = format!("cargo-stylus-base:{}", env!("CARGO_PKG_VERSION"));
fn image_exists(cargo_stylus_version: &str) -> Result<bool> {
let image_name = format!("cargo-stylus-base:{}", cargo_stylus_version);
let output = Command::new("docker")
.arg("images")
.arg(image_name)
Expand All @@ -37,12 +37,16 @@ a reproducible deployment, or opt out by using the --no-verify flag for local bu
Ok(output.stdout.iter().filter(|c| **c == b'\n').count() > 1)
}

fn create_image(version: &str) -> Result<()> {
if image_exists()? {
fn create_image(cargo_stylus_version: Option<String>, version: &str) -> Result<()> {
let cargo_stylus_version =
cargo_stylus_version.unwrap_or_else(|| env!("CARGO_PKG_VERSION").to_string());
if image_exists(&cargo_stylus_version)? {
return Ok(());
}
let pkg_version = env!("CARGO_PKG_VERSION");
let name = format!("cargo-stylus-base-{}-toolchain-{}", pkg_version, version);
let name = format!(
"cargo-stylus-base-{}-toolchain-{}",
cargo_stylus_version, version
);
println!("Building Docker image for Rust toolchain {}", version,);
let mut child = Command::new("docker")
.arg("build")
Expand All @@ -62,7 +66,7 @@ fn create_image(version: &str) -> Result<()> {
RUN rustup target add wasm32-unknown-unknown
RUN rustup component add rust-src --toolchain {}-x86_64-unknown-linux-gnu
",
pkg_version,
cargo_stylus_version,
version,
version,
version,
Expand All @@ -71,9 +75,17 @@ fn create_image(version: &str) -> Result<()> {
Ok(())
}

fn run_in_docker_container(version: &str, command_line: &[&str]) -> Result<()> {
let pkg_version = env!("CARGO_PKG_VERSION");
let name = format!("cargo-stylus-base-{}-toolchain-{}", pkg_version, version);
fn run_in_docker_container(
cargo_stylus_version: Option<String>,
toolchain_version: &str,
command_line: &[&str],
) -> Result<()> {
let cargo_stylus_version =
cargo_stylus_version.unwrap_or_else(|| env!("CARGO_PKG_VERSION").to_string());
let name = format!(
"cargo-stylus-base-{}-toolchain-{}",
cargo_stylus_version, toolchain_version
);
let dir =
std::env::current_dir().map_err(|e| eyre!("failed to find current directory: {e}"))?;
Command::new("docker")
Expand All @@ -93,7 +105,10 @@ fn run_in_docker_container(version: &str, command_line: &[&str]) -> Result<()> {
Ok(())
}

pub fn run_reproducible(command_line: &[String]) -> Result<()> {
pub fn run_reproducible(
cargo_stylus_version: Option<String>,
command_line: &[String],
) -> Result<()> {
verify_valid_host()?;
let toolchain_file_path = PathBuf::from(".").as_path().join(TOOLCHAIN_FILE_NAME);
let toolchain_channel = extract_toolchain_channel(&toolchain_file_path)?;
Expand All @@ -105,8 +120,8 @@ pub fn run_reproducible(command_line: &[String]) -> Result<()> {
for s in command_line.iter() {
command.push(s);
}
create_image(&toolchain_channel)?;
run_in_docker_container(&toolchain_channel, &command)
create_image(cargo_stylus_version.clone(), &toolchain_channel)?;
run_in_docker_container(cargo_stylus_version, &toolchain_channel, &command)
}

fn verify_valid_host() -> Result<()> {
Expand Down
26 changes: 21 additions & 5 deletions main/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ struct DeployConfig {
/// builds, but at the risk of not having a reproducible contract for verification purposes.
#[arg(long)]
no_verify: bool,
/// Cargo stylus version when deploying reproducibly to downloads the corresponding cargo-stylus-base Docker image.
/// If not set, uses the default version of the local cargo stylus binary.
#[arg(long)]
cargo_stylus_version: Option<String>,
}

#[derive(Args, Clone, Debug)]
Expand All @@ -228,6 +232,10 @@ pub struct VerifyConfig {
/// If specified, will not run the command in a reproducible docker container. Useful for local
/// builds, but at the risk of not having a reproducible contract for verification purposes.
no_verify: bool,
/// Cargo stylus version when deploying reproducibly to downloads the corresponding cargo-stylus-base Docker image.
/// If not set, uses the default version of the local cargo stylus binary.
#[arg(long)]
cargo_stylus_version: Option<String>,
}

#[derive(Args, Clone, Debug)]
Expand Down Expand Up @@ -324,7 +332,7 @@ impl fmt::Display for DeployConfig {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{} {} {} {}",
"{} {} {} {} {}",
self.check_config,
self.auth,
match self.estimate_gas {
Expand All @@ -335,6 +343,10 @@ impl fmt::Display for DeployConfig {
true => "--no-verify".to_string(),
false => "".to_string(),
},
match self.cargo_stylus_version.as_ref() {
Some(version) => format!("--cargo-stylus-version={}", version),
None => "".to_string(),
},
)
}
}
Expand Down Expand Up @@ -368,13 +380,17 @@ impl fmt::Display for VerifyConfig {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{} --deployment-tx={} {}",
"{} --deployment-tx={} {} {}",
self.common_cfg,
self.deployment_tx,
match self.no_verify {
true => "--no-verify".to_string(),
false => "".to_string(),
}
},
match self.cargo_stylus_version.as_ref() {
Some(version) => format!("--cargo-stylus-version={}", version),
None => "".to_string(),
},
)
}
}
Expand Down Expand Up @@ -501,7 +517,7 @@ async fn main_impl(args: Opts) -> Result<()> {
.collect::<Vec<String>>();
commands.extend(config_args);
run!(
docker::run_reproducible(&commands),
docker::run_reproducible(config.cargo_stylus_version, &commands),
"failed reproducible run"
);
}
Expand All @@ -523,7 +539,7 @@ async fn main_impl(args: Opts) -> Result<()> {
.collect::<Vec<String>>();
commands.extend(config_args);
run!(
docker::run_reproducible(&commands),
docker::run_reproducible(config.cargo_stylus_version, &commands),
"failed reproducible run"
);
}
Expand Down
Loading