sui_adapter_latest/static_programmable_transactions/execution/
interpreter.rs

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

use crate::{
    execution_mode::ExecutionMode,
    gas_charger::GasCharger,
    sp,
    static_programmable_transactions::{
        env::Env,
        execution::context::{Context, CtxValue},
        typing::ast as T,
    },
};
use move_core_types::account_address::AccountAddress;
use move_trace_format::format::MoveTraceBuilder;
use std::{cell::RefCell, rc::Rc, sync::Arc, time::Instant};
use sui_types::{
    base_types::TxContext,
    error::{ExecutionError, ExecutionErrorKind},
    execution::{ExecutionTiming, ResultWithTimings},
    execution_status::PackageUpgradeError,
    metrics::LimitsMetrics,
    move_package::MovePackage,
    object::Owner,
};
use tracing::instrument;

pub fn execute<'env, 'pc, 'vm, 'state, 'linkage, Mode: ExecutionMode>(
    env: &'env mut Env<'pc, 'vm, 'state, 'linkage>,
    metrics: Arc<LimitsMetrics>,
    tx_context: Rc<RefCell<TxContext>>,
    gas_charger: &mut GasCharger,
    ast: T::Transaction,
    trace_builder_opt: &mut Option<MoveTraceBuilder>,
) -> ResultWithTimings<Mode::ExecutionResults, ExecutionError>
where
    'pc: 'state,
    'env: 'state,
{
    let mut timings = vec![];
    let result = execute_inner::<Mode>(
        &mut timings,
        env,
        metrics,
        tx_context,
        gas_charger,
        ast,
        trace_builder_opt,
    );

    match result {
        Ok(result) => Ok((result, timings)),
        Err(e) => Err((e, timings)),
    }
}

pub fn execute_inner<'env, 'pc, 'vm, 'state, 'linkage, Mode: ExecutionMode>(
    timings: &mut Vec<ExecutionTiming>,
    env: &'env mut Env<'pc, 'vm, 'state, 'linkage>,
    metrics: Arc<LimitsMetrics>,
    tx_context: Rc<RefCell<TxContext>>,
    gas_charger: &mut GasCharger,
    ast: T::Transaction,
    trace_builder_opt: &mut Option<MoveTraceBuilder>,
) -> Result<Mode::ExecutionResults, ExecutionError>
where
    'pc: 'state,
{
    let T::Transaction { inputs, commands } = ast;
    let mut context = Context::new(env, metrics, tx_context, gas_charger, inputs)?;
    let mut mode_results = Mode::empty_results();
    for (sp!(idx, command), tys) in commands {
        let start = Instant::now();
        if let Err(err) = execute_command::<Mode>(
            &mut context,
            &mut mode_results,
            command,
            tys,
            trace_builder_opt.as_mut(),
        ) {
            let object_runtime = context.object_runtime()?;
            // We still need to record the loaded child objects for replay
            let loaded_runtime_objects = object_runtime.loaded_runtime_objects();
            // we do not save the wrapped objects since on error, they should not be modified
            drop(context);
            // TODO wtf is going on with the borrow checker here. 'state is bound into the object
            // runtime, but its since been dropped. what gives with this error?
            env.state_view
                .save_loaded_runtime_objects(loaded_runtime_objects);
            timings.push(ExecutionTiming::Abort(start.elapsed()));
            return Err(err.with_command_index(idx as usize));
        };
        timings.push(ExecutionTiming::Success(start.elapsed()));
    }
    // Save loaded objects table in case we fail in post execution
    let object_runtime = context.object_runtime()?;
    // We still need to record the loaded child objects for replay
    // Record the objects loaded at runtime (dynamic fields + received) for
    // storage rebate calculation.
    let loaded_runtime_objects = object_runtime.loaded_runtime_objects();
    // We record what objects were contained in at the start of the transaction
    // for expensive invariant checks
    let wrapped_object_containers = object_runtime.wrapped_object_containers();

    // apply changes
    let finished = context.finish::<Mode>();
    // Save loaded objects for debug. We dont want to lose the info
    env.state_view
        .save_loaded_runtime_objects(loaded_runtime_objects);
    env.state_view
        .save_wrapped_object_containers(wrapped_object_containers);
    env.state_view.record_execution_results(finished?);
    Ok(mode_results)
}

/// Execute a single command
#[instrument(level = "trace", skip_all)]
fn execute_command<Mode: ExecutionMode>(
    context: &mut Context,
    mode_results: &mut Mode::ExecutionResults,
    command: T::Command_,
    result_tys: T::ResultType,
    trace_builder_opt: Option<&mut MoveTraceBuilder>,
) -> Result<(), ExecutionError> {
    let mut args_to_update = vec![];
    let result = match command {
        T::Command_::MoveCall(move_call) => {
            let T::MoveCall {
                function,
                arguments,
            } = *move_call;
            if Mode::TRACK_EXECUTION {
                args_to_update.extend(
                    arguments
                        .iter()
                        .filter(|arg| matches!(&arg.value.1, T::Type::Reference(/* mut */ true, _)))
                        .map(|sp!(_, (arg, ty))| (arg.location(), ty.clone())),
                )
            }
            let arguments = context.arguments(arguments)?;
            context.vm_move_call(function, arguments, trace_builder_opt)?
        }
        T::Command_::TransferObjects(objects, recipient) => {
            let object_tys = objects
                .iter()
                .map(|sp!(_, (_, ty))| ty.clone())
                .collect::<Vec<_>>();
            let object_values: Vec<CtxValue> = context.arguments(objects)?;
            let recipient: AccountAddress = context.argument(recipient)?;
            assert_invariant!(
                object_values.len() == object_tys.len(),
                "object values and types mismatch"
            );
            for (object_value, ty) in object_values.into_iter().zip(object_tys) {
                // TODO should we just call a Move function?
                let recipient = Owner::AddressOwner(recipient.into());
                context.transfer_object(recipient, ty, object_value)?;
            }
            vec![]
        }
        T::Command_::SplitCoins(_, coin, amounts) => {
            // TODO should we just call a Move function?
            if Mode::TRACK_EXECUTION {
                args_to_update.push((coin.value.0.location(), coin.value.1.clone()));
            }
            let coin_ref: CtxValue = context.argument(coin)?;
            let amount_values: Vec<u64> = context.arguments(amounts)?;
            let mut total: u64 = 0;
            for amount in &amount_values {
                let Some(new_total) = total.checked_add(*amount) else {
                    return Err(ExecutionError::from_kind(
                        ExecutionErrorKind::CoinBalanceOverflow,
                    ));
                };
                total = new_total;
            }
            let coin_value = context.copy_value(&coin_ref)?.coin_ref_value()?;
            fp_ensure!(
                coin_value >= total,
                ExecutionError::new_with_source(
                    ExecutionErrorKind::InsufficientCoinBalance,
                    format!("balance: {coin_value} required: {total}")
                )
            );
            coin_ref.coin_ref_subtract_balance(total)?;
            amount_values
                .into_iter()
                .map(|a| context.new_coin(a))
                .collect::<Result<_, _>>()?
        }
        T::Command_::MergeCoins(_, target, coins) => {
            // TODO should we just call a Move function?
            if Mode::TRACK_EXECUTION {
                args_to_update.push((target.value.0.location(), target.value.1.clone()));
            }
            let target_ref: CtxValue = context.argument(target)?;
            let coins = context.arguments(coins)?;
            let amounts = coins
                .into_iter()
                .map(|coin| context.destroy_coin(coin))
                .collect::<Result<Vec<_>, _>>()?;
            let mut additional: u64 = 0;
            for amount in amounts {
                let Some(new_additional) = additional.checked_add(amount) else {
                    return Err(ExecutionError::from_kind(
                        ExecutionErrorKind::CoinBalanceOverflow,
                    ));
                };
                additional = new_additional;
            }
            let target_value = context.copy_value(&target_ref)?.coin_ref_value()?;
            fp_ensure!(
                target_value.checked_add(additional).is_some(),
                ExecutionError::from_kind(ExecutionErrorKind::CoinBalanceOverflow,)
            );
            target_ref.coin_ref_add_balance(additional)?;
            vec![]
        }
        T::Command_::MakeMoveVec(ty, items) => {
            let items: Vec<CtxValue> = context.arguments(items)?;
            vec![CtxValue::vec_pack(ty, items)?]
        }
        T::Command_::Publish(module_bytes, dep_ids, linkage) => {
            let modules =
                context.deserialize_modules(&module_bytes, /* is upgrade */ false)?;

            let runtime_id = context.publish_and_init_package::<Mode>(
                modules,
                &dep_ids,
                linkage,
                trace_builder_opt,
            )?;

            if <Mode>::packages_are_predefined() {
                // no upgrade cap for genesis modules
                std::vec![]
            } else {
                std::vec![context.new_upgrade_cap(runtime_id)?]
            }
        }
        T::Command_::Upgrade(
            module_bytes,
            dep_ids,
            current_package_id,
            upgrade_ticket,
            linkage,
        ) => {
            let upgrade_ticket = context
                .argument::<CtxValue>(upgrade_ticket)?
                .into_upgrade_ticket()?;
            // Make sure the passed-in package ID matches the package ID in the `upgrade_ticket`.
            if current_package_id != upgrade_ticket.package.bytes {
                return Err(ExecutionError::from_kind(
                    ExecutionErrorKind::PackageUpgradeError {
                        upgrade_error: PackageUpgradeError::PackageIDDoesNotMatch {
                            package_id: current_package_id,
                            ticket_id: upgrade_ticket.package.bytes,
                        },
                    },
                ));
            }
            // deserialize modules and charge gas
            let modules = context.deserialize_modules(&module_bytes, /* is upgrade */ true)?;

            let computed_digest = MovePackage::compute_digest_for_modules_and_deps(
                &module_bytes,
                &dep_ids,
                /* hash_modules */ true,
            )
            .to_vec();
            if computed_digest != upgrade_ticket.digest {
                return Err(ExecutionError::from_kind(
                    ExecutionErrorKind::PackageUpgradeError {
                        upgrade_error: PackageUpgradeError::DigestDoesNotMatch {
                            digest: computed_digest,
                        },
                    },
                ));
            }

            let upgraded_package_id = context.upgrade(
                modules,
                &dep_ids,
                current_package_id,
                upgrade_ticket.policy,
                linkage,
            )?;

            vec![context.upgrade_receipt(upgrade_ticket, upgraded_package_id)]
        }
    };
    if Mode::TRACK_EXECUTION {
        let argument_updates = context.location_updates(args_to_update)?;
        let command_result = context.tracked_results(&result, &result_tys)?;
        Mode::finish_command_v2(mode_results, argument_updates, command_result)?;
    }
    context.result(result)?;
    Ok(())
}