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

Scrypto compiler default options for tests #1807

Merged
3 changes: 2 additions & 1 deletion examples/hello-world/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ fn test_hello() {
fn test_hello_with_test_environment() -> Result<(), RuntimeError> {
// Arrange
let mut env = TestEnvironment::new();
let package_address = PackageFactory::compile_and_publish(this_package!(), &mut env)?;
let package_address =
PackageFactory::compile_and_publish(this_package!(), &mut env, CompileProfile::Fast)?;

let mut hello = Hello::instantiate_hello(package_address, &mut env)?;

Expand Down
3 changes: 2 additions & 1 deletion radix-clis/assets/template/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ fn test_hello() {
fn test_hello_with_test_environment() -> Result<(), RuntimeError> {
// Arrange
let mut env = TestEnvironment::new();
let package_address = PackageFactory::compile_and_publish(this_package!(), &mut env)?;
let package_address =
PackageFactory::compile_and_publish(this_package!(), &mut env, CompileProfile::Fast)?;

let mut hello = Hello::instantiate_hello(package_address, &mut env)?;

Expand Down
18 changes: 17 additions & 1 deletion radix-engine-tests/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ pub mod package_loader {
panic!("Package \"{}\" not found. Are you sure that this package is: a) in the blueprints folder, b) that this is the same as the package name in the Cargo.toml file?", name)
}
}

pub fn get_using_standard_compiler_profile(name: &str) -> (Vec<u8>, PackageDefinition) {
panic!("Package \"{}\" already compiled. Cannot use specific compiler profile in compile-blueprints-at-build-time mode.", name)
}
mstrug-rdx marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand All @@ -38,14 +42,26 @@ pub mod package_loader {
pub mod package_loader {
use radix_common::prelude::*;
use radix_substate_store_queries::typed_substate_layout::*;
use scrypto_test::ledger_simulator::CompileProfile;
use std::path::PathBuf;

pub struct PackageLoader;
impl PackageLoader {
pub fn get(name: &str) -> (Vec<u8>, PackageDefinition) {
Self::get_internal(name, CompileProfile::FastWithTraceLogs)
}

pub fn get_using_standard_compiler_profile(name: &str) -> (Vec<u8>, PackageDefinition) {
Self::get_internal(name, CompileProfile::Standard)
}

fn get_internal(
name: &str,
compile_profile: CompileProfile,
) -> (Vec<u8>, PackageDefinition) {
mstrug-rdx marked this conversation as resolved.
Show resolved Hide resolved
let manifest_dir = PathBuf::from_str(env!("CARGO_MANIFEST_DIR")).unwrap();
let package_dir = manifest_dir.join("assets").join("blueprints").join(name);
scrypto_test::prelude::Compile::compile(package_dir)
scrypto_test::prelude::Compile::compile(package_dir, compile_profile)
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions radix-engine-tests/tests/flash/crypto_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,11 @@ fn run_flash_test_test_environment(enable_bls: bool, expect_success: bool) {
.build();

// Act
let result =
PackageFactory::compile_and_publish(path_local_blueprint!("crypto_scrypto"), &mut test_env);
let result = PackageFactory::compile_and_publish(
path_local_blueprint!("crypto_scrypto"),
&mut test_env,
CompileProfile::Fast,
);

// Assert
if expect_success {
Expand Down
5 changes: 4 additions & 1 deletion radix-engine-tests/tests/vm/system_wasm_buffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ fn get_ledger() -> (
LedgerSimulator<NoExtension, InMemorySubstateDatabase>,
ComponentAddress,
) {
let (code, definition) = Compile::compile(path_local_blueprint!("system_wasm_buffers"));
let (code, definition) = Compile::compile(
path_local_blueprint!("system_wasm_buffers"),
CompileProfile::FastWithTraceLogs,
);

// Arrange
let mut ledger = LedgerSimulatorBuilder::new().build();
Expand Down
233 changes: 226 additions & 7 deletions scrypto-test/src/ledger_simulator/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,67 @@ use crate::prelude::*;
use scrypto_compiler::*;
use std::path::Path;

pub enum CompileProfile {
/// Uses default compilation options - same as `scrypto build`. Should be used in all cases which requires
/// compilation results to be as close to production as possible (for instance costing related tests).
Standard,
/// Same as Standard with enabled all logs from error to trace level.
StandardWithTraceLogs,
/// Disables WASM optimization to speed-up compilation process, by default used by SDK PackageFactory.
Fast,
/// Disables WASM optimization and enables all logs from error to trace level, by default used by Ledger Simulator.
FastWithTraceLogs,
}

pub struct Compile;

impl Compile {
pub fn compile<P: AsRef<Path>>(package_dir: P) -> (Vec<u8>, PackageDefinition) {
pub fn compile<P: AsRef<Path>>(
package_dir: P,
compile_profile: CompileProfile,
) -> (Vec<u8>, PackageDefinition) {
Self::compile_with_env_vars(
package_dir,
btreemap! {
"RUSTFLAGS".to_owned() => "".to_owned(),
"CARGO_ENCODED_RUSTFLAGS".to_owned() => "".to_owned(),
},
compile_profile,
true,
)
}

// required for compile-blueprints-at-build-time feature in radix-engine-tests
pub fn compile_with_env_vars<P: AsRef<Path>>(
package_dir: P,
env_vars: sbor::rust::collections::BTreeMap<String, String>,
compile_profile: CompileProfile,
_use_coverage: bool,
) -> (Vec<u8>, PackageDefinition) {
// Initialize compiler
let mut compiler_builder = ScryptoCompiler::builder();
compiler_builder
.manifest_path(package_dir.as_ref())
.optimize_with_wasm_opt(None)
.log_level(Level::Trace); // all logs from error to trace
compiler_builder.manifest_path(package_dir.as_ref());

match compile_profile {
CompileProfile::Standard => (),
CompileProfile::StandardWithTraceLogs => {
compiler_builder.log_level(Level::Trace); // all logs from error to trace
}
CompileProfile::Fast => {
compiler_builder.optimize_with_wasm_opt(None);
}
CompileProfile::FastWithTraceLogs => {
compiler_builder.optimize_with_wasm_opt(None);
compiler_builder.log_level(Level::Trace); // all logs from error to trace
}
}

env_vars.iter().for_each(|(name, value)| {
compiler_builder.env(name, EnvironmentVariableAction::Set(value.clone()));
});

#[cfg(feature = "coverage")]
{
if _use_coverage {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need to feature flag?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, because it changes some limits to larger values in transaction executor and wasm executor. Also there was missing coverage feature in scrypto-test project.
Additionally I've fixed scrypto coverage command memory error which was caused by using coverage feature and Rustflags on 1st compilation stage (schema extract).

compiler_builder.coverage();
}

Expand All @@ -42,7 +72,7 @@ impl Compile {

#[cfg(feature = "coverage")]
// Check if binary exists in coverage directory, if it doesn't only then build it
{
if _use_coverage {
let mut coverage_path = compiler.target_binary_path();
if coverage_path.is_file() {
let code = fs::read(&coverage_path).unwrap_or_else(|err| {
Expand Down Expand Up @@ -95,3 +125,192 @@ impl Compile {
}
}
}

#[cfg(test)]
mod tests {
use super::{Compile, CompileProfile};
use std::process::Command;

fn compile_blueprint(additional_args: &[&str]) -> Vec<u8> {
// Build `scrypto` cli
Command::new("cargo")
.arg("build")
.arg("--release")
.arg("--bin")
.arg("scrypto")
.current_dir(concat!(env!("CARGO_MANIFEST_DIR"), "/../radix-clis"))
.output()
.inspect_err(|e| println!("Scrypto cli build failed: {}", e))
.unwrap();

// Run `scrypto build` for example blueprit
Command::new(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../radix-clis/target/release/scrypto"
))
.arg("build")
.args(additional_args)
.current_dir(concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/blueprints/tuple-return"
))
.output()
.inspect_err(|e| println!("Blueprint compilation falied: {}", e))
.unwrap();

let output_file = concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/blueprints/target/wasm32-unknown-unknown/release/tuple_return.wasm"
);
std::fs::read(output_file)
.inspect_err(|e| println!("Failed to load file: {}, error: {}", output_file, e))
.unwrap()
}

#[test]
fn validate_compile_profile_standard() {
// Compile blueprint using `scrypto compile` command
let output_file_content = compile_blueprint(&[]);

// Compile same blueprint using Compile object
let (bin, _) = Compile::compile(
concat!(env!("CARGO_MANIFEST_DIR"), "/tests/blueprints/tuple-return"),
CompileProfile::Standard,
);

// Assert
assert_eq!(
output_file_content.len(),
bin.len(),
"Wasm files should have same size."
);
assert_eq!(
output_file_content, bin,
"Wasm files should have same content."
)
}

#[test]
fn validate_compile_profile_standard_with_logs() {
// Compile blueprint using `scrypto compile` command
let output_file_content = compile_blueprint(&[]);

// Compile same blueprint using Compile object
let (bin, _) = Compile::compile(
concat!(env!("CARGO_MANIFEST_DIR"), "/tests/blueprints/tuple-return"),
CompileProfile::StandardWithTraceLogs,
);

// Assert
assert!(
output_file_content.len() < bin.len(),
"Size of Wasm file compiled by `scrypto build` command should be smaller."
);
}

#[test]
fn validate_compile_profile_fast() {
// Compile blueprint using `scrypto compile` command
let output_file_content = compile_blueprint(&[]);

// Compile same blueprint using Compile object
let (bin, _) = Compile::compile(
concat!(env!("CARGO_MANIFEST_DIR"), "/tests/blueprints/tuple-return"),
CompileProfile::Fast,
);

// Assert
assert!(
output_file_content.len() < bin.len(),
"Size of Wasm file compiled by `scrypto build` command should be smaller."
);
}

#[test]
fn validate_compile_profile_fast_with_logs() {
// Compile blueprint using `scrypto compile` command
let output_file_content = compile_blueprint(&[]);

// Compile same blueprint using Compile object
let (bin, _) = Compile::compile(
concat!(env!("CARGO_MANIFEST_DIR"), "/tests/blueprints/tuple-return"),
CompileProfile::FastWithTraceLogs,
);

// Assert
assert!(
output_file_content.len() < bin.len(),
"Size of Wasm file compiled by `scrypto build` command should be smaller."
);
}

#[test]
fn verify_scrypto_build_with_args_for_compile_profile_standard_with_logs() {
// Compile blueprint using `scrypto compile` command
let output_file_content = compile_blueprint(&["--log-level", "TRACE"]);

// Compile same blueprint using Compile object
let (bin, _) = Compile::compile(
concat!(env!("CARGO_MANIFEST_DIR"), "/tests/blueprints/tuple-return"),
CompileProfile::StandardWithTraceLogs,
);

// Assert
assert_eq!(
output_file_content.len(),
bin.len(),
"Wasm files should have same size."
);
assert_eq!(
output_file_content, bin,
"Wasm files should have same content."
)
}

#[test]
fn verify_scrypto_build_with_args_for_compile_profile_fast() {
// Compile blueprint using `scrypto compile` command
let output_file_content = compile_blueprint(&["--disable-wasm-opt"]);

// Compile same blueprint using Compile object
let (bin, _) = Compile::compile(
concat!(env!("CARGO_MANIFEST_DIR"), "/tests/blueprints/tuple-return"),
CompileProfile::Fast,
);

// Assert
assert_eq!(
output_file_content.len(),
bin.len(),
"Wasm files should have same size."
);
assert_eq!(
output_file_content, bin,
"Wasm files should have same content."
)
}

#[test]
fn verify_scrypto_build_with_args_for_compile_profile_fast_with_logs() {
// Compile blueprint using `scrypto compile` command
let output_file_content =
compile_blueprint(&["--disable-wasm-opt", "--log-level", "TRACE"]);

// Compile same blueprint using Compile object
let (bin, _) = Compile::compile(
concat!(env!("CARGO_MANIFEST_DIR"), "/tests/blueprints/tuple-return"),
CompileProfile::FastWithTraceLogs,
);

// Assert
assert_eq!(
output_file_content.len(),
bin.len(),
"Wasm files should have same size."
);
assert_eq!(
output_file_content, bin,
"Wasm files should have same content."
)
}
}
6 changes: 4 additions & 2 deletions scrypto-test/src/ledger_simulator/ledger_simulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1238,7 +1238,7 @@ impl<E: NativeVmExtension, D: TestDatabase> LedgerSimulator<E, D> {
}

pub fn compile<P: AsRef<Path>>(&mut self, package_dir: P) -> (Vec<u8>, PackageDefinition) {
Compile::compile(package_dir)
Compile::compile(package_dir, CompileProfile::FastWithTraceLogs)
}
mstrug-rdx marked this conversation as resolved.
Show resolved Hide resolved

// Doesn't need to be here - kept for backward compatibility
Expand Down Expand Up @@ -2614,7 +2614,9 @@ impl From<(Vec<u8>, PackageDefinition)> for PackagePublishingSource {
impl PackagePublishingSource {
pub fn code_and_definition(self) -> (Vec<u8>, PackageDefinition) {
match self {
Self::CompileAndPublishFromSource(path) => Compile::compile(path),
Self::CompileAndPublishFromSource(path) => {
mstrug-rdx marked this conversation as resolved.
Show resolved Hide resolved
Compile::compile(path, CompileProfile::FastWithTraceLogs)
}
Self::PublishExisting(code, definition) => (code, definition),
}
}
Expand Down