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

mpm: release support init function and cleanup #3196

Merged
merged 1 commit into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/starcoin/src/dev/compile_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ impl CommandAction for CompileCommand {
&self,
ctx: &ExecContext<Self::State, Self::GlobalOpt, Self::Opt>,
) -> Result<Self::ReturnItem> {
eprintln!("WARNING: the command is deprecated in favor of move-package-manager, will be removed in next release.");
let sender = if let Some(sender) = ctx.opt().sender {
sender
} else {
Expand Down
1 change: 1 addition & 0 deletions cmd/starcoin/src/dev/package_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ impl CommandAction for PackageCmd {
&self,
ctx: &ExecContext<Self::State, Self::GlobalOpt, Self::Opt>,
) -> Result<Self::ReturnItem> {
eprintln!("WARNING: the command is deprecated in favor of move-package-manager, will be removed in next release.");
let opt = ctx.opt();
let mv_file_or_dir = opt.mv_file_or_dir.as_path();
ensure!(
Expand Down
21 changes: 3 additions & 18 deletions rpc/api/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use starcoin_types::U256;
use starcoin_vm_types::access_path::AccessPath;
use starcoin_vm_types::block_metadata::BlockMetadata;
use starcoin_vm_types::identifier::Identifier;
use starcoin_vm_types::language_storage::{FunctionId, ModuleId, StructTag};
use starcoin_vm_types::language_storage::{parse_module_id, FunctionId, ModuleId, StructTag};
use starcoin_vm_types::parser::{parse_transaction_argument, parse_type_tag};
use starcoin_vm_types::sign_message::SignedMessage;
use starcoin_vm_types::transaction::authenticator::AccountPublicKey;
Expand Down Expand Up @@ -1403,16 +1403,7 @@ impl std::fmt::Display for FunctionIdView {
impl FromStr for FunctionIdView {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let splits: Vec<&str> = s.rsplitn(2, "::").collect();
if splits.len() != 2 {
anyhow::bail!("invalid script function id");
}
let module_id = ModuleIdView::from_str(splits[1])?;
let function = Identifier::new(splits[0])?;
Ok(StrView(FunctionId {
module: module_id.0,
function,
}))
Ok(Self(FunctionId::from_str(s)?))
}
}

Expand All @@ -1426,13 +1417,7 @@ impl FromStr for StrView<ModuleId> {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let parts: Vec<_> = s.split("::").collect();
if parts.len() != 2 {
anyhow::bail!("invalid module id");
}
let module_addr = parts[0].parse::<AccountAddress>()?;
let module_name = Identifier::new(parts[1])?;
Ok(Self(ModuleId::new(module_addr, module_name)))
Ok(Self(parse_module_id(s)?))
}
}

Expand Down
2 changes: 2 additions & 0 deletions vm/move-package-manager/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod releasement;
use anyhow::Result;
use move_cli::Move;
use move_command_line_common::testing::UPDATE_BASELINE;
Expand All @@ -13,6 +14,7 @@ use once_cell::sync::Lazy;
use std::path::PathBuf;
use std::sync::Mutex;
use structopt::StructOpt;

#[derive(StructOpt, Debug)]
pub struct TransactionalTestCommand {
#[structopt(flatten)]
Expand Down
76 changes: 3 additions & 73 deletions vm/move-package-manager/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,15 @@
// SPDX-License-Identifier: Apache-2.0

use anyhow::Result;
use move_binary_format::file_format_common::VERSION_3;
use move_cli::package::cli::handle_package_commands;
use move_cli::sandbox::utils::PackageContext;
use move_cli::{experimental, package, sandbox, Move, DEFAULT_STORAGE_DIR};
use move_core_types::errmap::ErrorMapping;

use move_binary_format::CompiledModule;
use move_compiler::compiled_unit::{CompiledUnit, NamedCompiledModule};
use move_package_manager::releasement::{handle_release, Releasement};
use move_package_manager::{run_transactional_test, TransactionalTestCommand};
use starcoin_move_compiler::bytecode_transpose::ModuleBytecodeDowgrader;
use starcoin_vm_runtime::natives::starcoin_natives;
use starcoin_vm_types::transaction::{Module, Package};
use std::path::PathBuf;
use structopt::StructOpt;

pub const DEFAULT_RELEASE_DIR: &str = "release";
#[derive(StructOpt)]
pub struct CliOptions {
#[structopt(flatten)]
Expand All @@ -38,16 +31,7 @@ pub enum Commands {
},
/// Release the package.
#[structopt(name = "release")]
Release {
#[structopt(name = "move-version", long = "move-version", default_value="3", possible_values=&["3", "4"])]
/// specify the move lang version for the release.
/// currently, only v3, v4 are supported.
language_version: u8,

#[structopt(name="release-dir", long, parse(from_os_str), default_value=DEFAULT_RELEASE_DIR)]
/// dir to store released blob
release_dir: PathBuf,
},
Release(Releasement),
/// Execute a sandbox command.
#[structopt(name = "sandbox")]
Sandbox {
Expand Down Expand Up @@ -81,12 +65,6 @@ fn main() -> Result<()> {
let move_args = &args.move_args;
let natives = starcoin_natives();
match args.cmd {
// Commands::Command(cmd) => move_cli::run_cli(
// starcoin_natives(),
// &error_descriptions,
// &args.move_args,
// &cmd,
// ),
Commands::TransactionalTest(cmd) => run_transactional_test(args.move_args, cmd),
Commands::Package { cmd } => handle_package_commands(
&move_args.package_path,
Expand All @@ -98,54 +76,6 @@ fn main() -> Result<()> {
cmd.handle_command(natives, &error_descriptions, move_args, &storage_dir)
}
Commands::Experimental { storage_dir, cmd } => cmd.handle_command(move_args, &storage_dir),
Commands::Release {
language_version,
mut release_dir,
} => {
let mut ms = vec![];
let pkg_ctx = PackageContext::new(&move_args.package_path, &move_args.build_config)?;
let pkg = pkg_ctx.package();
let pkg_version = move_args
.build_config
.clone()
.resolution_graph_for_package(&move_args.package_path)
.unwrap()
.root_package
.package
.version;
let pkg_name = pkg.compiled_package_info.package_name.as_str();
println!("Packaging Modules:");
for m in pkg.modules()? {
let m = module(&m.unit)?;
println!("\t {}", m.self_id());
let code = if language_version as u32 == VERSION_3 {
ModuleBytecodeDowgrader::to_v3(m)?
} else {
let mut data = vec![];
m.serialize(&mut data)?;
data
};
ms.push(Module::new(code));
}
let p = Package::new(ms, None)?;
let package_bytes = bcs_ext::to_bytes(&p)?;
let release_path = {
std::fs::create_dir_all(&release_dir)?;
release_dir.push(format!(
"{}.v{}.{}.{}.blob",
pkg_name, pkg_version.0, pkg_version.1, pkg_version.2
));
release_dir
};
std::fs::write(&release_path, package_bytes)?;
println!("Release done: {}", release_path.display());
Ok(())
}
}
}
fn module(unit: &CompiledUnit) -> Result<&CompiledModule> {
match unit {
CompiledUnit::Module(NamedCompiledModule { module, .. }) => Ok(module),
_ => anyhow::bail!("Found script in modules -- this shouldn't happen"),
Commands::Release(releasement) => handle_release(move_args, releasement),
}
}
117 changes: 117 additions & 0 deletions vm/move-package-manager/src/releasement.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
use move_binary_format::file_format_common::VERSION_3;
use move_binary_format::CompiledModule;
use move_cli::sandbox::utils::PackageContext;
use move_cli::Move;
use move_compiler::compiled_unit::{CompiledUnit, NamedCompiledModule};
use move_core_types::language_storage::TypeTag;
use move_core_types::transaction_argument::{convert_txn_args, TransactionArgument};
use starcoin_move_compiler::bytecode_transpose::ModuleBytecodeDowgrader;
use starcoin_types::transaction::parse_transaction_argument;
use starcoin_vm_types::language_storage::FunctionId;
use starcoin_vm_types::parser::parse_type_tag;
use starcoin_vm_types::transaction::{Module, Package, ScriptFunction};
use std::path::PathBuf;
use structopt::StructOpt;

pub const DEFAULT_RELEASE_DIR: &str = "release";

#[derive(StructOpt)]
pub struct Releasement {
#[structopt(name = "move-version", long = "move-version", default_value="3", possible_values=&["3", "4"])]
/// specify the move lang version for the release.
/// currently, only v3, v4 are supported.
language_version: u8,

#[structopt(name="release-dir", long, parse(from_os_str), default_value=DEFAULT_RELEASE_DIR)]
/// dir to store released blob
release_dir: PathBuf,

#[structopt(long = "function", name = "script-function")]
/// init script function to execute, example: 0x123::MyScripts::init_script
init_script: Option<FunctionId>,

#[structopt(
short = "t",
long = "type_tag",
name = "type-tag",
parse(try_from_str = parse_type_tag)
)]
/// type tags for the init script function
type_tags: Option<Vec<TypeTag>>,

#[structopt(long = "arg", name = "transaction-args", parse(try_from_str = parse_transaction_argument))]
/// args for the init script function
args: Option<Vec<TransactionArgument>>,
}

pub fn handle_release(
move_args: &Move,
Releasement {
language_version,
mut release_dir,
init_script,
type_tags,
args,
}: Releasement,
) -> anyhow::Result<()> {
let mut ms = vec![];
let pkg_ctx = PackageContext::new(&move_args.package_path, &move_args.build_config)?;
let pkg = pkg_ctx.package();
let pkg_version = move_args
.build_config
.clone()
.resolution_graph_for_package(&move_args.package_path)
.unwrap()
.root_package
.package
.version;
let pkg_name = pkg.compiled_package_info.package_name.as_str();
println!("Packaging Modules:");
for m in pkg.modules()? {
let m = module(&m.unit)?;
println!("\t {}", m.self_id());
let code = if language_version as u32 == VERSION_3 {
ModuleBytecodeDowgrader::to_v3(m)?
} else {
let mut data = vec![];
m.serialize(&mut data)?;
data
};
ms.push(Module::new(code));
}
let init_script = match &init_script {
Some(script) => {
let type_tags = type_tags.unwrap_or_default();
let args = args.unwrap_or_default();
let script_function = script.clone();
Some(ScriptFunction::new(
script_function.module,
script_function.function,
type_tags,
convert_txn_args(&args),
))
}
None => None,
};

let p = Package::new(ms, init_script)?;
let package_bytes = bcs_ext::to_bytes(&p)?;
let release_path = {
std::fs::create_dir_all(&release_dir)?;
release_dir.push(format!(
"{}.v{}.{}.{}.blob",
pkg_name, pkg_version.0, pkg_version.1, pkg_version.2
));
release_dir
};
std::fs::write(&release_path, package_bytes)?;
println!("Release done: {}", release_path.display());
Ok(())
}

fn module(unit: &CompiledUnit) -> anyhow::Result<&CompiledModule> {
match unit {
CompiledUnit::Module(NamedCompiledModule { module, .. }) => Ok(module),
_ => anyhow::bail!("Found script in modules -- this shouldn't happen"),
}
}
29 changes: 29 additions & 0 deletions vm/types/src/language_storage_ext.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use move_core_types::account_address::AccountAddress;
use move_core_types::identifier::Identifier;
use move_core_types::language_storage::ModuleId;
use std::str::FromStr;

#[derive(Clone, Debug, Eq, Ord, PartialOrd, PartialEq)]
pub struct FunctionId {
Expand All @@ -12,3 +14,30 @@ impl std::fmt::Display for FunctionId {
write!(f, "{}::{}", &self.module, &self.function)
}
}

impl FromStr for FunctionId {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let splits: Vec<&str> = s.rsplitn(2, "::").collect();
if splits.len() != 2 {
anyhow::bail!("invalid script function id");
}
let module_id = parse_module_id(splits[1])?;
let function = Identifier::new(splits[0])?;
Ok(FunctionId {
module: module_id,
function,
})
}
}

pub fn parse_module_id(s: &str) -> Result<ModuleId, anyhow::Error> {
let parts: Vec<_> = s.split("::").collect();
if parts.len() != 2 {
anyhow::bail!("invalid module id");
}
let module_addr = parts[0].parse::<AccountAddress>()?;
let module_name = Identifier::new(parts[1])?;
Ok(ModuleId::new(module_addr, module_name))
}
1 change: 1 addition & 0 deletions vm/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pub mod identifier {
}

pub mod language_storage {
pub use crate::language_storage_ext::parse_module_id;
pub use crate::language_storage_ext::FunctionId;
pub use move_core_types::language_storage::{
ModuleId, ResourceKey, StructTag, TypeTag, CODE_TAG, CORE_CODE_ADDRESS, RESOURCE_TAG,
Expand Down