Skip to content

Commit 626d260

Browse files
committed
refactor(e2e): better naming for aggregators in e2e tests
1 parent 4f015b0 commit 626d260

File tree

5 files changed

+175
-51
lines changed

5 files changed

+175
-51
lines changed

mithril-relay/src/relay/signer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use warp::Filter;
1919
/// Signer relay mode
2020
///
2121
/// The relay mode defines how the relay will behave when it receives a message
22-
#[derive(Debug, Clone, Display, ValueEnum)]
22+
#[derive(Debug, Clone, Display, PartialEq, Eq, ValueEnum)]
2323
#[strum(serialize_all = "mixed_case")]
2424
pub enum SignerRelayMode {
2525
/// Passthrough relay mode

mithril-test-lab/mithril-end-to-end/src/mithril/aggregator.rs

+12-26
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ use tokio::sync::RwLock;
1717

1818
#[derive(Debug)]
1919
pub struct AggregatorConfig<'a> {
20-
pub index: usize,
20+
pub is_master: bool,
21+
pub name: &'a str,
2122
pub server_port: u64,
2223
pub pool_node: &'a PoolNode,
2324
pub cardano_cli_path: &'a Path,
@@ -37,7 +38,8 @@ pub struct AggregatorConfig<'a> {
3738

3839
#[derive(Debug)]
3940
pub struct Aggregator {
40-
index: usize,
41+
is_master: bool,
42+
name_suffix: String,
4143
server_port: u64,
4244
db_directory: PathBuf,
4345
command: Arc<RwLock<MithrilCommand>>,
@@ -128,13 +130,11 @@ impl Aggregator {
128130
env,
129131
&args,
130132
)?;
131-
command.set_log_name(&format!(
132-
"mithril-aggregator-{}",
133-
Self::name_suffix(aggregator_config.index),
134-
));
133+
command.set_log_name(&format!("mithril-aggregator-{}", aggregator_config.name,));
135134

136135
Ok(Self {
137-
index: aggregator_config.index,
136+
is_master: aggregator_config.is_master,
137+
name_suffix: aggregator_config.name.to_string(),
138138
server_port: aggregator_config.server_port,
139139
db_directory: aggregator_config.pool_node.db_path.clone(),
140140
command: Arc::new(RwLock::new(command)),
@@ -144,32 +144,21 @@ impl Aggregator {
144144

145145
pub fn copy_configuration(other: &Aggregator) -> Self {
146146
Self {
147-
index: other.index,
147+
is_master: other.is_master,
148+
name_suffix: other.name_suffix.clone(),
148149
server_port: other.server_port,
149150
db_directory: other.db_directory.clone(),
150151
command: other.command.clone(),
151152
process: RwLock::new(None),
152153
}
153154
}
154155

155-
pub fn index(&self) -> usize {
156-
self.index
157-
}
158-
159156
pub fn is_master(&self) -> bool {
160-
self.index == 0
157+
self.is_master
161158
}
162159

163160
pub fn name(&self) -> String {
164-
format!("mithril-aggregator-{}", Self::name_suffix(self.index))
165-
}
166-
167-
pub fn name_suffix(index: usize) -> String {
168-
if index == 0 {
169-
"master".to_string()
170-
} else {
171-
format!("slave-{}", index)
172-
}
161+
format!("mithril-aggregator-{}", self.name_suffix)
173162
}
174163

175164
pub fn endpoint(&self) -> String {
@@ -190,10 +179,7 @@ impl Aggregator {
190179
pub async fn bootstrap_genesis(&self) -> StdResult<()> {
191180
// Clone the command so we can alter it without affecting the original
192181
let mut command = self.command.write().await;
193-
let command_name = &format!(
194-
"mithril-aggregator-genesis-bootstrap-{}",
195-
Self::name_suffix(self.index),
196-
);
182+
let command_name = &format!("mithril-aggregator-genesis-bootstrap-{}", self.name_suffix,);
197183
command.set_log_name(command_name);
198184

199185
let exit_status = command

mithril-test-lab/mithril-end-to-end/src/mithril/infrastructure.rs

+153-6
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,63 @@ pub struct MithrilInfrastructureConfig {
3737
pub use_era_specific_work_dir: bool,
3838
}
3939

40+
impl MithrilInfrastructureConfig {
41+
pub fn is_master_aggregator(&self, index: usize) -> bool {
42+
assert!(
43+
index < self.number_of_aggregators as usize,
44+
"Aggregator index out of bounds"
45+
);
46+
47+
if self.relay_signer_registration_mode == SignerRelayMode::Passthrough {
48+
self.number_of_aggregators > 1 && index == 0
49+
} else {
50+
false
51+
}
52+
}
53+
54+
pub fn aggregator_name_suffix(&self, index: usize) -> String {
55+
assert!(
56+
index < self.number_of_aggregators as usize,
57+
"Aggregator index out of bounds"
58+
);
59+
60+
if self.is_master_aggregator(0) {
61+
if self.is_master_aggregator(index) {
62+
"master".to_string()
63+
} else {
64+
format!("slave-{index}")
65+
}
66+
} else {
67+
format!("{}", index + 1)
68+
}
69+
}
70+
71+
#[cfg(test)]
72+
pub fn dummy() -> Self {
73+
Self {
74+
number_of_aggregators: 1,
75+
number_of_signers: 1,
76+
server_port: 8080,
77+
devnet: Devnet::default(),
78+
work_dir: PathBuf::from("/tmp/work"),
79+
store_dir: PathBuf::from("/tmp/store"),
80+
artifacts_dir: PathBuf::from("/tmp/artifacts"),
81+
bin_dir: PathBuf::from("/tmp/bin"),
82+
cardano_node_version: "1.0.0".to_string(),
83+
mithril_run_interval: 10,
84+
mithril_era: "era1".to_string(),
85+
mithril_era_reader_adapter: "adapter1".to_string(),
86+
signed_entity_types: vec!["type1".to_string()],
87+
run_only_mode: false,
88+
use_relays: false,
89+
relay_signer_registration_mode: SignerRelayMode::Passthrough,
90+
relay_signature_registration_mode: SignerRelayMode::Passthrough,
91+
use_p2p_passive_relays: false,
92+
use_era_specific_work_dir: false,
93+
}
94+
}
95+
}
96+
4097
pub struct MithrilInfrastructure {
4198
artifacts_dir: PathBuf,
4299
bin_dir: PathBuf,
@@ -186,7 +243,8 @@ impl MithrilInfrastructure {
186243
};
187244

188245
let aggregator = Aggregator::new(&AggregatorConfig {
189-
index,
246+
is_master: config.is_master_aggregator(index),
247+
name: &config.aggregator_name_suffix(index),
190248
server_port: config.server_port + index as u64,
191249
pool_node,
192250
cardano_cli_path: &config.devnet.cardano_cli_path(),
@@ -246,7 +304,7 @@ impl MithrilInfrastructure {
246304
let mut bootstrap_peer_addr = None;
247305
for (index, aggregator_endpoint) in aggregator_endpoints.iter().enumerate() {
248306
let mut relay_aggregator = RelayAggregator::new(
249-
index,
307+
config.aggregator_name_suffix(index),
250308
config.server_port + index as u64 + 100,
251309
bootstrap_peer_addr.clone(),
252310
aggregator_endpoint,
@@ -424,10 +482,9 @@ impl MithrilInfrastructure {
424482

425483
pub async fn build_client(&self, aggregator: &Aggregator) -> StdResult<Client> {
426484
let work_dir = {
427-
let mut artifacts_dir = self.artifacts_dir.join(format!(
428-
"mithril-client-aggregator-{}",
429-
Aggregator::name_suffix(aggregator.index())
430-
));
485+
let mut artifacts_dir = self
486+
.artifacts_dir
487+
.join(format!("mithril-client-aggregator-{}", aggregator.name()));
431488
if self.use_era_specific_work_dir {
432489
let current_era = self.current_era.read().await;
433490
artifacts_dir = artifacts_dir.join(format!("era.{}", current_era));
@@ -479,3 +536,93 @@ impl MithrilInfrastructure {
479536
Ok(())
480537
}
481538
}
539+
540+
#[cfg(test)]
541+
mod tests {
542+
use crate::MithrilInfrastructureConfig;
543+
544+
use super::*;
545+
546+
#[test]
547+
fn is_master_aggregator_succeeds() {
548+
let config = MithrilInfrastructureConfig {
549+
use_relays: true,
550+
relay_signer_registration_mode: SignerRelayMode::Passthrough,
551+
number_of_aggregators: 2,
552+
..MithrilInfrastructureConfig::dummy()
553+
};
554+
555+
assert!(config.is_master_aggregator(0));
556+
assert!(!config.is_master_aggregator(1));
557+
558+
let config = MithrilInfrastructureConfig {
559+
use_relays: true,
560+
relay_signer_registration_mode: SignerRelayMode::P2P,
561+
number_of_aggregators: 2,
562+
..MithrilInfrastructureConfig::dummy()
563+
};
564+
565+
assert!(!config.is_master_aggregator(0));
566+
assert!(!config.is_master_aggregator(1));
567+
568+
let config = MithrilInfrastructureConfig {
569+
use_relays: false,
570+
number_of_aggregators: 1,
571+
..MithrilInfrastructureConfig::dummy()
572+
};
573+
574+
assert!(!config.is_master_aggregator(0));
575+
}
576+
577+
#[test]
578+
#[should_panic]
579+
fn is_master_aggregator_fails() {
580+
let config = MithrilInfrastructureConfig {
581+
number_of_aggregators: 1,
582+
..MithrilInfrastructureConfig::dummy()
583+
};
584+
585+
config.is_master_aggregator(2);
586+
}
587+
588+
#[test]
589+
fn aggregator_name_suffix_succeeds() {
590+
let config = MithrilInfrastructureConfig {
591+
relay_signer_registration_mode: SignerRelayMode::Passthrough,
592+
number_of_aggregators: 3,
593+
..MithrilInfrastructureConfig::dummy()
594+
};
595+
596+
assert_eq!(config.aggregator_name_suffix(0), "master");
597+
assert_eq!(config.aggregator_name_suffix(1), "slave-1");
598+
assert_eq!(config.aggregator_name_suffix(2), "slave-2");
599+
600+
let config = MithrilInfrastructureConfig {
601+
number_of_aggregators: 3,
602+
relay_signer_registration_mode: SignerRelayMode::P2P,
603+
..MithrilInfrastructureConfig::dummy()
604+
};
605+
606+
assert_eq!(config.aggregator_name_suffix(0), "1");
607+
assert_eq!(config.aggregator_name_suffix(1), "2");
608+
assert_eq!(config.aggregator_name_suffix(2), "3");
609+
610+
let config = MithrilInfrastructureConfig {
611+
number_of_aggregators: 1,
612+
..MithrilInfrastructureConfig::dummy()
613+
};
614+
615+
assert_eq!(config.aggregator_name_suffix(0), "1");
616+
}
617+
618+
#[test]
619+
#[should_panic]
620+
fn aggregator_name_suffix_fails() {
621+
let config = MithrilInfrastructureConfig {
622+
number_of_aggregators: 1,
623+
..MithrilInfrastructureConfig::dummy()
624+
};
625+
626+
config.aggregator_name_suffix(2);
627+
}
628+
}

mithril-test-lab/mithril-end-to-end/src/mithril/relay_aggregator.rs

+7-17
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ use tokio::process::Child;
66

77
#[derive(Debug)]
88
pub struct RelayAggregator {
9-
index: usize,
9+
name_suffix: String,
1010
listen_port: u64,
1111
command: MithrilCommand,
1212
process: Option<Child>,
1313
}
1414

1515
impl RelayAggregator {
1616
pub fn new(
17-
index: usize,
17+
name: String,
1818
listen_port: u64,
1919
dial_to: Option<String>,
2020
aggregator_endpoint: &str,
@@ -32,13 +32,10 @@ impl RelayAggregator {
3232
let args = vec!["-vvv", "aggregator"];
3333

3434
let mut command = MithrilCommand::new("mithril-relay", work_dir, bin_dir, env, &args)?;
35-
command.set_log_name(&format!(
36-
"mithril-relay-aggregator-{}",
37-
Self::name_suffix(index),
38-
));
35+
command.set_log_name(&format!("mithril-relay-aggregator-{}", name,));
3936

4037
Ok(Self {
41-
index,
38+
name_suffix: name,
4239
listen_port,
4340
command,
4441
process: None,
@@ -49,12 +46,8 @@ impl RelayAggregator {
4946
format!("/ip4/127.0.0.1/tcp/{}", self.listen_port)
5047
}
5148

52-
pub fn name_suffix(index: usize) -> String {
53-
if index == 0 {
54-
"master".to_string()
55-
} else {
56-
format!("slave-{}", index)
57-
}
49+
pub fn name_suffix(&self) -> String {
50+
self.name_suffix.clone()
5851
}
5952

6053
pub fn start(&mut self) -> StdResult<()> {
@@ -65,10 +58,7 @@ impl RelayAggregator {
6558
pub async fn tail_logs(&self, number_of_line: u64) -> StdResult<()> {
6659
self.command
6760
.tail_logs(
68-
Some(&format!(
69-
"mithril-relay-aggregator-{}",
70-
Self::name_suffix(self.index),
71-
)),
61+
Some(&format!("mithril-relay-aggregator-{}", self.name_suffix())),
7262
number_of_line,
7363
)
7464
.await

mithril-test-lab/mithril-end-to-end/src/stress_test/aggregator_helpers.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ pub async fn bootstrap_aggregator(
1919
let chain_observer_type = "cardano-cli";
2020

2121
let mut aggregator = Aggregator::new(&AggregatorConfig {
22-
index: 0,
22+
is_master: false,
23+
name: "genesis",
2324
server_port: args.server_port as u64,
2425
pool_node: &args.pool_node,
2526
cardano_cli_path: &args.cardano_cli_path,

0 commit comments

Comments
 (0)