Skip to content

Commit be09ee4

Browse files
committed
fix(testing): remove some non-determinism from p2p
re: #188
1 parent e15b191 commit be09ee4

File tree

5 files changed

+53
-29
lines changed

5 files changed

+53
-29
lines changed

node/testing/src/cluster/mod.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,18 +127,17 @@ impl Cluster {
127127

128128
pub fn add_rust_node(&mut self, testing_config: RustNodeTestingConfig) -> ClusterNodeId {
129129
let node_config = testing_config.clone();
130-
let node_i = self.nodes.len();
130+
let node_id = ClusterNodeId::new_unchecked(self.nodes.len());
131131
let (shutdown_tx, shutdown_rx) = mpsc::channel(1);
132132
let secret_key = P2pSecretKey::from_bytes(match testing_config.peer_id {
133133
TestPeerId::Derived => {
134134
let mut bytes = [0; 32];
135135
let bytes_len = bytes.len();
136-
let i_bytes = node_i.to_be_bytes();
136+
let i_bytes = node_id.index().to_be_bytes();
137137
let i = bytes_len - i_bytes.len();
138138
bytes[i..bytes_len].copy_from_slice(&i_bytes);
139139
bytes
140140
}
141-
TestPeerId::Random => rand::random(),
142141
TestPeerId::Bytes(bytes) => bytes,
143142
});
144143
let pub_key = secret_key.public_key();
@@ -274,7 +273,7 @@ impl Cluster {
274273
if let Some(producer_key) = block_producer_sec_key {
275274
real_service.block_producer_start(producer_key.into());
276275
}
277-
let mut service = NodeTestingService::new(real_service, shutdown_rx);
276+
let mut service = NodeTestingService::new(real_service, node_id, shutdown_rx);
278277
if self.config.all_rust_to_rust_use_webrtc() {
279278
service.set_rust_to_rust_use_webrtc();
280279
}
@@ -314,7 +313,7 @@ impl Cluster {
314313
let node = Node::new(node_config, store);
315314

316315
self.nodes.push(node);
317-
ClusterNodeId::new_unchecked(node_i)
316+
node_id
318317
}
319318

320319
pub fn add_ocaml_node(&mut self, testing_config: OcamlNodeTestingConfig) -> ClusterOcamlNodeId {

node/testing/src/node/rust/config.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ pub enum TestPeerId {
1313
/// runs (e.g. peer_id might be blacklisted).
1414
#[default]
1515
Derived,
16-
Random,
1716
Bytes([u8; 32]),
1817
}
1918

@@ -67,11 +66,6 @@ impl RustNodeTestingConfig {
6766
self
6867
}
6968

70-
pub fn with_random_peer_id(mut self) -> Self {
71-
self.peer_id = TestPeerId::Random;
72-
self
73-
}
74-
7569
pub fn with_peer_id(mut self, bytes: [u8; 32]) -> Self {
7670
self.peer_id = TestPeerId::Bytes(bytes);
7771
self

node/testing/src/scenarios/solo_node/basic_connectivity_accept_incoming.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use node::{
55
event_source::Event,
66
p2p::{connection::outgoing::P2pConnectionOutgoingInitOpts, P2pEvent, PeerId},
77
};
8+
use rand::Rng;
89

910
use crate::{
1011
node::{DaemonJson, OcamlNodeTestingConfig, RustNodeTestingConfig},
@@ -48,7 +49,7 @@ impl SoloNodeBasicConnectivityAcceptIncoming {
4849
.ask_initial_peers_interval(Duration::from_secs(3600))
4950
.max_peers(MAX_PEERS_PER_NODE)
5051
.initial_peers(initial_peers)
51-
.with_random_peer_id();
52+
.with_peer_id(rand::thread_rng().gen());
5253

5354
let node_id = runner.add_rust_node(config);
5455
let node_addr = runner.node(node_id).unwrap().dial_addr();

node/testing/src/service/mod.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ use node::{ActionWithMeta, State};
3636
use openmina_node_native::NodeService;
3737
use redux::Instant;
3838

39+
use crate::cluster::ClusterNodeId;
40+
3941
pub type DynEffects = Box<dyn FnMut(&State, &NodeTestingService, &ActionWithMeta) + Send>;
4042

4143
#[derive(Hash, Ord, PartialOrd, Eq, PartialEq)]
@@ -49,6 +51,7 @@ pub type PendingEventId = RequestId<PendingEventIdType>;
4951

5052
pub struct NodeTestingService {
5153
real: NodeService,
54+
id: ClusterNodeId,
5255
// Use webrtc p2p between Rust nodes.
5356
rust_to_rust_use_webrtc: bool,
5457
monotonic_time: Instant,
@@ -60,9 +63,10 @@ pub struct NodeTestingService {
6063
}
6164

6265
impl NodeTestingService {
63-
pub fn new(real: NodeService, _shutdown: mpsc::Receiver<()>) -> Self {
66+
pub fn new(real: NodeService, id: ClusterNodeId, _shutdown: mpsc::Receiver<()>) -> Self {
6467
Self {
6568
real,
69+
id,
6670
rust_to_rust_use_webrtc: false,
6771
monotonic_time: Instant::now(),
6872
pending_events: PendingRequests::new(),
@@ -201,7 +205,23 @@ impl P2pServiceWebrtcWithLibp2p for NodeTestingService {
201205
}
202206

203207
fn find_random_peer(&mut self) {
204-
self.real.find_random_peer()
208+
use node::p2p::identity::SecretKey as P2pSecretKey;
209+
use node::p2p::service_impl::libp2p::Cmd;
210+
211+
let secret_key = P2pSecretKey::from_bytes({
212+
let mut bytes = [1; 32];
213+
let bytes_len = bytes.len();
214+
let i_bytes = self.id.index().to_be_bytes();
215+
let i = bytes_len - i_bytes.len();
216+
bytes[i..bytes_len].copy_from_slice(&i_bytes);
217+
bytes
218+
});
219+
let peer_id = secret_key.public_key().peer_id();
220+
221+
self.libp2p()
222+
.cmd_sender()
223+
.send(Cmd::FindNode(peer_id.into()))
224+
.unwrap_or_default();
205225
}
206226

207227
fn start_discovery(&mut self, peers: Vec<P2pConnectionOutgoingInitOpts>) {

p2p/src/p2p_event.rs

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -81,35 +81,45 @@ impl fmt::Display for P2pEvent {
8181
Self::Listen(v) => v.fmt(f),
8282
Self::Channel(v) => v.fmt(f),
8383
#[cfg(not(target_arch = "wasm32"))]
84-
Self::Libp2pIdentify(peer_id, addr) => {
85-
write!(f, "{peer_id} {addr}")
84+
Self::Libp2pIdentify(peer_id, _) => {
85+
write!(f, "Libp2pIdentify, {peer_id}")
8686
}
8787
Self::Discovery(v) => v.fmt(f),
8888
}
8989
}
9090
}
9191

92+
fn maddr_ip(addr: &libp2p::Multiaddr) -> String {
93+
use libp2p::multiaddr::Protocol;
94+
addr.iter()
95+
.find_map(|p| match p {
96+
Protocol::Ip4(_)
97+
| Protocol::Ip6(_)
98+
| Protocol::Dns(_)
99+
| Protocol::Dns4(_)
100+
| Protocol::Dns6(_) => Some(p.to_string()),
101+
_ => None,
102+
})
103+
.unwrap_or(String::new())
104+
}
105+
92106
impl fmt::Display for P2pListenEvent {
93107
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94108
write!(f, "Listen, ")?;
95109
match self {
96-
P2pListenEvent::NewListenAddr { listener_id, addr } => {
97-
write!(f, "NewListenAddr, {listener_id}, {addr}")
110+
P2pListenEvent::NewListenAddr { addr, .. } => {
111+
write!(f, "NewListenAddr, {}", maddr_ip(addr))
98112
}
99-
P2pListenEvent::ExpiredListenAddr { listener_id, addr } => {
100-
write!(f, "ExpiredListenAddr, {listener_id}, {addr}")
113+
P2pListenEvent::ExpiredListenAddr { addr, .. } => {
114+
write!(f, "ExpiredListenAddr, {}", maddr_ip(addr))
101115
}
102-
P2pListenEvent::ListenerError { listener_id, error } => {
103-
write!(f, "ListenerError, {listener_id}, {error}")
116+
P2pListenEvent::ListenerError { error, .. } => {
117+
write!(f, "ListenerError, {error}")
104118
}
105119
P2pListenEvent::ListenerClosed {
106-
listener_id,
107-
error: Some(error),
108-
} => write!(f, "ListenerClosed, {listener_id}, {error}"),
109-
P2pListenEvent::ListenerClosed {
110-
listener_id,
111-
error: None,
112-
} => write!(f, "ListenerClosed, {listener_id}"),
120+
error: Some(error), ..
121+
} => write!(f, "ListenerClosed, {error}"),
122+
P2pListenEvent::ListenerClosed { error: None, .. } => write!(f, "ListenerClosed"),
113123
}
114124
}
115125
}

0 commit comments

Comments
 (0)