Skip to content

Commit 3a52808

Browse files
committed
Removed state access from effectful actions in p2p layer
1 parent 85bd8ac commit 3a52808

23 files changed

+295
-314
lines changed

node/src/action_kind.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,8 @@ pub enum ActionKind {
351351
P2pNetworkIdentifyStreamNew,
352352
P2pNetworkIdentifyStreamPrune,
353353
P2pNetworkIdentifyStreamRemoteClose,
354-
P2pNetworkIdentifyStreamEffectfulSendIdentify,
354+
P2pNetworkIdentifyStreamSendIdentify,
355+
P2pNetworkIdentifyStreamEffectfulGetListenAddresses,
355356
P2pNetworkKadBootstrapAppendRequest,
356357
P2pNetworkKadBootstrapCreateRequests,
357358
P2pNetworkKadBootstrapFinalizeRequests,
@@ -435,20 +436,17 @@ pub enum ActionKind {
435436
P2pNetworkSchedulerOutgoingDidConnect,
436437
P2pNetworkSchedulerPrune,
437438
P2pNetworkSchedulerPruneStream,
438-
P2pNetworkSchedulerPruneStreams,
439439
P2pNetworkSchedulerSelectDone,
440440
P2pNetworkSchedulerSelectError,
441441
P2pNetworkSchedulerYamuxDidInit,
442442
P2pNetworkSchedulerEffectfulDisconnect,
443-
P2pNetworkSchedulerEffectfulError,
444443
P2pNetworkSchedulerEffectfulIncomingConnectionIsReady,
445444
P2pNetworkSchedulerEffectfulIncomingDataIsReady,
446445
P2pNetworkSchedulerEffectfulIncomingDidAccept,
447446
P2pNetworkSchedulerEffectfulInterfaceDetected,
448447
P2pNetworkSchedulerEffectfulNoiseSelectDone,
449448
P2pNetworkSchedulerEffectfulOutgoingConnect,
450449
P2pNetworkSchedulerEffectfulOutgoingDidConnect,
451-
P2pNetworkSchedulerEffectfulSelectError,
452450
P2pNetworkSelectIncomingData,
453451
P2pNetworkSelectIncomingDataAuth,
454452
P2pNetworkSelectIncomingDataMux,
@@ -688,7 +686,7 @@ pub enum ActionKind {
688686
}
689687

690688
impl ActionKind {
691-
pub const COUNT: u16 = 579;
689+
pub const COUNT: u16 = 577;
692690
}
693691

694692
impl std::fmt::Display for ActionKind {
@@ -1804,7 +1802,6 @@ impl ActionKindGet for P2pNetworkSchedulerAction {
18041802
Self::Error { .. } => ActionKind::P2pNetworkSchedulerError,
18051803
Self::Disconnected { .. } => ActionKind::P2pNetworkSchedulerDisconnected,
18061804
Self::Prune { .. } => ActionKind::P2pNetworkSchedulerPrune,
1807-
Self::PruneStreams { .. } => ActionKind::P2pNetworkSchedulerPruneStreams,
18081805
Self::PruneStream { .. } => ActionKind::P2pNetworkSchedulerPruneStream,
18091806
}
18101807
}
@@ -1925,7 +1922,7 @@ impl ActionKindGet for P2pNetworkRpcAction {
19251922
impl ActionKindGet for P2pConnectionOutgoingEffectfulAction {
19261923
fn kind(&self) -> ActionKind {
19271924
match self {
1928-
Self::RandomInit => ActionKind::P2pConnectionOutgoingEffectfulRandomInit,
1925+
Self::RandomInit { .. } => ActionKind::P2pConnectionOutgoingEffectfulRandomInit,
19291926
Self::Init { .. } => ActionKind::P2pConnectionOutgoingEffectfulInit,
19301927
Self::OfferSend { .. } => ActionKind::P2pConnectionOutgoingEffectfulOfferSend,
19311928
Self::AnswerSet { .. } => ActionKind::P2pConnectionOutgoingEffectfulAnswerSet,
@@ -1973,9 +1970,7 @@ impl ActionKindGet for P2pNetworkSchedulerEffectfulAction {
19731970
ActionKind::P2pNetworkSchedulerEffectfulIncomingDataIsReady
19741971
}
19751972
Self::NoiseSelectDone { .. } => ActionKind::P2pNetworkSchedulerEffectfulNoiseSelectDone,
1976-
Self::SelectError { .. } => ActionKind::P2pNetworkSchedulerEffectfulSelectError,
19771973
Self::Disconnect { .. } => ActionKind::P2pNetworkSchedulerEffectfulDisconnect,
1978-
Self::Error { .. } => ActionKind::P2pNetworkSchedulerEffectfulError,
19791974
}
19801975
}
19811976
}
@@ -2034,6 +2029,7 @@ impl ActionKindGet for P2pNetworkIdentifyStreamAction {
20342029
Self::Close { .. } => ActionKind::P2pNetworkIdentifyStreamClose,
20352030
Self::RemoteClose { .. } => ActionKind::P2pNetworkIdentifyStreamRemoteClose,
20362031
Self::Prune { .. } => ActionKind::P2pNetworkIdentifyStreamPrune,
2032+
Self::SendIdentify { .. } => ActionKind::P2pNetworkIdentifyStreamSendIdentify,
20372033
}
20382034
}
20392035
}
@@ -2102,7 +2098,9 @@ impl ActionKindGet for P2pNetworkKademliaStreamAction {
21022098
impl ActionKindGet for P2pNetworkIdentifyStreamEffectfulAction {
21032099
fn kind(&self) -> ActionKind {
21042100
match self {
2105-
Self::SendIdentify { .. } => ActionKind::P2pNetworkIdentifyStreamEffectfulSendIdentify,
2101+
Self::GetListenAddresses { .. } => {
2102+
ActionKind::P2pNetworkIdentifyStreamEffectfulGetListenAddresses
2103+
}
21062104
}
21072105
}
21082106
}

node/src/rpc/rpc_reducer.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,18 @@ impl RpcState {
8181

8282
let dispatcher = state_context.into_dispatcher();
8383

84-
let callback = redux::callback!(
85-
on_p2p_connection_outgoing_rpc_connection_success((peer_id: PeerId, rpc_id: Option<RpcId>)) -> crate::Action {
86-
let Some(rpc_id) = rpc_id else {
87-
panic!("RPC ID not provided");
88-
};
89-
90-
RpcAction::P2pConnectionOutgoingPending{ rpc_id }
91-
}
92-
);
9384
dispatcher.push(P2pConnectionOutgoingAction::Init {
9485
opts: opts.clone(),
9586
rpc_id: Some(*rpc_id),
96-
on_success: Some(callback),
87+
on_success: Some(redux::callback!(
88+
on_p2p_connection_outgoing_rpc_connection_success((peer_id: PeerId, rpc_id: Option<RpcId>)) -> crate::Action {
89+
let Some(rpc_id) = rpc_id else {
90+
unreachable!("RPC ID not provided");
91+
};
92+
93+
RpcAction::P2pConnectionOutgoingPending{ rpc_id }
94+
}
95+
)),
9796
});
9897
}
9998
RpcAction::P2pConnectionOutgoingPending { rpc_id } => {

p2p/src/connection/outgoing/p2p_connection_outgoing_reducer.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ impl P2pConnectionOutgoingState {
3636

3737
match action {
3838
P2pConnectionOutgoingAction::RandomInit => {
39-
let dispatcher = state_context.into_dispatcher();
40-
dispatcher.push(P2pConnectionOutgoingEffectfulAction::RandomInit);
39+
let (dispatcher, state) = state_context.into_dispatcher_and_state();
40+
let p2p_state: &P2pState = state.substate()?;
41+
let peers = p2p_state.disconnected_peers().collect::<Vec<_>>();
42+
dispatcher.push(P2pConnectionOutgoingEffectfulAction::RandomInit { peers });
4143
Ok(())
4244
}
4345
P2pConnectionOutgoingAction::Init {
@@ -219,8 +221,17 @@ impl P2pConnectionOutgoingState {
219221
offer,
220222
});
221223
} else {
222-
dispatcher
223-
.push(P2pConnectionOutgoingEffectfulAction::OfferSend { peer_id, offer });
224+
let signaling_method = match opts {
225+
P2pConnectionOutgoingInitOpts::WebRTC { signaling, .. } => signaling,
226+
#[allow(unreachable_patterns)]
227+
_ => return Ok(()),
228+
};
229+
230+
dispatcher.push(P2pConnectionOutgoingEffectfulAction::OfferSend {
231+
peer_id,
232+
offer,
233+
signaling_method,
234+
});
224235
}
225236
Ok(())
226237
}

p2p/src/connection/outgoing_effectful/p2p_connection_outgoing_effectful_actions.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use openmina_core::requests::RpcId;
66
use crate::{
77
connection::{outgoing::P2pConnectionOutgoingInitOpts, P2pConnectionEffectfulAction},
88
identity::PublicKey,
9-
webrtc::{self, ConnectionAuth},
9+
webrtc::{self, ConnectionAuth, SignalingMethod},
1010
P2pState, PeerId,
1111
};
1212

@@ -15,7 +15,9 @@ use crate::{
1515
pub enum P2pConnectionOutgoingEffectfulAction {
1616
/// Initialize connection to a random peer.
1717
#[action_event(level = trace)]
18-
RandomInit,
18+
RandomInit {
19+
peers: Vec<P2pConnectionOutgoingInitOpts>,
20+
},
1921
/// Initialize connection to a new peer.
2022
#[action_event(level = info)]
2123
Init {
@@ -25,6 +27,7 @@ pub enum P2pConnectionOutgoingEffectfulAction {
2527
OfferSend {
2628
peer_id: PeerId,
2729
offer: Box<webrtc::Offer>,
30+
signaling_method: SignalingMethod,
2831
},
2932
AnswerSet {
3033
peer_id: PeerId,

p2p/src/connection/outgoing_effectful/p2p_connection_outgoing_effectful_effects.rs

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@ use redux::ActionMeta;
33

44
use crate::{
55
connection::{
6-
outgoing::{
7-
P2pConnectionOutgoingAction, P2pConnectionOutgoingError, P2pConnectionOutgoingInitOpts,
8-
P2pConnectionOutgoingState,
9-
},
10-
P2pConnectionService, P2pConnectionState,
6+
outgoing::{P2pConnectionOutgoingAction, P2pConnectionOutgoingError},
7+
P2pConnectionService,
118
},
12-
webrtc, P2pPeerStatus,
9+
webrtc,
1310
};
1411

1512
use super::P2pConnectionOutgoingEffectfulAction;
@@ -21,8 +18,7 @@ impl P2pConnectionOutgoingEffectfulAction {
2118
Store::Service: P2pConnectionService,
2219
{
2320
match self {
24-
P2pConnectionOutgoingEffectfulAction::RandomInit => {
25-
let peers = store.state().disconnected_peers().collect::<Vec<_>>();
21+
P2pConnectionOutgoingEffectfulAction::RandomInit { peers } => {
2622
let picked_peer = store.service().random_pick(&peers);
2723
if let Some(picked_peer) = picked_peer {
2824
store.dispatch(P2pConnectionOutgoingAction::Reconnect {
@@ -38,30 +34,19 @@ impl P2pConnectionOutgoingEffectfulAction {
3834
store.service().outgoing_init(opts);
3935
store.dispatch(P2pConnectionOutgoingAction::OfferSdpCreatePending { peer_id });
4036
}
41-
P2pConnectionOutgoingEffectfulAction::OfferSend { peer_id, offer } => {
42-
let (state, service) = store.state_and_service();
43-
let Some(peer) = state.peers.get(&peer_id) else {
44-
return;
45-
};
46-
let P2pPeerStatus::Connecting(P2pConnectionState::Outgoing(
47-
P2pConnectionOutgoingState::OfferReady { opts, .. },
48-
)) = &peer.status
49-
else {
50-
return;
51-
};
52-
let signaling_method = match opts {
53-
P2pConnectionOutgoingInitOpts::WebRTC { signaling, .. } => signaling,
54-
#[allow(unreachable_patterns)]
55-
_ => return,
56-
};
37+
P2pConnectionOutgoingEffectfulAction::OfferSend {
38+
peer_id,
39+
offer,
40+
signaling_method,
41+
} => {
5742
match signaling_method {
5843
webrtc::SignalingMethod::Http(_)
5944
| webrtc::SignalingMethod::Https(_)
6045
| webrtc::SignalingMethod::HttpsProxy(_, _) => {
6146
let Some(url) = signaling_method.http_url() else {
6247
return;
6348
};
64-
service.http_signaling_request(url, *offer);
49+
store.service().http_signaling_request(url, *offer);
6550
}
6651
webrtc::SignalingMethod::P2p { .. } => {
6752
bug_condition!("`P2pConnectionOutgoingEffectfulAction::OfferSend` shouldn't be called for `webrtc::SignalingMethod::P2p`");

p2p/src/network/identify/p2p_network_identify_reducer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{stream::P2pNetworkIdentifyStreamState, P2pNetworkIdentifyAction};
22
use crate::P2pLimits;
3-
use openmina_core::{Substate, SubstateAccess};
3+
use openmina_core::Substate;
44
use redux::ActionWithMeta;
55

66
impl super::P2pNetworkIdentifyState {
@@ -10,7 +10,7 @@ impl super::P2pNetworkIdentifyState {
1010
limits: &P2pLimits,
1111
) -> Result<(), String>
1212
where
13-
State: SubstateAccess<Self>,
13+
State: crate::P2pStateTrait,
1414
Action: crate::P2pActionTrait<State>,
1515
{
1616
let (action, meta) = action.split();

p2p/src/network/identify/stream/p2p_network_identify_stream_actions.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::{ConnectionAddr, Data, P2pAction, P2pState, PeerId, StreamId};
2+
use multiaddr::Multiaddr;
23
use openmina_core::ActionEvent;
34
use redux::EnablingCondition;
45
use serde::{Deserialize, Serialize};
@@ -38,6 +39,12 @@ pub enum P2pNetworkIdentifyStreamAction {
3839
peer_id: PeerId,
3940
stream_id: StreamId,
4041
},
42+
SendIdentify {
43+
addr: ConnectionAddr,
44+
peer_id: PeerId,
45+
stream_id: StreamId,
46+
addresses: Vec<Multiaddr>,
47+
},
4148
}
4249

4350
macro_rules! enum_field {
@@ -48,6 +55,7 @@ macro_rules! enum_field {
4855
| P2pNetworkIdentifyStreamAction::IncomingData { $field, .. }
4956
| P2pNetworkIdentifyStreamAction::Close { $field, .. }
5057
| P2pNetworkIdentifyStreamAction::RemoteClose { $field, .. }
58+
| P2pNetworkIdentifyStreamAction::SendIdentify { $field, .. }
5159
| P2pNetworkIdentifyStreamAction::Prune { $field, .. } => $field,
5260
}
5361
}

0 commit comments

Comments
 (0)