Skip to content

Commit

Permalink
[RPC Server] Simplify the response schema for object_info (#1440)
Browse files Browse the repository at this point in the history
  • Loading branch information
666lcz authored Apr 20, 2022
1 parent bc657fb commit d6a9285
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 10 deletions.
61 changes: 53 additions & 8 deletions sui/src/rest_gateway/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ use serde_with::serde_as;

use sui_types::base_types::{ObjectDigest, ObjectID, ObjectRef, SequenceNumber};
use sui_types::crypto::SignableBytes;
use sui_types::error::SuiError;
use sui_types::messages::TransactionData;
use sui_types::object::ObjectRead;

#[serde_as]
#[derive(Serialize, Deserialize, JsonSchema)]
Expand All @@ -39,14 +41,6 @@ pub struct NamedObjectRef {
}

impl NamedObjectRef {
pub fn from((object_id, version, digest): ObjectRef) -> Self {
Self {
object_id: object_id.to_hex(),
version: version.value(),
digest: Base64::encode_string(digest.as_ref()),
}
}

pub fn to_object_ref(self) -> Result<ObjectRef, anyhow::Error> {
Ok((
ObjectID::try_from(self.object_id)?,
Expand All @@ -56,6 +50,16 @@ impl NamedObjectRef {
}
}

impl From<ObjectRef> for NamedObjectRef {
fn from((object_id, version, digest): ObjectRef) -> Self {
Self {
object_id: object_id.to_hex(),
version: version.value(),
digest: Base64::encode_string(digest.as_ref()),
}
}
}

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase", transparent)]
pub struct JsonResponse<T>(pub T);
Expand Down Expand Up @@ -132,3 +136,44 @@ impl<T: JsonSchema + Serialize + Send + Sync + 'static> HttpResponse for HttpRes
dropshot::HttpResponseOk::<T>::metadata()
}
}

#[derive(Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct ObjectExistsResponse {
object_ref: NamedObjectRef,
object: Value,
}

#[derive(Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct ObjectNotExistsResponse {
object_id: String,
}

#[allow(clippy::large_enum_variant)]
#[derive(Serialize, Deserialize, JsonSchema)]
#[serde(tag = "status", content = "details")]
pub enum GetObjectInfoResponse {
Exists(ObjectExistsResponse),
NotExists(ObjectNotExistsResponse),
Deleted(NamedObjectRef),
}

impl TryFrom<ObjectRead> for GetObjectInfoResponse {
type Error = SuiError;

fn try_from(obj: ObjectRead) -> Result<Self, Self::Error> {
match obj {
ObjectRead::Exists(object_ref, object, layout) => {
Ok(Self::Exists(ObjectExistsResponse {
object_ref: object_ref.into(),
object: object.to_json(&layout)?,
}))
}
ObjectRead::NotExists(object_id) => Ok(Self::NotExists(ObjectNotExistsResponse {
object_id: object_id.to_hex(),
})),
ObjectRead::Deleted(obj_ref) => Ok(Self::Deleted(obj_ref.into())),
}
}
}
19 changes: 17 additions & 2 deletions sui/src/rpc_gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ use sui_types::object::ObjectRead;

use crate::config::PersistedConfig;
use crate::gateway::GatewayConfig;
use crate::rest_gateway::responses::GetObjectInfoResponse;
use crate::rest_gateway::responses::{NamedObjectRef, ObjectResponse};

#[rpc(server, client, namespace = "sui")]
pub trait RpcGateway {
#[method(name = "getObjectInfo")]
async fn get_object_info(&self, object_id: ObjectID) -> RpcResult<ObjectRead>;
#[method(name = "getObjectTypedInfo")]
async fn get_object_typed_info(&self, object_id: ObjectID) -> RpcResult<GetObjectInfoResponse>;

#[method(name = "transferCoin")]
async fn transfer_coin(
Expand Down Expand Up @@ -118,6 +119,11 @@ pub trait RpcGateway {

#[method(name = "getTransaction")]
async fn get_transaction(&self, digest: TransactionDigest) -> RpcResult<CertifiedTransaction>;

/// Low level API to get object info. Client Applications should prefer to use
/// `get_object_typed_info` instead.
#[method(name = "getObjectInfoRaw")]
async fn get_object_info(&self, object_id: ObjectID) -> RpcResult<ObjectRead>;
}

pub struct RpcGatewayImpl {
Expand Down Expand Up @@ -248,6 +254,15 @@ impl RpcGatewayServer for RpcGatewayImpl {
Ok(self.gateway.get_object_info(object_id).await?)
}

async fn get_object_typed_info(&self, object_id: ObjectID) -> RpcResult<GetObjectInfoResponse> {
Ok(self
.gateway
.get_object_info(object_id)
.await?
.try_into()
.map_err(|e| anyhow!("{}", e))?)
}

async fn execute_transaction(
&self,
signed_tx: SignedTransaction,
Expand Down

1 comment on commit d6a9285

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bench results

�[0m�[0m�[1m�[32m Finished�[0m release [optimized] target(s) in 0.45s
�[0m�[0m�[1m�[32m Running�[0m target/release/bench microbench throughput
�[2m2022-04-20T00:15:14.620206Z�[0m �[32m INFO�[0m �[2msui::benchmark::transaction_creator�[0m�[2m:�[0m Open database on path: "/tmp/DB_7CBE34A07DD065B3A14B662D83AE3B9D225D2363"
�[2m2022-04-20T00:15:16.455680Z�[0m �[32m INFO�[0m �[2msui_network::transport�[0m�[2m:�[0m Listening to TCP traffic on 127.0.0.1:9555
Throughout: 44723.979253440506 tps

Please sign in to comment.