Skip to content

Commit

Permalink
types: failable conversions to/from sdk and core types (MystenLabs#19414
Browse files Browse the repository at this point in the history
)
  • Loading branch information
bmwill authored Sep 18, 2024
1 parent d12505f commit 0b276e5
Show file tree
Hide file tree
Showing 10 changed files with 188 additions and 60 deletions.
4 changes: 2 additions & 2 deletions crates/sui-rest-api/src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ async fn list_account_objects(
let mut object_info = state
.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()),
type_: struct_tag_core_to_sdk(info.type_.into()).expect("object types must be valid"),
})
.take(limit + 1)
.collect::<Vec<_>>();

let cursor = if object_info.len() > limit {
Expand Down
12 changes: 8 additions & 4 deletions crates/sui-rest-api/src/checkpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,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 @@ -277,11 +277,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/sui-rest-api/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,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 @@ -66,15 +66,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 @@ -86,7 +86,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/sui-rest-api/src/client/sdk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,3 +666,9 @@ impl From<url::ParseError> for Error {
Self::from_error(error)
}
}

impl From<sui_types::sui_sdk2_conversions::SdkTypeConversionError> for Error {
fn from(value: sui_types::sui_sdk2_conversions::SdkTypeConversionError) -> Self {
Self::from_error(value)
}
}
2 changes: 1 addition & 1 deletion crates/sui-rest-api/src/coins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,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 sui_types::storage::CoinInfo {
coin_metadata_object_id,
Expand Down
18 changes: 18 additions & 0 deletions crates/sui-rest-api/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,24 @@ impl From<anyhow::Error> for RestError {
}
}

impl From<sui_types::sui_sdk2_conversions::SdkTypeConversionError> for RestError {
fn from(value: sui_types::sui_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<sui_types::quorum_driver_types::QuorumDriverError> for RestError {
fn from(error: sui_types::quorum_driver_types::QuorumDriverError) -> Self {
use itertools::Itertools;
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-rest-api/src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ 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).expect("object types must be valid"),
name_value,
dynamic_object_id: dynamic_object_id.map(Into::into),
}
Expand Down
16 changes: 11 additions & 5 deletions crates/sui-rest-api/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,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 @@ -90,7 +92,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/sui-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 = sui_types::quorum_driver_types::ExecuteTransactionRequestV3 {
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

0 comments on commit 0b276e5

Please sign in to comment.