Skip to content

Commit abace30

Browse files
committed
grpc: add support for rendering move objects as json
1 parent 4a16534 commit abace30

File tree

7 files changed

+51
-2
lines changed

7 files changed

+51
-2
lines changed

crates/sui-e2e-tests/tests/rpc/ledger_service/get_object.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ async fn get_object() {
3737
linkage_table,
3838
previous_transaction,
3939
storage_rebate,
40+
json,
4041
} = client
4142
.get_object(GetObjectRequest {
4243
object_id: Some(id.to_string()),
@@ -63,6 +64,7 @@ async fn get_object() {
6364
assert!(linkage_table.is_empty());
6465
assert!(previous_transaction.is_none());
6566
assert!(storage_rebate.is_none());
67+
assert!(json.is_none());
6668

6769
// Request with provided read_mask
6870
let Object {
@@ -79,6 +81,7 @@ async fn get_object() {
7981
linkage_table,
8082
previous_transaction,
8183
storage_rebate,
84+
json,
8285
} = client
8386
.get_object(GetObjectRequest {
8487
object_id: Some(id.to_string()),
@@ -104,6 +107,7 @@ async fn get_object() {
104107
assert!(linkage_table.is_empty());
105108
assert!(previous_transaction.is_none());
106109
assert!(storage_rebate.is_none());
110+
assert!(json.is_none());
107111

108112
let response = client
109113
.get_object(GetObjectRequest {
@@ -114,6 +118,7 @@ async fn get_object() {
114118
"version",
115119
"digest",
116120
"bcs",
121+
"json",
117122
])),
118123
})
119124
.await
@@ -134,6 +139,7 @@ async fn get_object() {
134139
linkage_table,
135140
previous_transaction,
136141
storage_rebate,
142+
json,
137143
} = &response;
138144

139145
assert!(object_id.is_some());
@@ -150,6 +156,7 @@ async fn get_object() {
150156
assert!(linkage_table.is_empty());
151157
assert!(previous_transaction.is_none());
152158
assert!(storage_rebate.is_none());
159+
assert!(json.is_some());
153160
}
154161

155162
#[sim_test]

crates/sui-rpc-api/proto/sui/rpc/v2beta/object.proto

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ syntax = "proto3";
55

66
package sui.rpc.v2beta;
77

8+
import "google/protobuf/struct.proto";
89
import "sui/rpc/v2beta/bcs.proto";
910
import "sui/rpc/v2beta/owner.proto";
1011

@@ -65,6 +66,9 @@ message Object {
6566
// This number is re-calculated each time the object is mutated based on
6667
// the present storage gas price.
6768
optional uint64 storage_rebate = 13;
69+
70+
// JSON rendering of the object.
71+
optional google.protobuf.Value json = 100;
6872
}
6973

7074
// Module defined by a package.

crates/sui-rpc-api/src/grpc/v2beta/ledger_service/get_object.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33

44
use prost_types::FieldMask;
55
use sui_sdk_types::ObjectId;
6+
use sui_types::sui_sdk_types_conversions::struct_tag_sdk_to_core;
67

78
use crate::error::ObjectNotFoundError;
89
use crate::field_mask::FieldMaskTree;
910
use crate::field_mask::FieldMaskUtil;
10-
use crate::message::MessageMergeFrom;
11+
use crate::message::MessageMerge;
1112
use crate::proto::google::rpc::bad_request::FieldViolation;
1213
use crate::proto::rpc::v2beta::BatchGetObjectsRequest;
1314
use crate::proto::rpc::v2beta::BatchGetObjectsResponse;
@@ -113,5 +114,31 @@ fn get_object_impl(
113114
.ok_or_else(|| ObjectNotFoundError::new(object_id))?
114115
};
115116

116-
Ok(Object::merge_from(object, read_mask))
117+
let mut message = Object::default();
118+
119+
if read_mask.contains(Object::JSON_FIELD.name) {
120+
message.json = object
121+
.as_struct()
122+
.and_then(|s| {
123+
let struct_tag = struct_tag_sdk_to_core(s.object_type().to_owned()).ok()?;
124+
let layout = service
125+
.reader
126+
.inner()
127+
.get_struct_layout(&struct_tag)
128+
.ok()
129+
.flatten()?;
130+
Some((layout, s.contents()))
131+
})
132+
.and_then(|(layout, contents)| {
133+
sui_types::proto_value::ProtoVisitor::default()
134+
.deserialize_value(contents, &layout)
135+
.map_err(|e| tracing::debug!("unable to convert to JSON: {e}"))
136+
.ok()
137+
.map(Box::new)
138+
});
139+
}
140+
141+
message.merge(object, read_mask);
142+
143+
Ok(message)
117144
}
Binary file not shown.

crates/sui-rpc-api/src/proto/generated/sui.rpc.v2beta.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2269,6 +2269,9 @@ pub struct Object {
22692269
/// the present storage gas price.
22702270
#[prost(uint64, optional, tag = "13")]
22712271
pub storage_rebate: ::core::option::Option<u64>,
2272+
/// JSON rendering of the object.
2273+
#[prost(message, optional, boxed, tag = "100")]
2274+
pub json: ::core::option::Option<::prost::alloc::boxed::Box<::prost_types::Value>>,
22722275
}
22732276
/// Module defined by a package.
22742277
#[derive(Clone, PartialEq, ::prost::Message)]

crates/sui-rpc-api/src/proto/rpc/v2beta/object.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ impl Object {
3030
pub const PREVIOUS_TRANSACTION_FIELD: &'static MessageField =
3131
&MessageField::new("previous_transaction");
3232
pub const STORAGE_REBATE_FIELD: &'static MessageField = &MessageField::new("storage_rebate");
33+
pub const JSON_FIELD: &'static MessageField = &MessageField::new("json");
3334
}
3435

3536
impl MessageFields for Object {
@@ -47,6 +48,7 @@ impl MessageFields for Object {
4748
Self::LINKAGE_TABLE_FIELD,
4849
Self::PREVIOUS_TRANSACTION_FIELD,
4950
Self::STORAGE_REBATE_FIELD,
51+
Self::JSON_FIELD,
5052
];
5153
}
5254

@@ -74,6 +76,7 @@ impl MessageMerge<&Object> for Object {
7476
linkage_table,
7577
previous_transaction,
7678
storage_rebate,
79+
json,
7780
} = source;
7881

7982
if mask.contains(Self::BCS_FIELD.name) {
@@ -127,6 +130,10 @@ impl MessageMerge<&Object> for Object {
127130
if mask.contains(Self::LINKAGE_TABLE_FIELD.name) {
128131
self.linkage_table = linkage_table.clone();
129132
}
133+
134+
if mask.contains(Self::JSON_FIELD.name) {
135+
self.json = json.clone();
136+
}
130137
}
131138
}
132139

crates/sui-rpc-api/tests/bootstrap.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ fn bootstrap() {
5050
.bytes(["."])
5151
.btree_map([".sui.node.v2alpha.GetProtocolConfigResponse"])
5252
.boxed(".sui.rpc.v2beta.Input.literal")
53+
.boxed("json")
5354
.out_dir(&out_dir)
5455
.compile_fds(fds.clone())
5556
{

0 commit comments

Comments
 (0)