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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::{
    sandbox::utils::{
        package::{MovePackage, SourceFilter},
        OnDiskStateView,
    },
    DEFAULT_PACKAGE_DIR,
};
use anyhow::{bail, Result};
use include_dir::{include_dir, Dir};
use move_binary_format::file_format::CompiledModule;
use move_lang::shared::AddressBytes;
use move_stdlib::move_stdlib_named_addresses;
use once_cell::sync::Lazy;
use std::{
    collections::{BTreeMap, HashSet},
    path::Path,
    str::FromStr,
};

use super::ModuleIdWithNamedAddress;

/// Content for the move stdlib directory
const DIR_MOVE_STDLIB: Dir = include_dir!("../../move-stdlib/modules");
/// Content for the nursery directory
const DIR_MOVE_STDLIB_NURSERY: Dir = include_dir!("../../move-stdlib/nursery");
/// Content for diem framework directory
const DIR_DIEM_FRAMEWORK: Dir = include_dir!("../../diem-framework/modules");

/// Pre-defined stdlib package
static PACKAGE_MOVE_STDLIB: Lazy<MovePackage> = Lazy::new(|| {
    MovePackage::new(
        "stdlib".to_string(),
        vec![
            SourceFilter {
                source_dir: &DIR_MOVE_STDLIB,
                inclusion: None, // include everything
                exclusion: HashSet::new(),
            },
            SourceFilter {
                source_dir: &DIR_MOVE_STDLIB_NURSERY,
                inclusion: None, // include everything
                exclusion: HashSet::new(),
            },
        ],
        vec![],
        move_stdlib_named_addresses(),
        Some("Std".to_owned()),
    )
});

static PACKAGE_DIEM_FRAMEWORK: Lazy<MovePackage> = Lazy::new(|| {
    // TODO: This will be removed once we have proper packages.
    let named_addresses = [
        ("Std", "0x1"),
        ("DiemFramework", "0x1"),
        ("DiemRoot", "0xA550C18"),
        ("CurrencyInfo", "0xA550C18"),
        ("TreasuryCompliance", "0xB1E55ED"),
        ("VMReserved", "0x0"),
    ]
    .iter()
    .map(|(name, addr)| (name.to_string(), AddressBytes::parse_str(addr).unwrap()))
    .collect();
    MovePackage::new(
        "diem".to_string(),
        vec![SourceFilter {
            source_dir: &DIR_DIEM_FRAMEWORK,
            inclusion: None, // include everything
            exclusion: HashSet::new(),
        }],
        vec![&PACKAGE_MOVE_STDLIB],
        named_addresses,
        Some("DiemFramework".to_owned()),
    )
});

/// The dependency interface exposed to CLI main
pub struct Mode(Vec<&'static MovePackage>);

/// Set of supported modes
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub enum ModeType {
    /// No dependencies
    Bare,
    /// Move stdlib dependencies only (e.g., Vector, Signer)
    Stdlib,
    /// Include stdlib and all Diem dependencies
    Diem,
}

impl Mode {
    pub fn new(t: ModeType) -> Self {
        match t {
            ModeType::Bare => Mode(vec![]),
            ModeType::Stdlib => Mode(vec![&*PACKAGE_MOVE_STDLIB]),
            ModeType::Diem => Mode(vec![&*PACKAGE_DIEM_FRAMEWORK]),
        }
    }

    /// Prepare an OnDiskStateView that is ready to use. Library modules will be preloaded into the
    /// storage if `load_libraries` is true.
    ///
    /// NOTE: this is the only way to get a state view in Move CLI, and thus, this function needs
    /// to be run before every command that needs a state view, i.e., `check`, `publish`, `run`,
    /// `view`, and `doctor`.
    pub fn prepare_state(&self, build_dir: &Path, storage_dir: &Path) -> Result<OnDiskStateView> {
        let package_dir = build_dir.join(DEFAULT_PACKAGE_DIR);
        let named_address_values = self.prepare(&package_dir, false)?;
        let state = OnDiskStateView::create(build_dir, storage_dir)?;

        // preload the storage with library modules (if such modules do not exist yet)
        let lib_modules = self.compiled_modules(&package_dir)?;
        let new_modules: Vec<_> = lib_modules
            .into_iter()
            .filter(|(_, m)| !state.has_module(&m.self_id()))
            .collect();

        let mut serialized_modules = vec![];
        for (id, module) in new_modules {
            let mut module_bytes = vec![];
            module.serialize(&mut module_bytes)?;
            serialized_modules.push((id, module_bytes));
        }
        state.save_modules(&serialized_modules, named_address_values)?;

        Ok(state)
    }

    pub fn prepare(
        &self,
        out_path: &Path,
        source_only: bool,
    ) -> Result<BTreeMap<String, AddressBytes>> {
        let mut named_address_values = BTreeMap::new();
        for pkg in &self.0 {
            pkg.prepare(out_path, source_only)?;
            named_address_values.extend(pkg.named_addresses().clone());
        }
        Ok(named_address_values)
    }

    pub fn source_files(&self, out_path: &Path) -> Result<Vec<String>> {
        let pkg_sources: Result<Vec<_>, _> = self
            .0
            .iter()
            .map(|pkg| pkg.source_files(out_path))
            .collect();
        Ok(pkg_sources?.into_iter().flatten().collect())
    }

    pub fn compiled_modules(
        &self,
        out_path: &Path,
    ) -> Result<Vec<(ModuleIdWithNamedAddress, CompiledModule)>> {
        let pkg_modules = self
            .0
            .iter()
            .map(|pkg| pkg.compiled_modules(out_path))
            .collect::<Result<Vec<Vec<_>>>>()?;
        Ok(pkg_modules.into_iter().flatten().collect())
    }
}

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

    fn from_str(mode: &str) -> Result<Self> {
        Ok(match mode {
            "bare" => ModeType::Bare,
            "stdlib" => ModeType::Stdlib,
            "diem" => ModeType::Diem,
            _ => bail!("Invalid mode for dependency: {}", mode),
        })
    }
}

impl ToString for ModeType {
    fn to_string(&self) -> String {
        match self {
            ModeType::Bare => "bare",
            ModeType::Stdlib => "stdlib",
            ModeType::Diem => "diem",
        }
        .to_string()
    }
}

impl Default for ModeType {
    fn default() -> Self {
        ModeType::Stdlib
    }
}

impl Default for Mode {
    fn default() -> Self {
        Self::new(ModeType::default())
    }
}