Skip to content

Commit

Permalink
[sui move] Add BuildConfig. Use move's package command structure. (My…
Browse files Browse the repository at this point in the history
…stenLabs#2912)

- Added build config to the build and test commands
- Used the cli's command structure
- Removed the std flag and instead had the behavior branch on the package name
  • Loading branch information
tnowacki authored Jul 1, 2022
1 parent db0ee7a commit fe65c67
Show file tree
Hide file tree
Showing 24 changed files with 252 additions and 152 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions crates/generate-json-rpc-spec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ sui-types = { path = "../sui-types" }
sui-config = { path = "../sui-config" }
test-utils = { path = "../test-utils" }
workspace-hack = { path = "../workspace-hack"}

move-package = { git = "https://github.com/move-language/move", rev = "ae62d5f1955a9b92c3ddd31d3cc4467f9aff76ae" }
15 changes: 13 additions & 2 deletions crates/generate-json-rpc-spec/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use clap::ArgEnum;
use clap::Parser;
use hyper::body::Buf;
use hyper::{Body, Client, Method, Request};
use move_package::BuildConfig;
use pretty_assertions::assert_str_eq;
use serde::Serialize;
use serde_json::{json, Map, Value};
Expand Down Expand Up @@ -186,8 +187,13 @@ async fn create_response_sample() -> Result<
async fn create_package_object_response(
context: &mut WalletContext,
) -> Result<(GetObjectDataResponse, TransactionResponse), anyhow::Error> {
let package_path = ["sui_programmability", "examples", "move_tutorial"]
.into_iter()
.collect();
let build_config = BuildConfig::default();
let result = SuiClientCommands::Publish {
path: "sui_programmability/examples/move_tutorial".to_string(),
package_path,
build_config,
gas: None,
gas_budget: 10000,
}
Expand Down Expand Up @@ -237,9 +243,14 @@ async fn create_hero_response(
coins: &[SuiObjectInfo],
) -> Result<(ObjectID, GetObjectDataResponse), anyhow::Error> {
// Create hero response
let package_path = ["sui_programmability", "examples", "games"]
.into_iter()
.collect();
let build_config = BuildConfig::default();
let result = SuiClientCommands::Publish {
path: "sui_programmability/examples/games".to_string(),
package_path,
gas: None,
build_config,
gas_budget: 10000,
}
.execute(context)
Expand Down
7 changes: 2 additions & 5 deletions crates/sui-config/src/genesis_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,8 @@ impl GenesisConfig {
);

for path in &self.move_packages {
let mut modules = sui_framework::build_move_package(
path,
move_package::BuildConfig::default(),
false,
)?;
let mut modules =
sui_framework::build_move_package(path, move_package::BuildConfig::default())?;

let package_id =
sui_adapter::adapter::generate_package_id(&mut modules, genesis_ctx)?;
Expand Down
7 changes: 5 additions & 2 deletions crates/sui-core/src/unit_tests/gateway_state_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use move_package::BuildConfig;
use serde_json::json;
use std::{collections::HashSet, path::Path};

Expand Down Expand Up @@ -140,7 +141,8 @@ async fn test_publish() {
let mut path = env!("CARGO_MANIFEST_DIR").to_owned();
path.push_str("/src/unit_tests/data/object_owner/");

let compiled_modules = build_move_package_to_bytes(Path::new(&path), false).unwrap();
let compiled_modules =
build_move_package_to_bytes(Path::new(&path), BuildConfig::default()).unwrap();
let data = gateway
.publish(
addr1,
Expand Down Expand Up @@ -533,7 +535,8 @@ async fn test_get_owner_object() {
path.push_str("/src/unit_tests/data/object_owner/");

// Publish object_owner package
let compiled_modules = build_move_package_to_bytes(Path::new(&path), false).unwrap();
let compiled_modules =
build_move_package_to_bytes(Path::new(&path), BuildConfig::default()).unwrap();
let data = gateway
.publish(
addr1,
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-core/src/unit_tests/move_integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ pub async fn build_and_try_publish_test_package(
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.push("src/unit_tests/data/");
path.push(test_dir);
let modules = sui_framework::build_move_package(&path, build_config, false).unwrap();
let modules = sui_framework::build_move_package(&path, build_config).unwrap();

let all_module_bytes = modules
.iter()
Expand Down
26 changes: 8 additions & 18 deletions crates/sui-framework-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ use std::{collections::HashSet, path::Path};
use sui_types::error::{SuiError, SuiResult};
use sui_verifier::verifier as sui_bytecode_verifier;

pub fn build_sui_framework_modules(lib_dir: &Path) -> SuiResult<Vec<CompiledModule>> {
let modules = build_framework(lib_dir)?;
verify_modules(&modules)?;
Ok(modules)
}
const SUI_PACKAGE_NAME: &str = "Sui";
const MOVE_STDLIB_PACKAGE_NAME: &str = "MoveStdlib";

pub fn build_move_stdlib_modules(lib_dir: &Path) -> SuiResult<Vec<CompiledModule>> {
let denylist = vec![
Expand All @@ -23,7 +20,8 @@ pub fn build_move_stdlib_modules(lib_dir: &Path) -> SuiResult<Vec<CompiledModule
#[cfg(not(test))]
ident_str!("debug").to_owned(),
];
let modules: Vec<CompiledModule> = build_framework(lib_dir)?
let build_config = BuildConfig::default();
let modules: Vec<CompiledModule> = build_move_package(lib_dir, build_config)?
.into_iter()
.filter(|m| !denylist.contains(&m.self_id().name().to_owned()))
.collect();
Expand All @@ -43,29 +41,21 @@ pub fn verify_modules(modules: &[CompiledModule]) -> SuiResult {
Ok(())
// TODO(https://github.com/MystenLabs/sui/issues/69): Run Move linker
}

pub fn build_framework(framework_dir: &Path) -> SuiResult<Vec<CompiledModule>> {
let build_config = BuildConfig {
dev_mode: false,
..Default::default()
};
build_move_package(framework_dir, build_config, true)
}

/// Given a `path` and a `build_config`, build the package in that path.
/// If we are building the Sui framework, `is_framework` will be true;
/// Otherwise `is_framework` should be false (e.g. calling from client).
/// If we are building the Sui framework, we skip the check that the addresses should be 0
pub fn build_move_package(
path: &Path,
build_config: BuildConfig,
is_framework: bool,
) -> SuiResult<Vec<CompiledModule>> {
match build_config.compile_package_no_exit(path, &mut Vec::new()) {
Err(error) => Err(SuiError::ModuleBuildFailure {
error: error.to_string(),
}),
Ok(package) => {
let compiled_modules = package.root_modules_map();
let package_name = package.compiled_package_info.package_name.as_str();
let is_framework =
package_name == SUI_PACKAGE_NAME || package_name == MOVE_STDLIB_PACKAGE_NAME;
if !is_framework {
if let Some(m) = compiled_modules
.iter_modules()
Expand Down
2 changes: 2 additions & 0 deletions crates/sui-framework/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ license = "Apache-2.0"
publish = false

[dependencies]
anyhow = { version = "1.0.58", features = ["backtrace"] }
bcs = "0.1.3"
smallvec = "1.8.0"
num_enum = "0.5.7"
Expand All @@ -32,6 +33,7 @@ anyhow = { version = "1.0.58", features = ["backtrace"] }
bcs = "0.1.3"
sui-framework-build = { path = "../sui-framework-build" }
move-binary-format = { git = "https://github.com/move-language/move", rev = "ae62d5f1955a9b92c3ddd31d3cc4467f9aff76ae" }
move-package = { git = "https://github.com/move-language/move", rev = "ae62d5f1955a9b92c3ddd31d3cc4467f9aff76ae" }

[package.metadata.cargo-udeps.ignore]
normal = ["move-cli", "move-unit-test"]
4 changes: 3 additions & 1 deletion crates/sui-framework/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use anyhow::Result;
use move_binary_format::CompiledModule;
use move_package::BuildConfig;
use std::{
env, fs,
path::{Path, PathBuf},
Expand All @@ -15,8 +16,9 @@ fn main() {
let sui_framwork_path = Path::new(env!("CARGO_MANIFEST_DIR"));
let move_stdlib_path = Path::new(env!("CARGO_MANIFEST_DIR")).join("deps/move-stdlib");

let sui_build_config = BuildConfig::default();
let sui_framework =
sui_framework_build::build_sui_framework_modules(sui_framwork_path).unwrap();
sui_framework_build::build_move_package(sui_framwork_path, sui_build_config).unwrap();
let move_stdlib = sui_framework_build::build_move_stdlib_modules(&move_stdlib_path).unwrap();

serialize_modules_to_file(sui_framework, &out_dir.join("sui-framework")).unwrap();
Expand Down
77 changes: 34 additions & 43 deletions crates/sui-framework/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

use move_binary_format::CompiledModule;
use move_cli::package::cli::UnitTestResult;
use move_package::BuildConfig;
use move_unit_test::UnitTestingConfig;
use num_enum::TryFromPrimitive;
Expand All @@ -12,8 +13,7 @@ use sui_types::error::{SuiError, SuiResult};
pub mod natives;

pub use sui_framework_build::build_move_stdlib_modules as get_move_stdlib_modules;
pub use sui_framework_build::build_sui_framework_modules as get_sui_framework_modules;
pub use sui_framework_build::{build_framework, build_move_package, verify_modules};
pub use sui_framework_build::{build_move_package, verify_modules};
use sui_types::sui_serde::{Base64, Encoding};

// Move unit tests will halt after executing this many steps. This is a protection to avoid divergence
Expand Down Expand Up @@ -75,32 +75,21 @@ pub enum EventType {

/// Given a `path` and a `build_config`, build the package in that path and return the compiled modules as base64.
/// This is useful for when publishing via JSON
/// If we are building the Sui framework, `is_framework` will be true;
/// Otherwise `is_framework` should be false (e.g. calling from client).
pub fn build_move_package_to_base64(
path: &Path,
is_framework: bool,
build_config: BuildConfig,
) -> Result<Vec<String>, SuiError> {
build_move_package_to_bytes(path, is_framework)
build_move_package_to_bytes(path, build_config)
.map(|mods| mods.iter().map(Base64::encode).collect::<Vec<_>>())
}

/// Given a `path` and a `build_config`, build the package in that path and return the compiled modules as Vec<Vec<u8>>.
/// This is useful for when publishing
/// If we are building the Sui framework, `is_framework` will be true;
/// Otherwise `is_framework` should be false (e.g. calling from client).
pub fn build_move_package_to_bytes(
path: &Path,
is_framework: bool,
build_config: BuildConfig,
) -> Result<Vec<Vec<u8>>, SuiError> {
build_move_package(
path,
BuildConfig {
..Default::default()
},
is_framework,
)
.map(|mods| {
build_move_package(path, build_config).map(|mods| {
mods.iter()
.map(|m| {
let mut bytes = Vec::new();
Expand All @@ -111,44 +100,36 @@ pub fn build_move_package_to_bytes(
})
}

pub fn build_and_verify_user_package(path: &Path) -> SuiResult<Vec<CompiledModule>> {
let build_config = BuildConfig {
dev_mode: false,
..Default::default()
};
let modules = build_move_package(path, build_config, false)?;
pub fn build_and_verify_package(
path: &Path,
build_config: BuildConfig,
) -> SuiResult<Vec<CompiledModule>> {
let modules = build_move_package(path, build_config)?;
verify_modules(&modules)?;
Ok(modules)
}

pub fn run_move_unit_tests(path: &Path, config: Option<UnitTestingConfig>) -> SuiResult {
use move_cli::package::cli::{self, UnitTestResult};
pub fn run_move_unit_tests(
path: &Path,
build_config: BuildConfig,
config: Option<UnitTestingConfig>,
compute_coverage: bool,
) -> anyhow::Result<UnitTestResult> {
use sui_types::{MOVE_STDLIB_ADDRESS, SUI_FRAMEWORK_ADDRESS};

let config = config
.unwrap_or_else(|| UnitTestingConfig::default_with_bound(Some(MAX_UNIT_TEST_INSTRUCTIONS)));

let result = cli::run_move_unit_tests(
move_cli::package::cli::run_move_unit_tests(
path,
BuildConfig::default(),
build_config,
UnitTestingConfig {
report_stacktrace_on_abort: true,
instruction_execution_bound: MAX_UNIT_TEST_INSTRUCTIONS,
..config
},
natives::all_natives(MOVE_STDLIB_ADDRESS, SUI_FRAMEWORK_ADDRESS),
/* compute_coverage */ false,
compute_coverage,
)
.map_err(|err| SuiError::MoveUnitTestFailure {
error: format!("{:?}", err),
})?;
if result == UnitTestResult::Failure {
Err(SuiError::MoveUnitTestFailure {
error: "Test failed".to_string(),
})
} else {
Ok(())
}
}

#[cfg(test)]
Expand All @@ -160,8 +141,18 @@ mod tests {
fn run_framework_move_unit_tests() {
get_sui_framework();
get_move_stdlib();
get_sui_framework_modules(&PathBuf::from(DEFAULT_FRAMEWORK_PATH)).unwrap();
run_move_unit_tests(Path::new(env!("CARGO_MANIFEST_DIR")), None).unwrap();
build_move_package(
&PathBuf::from(DEFAULT_FRAMEWORK_PATH),
BuildConfig::default(),
)
.unwrap();
run_move_unit_tests(
Path::new(env!("CARGO_MANIFEST_DIR")),
BuildConfig::default(),
None,
false,
)
.unwrap();
}

#[test]
Expand All @@ -179,8 +170,8 @@ mod tests {
let path = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../sui_programmability/examples")
.join(example);
build_and_verify_user_package(&path).unwrap();
run_move_unit_tests(&path, None).unwrap();
build_and_verify_package(&path, BuildConfig::default()).unwrap();
run_move_unit_tests(&path, BuildConfig::default(), None, false).unwrap();
}
}
}
3 changes: 2 additions & 1 deletion crates/sui-gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ sui-node = { path = "../sui-node" }

mysten-network = { git = "https://github.com/MystenLabs/mysten-infra", rev = "94d7da89f6a52d7f60a9802b0a03147a9c89c3e4" }
workspace-hack = { path = "../workspace-hack"}
move-package = { git = "https://github.com/move-language/move", rev = "ae62d5f1955a9b92c3ddd31d3cc4467f9aff76ae" }

[dev-dependencies]
test-utils = { path = "../test-utils" }
sui-framework = { path = "../sui-framework" }

[[bin]]
name = "rpc-server"
path = "src/main.rs"
path = "src/main.rs"
3 changes: 2 additions & 1 deletion crates/sui-gateway/src/unit_tests/rpc_server_tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use move_package::BuildConfig;
use std::{path::Path, str::FromStr};
use sui_config::SUI_KEYSTORE_FILENAME;
use sui_core::gateway_state::GatewayTxSeqNumber;
Expand Down Expand Up @@ -82,7 +83,7 @@ async fn test_publish() -> Result<(), anyhow::Error> {

let compiled_modules = build_move_package_to_bytes(
Path::new("../../sui_programmability/examples/fungible_tokens"),
false,
BuildConfig::default(),
)?
.iter()
.map(|bytes| Base64::from_bytes(bytes))
Expand Down
Loading

0 comments on commit fe65c67

Please sign in to comment.