Skip to content

Commit 1166d82

Browse files
Move broadcast bit validation to parse message
1 parent ea354a2 commit 1166d82

File tree

2 files changed

+36
-36
lines changed

2 files changed

+36
-36
lines changed

monad-raptorcast/src/decoding.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ impl MessageTier {
305305
where
306306
PT: PubKey,
307307
{
308-
if message.broadcast || message.secondary_broadcast {
308+
if message.maybe_broadcast_mode.is_some() {
309309
return MessageTier::Broadcast;
310310
}
311311

@@ -1587,6 +1587,7 @@ mod test {
15871587
use rand::seq::SliceRandom as _;
15881588

15891589
use super::*;
1590+
use crate::util::BroadcastMode;
15901591
type PT = monad_crypto::NopPubKey;
15911592

15921593
const EPOCH: Epoch = Epoch(1);
@@ -1650,14 +1651,13 @@ mod test {
16501651
author,
16511652
app_message_hash,
16521653
app_message_len: app_message.len() as u32,
1653-
broadcast: false,
1654+
maybe_broadcast_mode: None,
16541655
chunk: chunk.freeze(),
16551656
// these fields are never touched in this module
16561657
recipient_hash: HexBytes([0; 20]),
16571658
message: Bytes::new(),
16581659
epoch: EPOCH.0,
16591660
unix_ts_ms,
1660-
secondary_broadcast: false,
16611661
};
16621662
messages.push(message);
16631663
}
@@ -1724,7 +1724,7 @@ mod test {
17241724
);
17251725
symbols_broadcast
17261726
.iter_mut()
1727-
.for_each(|msg| msg.broadcast = true);
1727+
.for_each(|msg| msg.maybe_broadcast_mode = Some(BroadcastMode::Primary));
17281728

17291729
let symbols_validator = make_symbols(
17301730
&Bytes::from(vec![3u8; APP_MESSAGE_LEN]),
@@ -1912,10 +1912,10 @@ mod test {
19121912
// Use broadcast tier so that validator's and non-validator's
19131913
// messages get mixed in the same cache.
19141914
for msg in &mut all_symbols_part_1 {
1915-
msg.broadcast = true;
1915+
msg.maybe_broadcast_mode = Some(BroadcastMode::Primary);
19161916
}
19171917
for msg in &mut all_symbols_part_2 {
1918-
msg.broadcast = true;
1918+
msg.maybe_broadcast_mode = Some(BroadcastMode::Primary);
19191919
}
19201920

19211921
let mut cache = DecoderCache::new(config);

monad-raptorcast/src/udp.rs

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use monad_dataplane::{
3030
};
3131
use monad_merkle::{MerkleHash, MerkleProof};
3232
use monad_types::{Epoch, NodeId};
33-
use tracing::{debug, warn};
33+
use tracing::warn;
3434

3535
pub use crate::packet::build_messages;
3636
use crate::{
@@ -183,27 +183,10 @@ impl<ST: CertificateSignatureRecoverable> UdpState<ST> {
183183
continue;
184184
}
185185

186-
let maybe_broadcast_mode = match (
187-
parsed_message.broadcast,
188-
parsed_message.secondary_broadcast,
189-
) {
190-
(true, false) => Some(BroadcastMode::Primary),
191-
(false, true) => Some(BroadcastMode::Secondary),
192-
(false, false) => None,
193-
(true, true) => {
194-
// invalid to have both primary and secondary broadcast bit set
195-
debug!(
196-
?parsed_message.author,
197-
"Receiving invalid message with both broadcast and secondary broadcast bit set"
198-
);
199-
continue;
200-
}
201-
};
202-
203186
// Note: The check that parsed_message.author is valid is already
204187
// done in iterate_rebroadcast_peers(), but we want to drop invalid
205188
// chunks ASAP, before changing `recently_decoded_state`.
206-
if let Some(broadcast_mode) = maybe_broadcast_mode {
189+
if let Some(broadcast_mode) = parsed_message.maybe_broadcast_mode {
207190
if !group_map.check_source(
208191
Epoch(parsed_message.epoch),
209192
&parsed_message.author,
@@ -234,7 +217,7 @@ impl<ST: CertificateSignatureRecoverable> UdpState<ST> {
234217

235218
let mut try_rebroadcast_symbol = || {
236219
// rebroadcast raptorcast chunks if necessary
237-
if let Some(broadcast_mode) = maybe_broadcast_mode {
220+
if let Some(broadcast_mode) = parsed_message.maybe_broadcast_mode {
238221
if self_hash == parsed_message.recipient_hash {
239222
let maybe_targets = group_map.iterate_rebroadcast_peers(
240223
Epoch(parsed_message.epoch),
@@ -327,8 +310,7 @@ where
327310
pub unix_ts_ms: u64,
328311
pub app_message_hash: AppMessageHash,
329312
pub app_message_len: u32,
330-
pub broadcast: bool,
331-
pub secondary_broadcast: bool,
313+
pub maybe_broadcast_mode: Option<BroadcastMode>,
332314
pub recipient_hash: NodeIdHash, // if this matches our node_id, then we need to re-broadcast RaptorCast chunks
333315
pub chunk_id: u16,
334316
pub chunk: Bytes, // raptor-coded portion
@@ -347,6 +329,7 @@ pub enum MessageValidationError {
347329
max: u64,
348330
delta: i64,
349331
},
332+
InvalidBroadcastBits,
350333
}
351334

352335
/// - 65 bytes => Signature of sender over hash(rest of message up to merkle proof, concatenated with merkle root)
@@ -402,6 +385,15 @@ where
402385
let secondary_broadcast = (cursor_broadcast_tree_depth & (1 << 6)) != 0;
403386
let tree_depth = cursor_broadcast_tree_depth & 0b0000_1111; // bottom 4 bits
404387

388+
let maybe_broadcast_mode = match (broadcast, secondary_broadcast) {
389+
(true, false) => Some(BroadcastMode::Primary),
390+
(false, true) => Some(BroadcastMode::Secondary),
391+
(false, false) => None,
392+
(true, true) => {
393+
return Err(MessageValidationError::InvalidBroadcastBits);
394+
}
395+
};
396+
405397
if !(MIN_MERKLE_TREE_DEPTH..=MAX_MERKLE_TREE_DEPTH).contains(&tree_depth) {
406398
return Err(MessageValidationError::InvalidTreeDepth);
407399
}
@@ -506,8 +498,7 @@ where
506498
unix_ts_ms,
507499
app_message_hash,
508500
app_message_len,
509-
broadcast,
510-
secondary_broadcast,
501+
maybe_broadcast_mode,
511502
recipient_hash,
512503
chunk_id,
513504
chunk: cursor_payload,
@@ -682,7 +673,9 @@ mod tests {
682673
use super::{MessageValidationError, UdpState};
683674
use crate::{
684675
udp::{build_messages, parse_message, SIGNATURE_CACHE_SIZE},
685-
util::{BuildTarget, EpochValidators, Group, ReBroadcastGroupMap, Redundancy},
676+
util::{
677+
BroadcastMode, BuildTarget, EpochValidators, Group, ReBroadcastGroupMap, Redundancy,
678+
},
686679
};
687680

688681
type SignatureType = SecpSignature;
@@ -762,7 +755,10 @@ mod tests {
762755
assert_eq!(parsed_message.message, message);
763756
assert_eq!(parsed_message.app_message_hash.0, app_message_hash.0[..20]);
764757
assert_eq!(parsed_message.unix_ts_ms, UNIX_TS_MS);
765-
assert!(parsed_message.broadcast);
758+
assert!(matches!(
759+
parsed_message.maybe_broadcast_mode,
760+
Some(BroadcastMode::Primary)
761+
));
766762
assert_eq!(parsed_message.app_message_len, app_message.len() as u32);
767763
assert_eq!(parsed_message.author, NodeId::new(key.pubkey()));
768764
}
@@ -897,12 +893,16 @@ mod tests {
897893

898894
match build_target {
899895
BuildTarget::Raptorcast(_) => {
900-
assert!(parsed_message.broadcast);
901-
assert!(!parsed_message.secondary_broadcast);
896+
assert!(matches!(
897+
parsed_message.maybe_broadcast_mode,
898+
Some(BroadcastMode::Primary)
899+
));
902900
}
903901
BuildTarget::FullNodeRaptorCast(_) => {
904-
assert!(!parsed_message.broadcast);
905-
assert!(parsed_message.secondary_broadcast);
902+
assert!(matches!(
903+
parsed_message.maybe_broadcast_mode,
904+
Some(BroadcastMode::Secondary)
905+
));
906906
}
907907
_ => unreachable!(),
908908
}

0 commit comments

Comments
 (0)