-
Notifications
You must be signed in to change notification settings - Fork 290
/
Copy pathfrom_bcs.rs
77 lines (67 loc) · 2.75 KB
/
from_bcs.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright (c) The Starcoin Core Contributors
// SPDX-License-Identifier: Apache-2.0
use crate::util::make_native_from_func;
use move_binary_format::errors::{PartialVMError, PartialVMResult};
use move_core_types::gas_algebra::{InternalGas, InternalGasPerByte, NumBytes};
use move_core_types::vm_status::StatusCode;
use move_vm_runtime::native_functions::{NativeContext, NativeFunction};
use move_vm_types::loaded_data::runtime_types::Type;
use move_vm_types::natives::function::NativeResult;
use move_vm_types::pop_arg;
use move_vm_types::values::Value;
use smallvec::smallvec;
use std::collections::VecDeque;
// !!!! NOTE !!!!
// This file is intended for natives from the util module in the framework.
// DO NOT PUT HELPER FUNCTIONS HERE!
/// Abort code when from_bytes fails (0x01 == INVALID_ARGUMENT)
const EFROM_BYTES: u64 = 0x01_0001;
/***************************************************************************************************
* native fun from_bytes
*
* gas cost: base_cost + unit_cost * bytes_len
*
**************************************************************************************************/
fn native_from_bytes(
gas_params: &GasParameters,
context: &mut NativeContext,
ty_args: Vec<Type>,
mut args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
debug_assert_eq!(ty_args.len(), 1);
debug_assert_eq!(args.len(), 1);
let mut cost = gas_params.base;
// TODO(Gas): charge for getting the layout
let layout = context.type_to_type_layout(&ty_args[0])?.ok_or_else(|| {
PartialVMError::new(StatusCode::UNKNOWN_INVARIANT_VIOLATION_ERROR).with_message(format!(
"Failed to get layout of type {:?} -- this should not happen",
ty_args[0]
))
})?;
let bytes = pop_arg!(args, Vec<u8>);
//context.charge(gas_params.base + gas_params.per_byte * NumBytes::new(bytes.len() as u64))?;
cost += gas_params.per_byte * NumBytes::new(bytes.len() as u64);
let val = match Value::simple_deserialize(&bytes, &layout) {
Some(val) => val,
None => {
return Ok(NativeResult::err(cost, EFROM_BYTES));
}
};
Ok(NativeResult::ok(cost, smallvec![val]))
}
/***************************************************************************************************
* module
*
**************************************************************************************************/
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GasParameters {
pub base: InternalGas,
pub per_byte: InternalGasPerByte,
}
pub fn make_all(gas_params: GasParameters) -> impl Iterator<Item = (String, NativeFunction)> {
let natives = [(
"from_bytes",
make_native_from_func(gas_params, native_from_bytes),
)];
crate::helpers::make_module_natives(natives)
}