Skip to content
This repository has been archived by the owner on Jul 28, 2023. It is now read-only.

updated MetaData #141

Merged
merged 2 commits into from
Sep 10, 2021
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
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions dove/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::index::Index;
use crate::manifest::{default_dialect, DoveToml, MANIFEST, read_manifest};
use diem_crypto::hash::CryptoHash;
use crate::index::interface::{InterfaceBuilder, Interface};
use crate::metadata::MapDependencies;

/// Project context.
pub struct Context {
Expand Down Expand Up @@ -63,6 +64,7 @@ impl Context {
new_index.store(&index_path)?;
new_index.remove_unused(old_index.diff(&new_index))?;
new_index.remove_unnecessary_elements_in_dependencies();
MapDependencies::create_and_save(self)?;
new_index
};
let builder = InterfaceBuilder::new(self, &index);
Expand Down Expand Up @@ -102,18 +104,19 @@ impl Context {

/// Returns interface files dir.
pub fn interface_files_dir(&self) -> PathBuf {
self.path_for(&self.manifest.layout.artifacts)
self.path_for(&self.manifest.layout.system_folder)
.join("interface_files_dir")
}

/// Returns directory for dependency bytecode.
pub fn deps_mv_dir(&self) -> PathBuf {
self.path_for(&self.manifest.layout.artifacts).join("depmv")
self.path_for(&self.manifest.layout.system_folder)
.join("depmv")
}

/// Interface files lock.
pub fn interface_files_lock(&self) -> PathBuf {
self.path_for(&self.manifest.layout.artifacts)
self.path_for(&self.manifest.layout.system_folder)
.join("interface.lock")
}
}
Expand Down
2 changes: 2 additions & 0 deletions dove/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ pub mod executor;
pub mod index;
/// Dove configuration.
pub mod manifest;
/// Metadata from project
pub mod metadata;
/// StdOut stream
pub mod stdout;
#[doc(hidden)]
Expand Down
22 changes: 21 additions & 1 deletion dove/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ fn artifacts() -> String {
}

fn index() -> String {
format!("{}{}{}", artifacts(), MS, ".DoveIndex.toml")
format!("{}{}{}", system_folder(), MS, ".DoveIndex.toml")
}

fn storage_dir() -> String {
Expand All @@ -144,6 +144,14 @@ fn code_code_address() -> String {
"0x1".to_string()
}

fn system_folder() -> String {
format!("{}{}{}", artifacts(), MS, ".system")
}

fn project_map() -> String {
format!("{}{}{}", system_folder(), MS, "project.map")
}

/// Project layout.
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq)]
pub struct Layout {
Expand Down Expand Up @@ -203,6 +211,14 @@ pub struct Layout {
/// Path to pover settings
#[serde(default = "prover_toml")]
pub prover_toml: String,

/// Path t
#[serde(default = "system_folder")]
pub system_folder: String,

/// Path to project map
#[serde(default = "project_map")]
pub project_map: String,
}

impl Layout {
Expand All @@ -225,6 +241,8 @@ impl Layout {
storage_dir: ctx.str_path_for(&self.storage_dir)?,
exe_build_dir: ctx.str_path_for(&self.exe_build_dir)?,
prover_toml: ctx.str_path_for(&self.prover_toml)?,
system_folder: ctx.str_path_for(&self.system_folder)?,
project_map: ctx.str_path_for(&self.project_map)?,
})
}
}
Expand All @@ -248,6 +266,8 @@ impl Default for Layout {
storage_dir: storage_dir(),
exe_build_dir: exe_build_dir(),
prover_toml: prover_toml(),
system_folder: system_folder(),
project_map: project_map(),
}
}
}
Expand Down
148 changes: 148 additions & 0 deletions dove/src/metadata.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
use std::fs;
use std::fs::{create_dir_all, read_to_string};
use anyhow::Error;
use serde::{Deserialize, Serialize};
use lang::compiler::metadata::{Unit, parse, FuncMeta, ModuleMeta};
use lang::compiler::file::is_move_file;
use crate::context::Context;
use std::path::PathBuf;
use crate::manifest::DoveToml;

/// All modules and scripts from dependencies
pub(crate) fn get_all_dependencies(ctx: &Context) -> Result<Vec<Unit>, Error> {
let external_folder = ctx.path_for(&ctx.manifest.layout.deps);
if !external_folder.exists() {
return Ok(Vec::new());
}

let sender = ctx.account_address_str()?;
let dialect = ctx.dialect.as_ref();

let externals = external_folder
.read_dir()?
.filter_map(|path| path.ok())
.map(|path| path.path())
.filter(|path| {
path.is_dir()
&& path
.file_name()
.and_then(|name| name.to_str())
.unwrap_or_default()
.starts_with("git_")
})
.map(test_folder)
.map(|(project_path, test_path)| {
move_files_without_tests(project_path, test_path.as_ref())
})
.flatten()
.collect::<Vec<_>>();

let meta = externals
.iter()
.filter_map(|path| parse(&path.to_string_lossy(), dialect, &sender).ok())
.flatten()
.collect();

Ok(meta)
}

fn test_folder(path: PathBuf) -> (PathBuf, Option<PathBuf>) {
let dove_path = path.join("Dove.toml");
if !dove_path.exists() {
return (path, None);
}

let test = read_to_string(dove_path)
.ok()
.and_then(|str| toml::from_str::<DoveToml>(&str).ok())
.map(|dovetoml| path.join(dovetoml.layout.tests_dir));

(path, test)
}
fn move_files_without_tests(
project_folder: PathBuf,
test_folder: Option<&PathBuf>,
) -> Vec<PathBuf> {
project_folder
.read_dir()
.ok()
.map(|read| {
read.into_iter()
.filter_map(|path| path.ok())
.map(|path| path.path())
.filter(|path| {
!test_folder
.map(|test| path.starts_with(test))
.unwrap_or(false)
})
.filter_map(|path| {
if path.is_dir() {
Some(move_files_without_tests(path, test_folder))
} else if path.is_file() && is_move_file(&path) {
Some(vec![path])
} else {
None
}
})
.flatten()
.collect::<Vec<PathBuf>>()
})
.unwrap_or_default()
}

/// All scripts and modules from dependencies
#[derive(Debug, Serialize, Deserialize)]
pub struct MapDependencies {
/// All scripts from dependencies
pub scripts: Vec<FuncMeta>,
/// All modules from dependencies
pub modules: Vec<ModuleMeta>,
}
impl MapDependencies {
/// Create a dependencies map and save it to disk
pub fn create_and_save(ctx: &Context) -> Result<(), Error> {
let fpath = ctx.path_for(&ctx.manifest.layout.project_map);
if !fpath.exists() {
let parent = fpath.parent().map_or_else(
|| anyhow::bail!("The path to the dependencies map is set incorrectly"),
Ok,
)?;
if !parent.exists() {
create_dir_all(parent)?;
}
}

let map = Self::create(ctx)?;
let bmap = bcs::to_bytes(&map)?;
fs::write(&fpath, bmap).map_err(|err| anyhow!("{}", err.to_string()))
}

/// Get all scripts and modules from dependencies
pub fn create(ctx: &Context) -> Result<MapDependencies, Error> {
let deps = get_all_dependencies(ctx)?;

let (scripts, modules) = deps.into_iter().fold(
(Vec::new(), Vec::new()),
|(mut scripts, mut modules), unit| {
match unit {
Unit::Module(module) => modules.push(module),
Unit::Script(script) => scripts.push(script),
}
(scripts, modules)
},
);

Ok(MapDependencies { scripts, modules })
}

/// Download a dependencies map from disk
pub fn load(ctx: &Context) -> Result<MapDependencies, Error> {
let fpath = ctx.path_for(&ctx.manifest.layout.project_map);
if !fpath.exists() {
anyhow::bail!("The project map file was not found. Build a project.");
}

let bmap = fs::read(&fpath)?;
bcs::from_bytes::<MapDependencies>(&bmap).map_err(|err| anyhow!("{:?}", err))
}
}
20 changes: 14 additions & 6 deletions dove/src/tx/fn_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,12 @@ pub(crate) fn make_script_call(

let (path, meta) = select_function(scripts, addr, &type_tag, &args, &cfg)?;

let (signers, args) =
prepare_function_signature(&meta.parameters, &args, !cfg.deny_signers_definition, addr)?;
let (signers, args) = prepare_function_signature(
&meta.value.parameters,
&args,
!cfg.deny_signers_definition,
addr,
)?;

let (signers, mut tx) = match signers {
Signers::Explicit(signers) => (
Expand Down Expand Up @@ -141,8 +145,12 @@ pub(crate) fn make_function_call(
let addr = ctx.account_address()?;
let (_, meta) = select_function(functions, addr, &type_tag, &args, &cfg)?;

let (signers, args) =
prepare_function_signature(&meta.parameters, &args, !cfg.deny_signers_definition, addr)?;
let (signers, args) = prepare_function_signature(
&meta.value.parameters,
&args,
!cfg.deny_signers_definition,
addr,
)?;

let tx_name = format!("{}_{}", module, func);
let (signers, tx) = match signers {
Expand Down Expand Up @@ -191,10 +199,10 @@ fn select_function(
} else if func.len() > 1 {
let mut func = func
.into_iter()
.filter(|(_, f)| f.type_parameters.len() == type_tag.len())
.filter(|(_, f)| f.value.type_parameters.len() == type_tag.len())
.filter(|(_, f)| {
prepare_function_signature(
&f.parameters,
&f.value.parameters,
args,
!cfg.deny_signers_definition,
addr,
Expand Down
22 changes: 11 additions & 11 deletions dove/src/tx/resolver.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::fs;
use std::path::PathBuf;
use anyhow::Error;
use regex::Regex;
use move_core_types::account_address::AccountAddress;
use move_core_types::identifier::Identifier;
use crate::context::Context;
use anyhow::Error;
use lang::compiler::file::find_move_files;
use std::fs;
use regex::Regex;
use lang::compiler::metadata::{module_meta, FuncMeta, script_meta};
use std::path::PathBuf;
use lang::compiler::metadata::{module_meta, script_meta, FuncMeta};
use crate::context::Context;

pub(crate) fn find_module_function(
ctx: &Context,
Expand Down Expand Up @@ -41,12 +41,12 @@ pub(crate) fn find_module_function(
})
.flat_map(|(p, m)| {
m.into_iter()
.filter(|m| m.address == *address && &m.name == m_name)
.flat_map(|m| m.funs)
.filter(|f| &f.name == f_name)
.filter(|m| m.value.address == *address && &m.value.name == m_name)
.flat_map(|m| m.value.funs)
.filter(|f| &f.value.name == f_name)
.filter(|f| {
if script_only {
f.visibility.is_script()
f.value.visibility.is_script()
} else {
false
}
Expand Down Expand Up @@ -80,7 +80,7 @@ pub(crate) fn find_script(
})
.flat_map(|(p, m)| {
m.into_iter()
.filter(|m| &m.name == name)
.filter(|m| &m.value.name == name)
.map(|m| (p.to_owned(), m))
.collect::<Vec<_>>()
})
Expand Down
Loading