From 9bd5b0ef361f321c44c29af9ffdadcbb65a84281 Mon Sep 17 00:00:00 2001 From: Ilias Garnier Date: Thu, 27 Apr 2023 10:29:18 +0200 Subject: [PATCH] Gossipsub: add optional topic score cap --- src/lib_gossipsub/gossipsub_intf.ml | 8 +++++++- src/lib_gossipsub/peers_score.ml | 6 +++++- src/lib_gossipsub/test/test_gossipsub.ml | 1 + src/lib_gossipsub/tezos_gossipsub.ml | 1 + 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/lib_gossipsub/gossipsub_intf.ml b/src/lib_gossipsub/gossipsub_intf.ml index d20d33b54c9e..572d0adb8435 100644 --- a/src/lib_gossipsub/gossipsub_intf.ml +++ b/src/lib_gossipsub/gossipsub_intf.ml @@ -163,10 +163,16 @@ type ('topic, 'span) topic_score_parameters = (* N.B.: unlike the Go/Rust implementations, scores are not refreshed in an asynchronous loop but rather in the heartbeat. Hence, we do not have the [decay_interval] parameter. - Scores are refreshed and decayed every [score_cleanup_ticks] heartbeats (see [limits]). *) + Scores are refreshed and decayed every [score_cleanup_ticks] heartbeats (see [limits]). + + TODO: https://gitlab.com/tezos/tezos/-/issues/5545 + We did not implement P6, aka IP colocation factor *) type ('topic, 'span) score_parameters = { topics : ('topic, 'span) topic_score_parameters; (** Per-topic score parameters. *) + topic_score_cap : float option; + (** An optional cap on the total positive contribution of topics to the score of the peer. + If not equal to [None], must be non-negative. *) behaviour_penalty_weight : float; (** P7: The weight of the score associated to the behaviour penalty. This parameter must be negative. *) diff --git a/src/lib_gossipsub/peers_score.ml b/src/lib_gossipsub/peers_score.ml index e892f67d2b7e..4e81d391695f 100644 --- a/src/lib_gossipsub/peers_score.ml +++ b/src/lib_gossipsub/peers_score.ml @@ -178,7 +178,11 @@ struct else 0.0 let float ps = - let topic_scores = topic_scores ps in + let topic_scores = + match ps.parameters.topic_score_cap with + | None -> topic_scores ps + | Some cap -> Float.min cap (topic_scores ps) + in let p5 = p5 ps in let p7 = p7 ps in topic_scores +. p5 +. p7 diff --git a/src/lib_gossipsub/test/test_gossipsub.ml b/src/lib_gossipsub/test/test_gossipsub.ml index 9a1067f07490..458cc6fb834f 100644 --- a/src/lib_gossipsub/test/test_gossipsub.ml +++ b/src/lib_gossipsub/test/test_gossipsub.ml @@ -51,6 +51,7 @@ let per_topic_score_parameters = let score_parameters = { topics = per_topic_score_parameters; + topic_score_cap = Some 3600.; behaviour_penalty_weight = ~-.10.0; behaviour_penalty_threshold = 0.0; behaviour_penalty_decay = 0.2; diff --git a/src/lib_gossipsub/tezos_gossipsub.ml b/src/lib_gossipsub/tezos_gossipsub.ml index da5dedf0dbd1..6e88f1ab12b6 100644 --- a/src/lib_gossipsub/tezos_gossipsub.ml +++ b/src/lib_gossipsub/tezos_gossipsub.ml @@ -260,6 +260,7 @@ module Make (C : AUTOMATON_CONFIG) : | Topic_score_parameters_family {all_topics; parameters; weights = _} -> Seq.map parameters all_topics |> Seq.iter check_per_topic_score_parameters) ; + Option.iter (fun cap -> assert (cap >= 0.0)) sp.topic_score_cap ; assert (sp.behaviour_penalty_weight <= 0.0) ; assert (sp.behaviour_penalty_threshold >= 0.0) ; assert_in_unit_interval sp.behaviour_penalty_decay ; -- GitLab