Skip to content

Commit

Permalink
framework: move framework compiling functionality to another crate
Browse files Browse the repository at this point in the history
  • Loading branch information
bmwill committed May 23, 2022
1 parent 95a947f commit dce2488
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 144 deletions.
14 changes: 13 additions & 1 deletion 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ members = [
"crates/sui-config",
"crates/sui-faucet",
"crates/sui-framework",
"crates/sui-framework-build",
"crates/sui-gateway",
"crates/sui-json",
"crates/sui-network",
Expand Down
19 changes: 19 additions & 0 deletions crates/sui-framework-build/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "sui-framework-build"
version = "0.0.0"
edition = "2021"
authors = ["Mysten Labs <[email protected]>"]
description = "Move framework for sui platform"
license = "Apache-2.0"
publish = false

[dependencies]

sui-types = { path = "../sui-types" }
sui-verifier = { path = "../../crates/sui-verifier" }

move-binary-format = { git = "https://github.com/move-language/move", rev = "1b2d3b4274345f5b4b6a1a1bde5aee452003ab5b" }
move-bytecode-verifier = { git = "https://github.com/move-language/move", rev = "1b2d3b4274345f5b4b6a1a1bde5aee452003ab5b" }
move-compiler = { git = "https://github.com/move-language/move", rev = "1b2d3b4274345f5b4b6a1a1bde5aee452003ab5b" }
move-core-types = { git = "https://github.com/move-language/move", rev = "1b2d3b4274345f5b4b6a1a1bde5aee452003ab5b", features = ["address20"] }
move-package = { git = "https://github.com/move-language/move", rev = "1b2d3b4274345f5b4b6a1a1bde5aee452003ab5b" }
122 changes: 122 additions & 0 deletions crates/sui-framework-build/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use move_binary_format::CompiledModule;
use move_compiler::compiled_unit::{CompiledUnit, NamedCompiledModule};
use move_core_types::{account_address::AccountAddress, ident_str, language_storage::ModuleId};
use move_package::BuildConfig;
use std::{collections::HashSet, path::Path};
use sui_types::error::{SuiError, SuiResult};
use sui_verifier::verifier as sui_bytecode_verifier;

pub fn build_sui_framework_modules(lib_dir: &Path) -> SuiResult<Vec<CompiledModule>> {
let modules = build_framework(lib_dir)?;
verify_modules(&modules)?;
Ok(modules)
}

pub fn build_move_stdlib_modules(lib_dir: &Path) -> SuiResult<Vec<CompiledModule>> {
let denylist = vec![
ident_str!("Capability").to_owned(),
ident_str!("Event").to_owned(),
ident_str!("GUID").to_owned(),
#[cfg(not(test))]
ident_str!("Debug").to_owned(),
];
let modules: Vec<CompiledModule> = build_framework(lib_dir)?
.into_iter()
.filter(|m| !denylist.contains(&m.self_id().name().to_owned()))
.collect();
verify_modules(&modules)?;
Ok(modules)
}

pub fn verify_modules(modules: &[CompiledModule]) -> SuiResult {
for m in modules {
move_bytecode_verifier::verify_module(m).map_err(|err| {
SuiError::ModuleVerificationFailure {
error: err.to_string(),
}
})?;
sui_bytecode_verifier::verify_module(m)?;
}
Ok(())
// TODO(https://github.com/MystenLabs/sui/issues/69): Run Move linker
}

pub fn build_framework(framework_dir: &Path) -> SuiResult<Vec<CompiledModule>> {
let build_config = BuildConfig {
dev_mode: false,
..Default::default()
};
build_move_package(framework_dir, build_config, true)
}

/// Given a `path` and a `build_config`, build the package in that path.
/// If we are building the Sui framework, `is_framework` will be true;
/// Otherwise `is_framework` should be false (e.g. calling from client).
pub fn build_move_package(
path: &Path,
build_config: BuildConfig,
is_framework: bool,
) -> SuiResult<Vec<CompiledModule>> {
match build_config.compile_package(path, &mut Vec::new()) {
Err(error) => Err(SuiError::ModuleBuildFailure {
error: error.to_string(),
}),
Ok(package) => {
let compiled_modules = package.root_modules_map();
if !is_framework {
if let Some(m) = compiled_modules
.iter_modules()
.iter()
.find(|m| m.self_id().address() != &AccountAddress::ZERO)
{
return Err(SuiError::ModulePublishFailure {
error: format!(
"Modules must all have 0x0 as their addresses. Violated by module {:?}",
m.self_id()
),
});
}
}
// Collect all module IDs from the current package to be
// published (module names are not sufficient as we may
// have modules with the same names in user code and in
// Sui framework which would result in the latter being
// pulled into a set of modules to be published).
// For each transitive dependent module, if they are not to be published,
// they must have a non-zero address (meaning they are already published on-chain).
// TODO: Shall we also check if they are really on-chain in the future?
let self_modules: HashSet<ModuleId> = compiled_modules
.iter_modules()
.iter()
.map(|m| m.self_id())
.collect();
if let Some(m) =
package
.deps_compiled_units
.iter()
.find_map(|(_, unit)| match &unit.unit {
CompiledUnit::Module(NamedCompiledModule { module: m, .. })
if !self_modules.contains(&m.self_id())
&& m.self_id().address() == &AccountAddress::ZERO =>
{
Some(m)
}
_ => None,
})
{
return Err(SuiError::ModulePublishFailure { error: format!("Dependent modules must have been published on-chain with non-0 addresses, unlike module {:?}", m.self_id()) });
}
Ok(package
.all_modules_map()
.compute_dependency_graph()
.compute_topological_order()
.unwrap()
.filter(|m| self_modules.contains(&m.self_id()))
.cloned()
.collect())
}
}
}
5 changes: 2 additions & 3 deletions crates/sui-framework/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ base64 = "0.13.0"
smallvec = "1.8.0"
num_enum = "0.5.7"

sui-types = { path = "../../crates/sui-types" }
sui-verifier = { path = "../../crates/sui-verifier" }
sui-types = { path = "../sui-types" }
sui-framework-build = { path = "../sui-framework-build" }

move-binary-format = { git = "https://github.com/move-language/move", rev = "1b2d3b4274345f5b4b6a1a1bde5aee452003ab5b" }
move-bytecode-verifier = { git = "https://github.com/move-language/move", rev = "1b2d3b4274345f5b4b6a1a1bde5aee452003ab5b" }
move-cli = { git = "https://github.com/move-language/move", rev = "1b2d3b4274345f5b4b6a1a1bde5aee452003ab5b" }
move-compiler = { git = "https://github.com/move-language/move", rev = "1b2d3b4274345f5b4b6a1a1bde5aee452003ab5b" }
move-core-types = { git = "https://github.com/move-language/move", rev = "1b2d3b4274345f5b4b6a1a1bde5aee452003ab5b", features = ["address20"] }
move-package = { git = "https://github.com/move-language/move", rev = "1b2d3b4274345f5b4b6a1a1bde5aee452003ab5b" }
move-stdlib = { git = "https://github.com/move-language/move", rev = "1b2d3b4274345f5b4b6a1a1bde5aee452003ab5b" }
Expand Down
Loading

0 comments on commit dce2488

Please sign in to comment.