Skip to main content

mina_node/
reducer.rs

1use crate::p2p::{P2pAction, P2pEffectfulAction, P2pInitializeAction, P2pState};
2use mina_core::{bug_condition, error, Substate};
3
4use crate::{
5    external_snark_worker::ExternalSnarkWorkers,
6    rpc::RpcState,
7    state::{BlockProducerState, LedgerState},
8    transition_frontier::candidate::TransitionFrontierCandidateAction,
9    Action, ActionWithMeta, EventSourceAction, P2p, State,
10};
11
12pub fn reducer(
13    state: &mut State,
14    action: &ActionWithMeta,
15    dispatcher: &mut redux::Dispatcher<Action, State>,
16) {
17    let meta = action.meta().clone();
18    match action.action() {
19        Action::CheckTimeouts(_) => {
20            if state.p2p.ready().is_some() {
21                if let Err(error) =
22                    P2pState::p2p_timeout_dispatch(Substate::new(state, dispatcher), &meta)
23                {
24                    bug_condition!("{}", error);
25                };
26            }
27            dispatcher.push(TransitionFrontierCandidateAction::TransitionFrontierSyncTargetUpdate);
28        }
29        Action::EventSource(EventSourceAction::NewEvent { .. }) => {}
30        Action::EventSource(_) => {}
31        Action::P2p(a) => match a {
32            P2pAction::Initialization(P2pInitializeAction::Initialize { chain_id }) => {
33                if let Err(err) = state.p2p.initialize(chain_id) {
34                    error!(meta.time(); summary = "error initializing p2p", error = display(err));
35                }
36                dispatcher.push(P2pEffectfulAction::Initialize);
37            }
38            p2p_action => match &mut state.p2p {
39                P2p::Pending(_) => {
40                    error!(meta.time(); summary = "p2p is not initialized", action = debug(p2p_action))
41                }
42                P2p::Ready(_) => {
43                    let time = meta.time();
44                    let result = crate::p2p::P2pState::reducer(
45                        Substate::new(state, dispatcher),
46                        meta.with_action(p2p_action.clone()),
47                    );
48
49                    if let Err(error) = result {
50                        use crate::ActionKindGet as _;
51                        error!(time;
52                            summary = "Failure when handling a P2P action",
53                            action_kind = format!("{}", p2p_action.kind()),
54                            error = display(error));
55                    }
56                }
57            },
58        },
59        Action::P2pEffectful(_) => {}
60        Action::Ledger(action) => {
61            LedgerState::reducer(Substate::new(state, dispatcher), meta.with_action(action));
62        }
63        Action::LedgerEffects(_) => {}
64        Action::Snark(a) => {
65            crate::snark::SnarkState::reducer(
66                Substate::new(state, dispatcher),
67                meta.with_action(a),
68            );
69        }
70        Action::TransitionFrontier(a) => {
71            let skip_proof_verification = state.config.skip_proof_verification;
72            crate::transition_frontier::TransitionFrontierState::reducer(
73                Substate::new(state, dispatcher),
74                meta.with_action(a),
75                skip_proof_verification,
76            );
77        }
78        Action::SnarkPool(a) => {
79            crate::snark_pool::SnarkPoolState::reducer(
80                Substate::new(state, dispatcher),
81                meta.with_action(a),
82            );
83        }
84        Action::SnarkPoolEffect(_) => {}
85        Action::TransactionPool(a) => {
86            crate::transaction_pool::TransactionPoolState::reducer(
87                Substate::new(state, dispatcher),
88                meta.with_action(a),
89            );
90        }
91        Action::TransactionPoolEffect(_) => {}
92        Action::BlockProducer(action) => {
93            BlockProducerState::reducer(Substate::new(state, dispatcher), meta.with_action(action));
94        }
95        Action::BlockProducerEffectful(_) => {}
96        Action::ExternalSnarkWorker(action) => {
97            ExternalSnarkWorkers::reducer(
98                Substate::new(state, dispatcher),
99                meta.with_action(action),
100            );
101        }
102        Action::ExternalSnarkWorkerEffects(_) => {}
103        Action::Rpc(action) => {
104            RpcState::reducer(Substate::new(state, dispatcher), meta.with_action(action));
105        }
106        Action::RpcEffectful(_) => {}
107        Action::WatchedAccounts(a) => {
108            crate::watched_accounts::WatchedAccountsState::reducer(
109                Substate::new(state, dispatcher),
110                meta.with_action(a),
111            );
112        }
113        Action::P2pCallbacks(action) => {
114            State::p2p_callback_reducer(Substate::new(state, dispatcher), meta.with_action(action))
115        }
116        Action::CheckInvalidPeersAction(_) => {}
117    }
118
119    // must be the last.
120    state.action_applied(action);
121}