Skip to content

Commit 388270f

Browse files
committed
Add broadcast test to simulator
1 parent 6c8b775 commit 388270f

File tree

5 files changed

+90
-8
lines changed

5 files changed

+90
-8
lines changed

testing/node_test_rig/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ types = { workspace = true }
1111
tempfile = { workspace = true }
1212
eth2 = { workspace = true }
1313
validator_client = { workspace = true }
14-
validator_dir = { workspace = true }
14+
validator_dir = { workspace = true, features = ["insecure_keys"] }
1515
sensitive_url = { workspace = true }
1616
execution_layer = { workspace = true }
1717
tokio = { workspace = true }

testing/node_test_rig/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub use eth2;
2121
pub use execution_layer::test_utils::{
2222
Config as MockServerConfig, MockExecutionConfig, MockServer,
2323
};
24-
pub use validator_client::Config as ValidatorConfig;
24+
pub use validator_client::{ApiTopic, Config as ValidatorConfig};
2525

2626
/// The global timeout for HTTP requests to the beacon node.
2727
const HTTP_TIMEOUT: Duration = Duration::from_secs(8);

testing/simulator/src/eth1_sim.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ use futures::prelude::*;
1010
use node_test_rig::environment::RuntimeContext;
1111
use node_test_rig::{
1212
environment::{EnvironmentBuilder, LoggerConfig},
13-
testing_client_config, testing_validator_config, ClientConfig, ClientGenesis, ValidatorFiles,
13+
testing_client_config, testing_validator_config, ApiTopic, ClientConfig, ClientGenesis,
14+
ValidatorFiles,
1415
};
1516
use rayon::prelude::*;
1617
use sensitive_url::SensitiveUrl;
@@ -159,10 +160,25 @@ pub fn run_eth1_sim(matches: &ArgMatches) -> Result<(), String> {
159160
validator_config.fee_recipient = Some(SUGGESTED_FEE_RECIPIENT.into());
160161
}
161162
println!("Adding validator client {}", i);
162-
network_1
163-
.add_validator_client(validator_config, i, files, i % 2 == 0)
164-
.await
165-
.expect("should add validator");
163+
164+
// Enable broadcast on every 4th node.
165+
if i % 4 == 0 {
166+
validator_config.broadcast_topics = ApiTopic::all();
167+
let beacon_nodes = vec![i, (i + 1) % node_count];
168+
network_1
169+
.add_validator_client_with_fallbacks(
170+
validator_config,
171+
i,
172+
beacon_nodes,
173+
files,
174+
)
175+
.await
176+
} else {
177+
network_1
178+
.add_validator_client(validator_config, i, files, i % 2 == 0)
179+
.await
180+
}
181+
.expect("should add validator");
166182
},
167183
"vc",
168184
);

testing/simulator/src/local_network.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,48 @@ impl<E: EthSpec> LocalNetwork<E> {
270270
Ok(())
271271
}
272272

273+
pub async fn add_validator_client_with_fallbacks(
274+
&self,
275+
mut validator_config: ValidatorConfig,
276+
validator_index: usize,
277+
beacon_nodes: Vec<usize>,
278+
validator_files: ValidatorFiles,
279+
) -> Result<(), String> {
280+
let context = self
281+
.context
282+
.service_context(format!("validator_{}", validator_index));
283+
let self_1 = self.clone();
284+
let mut beacon_node_urls = vec![];
285+
for beacon_node in beacon_nodes {
286+
let socket_addr = {
287+
let read_lock = self.beacon_nodes.read();
288+
let beacon_node = read_lock
289+
.get(beacon_node)
290+
.ok_or_else(|| format!("No beacon node for index {}", beacon_node))?;
291+
beacon_node
292+
.client
293+
.http_api_listen_addr()
294+
.expect("Must have http started")
295+
};
296+
let beacon_node = SensitiveUrl::parse(
297+
format!("http://{}:{}", socket_addr.ip(), socket_addr.port()).as_str(),
298+
)
299+
.unwrap();
300+
beacon_node_urls.push(beacon_node);
301+
}
302+
303+
validator_config.beacon_nodes = beacon_node_urls;
304+
305+
let validator_client = LocalValidatorClient::production_with_insecure_keypairs(
306+
context,
307+
validator_config,
308+
validator_files,
309+
)
310+
.await?;
311+
self_1.validator_clients.write().push(validator_client);
312+
Ok(())
313+
}
314+
273315
/// For all beacon nodes in `Self`, return a HTTP client to access each nodes HTTP API.
274316
pub fn remote_nodes(&self) -> Result<Vec<BeaconNodeHttpClient>, String> {
275317
let beacon_nodes = self.beacon_nodes.read();

validator_client/src/beacon_node_fallback.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,11 +714,35 @@ impl<T: SlotClock, E: EthSpec> BeaconNodeFallback<T, E> {
714714
}
715715

716716
/// Serves as a cue for `BeaconNodeFallback` to tell which requests need to be broadcasted.
717-
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize, EnumString, EnumVariantNames)]
717+
#[derive(Clone, Copy, Debug, PartialEq, Deserialize, Serialize, EnumString, EnumVariantNames)]
718718
#[strum(serialize_all = "kebab-case")]
719719
pub enum ApiTopic {
720720
Attestations,
721721
Blocks,
722722
Subscriptions,
723723
SyncCommittee,
724724
}
725+
726+
impl ApiTopic {
727+
pub fn all() -> Vec<ApiTopic> {
728+
use ApiTopic::*;
729+
vec![Attestations, Blocks, Subscriptions, SyncCommittee]
730+
}
731+
}
732+
733+
#[cfg(test)]
734+
mod test {
735+
use super::*;
736+
use std::str::FromStr;
737+
use strum::VariantNames;
738+
739+
#[test]
740+
fn api_topic_all() {
741+
let all = ApiTopic::all();
742+
assert_eq!(all.len(), ApiTopic::VARIANTS.len());
743+
assert!(ApiTopic::VARIANTS
744+
.iter()
745+
.map(|topic| ApiTopic::from_str(topic).unwrap())
746+
.eq(all.into_iter()));
747+
}
748+
}

0 commit comments

Comments
 (0)