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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// Copyright (c) The Diem Core Contributors
// Copyright (c) The Move Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::{
    sandbox::{
        self,
        utils::{on_disk_state_view::OnDiskStateView, PackageContext},
    },
    Move, NativeFunctionRecord, DEFAULT_BUILD_DIR,
};
use anyhow::Result;
use clap::Parser;
use move_core_types::{
    errmap::ErrorMapping, language_storage::TypeTag, parser,
    transaction_argument::TransactionArgument,
};
use move_package::compilation::package_layout::CompiledPackageLayout;
use move_vm_test_utils::gas_schedule::CostTable;
use std::{
    fs,
    path::{Path, PathBuf},
};

#[derive(Parser)]
pub enum SandboxCommand {
    /// Compile the modules in this package and its dependencies and publish the resulting bytecodes in global storage.
    #[clap(name = "publish")]
    Publish {
        /// If set, fail when attempting to publish a module that already
        /// exists in global storage.
        #[clap(long = "no-republish")]
        no_republish: bool,
        /// By default, code that might cause breaking changes for bytecode
        /// linking or data layout compatibility checks will not be published.
        /// Set this flag to ignore breaking changes checks and publish anyway.
        #[clap(long = "ignore-breaking-changes")]
        ignore_breaking_changes: bool,
        /// If set, publish not only the modules in this package but also
        /// modules in all its dependencies.
        #[clap(long = "with-deps")]
        with_deps: bool,
        /// If set, all modules at once as a bundle. The default is to publish
        /// modules sequentially.
        #[clap(long = "bundle")]
        bundle: bool,
        /// Manually specify the publishing order of modules.
        #[clap(
            long = "override-ordering",
            takes_value(true),
            multiple_values(true),
            multiple_occurrences(true)
        )]
        override_ordering: Option<Vec<String>>,
    },
    /// Run a Move script that reads/writes resources stored on disk in `storage-dir`.
    /// The script must be defined in the package.
    #[clap(name = "run")]
    Run {
        /// Path to .mv file containing either script or module bytecodes. If the file is a module, the
        /// `script_name` parameter must be set.
        #[clap(name = "script", parse(from_os_str))]
        script_file: PathBuf,
        /// Name of the script function inside `script_file` to call. Should only be set if `script_file`
        /// points to a module.
        #[clap(name = "name")]
        script_name: Option<String>,
        /// Possibly-empty list of signers for the current transaction (e.g., `account` in
        /// `main(&account: signer)`). Must match the number of signers expected by `script_file`.
        #[clap(
            long = "signers",
            takes_value(true),
            multiple_values(true),
            multiple_occurrences(true)
        )]
        signers: Vec<String>,
        /// Possibly-empty list of arguments passed to the transaction (e.g., `i` in
        /// `main(i: u64)`). Must match the arguments types expected by `script_file`.
        /// Supported argument types are
        /// bool literals (true, false),
        /// u64 literals (e.g., 10, 58),
        /// address literals (e.g., 0x12, 0x0000000000000000000000000000000f),
        /// hexadecimal strings (e.g., x"0012" will parse as the vector<u8> value [00, 12]), and
        /// ASCII strings (e.g., 'b"hi" will parse as the vector<u8> value [68, 69]).
        #[clap(
            long = "args",
            parse(try_from_str = parser::parse_transaction_argument),
            takes_value(true),
            multiple_values(true),
            multiple_occurrences(true)
        )]
        args: Vec<TransactionArgument>,
        /// Possibly-empty list of type arguments passed to the transaction (e.g., `T` in
        /// `main<T>()`). Must match the type arguments kinds expected by `script_file`.
        #[clap(
            long = "type-args",
            parse(try_from_str = parser::parse_type_tag),
            takes_value(true),
            multiple_values(true),
            multiple_occurrences(true)
        )]
        type_args: Vec<TypeTag>,
        /// Maximum number of gas units to be consumed by execution.
        /// When the budget is exhaused, execution will abort.
        /// By default, no `gas-budget` is specified and gas metering is disabled.
        #[clap(long = "gas-budget", short = 'g')]
        gas_budget: Option<u64>,
        /// If set, the effects of executing `script_file` (i.e., published, updated, and
        /// deleted resources) will NOT be committed to disk.
        #[clap(long = "dry-run", short = 'n')]
        dry_run: bool,
    },
    /// Run expected value tests using the given batch file.
    #[clap(name = "exp-test")]
    Test {
        /// Use an ephemeral directory to serve as the testing workspace.
        /// By default, the directory containing the `args.txt` will be the workspace.
        #[clap(long = "use-temp-dir")]
        use_temp_dir: bool,
        /// Show coverage information after tests are done.
        /// By default, coverage will not be tracked nor shown.
        #[clap(long = "track-cov")]
        track_cov: bool,
    },
    /// View Move resources, events files, and modules stored on disk.
    #[clap(name = "view")]
    View {
        /// Path to a resource, events file, or module stored on disk.
        #[clap(name = "file", parse(from_os_str))]
        file: PathBuf,
    },
    /// Delete all resources, events, and modules stored on disk under `storage-dir`.
    /// Does *not* delete anything in `src`.
    Clean {},
    /// Run well-formedness checks on the `storage-dir` and `install-dir` directories.
    #[clap(name = "doctor")]
    Doctor {},
    /// Generate struct layout bindings for the modules stored on disk under `storage-dir`
    // TODO: expand this to generate script bindings, etc.?.
    #[clap(name = "generate")]
    Generate {
        #[clap(subcommand)]
        cmd: GenerateCommand,
    },
}

#[derive(Parser)]
pub enum GenerateCommand {
    /// Generate struct layout bindings for the modules stored on disk under `storage-dir`.
    #[clap(name = "struct-layouts")]
    StructLayouts {
        /// Path to a module stored on disk.
        #[clap(long, parse(from_os_str))]
        module: PathBuf,
        /// If set, generate bindings for the specified struct and type arguments. If unset,
        /// generate bindings for all closed struct definitions.
        #[clap(flatten)]
        options: StructLayoutOptions,
    },
}
#[derive(Parser)]
pub struct StructLayoutOptions {
    /// Generate layout bindings for this struct.
    #[clap(long = "struct")]
    struct_: Option<String>,
    /// Generate layout bindings for `struct` bound to these type arguments.
    #[clap(
        long = "type-args",
        parse(try_from_str = parser::parse_type_tag),
        requires="struct",
        takes_value(true),
        multiple_values(true),
        multiple_occurrences(true)
    )]
    type_args: Option<Vec<TypeTag>>,
    /// If set, generate bindings only for the struct passed in.
    /// When unset, generates bindings for the struct and all of its transitive dependencies.
    #[clap(long = "shallow")]
    shallow: bool,
}

impl SandboxCommand {
    pub fn handle_command(
        &self,
        natives: Vec<NativeFunctionRecord>,
        cost_table: &CostTable,
        error_descriptions: &ErrorMapping,
        move_args: &Move,
        storage_dir: &Path,
    ) -> Result<()> {
        match self {
            SandboxCommand::Publish {
                no_republish,
                ignore_breaking_changes,
                with_deps,
                bundle,
                override_ordering,
            } => {
                let context =
                    PackageContext::new(&move_args.package_path, &move_args.build_config)?;
                let state = context.prepare_state(storage_dir)?;
                sandbox::commands::publish(
                    natives,
                    cost_table,
                    &state,
                    context.package(),
                    *no_republish,
                    *ignore_breaking_changes,
                    *with_deps,
                    *bundle,
                    override_ordering.as_ref().map(|o| o.as_slice()),
                    move_args.verbose,
                )
            }
            SandboxCommand::Run {
                script_file,
                script_name,
                signers,
                args,
                type_args,
                gas_budget,
                dry_run,
            } => {
                let context =
                    PackageContext::new(&move_args.package_path, &move_args.build_config)?;
                let state = context.prepare_state(storage_dir)?;
                sandbox::commands::run(
                    natives,
                    cost_table,
                    error_descriptions,
                    &state,
                    context.package(),
                    script_file,
                    script_name,
                    signers,
                    args,
                    type_args.to_vec(),
                    *gas_budget,
                    *dry_run,
                    move_args.verbose,
                )
            }
            SandboxCommand::Test {
                use_temp_dir,
                track_cov,
            } => sandbox::commands::run_all(
                move_args
                    .package_path
                    .as_deref()
                    .unwrap_or_else(|| Path::new(".")),
                &std::env::current_exe()?,
                *use_temp_dir,
                *track_cov,
            ),
            SandboxCommand::View { file } => {
                let state = PackageContext::new(&move_args.package_path, &move_args.build_config)?
                    .prepare_state(storage_dir)?;
                sandbox::commands::view(&state, file)
            }
            SandboxCommand::Clean {} => {
                // delete storage
                let storage_dir = Path::new(storage_dir);
                if storage_dir.exists() {
                    fs::remove_dir_all(&storage_dir)?;
                }

                // delete build
                let build_dir = Path::new(
                    &move_args
                        .build_config
                        .install_dir
                        .as_ref()
                        .unwrap_or(&PathBuf::from(DEFAULT_BUILD_DIR)),
                )
                .join(CompiledPackageLayout::Root.path());
                if build_dir.exists() {
                    fs::remove_dir_all(&build_dir)?;
                }
                Ok(())
            }
            SandboxCommand::Doctor {} => {
                let state = PackageContext::new(&move_args.package_path, &move_args.build_config)?
                    .prepare_state(storage_dir)?;
                sandbox::commands::doctor(&state)
            }
            SandboxCommand::Generate { cmd } => {
                let state = PackageContext::new(&move_args.package_path, &move_args.build_config)?
                    .prepare_state(storage_dir)?;
                handle_generate_commands(cmd, &state)
            }
        }
    }
}

fn handle_generate_commands(cmd: &GenerateCommand, state: &OnDiskStateView) -> Result<()> {
    match cmd {
        GenerateCommand::StructLayouts { module, options } => {
            sandbox::commands::generate::generate_struct_layouts(
                module,
                &options.struct_,
                &options.type_args,
                options.shallow,
                state,
            )
        }
    }
}