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

[move] Implement and enable on-chain package hooks #19613

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
26 changes: 24 additions & 2 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ members = [
"crates/sui-open-rpc-macros",
"crates/sui-oracle",
"crates/sui-package-dump",
"crates/sui-package-hooks",
"crates/sui-package-management",
"crates/sui-package-resolver",
"crates/sui-pg-temp-db",
Expand Down Expand Up @@ -663,6 +664,7 @@ sui-node = { path = "crates/sui-node" }
sui-open-rpc = { path = "crates/sui-open-rpc" }
sui-open-rpc-macros = { path = "crates/sui-open-rpc-macros" }
sui-package-dump = { path = "crates/sui-package-dump" }
sui-package-hooks = { path = "crates/sui-package-hooks" }
sui-package-management = { path = "crates/sui-package-management" }
sui-package-resolver = { path = "crates/sui-package-resolver" }
sui-pg-temp-db = { path = "crates/sui-pg-temp-db" }
Expand Down
4 changes: 2 additions & 2 deletions crates/sui-core/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use sui_config::node::AuthorityOverloadConfig;
use sui_framework::BuiltInFramework;
use sui_genesis_builder::validator_info::ValidatorInfo;
use sui_macros::nondeterministic;
use sui_move_build::{BuildConfig, CompiledPackage, SuiPackageHooks};
use sui_move_build::{BuildConfig, CompiledPackage, SuiPackageHooksForTesting};
use sui_protocol_config::ProtocolConfig;
use sui_types::base_types::{random_object_ref, ObjectID};
use sui_types::crypto::{
Expand Down Expand Up @@ -199,7 +199,7 @@ pub fn compile_managed_coin_package() -> CompiledPackage {
}

pub fn compile_example_package(relative_path: &str) -> CompiledPackage {
move_package::package_hooks::register_package_hooks(Box::new(SuiPackageHooks));
move_package::package_hooks::register_package_hooks(Box::new(SuiPackageHooksForTesting));
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.push(relative_path);

Expand Down
6 changes: 3 additions & 3 deletions crates/sui-core/src/unit_tests/move_integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use sui_types::{

use move_core_types::language_storage::TypeTag;

use sui_move_build::{BuildConfig, SuiPackageHooks};
use sui_move_build::{BuildConfig, SuiPackageHooksForTesting};
use sui_types::{
crypto::{get_key_pair, AccountKeyPair},
error::SuiError,
Expand Down Expand Up @@ -2802,7 +2802,7 @@ pub fn build_package(
code_dir: &str,
with_unpublished_deps: bool,
) -> (Vec<u8>, Vec<Vec<u8>>, Vec<ObjectID>) {
move_package::package_hooks::register_package_hooks(Box::new(SuiPackageHooks));
move_package::package_hooks::register_package_hooks(Box::new(SuiPackageHooksForTesting));
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.extend(["src", "unit_tests", "data", code_dir]);
let compiled_package = BuildConfig::new_for_testing().build(&path).unwrap();
Expand All @@ -2822,7 +2822,7 @@ pub async fn build_and_try_publish_test_package(
gas_price: u64,
with_unpublished_deps: bool,
) -> (Transaction, SignedTransactionEffects) {
move_package::package_hooks::register_package_hooks(Box::new(SuiPackageHooks));
move_package::package_hooks::register_package_hooks(Box::new(SuiPackageHooksForTesting));
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.extend(["src", "unit_tests", "data", test_dir]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async fn test_manage_package_update() {
# @generated by Move, please check-in and do not edit manually.

[move]
version = 3
version = 4
manifest_digest = "919A5B078B47AD46674F36E1605578927D5BC4536A7646D78D1320A25DDD57CC"
deps_digest = "E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ async fn test_generate_lock_file() {
# @generated by Move, please check-in and do not edit manually.

[move]
version = 3
version = 4
manifest_digest = "4C5606BF71339416027A58BDB5BA2EF2F5E0929FCE98BAB8AFFCBC447AFE3A23"
deps_digest = "3C4103934B1E040BB6B23F1D610B4EF9F2F1166A50A104EADCF77467C004C600"
dependencies = [
Expand Down
8 changes: 4 additions & 4 deletions crates/sui-framework-tests/src/metered_verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::{
use sui_adapter::adapter::run_metered_move_bytecode_verifier;
use sui_config::verifier_signing_config::VerifierSigningConfig;
use sui_framework::BuiltInFramework;
use sui_move_build::{CompiledPackage, SuiPackageHooks};
use sui_move_build::{CompiledPackage, SuiPackageHooksForTesting};
use sui_protocol_config::ProtocolConfig;
use sui_types::{
error::{SuiError, SuiResult},
Expand All @@ -28,7 +28,7 @@ fn build(path: &Path) -> SuiResult<CompiledPackage> {
#[test]
#[cfg_attr(msim, ignore)]
fn test_metered_move_bytecode_verifier() {
move_package::package_hooks::register_package_hooks(Box::new(SuiPackageHooks));
move_package::package_hooks::register_package_hooks(Box::new(SuiPackageHooksForTesting));
let path =
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../sui-framework/packages/sui-framework");
let compiled_package = build(&path).unwrap();
Expand Down Expand Up @@ -230,7 +230,7 @@ fn test_metered_move_bytecode_verifier() {
#[test]
#[cfg_attr(msim, ignore)]
fn test_meter_system_packages() {
move_package::package_hooks::register_package_hooks(Box::new(SuiPackageHooks));
move_package::package_hooks::register_package_hooks(Box::new(SuiPackageHooksForTesting));

let signing_config = VerifierSigningConfig::default();
let protocol_config = ProtocolConfig::get_for_max_version_UNSAFE();
Expand Down Expand Up @@ -286,7 +286,7 @@ fn test_meter_system_packages() {
#[test]
#[cfg_attr(msim, ignore)]
fn test_build_and_verify_programmability_examples() {
move_package::package_hooks::register_package_hooks(Box::new(SuiPackageHooks));
move_package::package_hooks::register_package_hooks(Box::new(SuiPackageHooksForTesting));

let signing_config = VerifierSigningConfig::default();
let protocol_config = ProtocolConfig::get_for_max_version_UNSAFE();
Expand Down
4 changes: 2 additions & 2 deletions crates/sui-framework/tests/build-system-packages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::{
env, fs,
path::{Path, PathBuf},
};
use sui_move_build::{BuildConfig, SuiPackageHooks};
use sui_move_build::{BuildConfig, SuiPackageHooksForTesting};

const CRATE_ROOT: &str = env!("CARGO_MANIFEST_DIR");
const COMPILED_PACKAGES_DIR: &str = "packages_compiled";
Expand All @@ -19,7 +19,7 @@ const PUBLISHED_API_FILE: &str = "published_api.txt";

#[test]
fn build_system_packages() {
move_package::package_hooks::register_package_hooks(Box::new(SuiPackageHooks));
move_package::package_hooks::register_package_hooks(Box::new(SuiPackageHooksForTesting));
let tempdir = tempfile::tempdir().unwrap();
let out_dir = if std::env::var_os("UPDATE").is_some() {
let crate_root = Path::new(CRATE_ROOT);
Expand Down
4 changes: 2 additions & 2 deletions crates/sui-json-rpc-tests/tests/balance_changes_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

use std::path::PathBuf;
use sui_move_build::{BuildConfig, SuiPackageHooks};
use sui_move_build::{BuildConfig, SuiPackageHooksForTesting};
use sui_sdk::SuiClient;
use sui_types::programmable_transaction_builder::ProgrammableTransactionBuilder;
use sui_types::transaction::{TransactionData, TransactionKind};
Expand All @@ -17,7 +17,7 @@ async fn test_dry_run_publish_with_mocked_coin() -> Result<(), anyhow::Error> {
let client: SuiClient = context.get_client().await.unwrap();

// Publish test coin package
move_package::package_hooks::register_package_hooks(Box::new(SuiPackageHooks));
move_package::package_hooks::register_package_hooks(Box::new(SuiPackageHooksForTesting));
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.extend(["tests", "data", "dummy_modules_publish"]);
let compiled_package = BuildConfig::default().build(&path)?;
Expand Down
13 changes: 9 additions & 4 deletions crates/sui-move-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub struct BuildConfig {

impl BuildConfig {
pub fn new_for_testing() -> Self {
move_package::package_hooks::register_package_hooks(Box::new(SuiPackageHooks));
move_package::package_hooks::register_package_hooks(Box::new(SuiPackageHooksForTesting));
let mut build_config: Self = Default::default();
let install_dir = tempfile::tempdir().unwrap().into_path();
let lock_file = install_dir.join("Move.lock");
Expand Down Expand Up @@ -620,9 +620,9 @@ impl GetModule for CompiledPackage {

pub const PUBLISHED_AT_MANIFEST_FIELD: &str = "published-at";

pub struct SuiPackageHooks;
pub struct SuiPackageHooksForTesting;

impl PackageHooks for SuiPackageHooks {
impl PackageHooks for SuiPackageHooksForTesting {
fn custom_package_info_fields(&self) -> Vec<String> {
vec![
PUBLISHED_AT_MANIFEST_FIELD.to_string(),
Expand All @@ -642,6 +642,7 @@ impl PackageHooks for SuiPackageHooks {
fn custom_resolve_pkg_id(
&self,
manifest: &SourceManifest,
_: Option<&str>,
) -> anyhow::Result<PackageIdentifier> {
if (!cfg!(debug_assertions) || cfg!(test))
&& manifest.package.edition == Some(Edition::DEVELOPMENT)
Expand All @@ -651,7 +652,11 @@ impl PackageHooks for SuiPackageHooks {
Ok(manifest.package.name)
}

fn resolve_version(&self, _: &SourceManifest) -> anyhow::Result<Option<Symbol>> {
fn resolve_version(
&self,
_: &SourceManifest,
_: Option<&str>,
) -> anyhow::Result<Option<Symbol>> {
Ok(None)
}
}
Expand Down
5 changes: 4 additions & 1 deletion crates/sui-move-lsp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ tokio = { workspace = true, features = ["full"] }

bin-version.workspace = true
move-analyzer.workspace = true

sui.workspace = true
sui-sdk.workspace = true
sui-config.workspace = true
sui-package-hooks.workspace = true
16 changes: 15 additions & 1 deletion crates/sui-move-lsp/src/bin/move-analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

use clap::*;
use move_analyzer::analyzer;
use sui_config::{sui_config_dir, SUI_CLIENT_CONFIG};
use sui_package_hooks::package_hooks::SuiPackageHooks;
use sui_sdk::wallet_context::WalletContext;

// Define the `GIT_REVISION` and `VERSION` consts
bin_version::bin_version!();
Expand All @@ -16,7 +19,18 @@ bin_version::bin_version!();
)]
struct App {}

fn main() {
fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = sui_config_dir()?.join(SUI_CLIENT_CONFIG);
let context = WalletContext::new(&config, None, None)?;
let rt = tokio::runtime::Runtime::new().expect("Failed to create Tokio runtime");
rt.block_on(async {
SuiPackageHooks::register_from_ctx(&context, 1000)
.await
.unwrap();
});

App::parse();
analyzer::run();

Ok(())
}
24 changes: 24 additions & 0 deletions crates/sui-package-hooks/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "sui-package-hooks"
version.workspace = true
authors = ["Mysten Labs <build@mystenlabs.com>"]
license = "Apache-2.0"
publish = false
edition = "2021"

[lints]
workspace = true

[dependencies]
anyhow.workspace = true
lru.workspace = true
move-binary-format.workspace = true
move-compiler.workspace = true
move-core-types.workspace = true
move-package.workspace = true
move-symbol-pool.workspace = true
sui-json-rpc-types.workspace = true
sui-move-build.workspace = true
sui-sdk.workspace = true
sui-types.workspace = true
tokio.workspace = true
5 changes: 5 additions & 0 deletions crates/sui-package-hooks/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

pub mod package_cache;
pub mod package_hooks;
Loading
Loading