@@ -55,14 +55,14 @@ use crate::handler::{GossipsubHandler, HandlerEvent};
55
55
use crate :: mcache:: MessageCache ;
56
56
use crate :: peer_score:: { PeerScore , PeerScoreParams , PeerScoreThresholds , RejectReason } ;
57
57
use crate :: protocol:: SIGNING_PREFIX ;
58
- use crate :: rpc_proto;
59
58
use crate :: time_cache:: DuplicateCache ;
60
59
use crate :: topic:: { Hasher , Topic , TopicHash } ;
61
60
use crate :: types:: {
62
61
GossipsubControlAction , GossipsubMessage , GossipsubSubscription , GossipsubSubscriptionAction ,
63
62
MessageId , PeerInfo ,
64
63
} ;
65
64
use crate :: types:: { GossipsubRpc , PeerKind } ;
65
+ use crate :: { rpc_proto, TopicScoreParams } ;
66
66
use std:: cmp:: Ordering :: Equal ;
67
67
68
68
mod tests;
@@ -344,6 +344,7 @@ impl BackoffStorage {
344
344
}
345
345
}
346
346
347
+ #[ derive( Debug ) ]
347
348
pub enum MessageAcceptance {
348
349
/// The message is considered valid, and it should be delivered and forwarded to the network
349
350
Accept ,
@@ -686,9 +687,10 @@ impl Gossipsub {
686
687
}
687
688
688
689
/// This function should be called when `config.validate_messages()` is `true` after the
689
- /// message got validated by the caller. Messages are stored in the ['Memcache'] and validation
690
- /// is expected to be fast enough that the messages should still exist in the cache.There are
691
- /// three possible validation outcomes and the outcome is given in acceptance.
690
+ /// message got validated by the caller. Messages are stored in the
691
+ /// ['Memcache'] and validation is expected to be fast enough that the messages should still
692
+ /// exist in the cache. There are three possible validation outcomes and the outcome is given
693
+ /// in acceptance.
692
694
///
693
695
/// If acceptance = Accept the message will get propagated to the network. The
694
696
/// `propagation_source` parameter indicates who the message was received by and will not
@@ -704,7 +706,7 @@ impl Gossipsub {
704
706
/// in the cache anymore.
705
707
///
706
708
/// This should only be called once per message.
707
- pub fn validate_message (
709
+ pub fn report_message_validation_result (
708
710
& mut self ,
709
711
message_id : & MessageId ,
710
712
propagation_source : & PeerId ,
@@ -730,14 +732,7 @@ impl Gossipsub {
730
732
} ;
731
733
732
734
if let Some ( message) = self . mcache . remove ( message_id) {
733
- //tell peer_score and gossip promises about reject
734
- Self :: reject_message (
735
- & mut self . peer_score ,
736
- propagation_source,
737
- & message,
738
- message_id,
739
- reject_reason,
740
- ) ;
735
+ //tell peer_score about reject
741
736
if let Some ( ( peer_score, ..) ) = & mut self . peer_score {
742
737
peer_score. reject_message ( propagation_source, & message, reject_reason) ;
743
738
}
@@ -782,6 +777,12 @@ impl Gossipsub {
782
777
Ok ( ( ) )
783
778
}
784
779
780
+ pub fn set_topic_params ( & mut self , topic_hash : TopicHash , params : TopicScoreParams ) {
781
+ if let Some ( ( peer_score, ..) ) = & mut self . peer_score {
782
+ peer_score. set_topic_params ( topic_hash, params) ;
783
+ }
784
+ }
785
+
785
786
/// Sets the application specific score for a peer. Returns true if scoring is active and
786
787
/// the peer is connected or if the score of the peer is not yet expired, false otherwise.
787
788
pub fn set_application_score ( & mut self , peer_id : & PeerId , new_score : f64 ) -> bool {
@@ -1315,20 +1316,6 @@ impl Gossipsub {
1315
1316
}
1316
1317
}
1317
1318
1318
- /// informs peer score and gossip_promises about a rejected message
1319
- fn reject_message (
1320
- peer_score : & mut Option < ( PeerScore , PeerScoreThresholds , Interval , GossipPromises ) > ,
1321
- from : & PeerId ,
1322
- msg : & GossipsubMessage ,
1323
- id : & MessageId ,
1324
- reason : RejectReason ,
1325
- ) {
1326
- if let Some ( ( peer_score, _, _, gossip_promises) ) = peer_score {
1327
- peer_score. reject_message ( from, & msg, reason) ;
1328
- gossip_promises. reject_message ( id, & reason) ;
1329
- }
1330
- }
1331
-
1332
1319
/// Handles a newly received GossipsubMessage.
1333
1320
/// Forwards the message to all peers in the mesh.
1334
1321
fn handle_received_message ( & mut self , mut msg : GossipsubMessage , propagation_source : & PeerId ) {
@@ -1348,18 +1335,21 @@ impl Gossipsub {
1348
1335
1349
1336
// reject messages claiming to be from ourselves but not locally published
1350
1337
if let Some ( own_id) = self . publish_config . get_own_id ( ) {
1351
- if own_id != propagation_source && msg. source . as_ref ( ) . map_or ( false , |s| s == own_id) {
1338
+ //TODO remove this "hack" as soon as lighthouse uses Anonymous instead of this fixed
1339
+ // PeerId.
1340
+ let lighthouse_anonymous_id = PeerId :: from_bytes ( vec ! [ 0 , 1 , 0 ] ) . expect ( "Valid peer id" ) ;
1341
+ if own_id != & lighthouse_anonymous_id
1342
+ && own_id != propagation_source
1343
+ && msg. source . as_ref ( ) . map_or ( false , |s| s == own_id)
1344
+ {
1352
1345
debug ! (
1353
- "Dropping message claiming to be from self but forwarded from {:?}" ,
1354
- propagation_source
1355
- ) ;
1356
- Self :: reject_message (
1357
- & mut self . peer_score ,
1358
- propagation_source,
1359
- & msg,
1360
- & msg_id,
1361
- RejectReason :: SelfOrigin ,
1346
+ "Dropping message {:?} claiming to be from self but forwarded from {:?}" ,
1347
+ msg_id, propagation_source
1362
1348
) ;
1349
+ if let Some ( ( peer_score, _, _, gossip_promises) ) = & mut self . peer_score {
1350
+ peer_score. reject_message ( propagation_source, & msg, RejectReason :: SelfOrigin ) ;
1351
+ gossip_promises. reject_message ( & msg_id, & RejectReason :: SelfOrigin ) ;
1352
+ }
1363
1353
return ;
1364
1354
}
1365
1355
}
@@ -1374,8 +1364,10 @@ impl Gossipsub {
1374
1364
}
1375
1365
1376
1366
//tells score that message arrived (but is maybe not fully validated yet)
1377
- if let Some ( ( peer_score, ..) ) = & mut self . peer_score {
1367
+ //Consider message as delivered for gossip promises
1368
+ if let Some ( ( peer_score, .., gossip_promises) ) = & mut self . peer_score {
1378
1369
peer_score. validate_message ( propagation_source, & msg) ;
1370
+ gossip_promises. deliver_message ( & msg_id) ;
1379
1371
}
1380
1372
1381
1373
self . mcache . put ( msg. clone ( ) ) ;
@@ -1885,6 +1877,7 @@ impl Gossipsub {
1885
1877
self . mcache . shift ( ) ;
1886
1878
1887
1879
debug ! ( "Completed Heartbeat" ) ;
1880
+ debug ! ( "peer_scores: {:?}" , scores) ;
1888
1881
}
1889
1882
1890
1883
/// Emits gossip - Send IHAVE messages to a random set of gossip peers. This is applied to mesh
@@ -2032,12 +2025,11 @@ impl Gossipsub {
2032
2025
fn forward_msg ( & mut self , message : GossipsubMessage , source : Option < & PeerId > ) -> bool {
2033
2026
let msg_id = ( self . config . message_id_fn ( ) ) ( & message) ;
2034
2027
2035
- //message is fully validated, inform peer_score and gossip promises
2028
+ //message is fully validated inform peer_score
2036
2029
if let Some ( ( peer_score, _, _, gossip_promises) ) = & mut self . peer_score {
2037
2030
if let Some ( peer) = source {
2038
2031
peer_score. deliver_message ( peer, & message) ;
2039
2032
}
2040
- gossip_promises. deliver_message ( & msg_id) ;
2041
2033
}
2042
2034
2043
2035
debug ! ( "Forwarding message: {:?}" , msg_id) ;
@@ -2058,7 +2050,7 @@ impl Gossipsub {
2058
2050
//add explicit peers
2059
2051
for p in & self . explicit_peers {
2060
2052
if let Some ( topics) = self . peer_topics . get ( p) {
2061
- if message. topics . iter ( ) . any ( |t| topics. contains ( t) ) {
2053
+ if Some ( p ) != source && message. topics . iter ( ) . any ( |t| topics. contains ( t) ) {
2062
2054
recipient_peers. insert ( p. clone ( ) ) ;
2063
2055
}
2064
2056
}
@@ -2475,10 +2467,12 @@ impl NetworkBehaviour for Gossipsub {
2475
2467
invalid_messages,
2476
2468
} => {
2477
2469
// Handle any invalid messages from this peer
2478
- if let Some ( ( peer_score, ..) ) = & mut self . peer_score {
2470
+ if let Some ( ( peer_score, .., gossip_promises) ) = & mut self . peer_score {
2471
+ let mut id_fn = self . config . message_id_fn ( ) ;
2479
2472
for ( _message, validation_error) in invalid_messages {
2480
2473
let reason = RejectReason :: ProtocolValidationError ( validation_error) ;
2481
2474
peer_score. reject_message ( & propagation_source, & _message, reason) ;
2475
+ gossip_promises. reject_message ( & id_fn ( & _message) , & reason) ;
2482
2476
}
2483
2477
}
2484
2478
0 commit comments