iota_move_natives_latest/
test_utils.rs

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