Skip to content

Commit 9033737

Browse files
committed
Added exit if initial peers are invalid
1 parent 9f18cef commit 9033737

File tree

7 files changed

+46
-5
lines changed

7 files changed

+46
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2121
Dashboard, documenting all endpoints and specific data fields used by the
2222
frontend ([#1566](https://github.com/o1-labs/mina-rust/issues/1566))
2323

24+
- **FEATURE**: Add logic to exit the node if initial peers are invalid
25+
([#1703](https://github.com/o1-labs/mina-rust/pull/1703))
26+
2427
### Fixed
2528

2629
- **Docker Compose**: Fix frontend black screen issue by changing environment

node/src/action.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ pub enum Action {
5757
RpcEffectful(RpcEffectfulAction),
5858

5959
WatchedAccounts(WatchedAccountsAction),
60+
Exit(ExitAction),
6061
}
6162

6263
impl Action {
@@ -71,7 +72,11 @@ impl Action {
7172
#[derive(Serialize, Deserialize, Debug, Clone)]
7273
pub struct CheckTimeoutsAction {}
7374

75+
#[derive(Serialize, Deserialize, Debug, Clone)]
76+
pub struct ExitAction {}
77+
7478
impl redux::EnablingCondition<crate::State> for CheckTimeoutsAction {}
79+
impl redux::EnablingCondition<crate::State> for ExitAction {}
7580

7681
impl redux::EnablingCondition<crate::State> for Action {
7782
fn is_enabled(&self, state: &crate::State, time: redux::Timestamp) -> bool {
@@ -105,6 +110,7 @@ impl redux::EnablingCondition<crate::State> for Action {
105110
Action::TransactionPoolEffect(a) => a.is_enabled(state, time),
106111
Action::P2pCallbacks(a) => a.is_enabled(state, time),
107112
Action::RpcEffectful(a) => a.is_enabled(state, time),
113+
Action::Exit(a) => a.is_enabled(state, time),
108114
}
109115
}
110116
}

node/src/action_kind.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ use crate::{
107107
TransitionFrontierAction,
108108
},
109109
watched_accounts::WatchedAccountsAction,
110-
Action, ActionKindGet, CheckTimeoutsAction,
110+
Action, ActionKindGet, CheckTimeoutsAction, ExitAction,
111111
};
112112

113113
/// Unified kind enum for all action types
@@ -173,6 +173,7 @@ pub enum ActionKind {
173173
BlockProducerVrfEvaluatorEffectfulInitializeStats,
174174
BlockProducerVrfEvaluatorEffectfulSlotEvaluated,
175175
CheckTimeouts,
176+
Exit,
176177
EventSourceNewEvent,
177178
EventSourceProcessEvents,
178179
EventSourceWaitForEvents,
@@ -757,7 +758,7 @@ pub enum ActionKind {
757758
}
758759

759760
impl ActionKind {
760-
pub const COUNT: u16 = 628;
761+
pub const COUNT: u16 = 629;
761762
}
762763

763764
impl std::fmt::Display for ActionKind {
@@ -789,6 +790,7 @@ impl ActionKindGet for Action {
789790
Self::Rpc(a) => a.kind(),
790791
Self::RpcEffectful(a) => a.kind(),
791792
Self::WatchedAccounts(a) => a.kind(),
793+
Self::Exit(a) => a.kind(),
792794
}
793795
}
794796
}
@@ -799,6 +801,12 @@ impl ActionKindGet for CheckTimeoutsAction {
799801
}
800802
}
801803

804+
impl ActionKindGet for ExitAction {
805+
fn kind(&self) -> ActionKind {
806+
ActionKind::Exit
807+
}
808+
}
809+
802810
impl ActionKindGet for EventSourceAction {
803811
fn kind(&self) -> ActionKind {
804812
match self {

node/src/effects.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ pub fn effects<S: Service>(store: &mut Store<S>, action: ActionWithMeta) {
102102
| Action::Rpc(_)
103103
| Action::WatchedAccounts(_)
104104
| Action::P2pCallbacks(_)
105-
| Action::P2p(_) => {
105+
| Action::P2p(_)
106+
| Action::Exit(_) => {
106107
// Handled by reducer
107108
}
108109
}

node/src/reducer.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::process;
2+
13
use mina_core::{bug_condition, error, Substate};
24
use p2p::{P2pAction, P2pEffectfulAction, P2pInitializeAction, P2pState};
35

@@ -6,7 +8,7 @@ use crate::{
68
rpc::RpcState,
79
state::{BlockProducerState, LedgerState},
810
transition_frontier::candidate::TransitionFrontierCandidateAction,
9-
Action, ActionWithMeta, EventSourceAction, P2p, State,
11+
Action, ActionWithMeta, EventSourceAction, ExitAction, P2p, State,
1012
};
1113

1214
pub fn reducer(
@@ -17,7 +19,17 @@ pub fn reducer(
1719
let meta = action.meta().clone();
1820
match action.action() {
1921
Action::CheckTimeouts(_) => {
20-
if state.p2p.ready().is_some() {
22+
if let Some(p2p) = state.p2p.ready() {
23+
if let Some(kad_state) = &p2p.network.scheduler.discovery_state {
24+
if p2p.ready_peers().is_empty() && kad_state.has_bootstraped {
25+
if let Some(tip) = &state.transition_frontier.best_tip_breadcrumb() {
26+
if dbg!(tip.height()) == 296372 {
27+
dispatcher.push(ExitAction {});
28+
}
29+
}
30+
}
31+
}
32+
2133
if let Err(error) =
2234
P2pState::p2p_timeout_dispatch(Substate::new(state, dispatcher), &meta)
2335
{
@@ -108,6 +120,14 @@ pub fn reducer(
108120
Action::P2pCallbacks(action) => {
109121
State::p2p_callback_reducer(Substate::new(state, dispatcher), meta.with_action(action))
110122
}
123+
Action::Exit(_) => {
124+
crate::core::error!(
125+
crate::core::log::system_time();
126+
summary = "Exiting",
127+
error = "Invalid initial peers"
128+
);
129+
process::exit(1);
130+
}
111131
}
112132

113133
// must be the last.

p2p/src/network/kad/p2p_network_kad_reducer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ impl super::P2pNetworkKadState {
133133
time: meta.time(),
134134
stats: bootstrap_state.stats.clone(),
135135
};
136+
state.has_bootstraped = true;
136137
Ok(())
137138
}
138139
(_, P2pNetworkKademliaAction::UpdateRoutingTable { peer_id, addrs }) => {

p2p/src/network/kad/p2p_network_kad_state.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ pub struct P2pNetworkKadState {
6060
pub streams: StreamState<P2pNetworkKadStreamState>,
6161
pub status: P2pNetworkKadStatus,
6262
pub filter_addrs: bool,
63+
pub has_bootstraped: bool,
6364
}
6465

6566
impl Default for P2pNetworkKadState {
@@ -74,6 +75,7 @@ impl Default for P2pNetworkKadState {
7475
.ok()
7576
.and_then(|s| s.parse().ok())
7677
.unwrap_or(true),
78+
has_bootstraped: false,
7779
}
7880
}
7981
}

0 commit comments

Comments
 (0)