p2p: Use parsed types in ReadingPayload state
What changed, and why it matters
This commit is a small internal code cleanup in the Bitcoin peer-to-peer message decoder. It changes how message length and network magic bytes are stored while being parsed, switching from raw byte arrays to already-parsed types. There is no security fix here; it is purely a refactoring to simplify the code and make future maintenance easier.
No security action required. Treat as a normal refactoring commit during review/merge.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch refactors the V1NetworkMessageDecoder in p2p/src/message.rs. In the ReadingPayload state, magic_bytes: [u8; 4] and payload_len_bytes: [u8; 4] are replaced with magic: Magic and length: u32. The u32 length is parsed earlier from the header, and Magic::from_bytes is called at state transition rather than at the final end() call. The end() method now uses the pre-parsed fields directly. This removes redundant parsing and simplifies types, but does not change validation logic, bounds checking, or observable behavior.
Changed components
p2p/src/message.rsV1NetworkMessageDecoderDecoderState::ReadingPayloadInspect captured patch +8 / −18
diff --git a/p2p/src/message.rs b/p2p/src/message.rs
index 143d3f64..7667a8d1 100644
--- a/p2p/src/message.rs
+++ b/p2p/src/message.rs
@@ -1377,8 +1377,8 @@ enum DecoderState {
>,
},
ReadingPayload {
- magic_bytes: [u8; 4],
- payload_len_bytes: [u8; 4],
+ magic: Magic,
+ length: u32,
checksum: [u8; 4],
payload_decoder: NetworkMessageDecoder,
},
@@ -1432,7 +1432,8 @@ impl encoding::Decoder for V1NetworkMessageDecoder {
V1NetworkMessageDecoderError(V1NetworkMessageDecoderErrorInner::Header)
})?;
- let payload_len = u32::from_le_bytes(payload_len_bytes) as usize;
+ let length = u32::from_le_bytes(payload_len_bytes);
+ let payload_len = length as usize;
if payload_len > MAX_MSG_SIZE {
return Err(V1NetworkMessageDecoderError(
V1NetworkMessageDecoderErrorInner::PayloadTooLarge,
@@ -1441,8 +1442,8 @@ impl encoding::Decoder for V1NetworkMessageDecoder {
let payload_decoder = NetworkMessageDecoder::new(command, payload_len);
self.state = DecoderState::ReadingPayload {
- magic_bytes,
- payload_len_bytes,
+ magic: Magic::from_bytes(magic_bytes),
+ length,
checksum,
payload_decoder,
};
@@ -1462,13 +1463,7 @@ impl encoding::Decoder for V1NetworkMessageDecoder {
match self.state {
DecoderState::ReadingHeader { .. } =>
Err(V1NetworkMessageDecoderError(V1NetworkMessageDecoderErrorInner::Header)),
- DecoderState::ReadingPayload {
- magic_bytes,
- payload_len_bytes,
- checksum,
- payload_decoder,
- ..
- } => {
+ DecoderState::ReadingPayload { magic, length, checksum, payload_decoder, .. } => {
let payload = payload_decoder.end()?;
let (_, expected_checksum) = sha2_checksum(&payload);
if checksum != expected_checksum {
@@ -1480,12 +1475,7 @@ impl encoding::Decoder for V1NetworkMessageDecoder {
));
}
- Ok(V1NetworkMessage {
- magic: Magic::from_bytes(magic_bytes),
- payload,
- payload_len: u32::from_le_bytes(payload_len_bytes),
- checksum,
- })
+ Ok(V1NetworkMessage { magic, payload, payload_len: length, checksum })
}
}
}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.