Skip to content

Commit

Permalink
sui: remove genesis function
Browse files Browse the repository at this point in the history
Remove the genesis free function and instead completely rely on
ConfigBuilder + GenesisBuilder for performing genesis.
  • Loading branch information
bmwill committed May 16, 2022
1 parent 084786c commit 041a16f
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 188 deletions.
4 changes: 3 additions & 1 deletion crates/sui-config/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ impl<R: ::rand::RngCore + ::rand::CryptoRng> ConfigBuilder<R> {
let initial_accounts_config = self
.initial_accounts_config
.unwrap_or_else(|| GenesisConfig::for_local_testing().unwrap());
let (account_keys, objects) = initial_accounts_config.generate_accounts().unwrap();
let (account_keys, objects) = initial_accounts_config
.generate_accounts(&mut self.rng)
.unwrap();
// TODO: allow custom address to be used after the Gateway refactoring
// Default to use the last address in the wallet config for initializing modules.
// If there's no address in wallet config, then use 0x0
Expand Down
10 changes: 3 additions & 7 deletions crates/sui-config/src/genesis.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use std::path::PathBuf;

use base64ct::Encoding;
use move_binary_format::CompiledModule;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_with::{serde_as, DeserializeAs, SerializeAs};
use sui_types::{
base_types::{TransactionDigest, TxContext},
crypto::PublicKeyBytes,
object::Object,
};
use std::path::PathBuf;
use sui_types::{base_types::TxContext, crypto::PublicKeyBytes, object::Object};
use tracing::info;

#[serde_as]
Expand Down Expand Up @@ -129,6 +124,7 @@ impl Builder {
// self
// }

//TODO actually use the validators added to genesis
pub fn add_validator(mut self, public_key: PublicKeyBytes, stake: usize) -> Self {
self.validators.push((public_key, stake));
self
Expand Down
15 changes: 8 additions & 7 deletions crates/sui-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ use anyhow::Result;
use debug_ignore::DebugIgnore;
use move_binary_format::CompiledModule;
use multiaddr::Multiaddr;
use narwhal_config::Committee as ConsensusCommittee;
use narwhal_config::Parameters as ConsensusParameters;
use narwhal_config::{
Authority, Committee as ConsensusCommittee, PrimaryAddresses, Stake, WorkerAddresses,
};
use narwhal_crypto::ed25519::Ed25519PublicKey;
use rand::rngs::OsRng;
use serde::de::DeserializeOwned;
Expand All @@ -18,9 +16,9 @@ use std::fs;
use std::num::NonZeroUsize;
use std::path::{Path, PathBuf};
use sui_framework::DEFAULT_FRAMEWORK_PATH;
use sui_types::base_types::{encode_bytes_hex, ObjectID, SuiAddress, TxContext};
use sui_types::base_types::{ObjectID, SuiAddress, TxContext};
use sui_types::committee::{Committee, EpochId};
use sui_types::crypto::{get_key_pair, KeyPair, PublicKeyBytes};
use sui_types::crypto::{get_key_pair_from_rng, KeyPair, PublicKeyBytes};
use sui_types::object::Object;
use tracing::{info, trace};

Expand Down Expand Up @@ -262,7 +260,10 @@ pub struct GenesisConfig {
impl Config for GenesisConfig {}

impl GenesisConfig {
pub fn generate_accounts(&self) -> Result<(Vec<KeyPair>, Vec<Object>)> {
pub fn generate_accounts<R: ::rand::RngCore + ::rand::CryptoRng>(
&self,
mut rng: R,
) -> Result<(Vec<KeyPair>, Vec<Object>)> {
let mut addresses = Vec::new();
let mut preload_objects = Vec::new();
let mut all_preload_objects_set = BTreeSet::new();
Expand All @@ -274,7 +275,7 @@ impl GenesisConfig {
let address = if let Some(address) = account.address {
address
} else {
let (address, keypair) = get_key_pair();
let (address, keypair) = get_key_pair_from_rng(&mut rng);
keys.push(keypair);
address
};
Expand Down
14 changes: 8 additions & 6 deletions sui/src/bin/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

use clap::*;
use multiaddr::Multiaddr;
use std::path::PathBuf;
use std::{num::NonZeroUsize, path::PathBuf};
use sui::{
config::{sui_config_dir, SUI_NETWORK_CONFIG},
sui_commands::{genesis, make_server_with_genesis},
sui_commands::make_server_with_genesis,
};
use sui_config::PersistedConfig;
use sui_config::{builder::ConfigBuilder, PersistedConfig};
use sui_config::{GenesisConfig, ValidatorConfig};
use tracing::{error, info};

Expand Down Expand Up @@ -55,9 +55,11 @@ async fn main() -> Result<(), anyhow::Error> {

// If network.conf is missing, or if --force-genesis is true, we run genesis.
_ => {
let mut genesis_conf: GenesisConfig = PersistedConfig::read(&cfg.genesis_config_path)?;
genesis_conf.committee_size = 1;
let (network_config, _, _) = genesis(genesis_conf).await?;
let genesis_conf: GenesisConfig = PersistedConfig::read(&cfg.genesis_config_path)?;
let network_config = ConfigBuilder::new(sui_config_dir()?)
.committee_size(NonZeroUsize::new(1).unwrap())
.initial_accounts_config(genesis_conf)
.build();
network_config.into_validator_configs().remove(0)
}
};
Expand Down
181 changes: 18 additions & 163 deletions sui/src/sui_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,25 @@ use anyhow::{anyhow, bail};
use base64ct::{Base64, Encoding};
use clap::*;
use futures::future::join_all;
use move_binary_format::CompiledModule;
use move_package::BuildConfig;
use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::fs;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
use sui_adapter::adapter::generate_package_id;
use sui_adapter::genesis;
use sui_config::NetworkConfig;
use std::{collections::BTreeMap, num::NonZeroUsize};
use sui_config::{builder::ConfigBuilder, NetworkConfig};
use sui_config::{GenesisConfig, ValidatorConfig};
use sui_core::authority::{AuthorityState, AuthorityStore};
use sui_core::authority_active::ActiveAuthority;
use sui_core::authority_client::NetworkAuthorityClient;
use sui_core::authority_server::AuthorityServer;
use sui_core::authority_server::AuthorityServerHandle;
use sui_core::consensus_adapter::ConsensusListener;
use sui_types::base_types::{decode_bytes_hex, ObjectID};
use sui_types::base_types::{SequenceNumber, SuiAddress, TxContext};
use sui_types::base_types::decode_bytes_hex;
use sui_types::base_types::SuiAddress;
use sui_types::error::SuiResult;
use sui_types::object::Object;
use tokio::sync::mpsc::channel;
use tracing::{error, info};

pub const SUI_AUTHORITY_KEYS: &str = "authorities.key";

#[derive(Parser)]
#[clap(rename_all = "kebab-case")]
pub enum SuiCommand {
Expand Down Expand Up @@ -195,7 +187,20 @@ impl SuiCommand {
return Ok(());
}

let (network_config, accounts, mut keystore) = genesis(genesis_conf).await?;
let network_config = ConfigBuilder::new(sui_config_dir)
.committee_size(NonZeroUsize::new(genesis_conf.committee_size).unwrap())
.initial_accounts_config(genesis_conf)
.build();

let mut accounts = Vec::new();
let mut keystore = SuiKeystore::default();

for key in &network_config.account_keys {
let address = SuiAddress::from(key.public_key_bytes());
accounts.push(address);
keystore.add_key(address, key.copy())?;
}

info!("Network genesis completed.");
let network_config = network_config.persisted(&network_path);
network_config.save()?;
Expand Down Expand Up @@ -320,123 +325,6 @@ impl SuiNetwork {
}
}

pub async fn genesis(
genesis_conf: GenesisConfig,
) -> Result<(NetworkConfig, Vec<SuiAddress>, SuiKeystore), anyhow::Error> {
let num_to_provision = genesis_conf.committee_size;

info!("Creating {} new authorities...", num_to_provision);

let config_dir = sui_config_dir().unwrap();
let mut network_config = NetworkConfig::generate(&config_dir, num_to_provision);

let mut addresses = Vec::new();
let mut preload_modules: Vec<Vec<CompiledModule>> = Vec::new();
let mut preload_objects = Vec::new();
let mut all_preload_objects_set = BTreeSet::new();

info!("Creating accounts and gas objects...",);

let mut keystore = SuiKeystore::default();
for account in genesis_conf.accounts {
let address = if let Some(address) = account.address {
address
} else {
keystore.add_random_key()?
};

addresses.push(address);
let mut preload_objects_map = BTreeMap::new();

// Populate gas itemized objects
account.gas_objects.iter().for_each(|q| {
if !all_preload_objects_set.contains(&q.object_id) {
preload_objects_map.insert(q.object_id, q.gas_value);
}
});

// Populate ranged gas objects
if let Some(ranges) = account.gas_object_ranges {
for rg in ranges {
let ids = ObjectID::in_range(rg.offset, rg.count)?;

for obj_id in ids {
if !preload_objects_map.contains_key(&obj_id)
&& !all_preload_objects_set.contains(&obj_id)
{
preload_objects_map.insert(obj_id, rg.gas_value);
all_preload_objects_set.insert(obj_id);
}
}
}
}

for (object_id, value) in preload_objects_map {
let new_object = Object::with_id_owner_gas_coin_object_for_testing(
object_id,
SequenceNumber::new(),
address,
value,
);
preload_objects.push(new_object);
}
}

info!(
"Loading Move framework lib from {:?}",
genesis_conf.move_framework_lib_path
);
let move_lib = sui_framework::get_move_stdlib_modules(&genesis_conf.move_framework_lib_path)?;
preload_modules.push(move_lib);

// Load Sui and Move framework lib
info!(
"Loading Sui framework lib from {:?}",
genesis_conf.sui_framework_lib_path
);
let sui_lib = sui_framework::get_sui_framework_modules(&genesis_conf.sui_framework_lib_path)?;
preload_modules.push(sui_lib);

// TODO: allow custom address to be used after the Gateway refactoring
// Default to use the last address in the wallet config for initializing modules.
// If there's no address in wallet config, then use 0x0
let null_address = SuiAddress::default();
let module_init_address = addresses.last().unwrap_or(&null_address);
let mut genesis_ctx = genesis::get_genesis_context_with_custom_address(module_init_address);
// Build custom move packages
if !genesis_conf.move_packages.is_empty() {
info!(
"Loading {} Move packages from {:?}",
&genesis_conf.move_packages.len(),
&genesis_conf.move_packages
);

for path in genesis_conf.move_packages {
let mut modules =
sui_framework::build_move_package(&path, BuildConfig::default(), false)?;

let package_id = generate_package_id(&mut modules, &mut genesis_ctx)?;

info!("Loaded package [{}] from {:?}.", package_id, path);
// Writing package id to network config for user to retrieve later.
network_config.add_move_package(path, package_id);
preload_modules.push(modules)
}
}

for validator in network_config.validator_configs() {
make_server_with_genesis_ctx(
validator,
preload_modules.clone(),
&preload_objects,
&mut genesis_ctx.clone(),
)
.await?;
}

Ok((network_config, addresses, keystore))
}

pub async fn make_server(validator_config: &ValidatorConfig) -> SuiResult<AuthorityServer> {
let store = Arc::new(AuthorityStore::open(validator_config.db_path(), None));
let name = validator_config.public_key();
Expand Down Expand Up @@ -468,39 +356,6 @@ pub async fn make_server_with_genesis(
make_authority(validator_config, state).await
}

async fn make_server_with_genesis_ctx(
validator_config: &ValidatorConfig,
preload_modules: Vec<Vec<CompiledModule>>,
preload_objects: &[Object],
genesis_ctx: &mut TxContext,
) -> SuiResult<AuthorityServer> {
let store = Arc::new(AuthorityStore::open(validator_config.db_path(), None));
let name = *validator_config.key_pair().public_key_bytes();

let state = AuthorityState::new(
validator_config.committee_config().committee(),
name,
Arc::pin(validator_config.key_pair().copy()),
store,
preload_modules,
genesis_ctx,
)
.await;

// Okay to do this since we're at genesis
state
.insert_genesis_objects_bulk_unsafe(&preload_objects.iter().collect::<Vec<_>>())
.await;

let (tx_sui_to_consensus, _rx_sui_to_consensus) = channel(1);
Ok(AuthorityServer::new(
validator_config.network_address().clone(),
Arc::new(state),
validator_config.consensus_config().address().clone(),
/* tx_consensus_listener */ tx_sui_to_consensus,
))
}

/// Spawn all the subsystems run by a Sui authority: a consensus node, a sui authority server,
/// and a consensus listener bridging the consensus node and the sui authority.
pub async fn make_authority(
Expand Down
2 changes: 1 addition & 1 deletion sui_core/src/authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use std::{
Arc,
},
};
use sui_adapter::{adapter, genesis::get_genesis_context};
use sui_adapter::adapter;
use sui_config::genesis::Genesis;
use sui_types::{
base_types::*,
Expand Down
6 changes: 3 additions & 3 deletions test_utils/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use sui::{
Config, GatewayConfig, GatewayType, WalletConfig, SUI_GATEWAY_CONFIG, SUI_NETWORK_CONFIG,
SUI_WALLET_CONFIG,
},
keystore::{Keystore, KeystoreType, SuiKeystore},
sui_commands::{genesis, SuiNetwork},
keystore::{KeystoreType, SuiKeystore},
sui_commands::SuiNetwork,
};
use sui_config::{builder::ConfigBuilder, GenesisConfig, NetworkConfig};
use sui_config::{builder::ConfigBuilder, GenesisConfig};
use sui_types::base_types::SuiAddress;

const NUM_VALIDAOTR: usize = 4;
Expand Down

0 comments on commit 041a16f

Please sign in to comment.