From 83854cf33a4feacc9365a0601502b8a8ed63a322 Mon Sep 17 00:00:00 2001 From: simonjiao Date: Fri, 24 Jan 2025 21:53:56 +0800 Subject: [PATCH] refactor as_struct_tag --- rpc/api/src/types.rs | 1 + rpc/server/src/module/contract_rpc.rs | 2 +- state/statedb/src/lib.rs | 10 ++++++---- vm/dev/src/playground.rs | 2 +- vm/types/src/access_path.rs | 10 ++++++++-- 5 files changed, 17 insertions(+), 8 deletions(-) diff --git a/rpc/api/src/types.rs b/rpc/api/src/types.rs index ace5ae4488..8a66da930c 100644 --- a/rpc/api/src/types.rs +++ b/rpc/api/src/types.rs @@ -1224,6 +1224,7 @@ fn merge_ap_write_set( action: WriteOpView::Value, value: Some(WriteOpValueView::Resource(data.to_vec().into())), }), + // Parsing resources in group and expanding them into individual actions. DataPath::ResourceGroup(_) => { let group_data: BTreeMap = bcs_ext::from_bytes(&data).expect("resource group data must be valid"); diff --git a/rpc/server/src/module/contract_rpc.rs b/rpc/server/src/module/contract_rpc.rs index 07bab68415..15b7c1dda8 100644 --- a/rpc/server/src/module/contract_rpc.rs +++ b/rpc/server/src/module/contract_rpc.rs @@ -306,7 +306,7 @@ pub fn dry_run( view.abi = Some(resolver.resolve_module_code(view.code.0.as_slice())?); } WriteOpValueView::Resource(view) => { - let struct_tag = access_path.path.as_struct_tag().ok_or_else(|| { + let struct_tag = access_path.path.resource_tag().ok_or_else(|| { format_err!("invalid resource access path: {}", access_path) })?; let struct_abi = resolver.resolve_struct_tag(struct_tag)?; diff --git a/state/statedb/src/lib.rs b/state/statedb/src/lib.rs index fcdb771fae..b90f88f85c 100644 --- a/state/statedb/src/lib.rs +++ b/state/statedb/src/lib.rs @@ -164,10 +164,12 @@ impl AccountStateObject { if data_path.is_code() { bail!("Not supported remove code currently."); } - let struct_tag = data_path - .as_struct_tag() - .expect("DataPath must been struct tag at here."); - self.resource_tree.lock().remove(struct_tag); + if let Some(struct_tag) = data_path.resource_tag() { + self.resource_tree.lock().remove(struct_tag); + } + if let Some(struct_tag) = data_path.resource_group_tag() { + self.resource_tree.lock().remove(struct_tag); + } Ok(()) } diff --git a/vm/dev/src/playground.rs b/vm/dev/src/playground.rs index 53384da8b9..a873a72f50 100644 --- a/vm/dev/src/playground.rs +++ b/vm/dev/src/playground.rs @@ -134,7 +134,7 @@ pub fn dry_run_explain( view.abi = Some(resolver.resolve_module_code(view.code.0.as_slice())?); } WriteOpValueView::Resource(view) => { - let struct_tag = access_path.path.as_struct_tag().ok_or_else(|| { + let struct_tag = access_path.path.resource_tag().ok_or_else(|| { format_err!("invalid resource access path: {}", access_path) })?; let struct_abi = resolver.resolve_struct_tag(struct_tag)?; diff --git a/vm/types/src/access_path.rs b/vm/types/src/access_path.rs index 9bd5e656de..e3e2bc6b0f 100644 --- a/vm/types/src/access_path.rs +++ b/vm/types/src/access_path.rs @@ -58,6 +58,7 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer}; use starcoin_crypto::hash::HashValue; use std::fmt; use std::str::FromStr; + #[derive(Clone, Eq, PartialEq, Hash, Ord, PartialOrd, JsonSchema)] #[cfg_attr(any(test, feature = "fuzzing"), derive(Arbitrary))] #[schemars(with = "String")] @@ -328,13 +329,18 @@ impl DataPath { pub fn is_code(&self) -> bool { matches!(self, Self::Code(_)) } - // todo(simon): handle ResourceGroup - pub fn as_struct_tag(&self) -> Option<&StructTag> { + pub fn resource_tag(&self) -> Option<&StructTag> { match self { Self::Resource(struct_tag) => Some(struct_tag), _ => None, } } + pub fn resource_group_tag(&self) -> Option<&StructTag> { + match self { + Self::ResourceGroup(struct_tag) => Some(struct_tag), + _ => None, + } + } pub fn data_type(&self) -> DataType { match self { Self::Code(_) => DataType::CODE,