Skip to content

Commit

Permalink
move: manage-package command updates Move.lock info (#17886)
Browse files Browse the repository at this point in the history
## Description 

We have a `sui move manage-package` command to manually update the
`Move.lock` file with managed address info. This PR implements the
command to record the [final
schema](https://sourcegraph.com/github.com/MystenLabs/sui@35ea4206f568951e99e58054093d5e72d825c2f4/-/blob/external-crates/move/crates/move-package/src/lock_file/schema.rs?L99-109)
we arrived at (in particular, includes the addition of `environment`).

An example full invocation is:

```bash
sui move manage-package --environment mainnet \
                        --network-id '35834a8a' \
                        --original-id '0xa' \
                        --latest-id '0xb' \
                        --version-number 1
```

We need this command because it will be used in a next PR for
user-friendly error messages, that instruct the user to try this command
when some things can go wrong.

This command will be documented once the next PR(s) land, which actually
use the contents---there is no reason for a user to need to run this
currently.

## Test plan 

Added test.

---

## Release notes

Check each box that your changes affect. If none of the boxes relate to
your changes, release notes aren't required.

For each box you select, include information after the relevant heading
that describes the impact of your changes that a user might notice and
any actions they must take to implement updates.

- [ ] Protocol: 
- [ ] Nodes (Validators and Full nodes): 
- [ ] Indexer: 
- [ ] JSON-RPC: 
- [ ] GraphQL: 
- [ ] CLI: 
- [ ] Rust SDK:
  • Loading branch information
rvantonder authored May 23, 2024
1 parent 8b7216f commit c2cc23b
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 6 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.

1 change: 1 addition & 0 deletions crates/sui-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ move-symbol-pool.workspace = true

sui-test-transaction-builder.workspace = true
sui-types = { workspace = true, features = ["test-utils"] }
sui-move.workspace = true

[target.'cfg(not(target_env = "msvc"))'.dev-dependencies]
pprof.workspace = true
Expand Down
3 changes: 3 additions & 0 deletions crates/sui-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ pub mod verify_indexes;
#[path = "unit_tests/congestion_control_tests.rs"]
mod congestion_control_tests;
#[cfg(test)]
#[path = "unit_tests/move_package_management_tests.rs"]
mod move_package_management_tests;
#[cfg(test)]
#[path = "unit_tests/move_package_publish_tests.rs"]
mod move_package_publish_tests;
#[cfg(test)]
Expand Down
2 changes: 2 additions & 0 deletions crates/sui-core/src/unit_tests/data/basic_no_deps/Move.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[package]
name = "test"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

module 0x1::M {
public fun foo() { }
}
71 changes: 71 additions & 0 deletions crates/sui-core/src/unit_tests/move_package_management_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use expect_test::expect;
use std::{fs::File, io::Read, path::PathBuf};

use sui_move::manage_package::ManagePackage;
use sui_move_build::BuildConfig;
use sui_types::base_types::ObjectID;

#[tokio::test]
async fn test_manage_package_update() {
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.extend(["src", "unit_tests", "data", "basic_no_deps"]);

let tmp = tempfile::tempdir().expect("Could not create temp dir for Move.lock");
let lock_file_path = tmp.path().join("Move.lock");

let mut build_config = BuildConfig::new_for_testing();
build_config.config.lock_file = Some(lock_file_path.clone());
build_config
.clone()
.build(path.clone())
.expect("Move package did not build");
// Update the lock file with placeholder compiler version so this isn't bumped every release.
build_config
.config
.update_lock_file_toolchain_version(&path, "0.0.1".into())
.expect("Could not update lock file");

let original_id = ObjectID::from_hex_literal("0xa").unwrap();
let latest_id = ObjectID::from_hex_literal("0xb").unwrap();

let manage_package = ManagePackage {
environment: "mainnet".to_string(),
chain_id: "12345".to_string(),
original_id,
latest_id,
version_number: 5,
};
let _ = manage_package.execute(Some(lock_file_path.clone()), build_config.config);

let mut lock_file_contents = String::new();
File::open(lock_file_path)
.expect("Cannot open lock file")
.read_to_string(&mut lock_file_contents)
.expect("Error reading Move.lock file");

let expected = expect![[r#"
# @generated by Move, please check-in and do not edit manually.
[move]
version = 2
manifest_digest = "919A5B078B47AD46674F36E1605578927D5BC4536A7646D78D1320A25DDD57CC"
deps_digest = "E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855"
[move.toolchain-version]
compiler-version = "0.0.1"
edition = "legacy"
flavor = "sui"
[env]
[env.mainnet]
chain-id = "12345"
original-published-id = "0x000000000000000000000000000000000000000000000000000000000000000a"
latest-published-id = "0x000000000000000000000000000000000000000000000000000000000000000b"
published-version = "5"
"#]];
expected.assert_eq(lock_file_contents.as_str());
}
65 changes: 59 additions & 6 deletions crates/sui-move/src/manage_package.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,84 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use anyhow::bail;
use clap::Parser;
use move_package::BuildConfig;
use std::path::PathBuf;

use move_package::{
lock_file::{self, LockFile},
BuildConfig,
};
use sui_types::base_types::ObjectID;

/// Record addresses (Object IDs) for where this package is published on chain (this command sets variables in Move.lock).
use crate::build::resolve_lock_file_path;

const NO_LOCK_FILE: &str = "Expected a `Move.lock` file to exist in the package path, \
but none found. Consider running `sui move build` to \
generate the `Move.lock` file in the package directory.";

/// Record addresses (Object IDs) for where this package is published on chain (this command sets variables in
/// Move.lock).
#[derive(Parser)]
#[group(id = "sui-move-manage-package")]
pub struct ManagePackage {
#[clap(long)]
/// The environment to associate this package information with (consider using `sui client active-env`).
pub environment: String,
#[clap(long = "network-id")]
/// The network chain identifer. Use '35834a8a' for mainnet.
pub network: String,
pub chain_id: String,
#[clap(long = "original-id", value_parser = ObjectID::from_hex_literal)]
/// The original address (Object ID) where this package is published.
pub original_id: ObjectID,
#[clap(long = "latest-id", value_parser = ObjectID::from_hex_literal)]
/// The most recent address (Object ID) where this package is published. It is the same as 'original-id' if the package is immutable and published once. It is different from 'original-id' if the package has been upgraded to a different address.
/// The most recent address (Object ID) where this package is published. It is the same as 'original-id' if the
/// package is immutable and published once. It is different from 'original-id' if the package has been upgraded to
/// a different address.
pub latest_id: ObjectID,
#[clap(long = "version-number")]
/// The version number of the published package. It is '1' if the package is immutable and published once. It is some number greater than '1' if the package has been upgraded once or more.
/// The version number of the published package. It is '1' if the package is immutable and published once. It is
/// some number greater than '1' if the package has been upgraded once or more.
pub version_number: u64,
}

impl ManagePackage {
pub fn execute(self, _path: Option<PathBuf>, _build_config: BuildConfig) -> anyhow::Result<()> {
pub fn execute(
self,
package_path: Option<PathBuf>,
build_config: BuildConfig,
) -> anyhow::Result<()> {
let build_config = resolve_lock_file_path(build_config, package_path)?;
let Some(lock_file) = build_config.lock_file else {
bail!(NO_LOCK_FILE)
};
if !lock_file.exists() {
bail!(NO_LOCK_FILE)
};
let install_dir = build_config.install_dir.unwrap_or(PathBuf::from("."));
let mut lock = LockFile::from(install_dir.clone(), &lock_file)?;

// Updating managed packages in the Move.lock file is controlled by distinct `Published` and `Upgraded`
// commands. To set all relevant values, we run both commands. First use the `Published` update to set the
// environment, chain ID, and original ID.
lock_file::schema::update_managed_address(
&mut lock,
&self.environment,
lock_file::schema::ManagedAddressUpdate::Published {
chain_id: self.chain_id,
original_id: self.original_id.to_string(),
},
)?;
// Next use the `Upgraded` update to subsequently set the latest ID and version.
lock_file::schema::update_managed_address(
&mut lock,
&self.environment,
lock_file::schema::ManagedAddressUpdate::Upgraded {
latest_id: self.latest_id.to_string(),
version: self.version_number,
},
)?;
lock.commit(lock_file)?;
Ok(())
}
}

0 comments on commit c2cc23b

Please sign in to comment.