Skip to content

Commit b8f4103

Browse files
committed
fix(p2p): apply code review suggestions
1 parent 7e9e99f commit b8f4103

File tree

5 files changed

+40
-15
lines changed

5 files changed

+40
-15
lines changed

node/common/src/service/p2p.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,12 @@ impl P2pCryptoService for NodeService {
8888
.expect("unable to create signature")
8989
}
9090

91-
fn verify_publication(&mut self, pk: &[u8], publication: &[u8], sig: &[u8]) -> bool {
92-
let Ok(pk) = libp2p_identity::PublicKey::try_decode_protobuf(pk) else {
93-
return false;
94-
};
95-
println!("pk {:?}", pk);
91+
fn verify_publication(
92+
&mut self,
93+
pk: &libp2p_identity::PublicKey,
94+
publication: &[u8],
95+
sig: &[u8],
96+
) -> bool {
9697
let msg: Vec<u8> = [b"libp2p-pubsub:", publication].concat();
9798
pk.verify(&msg, sig)
9899
}

node/testing/src/service/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,12 @@ impl P2pCryptoService for NodeTestingService {
297297
self.real.sign_publication(publication)
298298
}
299299

300-
fn verify_publication(&mut self, pk: &[u8], publication: &[u8], sig: &[u8]) -> bool {
300+
fn verify_publication(
301+
&mut self,
302+
pk: &libp2p_identity::PublicKey,
303+
publication: &[u8],
304+
sig: &[u8],
305+
) -> bool {
301306
self.real.verify_publication(pk, publication, sig)
302307
}
303308
}

p2p/src/network/p2p_network_service.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ pub trait P2pCryptoService: redux::Service {
3636
fn sign_key(&mut self, key: &[u8; 32]) -> Vec<u8>;
3737

3838
fn sign_publication(&mut self, publication: &[u8]) -> Vec<u8>;
39-
fn verify_publication(&mut self, pk: &[u8], publication: &[u8], sig: &[u8]) -> bool;
39+
fn verify_publication(
40+
&mut self,
41+
pk: &libp2p_identity::PublicKey,
42+
publication: &[u8],
43+
sig: &[u8],
44+
) -> bool;
4045
}
4146

4247
#[derive(Debug, thiserror::Error)]

p2p/src/network/pubsub/p2p_network_pubsub_effects.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ impl P2pNetworkPubsubAction {
132132
..
133133
} => {
134134
let Some(state) = state.clients.get(&peer_id) else {
135+
// TODO: add bug_condition
135136
return;
136137
};
137138
let messages = state.incoming_messages.clone();
@@ -142,16 +143,27 @@ impl P2pNetworkPubsubAction {
142143
{
143144
message.key = None;
144145
let mut data = vec![];
146+
147+
let Ok(pk) = libp2p_identity::PublicKey::try_decode_protobuf(&from[2..])
148+
else {
149+
// TODO: add bug_condition
150+
// peer specify bad pk
151+
continue;
152+
};
153+
145154
#[allow(clippy::if_same_then_else)]
146155
if prost::Message::encode(&message, &mut data).is_err() {
156+
// TODO: add bug_condition
157+
// should never happen;
158+
// we just decode this message, so it should encode without error
147159
continue;
148-
} else if !store
149-
.service()
150-
.verify_publication(&from[2..], &data, &signature)
151-
{
160+
} else if !store.service().verify_publication(&pk, &data, &signature) {
161+
// TODO: add bug_condition
162+
// signature is invalid
152163
continue;
153164
}
154165
} else {
166+
// TODO: add bug_condition
155167
// the message doesn't contain signature or it doesn't contain verifying key
156168
continue;
157169
}

p2p/testing/src/service.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,12 @@ impl P2pCryptoService for ClusterService {
139139
.expect("unable to create signature")
140140
}
141141

142-
fn verify_publication(&mut self, pk: &[u8], publication: &[u8], sig: &[u8]) -> bool {
143-
let Ok(pk) = libp2p_identity::PublicKey::try_decode_protobuf(pk) else {
144-
return false;
145-
};
142+
fn verify_publication(
143+
&mut self,
144+
pk: &libp2p_identity::PublicKey,
145+
publication: &[u8],
146+
sig: &[u8],
147+
) -> bool {
146148
let msg: Vec<u8> = [b"libp2p-pubsub:", publication].concat();
147149
pk.verify(&msg, sig)
148150
}

0 commit comments

Comments
 (0)