Skip to content

Commit

Permalink
swarm: use Swarm in place of SuiNetwork
Browse files Browse the repository at this point in the history
  • Loading branch information
bmwill committed Jun 2, 2022
1 parent de84768 commit 6c825d9
Show file tree
Hide file tree
Showing 15 changed files with 94 additions and 197 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

6 changes: 2 additions & 4 deletions crates/generate-json-rpc-spec/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,8 @@ async fn main() {

async fn create_response_sample(
) -> Result<(ObjectResponseSample, TransactionResponseSample), anyhow::Error> {
let working_dir = tempfile::tempdir()?;
let working_dir = working_dir.path();
let _network = start_test_network(working_dir, None).await?;
let config = working_dir.join(SUI_WALLET_CONFIG);
let network = start_test_network(None).await?;
let config = network.dir().join(SUI_WALLET_CONFIG);

let mut context = WalletContext::new(&config)?;
let address = context.config.accounts.first().cloned().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-faucet/src/faucet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl From<&SuiObject> for CoinInfo {

#[cfg(test)]
mod tests {
use crate::setup_network_and_wallet;
use test_utils::network::setup_network_and_wallet;

use super::*;

Expand Down
6 changes: 0 additions & 6 deletions crates/sui-faucet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,3 @@ pub use errors::FaucetError;
pub use faucet::*;
pub use requests::*;
pub use responses::*;

#[cfg(test)]
mod test_utils;

#[cfg(test)]
pub use crate::test_utils::*;
30 changes: 0 additions & 30 deletions crates/sui-faucet/src/test_utils.rs

This file was deleted.

17 changes: 17 additions & 0 deletions crates/sui-swarm/src/memory/swarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,23 @@ impl<R: ::rand::RngCore + ::rand::CryptoRng> SwarmBuilder<R> {
fullnodes: HashMap::new(),
}
}

pub fn from_network_config(self, dir: PathBuf, network_config: NetworkConfig) -> Swarm {
let dir = SwarmDirectory::Persistent(dir);

let validators = network_config
.validator_configs()
.iter()
.map(|config| (config.sui_address(), Node::new(config.to_owned())))
.collect();

Swarm {
dir,
network_config,
validators,
fullnodes: HashMap::new(),
}
}
}

/// A handle to an in-memory Sui Network.
Expand Down
1 change: 1 addition & 0 deletions crates/sui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ sui-open-rpc-macros = { path = "../sui-open-rpc-macros" }
sui-json = { path = "../sui-json" }
sui-gateway = { path = "../sui-gateway" }
sui-node = { path = "../sui-node" }
sui-swarm = { path = "../sui-swarm" }

rustyline = "9.1.2"
rustyline-derive = "0.6.0"
Expand Down
1 change: 0 additions & 1 deletion crates/sui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
pub mod benchmark;
pub mod config;
pub mod keystore;
pub mod make;
pub mod shell;
pub mod sui_commands;
pub mod wallet_commands;
52 changes: 0 additions & 52 deletions crates/sui/src/make.rs

This file was deleted.

20 changes: 13 additions & 7 deletions crates/sui/src/sui_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ use sui_config::{
sui_config_dir, Config, PersistedConfig, SUI_FULLNODE_CONFIG, SUI_GATEWAY_CONFIG,
SUI_NETWORK_CONFIG, SUI_WALLET_CONFIG,
};
use sui_swarm::memory::Swarm;
use sui_types::base_types::decode_bytes_hex;
use sui_types::base_types::SuiAddress;
use tracing::info;

pub use crate::make::SuiNetwork;

#[derive(Parser)]
#[clap(rename_all = "kebab-case")]
pub enum SuiCommand {
Expand Down Expand Up @@ -79,11 +78,18 @@ impl SuiCommand {
))
})?;

// Start a sui validator (including its consensus node).
SuiNetwork::start(&network_config)
.await?
.wait_for_completion()
.await
let mut swarm =
Swarm::builder().from_network_config(sui_config_dir()?, network_config);
swarm.launch().await?;

let mut interval = tokio::time::interval(std::time::Duration::from_secs(5));
loop {
for node in swarm.validators_mut() {
node.health_check().await?;
}

interval.tick().await;
}
}
SuiCommand::Network {
config,
Expand Down
23 changes: 9 additions & 14 deletions crates/sui/src/unit_tests/cli_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ async fn test_create_example_nft_command() -> Result<(), anyhow::Error> {
#[traced_test]
#[tokio::test]
async fn test_custom_genesis() -> Result<(), anyhow::Error> {
let working_dir = tempfile::tempdir()?;
// Create and save genesis config file
// Create 4 authorities, 1 account with 1 gas object with custom id

Expand All @@ -233,10 +232,10 @@ async fn test_custom_genesis() -> Result<(), anyhow::Error> {
gas_object_ranges: None,
});

let _network = start_test_network(working_dir.path(), Some(config)).await?;
let network = start_test_network(Some(config)).await?;

// Wallet config
let mut context = WalletContext::new(&working_dir.path().join(SUI_WALLET_CONFIG))?;
let mut context = WalletContext::new(&network.dir().join(SUI_WALLET_CONFIG))?;
assert_eq!(1, context.config.accounts.len());
let address = context.config.accounts.first().cloned().unwrap();

Expand Down Expand Up @@ -268,8 +267,6 @@ async fn test_custom_genesis() -> Result<(), anyhow::Error> {
#[traced_test]
#[tokio::test]
async fn test_custom_genesis_with_custom_move_package() -> Result<(), anyhow::Error> {
let temp_dir = tempfile::tempdir()?;
let working_dir = temp_dir.path();
// Create and save genesis config file
// Create 4 authorities and 1 account
let num_authorities = 4;
Expand All @@ -282,15 +279,15 @@ async fn test_custom_genesis_with_custom_move_package() -> Result<(), anyhow::Er
.push(PathBuf::from(TEST_DATA_DIR).join("custom_genesis_package_2"));

// Start network
let _network = start_test_network(working_dir, Some(config)).await?;
let network = start_test_network(Some(config)).await?;

assert!(logs_contain("Loading 2 Move packages"));
// Checks network config contains package ids
let _network_conf =
PersistedConfig::<NetworkConfig>::read(&working_dir.join(SUI_NETWORK_CONFIG))?;
PersistedConfig::<NetworkConfig>::read(&network.dir().join(SUI_NETWORK_CONFIG))?;

// Create Wallet context.
let wallet_conf_path = working_dir.join(SUI_WALLET_CONFIG);
let wallet_conf_path = network.dir().join(SUI_WALLET_CONFIG);
let mut context = WalletContext::new(&wallet_conf_path)?;

// Make sure init() is executed correctly for custom_genesis_package_2::M1
Expand Down Expand Up @@ -914,11 +911,10 @@ fn test_bug_1078() {
#[traced_test]
#[tokio::test]
async fn test_switch_command() -> Result<(), anyhow::Error> {
let working_dir = tempfile::tempdir()?;
let _network = start_test_network(working_dir.path(), None).await?;
let network = start_test_network(None).await?;

// Create Wallet context.
let wallet_conf = working_dir.path().join(SUI_WALLET_CONFIG);
let wallet_conf = network.dir().join(SUI_WALLET_CONFIG);

let mut context = WalletContext::new(&wallet_conf)?;

Expand Down Expand Up @@ -1012,11 +1008,10 @@ async fn test_switch_command() -> Result<(), anyhow::Error> {
#[traced_test]
#[tokio::test]
async fn test_active_address_command() -> Result<(), anyhow::Error> {
let working_dir = tempfile::tempdir()?;
let _network = start_test_network(working_dir.path(), None).await?;
let network = start_test_network(None).await?;

// Create Wallet context.
let wallet_conf = working_dir.path().join(SUI_WALLET_CONFIG);
let wallet_conf = network.dir().join(SUI_WALLET_CONFIG);

let mut context = WalletContext::new(&wallet_conf)?;

Expand Down
14 changes: 7 additions & 7 deletions crates/sui/src/unit_tests/rpc_server_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use std::{
use sui::{
config::{PersistedConfig, WalletConfig},
keystore::{Keystore, SuiKeystore},
sui_commands::SuiNetwork,
};
use sui_config::{SUI_GATEWAY_CONFIG, SUI_WALLET_CONFIG};
use sui_core::gateway_state::GatewayTxSeqNumber;
Expand All @@ -31,6 +30,7 @@ use sui_gateway::{
rpc_gateway::RpcGatewayImpl,
};
use sui_json::SuiJsonValue;
use sui_swarm::memory::Swarm;
use sui_types::sui_serde::Base64;
use sui_types::{
base_types::{ObjectID, SuiAddress, TransactionDigest},
Expand Down Expand Up @@ -256,14 +256,14 @@ async fn test_get_transaction() -> Result<(), anyhow::Error> {
}

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).await?;
let network = start_test_network(None).await?;
let (server_addr, rpc_server_handle) =
start_rpc_gateway(&working_dir.join(SUI_GATEWAY_CONFIG)).await?;
let wallet_conf: WalletConfig = PersistedConfig::read(&working_dir.join(SUI_WALLET_CONFIG))?;
start_rpc_gateway(&network.dir().join(SUI_GATEWAY_CONFIG)).await?;
let wallet_conf: WalletConfig = PersistedConfig::read(&network.dir().join(SUI_WALLET_CONFIG))?;
let http_client = HttpClientBuilder::default().build(format!("http://{}", server_addr))?;
let working_dir = network.dir().into();
Ok(TestNetwork {
_network,
_network: network,
_rpc_server: rpc_server_handle,
accounts: wallet_conf.accounts,
http_client,
Expand All @@ -272,7 +272,7 @@ async fn setup_test_network() -> Result<TestNetwork, anyhow::Error> {
}

struct TestNetwork {
_network: SuiNetwork,
_network: Swarm,
_rpc_server: HttpServerHandle,
accounts: Vec<SuiAddress>,
http_client: HttpClient,
Expand Down
Loading

0 comments on commit 6c825d9

Please sign in to comment.