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            crate::transition_frontier::TransitionFrontierState::reducer(
72                Substate::new(state, dispatcher),
73                meta.with_action(a),
74            );
75        }
76        Action::SnarkPool(a) => {
77            crate::snark_pool::SnarkPoolState::reducer(
78                Substate::new(state, dispatcher),
79                meta.with_action(a),
80            );
81        }
82        Action::SnarkPoolEffect(_) => {}
83        Action::TransactionPool(a) => {
84            crate::transaction_pool::TransactionPoolState::reducer(
85                Substate::new(state, dispatcher),
86                meta.with_action(a),
87            );
88        }
89        Action::TransactionPoolEffect(_) => {}
90        Action::BlockProducer(action) => {
91            BlockProducerState::reducer(Substate::new(state, dispatcher), meta.with_action(action));
92        }
93        Action::BlockProducerEffectful(_) => {}
94        Action::ExternalSnarkWorker(action) => {
95            ExternalSnarkWorkers::reducer(
96                Substate::new(state, dispatcher),
97                meta.with_action(action),
98            );
99        }
100        Action::ExternalSnarkWorkerEffects(_) => {}
101        Action::Rpc(action) => {
102            RpcState::reducer(Substate::new(state, dispatcher), meta.with_action(action));
103        }
104        Action::RpcEffectful(_) => {}
105        Action::WatchedAccounts(a) => {
106            crate::watched_accounts::WatchedAccountsState::reducer(
107                Substate::new(state, dispatcher),
108                meta.with_action(a),
109            );
110        }
111        Action::P2pCallbacks(action) => {
112            State::p2p_callback_reducer(Substate::new(state, dispatcher), meta.with_action(action))
113        }
114        Action::CheckInvalidPeersAction(_) => {}
115    }
116
117    // must be the last.
118    state.action_applied(action);
119}