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 gateway] - Add test for each of the RPC endpoint #1732

Merged
merged 1 commit into from
May 4, 2022
Merged
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
109 changes: 101 additions & 8 deletions sui/src/unit_tests/rpc_server_tests.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use crate::rpc_server_tests::sui_network::start_test_network;
use jsonrpsee::{
http_client::{HttpClient, HttpClientBuilder},
http_server::{HttpServerBuilder, HttpServerHandle},
};
use move_core_types::identifier::Identifier;
use std::{
net::SocketAddr,
path::{Path, PathBuf},
str::FromStr,
};

use jsonrpsee::{
http_client::{HttpClient, HttpClientBuilder},
http_server::{HttpServerBuilder, HttpServerHandle},
};
use move_core_types::identifier::Identifier;

use sui::{
config::{PersistedConfig, WalletConfig, SUI_GATEWAY_CONFIG, SUI_WALLET_CONFIG},
keystore::{Keystore, SuiKeystore},
Expand All @@ -21,15 +22,19 @@ use sui::{
},
sui_commands::SuiNetwork,
};
use sui_core::gateway_state::gateway_responses::TransactionResponse;
use sui_core::gateway_state::gateway_responses::{TransactionEffectsResponse, TransactionResponse};
use sui_core::gateway_state::GatewayTxSeqNumber;
use sui_core::sui_json::SuiJsonValue;
use sui_framework::build_move_package_to_bytes;
use sui_types::{
base_types::{ObjectID, SuiAddress},
base_types::{ObjectID, SuiAddress, TransactionDigest},
json_schema::Base64,
object::ObjectRead,
SUI_FRAMEWORK_ADDRESS,
};

use crate::rpc_server_tests::sui_network::start_test_network;

mod sui_network;

#[tokio::test]
Expand Down Expand Up @@ -174,6 +179,94 @@ async fn test_move_call() -> Result<(), anyhow::Error> {
Ok(())
}

#[tokio::test]
async fn test_get_object_info() -> Result<(), anyhow::Error> {
let test_network = setup_test_network().await?;
let http_client = test_network.http_client;
let address = test_network.accounts.first().unwrap();
http_client.sync_account_state(*address).await?;
let result: ObjectResponse = http_client.get_owned_objects(*address).await?;
let result = result
.objects
.into_iter()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, this could probably be an iter here and below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh... to_object_ref ' s signature is pub fn to_object_ref(self) -> Result<ObjectRef, anyhow::Error>, so this need to be into_iter

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair enough!

.map(|o| o.to_object_ref())
.collect::<Result<Vec<_>, _>>()?;

for (id, _, _) in result {
let result: ObjectRead = http_client.get_object_info(id).await?;
assert!(
matches!(result, ObjectRead::Exists((obj_id,_,_), object, _) if id == obj_id && &object.owner.get_owner_address()? == address)
);
}
Ok(())
}

#[tokio::test]
async fn test_get_transaction() -> Result<(), anyhow::Error> {
let test_network = setup_test_network().await?;
let http_client = test_network.http_client;
let address = test_network.accounts.first().unwrap();

http_client.sync_account_state(*address).await?;

let result: ObjectResponse = http_client.get_owned_objects(*address).await?;
let objects = result
.objects
.into_iter()
.map(|o| o.to_object_ref())
.collect::<Result<Vec<_>, _>>()?;

let (gas_id, _, _) = objects.last().unwrap();

// Make some transactions
let mut tx_responses = Vec::new();
for (id, _, _) in &objects[..objects.len() - 1] {
let tx_data: TransactionBytes = http_client
.transfer_coin(*address, *id, *gas_id, 1000, *address)
.await?;

let keystore = SuiKeystore::load_or_create(&test_network.working_dir.join("wallet.key"))?;
let signature = keystore.sign(address, &tx_data.tx_bytes)?;

let response: TransactionResponse = http_client
.execute_transaction(SignedTransaction::new(tx_data.tx_bytes, signature))
.await?;

if let TransactionResponse::EffectResponse(effects) = response {
tx_responses.push(effects);
}
}
// test get_transactions_in_range
let tx: Vec<(GatewayTxSeqNumber, TransactionDigest)> =
http_client.get_transactions_in_range(0, 10).await?;
assert_eq!(4, tx.len());

// test get_transactions_in_range with smaller range
let tx: Vec<(GatewayTxSeqNumber, TransactionDigest)> =
http_client.get_transactions_in_range(1, 3).await?;
assert_eq!(2, tx.len());

// test get_recent_transactions with smaller range
let tx: Vec<(GatewayTxSeqNumber, TransactionDigest)> =
http_client.get_recent_transactions(3).await?;
assert_eq!(3, tx.len());

// test get_recent_transactions
let tx: Vec<(GatewayTxSeqNumber, TransactionDigest)> =
http_client.get_recent_transactions(10).await?;
assert_eq!(4, tx.len());

// test get_transaction
for (_, tx_digest) in tx {
let response: TransactionEffectsResponse = http_client.get_transaction(tx_digest).await?;
assert!(tx_responses.iter().any(
|effects| effects.effects.transaction_digest == response.effects.transaction_digest
))
}

Ok(())
}

async fn setup_test_network() -> Result<TestNetwork, anyhow::Error> {
let working_dir = tempfile::tempdir()?.path().to_path_buf();
let _network = start_test_network(&working_dir, None, None).await?;
Expand Down