Skip to content

Commit

Permalink
fixup: Update substate access to handle p2p readiness
Browse files Browse the repository at this point in the history
  • Loading branch information
tizoc committed May 28, 2024
1 parent cfb3bd4 commit 1404d69
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 25 deletions.
8 changes: 7 additions & 1 deletion node/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ impl redux::EnablingCondition<crate::State> for Action {
match self {
Action::CheckTimeouts(a) => a.is_enabled(state, time),
Action::EventSource(a) => a.is_enabled(state, time),
Action::P2p(a) => a.is_enabled(&state.p2p, time),
Action::P2p(a) => match a {
P2pAction::Initialization(a) => a.is_enabled(state, time),
other => state
.p2p
.ready()
.map_or(false, |p2p| other.is_enabled(p2p, time)),
},
Action::Ledger(a) => a.is_enabled(state, time),
Action::Snark(a) => a.is_enabled(&state.snark, time),
Action::Consensus(a) => a.is_enabled(state, time),
Expand Down
38 changes: 16 additions & 22 deletions node/src/reducer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,22 @@ pub fn reducer(
},

Check warning on line 16 in node/src/reducer.rs

View workflow job for this annotation

GitHub Actions / clippy

this match could be replaced by its body itself

warning: this match could be replaced by its body itself --> node/src/reducer.rs:14:71 | 14 | Action::EventSource(EventSourceAction::NewEvent { event }) => match event { | _______________________________________________________________________^ 15 | | _ => {} 16 | | }, | |_________^ help: consider using the match body instead: `{}` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#match_single_binding = note: `#[warn(clippy::match_single_binding)]` on by default

Check warning on line 16 in node/src/reducer.rs

View workflow job for this annotation

GitHub Actions / clippy

this match could be replaced by its body itself

warning: this match could be replaced by its body itself --> node/src/reducer.rs:14:71 | 14 | Action::EventSource(EventSourceAction::NewEvent { event }) => match event { | _______________________________________________________________________^ 15 | | _ => {} 16 | | }, | |_________^ help: consider using the match body instead: `{}` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#match_single_binding = note: `#[warn(clippy::match_single_binding)]` on by default
Action::EventSource(_) => {}

Action::P2p(a) => {
p2p::P2pState::reducer(Substate::new(state, dispatcher), meta.with_action(a));
}
Action::P2p(a) => match a {
P2pAction::Initialization(P2pInitializeAction::Initialize { chain_id }) => {
if let Err(err) = state.p2p.initialize(chain_id) {
error!(meta.time(); summary = "error initializing p2p", error = display(err));
}
}
action => match &mut state.p2p {
P2p::Pending(_) => {
error!(meta.time(); summary = "p2p is not initialized", action = debug(action))
}
P2p::Ready(_) => p2p::P2pState::reducer(
Substate::new(state, dispatcher),
meta.with_action(action),
),
},
},
Action::Ledger(a) => {
state.ledger.reducer(meta.with_action(a));
}
Expand Down Expand Up @@ -66,22 +79,3 @@ pub fn reducer(
// must be the last.
state.action_applied(action);
}

impl P2p {
fn reduce(&mut self, action: redux::ActionWithMeta<&P2pAction>) {
let (action, meta) = action.split();
match action {
P2pAction::Initialization(P2pInitializeAction::Initialize { chain_id }) => {
if let Err(err) = self.initialize(chain_id) {
error!(meta.time(); summary = "error initializing p2p", error = display(err));
}
}
action => match self {
P2p::Pending(_) => {
error!(meta.time(); summary = "p2p is not initialized", action = debug(action))
}
P2p::Ready(p2p_state) => p2p_state.reducer(meta.with_action(action)),
},
}
}
}
30 changes: 28 additions & 2 deletions node/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ pub struct State {
// Substate accessors that will be used in reducers
use openmina_core::impl_substate_access;

impl_substate_access!(State, P2pState, p2p);
impl_substate_access!(State, p2p::P2pNetworkState, p2p.network);
impl_substate_access!(State, SnarkState, snark);
impl_substate_access!(State, SnarkBlockVerifyState, snark.block_verify);
impl_substate_access!(State, SnarkWorkVerifyState, snark.work_verify);
Expand All @@ -69,6 +67,26 @@ impl_substate_access!(State, BlockProducerState, block_producer);
impl_substate_access!(State, RpcState, rpc);
impl_substate_access!(State, WatchedAccountsState, watched_accounts);

impl openmina_core::SubstateAccess<P2pState> for State {
fn substate(&self) -> &P2pState {
self.p2p.ready().unwrap()
}

fn substate_mut(&mut self) -> &mut P2pState {
self.p2p.ready_mut().unwrap()
}
}

impl openmina_core::SubstateAccess<p2p::P2pNetworkState> for State {
fn substate(&self) -> &p2p::P2pNetworkState {
&self.p2p.ready().unwrap().network
}

fn substate_mut(&mut self) -> &mut p2p::P2pNetworkState {
&mut self.p2p.ready_mut().unwrap().network
}
}

impl openmina_core::SubstateAccess<TransitionFrontierSyncLedgerState> for State {
fn substate(&self) -> &TransitionFrontierSyncLedgerState {
self.transition_frontier.sync.ledger().unwrap()
Expand Down Expand Up @@ -230,6 +248,14 @@ impl P2p {
}
}

pub fn ready_mut(&mut self) -> Option<&mut P2pState> {
if let P2p::Ready(state) = self {
Some(state)
} else {
None
}
}

pub fn unwrap(&self) -> &P2pState {
self.ready().expect("p2p is not initialized")
}
Expand Down

0 comments on commit 1404d69

Please sign in to comment.