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

[RPC Server] Simplify the response schema for object_info #1440

Merged
merged 3 commits into from
Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Add new rpc endpoint to get typed object info
  • Loading branch information
666lcz committed Apr 19, 2022
commit 1f91097853bd9d5e744a286dd19f240de5928006
7 changes: 3 additions & 4 deletions sui/src/rest_gateway/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ 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;

Expand Down Expand Up @@ -159,16 +160,14 @@ pub enum GetObjectInfoResponse {
}

impl TryFrom<ObjectRead> for GetObjectInfoResponse {
type Error = HttpError;
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).map_err(|e| {
custom_http_error(StatusCode::INTERNAL_SERVER_ERROR, e.to_string())
})?,
object: object.to_json(&layout)?,
}))
}
ObjectRead::NotExists(object_id) => Ok(Self::NotExists(ObjectNotExistsResponse {
Expand Down
24 changes: 10 additions & 14 deletions sui/src/rest_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ use sui::rest_gateway::requests::{
SplitCoinRequest, SyncRequest, TransferTransactionRequest,
};
use sui::rest_gateway::responses::{
custom_http_error, GetObjectInfoResponse, HttpResponseOk, JsonResponse, NamedObjectRef,
ObjectResponse, ObjectSchemaResponse, TransactionBytes,
custom_http_error, HttpResponseOk, JsonResponse, NamedObjectRef, ObjectResponse,
ObjectSchemaResponse, TransactionBytes,
};
use sui::{sui_config_dir, SUI_GATEWAY_CONFIG};
use sui_core::gateway_state::gateway_responses::TransactionResponse;
Expand Down Expand Up @@ -265,24 +265,20 @@ async fn object_schema(
async fn object_info(
ctx: Arc<RequestContext<ServerContext>>,
query: Query<GetObjectInfoRequest>,
) -> Result<HttpResponseOk<GetObjectInfoResponse>, HttpError> {
) -> Result<HttpResponseOk<JsonResponse<ObjectRead>>, HttpError> {
let gateway = ctx.context().gateway.lock().await;

let object_info_params = query.into_inner();
let object_id = ObjectID::try_from(object_info_params.object_id)
.map_err(|error| custom_http_error(StatusCode::BAD_REQUEST, format!("{error}")))?;

let object: GetObjectInfoResponse = gateway
.get_object_info(object_id)
.await
.map_err(|error| {
custom_http_error(
StatusCode::NOT_FOUND,
format!("Error while getting object info: {:?}", error),
)
})?
.try_into()?;
Ok(HttpResponseOk(object))
let object_read = gateway.get_object_info(object_id).await.map_err(|error| {
custom_http_error(
StatusCode::NOT_FOUND,
format!("Error while getting object info: {:?}", error),
)
})?;
Ok(HttpResponseOk(JsonResponse(object_read)))
}

/// Transfer object from one address to another. Gas will be paid using the gas
Expand Down
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