@@ -30,7 +30,7 @@ use monad_dataplane::{
3030} ;
3131use monad_merkle:: { MerkleHash , MerkleProof } ;
3232use monad_types:: { Epoch , NodeId } ;
33- use tracing:: { debug , warn} ;
33+ use tracing:: warn;
3434
3535pub use crate :: packet:: build_messages;
3636use 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