Skip to content

Commit cfec7f9

Browse files
authored
refactor(l1): remove usage of assert_eq in frame decoding (rlpx) (#2456)
**Motivation** Replaces `assert_eq` usage with proper errors in rlpx frame decoding <!-- Why does this pull request exist? What are its goals? --> **Description** * Add error variant `RLPXError::InvalidMessageFrame` * Remove usage of `assert_eq` in `RLPxCodec::decode` impl <!-- A clear and concise general description of the changes this PR introduces --> <!-- Link to issues: Resolves #111, Resolves #222 --> Closes #1748
1 parent 5aa86f0 commit cfec7f9

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

crates/networking/p2p/rlpx/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ pub(crate) enum RLPxError {
4949
MempoolError(#[from] MempoolError),
5050
#[error("Io Error: {0}")]
5151
IoError(#[from] std::io::Error),
52+
#[error("Failed to decode message due to invalid frame: {0}")]
53+
InvalidMessageFrame(String),
5254
}
5355

5456
// tokio::sync::mpsc::error::SendError<Message> is too large to be part of the RLPxError enum directly

crates/networking/p2p/rlpx/frame.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,11 @@ impl Decoder for RLPxCodec {
113113
.map_err(|_| RLPxError::CryptographyError("Invalid header mac".to_owned()))?,
114114
);
115115

116-
// TODO: replace these assert_eq! by actual errors
117-
// https://github.com/lambdaclass/ethrex/issues/1748
118-
assert_eq!(header_mac, expected_header_mac.0);
116+
if header_mac != expected_header_mac.0 {
117+
return Err(RLPxError::InvalidMessageFrame(
118+
"Mismatched header mac".to_string(),
119+
));
120+
}
119121

120122
let header_text = header_ciphertext;
121123
// Use temporary value as it can be discarded if the buffer does not contain yet the full message
@@ -175,9 +177,11 @@ impl Decoder for RLPxCodec {
175177
.try_into()
176178
.map_err(|_| RLPxError::CryptographyError("Invalid frame mac".to_owned()))?;
177179

178-
// TODO: replace these assert_eq! by actual errors
179-
// https://github.com/lambdaclass/ethrex/issues/1748
180-
assert_eq!(frame_mac, expected_frame_mac);
180+
if frame_mac != expected_frame_mac {
181+
return Err(RLPxError::InvalidMessageFrame(
182+
"Mismatched frame mac".to_string(),
183+
));
184+
}
181185

182186
// decrypt frame
183187
self.ingress_aes.apply_keystream(frame_ciphertext);

0 commit comments

Comments
 (0)