1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

#![forbid(unsafe_code)]

use move_binary_format::file_format::CompiledModule;
use move_command_line_common::files::{extension_equals, find_filenames, MOVE_EXTENSION};
use move_compiler::{
    compiled_unit::{CompiledUnit, NamedCompiledModule},
    shared::{NumberFormat, NumericalAddress},
};
use move_package::compilation::compiled_package::CompiledPackage;
use once_cell::sync::Lazy;
use std::{collections::BTreeMap, path::PathBuf};
use tempfile::tempdir;

pub mod natives;
pub mod release;

const CORE_MODULES_DIR: &str = "core/sources";
const DPN_MODULES_DIR: &str = "DPN/sources";

pub fn path_in_crate<S>(relative: S) -> PathBuf
where
    S: Into<String>,
{
    let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    path.push(relative.into());
    path
}

pub fn diem_core_modules_full_path() -> String {
    format!("{}/{}", env!("CARGO_MANIFEST_DIR"), CORE_MODULES_DIR)
}

pub fn diem_payment_modules_full_path() -> String {
    format!("{}/{}", env!("CARGO_MANIFEST_DIR"), DPN_MODULES_DIR)
}

pub fn diem_stdlib_files_no_dependencies() -> Vec<String> {
    let diem_payment_modules_path = path_in_crate(DPN_MODULES_DIR);
    find_filenames(&[diem_payment_modules_path], |p| {
        extension_equals(p, MOVE_EXTENSION)
    })
    .unwrap()
}

pub fn diem_stdlib_files() -> Vec<String> {
    let mut files = move_stdlib::move_stdlib_files();
    files.extend(diem_stdlib_files_no_dependencies());
    files
}

pub fn diem_framework_named_addresses() -> BTreeMap<String, NumericalAddress> {
    DPN_FRAMEWORK_PKG
        .compiled_package_info
        .address_alias_instantiation
        .iter()
        .map(|(name, addr)| {
            (
                name.to_string(),
                NumericalAddress::new(addr.into_bytes(), NumberFormat::Hex),
            )
        })
        .collect()
}

static DPN_FRAMEWORK_PKG: Lazy<CompiledPackage> = Lazy::new(|| {
    let build_config = move_package::BuildConfig {
        install_dir: Some(tempdir().unwrap().path().to_path_buf()),
        ..Default::default()
    };
    build_config
        .compile_package(&path_in_crate("DPN"), &mut Vec::new())
        .unwrap()
});

static EXPERIMENTAL_FRAMEWORK_PKG: Lazy<CompiledPackage> = Lazy::new(|| {
    let build_config = move_package::BuildConfig {
        install_dir: Some(tempdir().unwrap().path().to_path_buf()),
        ..Default::default()
    };
    build_config
        .compile_package(&path_in_crate("experimental"), &mut Vec::new())
        .unwrap()
});

pub fn modules() -> Vec<CompiledModule> {
    DPN_FRAMEWORK_PKG
        .transitive_compiled_units()
        .iter()
        .filter_map(|unit| match unit {
            CompiledUnit::Module(NamedCompiledModule { module, .. }) => Some(module.clone()),
            CompiledUnit::Script(_) => None,
        })
        .collect()
}

pub fn module_blobs() -> Vec<Vec<u8>> {
    DPN_FRAMEWORK_PKG
        .transitive_compiled_units()
        .iter()
        .filter_map(|unit| match unit {
            CompiledUnit::Module(NamedCompiledModule { module, .. }) => {
                let mut bytes = vec![];
                module.serialize(&mut bytes).unwrap();
                Some(bytes)
            }
            CompiledUnit::Script(_) => None,
        })
        .collect()
}

pub fn experimental_module_blobs() -> Vec<Vec<u8>> {
    EXPERIMENTAL_FRAMEWORK_PKG
        .transitive_compiled_units()
        .iter()
        .filter_map(|unit| match unit {
            CompiledUnit::Module(NamedCompiledModule { module, .. }) => {
                let mut bytes = vec![];
                module.serialize(&mut bytes).unwrap();
                Some(bytes)
            }
            CompiledUnit::Script(_) => None,
        })
        .collect()
}