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

Fix more types #4201

Merged
merged 3 commits into from
Sep 23, 2024
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
25 changes: 19 additions & 6 deletions types/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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},
};
Expand Down Expand Up @@ -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.
Expand All @@ -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() {
Expand All @@ -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()),
));
}
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion vm/starcoin-aggregator/src/aggregator_v1_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
4 changes: 4 additions & 0 deletions vm/types/src/access_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion vm/types/src/account_config/events/config_change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
9 changes: 3 additions & 6 deletions vm/types/src/on_chain_config/mod.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::account_config::genesis_address;
use crate::state_store::StateView;
use crate::state_view::StateView;
use crate::{
access_path::AccessPath,
account_address::AccountAddress,
Expand Down Expand Up @@ -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<AccessPath> {
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 {
Expand Down
16 changes: 14 additions & 2 deletions vm/types/src/on_chain_resource/dao/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,18 @@ pub struct Proposal<A: ProposalAction> {
/// 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<A>,
}

fn deserialize_proposal_action<'de, D, A>(deserializer: D) -> Result<Option<A>, D::Error>
where
D: serde::Deserializer<'de>,
A: ProposalAction,
{
Ok(Some(A::deserialize(deserializer)?))
}

impl<A> MoveStructType for Proposal<A>
where
A: ProposalAction,
Expand All @@ -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
},
}
}

Expand Down
5 changes: 1 addition & 4 deletions vm/types/src/state_store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,10 +50,6 @@ pub trait TStateView {
}
}

pub trait StateView: TStateView<Key = StateKey> {}

impl<T: TStateView<Key = StateKey>> StateView for T {}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum StateViewId {
// XXX FIXME YSG should remove
Expand Down
36 changes: 11 additions & 25 deletions vm/types/src/state_store/state_key/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -114,25 +113,11 @@ impl StateKey {
use access_path::DataPath;

let myself = match deserialized {
StateKeyInner::AccessPath(AccessPath { address, path }) => {
match path {
//bcs::from_bytes::<DataPath>(&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<u8>` 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),
};
Expand All @@ -148,7 +133,7 @@ impl StateKey {
Ok(StateKeyInner::AccessPath(AccessPath::resource_access_path(
*address,
struct_tag.clone(),
)?))
)))
},
)?))
}
Expand All @@ -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"),
)
Expand Down
8 changes: 4 additions & 4 deletions vm/types/src/state_store/state_key/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ impl Drop for Entry {
StateKeyInner::AccessPath(AccessPath { address, path }) => {
use crate::access_path::DataPath;

match &bcs::from_bytes::<DataPath>(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),
Expand Down
6 changes: 5 additions & 1 deletion vm/types/src/state_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -31,6 +31,10 @@ use move_core_types::{
language_storage::{ModuleId, StructTag},
};

pub trait StateView: TStateView<Key = StateKey> {}

impl<T: TStateView<Key = StateKey>> StateView for T {}

impl<T: ?Sized> StateReaderExt for T where T: StateView {}

pub trait StateReaderExt: StateView {
Expand Down
5 changes: 3 additions & 2 deletions vm/types/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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
Expand Down
Loading