sui_adapter_latest/static_programmable_transactions/execution/
context.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::{
5    adapter,
6    execution_mode::ExecutionMode,
7    execution_value::ExecutionState,
8    gas_charger::{GasCharger, GasPayment, PaymentLocation},
9    gas_meter::SuiGasMeter,
10    sp,
11    static_programmable_transactions::{
12        env::Env,
13        execution::{
14            self, trace_utils,
15            values::{Local, Locals, Value},
16        },
17        linkage::resolved_linkage::{ExecutableLinkage, ResolvedLinkage},
18        loading::ast::Datatype,
19        typing::ast::{self as T, Type},
20    },
21};
22use indexmap::{IndexMap, IndexSet};
23use move_binary_format::{
24    CompiledModule,
25    compatibility::{Compatibility, InclusionCheck},
26    errors::{Location, PartialVMError, PartialVMResult, VMResult},
27    file_format::FunctionDefinitionIndex,
28    normalized,
29};
30use move_core_types::{
31    account_address::AccountAddress,
32    identifier::IdentStr,
33    language_storage::{ModuleId, StructTag},
34    u256::U256,
35};
36use move_trace_format::format::MoveTraceBuilder;
37use move_vm_runtime::{
38    execution::{
39        Type as VMType, TypeSubst as _,
40        values::{VMValueCast, Value as VMValue},
41        vm::{LoadedFunctionInformation, MoveVM},
42    },
43    natives::extensions::NativeExtensions,
44    shared::{
45        gas::{GasMeter as _, SimpleInstruction},
46        linkage_context::LinkageHash,
47    },
48    validation::verification::ast::Package as VerifiedPackage,
49};
50use mysten_common::ZipDebugEqIteratorExt;
51use mysten_common::debug_fatal;
52use nonempty::nonempty;
53use quick_cache::unsync::Cache as QCache;
54use serde::{Deserialize, de::DeserializeSeed};
55use std::{
56    cell::RefCell,
57    collections::{BTreeMap, BTreeSet},
58    fmt,
59    rc::Rc,
60    sync::Arc,
61};
62use sui_move_natives::object_runtime::{
63    self, LoadedRuntimeObject, MoveAccumulatorAction, MoveAccumulatorEvent, MoveAccumulatorValue,
64    ObjectRuntime, RuntimeResults, get_all_uids, max_event_error,
65};
66use sui_protocol_config::ProtocolConfig;
67use sui_types::{
68    TypeTag,
69    accumulator_event::AccumulatorEvent,
70    accumulator_root::{self, AccumulatorObjId},
71    balance::Balance,
72    base_types::{
73        MoveObjectType, ObjectID, RESOLVED_ASCII_STR, RESOLVED_UTF8_STR, SequenceNumber,
74        SuiAddress, TxContext,
75    },
76    effects::{AccumulatorAddress, AccumulatorValue, AccumulatorWriteV1},
77    error::{ExecutionError, ExecutionErrorTrait, SafeIndex, command_argument_error},
78    event::Event,
79    execution::{ExecutionResults, ExecutionResultsV2},
80    execution_status::{CommandArgumentError, ExecutionErrorKind, PackageUpgradeError},
81    metrics::ExecutionMetrics,
82    move_package::{
83        MovePackage, UpgradeCap, UpgradePolicy, UpgradeReceipt, UpgradeTicket,
84        normalize_deserialized_modules,
85    },
86    object::{MoveObject, Object, ObjectPermissions, Owner},
87    storage::{BackingPackageStore, DenyListResult, PackageObject, get_package_objects},
88};
89use sui_verifier::INIT_FN_NAME;
90use tracing::instrument;
91
92macro_rules! unwrap {
93    ($e:expr, $($args:expr),* $(,)?) => {
94        match $e {
95            Some(v) => v,
96            None => {
97                invariant_violation!("Unexpected none: {}", format!($($args),*))
98            }
99        }
100
101    };
102}
103
104#[macro_export]
105macro_rules! object_runtime {
106    ($context:ident) => {
107        $context
108            .native_extensions
109            .try_borrow()
110            .map_err(|_| {
111                make_invariant_violation!(
112                    "Should be able to borrow object runtime native extension"
113                )
114            })?
115            .get::<sui_move_natives::object_runtime::ObjectRuntime>()
116            .map_err(|e| {
117                $context
118                    .env
119                    .convert_vm_error(e.finish(move_binary_format::errors::Location::Undefined))
120            })
121    };
122}
123
124macro_rules! object_runtime_mut {
125    ($context:ident) => {
126        $context
127            .native_extensions
128            .try_borrow_mut()
129            .map_err(|_| {
130                make_invariant_violation!(
131                    "Should be able to borrow object runtime native extension"
132                )
133            })?
134            .get_mut::<ObjectRuntime>()
135            .map_err(|e| $context.env.convert_vm_error(e.finish(Location::Undefined)))
136    };
137}
138
139macro_rules! charge_gas_ {
140    ($gas_charger:expr, $env:expr, $call:ident($($args:expr),*)) => {{
141        SuiGasMeter($gas_charger.move_gas_status_mut())
142            .$call($($args),*)
143            .map_err(|e| $env.convert_vm_error(e.finish(Location::Undefined)))
144    }};
145    ($gas_charger:expr, $env:expr, $case:ident, $value_view:expr) => {
146        charge_gas_!($gas_charger, $env, $case($value_view))
147    };
148}
149
150macro_rules! charge_gas {
151    ($context:ident, $case:ident, $value_view:expr) => {{ charge_gas_!($context.gas_charger, $context.env, $case, $value_view) }};
152}
153
154// Helper macro to manage Move VM cache for different linkage contexts. If the given linkage is
155// found the VM is reused, otherwise a new VM is created and inserted into the cache.
156macro_rules! with_vm {
157    ($self:ident, $linkage:expr, $error:ty, $body:expr) => {{
158        let link_context = $linkage.linkage_context::<$error>()?;
159        let linkage_hash = link_context.to_linkage_hash();
160        let mut vm = if let Some((_, vm)) = $self.executable_vm_cache.remove(&linkage_hash) {
161            vm
162        } else {
163            let data_store = &$self.env.linkable_store.package_store;
164            $self
165                .env
166                .vm
167                .make_vm_with_native_extensions(
168                    data_store,
169                    link_context.clone(),
170                    $self.native_extensions.clone(),
171                )
172                .map_err(|e| $self.env.convert_linked_vm_error(e, $linkage))?
173        };
174        let result = $body(&mut vm)?;
175        $self.executable_vm_cache.insert(linkage_hash, vm);
176        Ok(result)
177    }};
178}
179
180/// Type wrapper around Value to ensure safe usage
181#[derive(Debug)]
182pub struct CtxValue(Value);
183
184#[derive(Clone, Debug)]
185pub struct InputObjectMetadata {
186    pub newly_created: bool,
187    pub id: ObjectID,
188    pub refined_permissions: ObjectPermissions,
189    pub owner: Owner,
190    pub version: SequenceNumber,
191    pub type_: Type,
192}
193
194/// Metadata in the case that the GasCoin is transferred, either as an object to another recipient
195/// or as an address balance via `sui::coin::send_funds`. This is needed at the end to
196/// both refund the gas budget and set the correct location from which to charge gas.
197#[derive(Debug, Clone, Copy)]
198pub(crate) enum GasCoinTransfer {
199    /// Sent using the TransferObjects command
200    TransferObjects,
201    /// Sent with the `sui::coin::send_funds` command
202    SendFunds {
203        /// The recipient for `send_funds`.
204        recipient: AccountAddress,
205    },
206}
207
208#[derive(Copy, Clone)]
209enum UsageKind {
210    Move,
211    Copy,
212    Borrow,
213}
214
215// Locals and metadata for all `Location`s. Separated from `Context` for lifetime reasons.
216struct Locations {
217    // A single local for holding the TxContext
218    tx_context_value: Locals,
219    /// The runtime value for the Gas coin, None if no gas coin is provided
220    gas: Option<(GasPayment, InputObjectMetadata, Locals)>,
221    /// The runtime value for the input objects args
222    input_object_metadata: Vec<(T::InputIndex, InputObjectMetadata)>,
223    object_inputs: Locals,
224    input_withdrawal_metadata: Vec<T::WithdrawalInput>,
225    withdrawal_inputs: Locals,
226    pure_input_bytes: IndexSet<Vec<u8>>,
227    pure_input_metadata: Vec<T::PureInput>,
228    pure_inputs: Locals,
229    receiving_input_metadata: Vec<T::ReceivingInput>,
230    receiving_inputs: Locals,
231    /// The results of a given command. For most commands, the inner vector will have length 1.
232    /// It will only not be 1 for Move calls with multiple return values.
233    /// Inner values are None if taken/moved by-value
234    results: Vec<Locals>,
235}
236
237enum ResolvedLocation<'a> {
238    Local(Local<'a>),
239    Pure {
240        bytes: &'a [u8],
241        metadata: &'a T::PureInput,
242        local: Local<'a>,
243    },
244    Receiving {
245        metadata: &'a T::ReceivingInput,
246        local: Local<'a>,
247    },
248}
249
250/// Maintains all runtime state specific to programmable transactions
251pub struct Context<'env, 'pc, 'vm, 'state, 'linkage, 'gas, 'extension, Mode>
252where
253    Mode: ExecutionMode,
254{
255    pub env: &'env Env<'pc, 'vm, 'state, 'linkage, 'extension, Mode>,
256    /// Metrics for reporting exceeded limits
257    pub metrics: Arc<ExecutionMetrics>,
258    pub native_extensions: NativeExtensions<'env>,
259    /// A shared transaction context, contains transaction digest information and manages the
260    /// creation of new object IDs
261    pub tx_context: Rc<RefCell<TxContext>>,
262    /// The gas charger used for metering
263    pub gas_charger: &'gas mut GasCharger,
264    /// User events are claimed after each Move call
265    user_events: Vec<(ModuleId, StructTag, Vec<u8>)>,
266    // runtime data
267    locations: Locations,
268    /// Tracks where the gas coin was sent, if it was moved by value
269    gas_coin_transfer: Option<GasCoinTransfer>,
270    // cache of Move VMs created this transaction for different linkage contexts so that we can reuse them.
271    executable_vm_cache: QCache<LinkageHash, MoveVM<'env>>,
272}
273
274impl Locations {
275    /// NOTE! This does not charge gas and should not be used directly. It is exposed for
276    /// dev-inspect
277    fn resolve(&mut self, location: T::Location) -> Result<ResolvedLocation<'_>, ExecutionError> {
278        Ok(match location {
279            T::Location::TxContext => ResolvedLocation::Local(self.tx_context_value.local(0)?),
280            T::Location::GasCoin => {
281                let (_, _, gas_locals) = unwrap!(self.gas.as_mut(), "Gas coin not provided");
282                ResolvedLocation::Local(gas_locals.local(0)?)
283            }
284            T::Location::ObjectInput(i) => ResolvedLocation::Local(self.object_inputs.local(i)?),
285            T::Location::WithdrawalInput(i) => {
286                ResolvedLocation::Local(self.withdrawal_inputs.local(i)?)
287            }
288            T::Location::Result(i, j) => {
289                let result = unwrap!(self.results.get_mut(i as usize), "bounds already verified");
290                ResolvedLocation::Local(result.local(j)?)
291            }
292            T::Location::PureInput(i) => {
293                let local = self.pure_inputs.local(i)?;
294                let metadata = &self.pure_input_metadata.safe_get(i as usize)?;
295                let bytes = self
296                    .pure_input_bytes
297                    .get_index(metadata.byte_index)
298                    .ok_or_else(|| {
299                        make_invariant_violation!(
300                            "Pure input {} bytes out of bounds at index {}",
301                            metadata.original_input_index.0,
302                            metadata.byte_index,
303                        )
304                    })?;
305                ResolvedLocation::Pure {
306                    bytes,
307                    metadata,
308                    local,
309                }
310            }
311            T::Location::ReceivingInput(i) => ResolvedLocation::Receiving {
312                metadata: self.receiving_input_metadata.safe_get(i as usize)?,
313                local: self.receiving_inputs.local(i)?,
314            },
315        })
316    }
317}
318
319impl<'env, 'pc, 'vm, 'state, 'linkage, 'gas, 'extension, Mode>
320    Context<'env, 'pc, 'vm, 'state, 'linkage, 'gas, 'extension, Mode>
321where
322    Mode: ExecutionMode,
323{
324    #[instrument(name = "Context::new", level = "trace", skip_all)]
325    pub fn new(
326        env: &'env Env<'pc, 'vm, 'state, 'linkage, 'extension, Mode>,
327        metrics: Arc<ExecutionMetrics>,
328        tx_context: Rc<RefCell<TxContext>>,
329        gas_charger: &'gas mut GasCharger,
330        payment_location: Option<GasPayment>,
331        pure_input_bytes: IndexSet<Vec<u8>>,
332        object_inputs: Vec<T::ObjectInput>,
333        input_withdrawal_metadata: Vec<T::WithdrawalInput>,
334        pure_input_metadata: Vec<T::PureInput>,
335        receiving_input_metadata: Vec<T::ReceivingInput>,
336    ) -> Result<Self, Mode::Error>
337    where
338        'pc: 'state,
339    {
340        let mut input_object_map = BTreeMap::new();
341        let mut input_object_metadata = Vec::with_capacity(object_inputs.len());
342        let mut object_values = Vec::with_capacity(object_inputs.len());
343        for object_input in object_inputs {
344            let (i, m, v) = load_object_arg(gas_charger, env, &mut input_object_map, object_input)?;
345            input_object_metadata.push((i, m));
346            object_values.push(Some(v));
347        }
348        let object_inputs = Locals::new(object_values)?;
349        let mut withdrawal_values = Vec::with_capacity(input_withdrawal_metadata.len());
350        for withdrawal_input in &input_withdrawal_metadata {
351            let v = load_withdrawal_arg(gas_charger, env, withdrawal_input)?;
352            withdrawal_values.push(Some(v));
353        }
354        let withdrawal_inputs = Locals::new(withdrawal_values)?;
355        let pure_inputs = Locals::new_invalid(pure_input_metadata.len())?;
356        let receiving_inputs = Locals::new_invalid(receiving_input_metadata.len())?;
357        let mut new_gas_coin_id = None;
358        let gas = match payment_location {
359            Some(gas_payment)
360                if matches!(gas_payment.location, PaymentLocation::AddressBalance(_))
361                    && !env.protocol_config.gasless_transaction_drop_safety() =>
362            {
363                None
364            }
365            Some(gas_payment) => {
366                let ty = env.gas_coin_type()?;
367                let (gas_metadata, gas_value) = match gas_payment.location {
368                    PaymentLocation::AddressBalance(sui_address) => {
369                        assert_invariant!(
370                            env.protocol_config.enable_address_balance_gas_payments(),
371                            "Address balance gas payments must be enabled to have an address \
372                             balance payment location"
373                        );
374                        let max_gas_in_balance = gas_charger.gas_budget();
375                        assert_invariant!(
376                            gas_payment.amount >= max_gas_in_balance,
377                            "not enough gas to pay. How did we get this far?"
378                        );
379                        let id = tx_context.borrow_mut().fresh_id();
380                        new_gas_coin_id = Some(id);
381
382                        let metadata = InputObjectMetadata {
383                            newly_created: true,
384                            id,
385                            refined_permissions: ObjectPermissions::ALL,
386                            owner: Owner::AddressOwner(sui_address),
387                            version: SequenceNumber::new(),
388                            type_: ty,
389                        };
390                        let coin = Value::coin(id, gas_payment.amount);
391                        (metadata, coin)
392                    }
393                    PaymentLocation::Coin(gas_coin_id) => load_object_arg_impl(
394                        gas_charger,
395                        env,
396                        &mut input_object_map,
397                        gas_coin_id,
398                        ObjectPermissions::ALL,
399                        ty,
400                    )?,
401                };
402                let mut gas_locals = Locals::new([Some(gas_value)])?;
403                let mut gas_local = gas_locals.local(0)?;
404                let gas_ref = gas_local.borrow()?;
405                // We have already checked that the gas balance is enough to cover the gas budget
406                let max_gas_in_balance = gas_charger.gas_budget();
407                gas_ref.coin_ref_subtract_balance(max_gas_in_balance)?;
408                Some((gas_payment, gas_metadata, gas_locals))
409            }
410            None => None,
411        };
412        let native_extensions = adapter::new_native_extensions(
413            env.state_view.as_child_resolver(),
414            input_object_map,
415            !gas_charger.is_unmetered(),
416            env.protocol_config,
417            metrics.clone(),
418            tx_context.clone(),
419        )?;
420        if let Some(new_gas_coin_id) = new_gas_coin_id {
421            // If we created a new gas coin for the transaction,
422            // we need to add it to the object runtime
423            native_extensions
424                .try_borrow_mut()
425                .map_err(|_| {
426                    make_invariant_violation!(
427                        "Should be able to borrow object runtime native extension"
428                    )
429                })?
430                .get_mut::<ObjectRuntime>()
431                .and_then(|object_runtime| object_runtime.new_id(new_gas_coin_id))
432                .map_err(|e| env.convert_vm_error(e.finish(Location::Undefined)))?;
433        }
434
435        debug_assert_eq!(gas_charger.move_gas_status().stack_height_current(), 0);
436        let tx_context_value = Locals::new(vec![Some(Value::new_tx_context(
437            tx_context.borrow().digest(),
438        )?)])?;
439        Ok(Self {
440            env,
441            metrics,
442            native_extensions,
443            tx_context,
444            gas_charger,
445            user_events: vec![],
446            locations: Locations {
447                tx_context_value,
448                gas,
449                input_object_metadata,
450                object_inputs,
451                input_withdrawal_metadata,
452                withdrawal_inputs,
453                pure_input_bytes,
454                pure_input_metadata,
455                pure_inputs,
456                receiving_input_metadata,
457                receiving_inputs,
458                results: vec![],
459            },
460            gas_coin_transfer: None,
461            executable_vm_cache: QCache::new(1024),
462        })
463    }
464
465    pub(crate) fn record_gas_coin_transfer(
466        &mut self,
467        transfer: GasCoinTransfer,
468    ) -> Result<(), Mode::Error> {
469        // send funds transfer ==> accumulators/address balances are enabled
470        assert_invariant!(
471            !matches!(transfer, GasCoinTransfer::SendFunds { .. })
472                || self.env.protocol_config.enable_accumulators(),
473            "Gas coin transfers with send_funds are not allowed unless accumulators are enabled"
474        );
475        if self.gas_coin_transfer.is_some() {
476            invariant_violation!("Gas coin destination set more than once");
477        }
478        self.gas_coin_transfer = Some(transfer);
479        Ok(())
480    }
481
482    pub fn finish(mut self) -> Result<ExecutionResults, Mode::Error> {
483        assert_invariant!(
484            !self.locations.tx_context_value.local(0)?.is_invalid()?,
485            "tx context value should be present"
486        );
487        let gas_coin_transfer = self.gas_coin_transfer;
488        let gas = std::mem::take(&mut self.locations.gas);
489        let object_input_metadata = std::mem::take(&mut self.locations.input_object_metadata);
490        let mut object_inputs =
491            std::mem::replace(&mut self.locations.object_inputs, Locals::new_invalid(0)?);
492        let mut created_input_object_ids = BTreeSet::new();
493        let mut loaded_runtime_objects = BTreeMap::new();
494        let mut by_value_shared_objects = BTreeSet::new();
495        let mut consensus_owner_objects = BTreeMap::new();
496        let mut gas_payment_location = None;
497        let gas = gas
498            .map(|(payment_location, m, mut g)| {
499                gas_payment_location = Some(payment_location);
500                let value_opt = g.local(0)?.move_if_valid()?;
501                let moved = value_opt.is_none();
502                assert_invariant!(
503                    moved == gas_coin_transfer.is_some(),
504                    "Gas coin moved requires gas coin transfer to be recorded, and vice versa"
505                );
506                Result::<_, ExecutionError>::Ok((m, value_opt))
507            })
508            .transpose()?;
509
510        let gas_id_opt = gas.as_ref().map(|(m, _)| m.id);
511        let object_inputs = object_input_metadata
512            .into_iter()
513            .enumerate()
514            .map(|(i, (_, m))| {
515                let v_opt = object_inputs.local(checked_as!(i, u16)?)?.move_if_valid()?;
516                Ok((m, v_opt))
517            })
518            .collect::<Result<Vec<_>, ExecutionError>>()?;
519        for (metadata, value_opt) in object_inputs.into_iter().chain(gas) {
520            let InputObjectMetadata {
521                newly_created,
522                id,
523                refined_permissions,
524                owner,
525                version,
526                type_,
527            } = metadata;
528            if !refined_permissions.can_use_mutably() {
529                continue;
530            }
531
532            if newly_created {
533                created_input_object_ids.insert(id);
534            } else {
535                loaded_runtime_objects.insert(
536                    id,
537                    LoadedRuntimeObject {
538                        version,
539                        is_modified: true,
540                    },
541                );
542            }
543            if let Some(object) = value_opt {
544                self.transfer_object_(
545                    owner,
546                    type_,
547                    CtxValue(object),
548                    /* end of transaction */ true,
549                )?;
550            } else if owner.is_shared() {
551                by_value_shared_objects.insert(id);
552            } else if matches!(owner, Owner::ConsensusAddressOwner { .. }) {
553                consensus_owner_objects.insert(id, owner.clone());
554            }
555        }
556
557        let Self {
558            env,
559            native_extensions,
560            tx_context,
561            gas_charger,
562            user_events,
563            ..
564        } = self;
565        let ref_context: &RefCell<TxContext> = &tx_context;
566        let tx_context: &TxContext = &ref_context.borrow();
567        let tx_digest = ref_context.borrow().digest();
568
569        let object_runtime: ObjectRuntime = native_extensions
570            .try_borrow_mut()
571            .map_err(|_| {
572                make_invariant_violation!(
573                "Should be able to borrow object runtime native extension at the end of execution"
574            )
575            })?
576            .remove()
577            .map_err(|e| env.convert_vm_error(e.finish(Location::Undefined)))?;
578
579        let RuntimeResults {
580            mut writes,
581            user_events: remaining_events,
582            loaded_child_objects,
583            mut created_object_ids,
584            deleted_object_ids,
585            mut accumulator_events,
586            settlement_input_sui,
587            settlement_output_sui,
588        } = object_runtime.finish()?;
589        assert_invariant!(
590            loaded_runtime_objects
591                .keys()
592                .all(|id| !created_object_ids.contains(id)),
593            "Loaded input objects should not be in the created objects set"
594        );
595        // TODO generalize post transaction checks using permissions
596
597        assert_invariant!(
598            remaining_events.is_empty(),
599            "Events should be taken after every Move call"
600        );
601        // Refund unused gas to the coin, real or ephemeral
602        if let Some(gas_id) = gas_id_opt {
603            // deleted implies was moved and used in send_funds
604            assert_invariant!(
605                !deleted_object_ids.contains(&gas_id)
606                    || gas_coin_transfer.is_some_and(|destination| matches!(
607                        destination,
608                        GasCoinTransfer::SendFunds { .. }
609                    )),
610                "Gas coin should not be deleted"
611            );
612            let Some(gas_payment_location) = gas_payment_location else {
613                invariant_violation!("Gas payment should be specified if gas ID is present");
614            };
615            finish_gas_coin(
616                gas_charger,
617                &mut writes,
618                &mut created_object_ids,
619                &deleted_object_ids,
620                &mut accumulator_events,
621                gas_id,
622                gas_payment_location,
623                gas_coin_transfer,
624            )?;
625        }
626
627        loaded_runtime_objects.extend(loaded_child_objects);
628
629        let mut written_objects = BTreeMap::new();
630
631        let (writeout_vm, ty_linkage) =
632            Self::make_writeout_vm(env, writes.values().map(|(_, ty, _)| ty.clone()))?;
633
634        for (id, (recipient, ty, value)) in writes {
635            let (ty, layout) = Self::load_type_and_layout_from_struct_for_writeout(
636                env,
637                &writeout_vm,
638                &ty_linkage,
639                ty.clone().into(),
640            )?;
641            let abilities = ty.abilities();
642            let has_public_transfer = abilities.has_store();
643            let Some(bytes) = value.typed_serialize(&layout) else {
644                invariant_violation!("Failed to serialize already deserialized Move value");
645            };
646            // safe because has_public_transfer has been determined by the abilities
647            let move_object = unsafe {
648                create_written_object::<Mode>(
649                    env,
650                    &loaded_runtime_objects,
651                    id,
652                    ty,
653                    has_public_transfer,
654                    bytes,
655                )?
656            };
657            let object = Object::new_move(move_object, recipient, tx_digest);
658            written_objects.insert(id, object);
659        }
660
661        for package in self
662            .env
663            .linkable_store
664            .package_store
665            .to_new_packages()
666            .into_iter()
667        {
668            let package_obj = Object::new_from_package(package, tx_digest);
669            let id = package_obj.id();
670            created_object_ids.insert(id);
671            written_objects.insert(id, package_obj);
672        }
673
674        Ok(execution::context::finish(
675            env.protocol_config,
676            env.state_view,
677            gas_charger,
678            tx_context,
679            &by_value_shared_objects,
680            &consensus_owner_objects,
681            loaded_runtime_objects,
682            written_objects,
683            created_object_ids,
684            deleted_object_ids,
685            user_events,
686            accumulator_events,
687            settlement_input_sui,
688            settlement_output_sui,
689        )?)
690    }
691
692    pub fn take_user_events(
693        &mut self,
694        vm: &MoveVM<'_>,
695        version_mid: ModuleId,
696        function_def_idx: FunctionDefinitionIndex,
697        instr_length: u16,
698        linkage: &ExecutableLinkage,
699    ) -> Result<(), Mode::Error> {
700        let events = object_runtime_mut!(self)?.take_user_events();
701        let Some(num_events) = self.user_events.len().checked_add(events.len()) else {
702            invariant_violation!("usize overflow, too many events emitted")
703        };
704        let max_events = self.env.protocol_config.max_num_event_emit();
705        if num_events as u64 > max_events {
706            let err = max_event_error(max_events)
707                .at_code_offset(function_def_idx, instr_length)
708                .finish(Location::Module(version_mid.clone()));
709            return Err(self.env.convert_linked_vm_error(err, linkage));
710        }
711        let new_events = events
712            .into_iter()
713            .map(|(tag, value)| {
714                let type_tag = TypeTag::Struct(Box::new(tag));
715                let layout = vm
716                    .runtime_type_layout(&type_tag)
717                    .map_err(|e| self.env.convert_linked_vm_error(e, linkage))?;
718                let Some(bytes) = value.typed_serialize(&layout) else {
719                    invariant_violation!("Failed to serialize Move event");
720                };
721                let TypeTag::Struct(tag) = type_tag else {
722                    unreachable!()
723                };
724                Ok((version_mid.clone(), *tag, bytes))
725            })
726            .collect::<Result<Vec<_>, Mode::Error>>()?;
727        self.user_events.extend(new_events);
728        Ok(())
729    }
730
731    //
732    // Final serialization of written objects
733    //
734
735    /// The writeout VM is used to serialize all written objects at the end of execution. This
736    /// needs access to all types that were written during the transaction [`writes`]. Importantly,
737    /// it needs to be able to create a VM over any newly published packages as the `init`
738    /// functions in those packages may have created objects of types defined in those packages.
739    fn make_writeout_vm<I>(
740        env: &Env<'pc, 'vm, 'state, 'linkage, 'extension, Mode>,
741        writes: I,
742    ) -> Result<(MoveVM<'extension>, ExecutableLinkage), Mode::Error>
743    where
744        I: IntoIterator<Item = MoveObjectType>,
745    {
746        let tys_addrs = writes
747            .into_iter()
748            .flat_map(|ty| StructTag::from(ty).all_addresses())
749            .map(ObjectID::from)
750            .collect::<BTreeSet<_>>();
751
752        let ty_linkage = ExecutableLinkage::type_linkage::<_, Mode::Error>(
753            env.linkage_analysis.config().clone(),
754            &tys_addrs,
755            env.linkable_store,
756        )?;
757        env.vm
758            .make_vm(
759                &env.linkable_store.package_store,
760                ty_linkage.linkage_context::<Mode::Error>()?,
761            )
762            .map_err(|e| env.convert_linked_vm_error(e, &ty_linkage))
763            .map(|vm| (vm, ty_linkage))
764    }
765
766    /// Load the type and layout for a struct tag.
767    /// It is important that this use the VM passed in, and not the `resolution_vm` in the `env` as
768    /// the types requested may have only been created during the execution of the transaction and
769    /// therefore will not be present in the `resolution_vm`.
770    fn load_type_and_layout_from_struct_for_writeout(
771        env: &Env<'pc, 'vm, 'state, 'linkage, 'extension, Mode>,
772        vm: &MoveVM,
773        linkage: &ExecutableLinkage,
774        tag: StructTag,
775    ) -> Result<(Type, move_core_types::runtime_value::MoveTypeLayout), Mode::Error> {
776        let type_tag = TypeTag::Struct(Box::new(tag));
777        let vm_type = vm
778            .load_type(&type_tag)
779            .map_err(|e| env.convert_linked_vm_error(e, linkage))?;
780        let layout = vm
781            .runtime_type_layout(&type_tag)
782            .map_err(|e| env.convert_vm_error(e))?;
783        env.adapter_type_from_vm_type(vm, &vm_type)
784            .map(|ty| (ty, layout))
785    }
786
787    //
788    // Arguments and Values
789    //
790
791    fn location(&mut self, usage: UsageKind, location: T::Location) -> Result<Value, Mode::Error> {
792        let resolved = self.locations.resolve(location)?;
793        let mut local = match resolved {
794            ResolvedLocation::Local(l) => l,
795            ResolvedLocation::Pure {
796                bytes,
797                metadata,
798                mut local,
799            } => {
800                if local.is_invalid()? {
801                    let v = load_pure_value(self.gas_charger, self.env, bytes, metadata)?;
802                    local.store(v)?;
803                }
804                local
805            }
806            ResolvedLocation::Receiving {
807                metadata,
808                mut local,
809            } => {
810                if local.is_invalid()? {
811                    let v = load_receiving_value(self.gas_charger, self.env, metadata)?;
812                    local.store(v)?;
813                }
814                local
815            }
816        };
817        Ok(match usage {
818            UsageKind::Move => {
819                let value = local.move_()?;
820                charge_gas_!(self.gas_charger, self.env, charge_move_loc, &value)?;
821                value
822            }
823            UsageKind::Copy => {
824                let value = local.copy()?;
825                charge_gas_!(self.gas_charger, self.env, charge_copy_loc, &value)?;
826                value
827            }
828            UsageKind::Borrow => {
829                charge_gas_!(
830                    self.gas_charger,
831                    self.env,
832                    charge_simple_instr(SimpleInstruction::MutBorrowLoc)
833                )?;
834                local.borrow()?
835            }
836        })
837    }
838
839    fn location_usage(&mut self, usage: T::Usage) -> Result<Value, Mode::Error> {
840        match usage {
841            T::Usage::Move(location) => self.location(UsageKind::Move, location),
842            T::Usage::Copy { location, .. } => self.location(UsageKind::Copy, location),
843        }
844    }
845
846    fn argument_value(&mut self, sp!(_, (arg_, _)): T::Argument) -> Result<Value, Mode::Error> {
847        match arg_ {
848            T::Argument__::Use(usage) => self.location_usage(usage),
849            // freeze is a no-op for references since the value does not track mutability
850            T::Argument__::Freeze(usage) => self.location_usage(usage),
851            T::Argument__::Borrow(_, location) => self.location(UsageKind::Borrow, location),
852            T::Argument__::Read(usage) => {
853                let reference = self.location_usage(usage)?;
854                charge_gas!(self, charge_read_ref, &reference)?;
855                Ok(reference.read_ref()?)
856            }
857        }
858    }
859
860    pub fn argument<V>(&mut self, arg: T::Argument) -> Result<V, Mode::Error>
861    where
862        VMValue: VMValueCast<V>,
863    {
864        let before_height = self.gas_charger.move_gas_status().stack_height_current();
865        let value = self.argument_value(arg)?;
866        let after_height = self.gas_charger.move_gas_status().stack_height_current();
867        debug_assert_eq!(before_height.saturating_add(1), after_height);
868        let value: V = value.cast()?;
869        Ok(value)
870    }
871
872    pub fn arguments<V>(&mut self, args: Vec<T::Argument>) -> Result<Vec<V>, Mode::Error>
873    where
874        VMValue: VMValueCast<V>,
875    {
876        args.into_iter().map(|arg| self.argument(arg)).collect()
877    }
878
879    pub fn result(&mut self, result: Vec<Option<CtxValue>>) -> Result<(), Mode::Error> {
880        self.locations
881            .results
882            .push(Locals::new(result.into_iter().map(|v| v.map(|v| v.0)))?);
883        Ok(())
884    }
885
886    pub fn charge_command(
887        &mut self,
888        is_move_call: bool,
889        num_args: usize,
890        num_return: usize,
891    ) -> Result<(), Mode::Error> {
892        let move_gas_status = self.gas_charger.move_gas_status_mut();
893        let before_size = move_gas_status.stack_size_current();
894        // Pop all of the arguments
895        // If the return values came from the Move VM directly (via a Move call), pop those
896        // as well
897        let num_popped = if is_move_call {
898            num_args.checked_add(num_return).ok_or_else(|| {
899                make_invariant_violation!("usize overflow when charging gas for command",)
900            })?
901        } else {
902            num_args
903        };
904        move_gas_status
905            .charge(1, 0, num_popped as u64, 0, /* unused */ 1)
906            .map_err(|e| self.env.convert_vm_error(e.finish(Location::Undefined)))?;
907        let after_size = move_gas_status.stack_size_current();
908        assert_invariant!(
909            before_size == after_size,
910            "We assume currently that the stack size is not decremented. \
911            If this changes, we need to actually account for it here"
912        );
913        Ok(())
914    }
915
916    pub fn copy_value(&mut self, value: &CtxValue) -> Result<CtxValue, Mode::Error> {
917        Ok(CtxValue(copy_value(self.gas_charger, self.env, &value.0)?))
918    }
919
920    pub fn new_coin(&mut self, amount: u64) -> Result<CtxValue, Mode::Error> {
921        let id = self.tx_context.borrow_mut().fresh_id();
922        object_runtime_mut!(self)?
923            .new_id(id)
924            .map_err(|e| self.env.convert_vm_error(e.finish(Location::Undefined)))?;
925        Ok(CtxValue(Value::coin(id, amount)))
926    }
927
928    pub fn destroy_coin(&mut self, coin: CtxValue) -> Result<u64, Mode::Error> {
929        let (id, amount) = coin.0.unpack_coin()?;
930        object_runtime_mut!(self)?
931            .delete_id(id)
932            .map_err(|e| self.env.convert_vm_error(e.finish(Location::Undefined)))?;
933        Ok(amount)
934    }
935
936    pub fn new_upgrade_cap(&mut self, version_id: ObjectID) -> Result<CtxValue, Mode::Error> {
937        let id = self.tx_context.borrow_mut().fresh_id();
938        object_runtime_mut!(self)?
939            .new_id(id)
940            .map_err(|e| self.env.convert_vm_error(e.finish(Location::Undefined)))?;
941        let cap = UpgradeCap::new(id, version_id);
942        Ok(CtxValue(Value::upgrade_cap(cap)))
943    }
944
945    pub fn upgrade_receipt(
946        &self,
947        upgrade_ticket: UpgradeTicket,
948        upgraded_package_id: ObjectID,
949    ) -> CtxValue {
950        let receipt = UpgradeReceipt::new(upgrade_ticket, upgraded_package_id);
951        CtxValue(Value::upgrade_receipt(receipt))
952    }
953
954    //
955    // Move calls
956    //
957
958    pub fn vm_move_call(
959        &mut self,
960        function: T::LoadedFunction,
961        args: Vec<CtxValue>,
962        trace_builder_opt: &mut Option<MoveTraceBuilder>,
963    ) -> Result<Vec<CtxValue>, Mode::Error> {
964        with_vm!(self, &function.linkage, Mode::Error, |vm: &mut MoveVM<
965            'env,
966        >| {
967            let ty_args = function
968                .type_arguments
969                .iter()
970                .map(|ty| {
971                    let tag: TypeTag = ty.clone().try_into().map_err(|e| {
972                        Mode::Error::new_with_source(ExecutionErrorKind::VMInvariantViolation, e)
973                    })?;
974                    vm.load_type(&tag)
975                        .map_err(|e| self.env.convert_linked_vm_error(e, &function.linkage))
976                })
977                .collect::<Result<Vec<_>, Mode::Error>>()?;
978            let result = self.execute_function_bypass_visibility_with_vm(
979                vm,
980                &function.original_mid,
981                &function.name,
982                ty_args,
983                args,
984                &function.linkage,
985                trace_builder_opt,
986            )?;
987            self.take_user_events(
988                vm,
989                function.version_mid,
990                function.definition_index,
991                function.instruction_length,
992                &function.linkage,
993            )?;
994            Ok::<Vec<CtxValue>, Mode::Error>(result)
995        })
996    }
997
998    fn execute_function_bypass_visibility_with_vm(
999        &mut self,
1000        vm: &mut MoveVM<'env>,
1001        original_mid: &ModuleId,
1002        function_name: &IdentStr,
1003        ty_args: Vec<VMType>,
1004        args: Vec<CtxValue>,
1005        linkage: &ExecutableLinkage,
1006        tracer: &mut Option<MoveTraceBuilder>,
1007    ) -> Result<Vec<CtxValue>, Mode::Error> {
1008        let gas_status = self.gas_charger.move_gas_status_mut();
1009        let values = vm
1010            .execute_function_bypass_visibility(
1011                original_mid,
1012                function_name,
1013                ty_args,
1014                args.into_iter().map(|v| v.0.into()).collect(),
1015                &mut SuiGasMeter(gas_status),
1016                tracer.as_mut(),
1017            )
1018            .map_err(|e| self.env.convert_linked_vm_error(e, linkage))?;
1019        Ok(values.into_iter().map(|v| CtxValue(v.into())).collect())
1020    }
1021
1022    //
1023    // Publish and Upgrade
1024    //
1025
1026    // is_upgrade is used for gas charging. Assumed to be a new publish if false.
1027    pub fn deserialize_modules(
1028        &mut self,
1029        module_bytes: &[Vec<u8>],
1030        is_upgrade: bool,
1031    ) -> Result<Vec<CompiledModule>, Mode::Error> {
1032        assert_invariant!(
1033            !module_bytes.is_empty(),
1034            "empty package is checked in transaction input checker"
1035        );
1036        let total_bytes = module_bytes.iter().map(|v| v.len()).sum();
1037        if is_upgrade {
1038            self.gas_charger.charge_upgrade_package(total_bytes)?
1039        } else {
1040            self.gas_charger.charge_publish_package(total_bytes)?
1041        }
1042
1043        let binary_config = self.env.protocol_config.binary_config(None);
1044        let modules = module_bytes
1045            .iter()
1046            .map(|b| {
1047                CompiledModule::deserialize_with_config(b, &binary_config)
1048                    .map_err(|e| e.finish(Location::Undefined))
1049            })
1050            .collect::<VMResult<Vec<CompiledModule>>>()
1051            .map_err(|e| self.env.convert_vm_error(e))?;
1052        Ok(modules)
1053    }
1054
1055    fn fetch_package(&mut self, dependency_id: &ObjectID) -> Result<Rc<MovePackage>, Mode::Error> {
1056        let [fetched_package] = self.fetch_packages(&[*dependency_id])?.try_into().map_err(
1057            |_| {
1058                make_invariant_violation!(
1059                    "We should always fetch a single package for each object or return a dependency error."
1060                )
1061            },
1062        )?;
1063        Ok(fetched_package)
1064    }
1065
1066    fn fetch_packages(
1067        &mut self,
1068        dependency_ids: &[ObjectID],
1069    ) -> Result<Vec<Rc<MovePackage>>, Mode::Error> {
1070        let mut fetched = vec![];
1071        let mut missing = vec![];
1072
1073        // Collect into a set to avoid duplicate fetches and preserve existing behavior
1074        let dependency_ids: BTreeSet<_> = dependency_ids.iter().collect();
1075
1076        for id in &dependency_ids {
1077            match self.env.linkable_store.get_move_package(id) {
1078                Err(e) => {
1079                    return Err(Mode::Error::new_with_source(
1080                        ExecutionErrorKind::PublishUpgradeMissingDependency,
1081                        e,
1082                    ));
1083                }
1084                Ok(Some(inner)) => {
1085                    fetched.push(inner);
1086                }
1087                Ok(None) => {
1088                    missing.push(*id);
1089                }
1090            }
1091        }
1092
1093        if missing.is_empty() {
1094            assert_invariant!(
1095                fetched.len() == dependency_ids.len(),
1096                "all dependencies should be fetched"
1097            );
1098            Ok(fetched)
1099        } else {
1100            let msg = format!(
1101                "Missing dependencies: {}",
1102                missing
1103                    .into_iter()
1104                    .map(|dep| format!("{}", dep))
1105                    .collect::<Vec<_>>()
1106                    .join(", ")
1107            );
1108            Err(Mode::Error::new_with_source(
1109                ExecutionErrorKind::PublishUpgradeMissingDependency,
1110                msg,
1111            ))
1112        }
1113    }
1114
1115    fn publish_and_verify_modules(
1116        &mut self,
1117        package_id: ObjectID,
1118        pkg: &MovePackage,
1119        modules: &[CompiledModule],
1120        linkage: &ExecutableLinkage,
1121    ) -> Result<(VerifiedPackage, MoveVM<'env>), Mode::Error> {
1122        let serialized_pkg = pkg.into_serialized_move_package().map_err(|e| {
1123            make_invariant_violation!("Failed to serialize package for verification: {}", e)
1124        })?;
1125        let data_store = &self.env.linkable_store.package_store;
1126        let vm = self
1127            .env
1128            .vm
1129            .validate_package(
1130                data_store,
1131                *package_id,
1132                serialized_pkg,
1133                &mut SuiGasMeter(self.gas_charger.move_gas_status_mut()),
1134                self.native_extensions.clone(),
1135            )
1136            .map_err(|e| self.env.convert_linked_vm_error(e, linkage))?;
1137
1138        // run the Sui verifier
1139        for module in modules {
1140            // Run Sui bytecode verifier, which runs some additional checks that assume the Move
1141            // bytecode verifier has passed.
1142            sui_verifier::verifier::sui_verify_module_unmetered(
1143                module,
1144                &BTreeMap::new(),
1145                &self
1146                    .env
1147                    .protocol_config
1148                    .verifier_config(/* signing_limits */ None),
1149            )?;
1150        }
1151
1152        Ok(vm)
1153    }
1154
1155    fn init_modules(
1156        &mut self,
1157        mut vm: MoveVM<'env>,
1158        package_id: ObjectID,
1159        modules: &[CompiledModule],
1160        linkage: &ExecutableLinkage,
1161        trace_builder_opt: &mut Option<MoveTraceBuilder>,
1162    ) -> Result<(), Mode::Error> {
1163        for module in modules {
1164            let Some((fdef_idx, fdef)) = module.find_function_def_by_name(INIT_FN_NAME.as_str())
1165            else {
1166                continue;
1167            };
1168            let fhandle = module.function_handle_at(fdef.function);
1169            let fparameters = module.signature_at(fhandle.parameters);
1170            assert_invariant!(
1171                fparameters.0.len() <= 2,
1172                "init function should have at most 2 parameters"
1173            );
1174            let has_otw = fparameters.0.len() == 2;
1175            let tx_context = self
1176                .location(UsageKind::Borrow, T::Location::TxContext)
1177                .map_err(|e| {
1178                    make_invariant_violation!("Failed to get tx context for init function: {}", e)
1179                })?;
1180            // balance the stack after borrowing the tx context
1181            charge_gas!(self, charge_store_loc, &tx_context)?;
1182
1183            let args = if has_otw {
1184                vec![CtxValue(Value::one_time_witness()?), CtxValue(tx_context)]
1185            } else {
1186                vec![CtxValue(tx_context)]
1187            };
1188            debug_assert_eq!(self.gas_charger.move_gas_status().stack_height_current(), 0);
1189            trace_utils::trace_move_call_start(trace_builder_opt);
1190            let return_values = self.execute_function_bypass_visibility_with_vm(
1191                &mut vm,
1192                &module.self_id(),
1193                INIT_FN_NAME,
1194                vec![],
1195                args,
1196                linkage,
1197                trace_builder_opt,
1198            )?;
1199            trace_utils::trace_move_call_end(trace_builder_opt);
1200
1201            let version_mid = ModuleId::new(package_id.into(), module.self_id().name().to_owned());
1202            self.take_user_events(
1203                &vm,
1204                version_mid,
1205                fdef_idx,
1206                fdef.code
1207                    .as_ref()
1208                    .map(|c| checked_as!(c.code.len(), u16))
1209                    .transpose()?
1210                    .unwrap_or(0),
1211                linkage,
1212            )?;
1213            assert_invariant!(
1214                return_values.is_empty(),
1215                "init should not have return values"
1216            );
1217            debug_assert_eq!(self.gas_charger.move_gas_status().stack_height_current(), 0);
1218        }
1219
1220        Ok(())
1221    }
1222
1223    pub fn publish_and_init_package(
1224        &mut self,
1225        mut modules: Vec<CompiledModule>,
1226        dep_ids: &[ObjectID],
1227        linkage: ResolvedLinkage,
1228        trace_builder_opt: &mut Option<MoveTraceBuilder>,
1229    ) -> Result<ObjectID, Mode::Error> {
1230        let original_id = if Mode::packages_are_predefined() {
1231            // do not calculate or substitute id for predefined packages
1232            (*modules.safe_get(0)?.self_id().address()).into()
1233        } else {
1234            // It should be fine that this does not go through the object runtime since it does not
1235            // need to know about new packages created, since Move objects and Move packages
1236            // cannot interact
1237            let id = self.tx_context.borrow_mut().fresh_id();
1238            adapter::substitute_package_id(&mut modules, id)?;
1239            id
1240        };
1241
1242        let dependencies = self.fetch_packages(dep_ids)?;
1243        let package = Rc::new(MovePackage::new_initial(
1244            &modules,
1245            self.env.protocol_config,
1246            dependencies.iter().map(|p| p.as_ref()),
1247        )?);
1248        let package_id = package.id();
1249
1250        let linkage = ResolvedLinkage::update_for_publication(package_id, original_id, linkage);
1251
1252        let (pkg, vm) =
1253            self.publish_and_verify_modules(original_id, &package, &modules, &linkage)?;
1254        // Here we optimistically push the package that is being published/upgraded
1255        // and if there is an error of any kind (verification or module init) we
1256        // remove it.
1257        // The call to `pop_last_package` later is fine because we cannot re-enter and
1258        // the last package we pushed is the one we are verifying and running the init from
1259        self.env
1260            .linkable_store
1261            .package_store
1262            .push_package(package_id, package.clone(), pkg)?;
1263
1264        match self.init_modules(vm, package_id, &modules, &linkage, trace_builder_opt) {
1265            Ok(()) => Ok(original_id),
1266            Err(e) => {
1267                self.env
1268                    .linkable_store
1269                    .package_store
1270                    .pop_package(package_id)?;
1271                Err(e)
1272            }
1273        }
1274    }
1275
1276    pub fn upgrade(
1277        &mut self,
1278        mut modules: Vec<CompiledModule>,
1279        dep_ids: &[ObjectID],
1280        current_package_id: ObjectID,
1281        upgrade_ticket_policy: u8,
1282        linkage: ResolvedLinkage,
1283    ) -> Result<ObjectID, Mode::Error> {
1284        // Check that this package ID points to a package and get the package we're upgrading.
1285        let current_move_package = self.fetch_package(&current_package_id)?;
1286
1287        let original_id = current_move_package.original_package_id();
1288        adapter::substitute_package_id(&mut modules, original_id)?;
1289
1290        // Upgraded packages share their predecessor's runtime ID but get a new storage ID.
1291        // It should be fine that this does not go through the object runtime since it does not
1292        // need to know about new packages created, since Move objects and Move packages
1293        // cannot interact
1294        let version_id = self.tx_context.borrow_mut().fresh_id();
1295
1296        let dependencies = self.fetch_packages(dep_ids)?;
1297        let package = current_move_package.new_upgraded(
1298            version_id,
1299            &modules,
1300            self.env.protocol_config,
1301            dependencies.iter().map(|p| p.as_ref()),
1302        )?;
1303
1304        let linkage = ResolvedLinkage::update_for_publication(version_id, original_id, linkage);
1305        let (verified_pkg, _) =
1306            self.publish_and_verify_modules(original_id, &package, &modules, &linkage)?;
1307
1308        check_compatibility(
1309            self.env.protocol_config,
1310            current_move_package.as_ref(),
1311            &modules,
1312            upgrade_ticket_policy,
1313        )?;
1314
1315        // find newly added modules to the package,
1316        // and error if they have init functions
1317        let current_module_names: BTreeSet<&str> = current_move_package
1318            .serialized_module_map()
1319            .keys()
1320            .map(|s| s.as_str())
1321            .collect();
1322        let upgrade_module_names: BTreeSet<&str> = package
1323            .serialized_module_map()
1324            .keys()
1325            .map(|s| s.as_str())
1326            .collect();
1327        let new_module_names = upgrade_module_names
1328            .difference(&current_module_names)
1329            .copied()
1330            .collect::<BTreeSet<&str>>();
1331        let new_modules = modules
1332            .iter()
1333            .filter(|m| {
1334                let name = m.identifier_at(m.self_handle().name).as_str();
1335                new_module_names.contains(name)
1336            })
1337            .collect::<Vec<&CompiledModule>>();
1338        let new_module_has_init = new_modules.iter().any(|module| {
1339            module.function_defs.iter().any(|fdef| {
1340                let fhandle = module.function_handle_at(fdef.function);
1341                let fname = module.identifier_at(fhandle.name);
1342                fname == INIT_FN_NAME
1343            })
1344        });
1345        if new_module_has_init {
1346            // TODO we cannot run 'init' on upgrade yet due to global type cache limitations
1347            return Err(Mode::Error::new_with_source(
1348                ExecutionErrorKind::FeatureNotYetSupported,
1349                "`init` in new modules on upgrade is not yet supported",
1350            ));
1351        }
1352
1353        self.env.linkable_store.package_store.push_package(
1354            version_id,
1355            Rc::new(package),
1356            verified_pkg,
1357        )?;
1358        Ok(version_id)
1359    }
1360
1361    //
1362    // Commands
1363    //
1364
1365    pub fn transfer_object(
1366        &mut self,
1367        recipient: Owner,
1368        ty: Type,
1369        object: CtxValue,
1370    ) -> Result<(), Mode::Error> {
1371        self.transfer_object_(recipient, ty, object, /* end of transaction */ false)
1372    }
1373
1374    fn transfer_object_(
1375        &mut self,
1376        recipient: Owner,
1377        ty: Type,
1378        object: CtxValue,
1379        end_of_transaction: bool,
1380    ) -> Result<(), Mode::Error> {
1381        let tag = TypeTag::try_from(ty)
1382            .map_err(|_| make_invariant_violation!("Unable to convert Type to TypeTag"))?;
1383        let TypeTag::Struct(tag) = tag else {
1384            invariant_violation!("Expected struct type tag");
1385        };
1386        let ty = MoveObjectType::from(*tag);
1387        object_runtime_mut!(self)?
1388            .transfer(recipient, ty, object.0.into(), end_of_transaction)
1389            .map_err(|e| self.env.convert_vm_error(e.finish(Location::Undefined)))?;
1390        Ok(())
1391    }
1392
1393    //
1394    // Dev Inspect tracking
1395    //
1396
1397    #[allow(clippy::type_complexity)]
1398    pub fn argument_updates(
1399        &mut self,
1400        args: Vec<T::Argument>,
1401    ) -> Result<Vec<(sui_types::transaction::Argument, Vec<u8>, TypeTag)>, Mode::Error> {
1402        args.into_iter()
1403            .filter_map(|arg| self.argument_update(arg).transpose())
1404            .collect()
1405    }
1406
1407    #[allow(clippy::type_complexity)]
1408    fn argument_update(
1409        &mut self,
1410        sp!(_, (arg, ty)): T::Argument,
1411    ) -> Result<Option<(sui_types::transaction::Argument, Vec<u8>, TypeTag)>, Mode::Error> {
1412        use sui_types::transaction::Argument as TxArgument;
1413        let ty = match ty {
1414            Type::Reference(true, inner) => (*inner).clone(),
1415            ty => {
1416                debug_assert!(
1417                    false,
1418                    "Unexpected non reference type in location update: {ty:?}"
1419                );
1420                return Ok(None);
1421            }
1422        };
1423        let Ok(tag): Result<TypeTag, _> = ty.clone().try_into() else {
1424            invariant_violation!("unable to generate type tag from type")
1425        };
1426        let location = arg.location();
1427        let resolved = self.locations.resolve(location)?;
1428        let local = match resolved {
1429            ResolvedLocation::Local(local)
1430            | ResolvedLocation::Pure { local, .. }
1431            | ResolvedLocation::Receiving { local, .. } => local,
1432        };
1433        if local.is_invalid()? {
1434            return Ok(None);
1435        }
1436        // copy the value from the local
1437        let value = local.copy()?;
1438        let value = match arg {
1439            T::Argument__::Use(_) => {
1440                // dereference the reference
1441                value.read_ref()?
1442            }
1443            T::Argument__::Borrow(_, _) => {
1444                // value is not a reference, nothing to do
1445                value
1446            }
1447            T::Argument__::Freeze(_) => {
1448                invariant_violation!("freeze should not be used for a mutable reference")
1449            }
1450            T::Argument__::Read(_) => {
1451                invariant_violation!("read should not return a reference")
1452            }
1453        };
1454        let layout = self.env.runtime_layout(&ty)?;
1455        let Some(bytes) = value.typed_serialize(&layout) else {
1456            invariant_violation!("Failed to serialize Move value");
1457        };
1458        let arg = match location {
1459            T::Location::TxContext => return Ok(None),
1460            T::Location::GasCoin => TxArgument::GasCoin,
1461            T::Location::Result(i, j) => TxArgument::NestedResult(i, j),
1462            T::Location::ObjectInput(i) => TxArgument::Input(
1463                self.locations
1464                    .input_object_metadata
1465                    .safe_get(i as usize)?
1466                    .0
1467                    .0,
1468            ),
1469            T::Location::WithdrawalInput(i) => TxArgument::Input(
1470                self.locations
1471                    .input_withdrawal_metadata
1472                    .safe_get(i as usize)?
1473                    .original_input_index
1474                    .0,
1475            ),
1476            T::Location::PureInput(i) => TxArgument::Input(
1477                self.locations
1478                    .pure_input_metadata
1479                    .safe_get(i as usize)?
1480                    .original_input_index
1481                    .0,
1482            ),
1483            T::Location::ReceivingInput(i) => TxArgument::Input(
1484                self.locations
1485                    .receiving_input_metadata
1486                    .safe_get(i as usize)?
1487                    .original_input_index
1488                    .0,
1489            ),
1490        };
1491        Ok(Some((arg, bytes, tag)))
1492    }
1493
1494    pub fn tracked_results(
1495        &self,
1496        results: &[CtxValue],
1497        result_tys: &T::ResultType,
1498    ) -> Result<Vec<(Vec<u8>, TypeTag)>, Mode::Error> {
1499        assert_invariant!(
1500            results.len() == result_tys.len(),
1501            "results and result types should match"
1502        );
1503        results
1504            .iter()
1505            .zip_debug_eq(result_tys)
1506            .map(|(v, ty)| self.tracked_result(&v.0, ty.clone()))
1507            .collect()
1508    }
1509
1510    fn tracked_result(&self, result: &Value, ty: Type) -> Result<(Vec<u8>, TypeTag), Mode::Error> {
1511        let inner_value;
1512        let (v, ty) = match ty {
1513            Type::Reference(_, inner) => {
1514                inner_value = result.copy()?.read_ref()?;
1515                (&inner_value, (*inner).clone())
1516            }
1517            _ => (result, ty),
1518        };
1519        let layout = self.env.runtime_layout(&ty)?;
1520        let Some(bytes) = v.typed_serialize(&layout) else {
1521            invariant_violation!("Failed to serialize Move value");
1522        };
1523        let Ok(tag): Result<TypeTag, _> = ty.try_into() else {
1524            invariant_violation!("unable to generate type tag from type")
1525        };
1526        Ok((bytes, tag))
1527    }
1528}
1529
1530impl VMValueCast<CtxValue> for VMValue {
1531    fn cast(self) -> Result<CtxValue, PartialVMError> {
1532        Ok(CtxValue(self.into()))
1533    }
1534}
1535
1536impl CtxValue {
1537    pub fn vec_pack(ty: Type, values: Vec<CtxValue>) -> Result<CtxValue, ExecutionError> {
1538        Ok(CtxValue(Value::vec_pack(
1539            ty,
1540            values.into_iter().map(|v| v.0).collect(),
1541        )?))
1542    }
1543
1544    pub fn coin_ref_value(self) -> Result<u64, ExecutionError> {
1545        self.0.coin_ref_value()
1546    }
1547
1548    pub fn coin_ref_subtract_balance(self, amount: u64) -> Result<(), ExecutionError> {
1549        self.0.coin_ref_subtract_balance(amount)
1550    }
1551
1552    pub fn coin_ref_add_balance(self, amount: u64) -> Result<(), ExecutionError> {
1553        self.0.coin_ref_add_balance(amount)
1554    }
1555
1556    pub fn into_upgrade_ticket(self) -> Result<UpgradeTicket, ExecutionError> {
1557        self.0.into_upgrade_ticket()
1558    }
1559
1560    pub fn to_address(&self) -> Result<AccountAddress, ExecutionError> {
1561        self.0.copy()?.cast()
1562    }
1563
1564    /// Used to get access the inner Value for tracing.
1565    pub(super) fn inner_for_tracing(&self) -> &Value {
1566        &self.0
1567    }
1568}
1569
1570fn load_object_arg<Mode: ExecutionMode>(
1571    meter: &mut GasCharger,
1572    env: &Env<Mode>,
1573    input_object_map: &mut BTreeMap<ObjectID, object_runtime::InputObject>,
1574    input: T::ObjectInput,
1575) -> Result<(T::InputIndex, InputObjectMetadata, Value), Mode::Error> {
1576    let id = input.arg.id();
1577    let refined_permissions = input.arg.refined_permissions;
1578    let (metadata, value) = load_object_arg_impl(
1579        meter,
1580        env,
1581        input_object_map,
1582        id,
1583        refined_permissions,
1584        input.ty,
1585    )?;
1586    Ok((input.original_input_index, metadata, value))
1587}
1588
1589fn load_object_arg_impl<Mode: ExecutionMode>(
1590    meter: &mut GasCharger,
1591    env: &Env<Mode>,
1592    input_object_map: &mut BTreeMap<ObjectID, object_runtime::InputObject>,
1593    id: ObjectID,
1594    refined_permissions: ObjectPermissions,
1595    ty: T::Type,
1596) -> Result<(InputObjectMetadata, Value), Mode::Error> {
1597    let obj = env.read_object(&id)?;
1598    let owner = obj.owner.clone();
1599    let version = obj.version();
1600    let object_metadata = InputObjectMetadata {
1601        newly_created: false,
1602        id,
1603        refined_permissions,
1604        owner: owner.clone(),
1605        version,
1606        type_: ty.clone(),
1607    };
1608    let sui_types::object::ObjectInner {
1609        data: sui_types::object::Data::Move(move_obj),
1610        ..
1611    } = obj.as_inner()
1612    else {
1613        invariant_violation!("Expected a Move object");
1614    };
1615    assert_expected_move_object_type(&object_metadata.type_, move_obj.type_())?;
1616    let contained_uids = {
1617        let fully_annotated_layout = env.fully_annotated_layout(&ty)?;
1618        get_all_uids(&fully_annotated_layout, move_obj.contents()).map_err(|e| {
1619            make_invariant_violation!("Unable to retrieve UIDs for object. Got error: {e}")
1620        })?
1621    };
1622    input_object_map.insert(
1623        id,
1624        object_runtime::InputObject {
1625            contained_uids,
1626            version,
1627            owner,
1628        },
1629    );
1630
1631    let v = Value::deserialize(env, move_obj.contents(), ty)?;
1632    charge_gas_!(meter, env, charge_copy_loc, &v)?;
1633    charge_gas_!(meter, env, charge_store_loc, &v)?;
1634    Ok((object_metadata, v))
1635}
1636
1637fn load_withdrawal_arg<Mode: ExecutionMode>(
1638    meter: &mut GasCharger,
1639    env: &Env<Mode>,
1640    withdrawal: &T::WithdrawalInput,
1641) -> Result<Value, Mode::Error> {
1642    let T::WithdrawalInput {
1643        original_input_index: _,
1644        ty: _,
1645        owner,
1646        amount,
1647    } = withdrawal;
1648    let loaded = Value::funds_accumulator_withdrawal(*owner, *amount);
1649    charge_gas_!(meter, env, charge_copy_loc, &loaded)?;
1650    charge_gas_!(meter, env, charge_store_loc, &loaded)?;
1651    Ok(loaded)
1652}
1653
1654fn load_pure_value<Mode: ExecutionMode>(
1655    meter: &mut GasCharger,
1656    env: &Env<Mode>,
1657    bytes: &[u8],
1658    metadata: &T::PureInput,
1659) -> Result<Value, Mode::Error> {
1660    let loaded = Value::deserialize(env, bytes, metadata.ty.clone())?;
1661    // ByteValue::Receiving { id, version } => Value::receiving(*id, *version),
1662    charge_gas_!(meter, env, charge_copy_loc, &loaded)?;
1663    charge_gas_!(meter, env, charge_store_loc, &loaded)?;
1664    Ok(loaded)
1665}
1666
1667fn load_receiving_value<Mode: ExecutionMode>(
1668    meter: &mut GasCharger,
1669    env: &Env<Mode>,
1670    metadata: &T::ReceivingInput,
1671) -> Result<Value, Mode::Error> {
1672    let (id, version, _) = metadata.object_ref;
1673    let loaded = Value::receiving(id, version);
1674    charge_gas_!(meter, env, charge_copy_loc, &loaded)?;
1675    charge_gas_!(meter, env, charge_store_loc, &loaded)?;
1676    Ok(loaded)
1677}
1678
1679fn copy_value<Mode: ExecutionMode>(
1680    meter: &mut GasCharger,
1681    env: &Env<Mode>,
1682    value: &Value,
1683) -> Result<Value, Mode::Error> {
1684    charge_gas_!(meter, env, charge_copy_loc, value)?;
1685    charge_gas_!(meter, env, charge_pop, value)?;
1686    Ok(value.copy()?)
1687}
1688
1689/// The max budget was deducted from the gas coin at the beginning of the transaction,
1690/// now we return exactly that amount. Gas will be charged by the execution engine.
1691/// If the gas coin was transferred in any way, set the charge location.
1692fn refund_max_gas_budget<OType>(
1693    writes: &mut IndexMap<ObjectID, (Owner, OType, VMValue)>,
1694    accumulator_events: &mut Vec<MoveAccumulatorEvent>,
1695    gas_charger: &mut GasCharger,
1696    gas_id: ObjectID,
1697    gas_coin_transfer: Option<&GasCoinTransfer>,
1698) -> Result<(), ExecutionError> {
1699    match gas_coin_transfer {
1700        Some(GasCoinTransfer::SendFunds { recipient, .. }) => {
1701            // if the gas coin was transferred to an address balance, send the budget to that
1702            // address balance
1703            assert_invariant!(
1704                !writes.contains_key(&gas_id),
1705                "Gas coin should not be in writes if it was used with send_funds"
1706            );
1707            balance_change_accumulator_event(
1708                accumulator_events,
1709                *recipient,
1710                checked_as!(gas_charger.gas_budget(), i64)?,
1711            )?;
1712        }
1713        Some(GasCoinTransfer::TransferObjects) | None => {
1714            let Some((_, _, value_ref)) = writes.get_mut(&gas_id) else {
1715                invariant_violation!("Gas object cannot be wrapped or destroyed")
1716            };
1717            // replace with dummy value
1718            let value = std::mem::replace(value_ref, VMValue::u8(0));
1719            let mut locals = Locals::new([Some(value.into())])?;
1720            let mut local = locals.local(0)?;
1721            let coin_value = local.borrow()?.coin_ref_value()?;
1722            let additional = gas_charger.gas_budget();
1723            if coin_value.checked_add(additional).is_none() {
1724                return Err(ExecutionError::new_with_source(
1725                    ExecutionErrorKind::CoinBalanceOverflow,
1726                    "Gas coin too large after returning the max gas budget",
1727                ));
1728            };
1729            local.borrow()?.coin_ref_add_balance(additional)?;
1730            // put the value back
1731            *value_ref = local.move_()?.into();
1732        }
1733    };
1734    Ok(())
1735}
1736
1737/// Refunds the gas budget, overrides the charge location if transferred, and for ephemeral
1738/// coins settles the net balance change back to the source address balance.
1739/// Writes are always updated for refunding the gas budget.
1740/// The ephemeral coin is removed from writes and created objects in the case that it was not
1741/// transferred (left in its memory location at the end of the transaction).
1742fn finish_gas_coin<OType>(
1743    gas_charger: &mut GasCharger,
1744    writes: &mut IndexMap<ObjectID, (Owner, OType, VMValue)>,
1745    created_object_ids: &mut IndexSet<ObjectID>,
1746    deleted_object_ids: &IndexSet<ObjectID>,
1747    accumulator_events: &mut Vec<MoveAccumulatorEvent>,
1748    gas_id: ObjectID,
1749    gas_payment: GasPayment,
1750    gas_coin_transfer: Option<GasCoinTransfer>,
1751) -> Result<(), ExecutionError> {
1752    // return the max gas budget to the current gas location
1753    refund_max_gas_budget(
1754        writes,
1755        accumulator_events,
1756        gas_charger,
1757        gas_id,
1758        gas_coin_transfer.as_ref(),
1759    )?;
1760
1761    // Set the charge location if it was transferred
1762    // This might not actually "override" (that is the actual charge location might be the same
1763    // as it was at the beginning of the transaction), in the case where the coin was real and
1764    // was transferred, or in the case where the coin was ephemeral and transferred to the
1765    // an address balance recipient that matches the original address balance charge location
1766    match &gas_coin_transfer {
1767        Some(GasCoinTransfer::SendFunds { recipient, .. }) => {
1768            gas_charger.override_gas_charge_location(PaymentLocation::AddressBalance(
1769                (*recipient).into(),
1770            ))?;
1771        }
1772        Some(GasCoinTransfer::TransferObjects) => {
1773            gas_charger.override_gas_charge_location(PaymentLocation::Coin(gas_id))?;
1774        }
1775        None => (),
1776    }
1777
1778    // If the gas coin was not ephemeral, then we are done.
1779    let address = match gas_payment.location {
1780        PaymentLocation::Coin(_) => {
1781            // small sanity check
1782            assert_invariant!(
1783                !matches!(gas_coin_transfer, Some(GasCoinTransfer::SendFunds { .. }))
1784                    || deleted_object_ids.contains(&gas_id),
1785                "send_funds transfer implies the coin should be deleted"
1786            );
1787            return Ok(());
1788        }
1789        PaymentLocation::AddressBalance(address) => address,
1790    };
1791
1792    let net_balance_change = if let Some(gas_coin_transfer) = gas_coin_transfer {
1793        // sanity check storage changes
1794        match gas_coin_transfer {
1795            GasCoinTransfer::TransferObjects => {
1796                assert_invariant!(
1797                    created_object_ids.contains(&gas_id),
1798                    "ephemeral coin should be newly created"
1799                );
1800                assert_invariant!(
1801                    !deleted_object_ids.contains(&gas_id),
1802                    "ephemeral coin should not be deleted if transferred as an object"
1803                );
1804                assert_invariant!(
1805                    writes.contains_key(&gas_id),
1806                    "ephemeral coin should be in writes if transferred as an object"
1807                );
1808            }
1809            GasCoinTransfer::SendFunds { .. } => {
1810                assert_invariant!(
1811                    !created_object_ids.contains(&gas_id),
1812                    "ephemeral coin should not be newly created if transferred with send_funds"
1813                );
1814                assert_invariant!(
1815                    !deleted_object_ids.contains(&gas_id),
1816                    "ephemeral coin should not be deleted if transferred with send_funds"
1817                );
1818                assert_invariant!(
1819                    !writes.contains_key(&gas_id),
1820                    "ephemeral coin should not be in writes if transferred with send_funds"
1821                );
1822            }
1823        }
1824
1825        // If the gas coin was moved, it was transferred.
1826        // In such a case, the gas coin has a new location, so we fully withdraw the gas amount
1827        // and keep it in the coin object. The transferred location is now the source of payment
1828        // instead of the address balance.
1829        let Some(net_balance_change) = gas_payment
1830            .amount
1831            .try_into()
1832            .ok()
1833            .and_then(|i: i64| i.checked_neg())
1834        else {
1835            invariant_violation!("Gas payment amount cannot be represented as i64")
1836        };
1837        net_balance_change
1838    } else {
1839        // In this case the gas coin was not moved, so we want to return the remaining balance to
1840        // the address balance. To do so we need to destroy it and create an accumulator event for
1841        // the net balance change
1842        let was_created = created_object_ids.shift_remove(&gas_id);
1843        assert_invariant!(was_created, "ephemeral coin should be newly created");
1844        let Some((_owner, _ty, value)) = writes.shift_remove(&gas_id) else {
1845            invariant_violation!("checked above that the gas coin was present")
1846        };
1847        let (_id, remaining_balance) = Value::from(value).unpack_coin()?;
1848        // gas_payment.amount is the original value of the ephemeral coin.
1849        // If net_balance_change is negative, then balance was spent/withdrawn from the gas coin.
1850        // If the net_balance_change is positive, then balance was added/merged to the gas coin.
1851        let Some(net_balance_change): Option<i64> = (remaining_balance as i128)
1852            .checked_sub(gas_payment.amount as i128)
1853            .and_then(|i| i.try_into().ok())
1854        else {
1855            invariant_violation!("Remaining balance could not be represented as i64")
1856        };
1857        net_balance_change
1858    };
1859    balance_change_accumulator_event(accumulator_events, address.into(), net_balance_change)?;
1860    Ok(())
1861}
1862
1863fn balance_change_accumulator_event(
1864    accumulator_events: &mut Vec<MoveAccumulatorEvent>,
1865    address: AccountAddress,
1866    balance_change: i64,
1867) -> Result<(), ExecutionError> {
1868    if balance_change == 0 {
1869        return Ok(());
1870    }
1871    let balance_type = Balance::type_tag(sui_types::gas_coin::GAS::type_tag());
1872    let Some(accumulator_id) =
1873        accumulator_root::AccumulatorValue::get_field_id(address.into(), &balance_type).ok()
1874    else {
1875        invariant_violation!("Failed to compute accumulator field id")
1876    };
1877    let (action, value) = if balance_change < 0 {
1878        (
1879            MoveAccumulatorAction::Split,
1880            MoveAccumulatorValue::U64(balance_change.unsigned_abs()),
1881        )
1882    } else {
1883        (
1884            MoveAccumulatorAction::Merge,
1885            MoveAccumulatorValue::U64(balance_change as u64),
1886        )
1887    };
1888    accumulator_events.push(MoveAccumulatorEvent {
1889        accumulator_id: *accumulator_id.inner(),
1890        action,
1891        target_addr: address,
1892        target_ty: balance_type,
1893        value,
1894    });
1895    Ok(())
1896}
1897
1898/// Generate an MoveObject given an updated/written object
1899/// # Safety
1900///
1901/// This function assumes proper generation of has_public_transfer, either from the abilities of
1902/// the StructTag, or from the runtime correctly propagating from the inputs
1903unsafe fn create_written_object<Mode: ExecutionMode>(
1904    env: &Env<Mode>,
1905    objects_modified_at: &BTreeMap<ObjectID, LoadedRuntimeObject>,
1906    id: ObjectID,
1907    type_: Type,
1908    has_public_transfer: bool,
1909    contents: Vec<u8>,
1910) -> Result<MoveObject, ExecutionError> {
1911    debug_assert_eq!(
1912        id,
1913        MoveObject::id_opt(&contents).expect("object contents should start with an id")
1914    );
1915    let old_obj_ver = objects_modified_at
1916        .get(&id)
1917        .map(|obj: &LoadedRuntimeObject| obj.version);
1918
1919    let Ok(type_tag): Result<TypeTag, _> = type_.try_into() else {
1920        invariant_violation!("unable to generate type tag from type")
1921    };
1922
1923    let struct_tag = match type_tag {
1924        TypeTag::Struct(inner) => *inner,
1925        _ => invariant_violation!("Non struct type for object"),
1926    };
1927    unsafe {
1928        MoveObject::new_from_execution(
1929            struct_tag.into(),
1930            has_public_transfer,
1931            old_obj_ver.unwrap_or_default(),
1932            contents,
1933            env.protocol_config,
1934            Mode::packages_are_predefined(),
1935        )
1936    }
1937}
1938
1939/// substitutes the type arguments into the parameter and return types
1940pub fn subst_signature(
1941    signature: LoadedFunctionInformation,
1942    type_arguments: &[VMType],
1943) -> VMResult<LoadedFunctionInformation> {
1944    let LoadedFunctionInformation {
1945        parameters,
1946        return_,
1947        is_entry,
1948        is_native,
1949        visibility,
1950        index,
1951        instruction_count,
1952    } = signature;
1953    let parameters = parameters
1954        .into_iter()
1955        .map(|ty| ty.subst(type_arguments))
1956        .collect::<PartialVMResult<Vec<_>>>()
1957        .map_err(|err| err.finish(Location::Undefined))?;
1958    let return_ = return_
1959        .into_iter()
1960        .map(|ty| ty.subst(type_arguments))
1961        .collect::<PartialVMResult<Vec<_>>>()
1962        .map_err(|err| err.finish(Location::Undefined))?;
1963    Ok(LoadedFunctionInformation {
1964        parameters,
1965        return_,
1966        is_entry,
1967        is_native,
1968        visibility,
1969        index,
1970        instruction_count,
1971    })
1972}
1973
1974pub enum EitherError<E: ExecutionErrorTrait = ExecutionError> {
1975    CommandArgument(CommandArgumentError),
1976    Execution(E),
1977}
1978
1979impl<E: ExecutionErrorTrait> From<ExecutionError> for EitherError<E> {
1980    fn from(e: ExecutionError) -> Self {
1981        EitherError::Execution(e.into())
1982    }
1983}
1984
1985impl<E: ExecutionErrorTrait> From<CommandArgumentError> for EitherError<E> {
1986    fn from(e: CommandArgumentError) -> Self {
1987        EitherError::CommandArgument(e)
1988    }
1989}
1990
1991impl<E: ExecutionErrorTrait> EitherError<E> {
1992    pub fn into_execution_error(self, command_index: usize) -> E {
1993        match self {
1994            EitherError::CommandArgument(e) => command_argument_error(e, command_index).into(),
1995            EitherError::Execution(e) => e,
1996        }
1997    }
1998}
1999
2000/***************************************************************************************************
2001 * Special serialization formats
2002 **************************************************************************************************/
2003
2004/// Special enum for values that need additional validation, in other words
2005/// There is validation to do on top of the BCS layout. Currently only needed for
2006/// strings
2007#[derive(Debug)]
2008pub enum PrimitiveArgumentLayout {
2009    /// An option
2010    Option(Box<PrimitiveArgumentLayout>),
2011    /// A vector
2012    Vector(Box<PrimitiveArgumentLayout>),
2013    /// An ASCII encoded string
2014    Ascii,
2015    /// A UTF8 encoded string
2016    UTF8,
2017    // needed for Option validation
2018    Bool,
2019    U8,
2020    U16,
2021    U32,
2022    U64,
2023    U128,
2024    U256,
2025    Address,
2026}
2027
2028impl PrimitiveArgumentLayout {
2029    /// returns true iff all BCS compatible bytes are actually values for this type.
2030    /// For example, this function returns false for Option and Strings since they need additional
2031    /// validation.
2032    pub fn bcs_only(&self) -> bool {
2033        match self {
2034            // have additional restrictions past BCS
2035            PrimitiveArgumentLayout::Option(_)
2036            | PrimitiveArgumentLayout::Ascii
2037            | PrimitiveArgumentLayout::UTF8 => false,
2038            // Move primitives are BCS compatible and do not need additional validation
2039            PrimitiveArgumentLayout::Bool
2040            | PrimitiveArgumentLayout::U8
2041            | PrimitiveArgumentLayout::U16
2042            | PrimitiveArgumentLayout::U32
2043            | PrimitiveArgumentLayout::U64
2044            | PrimitiveArgumentLayout::U128
2045            | PrimitiveArgumentLayout::U256
2046            | PrimitiveArgumentLayout::Address => true,
2047            // vector only needs validation if it's inner type does
2048            PrimitiveArgumentLayout::Vector(inner) => inner.bcs_only(),
2049        }
2050    }
2051}
2052
2053/// Checks the bytes against the `SpecialArgumentLayout` using `bcs`. It does not actually generate
2054/// the deserialized value, only walks the bytes. While not necessary if the layout does not contain
2055/// special arguments (e.g. Option or String) we check the BCS bytes for predictability
2056pub fn bcs_argument_validate(
2057    bytes: &[u8],
2058    idx: u16,
2059    layout: PrimitiveArgumentLayout,
2060) -> Result<(), ExecutionError> {
2061    bcs::from_bytes_seed(&layout, bytes).map_err(|_| {
2062        ExecutionError::new_with_source(
2063            ExecutionErrorKind::command_argument_error(CommandArgumentError::InvalidBCSBytes, idx),
2064            format!("Function expects {layout} but provided argument's value does not match",),
2065        )
2066    })
2067}
2068
2069impl<'d> serde::de::DeserializeSeed<'d> for &PrimitiveArgumentLayout {
2070    type Value = ();
2071    fn deserialize<D: serde::de::Deserializer<'d>>(
2072        self,
2073        deserializer: D,
2074    ) -> Result<Self::Value, D::Error> {
2075        use serde::de::Error;
2076        match self {
2077            PrimitiveArgumentLayout::Ascii => {
2078                let s: &str = serde::Deserialize::deserialize(deserializer)?;
2079                if !s.is_ascii() {
2080                    Err(D::Error::custom("not an ascii string"))
2081                } else {
2082                    Ok(())
2083                }
2084            }
2085            PrimitiveArgumentLayout::UTF8 => {
2086                deserializer.deserialize_string(serde::de::IgnoredAny)?;
2087                Ok(())
2088            }
2089            PrimitiveArgumentLayout::Option(layout) => {
2090                deserializer.deserialize_option(OptionElementVisitor(layout))
2091            }
2092            PrimitiveArgumentLayout::Vector(layout) => {
2093                deserializer.deserialize_seq(VectorElementVisitor(layout))
2094            }
2095            // primitive move value cases, which are hit to make sure the correct number of bytes
2096            // are removed for elements of an option/vector
2097            PrimitiveArgumentLayout::Bool => {
2098                deserializer.deserialize_bool(serde::de::IgnoredAny)?;
2099                Ok(())
2100            }
2101            PrimitiveArgumentLayout::U8 => {
2102                deserializer.deserialize_u8(serde::de::IgnoredAny)?;
2103                Ok(())
2104            }
2105            PrimitiveArgumentLayout::U16 => {
2106                deserializer.deserialize_u16(serde::de::IgnoredAny)?;
2107                Ok(())
2108            }
2109            PrimitiveArgumentLayout::U32 => {
2110                deserializer.deserialize_u32(serde::de::IgnoredAny)?;
2111                Ok(())
2112            }
2113            PrimitiveArgumentLayout::U64 => {
2114                deserializer.deserialize_u64(serde::de::IgnoredAny)?;
2115                Ok(())
2116            }
2117            PrimitiveArgumentLayout::U128 => {
2118                deserializer.deserialize_u128(serde::de::IgnoredAny)?;
2119                Ok(())
2120            }
2121            PrimitiveArgumentLayout::U256 => {
2122                U256::deserialize(deserializer)?;
2123                Ok(())
2124            }
2125            PrimitiveArgumentLayout::Address => {
2126                SuiAddress::deserialize(deserializer)?;
2127                Ok(())
2128            }
2129        }
2130    }
2131}
2132
2133struct VectorElementVisitor<'a>(&'a PrimitiveArgumentLayout);
2134
2135impl<'d> serde::de::Visitor<'d> for VectorElementVisitor<'_> {
2136    type Value = ();
2137
2138    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
2139        formatter.write_str("Vector")
2140    }
2141
2142    fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
2143    where
2144        A: serde::de::SeqAccess<'d>,
2145    {
2146        while seq.next_element_seed(self.0)?.is_some() {}
2147        Ok(())
2148    }
2149}
2150
2151struct OptionElementVisitor<'a>(&'a PrimitiveArgumentLayout);
2152
2153impl<'d> serde::de::Visitor<'d> for OptionElementVisitor<'_> {
2154    type Value = ();
2155
2156    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
2157        formatter.write_str("Option")
2158    }
2159
2160    fn visit_none<E>(self) -> Result<Self::Value, E>
2161    where
2162        E: serde::de::Error,
2163    {
2164        Ok(())
2165    }
2166
2167    fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
2168    where
2169        D: serde::Deserializer<'d>,
2170    {
2171        self.0.deserialize(deserializer)
2172    }
2173}
2174
2175impl fmt::Display for PrimitiveArgumentLayout {
2176    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2177        match self {
2178            PrimitiveArgumentLayout::Vector(inner) => {
2179                write!(f, "vector<{inner}>")
2180            }
2181            PrimitiveArgumentLayout::Option(inner) => {
2182                write!(f, "std::option::Option<{inner}>")
2183            }
2184            PrimitiveArgumentLayout::Ascii => {
2185                write!(f, "std::{}::{}", RESOLVED_ASCII_STR.1, RESOLVED_ASCII_STR.2)
2186            }
2187            PrimitiveArgumentLayout::UTF8 => {
2188                write!(f, "std::{}::{}", RESOLVED_UTF8_STR.1, RESOLVED_UTF8_STR.2)
2189            }
2190            PrimitiveArgumentLayout::Bool => write!(f, "bool"),
2191            PrimitiveArgumentLayout::U8 => write!(f, "u8"),
2192            PrimitiveArgumentLayout::U16 => write!(f, "u16"),
2193            PrimitiveArgumentLayout::U32 => write!(f, "u32"),
2194            PrimitiveArgumentLayout::U64 => write!(f, "u64"),
2195            PrimitiveArgumentLayout::U128 => write!(f, "u128"),
2196            PrimitiveArgumentLayout::U256 => write!(f, "u256"),
2197            PrimitiveArgumentLayout::Address => write!(f, "address"),
2198        }
2199    }
2200}
2201
2202pub fn finish(
2203    protocol_config: &ProtocolConfig,
2204    state_view: &dyn ExecutionState,
2205    gas_charger: &mut GasCharger,
2206    tx_context: &TxContext,
2207    by_value_shared_objects: &BTreeSet<ObjectID>,
2208    consensus_owner_objects: &BTreeMap<ObjectID, Owner>,
2209    loaded_runtime_objects: BTreeMap<ObjectID, LoadedRuntimeObject>,
2210    written_objects: BTreeMap<ObjectID, Object>,
2211    created_object_ids: IndexSet<ObjectID>,
2212    deleted_object_ids: IndexSet<ObjectID>,
2213    user_events: Vec<(ModuleId, StructTag, Vec<u8>)>,
2214    accumulator_events: Vec<MoveAccumulatorEvent>,
2215    settlement_input_sui: u64,
2216    settlement_output_sui: u64,
2217) -> Result<ExecutionResults, ExecutionError> {
2218    // Before finishing, ensure that any shared object taken by value by the transaction is either:
2219    // 1. Mutated (and still has a shared ownership); or
2220    // 2. Deleted.
2221    // Otherwise, the shared object operation is not allowed and we fail the transaction.
2222    for id in by_value_shared_objects {
2223        // If it's been written it must have been reshared so must still have an ownership
2224        // of `Shared`.
2225        if let Some(obj) = written_objects.get(id) {
2226            if !obj.is_shared() {
2227                return Err(ExecutionError::new(
2228                    ExecutionErrorKind::SharedObjectOperationNotAllowed,
2229                    Some(
2230                        format!(
2231                            "Shared object operation on {} not allowed: \
2232                                 cannot be frozen, transferred, or wrapped",
2233                            id
2234                        )
2235                        .into(),
2236                    ),
2237                ));
2238            }
2239        } else {
2240            // If it's not in the written objects, the object must have been deleted. Otherwise
2241            // it's an error.
2242            if !deleted_object_ids.contains(id) {
2243                return Err(ExecutionError::new(
2244                    ExecutionErrorKind::SharedObjectOperationNotAllowed,
2245                    Some(
2246                        format!(
2247                            "Shared object operation on {} not allowed: \
2248                             shared objects used by value must be re-shared if not deleted",
2249                            id
2250                        )
2251                        .into(),
2252                    ),
2253                ));
2254            }
2255        }
2256    }
2257
2258    // Before finishing, enforce auth restrictions on consensus objects.
2259    for (id, original_owner) in consensus_owner_objects {
2260        let Owner::ConsensusAddressOwner { owner, .. } = original_owner else {
2261            panic!(
2262                "verified before adding to `consensus_owner_objects` that these are ConsensusAddressOwner"
2263            );
2264        };
2265        // Already verified in pre-execution checks that tx sender is the object owner.
2266        // Owner is allowed to do anything with the object.
2267        if tx_context.sender() != *owner {
2268            debug_fatal!(
2269                "transaction with a singly owned input object where the tx sender is not the owner should never be executed"
2270            );
2271            return Err(ExecutionError::new(
2272                ExecutionErrorKind::SharedObjectOperationNotAllowed,
2273                Some(
2274                    format!(
2275                        "Shared object operation on {} not allowed: \
2276                         transaction with singly owned input object must be sent by the owner",
2277                        id
2278                    )
2279                    .into(),
2280                ),
2281            ));
2282        }
2283        // If an Owner type is implemented with support for more fine-grained authorization,
2284        // checks should be performed here. For example, transfers and wraps can be detected
2285        // by comparing `original_owner` with:
2286        // let new_owner = written_objects.get(&id).map(|obj| obj.owner);
2287        //
2288        // Deletions can be detected with:
2289        // let deleted = deleted_object_ids.contains(&id);
2290    }
2291
2292    let user_events: Vec<Event> = user_events
2293        .into_iter()
2294        .map(|(module_id, tag, contents)| {
2295            Event::new(
2296                module_id.address(),
2297                module_id.name(),
2298                tx_context.sender(),
2299                tag,
2300                contents,
2301            )
2302        })
2303        .collect();
2304
2305    let mut receiving_funds_type_and_owners = BTreeMap::new();
2306    let accumulator_events = accumulator_events
2307        .into_iter()
2308        .map(|accum_event| {
2309            if let Some(ty) = Balance::maybe_get_balance_type_param(&accum_event.target_ty) {
2310                receiving_funds_type_and_owners
2311                    .entry(ty)
2312                    .or_insert_with(BTreeSet::new)
2313                    .insert(accum_event.target_addr.into());
2314            }
2315            let value = match accum_event.value {
2316                MoveAccumulatorValue::U64(amount) => AccumulatorValue::Integer(amount),
2317                MoveAccumulatorValue::EventRef(event_idx) => {
2318                    let Some(event) = user_events.get(checked_as!(event_idx, usize)?) else {
2319                        invariant_violation!(
2320                            "Could not find authenticated event at index {}",
2321                            event_idx
2322                        );
2323                    };
2324                    let digest = event.digest();
2325                    AccumulatorValue::EventDigest(nonempty![(event_idx, digest)])
2326                }
2327            };
2328
2329            let address =
2330                AccumulatorAddress::new(accum_event.target_addr.into(), accum_event.target_ty);
2331
2332            let write = AccumulatorWriteV1 {
2333                address,
2334                operation: accum_event.action.into_sui_accumulator_action(),
2335                value,
2336            };
2337
2338            Ok(AccumulatorEvent::new(
2339                AccumulatorObjId::new_unchecked(accum_event.accumulator_id),
2340                write,
2341            ))
2342        })
2343        .collect::<Result<Vec<_>, ExecutionError>>()?;
2344
2345    // Deny-list v2 checks
2346    for object in written_objects.values() {
2347        let coin_type = object.type_().and_then(|ty| ty.coin_type_maybe());
2348        let owner = object.owner.get_owner_address();
2349        if let (Some(ty), Ok(owner)) = (coin_type, owner) {
2350            receiving_funds_type_and_owners
2351                .entry(ty)
2352                .or_insert_with(BTreeSet::new)
2353                .insert(owner);
2354        }
2355    }
2356    let DenyListResult {
2357        result,
2358        num_non_gas_coin_owners,
2359    } = state_view.check_coin_deny_list(receiving_funds_type_and_owners);
2360    gas_charger.charge_coin_transfers(protocol_config, num_non_gas_coin_owners)?;
2361    result?;
2362
2363    let created_object_ids: BTreeSet<ObjectID> = created_object_ids.into_iter().collect();
2364    let deleted_object_ids: BTreeSet<ObjectID> = deleted_object_ids.into_iter().collect();
2365    let modified_objects: BTreeSet<ObjectID> = loaded_runtime_objects
2366        .into_iter()
2367        .filter_map(|(id, loaded)| loaded.is_modified.then_some(id))
2368        .collect();
2369
2370    assert_invariant!(
2371        created_object_ids.is_disjoint(&deleted_object_ids),
2372        "Created and deleted object sets should be disjoint"
2373    );
2374    assert_invariant!(
2375        modified_objects.is_disjoint(&created_object_ids),
2376        "Modified and created object sets should be disjoint"
2377    );
2378    assert_invariant!(
2379        written_objects
2380            .keys()
2381            .all(|id| !deleted_object_ids.contains(id)),
2382        "Written objects should not be deleted"
2383    );
2384    Ok(ExecutionResults::V2(ExecutionResultsV2 {
2385        written_objects,
2386        modified_objects,
2387        created_object_ids,
2388        deleted_object_ids,
2389        user_events,
2390        accumulator_events,
2391        settlement_input_sui,
2392        settlement_output_sui,
2393    }))
2394}
2395
2396pub fn fetch_package(
2397    state_view: &impl BackingPackageStore,
2398    package_id: &ObjectID,
2399) -> Result<PackageObject, ExecutionError> {
2400    let mut fetched_packages = fetch_packages(state_view, vec![package_id])?;
2401    assert_invariant!(
2402        fetched_packages.len() == 1,
2403        "Number of fetched packages must match the number of package object IDs if successful."
2404    );
2405    match fetched_packages.pop() {
2406        Some(pkg) => Ok(pkg),
2407        None => invariant_violation!(
2408            "We should always fetch a package for each object or return a dependency error."
2409        ),
2410    }
2411}
2412
2413pub fn fetch_packages<'ctx, 'state>(
2414    state_view: &'state impl BackingPackageStore,
2415    package_ids: impl IntoIterator<Item = &'ctx ObjectID>,
2416) -> Result<Vec<PackageObject>, ExecutionError> {
2417    let package_ids: BTreeSet<_> = package_ids.into_iter().collect();
2418    match get_package_objects(state_view, package_ids) {
2419        Err(e) => Err(ExecutionError::new_with_source(
2420            ExecutionErrorKind::PublishUpgradeMissingDependency,
2421            e,
2422        )),
2423        Ok(Err(missing_deps)) => {
2424            let msg = format!(
2425                "Missing dependencies: {}",
2426                missing_deps
2427                    .into_iter()
2428                    .map(|dep| format!("{}", dep))
2429                    .collect::<Vec<_>>()
2430                    .join(", ")
2431            );
2432            Err(ExecutionError::new_with_source(
2433                ExecutionErrorKind::PublishUpgradeMissingDependency,
2434                msg,
2435            ))
2436        }
2437        Ok(Ok(pkgs)) => Ok(pkgs),
2438    }
2439}
2440
2441pub fn check_compatibility(
2442    protocol_config: &ProtocolConfig,
2443    existing_package: &MovePackage,
2444    upgrading_modules: &[CompiledModule],
2445    policy: u8,
2446) -> Result<(), ExecutionError> {
2447    // Make sure this is a known upgrade policy.
2448    let Ok(policy) = UpgradePolicy::try_from(policy) else {
2449        return Err(ExecutionError::from_kind(
2450            ExecutionErrorKind::PackageUpgradeError {
2451                upgrade_error: PackageUpgradeError::UnknownUpgradePolicy { policy },
2452            },
2453        ));
2454    };
2455
2456    let pool = &mut normalized::RcPool::new();
2457    let binary_config = protocol_config.binary_config(None);
2458    let Ok(current_normalized) =
2459        existing_package.normalize(pool, &binary_config, /* include code */ true)
2460    else {
2461        invariant_violation!("Tried to normalize modules in existing package but failed")
2462    };
2463
2464    let existing_modules_len = current_normalized.len();
2465    let upgrading_modules_len = upgrading_modules.len();
2466    let disallow_new_modules = policy as u8 == UpgradePolicy::DEP_ONLY;
2467
2468    if disallow_new_modules && existing_modules_len != upgrading_modules_len {
2469        return Err(ExecutionError::new_with_source(
2470            ExecutionErrorKind::PackageUpgradeError {
2471                upgrade_error: PackageUpgradeError::IncompatibleUpgrade,
2472            },
2473            format!(
2474                "Existing package has {existing_modules_len} modules, but new package has \
2475                     {upgrading_modules_len}. Adding or removing a module to a deps only package is not allowed."
2476            ),
2477        ));
2478    }
2479
2480    let mut new_normalized = normalize_deserialized_modules(
2481        pool,
2482        upgrading_modules.iter(),
2483        /* include code */ true,
2484    );
2485    for (name, cur_module) in current_normalized {
2486        let Some(new_module) = new_normalized.remove(&name) else {
2487            return Err(ExecutionError::new_with_source(
2488                ExecutionErrorKind::PackageUpgradeError {
2489                    upgrade_error: PackageUpgradeError::IncompatibleUpgrade,
2490                },
2491                format!("Existing module {name} not found in next version of package"),
2492            ));
2493        };
2494
2495        check_module_compatibility(&policy, &cur_module, &new_module)?;
2496    }
2497
2498    // If we disallow new modules double check that there are no modules left in `new_normalized`.
2499    debug_assert!(!disallow_new_modules || new_normalized.is_empty());
2500
2501    Ok(())
2502}
2503
2504fn check_module_compatibility(
2505    policy: &UpgradePolicy,
2506    cur_module: &move_binary_format::compatibility::Module,
2507    new_module: &move_binary_format::compatibility::Module,
2508) -> Result<(), ExecutionError> {
2509    match policy {
2510        UpgradePolicy::Additive => InclusionCheck::Subset.check(cur_module, new_module),
2511        UpgradePolicy::DepOnly => InclusionCheck::Equal.check(cur_module, new_module),
2512        UpgradePolicy::Compatible => {
2513            let compatibility = Compatibility::upgrade_check();
2514
2515            compatibility.check(cur_module, new_module)
2516        }
2517    }
2518    .map_err(|e| {
2519        ExecutionError::new_with_source(
2520            ExecutionErrorKind::PackageUpgradeError {
2521                upgrade_error: PackageUpgradeError::IncompatibleUpgrade,
2522            },
2523            e,
2524        )
2525    })
2526}
2527
2528/// Assert the type inferred matches the object's type. This has already been done during loading,
2529/// but is checked again as an invariant. This may be removed safely at a later time if needed.
2530fn assert_expected_move_object_type(
2531    actual: &Type,
2532    expected: &MoveObjectType,
2533) -> Result<(), ExecutionError> {
2534    let Type::Datatype(actual) = actual else {
2535        invariant_violation!("Expected a datatype for a Move object");
2536    };
2537    let (a, m, n) = actual.qualified_ident();
2538    assert_invariant!(
2539        a == &expected.address(),
2540        "Actual address does not match expected. actual: {actual:?} vs expected: {expected:?}"
2541    );
2542    assert_invariant!(
2543        m == expected.module(),
2544        "Actual module does not match expected. actual: {actual:?} vs expected: {expected:?}"
2545    );
2546    assert_invariant!(
2547        n == expected.name(),
2548        "Actual struct does not match expected. actual: {actual:?} vs expected: {expected:?}"
2549    );
2550    let actual_type_arguments = &actual.type_arguments;
2551    let expected_type_arguments = expected.type_params();
2552    assert_invariant!(
2553        actual_type_arguments.len() == expected_type_arguments.len(),
2554        "Actual type arg length does not match expected. \
2555       actual: {actual:?} vs expected: {expected:?}",
2556    );
2557    for (actual_ty, expected_ty) in actual_type_arguments
2558        .iter()
2559        .zip_debug_eq(&expected_type_arguments)
2560    {
2561        assert_expected_type(actual_ty, expected_ty)?;
2562    }
2563    Ok(())
2564}
2565
2566/// Assert the type inferred matches the expected type. This has already been done during typing,
2567/// but is checked again as an invariant. This may be removed safely at a later time if needed.
2568fn assert_expected_type(actual: &Type, expected: &TypeTag) -> Result<(), ExecutionError> {
2569    match (actual, expected) {
2570        (Type::Bool, TypeTag::Bool)
2571        | (Type::U8, TypeTag::U8)
2572        | (Type::U16, TypeTag::U16)
2573        | (Type::U32, TypeTag::U32)
2574        | (Type::U64, TypeTag::U64)
2575        | (Type::U128, TypeTag::U128)
2576        | (Type::U256, TypeTag::U256)
2577        | (Type::Address, TypeTag::Address)
2578        | (Type::Signer, TypeTag::Signer) => Ok(()),
2579        (Type::Vector(inner_actual), TypeTag::Vector(inner_expected)) => {
2580            assert_expected_type(&inner_actual.element_type, inner_expected)
2581        }
2582        (Type::Datatype(actual_dt), TypeTag::Struct(expected_st)) => {
2583            assert_expected_data_type(actual_dt, expected_st)
2584        }
2585        _ => invariant_violation!(
2586            "Type mismatch between actual: {actual:?} and expected: {expected:?}"
2587        ),
2588    }
2589}
2590/// Assert the type inferred matches the expected type. This has already been done during typing,
2591/// but is checked again as an invariant. This may be removed safely at a later time if needed.
2592fn assert_expected_data_type(
2593    actual: &Datatype,
2594    expected: &StructTag,
2595) -> Result<(), ExecutionError> {
2596    let (a, m, n) = actual.qualified_ident();
2597    assert_invariant!(
2598        a == &expected.address,
2599        "Actual address does not match expected. actual: {actual:?} vs expected: {expected:?}"
2600    );
2601    assert_invariant!(
2602        m == expected.module.as_ident_str(),
2603        "Actual module does not match expected. actual: {actual:?} vs expected: {expected:?}"
2604    );
2605    assert_invariant!(
2606        n == expected.name.as_ident_str(),
2607        "Actual struct does not match expected. actual: {actual:?} vs expected: {expected:?}"
2608    );
2609    let actual_type_arguments = &actual.type_arguments;
2610    let expected_type_arguments = &expected.type_params;
2611    assert_invariant!(
2612        actual_type_arguments.len() == expected_type_arguments.len(),
2613        "Actual type arg length does not match expected. \
2614       actual: {actual:?} vs expected: {expected:?}",
2615    );
2616    for (actual_ty, expected_ty) in actual_type_arguments
2617        .iter()
2618        .zip_debug_eq(expected_type_arguments)
2619    {
2620        assert_expected_type(actual_ty, expected_ty)?;
2621    }
2622    Ok(())
2623}