Skip to content

Commit

Permalink
feat: allow passing in narwhal ip/port
Browse files Browse the repository at this point in the history
  • Loading branch information
joske committed Oct 3, 2023
1 parent c31eb11 commit 49002a7
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
25 changes: 17 additions & 8 deletions cli/src/commands/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ pub struct Start {
#[clap(default_value = "", long = "validators")]
pub validators: String,

// Specify the IP address and port for narwhal.
#[clap(long = "narwhal")]
pub narwhal: Option<SocketAddr>,

/// Specify the IP address and port for the REST server
#[clap(default_value = "0.0.0.0:3033", long = "rest")]
pub rest: SocketAddr,
Expand Down Expand Up @@ -243,15 +247,19 @@ impl Start {
// and the REST IP to `3030 + dev`.
if let Some(dev) = self.dev {
// Add the dev nodes to the trusted peers.
for i in 0..dev {
if i != dev {
trusted_peers.push(SocketAddr::from_str(&format!("127.0.0.1:{}", 4130 + i))?);
if trusted_peers.is_empty() {
for i in 0..dev {
if i != dev {
trusted_peers.push(SocketAddr::from_str(&format!("127.0.0.1:{}", 4130 + i))?);
}
}
}
// To avoid ambiguity, we define the first few nodes to be the trusted validators to connect to.
for i in 0..2 {
if i != dev {
trusted_validators.push(SocketAddr::from_str(&format!("127.0.0.1:{}", MEMORY_POOL_PORT + i))?);
if trusted_validators.is_empty() {
// To avoid ambiguity, we define the first few nodes to be the trusted validators to connect to.
for i in 0..2 {
if i != dev {
trusted_validators.push(SocketAddr::from_str(&format!("127.0.0.1:{}", MEMORY_POOL_PORT + i))?);
}
}
}
// Set the node IP to `4130 + dev`.
Expand Down Expand Up @@ -382,8 +390,9 @@ impl Start {
crate::helpers::check_validator_machine(node_type, self.dev.is_some());

// Initialize the node.
let narwhal_ip = if self.dev.is_some() { self.narwhal } else { None };
match node_type {
NodeType::Validator => Node::new_validator(self.node, rest_ip, account, &trusted_peers, &trusted_validators, genesis, cdn, self.dev).await,
NodeType::Validator => Node::new_validator(self.node, rest_ip, narwhal_ip, account, &trusted_peers, &trusted_validators, genesis, cdn, self.dev).await,
NodeType::Prover => Node::new_prover(self.node, account, &trusted_peers, genesis, self.dev).await,
NodeType::Client => Node::new_client(self.node, rest_ip, account, &trusted_peers, genesis, cdn, self.dev).await,
}
Expand Down
4 changes: 2 additions & 2 deletions node/narwhal/src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ impl<N: Network> Gateway<N> {
) -> Result<Self> {
// Initialize the gateway IP.
let ip = match (ip, dev) {
(_, Some(dev)) => SocketAddr::from_str(&format!("127.0.0.1:{}", MEMORY_POOL_PORT + dev))?,
(None, Some(dev)) => SocketAddr::from_str(&format!("127.0.0.1:{}", MEMORY_POOL_PORT + dev))?,
(None, None) => SocketAddr::from_str(&format!("0.0.0.0:{}", MEMORY_POOL_PORT))?,
(Some(ip), None) => ip,
(Some(ip), _) => ip,
};
// Initialize the TCP stack.
let tcp = Tcp::new(Config::new(ip, MAX_COMMITTEE_SIZE));
Expand Down
4 changes: 3 additions & 1 deletion node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ impl<N: Network> Node<N> {
pub async fn new_validator(
node_ip: SocketAddr,
rest_ip: Option<SocketAddr>,
narwhal_ip: Option<SocketAddr>,
account: Account<N>,
trusted_peers: &[SocketAddr],
trusted_validators: &[SocketAddr],
Expand All @@ -49,7 +50,8 @@ impl<N: Network> Node<N> {
dev: Option<u16>,
) -> Result<Self> {
Ok(Self::Validator(Arc::new(
Validator::new(node_ip, rest_ip, account, trusted_peers, trusted_validators, genesis, cdn, dev).await?,
Validator::new(node_ip, rest_ip, narwhal_ip, account, trusted_peers, trusted_validators, genesis, cdn, dev)
.await?,
)))
}

Expand Down
4 changes: 3 additions & 1 deletion node/src/validator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ impl<N: Network, C: ConsensusStorage<N>> Validator<N, C> {
pub async fn new(
node_ip: SocketAddr,
rest_ip: Option<SocketAddr>,
narwhal_ip: Option<SocketAddr>,
account: Account<N>,
trusted_peers: &[SocketAddr],
trusted_validators: &[SocketAddr],
Expand Down Expand Up @@ -101,7 +102,7 @@ impl<N: Network, C: ConsensusStorage<N>> Validator<N, C> {
let sync = BlockSync::new(BlockSyncMode::Gateway, ledger_service.clone());

// Initialize the consensus.
let mut consensus = Consensus::new(account.clone(), ledger_service, None, trusted_validators, dev)?;
let mut consensus = Consensus::new(account.clone(), ledger_service, narwhal_ip, trusted_validators, dev)?;
// Initialize the primary channels.
let (primary_sender, primary_receiver) = init_primary_channels::<N>();
// Start the consensus.
Expand Down Expand Up @@ -474,6 +475,7 @@ mod tests {
let validator = Validator::<CurrentNetwork, ConsensusMemory<CurrentNetwork>>::new(
node,
Some(rest),
None,
account,
&[],
&[],
Expand Down
1 change: 1 addition & 0 deletions node/tests/common/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub async fn validator() -> Validator<CurrentNetwork, ConsensusMemory<CurrentNet
Validator::new(
"127.0.0.1:0".parse().unwrap(),
None,
None,
Account::<CurrentNetwork>::from_str("APrivateKey1zkp2oVPTci9kKcUprnbzMwq95Di1MQERpYBhEeqvkrDirK1").unwrap(),
&[],
&[],
Expand Down

0 comments on commit 49002a7

Please sign in to comment.