Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-mysten committed Feb 26, 2025
1 parent 8e3b8a6 commit 7b3494c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 39 deletions.
61 changes: 33 additions & 28 deletions crates/sui/src/client_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@ impl SuiClientCommands {
check_dep_verification_flags(skip_dependency_verification, verify_deps)?;

let upgrade_result = upgrade_package(
client.read_api(),
&client,
build_config.clone(),
&package_path,
upgrade_capability,
Expand All @@ -954,16 +954,9 @@ impl SuiClientCommands {
)?;
}

let (upgrade_policy, mut compiled_package) =
let (upgrade_policy, compiled_package) =
upgrade_result.map_err(|e| anyhow!("{e}"))?;

filter_deps(
&client,
with_unpublished_dependencies,
&mut compiled_package,
)
.await?;

let compiled_modules =
compiled_package.get_package_bytes(with_unpublished_dependencies);
let package_id = compiled_package.published_at.clone()?;
Expand Down Expand Up @@ -1070,7 +1063,7 @@ impl SuiClientCommands {
check_dep_verification_flags(skip_dependency_verification, verify_deps)?;

let compile_result = compile_package(
client.read_api(),
&client,
build_config.clone(),
&package_path,
with_unpublished_dependencies,
Expand All @@ -1087,13 +1080,7 @@ impl SuiClientCommands {
)?;
}

let mut compiled_package = compile_result?;
filter_deps(
&client,
with_unpublished_dependencies,
&mut compiled_package,
)
.await?;
let compiled_package = compile_result?;
let compiled_modules =
compiled_package.get_package_bytes(with_unpublished_dependencies);
let dep_ids = compiled_package.get_published_dependencies_ids();
Expand Down Expand Up @@ -1134,6 +1121,7 @@ impl SuiClientCommands {
package_path,
build_config,
} => {
let client = context.get_client().await?;
let protocol_version =
protocol_version.map_or(ProtocolVersion::MAX, ProtocolVersion::new);
let protocol_config =
Expand Down Expand Up @@ -1161,7 +1149,9 @@ impl SuiClientCommands {

(_, package_path) => {
let package_path = package_path.unwrap_or_else(|| PathBuf::from("."));
let package = compile_package_simple(build_config, &package_path, None)?;
let package =
compile_package_simple(&client, build_config, &package_path, None)
.await?;
let name = package
.package
.compiled_package_info
Expand Down Expand Up @@ -1775,7 +1765,8 @@ fn check_dep_verification_flags(
}
}

fn compile_package_simple(
async fn compile_package_simple(
client: &SuiClient,
build_config: MoveBuildConfig,
package_path: &Path,
chain_id: Option<String>,
Expand All @@ -1787,29 +1778,38 @@ fn compile_package_simple(
chain_id: chain_id.clone(),
};
let resolution_graph = config.resolution_graph(package_path, chain_id.clone())?;
let compiled_package = build_from_resolution_graph(resolution_graph, false, false, chain_id)?;
let mut compiled_package =
build_from_resolution_graph(resolution_graph, false, false, chain_id)?;
pkg_tree_shake(&client, false, &mut compiled_package).await?;

Ok(compiled_package)
}

pub(crate) async fn upgrade_package(
read_api: &ReadApi,
client: &SuiClient,
build_config: MoveBuildConfig,
package_path: &Path,
upgrade_capability: ObjectID,
with_unpublished_dependencies: bool,
skip_dependency_verification: bool,
env_alias: Option<String>,
) -> Result<(u8, CompiledPackage), anyhow::Error> {
let compiled_package = compile_package(
read_api,
let mut compiled_package = compile_package(
client,
build_config,
package_path,
with_unpublished_dependencies,
skip_dependency_verification,
)
.await?;

pkg_tree_shake(
&client,
with_unpublished_dependencies,
&mut compiled_package,
)
.await?;

compiled_package.published_at.as_ref().map_err(|e| match e {
PublishedAtError::NotPresent => {
anyhow!("No 'published-at' field in Move.toml or 'published-id' in Move.lock for package to be upgraded.")
Expand All @@ -1836,7 +1836,8 @@ pub(crate) async fn upgrade_package(
}
})?;

let resp = read_api
let resp = client
.read_api()
.get_object_with_options(
upgrade_capability,
SuiObjectDataOptions::default().with_bcs().with_owner(),
Expand Down Expand Up @@ -1864,12 +1865,13 @@ pub(crate) async fn upgrade_package(
}

pub(crate) async fn compile_package(
read_api: &ReadApi,
client: &SuiClient,
build_config: MoveBuildConfig,
package_path: &Path,
with_unpublished_dependencies: bool,
skip_dependency_verification: bool,
) -> Result<CompiledPackage, anyhow::Error> {
let read_api = client.read_api();
let config = resolve_lock_file_path(build_config, Some(package_path))?;
let run_bytecode_verifier = true;
let print_diags_to_stderr = true;
Expand All @@ -1886,12 +1888,15 @@ pub(crate) async fn compile_package(
if !with_unpublished_dependencies {
check_unpublished_dependencies(&dependencies.unpublished)?;
};
let compiled_package = build_from_resolution_graph(
let mut compiled_package = build_from_resolution_graph(
resolution_graph,
run_bytecode_verifier,
print_diags_to_stderr,
chain_id,
)?;

pkg_tree_shake(client, with_unpublished_dependencies, &mut compiled_package).await?;

let protocol_config = read_api.get_protocol_config(None).await?;

// Check that the package's Move version is compatible with the chain's
Expand Down Expand Up @@ -3133,12 +3138,12 @@ pub async fn fetch_move_packages(
}

/// Filter out a package's dependencies which are not referenced in the source code.
async fn filter_deps(
async fn pkg_tree_shake(
client: &SuiClient,
with_unpublished_dependencies: bool,
compiled_package: &mut CompiledPackage,
) -> Result<(), anyhow::Error> {
let pkgs = compiled_package.find_deps_pkgs_to_keep(with_unpublished_dependencies)?;
let pkgs = compiled_package.find_immediate_deps_pkgs_to_keep(with_unpublished_dependencies)?;
let pkg_ids = pkgs
.clone()
.into_iter()
Expand Down
22 changes: 12 additions & 10 deletions crates/sui/src/client_ptb/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use std::{collections::BTreeMap, path::Path};
use sui_json::{is_receiving_argument, primitive_type};
use sui_json_rpc_types::{SuiObjectData, SuiObjectDataOptions, SuiRawData};
use sui_move::manage_package::resolve_lock_file_path;
use sui_sdk::apis::ReadApi;
use sui_sdk::SuiClient;
use sui_types::{
base_types::{is_primitive_type_tag, ObjectID, TxContext, TxContextKind},
move_package::MovePackage,
Expand Down Expand Up @@ -223,7 +223,7 @@ pub struct PTBBuilder<'a> {
/// transaction arguments.
resolved_arguments: BTreeMap<String, Tx::Argument>,
/// Read API for reading objects from chain. Needed for object resolution.
reader: &'a ReadApi,
client: &'a SuiClient,
/// The last command that we have added. This is used to support assignment commands.
last_command: Option<Tx::Argument>,
/// The actual PTB that we are building up.
Expand Down Expand Up @@ -275,14 +275,14 @@ impl ArgWithHistory {
}

impl<'a> PTBBuilder<'a> {
pub fn new(starting_env: BTreeMap<String, AccountAddress>, reader: &'a ReadApi) -> Self {
pub fn new(starting_env: BTreeMap<String, AccountAddress>, client: &'a SuiClient) -> Self {
Self {
addresses: starting_env,
identifiers: BTreeMap::new(),
arguments_to_resolve: BTreeMap::new(),
resolved_arguments: BTreeMap::new(),
ptb: ProgrammableTransactionBuilder::new(),
reader,
client,
last_command: None,
errors: Vec::new(),
}
Expand Down Expand Up @@ -412,7 +412,8 @@ impl<'a> PTBBuilder<'a> {
loc: Span,
) -> PTBResult<MovePackage> {
let object = self
.reader
.client
.read_api()
.get_object_with_options(package_id, SuiObjectDataOptions::bcs_lossless())
.await
.map_err(|e| err!(loc, "{e}"))?
Expand Down Expand Up @@ -763,7 +764,8 @@ impl<'a> PTBBuilder<'a> {
/// Fetch the `SuiObjectData` for an object ID -- this is used for object resolution.
async fn get_object(&self, object_id: ObjectID, obj_loc: Span) -> PTBResult<SuiObjectData> {
let res = self
.reader
.client
.read_api()
.get_object_with_options(
object_id,
SuiObjectDataOptions::new().with_type().with_owner(),
Expand Down Expand Up @@ -931,7 +933,7 @@ impl<'a> PTBBuilder<'a> {
self.last_command = Some(res);
}
ParsedPTBCommand::Publish(sp!(pkg_loc, package_path)) => {
let chain_id = self.reader.get_chain_identifier().await.ok();
let chain_id = self.client.read_api().get_chain_identifier().await.ok();
let build_config = BuildConfig::default();
let package_path = Path::new(&package_path);
let build_config = resolve_lock_file_path(build_config.clone(), Some(package_path))
Expand All @@ -958,7 +960,7 @@ impl<'a> PTBBuilder<'a> {
.map_err(|e| err!(pkg_loc, "{e}"))?;
}
let compiled_package = compile_package(
self.reader,
self.client,
build_config.clone(),
package_path,
false, /* with_unpublished_dependencies */
Expand Down Expand Up @@ -999,7 +1001,7 @@ impl<'a> PTBBuilder<'a> {
)
.await?;

let chain_id = self.reader.get_chain_identifier().await.ok();
let chain_id = self.client.read_api().get_chain_identifier().await.ok();
let build_config = BuildConfig::default();
let package_path = Path::new(&package_path);
let build_config = resolve_lock_file_path(build_config.clone(), Some(package_path))
Expand Down Expand Up @@ -1027,7 +1029,7 @@ impl<'a> PTBBuilder<'a> {
}

let (upgrade_policy, compiled_package) = upgrade_package(
self.reader,
self.client,
build_config.clone(),
package_path,
ObjectID::from_address(upgrade_cap_id.into_inner()),
Expand Down
2 changes: 1 addition & 1 deletion crates/sui/src/client_ptb/ptb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ impl PTB {
.into_iter()
.map(|(sa, alias)| (alias.alias.clone(), AccountAddress::from(*sa)))
.collect();
let builder = PTBBuilder::new(starting_addresses, client.read_api());
let builder = PTBBuilder::new(starting_addresses, &client);
builder.build(program).await
}

Expand Down

0 comments on commit 7b3494c

Please sign in to comment.