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

rest: add support for protobuf to remaining endpoints that support a binary content type #20096

Merged
merged 6 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
65 changes: 65 additions & 0 deletions crates/sui-e2e-tests/tests/rest/committee.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use prost::Message;
use sui_macros::sim_test;
use sui_rest_api::client::sdk::Client;
use sui_sdk_types::types::ValidatorCommittee;
use test_cluster::TestClusterBuilder;

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

let client = Client::new(test_cluster.rpc_url()).unwrap();

let _committee = client.get_committee(0).await.unwrap();
let _committee = client.get_current_committee().await.unwrap();

async fn raw_request(url: &str) {
let client = reqwest::Client::new();

// Make sure list works with json
let _object = client
.get(url)
.header(reqwest::header::ACCEPT, sui_rest_api::APPLICATION_JSON)
.send()
.await
.unwrap()
.json::<ValidatorCommittee>()
.await
.unwrap();

// Make sure it works with protobuf
let bytes = client
.get(url)
.header(reqwest::header::ACCEPT, sui_rest_api::APPLICATION_PROTOBUF)
.send()
.await
.unwrap()
.bytes()
.await
.unwrap();
let _object = sui_rest_api::proto::ValidatorCommittee::decode(bytes).unwrap();

// TODO remove this once the BCS format is no longer accepted and clients have migrated to the
// protobuf version
let bytes = client
.get(url)
.header(reqwest::header::ACCEPT, sui_rest_api::APPLICATION_BCS)
.send()
.await
.unwrap()
.bytes()
.await
.unwrap();
let _object = bcs::from_bytes::<ValidatorCommittee>(&bytes).unwrap();
}

let url = format!("{}/v2/system/committee", test_cluster.rpc_url(),);

raw_request(&url).await;

let url = format!("{}/v2/system/committee/0", test_cluster.rpc_url());
raw_request(&url).await;
}
3 changes: 3 additions & 0 deletions crates/sui-e2e-tests/tests/rest/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
// SPDX-License-Identifier: Apache-2.0

mod checkpoints;
mod committee;
mod execute;
mod objects;
mod resolve;
mod transactions;

async fn transfer_coin(
context: &sui_sdk::wallet_context::WalletContext,
Expand Down
79 changes: 79 additions & 0 deletions crates/sui-e2e-tests/tests/rest/objects.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use prost::Message;
use sui_macros::sim_test;
use sui_rest_api::client::sdk::Client;
use sui_rest_api::client::Client as CoreClient;
use sui_rest_api::ObjectResponse;
use sui_sdk_types::types::Object;
use test_cluster::TestClusterBuilder;

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

let client = Client::new(test_cluster.rpc_url()).unwrap();
let core_client = CoreClient::new(test_cluster.rpc_url());

let _object = client.get_object("0x5".parse().unwrap()).await.unwrap();
let _object = core_client
.get_object("0x5".parse().unwrap())
.await
.unwrap();

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

async fn raw_request(url: &str) {
let client = reqwest::Client::new();

// Make sure list works with json
let _object = client
.get(url)
.header(reqwest::header::ACCEPT, sui_rest_api::APPLICATION_JSON)
.send()
.await
.unwrap()
.json::<ObjectResponse>()
.await
.unwrap();

// Make sure it works with protobuf
let bytes = client
.get(url)
.header(reqwest::header::ACCEPT, sui_rest_api::APPLICATION_PROTOBUF)
.send()
.await
.unwrap()
.bytes()
.await
.unwrap();
let _object = sui_rest_api::proto::GetObjectResponse::decode(bytes).unwrap();

// TODO remove this once the BCS format is no longer accepted and clients have migrated to the
// protobuf version
let bytes = client
.get(url)
.header(reqwest::header::ACCEPT, sui_rest_api::APPLICATION_BCS)
.send()
.await
.unwrap()
.bytes()
.await
.unwrap();
let _object = bcs::from_bytes::<Object>(&bytes).unwrap();
}

let url = format!("{}/v2/objects/0x5", test_cluster.rpc_url());
raw_request(&url).await;

let url = format!("{}/v2/objects/0x5/version/1", test_cluster.rpc_url());
raw_request(&url).await;
}
118 changes: 118 additions & 0 deletions crates/sui-e2e-tests/tests/rest/transactions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use prost::Message;
use sui_macros::sim_test;
use sui_rest_api::client::sdk::Client;
use sui_rest_api::transactions::{ListTransactionsQueryParameters, TransactionResponse};
use test_cluster::TestClusterBuilder;

use crate::transfer_coin;

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

let transaction_digest = transfer_coin(&test_cluster.wallet).await;

let client = Client::new(test_cluster.rpc_url()).unwrap();

let _transaction = client.get_transaction(&transaction_digest).await.unwrap();

let client = reqwest::Client::new();
let url = format!(
"{}/v2/transactions/{}",
test_cluster.rpc_url(),
transaction_digest,
);
// Make sure it works with json
let _transaction = client
.get(&url)
.header(reqwest::header::ACCEPT, sui_rest_api::APPLICATION_JSON)
.send()
.await
.unwrap()
.json::<TransactionResponse>()
.await
.unwrap();

// Make sure it works with protobuf
let bytes = client
.get(&url)
.header(reqwest::header::ACCEPT, sui_rest_api::APPLICATION_PROTOBUF)
.send()
.await
.unwrap()
.bytes()
.await
.unwrap();
let _transaction = sui_rest_api::proto::GetTransactionResponse::decode(bytes).unwrap();

// TODO remove this once the BCS format is no longer accepted and clients have migrated to the
// protobuf version
let bytes = client
.get(&url)
.header(reqwest::header::ACCEPT, sui_rest_api::APPLICATION_BCS)
.send()
.await
.unwrap()
.bytes()
.await
.unwrap();
let _transaction = bcs::from_bytes::<TransactionResponse>(&bytes).unwrap();
}

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

let _transaction_digest = transfer_coin(&test_cluster.wallet).await;

let client = Client::new(test_cluster.rpc_url()).unwrap();

let transactions = client
.list_transactions(&ListTransactionsQueryParameters::default())
.await
.unwrap()
.into_inner();

assert!(!transactions.is_empty());

let client = reqwest::Client::new();
let url = format!("{}/v2/transactions", test_cluster.rpc_url());
// Make sure it works with json
let _transactions = client
.get(&url)
.header(reqwest::header::ACCEPT, sui_rest_api::APPLICATION_JSON)
.send()
.await
.unwrap()
.json::<Vec<TransactionResponse>>()
.await
.unwrap();

// Make sure it works with protobuf
let bytes = client
.get(&url)
.header(reqwest::header::ACCEPT, sui_rest_api::APPLICATION_PROTOBUF)
.send()
.await
.unwrap()
.bytes()
.await
.unwrap();
let _transactions = sui_rest_api::proto::ListTransactionsResponse::decode(bytes).unwrap();

// TODO remove this once the BCS format is no longer accepted and clients have migrated to the
// protobuf version
let bytes = client
.get(&url)
.header(reqwest::header::ACCEPT, sui_rest_api::APPLICATION_BCS)
.send()
.await
.unwrap()
.bytes()
.await
.unwrap();
let _transactions = bcs::from_bytes::<Vec<TransactionResponse>>(&bytes).unwrap();
}
Loading
Loading