Skip to content

Commit

Permalink
rest: avoid panic when converting from TypeTag (#19431)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmwill authored Sep 18, 2024
1 parent 70ff0c7 commit fd3ffac
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 26 deletions.
15 changes: 9 additions & 6 deletions crates/sui-rest-api/src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,16 @@ async fn list_account_objects(
.inner()
.account_owned_objects_info_iter(address.into(), start)?
.take(limit + 1)
.map(|info| AccountOwnedObjectInfo {
owner: info.owner.into(),
object_id: info.object_id.into(),
version: info.version.into(),
type_: struct_tag_core_to_sdk(info.type_.into()).expect("object types must be valid"),
.map(|info| {
AccountOwnedObjectInfo {
owner: info.owner.into(),
object_id: info.object_id.into(),
version: info.version.into(),
type_: struct_tag_core_to_sdk(info.type_.into())?,
}
.pipe(Ok)
})
.collect::<Vec<_>>();
.collect::<Result<Vec<_>>>()?;

let cursor = if object_info.len() > limit {
// SAFETY: We've already verified that object_info is greater than limit, which is
Expand Down
18 changes: 12 additions & 6 deletions crates/sui-rest-api/src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ use axum::extract::Query;
use axum::extract::{Path, State};
use serde::{Deserialize, Serialize};
use sui_sdk2::types::{Object, ObjectId, TypeTag, Version};
use sui_types::storage::{DynamicFieldIndexInfo, DynamicFieldKey};
use sui_types::sui_sdk2_conversions::type_tag_core_to_sdk;
use sui_types::{
storage::{DynamicFieldIndexInfo, DynamicFieldKey},
sui_sdk2_conversions::SdkTypeConversionError,
};
use tap::Pipe;

pub struct GetObject;
Expand Down Expand Up @@ -219,8 +222,8 @@ async fn list_dynamic_fields(
.inner()
.dynamic_field_iter(parent.into(), start)?
.take(limit + 1)
.map(DynamicFieldInfo::from)
.collect::<Vec<_>>();
.map(DynamicFieldInfo::try_from)
.collect::<Result<Vec<_>, _>>()?;

let cursor = if dynamic_fields.len() > limit {
// SAFETY: We've already verified that object_keys is greater than limit, which is
Expand Down Expand Up @@ -271,8 +274,10 @@ pub struct DynamicFieldInfo {
pub dynamic_object_id: Option<ObjectId>,
}

impl From<(DynamicFieldKey, DynamicFieldIndexInfo)> for DynamicFieldInfo {
fn from(value: (DynamicFieldKey, DynamicFieldIndexInfo)) -> Self {
impl TryFrom<(DynamicFieldKey, DynamicFieldIndexInfo)> for DynamicFieldInfo {
type Error = SdkTypeConversionError;

fn try_from(value: (DynamicFieldKey, DynamicFieldIndexInfo)) -> Result<Self, Self::Error> {
let DynamicFieldKey { parent, field_id } = value.0;
let DynamicFieldIndexInfo {
dynamic_field_type,
Expand All @@ -285,10 +290,11 @@ impl From<(DynamicFieldKey, DynamicFieldIndexInfo)> for DynamicFieldInfo {
parent: parent.into(),
field_id: field_id.into(),
dynamic_field_type: dynamic_field_type.into(),
name_type: type_tag_core_to_sdk(name_type).expect("object types must be valid"),
name_type: type_tag_core_to_sdk(name_type)?,
name_value,
dynamic_object_id: dynamic_object_id.map(Into::into),
}
.pipe(Ok)
}
}

Expand Down
14 changes: 0 additions & 14 deletions crates/sui-types/src/sui_sdk2_conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,6 @@ impl From<bcs::Error> for SdkTypeConversionError {

macro_rules! bcs_convert_impl {
($core:ty, $external:ty) => {
// impl From<$core> for $external {
// fn from(value: $core) -> Self {
// let bytes = bcs::to_bytes(&value).unwrap();
// bcs::from_bytes(&bytes).unwrap()
// }
// }

// impl From<$external> for $core {
// fn from(value: $external) -> Self {
// let bytes = bcs::to_bytes(&value).unwrap();
// bcs::from_bytes(&bytes).unwrap()
// }
// }

impl TryFrom<$core> for $external {
type Error = bcs::Error;

Expand Down

0 comments on commit fd3ffac

Please sign in to comment.