sui_move_natives_v2/
test_utils.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::{legacy_test_cost, types::is_otw_struct};
5use move_binary_format::errors::PartialVMResult;
6use move_core_types::{gas_algebra::InternalGas, runtime_value::MoveTypeLayout};
7use move_vm_runtime::native_functions::NativeContext;
8use move_vm_types::{
9    loaded_data::runtime_types::Type, natives::function::NativeResult, values::Value,
10};
11use smallvec::smallvec;
12use std::collections::VecDeque;
13
14pub fn create_one_time_witness(
15    context: &mut NativeContext,
16    mut ty_args: Vec<Type>,
17    args: VecDeque<Value>,
18) -> PartialVMResult<NativeResult> {
19    debug_assert!(ty_args.len() == 1);
20    debug_assert!(args.is_empty());
21
22    let ty = ty_args.pop().unwrap();
23    let type_tag = context.type_to_type_tag(&ty)?;
24    let type_layout = context.type_to_type_layout(&ty)?;
25
26    let Some(MoveTypeLayout::Struct(struct_layout)) = type_layout else {
27        return Ok(NativeResult::err(InternalGas::new(1), 0));
28    };
29
30    let hardened_check = context.runtime_limits_config().hardened_otw_check;
31    if is_otw_struct(&struct_layout, &type_tag, hardened_check) {
32        Ok(NativeResult::ok(
33            legacy_test_cost(),
34            smallvec![Value::struct_(move_vm_types::values::Struct::pack(vec![
35                Value::bool(true)
36            ]))],
37        ))
38    } else {
39        Ok(NativeResult::err(InternalGas::new(1), 1))
40    }
41}