Skip to content

Commit

Permalink
[API] MoveConverter::try_into_move_value()
Browse files Browse the repository at this point in the history
gets layout from the resolver and annotates vm value

Closes: aptos-labs#568
  • Loading branch information
msmouse authored and aptos-bot committed Apr 22, 2022
1 parent ff49ae8 commit 6343561
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
31 changes: 18 additions & 13 deletions api/src/tests/converter_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,32 @@ use serde_json::json;
use std::convert::TryInto;

#[tokio::test]
async fn test_parse_move_value() {
async fn test_value_conversion() {
let context = new_test_context(current_function_name!());
let address = AccountAddress::from_hex_literal("0x1").unwrap();

let state_view = context.latest_state_view();
let resolver = state_view.as_move_resolver();
let converter = resolver.as_converter();

assert_parse_move_value(&converter, "u8", 1i32, VmMoveValue::U8(1));
assert_parse_move_value(&converter, "u64", "1", VmMoveValue::U64(1));
assert_parse_move_value(&converter, "u128", "1", VmMoveValue::U128(1));
assert_parse_move_value(&converter, "bool", true, VmMoveValue::Bool(true));
assert_parse_move_value(&converter, "address", "0x1", VmMoveValue::Address(address));
assert_parse_move_value(
assert_value_conversion(&converter, "u8", 1i32, VmMoveValue::U8(1));
assert_value_conversion(&converter, "u64", "1", VmMoveValue::U64(1));
assert_value_conversion(&converter, "u128", "1", VmMoveValue::U128(1));
assert_value_conversion(&converter, "bool", true, VmMoveValue::Bool(true));
assert_value_conversion(&converter, "address", "0x1", VmMoveValue::Address(address));
assert_value_conversion(
&converter,
"vector<u8>",
"0x0102",
VmMoveValue::Vector(vec![VmMoveValue::U8(1), VmMoveValue::U8(2)]),
);
assert_parse_move_value(
assert_value_conversion(
&converter,
"vector<u64>",
["1", "2"],
VmMoveValue::Vector(vec![VmMoveValue::U64(1), VmMoveValue::U64(2)]),
);
assert_parse_move_value(
assert_value_conversion(
&converter,
"0x1::GUID::ID",
json!({"addr": "0x1", "creation_num": "1"}),
Expand All @@ -50,16 +50,21 @@ async fn test_parse_move_value() {
);
}

fn assert_parse_move_value<'r, R: MoveResolver, V: Serialize>(
fn assert_value_conversion<'r, R: MoveResolver, V: Serialize>(
converter: &MoveConverter<'r, R>,
json_move_type: &str,
json_value: V,
expected_move_value: VmMoveValue,
expected_vm_value: VmMoveValue,
) {
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
let vm_value = converter
.try_into_vm_value(&type_tag, json!(json_value))
.unwrap();
assert_eq!(move_value, expected_move_value);
assert_eq!(vm_value, expected_vm_value);

let vm_bytes = vm_value.undecorate().simple_serialize().unwrap();
let move_value_back = converter.try_into_move_value(&type_tag, &vm_bytes).unwrap();
let json_value_back = serde_json::to_value(move_value_back).unwrap();
assert_eq!(json_value_back, json!(json_value));
}
4 changes: 4 additions & 0 deletions api/types/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,10 @@ impl<'a, R: MoveResolver + ?Sized> MoveConverter<'a, R> {
}
}

pub fn try_into_move_value(&self, typ: &TypeTag, bytes: &[u8]) -> Result<MoveValue> {
self.inner.view_value(typ, bytes)?.try_into()
}

fn explain_function_index(&self, module_id: &ModuleId, function: &u16) -> Result<String> {
let code = self.inner.get_module(&module_id.clone())? as Rc<dyn Bytecode>;
let func = code.function_handle_at(FunctionHandleIndex::new(*function));
Expand Down

0 comments on commit 6343561

Please sign in to comment.