Skip to content

Commit

Permalink
rpc: add grpc tests for GetObject
Browse files Browse the repository at this point in the history
  • Loading branch information
bmwill committed Dec 23, 2024
1 parent 3805229 commit 8740315
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 8 deletions.
85 changes: 77 additions & 8 deletions crates/sui-e2e-tests/tests/rpc/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,96 @@
use sui_macros::sim_test;
use sui_rpc_api::client::sdk::Client;
use sui_rpc_api::client::Client as CoreClient;
use sui_rpc_api::proto::node::node_client::NodeClient;
use sui_rpc_api::proto::node::GetObjectOptions;
use sui_rpc_api::proto::node::GetObjectRequest;
use sui_rpc_api::proto::node::GetObjectResponse;
use sui_sdk_types::types::ObjectId;
use test_cluster::TestClusterBuilder;

#[sim_test]
async fn get_object() {
let test_cluster = TestClusterBuilder::new().build().await;

let id: ObjectId = "0x5".parse().unwrap();

let client = Client::new(test_cluster.rpc_url()).unwrap();
let core_client = CoreClient::new(test_cluster.rpc_url()).unwrap();
let mut grpc_client = NodeClient::connect(test_cluster.rpc_url().to_owned())
.await
.unwrap();

let _object = client.get_object(id).await.unwrap();
let _object = core_client.get_object(id.into()).await.unwrap();

let _object = client.get_object("0x5".parse().unwrap()).await.unwrap();
let _object = client.get_object_with_version(id, 1).await.unwrap();
let _object = core_client
.get_object("0x5".parse().unwrap())
.get_object_with_version(id.into(), 1.into())
.await
.unwrap();

let _object = client
.get_object_with_version("0x5".parse().unwrap(), 1)
let GetObjectResponse {
object_id,
version,
digest,
object,
object_bcs,
} = grpc_client
.get_object(GetObjectRequest::new(id))
.await
.unwrap();
let _object = core_client
.get_object_with_version("0x5".parse().unwrap(), 1.into())
.unwrap()
.into_inner();

assert_eq!(object_id, Some(id.into()));
assert!(version.is_some());
assert!(digest.is_some());
assert!(object.is_some());
assert!(object_bcs.is_none()); // By default object_bcs isn't returned

let GetObjectResponse {
object_id,
version,
digest,
object,
object_bcs,
} = grpc_client
.get_object(
GetObjectRequest::new(id)
.with_version(1)
.with_options(GetObjectOptions::none()),
)
.await
.unwrap();
.unwrap()
.into_inner();

assert_eq!(object_id, Some(id.into()));
assert_eq!(version, Some(1));
assert!(digest.is_some());

// These fields were not requested
assert!(object.is_none());
assert!(object_bcs.is_none());

let response = grpc_client
.get_object(GetObjectRequest::new(id).with_options(GetObjectOptions::all()))
.await
.unwrap()
.into_inner();

let GetObjectResponse {
object_id,
version,
digest,
object,
object_bcs,
} = &response;

assert!(object_id.is_some());
assert!(version.is_some());
assert!(digest.is_some());
assert!(object.is_some());
assert!(object_bcs.is_some());

// ensure we can convert proto ObjectResponse type to rust ObjectResponse
sui_rpc_api::types::ObjectResponse::try_from(&response).unwrap();
}
60 changes: 60 additions & 0 deletions crates/sui-rpc-api/src/proto/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,42 @@ impl TryFrom<&GetNodeInfoResponse> for crate::types::NodeInfo {
// GetObjectOptions
//

impl GetObjectOptions {
pub fn all() -> Self {
Self {
object: Some(true),
object_bcs: Some(true),
}
}

pub fn none() -> Self {
Self {
object: Some(false),
object_bcs: Some(false),
}
}

pub fn with_object(mut self) -> Self {
self.object = Some(true);
self
}

pub fn without_object(mut self) -> Self {
self.object = Some(false);
self
}

pub fn with_object_bcs(mut self) -> Self {
self.object_bcs = Some(true);
self
}

pub fn without_object_bcs(mut self) -> Self {
self.object = Some(false);
self
}
}

impl From<crate::types::GetObjectOptions> for GetObjectOptions {
fn from(
crate::types::GetObjectOptions { object, object_bcs }: crate::types::GetObjectOptions,
Expand All @@ -166,6 +202,30 @@ impl From<GetObjectOptions> for crate::types::GetObjectOptions {
}
}

//
// GetObjectRequest
//

impl GetObjectRequest {
pub fn new<T: Into<super::types::ObjectId>>(object_id: T) -> Self {
Self {
object_id: Some(object_id.into()),
version: None,
options: None,
}
}

pub fn with_version(mut self, version: u64) -> Self {
self.version = Some(version);
self
}

pub fn with_options(mut self, options: GetObjectOptions) -> Self {
self.options = Some(options);
self
}
}

//
// ObjectResponse
//
Expand Down

0 comments on commit 8740315

Please sign in to comment.