Skip to content

Commit

Permalink
config: move validator-set into genesis
Browse files Browse the repository at this point in the history
  • Loading branch information
bmwill committed May 31, 2022
1 parent d604a2f commit 663aa3e
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 64 deletions.
16 changes: 4 additions & 12 deletions crates/sui-config/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
use crate::{
genesis,
genesis_config::{GenesisConfig, ValidatorGenesisInfo},
utils, CommitteeConfig, ConsensusConfig, NetworkConfig, NodeConfig, ValidatorInfo,
AUTHORITIES_DB_NAME, CONSENSUS_DB_NAME, DEFAULT_STAKE,
utils, ConsensusConfig, NetworkConfig, NodeConfig, ValidatorInfo, AUTHORITIES_DB_NAME,
CONSENSUS_DB_NAME, DEFAULT_STAKE,
};
use debug_ignore::DebugIgnore;
use narwhal_config::{
Expand Down Expand Up @@ -86,8 +86,6 @@ impl<R: ::rand::RngCore + ::rand::CryptoRng> ConfigBuilder<R> {
}

pub fn build_with_validators(mut self, validators: Vec<ValidatorGenesisInfo>) -> NetworkConfig {
let epoch = 0;

let validator_set = validators
.iter()
.map(|validator| {
Expand Down Expand Up @@ -121,8 +119,8 @@ impl<R: ::rand::RngCore + ::rand::CryptoRng> ConfigBuilder<R> {
.add_move_modules(custom_modules)
.add_objects(objects);

for validator in &validator_set {
builder = builder.add_validator(validator.public_key(), validator.stake());
for validator in validator_set {
builder = builder.add_validator(validator);
}

builder.build()
Expand Down Expand Up @@ -163,11 +161,6 @@ impl<R: ::rand::RngCore + ::rand::CryptoRng> ConfigBuilder<R> {
authorities: narwhal_committee,
});

let committee_config = CommitteeConfig {
epoch,
validator_set,
};

let validator_configs = validators
.into_iter()
.map(|validator| {
Expand Down Expand Up @@ -196,7 +189,6 @@ impl<R: ::rand::RngCore + ::rand::CryptoRng> ConfigBuilder<R> {
metrics_address: utils::available_local_socket_address(),
json_rpc_address: utils::available_local_socket_address(),
consensus_config: Some(consensus_config),
committee_config: committee_config.clone(),
genesis: crate::node::Genesis::new(genesis.clone()),
}
})
Expand Down
38 changes: 33 additions & 5 deletions crates/sui-config/src/genesis.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use crate::ValidatorInfo;
use anyhow::Context;
use base64ct::Encoding;
use move_binary_format::CompiledModule;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::{fs, path::Path};
use sui_types::{base_types::TxContext, crypto::PublicKeyBytes, object::Object};
use sui_types::{
base_types::TxContext,
committee::{Committee, EpochId},
object::Object,
};
use tracing::{info, trace};

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Genesis {
modules: Vec<Vec<CompiledModule>>,
objects: Vec<Object>,
genesis_ctx: TxContext,
validator_set: Vec<ValidatorInfo>,
}

impl Genesis {
Expand All @@ -29,6 +35,23 @@ impl Genesis {
&self.genesis_ctx
}

pub fn epoch(&self) -> EpochId {
self.genesis_ctx.epoch()
}

pub fn validator_set(&self) -> &[ValidatorInfo] {
&self.validator_set
}

pub fn committee(&self) -> Committee {
let voting_rights = self
.validator_set()
.iter()
.map(|validator| (validator.public_key(), validator.stake()))
.collect();
Committee::new(self.epoch(), voting_rights)
}

pub fn get_default_genesis() -> Self {
Builder::new(sui_adapter::genesis::get_genesis_context()).build()
}
Expand Down Expand Up @@ -63,6 +86,7 @@ impl Serialize for Genesis {
modules: Vec<Vec<Vec<u8>>>,
objects: &'a [Object],
genesis_ctx: &'a TxContext,
validator_set: &'a [ValidatorInfo],
}

let mut vec_serialized_modules = Vec::new();
Expand All @@ -82,6 +106,7 @@ impl Serialize for Genesis {
modules: vec_serialized_modules,
objects: &self.objects,
genesis_ctx: &self.genesis_ctx,
validator_set: &self.validator_set,
};

let bytes = bcs::to_bytes(&raw_genesis).map_err(|e| Error::custom(e.to_string()))?;
Expand All @@ -107,6 +132,7 @@ impl<'de> Deserialize<'de> for Genesis {
modules: Vec<Vec<Vec<u8>>>,
objects: Vec<Object>,
genesis_ctx: TxContext,
validator_set: Vec<ValidatorInfo>,
}

let bytes = if deserializer.is_human_readable() {
Expand Down Expand Up @@ -135,6 +161,7 @@ impl<'de> Deserialize<'de> for Genesis {
modules,
objects: raw_genesis.objects,
genesis_ctx: raw_genesis.genesis_ctx,
validator_set: raw_genesis.validator_set,
})
}
}
Expand All @@ -145,7 +172,7 @@ pub struct Builder {
move_modules: Vec<Vec<CompiledModule>>,
objects: Vec<Object>,
genesis_ctx: TxContext,
validators: Vec<(PublicKeyBytes, usize)>,
validators: Vec<ValidatorInfo>,
}

impl Builder {
Expand Down Expand Up @@ -190,9 +217,8 @@ 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));
pub fn add_validator(mut self, validator: ValidatorInfo) -> Self {
self.validators.push(validator);
self
}

Expand Down Expand Up @@ -226,6 +252,7 @@ impl Builder {
modules,
objects,
genesis_ctx: self.genesis_ctx,
validator_set: self.validators,
}
}
}
Expand All @@ -242,6 +269,7 @@ mod test {
modules: vec![sui_lib],
objects: vec![],
genesis_ctx: sui_adapter::genesis::get_genesis_context(),
validator_set: vec![],
};

let s = serde_yaml::to_string(&genesis).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub mod node;
mod swarm;
pub mod utils;

pub use node::{CommitteeConfig, ConsensusConfig, NodeConfig, ValidatorInfo};
pub use node::{ConsensusConfig, NodeConfig, ValidatorInfo};
pub use swarm::NetworkConfig;

const SUI_DIR: &str = ".sui";
Expand Down
35 changes: 1 addition & 34 deletions crates/sui-config/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use serde::{Deserialize, Serialize};
use std::net::SocketAddr;
use std::path::{Path, PathBuf};
use sui_types::base_types::SuiAddress;
use sui_types::committee::{Committee, EpochId};
use sui_types::crypto::{KeyPair, PublicKeyBytes};

#[derive(Debug, Deserialize, Serialize)]
Expand All @@ -30,7 +29,6 @@ pub struct NodeConfig {

#[serde(skip_serializing_if = "Option::is_none")]
pub consensus_config: Option<ConsensusConfig>,
pub committee_config: CommitteeConfig,

pub genesis: Genesis,
}
Expand Down Expand Up @@ -77,10 +75,6 @@ impl NodeConfig {
self.consensus_config.as_ref()
}

pub fn committee_config(&self) -> &CommitteeConfig {
&self.committee_config
}

pub fn genesis(&self) -> Result<&genesis::Genesis> {
self.genesis.genesis()
}
Expand Down Expand Up @@ -118,36 +112,9 @@ impl ConsensusConfig {
}
}

//TODO get this information from on-chain + some way to do network discovery
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")]
pub struct CommitteeConfig {
pub epoch: EpochId,
pub validator_set: Vec<ValidatorInfo>,
}

impl CommitteeConfig {
pub fn epoch(&self) -> EpochId {
self.epoch
}

pub fn validator_set(&self) -> &[ValidatorInfo] {
&self.validator_set
}

pub fn committee(&self) -> Committee {
let voting_rights = self
.validator_set()
.iter()
.map(|validator| (validator.public_key(), validator.stake()))
.collect();
Committee::new(self.epoch(), voting_rights)
}
}

/// Publicly known information about a validator
/// TODO read most of this from on-chain
#[derive(Debug, Clone, Deserialize, Serialize)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
pub struct ValidatorInfo {
pub public_key: PublicKeyBytes,
Expand Down
7 changes: 2 additions & 5 deletions crates/sui-config/src/swarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@ impl NetworkConfig {
}

pub fn validator_set(&self) -> &[ValidatorInfo] {
self.validator_configs()[0]
.committee_config()
.validator_set()
self.genesis.validator_set()
}

pub fn committee(&self) -> Committee {
self.validator_configs()[0].committee_config().committee()
self.genesis.committee()
}

pub fn into_validator_configs(self) -> Vec<NodeConfig> {
Expand Down Expand Up @@ -71,7 +69,6 @@ impl NetworkConfig {
json_rpc_address: utils::available_local_socket_address(),

consensus_config: None,
committee_config: validator_config.committee_config.clone(),

genesis: validator_config.genesis.clone(),
}
Expand Down
10 changes: 6 additions & 4 deletions crates/sui-node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ impl SuiNode {
"Initializing sui-node listening on {}", config.network_address
);

let genesis = config.genesis()?;

let secret = Arc::pin(config.key_pair().copy());
let store = Arc::new(AuthorityStore::open(config.db_path().join("store"), None));
let checkpoint_store = if config.consensus_config().is_some() {
Some(Arc::new(Mutex::new(CheckpointStore::open(
config.db_path().join("checkpoints"),
None,
config.public_key(),
config.committee_config().committee(),
genesis.committee(),
secret.clone(),
)?)))
} else {
Expand All @@ -58,13 +60,13 @@ impl SuiNode {

let state = Arc::new(
AuthorityState::new(
config.committee_config().committee(),
genesis.committee(),
config.public_key(),
secret,
store,
index_store,
checkpoint_store,
config.genesis()?,
genesis,
)
.await,
);
Expand All @@ -77,7 +79,7 @@ impl SuiNode {
net_config.request_timeout = Some(Duration::from_secs(5));

let mut authority_clients = BTreeMap::new();
for validator in config.committee_config().validator_set() {
for validator in genesis.validator_set() {
let channel = net_config
.connect_lazy(validator.network_address())
.unwrap();
Expand Down
4 changes: 2 additions & 2 deletions crates/sui-node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use anyhow::Result;
use clap::Parser;
use multiaddr::Multiaddr;
use std::path::PathBuf;
use sui_config::{NodeConfig, PersistedConfig};
use sui_config::{Config, NodeConfig};
use tracing::info;

#[derive(Parser)]
Expand All @@ -32,7 +32,7 @@ async fn main() -> Result<()> {

let args = Args::parse();

let mut config = PersistedConfig::<NodeConfig>::read(&args.config_path)?;
let mut config = NodeConfig::load(&args.config_path)?;

// TODO: Switch from prometheus exporter. See https://github.com/MystenLabs/sui/issues/1907
info!(
Expand Down
4 changes: 4 additions & 0 deletions crates/sui-types/src/base_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,10 @@ impl TxContext {
}
}

pub fn epoch(&self) -> EpochId {
self.epoch
}

/// Derive a globally unique object ID by hashing self.digest | self.ids_created
pub fn fresh_id(&mut self) -> ObjectID {
let id = self.digest().derive_id(self.ids_created);
Expand Down
3 changes: 2 additions & 1 deletion crates/sui/src/benchmark/validator_preparer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,14 @@ impl ValidatorPreparer {
RunningMode::SingleValidatorThread => {
// Pick the first validator and create state.
let validator_config = &network_config.validator_configs()[0];
let committee = network_config.committee();

// Create a random directory to store the DB
let path = env::temp_dir().join(format!("DB_{:?}", ObjectID::random()));
let auth_state = make_authority_state(
&path,
db_cpus as i32,
&validator_config.committee_config().committee(),
&committee,
&validator_config.public_key(),
validator_config.key_pair().copy(),
);
Expand Down

0 comments on commit 663aa3e

Please sign in to comment.