Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[types] Refactor MoveState & MoveStructState API #1040

Merged
merged 1 commit into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions crates/rooch-rpc-api/src/jsonrpc_types/move_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ use move_core_types::{
u256,
};
use move_resource_viewer::{AnnotatedMoveStruct, AnnotatedMoveValue};
use moveos_types::move_std::ascii::MoveAsciiString;
use moveos_types::move_std::string::MoveString;
use moveos_types::move_types::parse_module_id;
use moveos_types::moveos_std::event::{AnnotatedEvent, Event, EventID};
use moveos_types::moveos_std::type_info::TypeInfo;
use moveos_types::state::MoveStructState;
use moveos_types::transaction::MoveAction;
use moveos_types::{
access_path::AccessPath,
Expand All @@ -24,6 +22,7 @@ use moveos_types::{
moveos_std::object::{AnnotatedObject, ObjectID},
transaction::{FunctionCall, ScriptCall},
};
use moveos_types::{move_std::ascii::MoveAsciiString, state::MoveStructType};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::Value;
Expand Down Expand Up @@ -97,15 +96,15 @@ pub enum SpecificStructView {

impl SpecificStructView {
pub fn try_from_annotated(move_struct: AnnotatedMoveStruct) -> Option<Self> {
if MoveString::type_match(&move_struct.type_) {
if MoveString::struct_tag_match(&move_struct.type_) {
MoveString::try_from(move_struct)
.ok()
.map(SpecificStructView::MoveString)
} else if MoveAsciiString::type_match(&move_struct.type_) {
} else if MoveAsciiString::struct_tag_match(&move_struct.type_) {
MoveAsciiString::try_from(move_struct)
.ok()
.map(SpecificStructView::MoveAsciiString)
} else if ObjectID::type_match(&move_struct.type_) {
} else if ObjectID::struct_tag_match(&move_struct.type_) {
ObjectID::try_from(move_struct)
.ok()
.map(SpecificStructView::ObjectID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use moveos_types::{
module_binding::{ModuleBinding, MoveFunctionCaller},
move_std::option::MoveOption,
moveos_std::tx_context::TxContext,
state::{MoveStructState, MoveStructType},
state::{MoveState, MoveStructState, MoveStructType},
transaction::FunctionCall,
};
use serde::{Deserialize, Serialize};
Expand Down
2 changes: 1 addition & 1 deletion crates/rooch-types/src/framework/account_coin_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use move_core_types::value::MoveValue;
use move_core_types::{account_address::AccountAddress, ident_str, identifier::IdentStr};
use moveos_types::move_std::string::MoveString;
use moveos_types::moveos_std::object::ObjectID;
use moveos_types::state::{MoveStructState, MoveStructType};
use moveos_types::state::{MoveState, MoveStructState, MoveStructType};
use moveos_types::{
module_binding::{ModuleBinding, MoveFunctionCaller},
move_std::option::MoveOption,
Expand Down
2 changes: 1 addition & 1 deletion crates/rooch-types/src/framework/address_mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use moveos_types::{
module_binding::{ModuleBinding, MoveFunctionCaller},
move_std::option::MoveOption,
moveos_std::tx_context::TxContext,
state::MoveStructState,
state::MoveState,
transaction::FunctionCall,
};

Expand Down
2 changes: 1 addition & 1 deletion crates/rooch-types/src/framework/coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use move_core_types::u256::U256;
use move_core_types::{account_address::AccountAddress, ident_str, identifier::IdentStr};
use moveos_types::move_std::string::MoveString;
use moveos_types::moveos_std::object::ObjectID;
use moveos_types::state::{MoveStructState, MoveStructType};
use moveos_types::state::{MoveState, MoveStructState, MoveStructType};
use moveos_types::{
module_binding::{ModuleBinding, MoveFunctionCaller},
moveos_std::tx_context::TxContext,
Expand Down
2 changes: 1 addition & 1 deletion crates/rooch-types/src/framework/coin_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use move_core_types::language_storage::StructTag;
use move_core_types::u256::U256;
use move_core_types::{account_address::AccountAddress, ident_str, identifier::IdentStr};
use moveos_types::move_std::string::MoveString;
use moveos_types::state::{MoveStructState, MoveStructType};
use moveos_types::state::{MoveState, MoveStructState, MoveStructType};
use serde::{Deserialize, Serialize};

pub const MODULE_NAME: &IdentStr = ident_str!("coin_store");
Expand Down
17 changes: 6 additions & 11 deletions crates/rooch-types/src/framework/session_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@ use crate::authentication_key::AuthenticationKey;
use anyhow::Result;
use move_core_types::value::MoveValue;
use move_core_types::{account_address::AccountAddress, ident_str, identifier::IdentStr};
use moveos_types::move_std::ascii::MoveAsciiString;
use moveos_types::{
module_binding::{ModuleBinding, MoveFunctionCaller},
move_std::ascii::MoveAsciiString,
move_std::option::MoveOption,
moveos_std::tx_context::TxContext,
state::MoveState,
transaction::{FunctionCall, MoveAction},
};
use moveos_types::{
serde::Readable,
state::{MoveStructState, MoveStructType},
state::{MoveState, MoveStructState, MoveStructType},
transaction::{FunctionCall, MoveAction},
};
use serde::{Deserialize, Serialize};
use serde_with::hex::Hex;
Expand Down Expand Up @@ -52,8 +49,8 @@ impl MoveStructState for SessionScope {
fn struct_layout() -> move_core_types::value::MoveStructLayout {
move_core_types::value::MoveStructLayout::new(vec![
move_core_types::value::MoveTypeLayout::Address,
<MoveAsciiString as MoveStructState>::type_layout(),
<MoveAsciiString as MoveStructState>::type_layout(),
MoveAsciiString::type_layout(),
MoveAsciiString::type_layout(),
])
}
}
Expand Down Expand Up @@ -112,9 +109,7 @@ impl MoveStructState for SessionKey {
move_core_types::value::MoveTypeLayout::Vector(Box::new(
move_core_types::value::MoveTypeLayout::U8,
)),
move_core_types::value::MoveTypeLayout::Vector(Box::new(
<SessionScope as MoveStructState>::type_layout(),
)),
move_core_types::value::MoveTypeLayout::Vector(Box::new(SessionScope::type_layout())),
move_core_types::value::MoveTypeLayout::U64,
move_core_types::value::MoveTypeLayout::U64,
move_core_types::value::MoveTypeLayout::U64,
Expand Down
1 change: 1 addition & 0 deletions moveos/moveos-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ proptest = { optional = true, workspace = true }
proptest-derive = { optional = true, workspace = true }

move-core-types = { workspace = true }
move-vm-types = { workspace = true }
move-resource-viewer = { workspace = true }
move-bytecode-utils = { workspace = true }
move-binary-format = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion moveos/moveos-types/src/moveos_std/object_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use crate::{
addresses::MOVEOS_STD_ADDRESS,
moveos_std::object::ObjectID,
state::{MoveStructState, MoveStructType},
state::{MoveState, MoveStructState, MoveStructType},
};
use move_core_types::{
account_address::AccountAddress, ident_str, identifier::IdentStr, language_storage::TypeTag,
Expand Down
2 changes: 1 addition & 1 deletion moveos/moveos-types/src/moveos_std/tx_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
addresses::MOVEOS_STD_ADDRESS,
move_std::ascii::MoveAsciiString,
move_std::option::MoveOption,
state::{MoveStructState, MoveStructType},
state::{MoveState, MoveStructState, MoveStructType},
transaction::MoveAction,
};
use move_core_types::{account_address::AccountAddress, ident_str, identifier::IdentStr};
Expand Down
94 changes: 70 additions & 24 deletions moveos/moveos-types/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use move_core_types::{
value::{MoveStructLayout, MoveTypeLayout, MoveValue},
};
use move_resource_viewer::{AnnotatedMoveValue, MoveValueAnnotator};
use move_vm_types::values::Value;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use smt::UpdateSet;
use std::collections::{btree_map, BTreeMap, BTreeSet};
Expand All @@ -33,12 +34,16 @@ pub struct State {
/// The rust representation of a Move value
pub trait MoveType {
fn type_tag() -> TypeTag;

fn type_tag_match(type_tag: &TypeTag) -> bool {
type_tag == &Self::type_tag()
}
}

/// The rust representation of a Move Struct
/// This trait copy from `move_core_types::move_resource::MoveStructType`
/// For auto implement `MoveType` to `MoveStructType`
pub trait MoveStructType {
pub trait MoveStructType: MoveType {
const ADDRESS: AccountAddress = move_core_types::language_storage::CORE_CODE_ADDRESS;
const MODULE_NAME: &'static IdentStr;
const STRUCT_NAME: &'static IdentStr;
Expand All @@ -64,14 +69,51 @@ pub trait MoveStructType {
}
}

fn type_tag() -> TypeTag {
TypeTag::Struct(Box::new(Self::struct_tag()))
fn struct_tag_match(type_tag: &StructTag) -> bool {
type_tag == &Self::struct_tag()
}
}

fn type_layout_match(first_layout: &MoveTypeLayout, second_layout: &MoveTypeLayout) -> bool {
match (first_layout, second_layout) {
(MoveTypeLayout::Address, MoveTypeLayout::Address) => true,
(MoveTypeLayout::Signer, MoveTypeLayout::Signer) => true,
(MoveTypeLayout::Bool, MoveTypeLayout::Bool) => true,
(MoveTypeLayout::U8, MoveTypeLayout::U8) => true,
(MoveTypeLayout::U16, MoveTypeLayout::U16) => true,
(MoveTypeLayout::U32, MoveTypeLayout::U32) => true,
(MoveTypeLayout::U64, MoveTypeLayout::U64) => true,
(MoveTypeLayout::U128, MoveTypeLayout::U128) => true,
(MoveTypeLayout::U256, MoveTypeLayout::U256) => true,
(
MoveTypeLayout::Vector(first_inner_layout),
MoveTypeLayout::Vector(second_inner_layout),
) => type_layout_match(first_inner_layout, second_inner_layout),
(
MoveTypeLayout::Struct(first_struct_layout),
MoveTypeLayout::Struct(second_struct_layout),
) => {
if first_struct_layout.fields().len() != second_struct_layout.fields().len() {
false
} else {
first_struct_layout
.fields()
.iter()
.zip(second_struct_layout.fields().iter())
.all(|(first_field, second_field)| type_layout_match(first_field, second_field))
}
}
(_, _) => false,
}
}

/// The rust representation of a Move value state
pub trait MoveState: MoveType + DeserializeOwned + Serialize {
fn type_layout() -> MoveTypeLayout;
fn type_layout_match(other_type_layout: &MoveTypeLayout) -> bool {
let self_layout = Self::type_layout();
type_layout_match(&self_layout, other_type_layout)
}
fn from_bytes(bytes: &[u8]) -> Result<Self>
where
Self: Sized,
Expand All @@ -91,6 +133,28 @@ pub trait MoveState: MoveType + DeserializeOwned + Serialize {
MoveValue::simple_deserialize(&blob, &Self::type_layout())
.expect("Deserialize the MoveValue from MoveState should success")
}

fn to_runtime_value(&self) -> Value {
let blob = self.to_bytes();
Value::simple_deserialize(&blob, &Self::type_layout())
.expect("Deserialize the MoveValue from MoveState should success")
}

/// Deserialize the MoveState from MoveRuntime Value
fn from_runtime_value(value: Value) -> Result<Self>
where
Self: Sized,
{
let blob = value
.simple_serialize(&Self::type_layout())
.ok_or_else(|| {
anyhow::anyhow!(
"Serialize the MoveState from Value error: {:?}",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments and error msg are inconsistent?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The AI generated. I will fix it in a new PR.

Self::type_tag()
)
})?;
Self::from_bytes(&blob)
}
}

impl MoveType for u8 {
Expand Down Expand Up @@ -226,28 +290,8 @@ where

/// Move State is a trait that is used to represent the state of a Move Resource in Rust
/// It is like the `MoveResource` in move_core_types
pub trait MoveStructState: MoveStructType + DeserializeOwned + Serialize {
fn type_layout() -> MoveTypeLayout {
MoveTypeLayout::Struct(Self::struct_layout())
}
pub trait MoveStructState: MoveState + MoveStructType + DeserializeOwned + Serialize {
fn struct_layout() -> MoveStructLayout;
fn type_match(type_tag: &StructTag) -> bool {
type_tag == &Self::struct_tag()
}
fn from_bytes(bytes: &[u8]) -> Result<Self>
where
Self: Sized,
{
bcs::from_bytes(bytes)
.map_err(|e| anyhow::anyhow!("Deserialize the MoveState error: {:?}", e))
}
fn to_bytes(&self) -> Vec<u8> {
bcs::to_bytes(self).expect("Serialize the MoveState should success")
}
fn into_state(self) -> State {
let value = self.to_bytes();
State::new(value, TypeTag::Struct(Box::new(Self::struct_tag())))
}
}

impl<T> From<T> for State
Expand All @@ -259,6 +303,8 @@ where
}
}

pub trait MoveRuntimeValue {}

impl State {
pub fn new(value: Vec<u8>, value_type: TypeTag) -> Self {
Self { value, value_type }
Expand Down
Loading