From b05939507c9a5c866c445df922548b0681af116b Mon Sep 17 00:00:00 2001 From: simonjiao Date: Sat, 21 Sep 2024 13:17:49 +0800 Subject: [PATCH 1/3] fix more compiling errors in starcoin-vm-types --- vm/types/src/access_path.rs | 4 +++ .../account_config/events/config_change.rs | 2 +- vm/types/src/on_chain_config/mod.rs | 7 ++-- vm/types/src/on_chain_resource/dao/mod.rs | 16 +++++++-- vm/types/src/state_store/state_key/mod.rs | 36 ++++++------------- .../src/state_store/state_key/registry.rs | 8 ++--- vm/types/src/transaction/mod.rs | 5 +-- 7 files changed, 39 insertions(+), 39 deletions(-) diff --git a/vm/types/src/access_path.rs b/vm/types/src/access_path.rs index 55aef42842..863966730e 100644 --- a/vm/types/src/access_path.rs +++ b/vm/types/src/access_path.rs @@ -117,6 +117,10 @@ impl AccessPath { _ => None, } } + + pub fn size(&self) -> usize { + std::mem::size_of_val(&self.address) + std::mem::size_of_val(&self.path) + } } impl Serialize for AccessPath { diff --git a/vm/types/src/account_config/events/config_change.rs b/vm/types/src/account_config/events/config_change.rs index 8049f4328d..a39e392652 100644 --- a/vm/types/src/account_config/events/config_change.rs +++ b/vm/types/src/account_config/events/config_change.rs @@ -7,7 +7,7 @@ use move_core_types::account_address::AccountAddress; use move_core_types::ident_str; use move_core_types::identifier::IdentStr; use move_core_types::move_resource::MoveStructType; -use serde::{Deserializer, Serialize}; +use serde::Serialize; //TODO support deserialize #[derive(Debug, Serialize)] diff --git a/vm/types/src/on_chain_config/mod.rs b/vm/types/src/on_chain_config/mod.rs index 8b358f3a82..2aa7a979c5 100644 --- a/vm/types/src/on_chain_config/mod.rs +++ b/vm/types/src/on_chain_config/mod.rs @@ -213,12 +213,9 @@ pub fn new_epoch_event_key() -> EventKey { EventKey::new_from_address(&genesis_address(), 0) } -pub fn access_path_for_config(config_id: ConfigID) -> Result { +pub fn access_path_for_config(config_id: ConfigID) -> AccessPath { let struct_tag = struct_tag_for_config(config_id); - Ok(AccessPath::new( - CORE_CODE_ADDRESS, - AccessPath::resource_path_vec(struct_tag)?, - )) + AccessPath::resource_access_path(CORE_CODE_ADDRESS, struct_tag) } pub fn struct_tag_for_config(config_id: ConfigID) -> StructTag { diff --git a/vm/types/src/on_chain_resource/dao/mod.rs b/vm/types/src/on_chain_resource/dao/mod.rs index 5f82741cc4..a3ae2d63d4 100644 --- a/vm/types/src/on_chain_resource/dao/mod.rs +++ b/vm/types/src/on_chain_resource/dao/mod.rs @@ -74,10 +74,18 @@ pub struct Proposal { /// how many votes to reach to make the proposal pass. pub quorum_votes: u128, /// proposal action. - #[serde(deserialize_with = "A::deserialize")] + #[serde(deserialize_with = "deserialize_proposal_action")] pub action: Option, } +fn deserialize_proposal_action<'de, D, A>(deserializer: D) -> Result, D::Error> +where + D: serde::Deserializer<'de>, + A: ProposalAction, +{ + Ok(Some(A::deserialize(deserializer)?)) +} + impl MoveStructType for Proposal where A: ProposalAction, @@ -97,7 +105,11 @@ where address: CORE_CODE_ADDRESS, module: Self::module_identifier(), name: Self::struct_identifier(), - type_args: vec![TypeTag::Struct(Box::new(token_type_tag)), A::type_tag()], + type_args: { + let mut args = A::type_args(); + args.push(TypeTag::Struct(Box::new(token_type_tag))); + args + }, } } diff --git a/vm/types/src/state_store/state_key/mod.rs b/vm/types/src/state_store/state_key/mod.rs index 52c85a04ae..9594e936e5 100644 --- a/vm/types/src/state_store/state_key/mod.rs +++ b/vm/types/src/state_store/state_key/mod.rs @@ -72,9 +72,8 @@ impl StateKey { let myself = match state_key_tag { StateKeyTag::AccessPath => { let AccessPath { address, path } = bcs::from_bytes(&val[1..])?; - //let path: DataPath = bcs::from_bytes(&path)?; match path { - DataPath::Code(ModuleId { address, name }) => Self::module(&address, &name), + DataPath::Code(module_name) => Self::module(&address, &module_name), DataPath::Resource(struct_tag) => Self::resource(&address, &struct_tag)?, DataPath::ResourceGroup(struct_tag) => { Self::resource_group(&address, &struct_tag) @@ -114,25 +113,11 @@ impl StateKey { use access_path::DataPath; let myself = match deserialized { - StateKeyInner::AccessPath(AccessPath { address, path }) => { - match path { - //bcs::from_bytes::(&path) { - Err(err) => { - if cfg!(feature = "fuzzing") { - // note: to make analyze-serde-formats test happy, do not error out - // alternative is to wrap `AccessPath::path: Vec` in an enum - Self::raw(&bcs::to_bytes(&(address, path)).unwrap()) - } else { - return Err(err.into()); - } - } - Ok(DataPath::Code(module_id)) => Self::module_id(&module_id), - Ok(DataPath::Resource(struct_tag)) => Self::resource(&address, &struct_tag)?, - Ok(DataPath::ResourceGroup(struct_tag)) => { - Self::resource_group(&address, &struct_tag) - } - } - } + StateKeyInner::AccessPath(AccessPath { address, path }) => match path { + DataPath::Code(module_name) => Self::module(&address, &module_name), + DataPath::Resource(struct_tag) => Self::resource(&address, &struct_tag)?, + DataPath::ResourceGroup(struct_tag) => Self::resource_group(&address, &struct_tag), + }, StateKeyInner::TableItem { handle, key } => Self::table_item(&handle, &key), StateKeyInner::Raw(bytes) => Self::raw(&bytes), }; @@ -148,7 +133,7 @@ impl StateKey { Ok(StateKeyInner::AccessPath(AccessPath::resource_access_path( *address, struct_tag.clone(), - )?)) + ))) }, )?)) } @@ -166,9 +151,10 @@ impl StateKey { REGISTRY .resource_group(struct_tag, address) .get_or_add(struct_tag, address, || { - Ok(StateKeyInner::AccessPath( - AccessPath::resource_group_access_path(*address, struct_tag.clone()), - )) + Ok(StateKeyInner::AccessPath(AccessPath::resource_access_path( + *address, + struct_tag.clone(), + ))) }) .expect("only possible error is resource path serialization"), ) diff --git a/vm/types/src/state_store/state_key/registry.rs b/vm/types/src/state_store/state_key/registry.rs index 35a83319ed..ff8675366d 100644 --- a/vm/types/src/state_store/state_key/registry.rs +++ b/vm/types/src/state_store/state_key/registry.rs @@ -50,10 +50,10 @@ impl Drop for Entry { StateKeyInner::AccessPath(AccessPath { address, path }) => { use crate::access_path::DataPath; - match &bcs::from_bytes::(path).expect("Failed to deserialize Path.") { - DataPath::Code(module_id) => REGISTRY - .module(address, &module_id.name) - .maybe_remove(&module_id.address, &module_id.name), + match path { + DataPath::Code(module_name) => REGISTRY + .module(address, module_name) + .maybe_remove(&address, module_name), DataPath::Resource(struct_tag) => REGISTRY .resource(struct_tag, address) .maybe_remove(struct_tag, address), diff --git a/vm/types/src/transaction/mod.rs b/vm/types/src/transaction/mod.rs index 8f566ad712..6662a0babc 100644 --- a/vm/types/src/transaction/mod.rs +++ b/vm/types/src/transaction/mod.rs @@ -64,6 +64,7 @@ mod transaction_argument; mod change_set; pub mod user_transaction_context; +use crate::state_store::state_key::inner::StateKeyInner; pub use script::{ ArgumentABI, EntryABI, EntryFunction, EntryFunctionABI, TransactionScriptABI, TypeArgumentABI, }; @@ -739,8 +740,8 @@ impl TransactionOutput { pub fn table_items(&self) -> Vec<(StateKey, WriteOp)> { let mut table_items = vec![]; for (state_key, op) in &self.write_set { - if let StateKey::TableItem(table_item) = state_key { - table_items.push((StateKey::TableItem(table_item.clone()), op.clone())); + if let StateKeyInner::TableItem { handle: _, key: _ } = state_key.inner() { + table_items.push((state_key.clone(), op.clone())); } } table_items From 2de429acca283f1ae4ef73a553b9deb05b75eda1 Mon Sep 17 00:00:00 2001 From: simonjiao Date: Sat, 21 Sep 2024 17:14:37 +0800 Subject: [PATCH 2/3] fix compiling errors in starcoin-types --- types/src/account.rs | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/types/src/account.rs b/types/src/account.rs index cbed699207..cb2160a4ae 100644 --- a/types/src/account.rs +++ b/types/src/account.rs @@ -13,6 +13,7 @@ use crate::{ }, write_set::{WriteOp, WriteSet, WriteSetMut}, }; +use move_core_types::move_resource::MoveStructType; use starcoin_crypto::ed25519::*; use starcoin_crypto::keygen::KeyGen; use starcoin_crypto::multi_ed25519::genesis_multi_key_pair; @@ -27,7 +28,6 @@ use starcoin_vm_types::value::{MoveStructLayout, MoveTypeLayout}; use starcoin_vm_types::{ account_config::{self, AccountResource, BalanceResource}, language_storage::StructTag, - move_resource::MoveResource, transaction::authenticator::{AccountPrivateKey, AccountPublicKey}, values::{Struct, Value}, }; @@ -538,8 +538,13 @@ impl AccountData { /// Returns the AccessPath that describes the Account balance resource instance. /// /// Use this to retrieve or publish the Account blob. - pub fn make_balance_access_path(&self, token_code: &str) -> AccessPath { - self.account.make_balance_access_path(token_code) + fn balance_resource_tag(token_code: &str) -> StructTag { + let token_code = + TokenCode::from_str(token_code).expect("token code str should been valid."); + let token_type_tag = token_code + .try_into() + .expect("token code to type tag should be ok"); + BalanceResource::struct_tag_for_token(token_type_tag) } /// Returns the AccessPath that describes the EventHandleGenerator resource instance. @@ -561,7 +566,8 @@ impl AccountData { .simple_serialize(&Self::layout()) .unwrap(); write_set.push(( - StateKey::AccessPath(self.make_account_access_path()), + // Relax, this unwrap is safe and only for tests + StateKey::resource(self.address(), &AccountResource::struct_tag()).unwrap(), WriteOp::legacy_creation(account.into()), )); for (code, balance_blob) in balance_blobs.into_iter() { @@ -571,7 +577,9 @@ impl AccountData { .simple_serialize(&Balance::layout()) .unwrap(); write_set.push(( - StateKey::AccessPath(self.make_balance_access_path(code.as_str())), + // Relax, this unwrap is safe and only for tests + StateKey::resource(self.address(), &Self::balance_resource_tag(code.as_str())) + .unwrap(), WriteOp::legacy_creation(balance.into()), )); } @@ -582,7 +590,12 @@ impl AccountData { .simple_serialize(&EventHandleGenerator::layout()) .unwrap(); write_set.push(( - StateKey::AccessPath(self.make_event_generator_access_path()), + // Relax, this unwrap is safe and only for tests + StateKey::resource( + self.address(), + &account_config::event_handle_generator_struct_tag(), + ) + .unwrap(), WriteOp::legacy_creation(event_generator.into()), )); WriteSetMut::new(write_set).freeze().unwrap() From 5a314e2889d35559e85bf550fd4f5696f3d3e5d0 Mon Sep 17 00:00:00 2001 From: simonjiao Date: Sun, 22 Sep 2024 15:31:16 +0800 Subject: [PATCH 3/3] fix StateView publicity --- vm/starcoin-aggregator/src/aggregator_v1_extension.rs | 2 +- vm/types/src/on_chain_config/mod.rs | 2 +- vm/types/src/state_store/mod.rs | 5 +---- vm/types/src/state_view.rs | 6 +++++- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/vm/starcoin-aggregator/src/aggregator_v1_extension.rs b/vm/starcoin-aggregator/src/aggregator_v1_extension.rs index 2b434f5d75..e054f09323 100644 --- a/vm/starcoin-aggregator/src/aggregator_v1_extension.rs +++ b/vm/starcoin-aggregator/src/aggregator_v1_extension.rs @@ -24,7 +24,7 @@ pub struct AggregatorID(pub StateKey); impl AggregatorID { pub fn new(handle: TableHandle, key: AccountAddress) -> Self { - let state_key = StateKey::table_item(handle, key.to_vec()); + let state_key = StateKey::table_item(&handle, key.as_slice()); AggregatorID(state_key) } diff --git a/vm/types/src/on_chain_config/mod.rs b/vm/types/src/on_chain_config/mod.rs index 2aa7a979c5..7cc0af2317 100644 --- a/vm/types/src/on_chain_config/mod.rs +++ b/vm/types/src/on_chain_config/mod.rs @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 use crate::account_config::genesis_address; -use crate::state_store::StateView; +use crate::state_view::StateView; use crate::{ access_path::AccessPath, account_address::AccountAddress, diff --git a/vm/types/src/state_store/mod.rs b/vm/types/src/state_store/mod.rs index 8373380a9d..385e6beb1f 100644 --- a/vm/types/src/state_store/mod.rs +++ b/vm/types/src/state_store/mod.rs @@ -5,6 +5,7 @@ use crate::state_store::{ errors::StateviewError, in_memory_state_view::InMemoryStateView, state_key::StateKey, state_storage_usage::StateStorageUsage, state_value::StateValue, }; +use crate::state_view::StateView; use crate::transaction::Version; use arr_macro::arr; use bytes::Bytes; @@ -49,10 +50,6 @@ pub trait TStateView { } } -pub trait StateView: TStateView {} - -impl> StateView for T {} - #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum StateViewId { // XXX FIXME YSG should remove diff --git a/vm/types/src/state_view.rs b/vm/types/src/state_view.rs index afa9f81c94..20ce541ac4 100644 --- a/vm/types/src/state_view.rs +++ b/vm/types/src/state_view.rs @@ -9,7 +9,7 @@ //! This crate defines [`trait StateView`](StateView). use crate::state_store::state_key::StateKey; -use crate::state_store::StateView; +use crate::state_store::TStateView; use crate::{ account_config::{ genesis_address, token_code::TokenCode, AccountResource, BalanceResource, TokenInfo, @@ -31,6 +31,10 @@ use move_core_types::{ language_storage::{ModuleId, StructTag}, }; +pub trait StateView: TStateView {} + +impl> StateView for T {} + impl StateReaderExt for T where T: StateView {} pub trait StateReaderExt: StateView {