Skip to content

Commit

Permalink
Add build_move_package API
Browse files Browse the repository at this point in the history
  • Loading branch information
lxfind committed Jan 17, 2022
1 parent a64d252 commit 730a6af
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 10 deletions.
12 changes: 12 additions & 0 deletions fastx_programmability/examples/Move.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "Examples"
version = "0.0.1"

[dependencies]
FastX = { local = "../framework" }

[addresses]
Examples = "0x0"

[dev-addresses]
Examples = "0x0"
File renamed without changes.
File renamed without changes.
File renamed without changes.
85 changes: 75 additions & 10 deletions fastx_programmability/framework/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// Copyright (c) Mysten Labs
// SPDX-License-Identifier: Apache-2.0

use fastx_types::error::{FastPayError, FastPayResult};
use fastx_verifier::verifier as fastx_bytecode_verifier;
use move_binary_format::CompiledModule;
use move_core_types::ident_str;
use move_core_types::{account_address::AccountAddress, ident_str};
use move_package::BuildConfig;
use std::path::PathBuf;
use std::collections::HashSet;
use std::path::{Path, PathBuf};

pub mod natives;

Expand All @@ -14,7 +16,7 @@ pub mod natives;
const MAX_UNIT_TEST_INSTRUCTIONS: u64 = 100_000;

pub fn get_fastx_framework_modules() -> Vec<CompiledModule> {
let modules = build(".");
let modules = build_framework(".");
veirfy_modules(&modules);
modules
}
Expand All @@ -25,14 +27,67 @@ pub fn get_move_stdlib_modules() -> Vec<CompiledModule> {
ident_str!("Event").to_owned(),
ident_str!("GUID").to_owned(),
];
let modules: Vec<CompiledModule> = build("deps/move-stdlib")
let modules: Vec<CompiledModule> = build_framework("deps/move-stdlib")
.into_iter()
.filter(|m| !denylist.contains(&m.self_id().name().to_owned()))
.collect();
veirfy_modules(&modules);
modules
}

/// Given a `path` and a `build_config`, build the package in that path.
/// If we are building the FastX 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,
) -> FastPayResult<Vec<CompiledModule>> {
match build_config.compile_package(path, &mut Vec::new()) {
Err(error) => Err(FastPayError::ModuleBuildFailure {
error: error.to_string(),
}),
Ok(package) => {
let compiled_modules = package.compiled_modules();
if !is_framework {
if let Some(m) = compiled_modules
.iter_modules()
.iter()
.find(|m| m.self_id().address() != &AccountAddress::ZERO)
{
return Err(FastPayError::ModulePublishFailure {
error: format!(
"Modules must all have 0x0 as their addresses. Violated by module {:?}",
m.self_id()
),
});
}
// Collect all module names from the current package to be published.
// For each transistive 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<String> = compiled_modules
.iter_modules()
.iter()
.map(|m| m.self_id().name().to_string())
.collect();
if let Some(m) = package
.transitive_compiled_modules()
.iter_modules()
.iter()
.find(|m| {
!self_modules.contains(m.self_id().name().as_str())
&& m.self_id().address() == &AccountAddress::ZERO
})
{
return Err(FastPayError::ModulePublishFailure { error: format!("Denpendent modules must have already been published on-chain with non-0 addresses. Violated by module {:?}", m.self_id()) });
}
}
Ok(compiled_modules.iter_modules_owned())
}
}
}

fn veirfy_modules(modules: &[CompiledModule]) {
for m in modules {
move_bytecode_verifier::verify_module(m).unwrap();
Expand All @@ -41,23 +96,33 @@ fn veirfy_modules(modules: &[CompiledModule]) {
}
}

fn build(sub_dir: &str) -> Vec<CompiledModule> {
fn build_framework(sub_dir: &str) -> Vec<CompiledModule> {
let mut framework_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
framework_dir.push(sub_dir);
let build_config = BuildConfig {
dev_mode: false,
..Default::default()
};
build_config
.compile_package(&framework_dir, &mut Vec::new())
.unwrap()
.compiled_modules()
.iter_modules_owned()
build_move_package(&framework_dir, build_config, true).unwrap()
}

#[cfg(test)]
fn get_examples() -> Vec<CompiledModule> {
let mut framework_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
framework_dir.push("../examples/");
let build_config = BuildConfig {
dev_mode: false,
..Default::default()
};
let modules = build_move_package(&framework_dir, build_config, true).unwrap();
veirfy_modules(&modules);
modules
}

#[test]
fn check_that_move_code_can_be_built_verified_tested() {
get_fastx_framework_modules();
get_examples();
}

#[test]
Expand Down
2 changes: 2 additions & 0 deletions fastx_types/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ pub enum FastPayError {
ModuleDeserializationFailure { error: String },
#[error("Failed to publish the Move module(s), reason: {error:?}.")]
ModulePublishFailure { error: String },
#[error("FAiled to build Move modules")]
ModuleBuildFailure { error: String },

// Move call related errors
#[error("Function resolution failure: {error:?}.")]
Expand Down

0 comments on commit 730a6af

Please sign in to comment.