Skip to content

Commit

Permalink
parse resources in group for rpc-api module
Browse files Browse the repository at this point in the history
  • Loading branch information
simonjiao committed Jan 23, 2025
1 parent 30cbba6 commit 9f402ca
Showing 1 changed file with 46 additions and 27 deletions.
73 changes: 46 additions & 27 deletions rpc/api/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use anyhow::bail;
use bcs_ext::BCSCodec;
use bytes::Bytes;
use hex::FromHex;
use jsonrpc_core_client::RpcChannel;
use move_core_types::u256;
Expand Down Expand Up @@ -36,7 +37,7 @@ use starcoin_types::transaction::authenticator::{AuthenticationKey, TransactionA
use starcoin_types::transaction::{EntryFunction, RawUserTransaction, TransactionArgument};
use starcoin_types::vm_error::AbortLocation;
use starcoin_types::U256;
use starcoin_vm_types::access_path::AccessPath;
use starcoin_vm_types::access_path::{AccessPath, DataPath};
use starcoin_vm_types::block_metadata::BlockMetadata;
use starcoin_vm_types::identifier::Identifier;
use starcoin_vm_types::language_storage::{parse_module_id, FunctionId, ModuleId, StructTag};
Expand Down Expand Up @@ -1225,14 +1226,54 @@ impl From<TransactionOutput> for TransactionOutputView {
StateKeyInner::Raw(_) => todo!("not support raw key"),
}
}
let write_set =
access_write_set
.into_iter()
.fold(Vec::new(), |mut acc, (access_path, op)| {
match op {
WriteOp::Deletion { .. } => acc.push(TransactionOutputAction {
access_path: access_path.clone(),
action: WriteOpView::Deletion,
value: None,
}),
WriteOp::Modification { data, .. } | WriteOp::Creation { data, .. } => {
match access_path.path {
DataPath::Resource(_) => acc.push(TransactionOutputAction {
access_path: access_path.clone(),
action: WriteOpView::Value,
value: Some(WriteOpValueView::Resource(data.to_vec().into())),
}),
DataPath::ResourceGroup(_) => {
let group_data: BTreeMap<StructTag, Bytes> =
bcs_ext::from_bytes(&data).unwrap();
for (struct_tag, data) in group_data {
let access_path = AccessPath::resource_access_path(
access_path.address,
struct_tag,
);
acc.push(TransactionOutputAction {
access_path: access_path.clone(),
action: WriteOpView::Value,
value: Some(WriteOpValueView::Resource(data.into())),
});
}
}
DataPath::Code(_) => acc.push(TransactionOutputAction {
access_path: access_path.clone(),
action: WriteOpView::Value,
value: Some(WriteOpValueView::Code(data.to_vec().into())),
}),
}
}
};
acc
});

Self {
events: events.into_iter().map(Into::into).collect(),
gas_used: gas_used.into(),
status: status.into(),
write_set: access_write_set
.into_iter()
.map(TransactionOutputAction::from)
.collect(),
write_set,
table_item_write_set: table_item_write_set
.into_iter()
.map(TransactionOutputTableItemAction::from)
Expand All @@ -1241,28 +1282,6 @@ impl From<TransactionOutput> for TransactionOutputView {
}
}

impl From<(AccessPath, WriteOp)> for TransactionOutputAction {
fn from((access_path, op): (AccessPath, WriteOp)) -> Self {
let (action, value) = match op {
WriteOp::Deletion { .. } => (WriteOpView::Deletion, None),
WriteOp::Modification { data, .. } | WriteOp::Creation { data, .. } => (
WriteOpView::Value,
Some(if access_path.path.is_resource() {
WriteOpValueView::Resource(data.to_vec().into())
} else {
WriteOpValueView::Code(data.to_vec().into())
}),
),
};

Self {
access_path,
action,
value,
}
}
}

#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
pub struct TransactionOutputAction {
pub access_path: AccessPath,
Expand Down

0 comments on commit 9f402ca

Please sign in to comment.