Skip to content

Commit

Permalink
feat: generate test actor "bundle" (#1802)
Browse files Browse the repository at this point in the history
Instead of hard-coding it. This way, we can specify all actors in one
place.
  • Loading branch information
Stebalien authored Jun 30, 2023
1 parent 2138878 commit 9c16cf5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 68 deletions.
4 changes: 2 additions & 2 deletions testing/integration/tests/calibration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use fvm_shared::error::ExitCode;
use fvm_shared::message::Message;
use fvm_shared::state::StateTreeVersion;
use fvm_shared::version::NetworkVersion;
use fvm_test_actors::wasm_bin::GAS_CALIBRATION_ACTOR_BIN;
use fvm_test_actors::wasm_bin::GAS_CALIBRATION_ACTOR_BINARY;
use lazy_static::lazy_static;
use num_traits::Zero;
use serde::Serialize;
Expand Down Expand Up @@ -131,7 +131,7 @@ pub fn instantiate_tester() -> TestEnv {

tester
.set_actor_from_bin(
GAS_CALIBRATION_ACTOR_BIN,
GAS_CALIBRATION_ACTOR_BINARY,
state_cid,
actor_address,
TokenAmount::from_whole(100),
Expand Down
75 changes: 48 additions & 27 deletions testing/test_actors/build.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,47 @@
// Copyright 2021-2023 Protocol Labs
// SPDX-License-Identifier: Apache-2.0, MIT
use std::error::Error;
use std::io::{BufRead, BufReader};
use std::path::Path;
use std::fs::File;
use std::io::{BufRead, BufReader, BufWriter, Write};
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::thread;

const ACTORS: &[&str] = &[
const ACTORS: &[(&str, &str)] = &[
// calibration test actors
"fil_gas_calibration_actor",
("GAS_CALIBRATION_ACTOR_BINARY", "fil_gas_calibration_actor"),
// integration test
"fil_hello_world_actor",
"fil_stack_overflow_actor",
"fil_ipld_actor",
"fil_malformed_syscall_actor",
"fil_integer_overflow_actor",
"fil_syscall_actor",
"fil_address_actor",
"fil_events_actor",
"fil_exit_data_actor",
"fil_gaslimit_actor",
"fil_readonly_actor",
"fil_create_actor",
"fil_oom_actor",
"fil_sself_actor",
("HELLO_WORLD_ACTOR_BINARY", "fil_hello_world_actor"),
("STACK_OVERFLOW_ACTOR_BINARY", "fil_stack_overflow_actor"),
("IPLD_ACTOR_BINARY", "fil_ipld_actor"),
(
"MALFORMED_SYSCALL_ACTOR_BINARY",
"fil_malformed_syscall_actor",
),
(
"INTEGER_OVERFLOW_ACTOR_BINARY",
"fil_integer_overflow_actor",
),
("SYSCALL_ACTOR_BINARY", "fil_syscall_actor"),
("ADDRESS_ACTOR_BINARY", "fil_address_actor"),
("EVENTS_ACTOR_BINARY", "fil_events_actor"),
("EXIT_DATA_ACTOR_BINARY", "fil_exit_data_actor"),
("GASLIMIT_ACTOR_BINARY", "fil_gaslimit_actor"),
("READONLY_ACTOR_BINARY", "fil_readonly_actor"),
("CREATE_ACTOR_BINARY", "fil_create_actor"),
("OOM_ACTOR_BINARY", "fil_oom_actor"),
("SSELF_ACTOR_BINARY", "fil_sself_actor"),
];

const WASM_TARGET: &str = "wasm32-unknown-unknown";

fn main() -> Result<(), Box<dyn Error>> {
// Cargo executable location.
let cargo = std::env::var_os("CARGO").expect("no CARGO env var");

let out_dir = std::env::var_os("OUT_DIR")
.as_ref()
.map(Path::new)
.map(|p| p.join("bundle"))
.expect("no OUT_DIR env var");
println!("cargo:warning=out_dir: {:?}", &out_dir);
let out_dir = PathBuf::from(std::env::var_os("OUT_DIR").expect("no OUT_DIR env var"));
let bundle_dir = out_dir.join("bundle");
println!("cargo:warning=bundle_dir: {:?}", &bundle_dir);

let manifest_path =
Path::new(&std::env::var_os("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR unset"))
Expand All @@ -48,16 +54,16 @@ fn main() -> Result<(), Box<dyn Error>> {
// Cargo build command for all actors at once.
let mut cmd = Command::new(cargo);
cmd.arg("build")
.args(ACTORS.iter().map(|pkg| "-p=".to_owned() + pkg))
.arg("--target=wasm32-unknown-unknown")
.args(ACTORS.iter().map(|(_, pkg)| "-p=".to_owned() + pkg))
.arg(format!("--target={WASM_TARGET}"))
.arg("--profile=wasm")
.arg("--locked")
.arg("--manifest-path=".to_owned() + manifest_path.to_str().unwrap())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
// We are supposed to only generate artifacts under OUT_DIR,
// so set OUT_DIR as the target directory for this build.
.env("CARGO_TARGET_DIR", &out_dir)
.env("CARGO_TARGET_DIR", &bundle_dir)
// As we are being called inside a build-script, this env variable is set. However, we set
// our own `RUSTFLAGS` and thus, we need to remove this. Otherwise cargo favors this
// env variable.
Expand Down Expand Up @@ -92,5 +98,20 @@ fn main() -> Result<(), Box<dyn Error>> {
return Err("actor build failed".into());
}

let wasm_bin_file =
File::create(out_dir.join("wasm_bin.rs")).expect("failed to create manifest");
let mut wasm_bin_file = BufWriter::new(wasm_bin_file);
for (var, pkg) in ACTORS {
let bin = bundle_dir
.join(WASM_TARGET)
.join("wasm")
.join(format!("{pkg}.wasm"));
writeln!(
&mut wasm_bin_file,
"pub const {var}: &[u8] = include_bytes!({bin:?});"
)
.expect("failed to write to manifest");
}
wasm_bin_file.flush().expect("failed to flush manifest");
Ok(())
}
4 changes: 3 additions & 1 deletion testing/test_actors/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright 2021-2023 Protocol Labs
// SPDX-License-Identifier: Apache-2.0, MIT
pub mod wasm_bin;
pub mod wasm_bin {
include!(concat!(env!("OUT_DIR"), "/wasm_bin.rs"));
}
38 changes: 0 additions & 38 deletions testing/test_actors/src/wasm_bin/mod.rs

This file was deleted.

0 comments on commit 9c16cf5

Please sign in to comment.