Skip to content

Commit 054d99f

Browse files
authored
Merge pull request #701 from pragma-org/etorreborre/test/fix-flaky-simulation-2
test: fix simulation issues
2 parents b23ec51 + 3bb754d commit 054d99f

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

crates/amaru/src/tests/setup.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use amaru_ouroboros::{
2424
can_validate_blocks::mock::{MockCanValidateBlocks, MockCanValidateHeaders},
2525
};
2626
use amaru_protocols::{manager::ManagerMessage, store_effects::Store};
27+
use anyhow::anyhow;
2728
use pure_stage::{
2829
Effects, StageGraph, StageRef, TryInStage,
2930
simulation::{RandStdRng, SimulationBuilder},
@@ -79,7 +80,8 @@ pub fn create_node(
7980
// in order to simulate what happens when new tips are added and trigger a move of the best
8081
// chain anchor.
8182
global_parameters.consensus_security_param = node_config.chain_length;
82-
let manager_stage = build_node(&config, &global_parameters, None, stage_graph)?;
83+
let manager_stage = build_node(&config, &global_parameters, None, stage_graph)
84+
.map_err(|e| anyhow!("Cannot build node.\nThe node config is\n{:?}\n\nThe error is {e:?}", node_config))?;
8385

8486
// The actions stage allows us to send NewTip messages to the manager so that chainsync
8587
// events can be sent to the node under test.

simulation/amaru-sim/src/simulator/run_config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,11 @@ impl RunConfig {
9393
}
9494

9595
pub fn upstream_peers(&self) -> Vec<Peer> {
96-
(1..=self.number_of_upstream_peers).map(|i| Peer::new(&format!("127.0.0.1:300{i}"))).collect()
96+
(1..=self.number_of_upstream_peers as u16).map(|i| Peer::new(&format!("127.0.0.1:{}", 30000 + i))).collect()
9797
}
9898

9999
pub fn downstream_peers(&self) -> Vec<Peer> {
100-
(1..=self.number_of_downstream_peers).map(|i| Peer::new(&format!("127.0.0.1:400{i}"))).collect()
100+
(1..=self.number_of_downstream_peers as u16).map(|i| Peer::new(&format!("127.0.0.1:{}", 40000 + i))).collect()
101101
}
102102

103103
pub fn rng(&self) -> RandStdRng {

simulation/amaru-sim/src/simulator/run_tests.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,14 @@ pub fn run_test(run_config: &RunConfig, actions: &GeneratedActions) -> TestResul
8989
let mut rng = run_config.rng();
9090
let mut nodes = create_nodes(&mut rng, node_configs(run_config, actions)).expect("failed to create nodes");
9191

92-
// Scale steps based on number of peers (more peers = more stages = more effects)
93-
let base_steps = 10000;
94-
let per_peer_steps = 2000;
95-
let total_peers = run_config.number_of_upstream_peers as usize + run_config.number_of_downstream_peers as usize;
96-
let steps = base_steps + (total_peers * per_peer_steps);
92+
// Scale steps based on the number of nodes in the system.
93+
// Each step runs one effect on one randomly-selected node, so with N nodes
94+
// each node gets ~total_steps/N turns. We multiply by total_nodes to ensure
95+
// every node (especially the node under test) gets enough turns.
96+
let upstream = run_config.number_of_upstream_peers as usize;
97+
let downstream = run_config.number_of_downstream_peers as usize;
98+
let total_nodes = upstream + downstream + 1;
99+
let steps = total_nodes * (5000 + upstream * 500);
97100

98101
nodes.run(&mut rng, steps);
99102
check_chain_property(nodes, actions)
@@ -117,7 +120,12 @@ pub fn run_test(run_config: &RunConfig, actions: &GeneratedActions) -> TestResul
117120
pub fn node_configs(run_config: &RunConfig, actions: &GeneratedActions) -> Vec<NodeTestConfig> {
118121
let upstream_peers = run_config.upstream_peers();
119122

120-
let upstream_nodes = upstream_peers
123+
// Only create nodes for peers that have actions. During shrinking, some peers may lose
124+
// all their actions, so we skip creating nodes for them.
125+
let active_peers: Vec<_> =
126+
upstream_peers.iter().filter(|peer| !get_peer_actions(actions, peer).is_empty()).cloned().collect();
127+
128+
let upstream_nodes = active_peers
121129
.iter()
122130
.map(|peer| {
123131
NodeTestConfig::default()
@@ -135,7 +143,7 @@ pub fn node_configs(run_config: &RunConfig, actions: &GeneratedActions) -> Vec<N
135143
let node_under_test = NodeTestConfig::default()
136144
.with_listen_address(listen_address)
137145
.with_chain_length(run_config.generated_chain_depth)
138-
.with_upstream_peers(upstream_peers)
146+
.with_upstream_peers(active_peers)
139147
.with_trace_buffer(trace_buffer)
140148
.with_validated_blocks(vec![actions.get_anchor()])
141149
.with_node_type(NodeUnderTest);

0 commit comments

Comments
 (0)