Skip to content

Commit e0947ce

Browse files
committed
fix(p2p): p2p testing crate compilation error and clippy errors
1 parent 1475d36 commit e0947ce

File tree

10 files changed

+86
-34
lines changed

10 files changed

+86
-34
lines changed

p2p/src/channels/signaling/discovery/p2p_channels_signaling_discovery_reducer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ impl P2pChannelsSignalingDiscoveryState {
366366
P2pConnectionResponse::Rejected(reason) => {
367367
dispatcher.push(P2pConnectionOutgoingAction::AnswerRecvError {
368368
peer_id: target_public_key.peer_id(),
369-
error: P2pConnectionErrorResponse::Rejected(reason.clone()),
369+
error: P2pConnectionErrorResponse::Rejected(*reason),
370370
})
371371
}
372372
P2pConnectionResponse::SignalDecryptionFailed => {

p2p/src/channels/signaling/discovery/p2p_channels_signaling_discovery_state.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use serde::{Deserialize, Serialize};
22

33
use crate::identity::PublicKey;
44

5+
#[allow(clippy::large_enum_variant)]
56
#[derive(Serialize, Deserialize, Debug, Clone)]
67
pub enum P2pChannelsSignalingDiscoveryState {
78
Disabled,

p2p/src/channels/signaling/exchange/p2p_channels_signaling_exchange_state.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use serde::{Deserialize, Serialize};
22

33
use crate::identity::PublicKey;
44

5+
#[allow(clippy::large_enum_variant)]
56
#[derive(Serialize, Deserialize, Debug, Clone)]
67
pub enum P2pChannelsSignalingExchangeState {
78
Disabled,

p2p/src/channels/signaling/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ impl crate::P2pState {
5252

5353
// TODO(binier): maybe randomize
5454
for requester in requests {
55-
if let Some(target_peer_id) =
56-
available_peers.iter().filter(|&&id| id != requester).next()
57-
{
55+
if let Some(target_peer_id) = available_peers.iter().find(|&&id| id != requester) {
5856
let target_peer_id = *target_peer_id;
5957
dispatcher.push(P2pChannelsSignalingDiscoveryAction::DiscoveredSend {
6058
peer_id: *requester,

p2p/src/channels/signaling/p2p_channels_signaling_state.rs

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,16 @@ impl P2pChannelsSignalingState {
2525

2626
pub fn received_discovered_peer_id(&self) -> Option<PeerId> {
2727
match &self.discovery {
28-
P2pChannelsSignalingDiscoveryState::Ready { remote, .. } => match remote {
29-
SignalingDiscoveryState::Discovered {
30-
target_public_key, ..
31-
}
32-
| SignalingDiscoveryState::DiscoveredAccepted {
33-
target_public_key, ..
34-
} => Some(target_public_key.peer_id()),
35-
_ => None,
36-
},
28+
P2pChannelsSignalingDiscoveryState::Ready {
29+
remote:
30+
SignalingDiscoveryState::Discovered {
31+
target_public_key, ..
32+
}
33+
| SignalingDiscoveryState::DiscoveredAccepted {
34+
target_public_key, ..
35+
},
36+
..
37+
} => Some(target_public_key.peer_id()),
3738
_ => None,
3839
}
3940
}
@@ -49,25 +50,27 @@ impl P2pChannelsSignalingState {
4950

5051
pub fn sent_discovered_peer_id(&self) -> Option<PeerId> {
5152
match &self.discovery {
52-
P2pChannelsSignalingDiscoveryState::Ready { local, .. } => match local {
53-
SignalingDiscoveryState::Discovered {
54-
target_public_key, ..
55-
}
56-
| SignalingDiscoveryState::DiscoveredAccepted {
57-
target_public_key, ..
58-
} => Some(target_public_key.peer_id()),
59-
_ => None,
60-
},
53+
P2pChannelsSignalingDiscoveryState::Ready {
54+
local:
55+
SignalingDiscoveryState::Discovered {
56+
target_public_key, ..
57+
}
58+
| SignalingDiscoveryState::DiscoveredAccepted {
59+
target_public_key, ..
60+
},
61+
..
62+
} => Some(target_public_key.peer_id()),
6163
_ => None,
6264
}
6365
}
6466

6567
pub fn is_looking_for_incoming_peer(&self) -> bool {
66-
match &self.exchange {
67-
P2pChannelsSignalingExchangeState::Ready { remote, .. } => {
68-
matches!(remote, SignalingExchangeState::Requested { .. })
68+
matches!(
69+
self.exchange,
70+
P2pChannelsSignalingExchangeState::Ready {
71+
remote: SignalingExchangeState::Requested { .. },
72+
..
6973
}
70-
_ => false,
71-
}
74+
)
7275
}
7376
}

p2p/src/connection/incoming/p2p_connection_incoming_reducer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ impl P2pConnectionIncomingState {
144144
}
145145
P2pConnectionIncomingAction::AnswerReady { peer_id, answer } => {
146146
let state = p2p_state
147-
.incoming_peer_connection_mut(&peer_id)
147+
.incoming_peer_connection_mut(peer_id)
148148
.ok_or_else(|| format!("Invalid state for: {:?}", action))?;
149149

150150
let Self::AnswerSdpCreateSuccess {
@@ -187,7 +187,7 @@ impl P2pConnectionIncomingState {
187187
}
188188
}
189189

190-
if let Some(rpc_id) = p2p_state.peer_connection_rpc_id(&peer_id) {
190+
if let Some(rpc_id) = p2p_state.peer_connection_rpc_id(peer_id) {
191191
if let Some(callback) =
192192
&p2p_state.callbacks.on_p2p_connection_incoming_answer_ready
193193
{

p2p/src/identity/secret_key.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,16 @@ impl TryFrom<SecretKey> for libp2p_identity::Keypair {
155155
}
156156
}
157157

158+
#[cfg(feature = "p2p-libp2p")]
159+
impl TryFrom<libp2p_identity::Keypair> for SecretKey {
160+
type Error = ();
161+
162+
fn try_from(value: libp2p_identity::Keypair) -> Result<Self, Self::Error> {
163+
let bytes = value.try_into_ed25519().or(Err(()))?.to_bytes();
164+
Ok(Self::from_bytes(bytes[0..32].try_into().or(Err(()))?))
165+
}
166+
}
167+
158168
impl Serialize for SecretKey {
159169
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
160170
where

p2p/src/service_impl/mod.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub mod webrtc {
2121
use crate::{
2222
channels::{ChannelId, ChannelMsg, MsgId},
2323
connection::outgoing::P2pConnectionOutgoingInitOpts,
24-
identity::SecretKey,
24+
identity::{EncryptableType, PublicKey, SecretKey},
2525
webrtc, P2pEvent, PeerId,
2626
};
2727

@@ -72,5 +72,17 @@ pub mod webrtc {
7272
fn channel_open(&mut self, peer_id: PeerId, id: ChannelId) {}
7373

7474
fn channel_send(&mut self, peer_id: PeerId, msg_id: MsgId, msg: ChannelMsg) {}
75+
76+
fn encrypt<T: EncryptableType>(
77+
&mut self,
78+
other_pk: &PublicKey,
79+
message: &T,
80+
) -> Result<T::Encrypted, ()>;
81+
82+
fn decrypt<T: EncryptableType>(
83+
&mut self,
84+
other_pub_key: &PublicKey,
85+
encrypted: &T::Encrypted,
86+
) -> Result<T, ()>;
7587
}
7688
}

p2p/testing/src/redux.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,18 @@ use openmina_core::{
1212
use p2p::{
1313
bootstrap::P2pNetworkKadBootstrapState,
1414
channels::{
15-
best_tip::P2pChannelsBestTipAction, best_tip_effectful::P2pChannelsBestTipEffectfulAction,
16-
rpc::P2pChannelsRpcAction, rpc_effectful::P2pChannelsRpcEffectfulAction,
17-
signaling::exchange_effectful::P2pChannelsSignalingExchangeEffectfulAction,
18-
snark::P2pChannelsSnarkAction, snark_effectful::P2pChannelsSnarkEffectfulAction,
15+
best_tip::P2pChannelsBestTipAction,
16+
best_tip_effectful::P2pChannelsBestTipEffectfulAction,
17+
rpc::P2pChannelsRpcAction,
18+
rpc_effectful::P2pChannelsRpcEffectfulAction,
19+
signaling::{
20+
discovery::P2pChannelsSignalingDiscoveryAction,
21+
discovery_effectful::P2pChannelsSignalingDiscoveryEffectfulAction,
22+
exchange::P2pChannelsSignalingExchangeAction,
23+
exchange_effectful::P2pChannelsSignalingExchangeEffectfulAction,
24+
},
25+
snark::P2pChannelsSnarkAction,
26+
snark_effectful::P2pChannelsSnarkEffectfulAction,
1927
snark_job_commitment::P2pChannelsSnarkJobCommitmentAction,
2028
snark_job_commitment_effectful::P2pChannelsSnarkJobCommitmentEffectfulAction,
2129
streaming_rpc::P2pChannelsStreamingRpcAction,
@@ -287,6 +295,8 @@ impl_from_p2p!(p2p::P2pNetworkPnetAction);
287295
impl_from_p2p!(p2p::P2pNetworkNoiseAction);
288296
impl_from_p2p!(p2p::connection::incoming::P2pConnectionIncomingAction);
289297
impl_from_p2p!(p2p::P2pNetworkPubsubAction);
298+
impl_from_p2p!(P2pChannelsSignalingDiscoveryAction);
299+
impl_from_p2p!(P2pChannelsSignalingExchangeAction);
290300
impl_from_p2p!(P2pChannelsTransactionAction);
291301
impl_from_p2p!(P2pChannelsSnarkAction);
292302
impl_from_p2p!(p2p::P2pNetworkRpcAction);
@@ -304,6 +314,7 @@ impl_from_p2p!(effectful p2p::P2pNetworkPubsubEffectfulAction);
304314
impl_from_p2p!(effectful P2pNetworkIdentifyStreamEffectfulAction);
305315
impl_from_p2p!(effectful P2pConnectionOutgoingEffectfulAction);
306316
impl_from_p2p!(effectful P2pDisconnectionEffectfulAction);
317+
impl_from_p2p!(effectful P2pChannelsSignalingDiscoveryEffectfulAction);
307318
impl_from_p2p!(effectful P2pChannelsSignalingExchangeEffectfulAction);
308319
impl_from_p2p!(effectful P2pChannelsBestTipEffectfulAction);
309320
impl_from_p2p!(effectful P2pChannelsStreamingRpcEffectfulAction);

p2p/testing/src/service.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,22 @@ impl P2pServiceWebrtc for ClusterService {
9999
) -> &mut std::collections::BTreeMap<p2p::PeerId, p2p::service_impl::webrtc::PeerState> {
100100
&mut self.peers
101101
}
102+
103+
fn encrypt<T: p2p::identity::EncryptableType>(
104+
&mut self,
105+
_other_pk: &p2p::identity::PublicKey,
106+
_message: &T,
107+
) -> Result<T::Encrypted, ()> {
108+
unreachable!("this is webrtc only and this crate tests libp2p only")
109+
}
110+
111+
fn decrypt<T: p2p::identity::EncryptableType>(
112+
&mut self,
113+
_other_pub_key: &p2p::identity::PublicKey,
114+
_encrypted: &T::Encrypted,
115+
) -> Result<T, ()> {
116+
unreachable!("this is webrtc only and this crate tests libp2p only")
117+
}
102118
}
103119

104120
impl P2pCryptoService for ClusterService {

0 commit comments

Comments
 (0)