Skip to content

Commit

Permalink
simplified tool api
Browse files Browse the repository at this point in the history
  • Loading branch information
pompon0 committed Apr 3, 2024
1 parent 3e37015 commit 7ba2961
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 187 deletions.
6 changes: 6 additions & 0 deletions node/libs/crypto/src/ed25519/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ impl Ord for PublicKey {
}
}

impl PartialEq for SecretKey {
fn eq(&self, other: &Self) -> bool {
self.public() == other.public()
}
}

/// ed25519 signature.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Signature(ed::Signature);
Expand Down
2 changes: 1 addition & 1 deletion node/libs/roles/src/node/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use zksync_consensus_crypto::{ed25519, ByteFmt, Text, TextFmt};
use zksync_consensus_utils::enum_util::Variant;

/// A node's secret key.
#[derive(Clone)]
#[derive(Clone, PartialEq)]
pub struct SecretKey(pub(super) Arc<ed25519::SecretKey>);

impl SecretKey {
Expand Down
2 changes: 1 addition & 1 deletion node/libs/roles/src/validator/keys/secret_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use zksync_consensus_utils::enum_util::Variant;
/// A secret key for the validator role.
/// SecretKey is put into an Arc, so that we can clone it,
/// without copying the secret all over the RAM.
#[derive(Clone)]
#[derive(Clone, PartialEq)]
pub struct SecretKey(pub(crate) Arc<bn254::SecretKey>);

impl SecretKey {
Expand Down
28 changes: 13 additions & 15 deletions node/tools/src/bin/deployer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,22 @@ fn generate_consensus_nodes(nodes: usize, seed_nodes_amount: Option<usize>) -> V

let node_keys: Vec<SecretKey> = (0..nodes).map(|_| SecretKey::generate()).collect();

let default_config = AppConfig {
server_addr: SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), NODES_PORT),
public_addr: SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), NODES_PORT).into(),
debug_addr: None,
metrics_server_addr: None,
genesis: setup.genesis.clone(),
max_payload_size: 1000000,
gossip_dynamic_inbound_limit: 2,
gossip_static_inbound: [].into(),
gossip_static_outbound: [].into(),
};

let mut cfgs: Vec<ConsensusNode> = (0..nodes)
.map(|i| ConsensusNode {
id: format!("consensus-node-{i:0>2}"),
config: default_config.clone(),
key: node_keys[i].clone(),
validator_key: Some(validator_keys[i].clone()),
config: AppConfig {
server_addr: SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), NODES_PORT),
public_addr: SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), NODES_PORT).into(),
debug_addr: None,
metrics_server_addr: None,
genesis: setup.genesis.clone(),
max_payload_size: 1000000,
validator_key: Some(validator_keys[i].clone()),
node_key: node_keys[i].clone(),
gossip_dynamic_inbound_limit: 2,
gossip_static_inbound: [].into(),
gossip_static_outbound: [].into(),
},
node_addr: None, //It's not assigned yet
is_seed: i < seed_nodes_amount,
})
Expand Down
2 changes: 2 additions & 0 deletions node/tools/src/bin/localnet_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ fn main() -> anyhow::Result<()> {
.map(|port| SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), port)),
genesis: setup.genesis.clone(),
max_payload_size: 1000000,
node_key: node_keys[i].clone(),
validator_key: Some(validator_keys[i].clone()),
gossip_dynamic_inbound_limit: 0,
gossip_static_inbound: HashSet::default(),
gossip_static_outbound: HashMap::default(),
Expand Down
121 changes: 38 additions & 83 deletions node/tools/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,32 @@ use anyhow::Context as _;
use serde_json::{ser::Formatter, Serializer};
use std::{
collections::{HashMap, HashSet},
fs,
net::SocketAddr,
path::{Path, PathBuf},
path::PathBuf,
};
use zksync_concurrency::{ctx, net};
use zksync_consensus_bft as bft;
use zksync_consensus_crypto::{read_optional_text, read_required_text, Text, TextFmt};
use zksync_consensus_executor as executor;
use zksync_consensus_roles::{node, validator};
use zksync_consensus_storage::{BlockStore, BlockStoreRunner};
use zksync_protobuf::{read_required, required, serde::Serde, ProtoFmt};
use zksync_protobuf::{read_required, required, ProtoFmt};

fn read_required_secret_text<T: TextFmt>(text: &Option<String>) -> anyhow::Result<T> {
Text::new(
text.as_ref()
.ok_or_else(|| anyhow::format_err!("missing"))?,
)
.decode()
.map_err(|_| anyhow::format_err!("invalid format"))
}

fn read_optional_secret_text<T: TextFmt>(text: &Option<String>) -> anyhow::Result<Option<T>> {
text.as_ref()
.map(|t| Text::new(t).decode())
.transpose()
.map_err(|_| anyhow::format_err!("invalid format"))
}

/// Ports for the nodes to listen on kubernetes pod.
pub const NODES_PORT: u16 = 3054;
Expand Down Expand Up @@ -77,7 +92,9 @@ pub struct AppConfig {

pub genesis: validator::Genesis,
pub max_payload_size: usize,
pub validator_key: Option<validator::SecretKey>,

pub node_key: node::SecretKey,
pub gossip_dynamic_inbound_limit: usize,
pub gossip_static_inbound: HashSet<node::PublicKey>,
pub gossip_static_outbound: HashMap<node::PublicKey, net::Host>,
Expand Down Expand Up @@ -113,7 +130,11 @@ impl ProtoFmt for AppConfig {
max_payload_size: required(&r.max_payload_size)
.and_then(|x| Ok((*x).try_into()?))
.context("max_payload_size")?,
// TODO: read secret.
validator_key: read_optional_secret_text(&r.validator_secret_key)
.context("validator_secret_key")?,

node_key: read_required_secret_text(&r.node_secret_key).context("node_secret_key")?,
gossip_dynamic_inbound_limit: required(&r.gossip_dynamic_inbound_limit)
.and_then(|x| Ok((*x).try_into()?))
.context("gossip_dynamic_inbound_limit")?,
Expand All @@ -131,7 +152,9 @@ impl ProtoFmt for AppConfig {

genesis: Some(self.genesis.build()),
max_payload_size: Some(self.max_payload_size.try_into().unwrap()),
validator_secret_key: self.validator_key.as_ref().map(TextFmt::encode),

node_secret_key: Some(self.node_key.encode()),
gossip_dynamic_inbound_limit: Some(
self.gossip_dynamic_inbound_limit.try_into().unwrap(),
),
Expand All @@ -152,86 +175,12 @@ impl ProtoFmt for AppConfig {
}
}

/// Configuration information.
#[derive(Debug)]
pub struct ConfigArgs<'a> {
/// Node configuration.
pub config_args: ConfigSource<'a>,
/// Path to the rocksdb database.
pub database: &'a Path,
}

#[derive(Debug)]
pub enum ConfigSource<'a> {
CliConfig {
/// Node configuration from command line.
config: AppConfig,
/// Node key as a string.
node_key: node::SecretKey,
/// Validator key as a string.
validator_key: Option<validator::SecretKey>,
},
PathConfig {
/// Path to a JSON file with node configuration.
config_file: &'a Path,
/// Path to a validator key file.
validator_key_file: &'a Path,
/// Path to a node key file.
node_key_file: &'a Path,
},
}

pub struct Configs {
pub app: AppConfig,
pub validator_key: Option<validator::SecretKey>,
pub node_key: node::SecretKey,
pub database: PathBuf,
}

impl<'a> ConfigArgs<'a> {
// Loads configs from the file system.
pub fn load(self) -> anyhow::Result<Configs> {
match self.config_args {
ConfigSource::CliConfig {
config,
node_key,
validator_key,
} => Ok(Configs {
app: config.clone(),
validator_key: validator_key.clone(),
node_key: node_key.clone(),
database: self.database.into(),
}),
ConfigSource::PathConfig {
config_file,
validator_key_file,
node_key_file,
} => Ok(Configs {
app: (|| {
let app = fs::read_to_string(config_file).context("failed reading file")?;
decode_json::<Serde<AppConfig>>(&app).context("failed decoding JSON")
})()
.with_context(|| config_file.display().to_string())?
.0,

validator_key: fs::read_to_string(validator_key_file)
.ok()
.map(|value| Text::new(&value).decode().context("failed decoding key"))
.transpose()
.with_context(|| validator_key_file.display().to_string())?,

node_key: (|| {
let key = fs::read_to_string(node_key_file).context("failed reading file")?;
Text::new(&key).decode().context("failed decoding key")
})()
.with_context(|| node_key_file.display().to_string())?,

database: self.database.into(),
}),
}
}
}

impl Configs {
pub async fn make_executor(
&self,
Expand All @@ -243,18 +192,24 @@ impl Configs {
config: executor::Config {
server_addr: self.app.server_addr,
public_addr: self.app.public_addr.clone(),
node_key: self.node_key.clone(),
node_key: self.app.node_key.clone(),
gossip_dynamic_inbound_limit: self.app.gossip_dynamic_inbound_limit,
gossip_static_inbound: self.app.gossip_static_inbound.clone(),
gossip_static_outbound: self.app.gossip_static_outbound.clone(),
max_payload_size: self.app.max_payload_size,
},
block_store,
validator: self.validator_key.as_ref().map(|key| executor::Validator {
key: key.clone(),
replica_store: Box::new(store),
payload_manager: Box::new(bft::testonly::RandomPayload(self.app.max_payload_size)),
}),
validator: self
.app
.validator_key
.as_ref()
.map(|key| executor::Validator {
key: key.clone(),
replica_store: Box::new(store),
payload_manager: Box::new(bft::testonly::RandomPayload(
self.app.max_payload_size,
)),
}),
};
Ok((e, runner))
}
Expand Down
17 changes: 2 additions & 15 deletions node/tools/src/k8s.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ use kube::{
use std::{collections::BTreeMap, net::SocketAddr, time::Duration};
use tokio::time;
use tracing::log::info;
use zksync_consensus_crypto::TextFmt;
use zksync_consensus_roles::{node, validator};
use zksync_protobuf::serde::Serde;

/// Docker image name for consensus nodes.
Expand All @@ -35,10 +33,6 @@ pub struct ConsensusNode {
pub id: String,
/// Node configuration
pub config: AppConfig,
/// Node key
pub key: node::SecretKey,
/// Node key
pub validator_key: Option<validator::SecretKey>,
/// Full NodeAddr
pub node_addr: Option<NodeAddr>,
/// Is seed node (meaning it has no gossipStaticOutbound configuration)
Expand Down Expand Up @@ -74,7 +68,7 @@ impl ConsensusNode {
.pod_ip
.context("Pod IP address not present")?;
self.node_addr = Some(NodeAddr {
key: self.key.public(),
key: self.config.node_key.public(),
addr: SocketAddr::new(ip.parse()?, config::NODES_PORT).into(),
});
Ok(())
Expand Down Expand Up @@ -339,20 +333,13 @@ fn is_pod_running(pod: &Pod) -> bool {
}

fn get_cli_args(consensus_node: &ConsensusNode) -> Vec<String> {
let mut cli_args = [
vec![
"--config".to_string(),
config::encode_with_serializer(
&Serde(consensus_node.config.clone()),
serde_json::Serializer::new(vec![]),
),
"--node-key".to_string(),
TextFmt::encode(&consensus_node.key),
]
.to_vec();
if let Some(key) = &consensus_node.validator_key {
cli_args.append(&mut ["--validator-key".to_string(), TextFmt::encode(key)].to_vec())
};
cli_args
}

async fn retry<T, Fut, F>(retries: usize, delay: Duration, mut f: F) -> anyhow::Result<T>
Expand Down
4 changes: 1 addition & 3 deletions node/tools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,5 @@ mod store;
#[cfg(test)]
mod tests;

pub use config::{
decode_json, encode_json, AppConfig, ConfigArgs, ConfigSource, NodeAddr, NODES_PORT,
};
pub use config::{decode_json, encode_json, AppConfig, Configs, NodeAddr, NODES_PORT};
pub use rpc::server::RPCServer;
Loading

0 comments on commit 7ba2961

Please sign in to comment.