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

feat(types): Enable failable conversions to/from sdk and core types #4871

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
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
17 changes: 10 additions & 7 deletions crates/iota-rest-api/src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,17 @@ async fn list_account_objects(
let mut object_info = state
.inner()
.account_owned_objects_info_iter(address.into(), start)?
.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()),
})
.take(limit + 1)
.collect::<Vec<_>>();
.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::<Result<Vec<_>>>()?;

let cursor = if object_info.len() > limit {
// SAFETY: We've already verified that object_info is greater than limit, which
Expand Down
12 changes: 8 additions & 4 deletions crates/iota-rest-api/src/checkpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ async fn get_checkpoint(
}?
.ok_or(CheckpointNotFoundError(checkpoint_id))?
.into_inner()
.into();
.try_into()?;

match accept {
AcceptFormat::Json => ResponseContent::Json(summary),
Expand Down Expand Up @@ -279,11 +279,15 @@ async fn list_checkpoints(

let checkpoints = state
.checkpoint_iter(direction, start)
.take(limit)
.map(|result| {
result.map(|(checkpoint, _contents)| SignedCheckpointSummary::from(checkpoint))
result
.map_err(Into::into)
.and_then(|(checkpoint, _contents)| {
SignedCheckpointSummary::try_from(checkpoint).map_err(Into::into)
})
})
.take(limit)
.collect::<Result<Vec<_>, _>>()?;
.collect::<Result<Vec<_>>>()?;

let cursor = checkpoints.last().and_then(|checkpoint| match direction {
Direction::Ascending => checkpoint.checkpoint.sequence_number.checked_add(1),
Expand Down
8 changes: 4 additions & 4 deletions crates/iota-rest-api/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Client {
.get_latest_checkpoint()
.await
.map(Response::into_inner)
.map(Into::into)
.and_then(|checkpoint| checkpoint.try_into().map_err(Into::into))
}

pub async fn get_full_checkpoint(
Expand Down Expand Up @@ -68,15 +68,15 @@ impl Client {
.get_checkpoint(checkpoint_sequence_number)
.await
.map(Response::into_inner)
.map(Into::into)
.and_then(|checkpoint| checkpoint.try_into().map_err(Into::into))
}

pub async fn get_object(&self, object_id: ObjectID) -> Result<Object> {
self.inner
.get_object(object_id.into())
.await
.map(Response::into_inner)
.map(Into::into)
.and_then(|object| object.try_into().map_err(Into::into))
}

pub async fn get_object_with_version(
Expand All @@ -88,7 +88,7 @@ impl Client {
.get_object_with_version(object_id.into(), version.into())
.await
.map(Response::into_inner)
.map(Into::into)
.and_then(|object| object.try_into().map_err(Into::into))
}

pub async fn execute_transaction(
Expand Down
6 changes: 6 additions & 0 deletions crates/iota-rest-api/src/client/sdk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,3 +652,9 @@ impl From<url::ParseError> for Error {
Self::from_error(error)
}
}

impl From<iota_types::iota_sdk2_conversions::SdkTypeConversionError> for Error {
fn from(value: iota_types::iota_sdk2_conversions::SdkTypeConversionError) -> Self {
Self::from_error(value)
}
}
2 changes: 1 addition & 1 deletion crates/iota-rest-api/src/coins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async fn get_coin_info(
Path(coin_type): Path<StructTag>,
State(state): State<StateReader>,
) -> Result<Json<CoinInfo>> {
let core_coin_type = struct_tag_sdk_to_core(coin_type.clone());
let core_coin_type = struct_tag_sdk_to_core(coin_type.clone())?;

let iota_types::storage::CoinInfo {
coin_metadata_object_id,
Expand Down
18 changes: 18 additions & 0 deletions crates/iota-rest-api/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@ impl From<anyhow::Error> for RestError {
}
}

impl From<iota_types::iota_sdk2_conversions::SdkTypeConversionError> for RestError {
fn from(value: iota_types::iota_sdk2_conversions::SdkTypeConversionError) -> Self {
Self {
status: StatusCode::INTERNAL_SERVER_ERROR,
message: Some(value.to_string()),
}
}
}

impl From<bcs::Error> for RestError {
fn from(value: bcs::Error) -> Self {
Self {
status: StatusCode::INTERNAL_SERVER_ERROR,
message: Some(value.to_string()),
}
}
}

impl From<iota_types::quorum_driver_types::QuorumDriverError> for RestError {
fn from(error: iota_types::quorum_driver_types::QuorumDriverError) -> Self {
use iota_types::{error::IotaError, quorum_driver_types::QuorumDriverError::*};
Expand Down
15 changes: 9 additions & 6 deletions crates/iota-rest-api/src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use axum::extract::{Path, Query, State};
use iota_sdk2::types::{Object, ObjectId, TypeTag, Version};
use iota_types::{
iota_sdk2_conversions::type_tag_core_to_sdk,
iota_sdk2_conversions::{SdkTypeConversionError, type_tag_core_to_sdk},
storage::{DynamicFieldIndexInfo, DynamicFieldKey},
};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -222,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
Expand Down Expand Up @@ -275,8 +275,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 @@ -289,10 +291,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),
name_type: type_tag_core_to_sdk(name_type)?,
name_value,
dynamic_object_id: dynamic_object_id.map(Into::into),
}
.pipe(Ok)
}
}

Expand Down
16 changes: 11 additions & 5 deletions crates/iota-rest-api/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,22 @@ impl StateReader {
&self.inner
}

pub fn get_object(&self, object_id: ObjectId) -> Result<Option<Object>> {
pub fn get_object(&self, object_id: ObjectId) -> crate::Result<Option<Object>> {
self.inner
.get_object(&object_id.into())
.map(|maybe| maybe.map(Into::into))
.map_err(Into::into)
.and_then(|maybe| maybe.map(TryInto::try_into).transpose().map_err(Into::into))
}

pub fn get_object_with_version(
&self,
object_id: ObjectId,
version: Version,
) -> Result<Option<Object>> {
) -> crate::Result<Option<Object>> {
self.inner
.get_object_by_key(&object_id.into(), version.into())
.map(|maybe| maybe.map(Into::into))
.map_err(Into::into)
.and_then(|maybe| maybe.map(TryInto::try_into).transpose().map_err(Into::into))
}

pub fn get_committee(&self, epoch: EpochId) -> Result<Option<ValidatorCommittee>> {
Expand Down Expand Up @@ -95,7 +97,11 @@ impl StateReader {
None
};

Ok((transaction.into(), effects.into(), events.map(Into::into)))
Ok((
transaction.try_into()?,
effects.try_into()?,
events.map(TryInto::try_into).transpose()?,
))
}

pub fn get_transaction_response(
Expand Down
26 changes: 19 additions & 7 deletions crates/iota-rest-api/src/transactions/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ async fn execute_transaction(
) -> Result<ResponseContent<TransactionExecutionResponse>> {
let executor = state.ok_or_else(|| anyhow::anyhow!("No Transaction Executor"))?;
let request = iota_types::quorum_driver_types::ExecuteTransactionRequestV1 {
transaction: transaction.into(),
transaction: transaction.try_into()?,
include_events: parameters.events,
include_input_objects: parameters.input_objects || parameters.balance_changes,
include_output_objects: parameters.output_objects || parameters.balance_changes,
Expand Down Expand Up @@ -108,19 +108,31 @@ async fn execute_transaction(
) => EffectsFinality::Checkpointed { checkpoint },
};

(effects.into(), finality)
(effects.try_into()?, finality)
};

let events = if parameters.events {
events.map(Into::into)
events.map(TryInto::try_into).transpose()?
} else {
None
};

let input_objects =
input_objects.map(|objects| objects.into_iter().map(Into::into).collect::<Vec<_>>());
let output_objects =
output_objects.map(|objects| objects.into_iter().map(Into::into).collect::<Vec<_>>());
let input_objects = input_objects
.map(|objects| {
objects
.into_iter()
.map(TryInto::try_into)
.collect::<Result<Vec<_>, _>>()
})
.transpose()?;
let output_objects = output_objects
.map(|objects| {
objects
.into_iter()
.map(TryInto::try_into)
.collect::<Result<Vec<_>, _>>()
})
.transpose()?;

let balance_changes = match (parameters.balance_changes, &input_objects, &output_objects) {
(true, Some(input_objects), Some(output_objects)) => Some(derive_balance_changes(
Expand Down
Loading
Loading