Skip to content

Commit

Permalink
sui: when running 'sui genesis' save genesis to file
Browse files Browse the repository at this point in the history
  • Loading branch information
bmwill committed May 31, 2022
1 parent 663aa3e commit 6807726
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
1 change: 1 addition & 0 deletions crates/sui-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub const SUI_NETWORK_CONFIG: &str = "network.yaml";
pub const SUI_FULLNODE_CONFIG: &str = "fullnode.yaml";
pub const SUI_WALLET_CONFIG: &str = "wallet.yaml";
pub const SUI_GATEWAY_CONFIG: &str = "gateway.yaml";
pub const SUI_GENESIS_FILENAME: &str = "genesis.blob";
pub const SUI_DEV_NET_URL: &str = "https://gateway.devnet.sui.io:443";

pub const AUTHORITIES_DB_NAME: &str = "authorities_db";
Expand Down
5 changes: 5 additions & 0 deletions crates/sui-config/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use sui_types::crypto::{KeyPair, PublicKeyBytes};
#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")]
pub struct NodeConfig {
#[serde(default = "default_key_pair")]
pub key_pair: KeyPair,
pub db_path: PathBuf,
#[serde(default = "default_grpc_address")]
Expand All @@ -33,6 +34,10 @@ pub struct NodeConfig {
pub genesis: Genesis,
}

fn default_key_pair() -> KeyPair {
sui_types::crypto::get_key_pair().1
}

fn default_grpc_address() -> Multiaddr {
use multiaddr::multiaddr;
multiaddr!(Ip4([0, 0, 0, 0]), Tcp(8080u16))
Expand Down
29 changes: 14 additions & 15 deletions crates/sui/src/sui_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use clap::*;
use std::fs;
use std::num::NonZeroUsize;
use std::path::PathBuf;
use sui_config::genesis_config::GenesisConfig;
use sui_config::{builder::ConfigBuilder, NetworkConfig};
use sui_config::{genesis_config::GenesisConfig, SUI_GENESIS_FILENAME};
use sui_config::{
sui_config_dir, Config, PersistedConfig, SUI_FULLNODE_CONFIG, SUI_GATEWAY_CONFIG,
SUI_NETWORK_CONFIG, SUI_WALLET_CONFIG,
Expand Down Expand Up @@ -162,6 +162,7 @@ impl SuiCommand {
}

let network_path = sui_config_dir.join(SUI_NETWORK_CONFIG);
let genesis_path = sui_config_dir.join(SUI_GENESIS_FILENAME);
let wallet_path = sui_config_dir.join(SUI_WALLET_CONFIG);
let gateway_path = sui_config_dir.join(SUI_GATEWAY_CONFIG);
let keystore_path = sui_config_dir.join("wallet.key");
Expand All @@ -180,7 +181,7 @@ impl SuiCommand {
}

let validator_info = genesis_conf.validator_genesis_info.take();
let network_config = if let Some(validators) = validator_info {
let mut network_config = if let Some(validators) = validator_info {
ConfigBuilder::new(sui_config_dir)
.initial_accounts_config(genesis_conf)
.build_with_validators(validators)
Expand All @@ -200,9 +201,13 @@ impl SuiCommand {
keystore.add_key(address, key.copy())?;
}

network_config.genesis.save(&genesis_path)?;
for validator in &mut network_config.validator_configs {
validator.genesis = sui_config::node::Genesis::new_from_file(&genesis_path);
}

info!("Network genesis completed.");
let network_config = network_config.persisted(&network_path);
network_config.save()?;
network_config.save(&network_path)?;
info!("Network config file is stored in {:?}.", network_path);

keystore.set_path(&keystore_path);
Expand All @@ -212,17 +217,14 @@ impl SuiCommand {
// Use the first address if any
let active_address = accounts.get(0).copied();

let validator_set = network_config.validator_configs()[0]
.committee_config()
.validator_set();
let validator_set = network_config.validator_set();

GatewayConfig {
db_folder_path: gateway_db_folder_path,
validator_set: validator_set.to_owned(),
..Default::default()
}
.persisted(&gateway_path)
.save()?;
.save(&gateway_path)?;
info!("Gateway config file is stored in {:?}.", gateway_path);

let wallet_gateway_config = GatewayConfig {
Expand All @@ -238,8 +240,7 @@ impl SuiCommand {
active_address,
};

let wallet_config = wallet_config.persisted(&wallet_path);
wallet_config.save()?;
wallet_config.save(&wallet_path)?;
info!("Wallet config file is stored in {:?}.", wallet_path);

let fullnode_config = network_config
Expand All @@ -248,14 +249,12 @@ impl SuiCommand {
fullnode_config.save()?;

for (i, validator) in network_config
.into_inner()
.into_validator_configs()
.into_iter()
.enumerate()
{
let validator_config = validator
.persisted(&sui_config_dir.join(format!("validator-config-{}.yaml", i)));
validator_config.save()?;
let path = sui_config_dir.join(format!("validator-config-{}.yaml", i));
validator.save(path)?;
}

Ok(())
Expand Down
5 changes: 3 additions & 2 deletions crates/sui/src/unit_tests/cli_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use sui::{
use sui_config::genesis_config::{AccountConfig, GenesisConfig, ObjectConfig};
use sui_config::{
Config, NetworkConfig, PersistedConfig, SUI_FULLNODE_CONFIG, SUI_GATEWAY_CONFIG,
SUI_NETWORK_CONFIG, SUI_WALLET_CONFIG,
SUI_GENESIS_FILENAME, SUI_NETWORK_CONFIG, SUI_WALLET_CONFIG,
};
use sui_core::gateway_types::{GetObjectDataResponse, SuiObject, SuiTransactionEffects};
use sui_json::SuiJsonValue;
Expand Down Expand Up @@ -75,11 +75,12 @@ async fn test_genesis() -> Result<(), anyhow::Error> {
.flat_map(|r| r.map(|file| file.file_name().to_str().unwrap().to_owned()))
.collect::<Vec<_>>();

assert_eq!(9, files.len());
assert_eq!(10, files.len());
assert!(files.contains(&SUI_WALLET_CONFIG.to_string()));
assert!(files.contains(&SUI_GATEWAY_CONFIG.to_string()));
assert!(files.contains(&SUI_NETWORK_CONFIG.to_string()));
assert!(files.contains(&SUI_FULLNODE_CONFIG.to_string()));
assert!(files.contains(&SUI_GENESIS_FILENAME.to_string()));

assert!(files.contains(&"wallet.key".to_string()));

Expand Down

0 comments on commit 6807726

Please sign in to comment.