Skip to content

Commit

Permalink
[API] trivial: rename: try_into_move_value -> try_into_vm_value
Browse files Browse the repository at this point in the history
And change the interface to use TypeTag instead of the json wrapper type
MoveType, which is generally the approach in the converter (with
exceptions)
  • Loading branch information
msmouse authored and aptos-bot committed Apr 22, 2022
1 parent 58500e2 commit ff49ae8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 29 deletions.
8 changes: 5 additions & 3 deletions api/src/tests/converter_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

use crate::{current_function_name, tests::new_test_context};
use aptos_api_types::{AsConverter, MoveConverter};
use aptos_api_types::{AsConverter, MoveConverter, MoveType};
use aptos_vm::data_cache::AsMoveResolver;
use move_core_types::{
account_address::AccountAddress,
Expand All @@ -11,6 +11,7 @@ use move_core_types::{
};
use serde::Serialize;
use serde_json::json;
use std::convert::TryInto;

#[tokio::test]
async fn test_parse_move_value() {
Expand Down Expand Up @@ -55,9 +56,10 @@ fn assert_parse_move_value<'r, R: MoveResolver, V: Serialize>(
json_value: V,
expected_move_value: VmMoveValue,
) {
let move_type = serde_json::from_value(json!(json_move_type)).unwrap();
let move_type: MoveType = serde_json::from_value(json!(json_move_type)).unwrap();
let type_tag = move_type.try_into().unwrap();
let move_value = converter
.try_into_move_value(&move_type, json!(json_value))
.try_into_vm_value(&type_tag, json!(json_value))
.unwrap();
assert_eq!(move_value, expected_move_value);
}
51 changes: 25 additions & 26 deletions api/types/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use crate::{
transaction::{ModuleBundlePayload, StateCheckpointTransaction},
Bytecode, DirectWriteSet, Event, HexEncodedBytes, MoveFunction, MoveModuleBytecode,
MoveResource, MoveScriptBytecode, MoveType, MoveValue, ScriptFunctionId, ScriptFunctionPayload,
MoveResource, MoveScriptBytecode, MoveValue, ScriptFunctionId, ScriptFunctionPayload,
ScriptPayload, ScriptWriteSet, Transaction, TransactionInfo, TransactionOnChainData,
TransactionPayload, UserTransactionRequest, WriteSet, WriteSetChange, WriteSetPayload,
};
Expand All @@ -23,7 +23,7 @@ use aptos_types::{
use move_binary_format::file_format::FunctionHandleIndex;
use move_core_types::{
identifier::Identifier,
language_storage::{ModuleId, StructTag},
language_storage::{ModuleId, StructTag, TypeTag},
resolver::MoveResolver,
value::{MoveStructLayout, MoveTypeLayout},
};
Expand Down Expand Up @@ -337,7 +337,7 @@ impl<'a, R: MoveResolver + ?Sized> MoveConverter<'a, R> {
type_arguments.len()
);
let args = self
.try_into_move_values(func, arguments)?
.try_into_vm_values(func, arguments)?
.iter()
.map(bcs::to_bytes)
.collect::<Result<_, bcs::Error>>()?;
Expand Down Expand Up @@ -371,7 +371,7 @@ impl<'a, R: MoveResolver + ?Sized> MoveConverter<'a, R> {
let MoveScriptBytecode { bytecode, abi } = code.try_parse_abi();
match abi {
Some(func) => {
let args = self.try_into_move_values(func, arguments)?;
let args = self.try_into_vm_values(func, arguments)?;
Target::Script(Script::new(
bytecode.into(),
type_arguments
Expand All @@ -395,7 +395,7 @@ impl<'a, R: MoveResolver + ?Sized> MoveConverter<'a, R> {
Ok(ret)
}

pub fn try_into_move_values(
pub fn try_into_vm_values(
&self,
func: MoveFunction,
args: Vec<serde_json::Value>,
Expand All @@ -422,14 +422,15 @@ impl<'a, R: MoveResolver + ?Sized> MoveConverter<'a, R> {
.zip(args.into_iter())
.enumerate()
.map(|(i, (arg_type, arg))| {
self.try_into_move_value(&arg_type, arg).map_err(|e| {
format_err!(
"parse arguments[{}] failed, expect {}, caused by error: {}",
i,
arg_type.json_type_name(),
e,
)
})
self.try_into_vm_value(&arg_type.clone().try_into()?, arg)
.map_err(|e| {
format_err!(
"parse arguments[{}] failed, expect {}, caused by error: {}",
i,
arg_type.json_type_name(),
e,
)
})
})
.collect::<Result<_>>()
}
Expand All @@ -438,18 +439,17 @@ impl<'a, R: MoveResolver + ?Sized> MoveConverter<'a, R> {
// representation in the DB.
// Notice that structs are of the `MoveStruct::Runtime` flavor, matching the representation in
// DB.
pub fn try_into_move_value(
pub fn try_into_vm_value(
&self,
typ: &MoveType,
type_tag: &TypeTag,
val: Value,
) -> Result<move_core_types::value::MoveValue> {
let type_tag = typ.clone().try_into()?;
let layout = self.inner.get_type_layout_with_fields(&type_tag)?;
let layout = self.inner.get_type_layout_with_fields(type_tag)?;

self.try_into_move_value_from_layout(&layout, val)
self.try_into_vm_value_from_layout(&layout, val)
}

fn try_into_move_value_from_layout(
fn try_into_vm_value_from_layout(
&self,
layout: &MoveTypeLayout,
val: Value,
Expand All @@ -463,18 +463,18 @@ impl<'a, R: MoveResolver + ?Sized> MoveConverter<'a, R> {
MoveTypeLayout::U128 => serde_json::from_value::<crate::U128>(val)?.into(),
MoveTypeLayout::Address => serde_json::from_value::<crate::Address>(val)?.into(),
MoveTypeLayout::Vector(item_layout) => {
self.try_into_move_value_vector(item_layout.as_ref(), val)?
self.try_into_vm_value_vector(item_layout.as_ref(), val)?
}
MoveTypeLayout::Struct(struct_layout) => {
self.try_into_move_value_struct(struct_layout, val)?
self.try_into_vm_value_struct(struct_layout, val)?
}
MoveTypeLayout::Signer => {
bail!("unexpected move type {:?} for value {:?}", layout, val)
}
})
}

pub fn try_into_move_value_vector(
pub fn try_into_vm_value_vector(
&self,
layout: &MoveTypeLayout,
val: Value,
Expand All @@ -484,7 +484,7 @@ impl<'a, R: MoveResolver + ?Sized> MoveConverter<'a, R> {
} else if let Value::Array(list) = val {
let vals = list
.into_iter()
.map(|v| self.try_into_move_value_from_layout(layout, v))
.map(|v| self.try_into_vm_value_from_layout(layout, v))
.collect::<Result<_>>()?;

Ok(move_core_types::value::MoveValue::Vector(vals))
Expand All @@ -493,7 +493,7 @@ impl<'a, R: MoveResolver + ?Sized> MoveConverter<'a, R> {
}
}

pub fn try_into_move_value_struct(
pub fn try_into_vm_value_struct(
&self,
layout: &MoveStructLayout,
val: Value,
Expand All @@ -520,8 +520,7 @@ impl<'a, R: MoveResolver + ?Sized> MoveConverter<'a, R> {
let value = field_values
.remove(name)
.ok_or_else(|| format_err!("field {} not found.", name))?;
let move_value =
self.try_into_move_value_from_layout(&field_layout.layout, value)?;
let move_value = self.try_into_vm_value_from_layout(&field_layout.layout, value)?;
Ok(move_value)
})
.collect::<Result<_>>()?;
Expand Down

0 comments on commit ff49ae8

Please sign in to comment.