Skip to content

Commit

Permalink
Fix sui move build --dump-bytecode-as-base64
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-mysten committed Feb 27, 2025
1 parent 4dca648 commit 51cb5b1
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 45 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

22 changes: 1 addition & 21 deletions crates/sui-move/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ use crate::manage_package::resolve_lock_file_path;
use clap::Parser;
use move_cli::base;
use move_package::BuildConfig as MoveBuildConfig;
use serde_json::json;
use std::{fs, path::Path};
use sui_move_build::{check_invalid_dependencies, check_unpublished_dependencies, BuildConfig};
use sui_move_build::BuildConfig;

const LAYOUTS_DIR: &str = "layouts";
const STRUCT_LAYOUTS_FILENAME: &str = "struct_layouts.yaml";
Expand Down Expand Up @@ -52,8 +51,6 @@ impl Build {
Self::execute_internal(
&rerooted_path,
build_config,
self.with_unpublished_dependencies,
self.dump_bytecode_as_base64,
self.generate_struct_layouts,
self.chain_id.clone(),
)
Expand All @@ -62,8 +59,6 @@ impl Build {
pub fn execute_internal(
rerooted_path: &Path,
config: MoveBuildConfig,
with_unpublished_deps: bool,
dump_bytecode_as_base64: bool,
generate_struct_layouts: bool,
chain_id: Option<String>,
) -> anyhow::Result<()> {
Expand All @@ -74,21 +69,6 @@ impl Build {
chain_id,
}
.build(rerooted_path)?;
if dump_bytecode_as_base64 {
check_invalid_dependencies(&pkg.dependency_ids.invalid)?;
if !with_unpublished_deps {
check_unpublished_dependencies(&pkg.dependency_ids.unpublished)?;
}

println!(
"{}",
json!({
"modules": pkg.get_package_base64(with_unpublished_deps),
"dependencies": pkg.get_dependency_storage_package_ids(),
"digest": pkg.get_package_digest(with_unpublished_deps),
})
)
}

if generate_struct_layouts {
let layout_str = serde_yaml::to_string(&pkg.generate_struct_layouts()).unwrap();
Expand Down
1 change: 1 addition & 0 deletions crates/sui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ move-vm-profiler.workspace = true
move-vm-config.workspace = true
move-ir-types.workspace = true
move-command-line-common.workspace = true
move-cli.workspace = true

[target.'cfg(not(target_env = "msvc"))'.dependencies]
jemalloc-ctl.workspace = true
Expand Down
4 changes: 2 additions & 2 deletions crates/sui/src/client_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3109,7 +3109,7 @@ async fn check_protocol_version_and_warn(client: &SuiClient) -> Result<(), anyho
}

/// Fetch move packages based on the provided package IDs.
pub async fn fetch_move_packages(
async fn fetch_move_packages(
client: &SuiClient,
package_ids: &[ObjectID],
) -> Result<Vec<MovePackage>, anyhow::Error> {
Expand All @@ -3133,7 +3133,7 @@ pub async fn fetch_move_packages(
}

/// Filter out a package's dependencies which are not referenced in the source code.
async fn pkg_tree_shake(
pub(crate) async fn pkg_tree_shake(
client: &SuiClient,
with_unpublished_dependencies: bool,
compiled_package: &mut CompiledPackage,
Expand Down
84 changes: 62 additions & 22 deletions crates/sui/src/sui_commands.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use crate::client_commands::SuiClientCommands;
use crate::client_commands::{pkg_tree_shake, SuiClientCommands};
use crate::console::start_console;
use crate::fire_drill::{run_fire_drill, FireDrill};
use crate::genesis_ceremony::{run, Ceremony};
Expand Down Expand Up @@ -43,10 +43,15 @@ use sui_graphql_rpc::{
test_infra::cluster::start_graphql_server_with_fn_rpc,
};

use serde_json::json;
use sui_keys::keypair_file::read_key;
use sui_keys::keystore::{AccountKeystore, FileBasedKeystore, Keystore};
use sui_move::manage_package::resolve_lock_file_path;
use sui_move::{self, execute_move_command};
use sui_move_build::SuiPackageHooks;
use sui_move_build::{
check_invalid_dependencies, check_unpublished_dependencies, BuildConfig as SuiBuildConfig,
SuiPackageHooks,
};
use sui_sdk::sui_client_config::{SuiClientConfig, SuiEnv};
use sui_sdk::wallet_context::WalletContext;
use sui_swarm::memory::Swarm;
Expand Down Expand Up @@ -499,31 +504,66 @@ impl SuiCommand {
SuiCommand::Move {
package_path,
build_config,
mut cmd,
cmd,
config: client_config,
} => {
match &mut cmd {
match cmd {
sui_move::Command::Build(build) if build.dump_bytecode_as_base64 => {
if build.ignore_chain {
build.chain_id = None;
// `sui move build` does not ordinarily require a network connection.
// The exception is when --dump-bytecode-as-base64 is specified: In this
// case, we should resolve the correct addresses for the respective chain
// (e.g., testnet, mainnet) from the Move.lock under automated address management.
// In addition, tree shaking also requires a network as it needs to fetch
// on-chain linkage table of package dependencies.
let config =
client_config.unwrap_or(sui_config_dir()?.join(SUI_CLIENT_CONFIG));
prompt_if_no_config(&config, false).await?;
let context = WalletContext::new(&config, None, None)?;

let Ok(client) = context.get_client().await else {
bail!("`sui move build --dump-bytecode-as-base64` requires a connection to the network. Current active network is {} but failed to connect to it.", context.config.active_env.as_ref().unwrap());
};

if let Err(e) = client.check_api_version() {
eprintln!("{}", format!("[warning] {e}").yellow().bold());
}

let chain_id = if build.ignore_chain {
// for tests it's useful to ignore the chain id!
None
} else {
// `sui move build` does not ordinarily require a network connection.
// The exception is when --dump-bytecode-as-base64 is specified: In this
// case, we should resolve the correct addresses for the respective chain
// (e.g., testnet, mainnet) from the Move.lock under automated address management.
let config =
client_config.unwrap_or(sui_config_dir()?.join(SUI_CLIENT_CONFIG));
prompt_if_no_config(&config, false).await?;
let context = WalletContext::new(&config, None, None)?;
if let Ok(client) = context.get_client().await {
if let Err(e) = client.check_api_version() {
eprintln!("{}", format!("[warning] {e}").yellow().bold());
}
}
let client = context.get_client().await?;
let chain_id = client.read_api().get_chain_identifier().await.ok();
build.chain_id = chain_id.clone();
client.read_api().get_chain_identifier().await.ok()
};

let rerooted_path = move_cli::base::reroot_path(package_path.as_deref())?;
let build_config =
resolve_lock_file_path(build_config, Some(&rerooted_path))?;
let mut pkg = SuiBuildConfig {
config: build_config,
run_bytecode_verifier: true,
print_diags_to_stderr: true,
chain_id,
}
.build(&rerooted_path)?;

let with_unpublished_deps = build.with_unpublished_dependencies;

check_invalid_dependencies(&pkg.dependency_ids.invalid)?;
if !with_unpublished_deps {
check_unpublished_dependencies(&pkg.dependency_ids.unpublished)?;
}

pkg_tree_shake(&client, with_unpublished_deps, &mut pkg).await?;

println!(
"{}",
json!({
"modules": pkg.get_package_base64(with_unpublished_deps),
"dependencies": pkg.get_dependency_storage_package_ids(),
"digest": pkg.get_package_digest(with_unpublished_deps),
})
);
return Ok(());
}
_ => (),
};
Expand Down
14 changes: 14 additions & 0 deletions crates/sui/tests/cli_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4535,5 +4535,19 @@ async fn test_tree_shaking_package_system_deps() -> Result<(), anyhow::Error> {
"Package J should have no dependencies"
);

// sui move build --dump-bytecode-as-base64 should also yield a json with no dependencies
let package_path = test.package_path("J_system_deps");
let binary_path = env!("CARGO_BIN_EXE_sui");
let cmd = std::process::Command::new(binary_path)
.arg("move")
.arg("build")
.arg("--dump-bytecode-as-base64")
.arg(package_path)
.output()
.expect("Failed to execute command");

let output = String::from_utf8_lossy(&cmd.stdout);
assert!(!output.contains("dependencies: []"));

Ok(())
}

0 comments on commit 51cb5b1

Please sign in to comment.