Skip to content

Commit

Permalink
Use SuiClient instead of RPCGatewayClient in Sui CLI (MystenLabs#3373)
Browse files Browse the repository at this point in the history
* Refactor GatewayClient and SuiClient
Enhance Sui SDK apis

* update import and SDK docs

* revert rename to reduce PR size

* more gateway deps clean up

* fixup after rebase
  • Loading branch information
patrickkuo authored Jul 31, 2022
1 parent 0938aa9 commit 4a1f5ef
Show file tree
Hide file tree
Showing 35 changed files with 563 additions and 697 deletions.
7 changes: 2 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/sui-benchmark/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ bcs = "0.1.3"
sui-core = { path = "../sui-core" }
sui-config = { path = "../sui-config" }
sui-types = { path = "../sui-types" }
sui-gateway = { path = "../sui-gateway" }
sui-sdk = { path = "../sui-sdk" }

move-core-types = { git = "https://github.com/move-language/move", rev = "79071528524f08b12e9abb84c1094d8e976aa17a", features = ["address20"] }
Expand Down
11 changes: 6 additions & 5 deletions crates/sui-benchmark/src/bin/stress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ use sui_benchmark::workloads::transfer_object::TransferObjectWorkload;
use sui_benchmark::workloads::workload::get_latest;
use sui_benchmark::workloads::workload::Payload;
use sui_benchmark::workloads::workload::Workload;
use sui_config::gateway::GatewayConfig;
use sui_config::Config;
use sui_config::PersistedConfig;
use sui_core::authority_aggregator::AuthAggMetrics;
use sui_core::authority_aggregator::AuthorityAggregator;
use sui_core::authority_client::NetworkAuthorityClient;
use sui_gateway::config::GatewayConfig;
use sui_core::gateway_state::GatewayState;
use sui_node::SuiNode;
use sui_quorum_driver::QuorumDriverHandler;
use sui_sdk::crypto::SuiKeystore;
Expand Down Expand Up @@ -458,8 +459,8 @@ async fn main() -> Result<()> {
))
})?;
let config: GatewayConfig = PersistedConfig::read(&config_path)?;
let committee = config.make_committee()?;
let authority_clients = config.make_authority_clients();
let committee = GatewayState::make_committee(&config)?;
let authority_clients = GatewayState::make_authority_clients(&config);
let metrics = AuthAggMetrics::new(&prometheus::Registry::new());
let aggregator = AuthorityAggregator::new(committee, authority_clients, metrics);
let primary_gas_id = ObjectID::from_hex_literal(&opts.primary_gas_id)?;
Expand Down Expand Up @@ -509,8 +510,8 @@ async fn main() -> Result<()> {
.unwrap();
let handle: JoinHandle<()> = std::thread::spawn(move || {
client_runtime.block_on(async move {
let committee = gateway_config.make_committee().unwrap();
let authority_clients = gateway_config.make_authority_clients();
let committee = GatewayState::make_committee(&gateway_config).unwrap();
let authority_clients = GatewayState::make_authority_clients(&gateway_config);
let metrics = AuthAggMetrics::new(&prometheus::Registry::new());
let aggregator = AuthorityAggregator::new(committee, authority_clients, metrics);
let mut workload = make_workload(primary_gas_id, owner, keypair, &opts);
Expand Down
1 change: 0 additions & 1 deletion crates/sui-cluster-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ sui-swarm = { path = "../sui-swarm" }
sui = { path = "../sui" }
sui-json-rpc-types= { path = "../sui-json-rpc-types" }
sui-sdk = { path = "../sui-sdk" }
sui-gateway = { path = "../sui-gateway" }
sui-types = { path = "../sui-types" }
sui-core = { path = "../sui-core" }
sui-json = { path = "../sui-json" }
Expand Down
11 changes: 4 additions & 7 deletions crates/sui-cluster-test/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: Apache-2.0

use anyhow::bail;
use sui_core::gateway_state::GatewayClient;
use sui_json_rpc_types::{GetObjectDataResponse, SuiEvent, SuiObject, SuiParsedMoveObject};
use sui_types::gas_coin::GasCoin;
use sui_types::{
Expand All @@ -11,6 +10,7 @@ use sui_types::{
object::Owner,
};

use sui_sdk::SuiClient;
use tracing::debug;

/// A util struct that helps verify Sui Object.
Expand Down Expand Up @@ -54,7 +54,7 @@ impl ObjectChecker {
self
}

pub async fn check_into_gas_coin(self, client: &GatewayClient) -> GasCoin {
pub async fn check_into_gas_coin(self, client: &SuiClient) -> GasCoin {
if self.is_sui_coin == Some(false) {
panic!("'check_into_gas_coin' shouldn't be called with 'is_sui_coin' set as false");
}
Expand All @@ -65,14 +65,11 @@ impl ObjectChecker {
.into_gas_coin()
}

pub async fn check_into_sui_object(
self,
client: &GatewayClient,
) -> SuiObject<SuiParsedMoveObject> {
pub async fn check_into_sui_object(self, client: &SuiClient) -> SuiObject<SuiParsedMoveObject> {
self.check(client).await.unwrap().into_sui_object()
}

pub async fn check(self, client: &GatewayClient) -> Result<CheckerResultObject, anyhow::Error> {
pub async fn check(self, client: &SuiClient) -> Result<CheckerResultObject, anyhow::Error> {
debug!(?self);

let object_id = self.object_id;
Expand Down
9 changes: 5 additions & 4 deletions crates/sui-cluster-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ use cluster::{Cluster, ClusterFactory};
use config::ClusterTestOpt;
use std::sync::Arc;
use sui::client_commands::WalletContext;
use sui_core::gateway_state::GatewayClient;

use sui_json_rpc_types::TransactionResponse;
use sui_sdk::SuiClient;
use sui_types::gas_coin::GasCoin;
use sui_types::{
base_types::SuiAddress,
Expand Down Expand Up @@ -52,11 +53,11 @@ impl TestContext {
&self.client
}

fn get_gateway(&self) -> &GatewayClient {
fn get_gateway(&self) -> &SuiClient {
self.client.get_gateway()
}

fn get_fullnode(&self) -> &GatewayClient {
fn get_fullnode(&self) -> &SuiClient {
self.client.get_fullnode()
}

Expand All @@ -83,7 +84,7 @@ impl TestContext {
pub async fn setup(options: ClusterTestOpt) -> Result<Self, anyhow::Error> {
let cluster = ClusterFactory::start(&options).await?;
let faucet_url = cluster.faucet_url().map(String::from);
let wallet_client = WalletClient::new_from_cluster(&cluster);
let wallet_client = WalletClient::new_from_cluster(&cluster).await;
Ok(Self {
cluster,
client: wallet_client,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl TestCaseImpl for NativeTransferTest {
let (recipient_addr, _): (_, AccountKeyPair) = get_key_pair();
let data = ctx
.get_gateway()
.public_transfer_object(
.transfer_object(
signer,
*obj_to_transfer.id(),
Some(*gas_obj.id()),
Expand Down
32 changes: 16 additions & 16 deletions crates/sui-cluster-test/src/wallet_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
// SPDX-License-Identifier: Apache-2.0

use super::Cluster;
use std::sync::Arc;
use sui::client_commands::WalletContext;
use sui::config::{Config, GatewayType, SuiClientConfig};
use sui::config::{Config, SuiClientConfig};
use sui_config::SUI_KEYSTORE_FILENAME;
use sui_core::gateway_state::GatewayClient;
use sui_gateway::rpc_gateway_client::RpcGatewayClient;
use sui_sdk::crypto::KeystoreType;
use sui_sdk::{ClientType, SuiClient};
use sui_types::base_types::SuiAddress;
use sui_types::crypto::{KeypairTraits, Signature};
use sui_types::messages::TransactionData;
Expand All @@ -17,12 +15,12 @@ use tracing::info;
pub struct WalletClient {
wallet_context: WalletContext,
address: SuiAddress,
fullnode_client: GatewayClient,
fullnode_client: SuiClient,
}

#[allow(clippy::borrowed_box)]
impl WalletClient {
pub fn new_from_cluster(cluster: &Box<dyn Cluster + Sync + Send>) -> Self {
pub async fn new_from_cluster(cluster: &Box<dyn Cluster + Sync + Send>) -> Self {
let temp_dir = tempfile::tempdir().unwrap();
let wallet_config_path = temp_dir.path().join("client.yaml");
let rpc_url = cluster.rpc_url();
Expand All @@ -35,7 +33,7 @@ impl WalletClient {
SuiClientConfig {
accounts: vec![address],
keystore,
gateway: GatewayType::RPC(rpc_url.into()),
gateway: ClientType::RPC(rpc_url.into()),
active_address: Some(address),
}
.persisted(&wallet_config_path)
Expand All @@ -47,16 +45,18 @@ impl WalletClient {
wallet_config_path
);

let wallet_context = WalletContext::new(&wallet_config_path).unwrap_or_else(|e| {
panic!(
"Failed to init wallet context from path {:?}, error: {e}",
wallet_config_path
)
});
let wallet_context = WalletContext::new(&wallet_config_path)
.await
.unwrap_or_else(|e| {
panic!(
"Failed to init wallet context from path {:?}, error: {e}",
wallet_config_path
)
});

let fullnode_url = String::from(cluster.fullnode_url());
info!("Use fullnode: {}", &fullnode_url);
let fullnode_client: GatewayClient = Arc::new(RpcGatewayClient::new(fullnode_url).unwrap());
let fullnode_client = SuiClient::new_http_client(&fullnode_url).unwrap();

Self {
wallet_context,
Expand All @@ -77,11 +77,11 @@ impl WalletClient {
self.address
}

pub fn get_gateway(&self) -> &GatewayClient {
pub fn get_gateway(&self) -> &SuiClient {
&self.wallet_context.gateway
}

pub fn get_fullnode(&self) -> &GatewayClient {
pub fn get_fullnode(&self) -> &SuiClient {
&self.fullnode_client
}

Expand Down
34 changes: 34 additions & 0 deletions crates/sui-config/src/gateway.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use crate::{Config, ValidatorInfo};
use serde::Deserialize;
use serde::Serialize;
use std::path::PathBuf;
use std::time::Duration;
use sui_types::committee::EpochId;

#[derive(Serialize, Deserialize)]
pub struct GatewayConfig {
pub epoch: EpochId,
pub validator_set: Vec<ValidatorInfo>,
pub send_timeout: Duration,
pub recv_timeout: Duration,
pub buffer_size: usize,
pub db_folder_path: PathBuf,
}

impl Config for GatewayConfig {}

impl Default for GatewayConfig {
fn default() -> Self {
Self {
epoch: 0,
validator_set: vec![],
send_timeout: Duration::from_micros(4000000),
recv_timeout: Duration::from_micros(4000000),
buffer_size: 650000,
db_folder_path: Default::default(),
}
}
}
1 change: 1 addition & 0 deletions crates/sui-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use sui_types::committee::StakeUnit;
use tracing::trace;

pub mod builder;
pub mod gateway;
pub mod genesis;
pub mod genesis_config;
pub mod node;
Expand Down
51 changes: 48 additions & 3 deletions crates/sui-core/src/gateway_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// SPDX-License-Identifier: Apache-2.0

use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::path::PathBuf;
use std::path::Path;
use std::sync::atomic::AtomicU64;
use std::sync::Arc;
use std::time::Duration;
Expand All @@ -23,6 +23,8 @@ use sui_types::SUI_SYSTEM_STATE_OBJECT_ID;
use tracing::{debug, error, Instrument};

use sui_adapter::adapter::resolve_and_type_check;
use sui_config::gateway::GatewayConfig;
use sui_config::ValidatorInfo;
use sui_types::gas_coin::GasCoin;
use sui_types::object::{Data, ObjectFormatOptions, Owner};
use sui_types::{
Expand All @@ -38,6 +40,7 @@ use sui_types::{

use crate::authority::ResolverWrapper;
use crate::authority_aggregator::AuthAggMetrics;
use crate::authority_client::NetworkAuthorityClient;
use crate::transaction_input_checker;
use crate::{
authority::GatewayStore, authority_aggregator::AuthorityAggregator,
Expand Down Expand Up @@ -171,7 +174,7 @@ pub struct GatewayState<A> {
impl<A> GatewayState<A> {
/// Create a new manager which stores its managed addresses at `path`
pub fn new(
path: PathBuf,
path: &Path,
committee: Committee,
authority_clients: BTreeMap<AuthorityName, A>,
prometheus_registry: &Registry,
Expand All @@ -186,7 +189,7 @@ impl<A> GatewayState<A> {
}

pub fn new_with_authorities(
path: PathBuf,
path: &Path,
authorities: AuthorityAggregator<A>,
metrics: GatewayMetrics,
) -> SuiResult<Self> {
Expand Down Expand Up @@ -223,6 +226,48 @@ impl<A> GatewayState<A> {
}
}

impl GatewayState<NetworkAuthorityClient> {
pub fn create_client(
config: &GatewayConfig,
prometheus_registry: Option<&Registry>,
) -> Result<GatewayClient, anyhow::Error> {
let committee = Self::make_committee(config)?;
let authority_clients = Self::make_authority_clients(config);
let default_registry = Registry::new();
let prometheus_registry = prometheus_registry.unwrap_or(&default_registry);
Ok(Arc::new(GatewayState::new(
&config.db_folder_path,
committee,
authority_clients,
prometheus_registry,
)?))
}

pub fn make_committee(config: &GatewayConfig) -> SuiResult<Committee> {
Committee::new(
config.epoch,
ValidatorInfo::voting_rights(&config.validator_set),
)
}

pub fn make_authority_clients(
config: &GatewayConfig,
) -> BTreeMap<AuthorityName, NetworkAuthorityClient> {
let mut authority_clients = BTreeMap::new();
let mut network_config = mysten_network::config::Config::new();
network_config.connect_timeout = Some(config.send_timeout);
network_config.request_timeout = Some(config.recv_timeout);
for authority in &config.validator_set {
let channel = network_config
.connect_lazy(authority.network_address())
.unwrap();
let client = NetworkAuthorityClient::new(channel);
authority_clients.insert(authority.public_key(), client);
}
authority_clients
}
}

// Operations are considered successful when they successfully reach a quorum of authorities.
#[async_trait]
pub trait GatewayAPI {
Expand Down
Loading

0 comments on commit 4a1f5ef

Please sign in to comment.