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
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use move_core_types::language_storage::TypeTag;

use crate::{
    error::ExecutionError,
    execution::{RawValueType, Value},
    transaction::Argument,
    transfer::Receiving,
    type_resolver::TypeTagResolver,
};

pub type TransactionIndex = usize;

pub trait ExecutionMode {
    /// All updates to a Arguments used in that Command
    type ArgumentUpdates;
    /// the gathered results from batched executions
    type ExecutionResults;

    /// Controls the calling of arbitrary Move functions
    fn allow_arbitrary_function_calls() -> bool;

    /// Controls the ability to instantiate any Move function parameter with a Pure call arg.
    ///  In other words, you can instantiate any struct or object or other value with its BCS byte
    fn allow_arbitrary_values() -> bool;

    /// Do not perform conservation checks after execution.
    fn skip_conservation_checks() -> bool;

    /// If not set, the package ID should be calculated like an object and an
    /// UpgradeCap is produced
    fn packages_are_predefined() -> bool;

    fn empty_arguments() -> Self::ArgumentUpdates;

    fn empty_results() -> Self::ExecutionResults;

    fn add_argument_update(
        resolver: &impl TypeTagResolver,
        acc: &mut Self::ArgumentUpdates,
        arg: Argument,
        _new_value: &Value,
    ) -> Result<(), ExecutionError>;

    fn finish_command(
        resolver: &impl TypeTagResolver,
        acc: &mut Self::ExecutionResults,
        argument_updates: Self::ArgumentUpdates,
        command_result: &[Value],
    ) -> Result<(), ExecutionError>;
}

#[derive(Copy, Clone)]
pub struct Normal;

impl ExecutionMode for Normal {
    type ArgumentUpdates = ();
    type ExecutionResults = ();

    fn allow_arbitrary_function_calls() -> bool {
        false
    }

    fn allow_arbitrary_values() -> bool {
        false
    }

    fn skip_conservation_checks() -> bool {
        false
    }

    fn packages_are_predefined() -> bool {
        false
    }

    fn empty_arguments() -> Self::ArgumentUpdates {}

    fn empty_results() -> Self::ExecutionResults {}

    fn add_argument_update(
        _resolver: &impl TypeTagResolver,
        _acc: &mut Self::ArgumentUpdates,
        _arg: Argument,
        _new_value: &Value,
    ) -> Result<(), ExecutionError> {
        Ok(())
    }

    fn finish_command(
        _resolver: &impl TypeTagResolver,
        _acc: &mut Self::ExecutionResults,
        _argument_updates: Self::ArgumentUpdates,
        _command_result: &[Value],
    ) -> Result<(), ExecutionError> {
        Ok(())
    }
}

#[derive(Copy, Clone)]
pub struct Genesis;

impl ExecutionMode for Genesis {
    type ArgumentUpdates = ();
    type ExecutionResults = ();

    fn allow_arbitrary_function_calls() -> bool {
        true
    }

    fn allow_arbitrary_values() -> bool {
        true
    }

    fn packages_are_predefined() -> bool {
        true
    }

    fn skip_conservation_checks() -> bool {
        false
    }

    fn empty_arguments() -> Self::ArgumentUpdates {}

    fn empty_results() -> Self::ExecutionResults {}

    fn add_argument_update(
        _resolver: &impl TypeTagResolver,
        _acc: &mut Self::ArgumentUpdates,
        _arg: Argument,
        _new_value: &Value,
    ) -> Result<(), ExecutionError> {
        Ok(())
    }

    fn finish_command(
        _resolver: &impl TypeTagResolver,
        _acc: &mut Self::ExecutionResults,
        _argument_updates: Self::ArgumentUpdates,
        _command_result: &[Value],
    ) -> Result<(), ExecutionError> {
        Ok(())
    }
}

#[derive(Copy, Clone)]
pub struct System;

/// Execution mode for executing a system transaction, including the epoch change
/// transaction and the consensus commit prologue. In this mode, we allow calls to
/// any function bypassing visibility.
impl ExecutionMode for System {
    type ArgumentUpdates = ();
    type ExecutionResults = ();

    fn allow_arbitrary_function_calls() -> bool {
        // allows bypassing visibility for system calls
        true
    }

    fn allow_arbitrary_values() -> bool {
        // For AuthenticatorStateUpdate, we need to be able to pass in a vector of
        // JWKs, so we need to allow arbitrary values.
        true
    }

    fn skip_conservation_checks() -> bool {
        false
    }

    fn packages_are_predefined() -> bool {
        true
    }

    fn empty_arguments() -> Self::ArgumentUpdates {}

    fn empty_results() -> Self::ExecutionResults {}

    fn add_argument_update(
        _resolver: &impl TypeTagResolver,
        _acc: &mut Self::ArgumentUpdates,
        _arg: Argument,
        _new_value: &Value,
    ) -> Result<(), ExecutionError> {
        Ok(())
    }

    fn finish_command(
        _resolver: &impl TypeTagResolver,
        _acc: &mut Self::ExecutionResults,
        _argument_updates: Self::ArgumentUpdates,
        _command_result: &[Value],
    ) -> Result<(), ExecutionError> {
        Ok(())
    }
}

/// WARNING! Using this mode will bypass all normal checks around Move entry functions! This
/// includes the various rules for function arguments, meaning any object can be created just from
/// BCS bytes!
pub struct DevInspect<const SKIP_ALL_CHECKS: bool>;

pub type ExecutionResult = (
    /*  mutable_reference_outputs */ Vec<(Argument, Vec<u8>, TypeTag)>,
    /*  return_values */ Vec<(Vec<u8>, TypeTag)>,
);

impl<const SKIP_ALL_CHECKS: bool> ExecutionMode for DevInspect<SKIP_ALL_CHECKS> {
    type ArgumentUpdates = Vec<(Argument, Vec<u8>, TypeTag)>;
    type ExecutionResults = Vec<ExecutionResult>;

    fn allow_arbitrary_function_calls() -> bool {
        SKIP_ALL_CHECKS
    }

    fn allow_arbitrary_values() -> bool {
        SKIP_ALL_CHECKS
    }

    fn skip_conservation_checks() -> bool {
        SKIP_ALL_CHECKS
    }

    fn packages_are_predefined() -> bool {
        false
    }

    fn empty_arguments() -> Self::ArgumentUpdates {
        vec![]
    }

    fn empty_results() -> Self::ExecutionResults {
        vec![]
    }

    fn add_argument_update(
        resolver: &impl TypeTagResolver,
        acc: &mut Self::ArgumentUpdates,
        arg: Argument,
        new_value: &Value,
    ) -> Result<(), ExecutionError> {
        let (bytes, type_tag) = value_to_bytes_and_tag(resolver, new_value)?;
        acc.push((arg, bytes, type_tag));
        Ok(())
    }

    fn finish_command(
        resolver: &impl TypeTagResolver,
        acc: &mut Self::ExecutionResults,
        argument_updates: Self::ArgumentUpdates,
        command_result: &[Value],
    ) -> Result<(), ExecutionError> {
        let command_bytes = command_result
            .iter()
            .map(|value| value_to_bytes_and_tag(resolver, value))
            .collect::<Result<_, _>>()?;
        acc.push((argument_updates, command_bytes));
        Ok(())
    }
}

fn value_to_bytes_and_tag(
    resolver: &impl TypeTagResolver,
    value: &Value,
) -> Result<(Vec<u8>, TypeTag), ExecutionError> {
    let (type_tag, bytes) = match value {
        Value::Object(obj) => {
            let tag = resolver.get_type_tag(&obj.type_)?;
            let mut bytes = vec![];
            obj.write_bcs_bytes(&mut bytes);
            (tag, bytes)
        }
        Value::Raw(RawValueType::Any, bytes) => {
            // this case shouldn't happen
            (TypeTag::Vector(Box::new(TypeTag::U8)), bytes.clone())
        }
        Value::Raw(RawValueType::Loaded { ty, .. }, bytes) => {
            let tag = resolver.get_type_tag(ty)?;
            (tag, bytes.clone())
        }
        Value::Receiving(id, seqno, _) => (
            Receiving::type_tag(),
            Receiving::new(*id, *seqno).to_bcs_bytes(),
        ),
    };
    Ok((bytes, type_tag))
}