Skip to content

Commit

Permalink
Consensus node now can obtain it's public address from an ENV VAR. Al…
Browse files Browse the repository at this point in the history
…so added a endpoint to be able to check config from nodes
  • Loading branch information
ElFantasma committed Feb 14, 2024
1 parent c864f51 commit b5b5ee6
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 16 deletions.
33 changes: 22 additions & 11 deletions node/tools/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use crate::{proto, store};
use anyhow::Context as _;
use serde_json::{ser::Formatter, Serializer};
use std::str::FromStr;
use std::{
collections::{HashMap, HashSet},
fs,
Expand All @@ -12,10 +13,7 @@ use zksync_concurrency::ctx;
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::{self, PublicKey},
validator,
};
use zksync_consensus_roles::{node, validator};
use zksync_consensus_storage::{BlockStore, BlockStoreRunner, PersistentBlockStore};
use zksync_protobuf::{required, serde::Serde, ProtoFmt};

Expand All @@ -33,10 +31,10 @@ pub fn decode_json<T: serde::de::DeserializeOwned>(json: &str) -> anyhow::Result
/// Encodes a generated proto message to json for arbitrary ProtoFmt.
pub(crate) fn encode_json<T: serde::ser::Serialize>(x: &T) -> String {
let s = serde_json::Serializer::pretty(vec![]);
encode_json_with_serializer(x, s)
encode_with_serializer(x, s)
}

pub(crate) fn encode_json_with_serializer<T: serde::ser::Serialize, F: Formatter>(
pub(crate) fn encode_with_serializer<T: serde::ser::Serialize, F: Formatter>(
x: &T,
mut serializer: Serializer<Vec<u8>, F>,
) -> String {
Expand All @@ -53,7 +51,7 @@ pub(crate) fn encode_json_with_serializer<T: serde::ser::Serialize, F: Formatter
/// Pair of (public key, ip address) for a gossip network node.
#[derive(Debug, Clone)]
pub struct NodeAddr {
pub key: PublicKey,
pub key: node::PublicKey,
pub addr: SocketAddr,
}

Expand Down Expand Up @@ -87,8 +85,17 @@ pub struct AppConfig {
pub max_payload_size: usize,

pub gossip_dynamic_inbound_limit: usize,
pub gossip_static_inbound: HashSet<PublicKey>,
pub gossip_static_outbound: HashMap<PublicKey, SocketAddr>,
pub gossip_static_inbound: HashSet<node::PublicKey>,
pub gossip_static_outbound: HashMap<node::PublicKey, SocketAddr>,
}

impl AppConfig {
pub fn check_public_addr(&mut self) -> anyhow::Result<()> {
if let Ok(public_addr) = std::env::var("PUBLIC_ADDR") {
self.public_addr = SocketAddr::from_str(&format!("{public_addr}:{NODES_PORT}"))?;
}
Ok(())
}
}

impl ProtoFmt for AppConfig {
Expand Down Expand Up @@ -275,12 +282,16 @@ impl AppConfig {
self
}

pub fn add_gossip_static_outbound(&mut self, key: PublicKey, addr: SocketAddr) -> &mut Self {
pub fn add_gossip_static_outbound(
&mut self,
key: node::PublicKey,
addr: SocketAddr,
) -> &mut Self {
self.gossip_static_outbound.insert(key, addr);
self
}

pub fn add_gossip_static_inbound(&mut self, key: PublicKey) -> &mut Self {
pub fn add_gossip_static_inbound(&mut self, key: node::PublicKey) -> &mut Self {
self.gossip_static_inbound.insert(key);
self
}
Expand Down
10 changes: 9 additions & 1 deletion node/tools/src/k8s.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ pub async fn create_deployment(
{
"name": "NODE_ID",
"value": node_id
},
{
"name": "PUBLIC_ADDR",
"valueFrom": {
"fieldRef": {
"fieldPath": "status.podIP"
}
}
}
],
"command": ["./k8s_entrypoint.sh"],
Expand Down Expand Up @@ -201,7 +209,7 @@ fn get_cli_args(peers: Vec<NodeAddr>) -> Vec<String> {
} else {
[
"--add-gossip-static-outbound".to_string(),
config::encode_json_with_serializer(
config::encode_with_serializer(
&peers
.iter()
.map(|e| Serde(e.clone()))
Expand Down
8 changes: 7 additions & 1 deletion node/tools/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ async fn main() -> anyhow::Result<()> {
.load()
.context("config_paths().load()")?;

// if `PUBLIC_ADDR` env var is set, use it to override publicAddr in config
configs.app.check_public_addr().context("Public Address")?;

// Add gossipStaticOutbound pairs from cli to config
if let Some(addrs) = args.add_gossip_static_outbound {
configs
Expand All @@ -121,7 +124,10 @@ async fn main() -> anyhow::Result<()> {
} else {
rpc_addr.set_port(rpc_addr.port() + 100);
}
let rpc_server = RPCServer::new(rpc_addr);

// cloning configuration to let RPCServer show it
// TODO this should be queried in real time instead, to reflect any possible change in config
let rpc_server = RPCServer::new(rpc_addr, configs.app.clone());

// Initialize the storage.
scope::run!(ctx, |ctx, s| async {
Expand Down
1 change: 1 addition & 0 deletions node/tools/src/rpc/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ pub(crate) trait RPCMethod {
fn path() -> &'static str;
}

pub(crate) mod config;
pub(crate) mod health_check;
pub(crate) mod peers;
20 changes: 17 additions & 3 deletions node/tools/src/rpc/server.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use super::methods::{health_check::HealthCheck, peers::PeersInfo, RPCMethod};
use crate::AppConfig;

use super::methods::{config::ConfigInfo, health_check::HealthCheck, peers::PeersInfo, RPCMethod};
use jsonrpsee::server::{middleware::http::ProxyGetRequestLayer, RpcModule, Server};
use std::net::SocketAddr;
use zksync_concurrency::{ctx, scope};
Expand All @@ -7,11 +9,13 @@ use zksync_concurrency::{ctx, scope};
pub struct RPCServer {
/// IP address to bind to.
ip_address: SocketAddr,
/// AppConfig
config: AppConfig,
}

impl RPCServer {
pub fn new(ip_address: SocketAddr) -> Self {
Self { ip_address }
pub fn new(ip_address: SocketAddr, config: AppConfig) -> Self {
Self { ip_address, config }
}

/// Runs the RPC server.
Expand All @@ -26,6 +30,10 @@ impl RPCServer {
.layer(ProxyGetRequestLayer::new(
PeersInfo::path(),
PeersInfo::method(),
)?)
.layer(ProxyGetRequestLayer::new(
ConfigInfo::path(),
ConfigInfo::method(),
)?);

let server = Server::builder()
Expand All @@ -39,6 +47,12 @@ impl RPCServer {
})?;
module.register_method(PeersInfo::method(), |params, _| PeersInfo::callback(params))?;

// TODO find a better way to implement this as I had to clone the clone and move it to pass the borrow checker
let config = self.config.clone();
module.register_method(ConfigInfo::method(), move |_params, _| {
ConfigInfo::info(config.clone())
})?;

let handle = server.start(module);
scope::run!(ctx, |ctx, s| async {
s.spawn_bg(async {
Expand Down

0 comments on commit b5b5ee6

Please sign in to comment.