Skip to content

Commit 87c3fd3

Browse files
authored
Merge pull request #1009 from openmina/fix/p2p/webrtc/seed_peers_gone
P2P WebRTC Fixes
2 parents 351de56 + 9e7a0b2 commit 87c3fd3

File tree

15 files changed

+93
-48
lines changed

15 files changed

+93
-48
lines changed

core/src/log.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ pub use crate::{debug, error, info, trace, warn};
164164
macro_rules! bug_condition {
165165
($($arg:tt)*) => {{
166166
if std::env::var("OPENMINA_PANIC_ON_BUG")
167-
.map(|v| v.to_lowercase() == "true")
167+
.map(|v| ["true", "1"].contains(&v.to_lowercase().as_str()))
168168
.unwrap_or(false) {
169169
panic!($($arg)*)
170170
} else {

node/native/src/node/builder.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ impl NodeBuilder {
301301
.p2p
302302
.initial_peers
303303
.into_iter()
304+
.filter(|opts| *opts.peer_id() != p2p_sec_key.public_key().peer_id())
304305
.filter_map(|opts| match opts {
305306
P2pConnectionOutgoingInitOpts::LibP2P(mut opts) => {
306307
opts.host = opts.host.resolve()?;

node/testing/src/node/rust/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ impl Node {
187187
false
188188
}
189189

190-
pub fn p2p_disconnect(&mut self, peer_id: PeerId) {
190+
pub fn p2p_disconnect(&mut self, peer_id: PeerId) -> bool {
191191
self.service_mut().disconnect(peer_id)
192192
}
193193
}

node/testing/src/scenarios/record_replay/block_production.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl RecordReplayBlockProduction {
3535
block_producers: 3,
3636
advance_time: RunCfgAdvanceTime::Rand(1..=200),
3737
run_until: SimulatorRunUntil::BlockchainLength(10),
38-
run_until_timeout: Duration::from_secs(10 * 60),
38+
run_until_timeout: Duration::from_secs(15 * 60),
3939
recorder: Recorder::StateWithInputActions,
4040
};
4141
let mut simulator = Simulator::new(initial_time, cfg);

node/web/src/node/builder.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ impl NodeBuilder {
202202
} else {
203203
self.initial_peers
204204
};
205+
let initial_peers = initial_peers
206+
.into_iter()
207+
.filter(|opts| *opts.peer_id() != p2p_sec_key.public_key().peer_id())
208+
.collect();
205209

206210
let srs = self.verifier_srs.unwrap_or_else(get_srs);
207211
let block_verifier_index = self

p2p/src/disconnection/p2p_disconnection_reducer.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,18 @@ impl P2pDisconnectedState {
123123

124124
let (dispatcher, state) = state_context.into_dispatcher_and_state();
125125
let p2p_state: &P2pState = state.substate()?;
126-
dispatcher.push(P2pPeerAction::Remove { peer_id });
126+
127+
// remove oldest disconnected peer
128+
if let Some((peer_id, _)) = p2p_state
129+
.peers
130+
.iter()
131+
.filter_map(|(id, p)| {
132+
Some((*id, p.status.disconnected_or_disconnecting_time()?))
133+
})
134+
.min_by_key(|(_, t)| *t)
135+
{
136+
dispatcher.push(P2pPeerAction::Remove { peer_id });
137+
}
127138

128139
if let Some(callback) = &p2p_state.callbacks.on_p2p_disconnection_finish {
129140
dispatcher.push_callback(callback.clone(), peer_id);

p2p/src/disconnection_effectful/p2p_disconnection_effectful_effects.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use redux::ActionMeta;
22

3+
use crate::disconnection::P2pDisconnectionAction;
4+
35
use super::{P2pDisconnectionEffectfulAction, P2pDisconnectionService};
46

57
impl P2pDisconnectionEffectfulAction {
@@ -10,7 +12,10 @@ impl P2pDisconnectionEffectfulAction {
1012
{
1113
match self {
1214
P2pDisconnectionEffectfulAction::Init { peer_id } => {
13-
store.service().disconnect(peer_id);
15+
if store.service().disconnect(peer_id) {
16+
// peer was already disconnected, so dispatch finish.
17+
store.dispatch(P2pDisconnectionAction::Finish { peer_id });
18+
}
1419
}
1520
}
1621
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::PeerId;
22

33
pub trait P2pDisconnectionService: redux::Service {
4-
fn disconnect(&mut self, peer_id: PeerId);
4+
fn disconnect(&mut self, peer_id: PeerId) -> bool;
55
}

p2p/src/p2p_config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl Default for P2pTimeouts {
102102
"OUTGOING_CONNECTION_TIMEOUT",
103103
Some(Duration::from_secs(15)),
104104
),
105-
reconnect_timeout: from_env_or("RECONNECT_TIMEOUT", Some(Duration::from_secs(1))),
105+
reconnect_timeout: from_env_or("RECONNECT_TIMEOUT", Some(Duration::from_secs(30))),
106106
incoming_error_reconnect_timeout: from_env_or(
107107
"INCOMING_ERROR_RECONNECT_TIMEOUT",
108108
Some(Duration::from_secs(30)),

p2p/src/p2p_reducer.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ impl P2pState {
8383
dispatcher.push(P2pConnectionOutgoingAction::RandomInit);
8484
dispatcher.push(P2pDisconnectionAction::RandomTry);
8585

86+
state.p2p_connect_initial_peers(dispatcher);
8687
state.p2p_try_reconnect_disconnected_peers(dispatcher, time)?;
8788
state.p2p_discovery(dispatcher, time)?;
8889

@@ -153,6 +154,29 @@ impl P2pState {
153154
Ok(())
154155
}
155156

157+
fn p2p_connect_initial_peers<State, Action>(&self, dispatcher: &mut Dispatcher<Action, State>)
158+
where
159+
State: crate::P2pStateTrait,
160+
Action: crate::P2pActionTrait<State>,
161+
{
162+
if self.ready_peers_iter().count() >= self.config.initial_peers.len() {
163+
return;
164+
}
165+
166+
self.config
167+
.initial_peers
168+
.iter()
169+
.filter(|opts| !self.peers.contains_key(opts.peer_id()))
170+
.cloned()
171+
.for_each(|opts| {
172+
dispatcher.push(P2pConnectionOutgoingAction::Init {
173+
opts,
174+
rpc_id: None,
175+
on_success: None,
176+
});
177+
});
178+
}
179+
156180
fn rpc_timeouts<State, Action>(
157181
&self,
158182
dispatcher: &mut Dispatcher<Action, State>,

0 commit comments

Comments
 (0)