1use openmina_core::{bug_condition, error, Substate};
2use p2p::{P2pAction, P2pEffectfulAction, P2pInitializeAction, P2pState};
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 = 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 snark::SnarkState::reducer(Substate::new(state, dispatcher), meta.with_action(a));
66 }
67 Action::TransitionFrontier(a) => {
68 crate::transition_frontier::TransitionFrontierState::reducer(
69 Substate::new(state, dispatcher),
70 meta.with_action(a),
71 );
72 }
73 Action::SnarkPool(a) => {
74 crate::snark_pool::SnarkPoolState::reducer(
75 Substate::new(state, dispatcher),
76 meta.with_action(a),
77 );
78 }
79 Action::SnarkPoolEffect(_) => {}
80 Action::TransactionPool(a) => {
81 crate::transaction_pool::TransactionPoolState::reducer(
82 Substate::new(state, dispatcher),
83 meta.with_action(a),
84 );
85 }
86 Action::TransactionPoolEffect(_) => {}
87 Action::BlockProducer(action) => {
88 BlockProducerState::reducer(Substate::new(state, dispatcher), meta.with_action(action));
89 }
90 Action::BlockProducerEffectful(_) => {}
91 Action::ExternalSnarkWorker(action) => {
92 ExternalSnarkWorkers::reducer(
93 Substate::new(state, dispatcher),
94 meta.with_action(action),
95 );
96 }
97 Action::ExternalSnarkWorkerEffects(_) => {}
98 Action::Rpc(action) => {
99 RpcState::reducer(Substate::new(state, dispatcher), meta.with_action(action));
100 }
101 Action::RpcEffectful(_) => {}
102 Action::WatchedAccounts(a) => {
103 crate::watched_accounts::WatchedAccountsState::reducer(
104 Substate::new(state, dispatcher),
105 meta.with_action(a),
106 );
107 }
108 Action::P2pCallbacks(action) => {
109 State::p2p_callback_reducer(Substate::new(state, dispatcher), meta.with_action(action))
110 }
111 }
112
113 state.action_applied(action);
115}