Skip to content

Commit cfd1ea9

Browse files
Some update to the way ports are assigned. (#4357)
## Motivation We may want to run with more than 10 validators. A previous PR makes the work on more than 10 validators impossible. Here we constrain the product `num_validator * num_shard`. Fixes #4319 ## Proposal We assign the ports according to the set constants. For the test `linera-service/src/exporter/tests.rs` we have a leakage of the formula. But this is just one test. ## Test Plan The CI. The wasm end-to-end tests were run with 20 validators and 1 shard. ## Release Plan - Nothing to do / These changes follow the usual release cycle. ## Links None
1 parent 51705f0 commit cfd1ea9

File tree

3 files changed

+54
-52
lines changed

3 files changed

+54
-52
lines changed

linera-service/src/cli_wrappers/local_net.rs

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,10 @@ use crate::{
4545
util::ChildExt,
4646
};
4747

48-
/// Maximum number of shards allowed.
49-
const MAX_NUMBER_SHARD: usize = 100;
48+
/// Maximum allowed number of shards over all validators.
49+
const MAX_NUMBER_SHARDS: usize = 1000;
5050

51-
/// Maximum number of validators allowed.
52-
const MAX_NUMBER_VALIDATOR: usize = 10;
51+
pub const FIRST_PUBLIC_PORT: usize = 13000;
5352

5453
pub enum ProcessInbox {
5554
Skip,
@@ -346,17 +345,12 @@ impl LineraNetConfig for LocalNetConfig {
346345
self.num_initial_validators > 0,
347346
"There should be at least one initial validator"
348347
);
348+
let total_number_shards = self.num_initial_validators * self.num_shards;
349349
ensure!(
350-
self.num_initial_validators <= MAX_NUMBER_VALIDATOR,
351-
"Number of initial validators ({}) exceeds maximum allowed ({})",
352-
self.num_initial_validators,
353-
MAX_NUMBER_VALIDATOR
354-
);
355-
ensure!(
356-
self.num_shards <= MAX_NUMBER_SHARD,
357-
"Number of shards ({}) exceeds maximum allowed ({})",
350+
total_number_shards <= MAX_NUMBER_SHARDS,
351+
"Total number of shards ({}) exceeds maximum allowed ({})",
358352
self.num_shards,
359-
MAX_NUMBER_SHARD
353+
MAX_NUMBER_SHARDS
360354
);
361355
net.generate_initial_validator_config().await?;
362356
client
@@ -449,32 +443,36 @@ impl LocalNet {
449443
crate::util::read_json(path.join("genesis.json"))
450444
}
451445

452-
pub fn proxy_public_port(validator: usize, proxy_id: usize) -> usize {
453-
13000 + validator * MAX_NUMBER_SHARD + proxy_id + 1
446+
fn shard_port(&self, validator: usize, shard: usize) -> usize {
447+
9000 + validator * self.num_shards + shard + 1
448+
}
449+
450+
fn proxy_internal_port(&self, validator: usize, proxy_id: usize) -> usize {
451+
10000 + validator * self.num_shards + proxy_id + 1
454452
}
455453

456-
fn proxy_internal_port(validator: usize, proxy_id: usize) -> usize {
457-
10000 + validator * MAX_NUMBER_SHARD + proxy_id + 1
454+
fn shard_metrics_port(&self, validator: usize, shard: usize) -> usize {
455+
11000 + validator * self.num_shards + shard + 1
458456
}
459457

460-
fn shard_port(validator: usize, shard: usize) -> usize {
461-
9000 + validator * MAX_NUMBER_SHARD + shard + 1
458+
fn proxy_metrics_port(&self, validator: usize, proxy_id: usize) -> usize {
459+
12000 + validator * self.num_shards + proxy_id + 1
462460
}
463461

464-
fn proxy_metrics_port(validator: usize, proxy_id: usize) -> usize {
465-
12000 + validator * MAX_NUMBER_SHARD + proxy_id + 1
462+
fn block_exporter_port(&self, validator: usize, exporter_id: usize) -> usize {
463+
12000 + validator * self.num_shards + exporter_id + 1
466464
}
467465

468-
fn shard_metrics_port(validator: usize, shard: usize) -> usize {
469-
11000 + validator * MAX_NUMBER_SHARD + shard + 1
466+
pub fn proxy_public_port(&self, validator: usize, proxy_id: usize) -> usize {
467+
FIRST_PUBLIC_PORT + validator * self.num_shards + proxy_id + 1
470468
}
471469

472-
fn block_exporter_port(validator: usize, exporter_id: usize) -> usize {
473-
12000 + validator * MAX_NUMBER_SHARD + exporter_id + 1
470+
pub fn first_public_port() -> usize {
471+
FIRST_PUBLIC_PORT + 1
474472
}
475473

476474
fn block_exporter_metrics_port(exporter_id: usize) -> usize {
477-
13000 + exporter_id + 1
475+
FIRST_PUBLIC_PORT + exporter_id + 1
478476
}
479477

480478
fn configuration_string(&self, server_number: usize) -> Result<String> {
@@ -483,7 +481,7 @@ impl LocalNet {
483481
.path_provider
484482
.path()
485483
.join(format!("validator_{n}.toml"));
486-
let port = Self::proxy_public_port(n, 0);
484+
let port = self.proxy_public_port(n, 0);
487485
let external_protocol = self.network.external.toml();
488486
let internal_protocol = self.network.internal.toml();
489487
let external_host = self.network.external.localhost();
@@ -499,8 +497,8 @@ impl LocalNet {
499497
);
500498

501499
for k in 0..self.num_proxies {
502-
let internal_port = Self::proxy_internal_port(n, k);
503-
let metrics_port = Self::proxy_metrics_port(n, k);
500+
let internal_port = self.proxy_internal_port(n, k);
501+
let metrics_port = self.proxy_metrics_port(n, k);
504502
// In the local network, the validator ingress is
505503
// the proxy - so the `public_port` is the validator
506504
// port.
@@ -517,8 +515,8 @@ impl LocalNet {
517515
}
518516

519517
for k in 0..self.num_shards {
520-
let shard_port = Self::shard_port(n, k);
521-
let shard_metrics_port = Self::shard_metrics_port(n, k);
518+
let shard_port = self.shard_port(n, k);
519+
let shard_metrics_port = self.shard_metrics_port(n, k);
522520
content.push_str(&format!(
523521
r#"
524522
@@ -534,7 +532,7 @@ impl LocalNet {
534532
ExportersSetup::Local(ref exporters) => {
535533
for (j, exporter) in exporters.iter().enumerate() {
536534
let host = Network::Grpc.localhost();
537-
let port = Self::block_exporter_port(n, j);
535+
let port = self.block_exporter_port(n, j);
538536
let config_content = format!(
539537
r#"
540538
@@ -593,7 +591,7 @@ impl LocalNet {
593591
) -> String {
594592
let n = validator;
595593
let host = Network::Grpc.localhost();
596-
let port = Self::block_exporter_port(n, exporter_id as usize);
594+
let port = self.block_exporter_port(n, exporter_id as usize);
597595
let metrics_port = Self::block_exporter_metrics_port(exporter_id as usize);
598596
let mut config = format!(
599597
r#"
@@ -713,7 +711,7 @@ impl LocalNet {
713711
.args(["--storage", &storage.to_string()])
714712
.spawn_into()?;
715713

716-
let port = Self::proxy_public_port(validator, 0);
714+
let port = self.proxy_public_port(validator, 0);
717715
let nickname = format!("validator proxy {validator}");
718716
match self.network.external {
719717
Network::Grpc => {
@@ -753,12 +751,12 @@ impl LocalNet {
753751

754752
match self.network.internal {
755753
Network::Grpc => {
756-
let port = Self::block_exporter_port(validator, exporter_id as usize);
754+
let port = self.block_exporter_port(validator, exporter_id as usize);
757755
let nickname = format!("block exporter {validator}:{exporter_id}");
758756
Self::ensure_grpc_server_has_started(&nickname, port, "http").await?;
759757
}
760758
Network::Grpcs => {
761-
let port = Self::block_exporter_port(validator, exporter_id as usize);
759+
let port = self.block_exporter_port(validator, exporter_id as usize);
762760
let nickname = format!("block exporter {validator}:{exporter_id}");
763761
Self::ensure_grpc_server_has_started(&nickname, port, "https").await?;
764762
}
@@ -884,7 +882,7 @@ impl LocalNet {
884882
.args(self.cross_chain_config.to_args());
885883
let child = command.spawn_into()?;
886884

887-
let port = Self::shard_port(validator, shard);
885+
let port = self.shard_port(validator, shard);
888886
let nickname = format!("validator server {validator}:{shard}");
889887
match self.network.internal {
890888
Network::Grpc => {
@@ -962,7 +960,7 @@ impl LocalNet {
962960
/// Returns the address to connect to a validator's proxy.
963961
/// In local networks, the zeroth proxy _is_ the validator ingress.
964962
pub fn validator_address(&self, validator: usize) -> String {
965-
let port = Self::proxy_public_port(validator, 0);
963+
let port = self.proxy_public_port(validator, 0);
966964
let schema = self.network.external.schema();
967965

968966
format!("{schema}:localhost:{port}")

linera-service/src/exporter/tests.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,13 @@ use test_case::test_case;
2727
async fn test_linera_exporter(database: Database, network: Network) -> Result<()> {
2828
tracing::info!("Starting test {}", test_name!());
2929

30+
let num_shards = 1;
31+
let num_initial_validators = 1;
32+
// This is based on the formula for proxy_public_port in local_net.rs
33+
let port = LocalNet::first_public_port() + num_shards;
3034
let destination = Destination::Validator {
3135
endpoint: "127.0.0.1".to_owned(),
32-
port: LocalNet::proxy_public_port(1, 0) as u16,
36+
port: port as u16,
3337
};
3438

3539
let destination_config = DestinationConfig {
@@ -49,8 +53,8 @@ async fn test_linera_exporter(database: Database, network: Network) -> Result<()
4953
};
5054

5155
let config = LocalNetConfig {
52-
num_initial_validators: 1,
53-
num_shards: 1,
56+
num_initial_validators,
57+
num_shards,
5458
block_exporters: ExportersSetup::Local(vec![block_exporter_config]),
5559
..LocalNetConfig::new_test(database, network)
5660
};

linera-service/tests/local_net_tests.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use linera_core::{data_types::ChainInfoQuery, node::ValidatorNode};
2525
use linera_sdk::linera_base_types::AccountSecretKey;
2626
use linera_service::{
2727
cli_wrappers::{
28-
local_net::{get_node_port, Database, LocalNet, LocalNetConfig, ProcessInbox},
28+
local_net::{get_node_port, Database, LocalNetConfig, ProcessInbox},
2929
ClientWrapper, LineraNet, LineraNetConfig, Network,
3030
},
3131
test_name,
@@ -99,7 +99,7 @@ async fn test_end_to_end_reconfiguration(config: LocalNetConfig) -> Result<()> {
9999
let address = format!(
100100
"{}:127.0.0.1:{}",
101101
network.short(),
102-
LocalNet::proxy_public_port(0, 0)
102+
net.proxy_public_port(0, 0)
103103
);
104104
assert_eq!(
105105
client.query_validator(&address).await?,
@@ -124,7 +124,7 @@ async fn test_end_to_end_reconfiguration(config: LocalNetConfig) -> Result<()> {
124124
let address = format!(
125125
"{}:127.0.0.1:{}",
126126
network.short(),
127-
LocalNet::proxy_public_port(4, 0)
127+
net.proxy_public_port(4, 0)
128128
);
129129

130130
assert_eq!(
@@ -136,7 +136,7 @@ async fn test_end_to_end_reconfiguration(config: LocalNetConfig) -> Result<()> {
136136
client
137137
.set_validator(
138138
net.validator_keys(4).unwrap(),
139-
LocalNet::proxy_public_port(4, 0),
139+
net.proxy_public_port(4, 0),
140140
100,
141141
)
142142
.await?;
@@ -154,7 +154,7 @@ async fn test_end_to_end_reconfiguration(config: LocalNetConfig) -> Result<()> {
154154
client
155155
.set_validator(
156156
net.validator_keys(5).unwrap(),
157-
LocalNet::proxy_public_port(5, 0),
157+
net.proxy_public_port(5, 0),
158158
100,
159159
)
160160
.await?;
@@ -291,7 +291,7 @@ async fn test_end_to_end_receipt_of_old_create_committee_messages(
291291
let address = format!(
292292
"{}:127.0.0.1:{}",
293293
network.short(),
294-
LocalNet::proxy_public_port(4, 0)
294+
net.proxy_public_port(4, 0)
295295
);
296296

297297
assert_eq!(
@@ -303,7 +303,7 @@ async fn test_end_to_end_receipt_of_old_create_committee_messages(
303303
client
304304
.set_validator(
305305
net.validator_keys(4).unwrap(),
306-
LocalNet::proxy_public_port(4, 0),
306+
net.proxy_public_port(4, 0),
307307
100,
308308
)
309309
.await?;
@@ -388,7 +388,7 @@ async fn test_end_to_end_receipt_of_old_remove_committee_messages(
388388
let address = format!(
389389
"{}:127.0.0.1:{}",
390390
network.short(),
391-
LocalNet::proxy_public_port(4, 0)
391+
net.proxy_public_port(4, 0)
392392
);
393393

394394
assert_eq!(
@@ -400,7 +400,7 @@ async fn test_end_to_end_receipt_of_old_remove_committee_messages(
400400
client
401401
.set_validator(
402402
net.validator_keys(4).unwrap(),
403-
LocalNet::proxy_public_port(4, 0),
403+
net.proxy_public_port(4, 0),
404404
100,
405405
)
406406
.await?;
@@ -436,7 +436,7 @@ async fn test_end_to_end_receipt_of_old_remove_committee_messages(
436436
let address = format!(
437437
"{}:127.0.0.1:{}",
438438
network.short(),
439-
LocalNet::proxy_public_port(5, 0)
439+
net.proxy_public_port(5, 0)
440440
);
441441

442442
assert_eq!(
@@ -448,7 +448,7 @@ async fn test_end_to_end_receipt_of_old_remove_committee_messages(
448448
client
449449
.set_validator(
450450
net.validator_keys(5).unwrap(),
451-
LocalNet::proxy_public_port(5, 0),
451+
net.proxy_public_port(5, 0),
452452
100,
453453
)
454454
.await?;

0 commit comments

Comments
 (0)