Skip to content

Commit 4f7efe4

Browse files
committed
fix(p2p): improve code style
1 parent e91f8c7 commit 4f7efe4

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

p2p/src/network/noise/p2p_network_noise_state.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ impl NoiseState {
127127
let hkdf = Hkdf::<Sha256, Hmac<Sha256>>::new(Some(&self.chaining_key.0), &secret);
128128
secret.zeroize();
129129
let mut okm = [0; 64];
130-
hkdf.expand(&[], &mut okm).unwrap();
130+
hkdf.expand(&[], &mut okm)
131+
.expect("the length is constant and small");
131132
self.chaining_key.0.clone_from_slice(&okm[..32]);
132133
self.aead_key.0.clone_from_slice(&okm[32..]);
133134
}
@@ -154,7 +155,7 @@ impl NoiseState {
154155

155156
let tag = ChaCha20Poly1305::new(GenericArray::from_slice(&self.aead_key.0))
156157
.encrypt_in_place_detached(&nonce, &self.hash.0, data)
157-
.unwrap();
158+
.expect("data length must be sufficiently small");
158159
let hash = Sha256::default()
159160
.chain(self.hash.0)
160161
.chain(&*data)
@@ -171,7 +172,8 @@ impl NoiseState {
171172

172173
let hkdf = Hkdf::<Sha256, Hmac<Sha256>>::new(Some(&self.chaining_key.0), b"");
173174
let mut okm = [0; 64];
174-
hkdf.expand(&[], &mut okm).unwrap();
175+
hkdf.expand(&[], &mut okm)
176+
.expect("the length is constant and small");
175177
fst.clone_from_slice(&okm[..32]);
176178
scd.clone_from_slice(&okm[32..]);
177179
(DataSized(fst), DataSized(scd))

p2p/src/network/pubsub/p2p_network_pubsub_effects.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,13 @@ impl P2pNetworkPubsubAction {
114114
P2pNetworkPubsubAction::Sign { .. } => {
115115
if let Some(to_sign) = state.to_sign.front() {
116116
let mut publication = vec![];
117-
prost::Message::encode(to_sign, &mut publication).unwrap();
118-
let signature = store.service().sign_publication(&publication).into();
119-
store.dispatch(P2pNetworkPubsubAction::BroadcastSigned { signature });
117+
if let Err(err) = prost::Message::encode(to_sign, &mut publication) {
118+
// TODO: dispatch action for logging
119+
let _ = err;
120+
} else {
121+
let signature = store.service().sign_publication(&publication).into();
122+
store.dispatch(P2pNetworkPubsubAction::BroadcastSigned { signature });
123+
}
120124
}
121125
}
122126
P2pNetworkPubsubAction::BroadcastSigned { .. } => broadcast(store),
@@ -179,7 +183,10 @@ impl P2pNetworkPubsubAction {
179183
// println!("{}", std::str::from_utf8(&id).unwrap());
180184
// }
181185
let mut data = vec![];
182-
if prost::Message::encode_length_delimited(&msg, &mut data).is_ok() {
186+
if let Err(err) = prost::Message::encode_length_delimited(&msg, &mut data) {
187+
// TODO: dispatch action for logging
188+
let _ = err;
189+
} else {
183190
store.dispatch(P2pNetworkPubsubAction::OutgoingData {
184191
data: data.clone().into(),
185192
peer_id,

0 commit comments

Comments
 (0)