Skip to content

Commit

Permalink
[easy/ rust sdk] Adding comparator on SuiObjectResponse (MystenLabs#9928
Browse files Browse the repository at this point in the history
)

## Description 

Adding ordering impl on SuiObjectResposne
## Test Plan 

CI
---
If your changes are not user-facing and not a breaking change, you can
skip the following section. Otherwise, please indicate what changed, and
then add to the Release Notes section as highlighted during the release
process.

### Type of Change (Check all that apply)

- [ ] user-visible impact
- [ ] breaking change for a client SDKs
- [ ] breaking change for FNs (FN binary must upgrade)
- [ ] breaking change for validators or node operators (must upgrade
binaries)
- [ ] breaking change for on-chain data layout
- [ ] necessitate either a data wipe or data migration

### Release notes
  • Loading branch information
healthydeve authored Mar 28, 2023
1 parent d505085 commit 0fdbdea
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
27 changes: 27 additions & 0 deletions crates/sui-json-rpc-types/src/sui_object.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use std::cmp::Ordering;
use std::collections::BTreeMap;
use std::fmt;
use std::fmt::Write;
Expand Down Expand Up @@ -61,6 +62,32 @@ impl SuiObjectResponse {
}
}

impl Ord for SuiObjectResponse {
fn cmp(&self, other: &Self) -> Ordering {
match (&self.data, &other.data) {
(Some(data), Some(data_2)) => {
if data.object_id.cmp(&data_2.object_id).eq(&Ordering::Greater) {
return Ordering::Greater;
} else if data.object_id.cmp(&data_2.object_id).eq(&Ordering::Less) {
return Ordering::Less;
}
Ordering::Equal
}
// In this ordering those with data will come before SuiObjectResponses that are errors.
(Some(_), None) => Ordering::Less,
(None, Some(_)) => Ordering::Greater,
// SuiObjectResponses that are errors are just considered equal.
_ => Ordering::Equal,
}
}
}

impl PartialOrd for SuiObjectResponse {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl SuiObjectResponse {
pub fn move_object_bcs(&self) -> Option<&Vec<u8>> {
if let Some(data) = &self.data {
Expand Down
9 changes: 4 additions & 5 deletions crates/sui/src/unit_tests/cli_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1258,15 +1258,15 @@ async fn test_switch_command() -> Result<(), anyhow::Error> {
.execute(context)
.await?;

let cmd_objs = if let SuiClientCommandResult::Objects(v) = os {
let mut cmd_objs = if let SuiClientCommandResult::Objects(v) = os {
v
} else {
panic!("Command failed")
};

// Check that we indeed fetched for addr1
let client = context.get_client().await?;
let actual_objs = client
let mut actual_objs = client
.read_api()
.get_owned_objects(
addr1,
Expand All @@ -1279,9 +1279,8 @@ async fn test_switch_command() -> Result<(), anyhow::Error> {
.await
.unwrap()
.data;
// TODO (jian): impl Ord on SuiObjectResponse
// cmd_objs.sort();
// actual_objs.sort();
cmd_objs.sort();
actual_objs.sort();
assert_eq!(cmd_objs, actual_objs);

// Switch the address
Expand Down
6 changes: 4 additions & 2 deletions crates/test-utils/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,10 @@ pub async fn make_transactions_with_wallet_context(
recipient,
*address,
Some(2),
// TODO (jian)
obj.clone().into_object().expect("REASON").object_ref(),
obj.clone()
.into_object()
.expect("Gas coin could not be converted to object ref.")
.object_ref(),
MAX_GAS,
);
let tx = to_sender_signed_transaction(
Expand Down

0 comments on commit 0fdbdea

Please sign in to comment.